Why is there no onSurfaceDestroyed method in GLSurfaceView.Renderer?

I am working on an Android application that performs OpenCL / OpenGL transitions on a camera screen. I am using GLSurfaceView.Renderer. Naturally, the code for creating and initializing the OpenCL working environment (from OpenGL) is called from onSurfaceCreated, and the actual processing of each preview frame occurs in onDrawFrame.

Everything works well, except when I'm finished, I want to clear OpenCL stuff. Ideally, the onSurfaceDestroyed method would be the perfect place to clean, but there is no such method in GLSurfaceView.Renderer. Thus, the cleanup code has nowhere to go, and there is probably a memory leak in my application.

Here are my questions:

  • Why is there no onSurfaceDestroyed method in GLSurfaceView.Renderer? There are onSurfaceCreated and onSurfaceChanged. One would expect that there would be a SurfaceDestroyed.

  • Given the fact that onSurfaceDestroyed does not exist in GLSurfaceView.Renderer, where should my cleanup code go and why?

+5
source share
1 answer

GLSurfaceView is a set of helper code that simplifies the use of OpenGL ES with SurfaceView. You are not required to use it to use GLES, and if you have tons of other things going at the same time, I recommend that you do not.

If you compare Grafika's β€œ show + capture camera ” complexity, which uses GLSurfaceView, β€œ continuous capture ”, which uses the usual SurfaceView, you can see that the latter requires a bunch of extra code to control the EGL and rendering stream, but it also there are fewer hoops, he does not need to deal with GLSurfaceView EGL and flow control. (Just read the comments at the top of the CameraCaptureActivity class.)

As one commenter noted, I suspect that there is no β€œon destroyed” callback because the class aggressively destroys its EGL context, so there is no need to clear GLES. Of course, it would be useful if the renderer thread had the ability to clear resources other than GLES, but that is not the case, so you should handle this through the Activity lifecycle callbacks. (At some point in the development, CameraCaptureActivity processed the camera on a rendering stream, but the lack of a reliable shutdown callback made it difficult.)

The cleanup code should probably be based on activity lifecycle callbacks. Note that they are partially separate from SurfaceView callbacks. A full explanation can be found in the doc application for architecture .

+4
source

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


All Articles