0

I have a GLSurfaceView and a Renderer which loads textures in onSurfaceCreated. My textures are created like so :

public Texture3D(final GL10 gl, final int id) {
    _pBitmap = BitmapFactory.decodeResource(Utils.getResources(), id);
    gl.glEnable(GL10.GL_TEXTURE_2D);

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    texture = newTextureID(gl);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);


    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, _pBitmap, 0);

    _pBitmap.recycle();     
    _pBitmap = null;

    gl.glEnable(GL10.GL_BLEND);
}

I store them in an HashMap :

textures.put(R.drawable.tile, new Texture3D(gl, R.drawable.tile));

My problem is when I create a texture from a large image (720x1280, 561 Ko) after I sometimes get the following error :

04-24 11:05:19.870: D/dalvikvm(27953): GC_CONCURRENT freed 26K, 18% free 50397K/60743K, paused 18ms+6ms, total 57ms
04-24 11:05:19.870: D/dalvikvm(27953): WAIT_FOR_CONCURRENT_GC blocked 2ms
04-24 11:05:19.895: D/dalvikvm(27953): GC_FOR_ALLOC freed 7K, 18% free 50390K/60743K, paused 25ms, total 25ms
04-24 11:05:19.900: I/dalvikvm-heap(27953): Forcing collection of SoftReferences for 14745616-byte allocation
04-24 11:05:19.940: D/dalvikvm(27953): GC_BEFORE_OOM freed 10K, 18% free 50380K/60743K, paused 41ms, total 41ms
04-24 11:05:19.940: E/dalvikvm-heap(27953): Out of memory on a 14745616-byte allocation.
04-24 11:05:19.940: I/dalvikvm(27953): "GLThread 11210" prio=5 tid=35 RUNNABLE
04-24 11:05:19.940: I/dalvikvm(27953):   | group="main" sCount=0 dsCount=0 obj=0x42ec2008 self=0x6095d078
04-24 11:05:19.940: I/dalvikvm(27953):   | sysTid=29199 nice=0 sched=0/0 cgrp=apps handle=1623155456
04-24 11:05:19.940: I/dalvikvm(27953):   | schedstat=( 142147207 17456123 110 ) utm=11 stm=2 core=2
04-24 11:05:19.940: I/dalvikvm(27953):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-24 11:05:19.945: I/dalvikvm(27953):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
04-24 11:05:19.945: I/dalvikvm(27953):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:476)
04-24 11:05:19.945: I/dalvikvm(27953):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:499)
04-24 11:05:19.945: I/dalvikvm(27953):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:529)
04-24 11:05:19.945: I/dalvikvm(27953):   at com.gbanga.opengl.Texture3D.<init>(Texture3D.java:73)
04-24 11:05:19.945: I/dalvikvm(27953):   at com.gbanga.opengl.Texture3D.setupTextures(Texture3D.java:169)
04-24 11:05:19.945: I/dalvikvm(27953):   at com.gbanga.opengl.OpenGLRenderer.onSurfaceCreated(OpenGLRenderer.java:440)
04-24 11:05:19.950: I/dalvikvm(27953):   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1494)
04-24 11:05:19.950: I/dalvikvm(27953):   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

That points to the first line of the constructor where I use BitmapFactory.decodeResource. On my Galaxy S3 I get the error when I switch to another fragment and then I come back to the fragment that displays the GLSurfaceView (onPause and onResume are called on the GLSurfaceView).

How can I avoid this problem ? I tried this solution but I lose too much quality and in rare case the bug happens anyway (probably on old phones models).

Is there something wrong how I create / store my textures ? Also do you know why I don't always get this error ? (generally I get it the second time I load the textures).

4

3 回答 3

2

您是否熟悉: http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html 较新的 Android 具有清单参数:largeHeap=true

于 2013-04-25T07:48:36.497 回答
0

您可以尝试通过从 Android 虚拟设备管理器编辑 AVD 来增加 VM 堆的大小...

于 2013-04-25T07:21:25.450 回答
0

如果您的应用程序将处理大量大型纹理,并且如果您想支持旧型号的手机,那么在本机端进行 opengl 处理可能更有利。

NDK 中的 hello-gl2 示例显示了如何做到这一点。在本机方面,您将不受应用程序虚拟机强制执行的限制(但是,您应该确保正确管理内存并且不会导致任何泄漏,因为即使用户关闭您的活动,该进程也会继续运行)

于 2013-04-25T08:30:05.213 回答