androidopengl-esglsurfaceview

Android OpenGL: GLSurfaceView.Renderer.onDrawFrame() called twice at startup


I have a simple Android application which uses OpenGL (via GLSurfaceView). Code is below:

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyGLSurfaceView(this));
    }

    class MyGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer {
        MyGLSurfaceView(Context context) {
            super(context);
            setEGLContextClientVersion(2);        // OpenGL ES 2.0
            setRenderer(this);                    // callbacks go to this class
            setRenderMode(RENDERMODE_WHEN_DIRTY); // draw on request
        }

        public void onSurfaceChanged(GL10 gl, int width, int height) { }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) { }

        public void onDrawFrame(GL10 gl) {
            /* if (startup) do_only_once(); */
            try { Thread.sleep(250); } catch (Exception ignored) { }
        }
    }
}

At the application startup onDrawFrame() (of GLSurfaceView.Renderer) called twice most of times. As far as I know, onSurfaceChanged() (of GLSurfaceView.Renderer) should call onDrawFrame(), but I don't understand why it happens several times.

I would like to compute and draw some "background" objects, that should be done only once. I use setRenderMode(RENDERMODE_WHEN_DIRTY). But onDrawFrame() is still called several times. How can I make it to be called only once? What is the reason of such repetitive calling?

Adding Logging for these functions (and also some default Activity callbacks) in the start and the end of each will result in output below. Activity lifecycle seems to work fine:

// Example of function with Logging:
protected void onCreate(Bundle savedInstanceState) {
    Log.d("__DEBUG__", "onCreate() {");
    super.onCreate(savedInstanceState);
    setContentView(new MyGLSurfaceView(this));
    Log.d("__DEBUG__", "onCreate() }");
}

22:12:46.361 __DEBUG__: onCreate() {
22:12:46.376 __DEBUG__: MyGLSurfaceView() {
22:12:46.376 __DEBUG__: MyGLSurfaceView() }
22:12:46.430 __DEBUG__: onCreate() }
22:12:46.431 __DEBUG__: onStart() {
22:12:46.431 __DEBUG__: onStart() }
22:12:46.432 __DEBUG__: onResume() {
22:12:46.433 __DEBUG__: onResume() }
22:12:46.498 __DEBUG__: onSurfaceCreated() {
22:12:46.498 __DEBUG__: onSurfaceCreated() }
22:12:46.498 __DEBUG__: onSurfaceChanged() {
22:12:46.498 __DEBUG__: onSurfaceChanged() }
22:12:46.498 __DEBUG__: onDrawFrame() {
22:12:46.748 __DEBUG__: onDrawFrame() }
22:12:46.754 __DEBUG__: onDrawFrame() {
22:12:47.004 __DEBUG__: onDrawFrame() }

Solution

  • I've studied platform code for GLSurfaceView and found that onDrawFrame() could be triggered twice, once of them in ctor.

    I created an AOSP merge request with a proposed fix which could be found here: https://android-review.googlesource.com/c/platform/frameworks/base/+/858050. However, it was "rejected" as being not too much important considering possible risks...

    The only way to handle this is to implement simple state machine with switch-case inside onDrawFrame(). Need to keep "initial" state and redraw initial elements (backgroud in my case) untill real draw request comes (first requests switches state machine to "normal" state).