0

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() }
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
DualGlad
  • 1
  • 4
  • Possible duplicate of [Is it normal for the "activity.onCreate()" method to be called multiple times](https://stackoverflow.com/questions/3588682/is-it-normal-for-the-activity-oncreate-method-to-be-called-multiple-times) – muradm Nov 11 '18 at 20:03
  • @muradm, my _Activity.onCreate()_ works fine (called only once, according to Log), the question is about _GLSurfaceView_ lifecycle. – DualGlad Nov 11 '18 at 20:09
  • According to log everything is called twice. And your `GLSurfaceView` is initialized within `onCreate()` method. So if it is called twice, then `onCreate()` is called twice. Otherwise you are initializing `GLSurfaceView` also somewhere else, which is not illustrated in code snippets. – muradm Nov 11 '18 at 20:15
  • @muradm, for logging I used "func_name() **{**" at the beginning ot _func_name()_ and "func_name() **}**" at the end of it. It shows that new _onDrawFrame()_ is called only after the end of previous one. – DualGlad Nov 11 '18 at 20:27
  • @muradm, added an example of Logging. Each function should be printed 2 times – DualGlad Nov 11 '18 at 20:33
  • Possible duplicate of https://stackoverflow.com/questions/30034277/onsurfacechanged-called-twice – muradm Nov 11 '18 at 20:35

1 Answers1

0

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).

DualGlad
  • 1
  • 4