Android: GLSurfaceView black after resuming application

According to the title, GLSurfaceView is empty after resuming from a paused state. Renderer onSurfaceCreated , onSurfaceChanged and onDrawFrame is called after it is resumed, but the screen is still blank!

Here is the relevant code:

 @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL10.GL_SMOOTH); gl.glClearDepthf(1.0f); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glDepthFunc(GL10.GL_LEQUAL); boxWidth = 0.5f; boxHeight = 0.5f; boxCenter = new float[] { 0.5f, 0.5f }; Log.v("resume", "onSurfaceCreated"); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrthof(0, 1, 0, 1, -5, 5); gl.glMatrixMode(GL10.GL_MODELVIEW); viewHeight = height; viewWidth = width; Log.v("resume", "onSurfaceChanged"); } @Override public void onDrawFrame(GL10 gl) { gl.glLoadIdentity(); float halfW = boxWidth/2.0f; float halfH = boxHeight/2.0f; float left = Math.max(boxCenter[0]-halfW, 0.001f); float right = Math.min(boxCenter[0]+halfW, 0.999f); float top = Math.min(boxCenter[1]+halfH, 0.999f); float bottom = Math.max(boxCenter[1]-halfH, 0.001f); boxHeight = Math.max(top - bottom, 0.05f); boxWidth = Math.max(right - left, 0.05f); boxCenter[0] = left + boxWidth/2.0f; boxCenter[1] = bottom + boxHeight/2.0f; float vertices[] = { left, top, 0.0f, left, bottom, 0.0f, right, bottom, 0.0f, right, top, 0.0f }; ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); byteBuf.order(ByteOrder.nativeOrder()); vertexBuffer = byteBuf.asFloatBuffer(); vertexBuffer.put(vertices); gl.glTranslatef(0.001f, -0.001f, -3.0f); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); vertexBuffer.position(0); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); Log.v("resume", "drawing"); } 

I read that you need to recreate the GL context inside onSurfaceChanged, but I'm not sure how, or if I already did this when calling onSurfaceCreated.

Please, help!

Edit: Here is the onResume code from the Activity that contains GLSurfaceView: (here called GLSurface)

 public void onResume() { super.onResume(); if (mGLSurface != null) { if (mRenderer != null) { float[] center = new float[2]; center[0] = settings.getFloat("renderer_boxCenter0", 0.5f); center[1] = settings.getFloat("renderer_boxCenter1", 0.5f); mRenderer.setBoxCenter(center); mRenderer.setBoxWidth(settings.getFloat("renderer_boxWidth", 0.5f)); mRenderer.setBoxHeight(settings.getFloat("renderer_boxHeight", 0.5f)); } mGLSurface.onResume(); } 

I didn’t do much in onResume for GLSurfaceView, and I did not find any documentation that suggested that I needed to do anything, in particular, to return my EGL context.

Edit 2: I would also like to note that calling setPreserveEGLContextOnPause(true) in the constructor of my GLSurfaceView, unfortunately, did not solve my problem.

+4
source share
2 answers

setPreserveEGLContextOnPause (true) is only supported on devices with Android 3.X and higher. (even then it depends on the device whether it is supported)

The solution you are looking for is in the parent activity of GlSurfaceView, you call the GlSurfaceView.onPause () method in the onPause method of activity and GlSurfaceView.onResume () in the onResume method of activity.

Note that all your textures and buffers are lost. You need to restart them at this moment.

+3
source

This is a slightly long shot, but are you trying to control orientation or configuration changes (i.e. do you have public void onConfigurationChanged(Configuration config) in overriding your activity)?

I found that if I have onConfigurationChanges() overridden in my activity and android:screenOrientation="landscape" in my manifest file, but not android:configChanges="orientation" , I get something like what you are describing

0
source

Source: https://habr.com/ru/post/1381825/


All Articles