Running a lengthy task on glThread without blocking the user interface thread on Android

I need to do a lot of initialization before I can do anything in my GLSurfaceView

Abstracts must be completed in the OpenGL thread.

However, this freezes my main thread during initialization.

Here is my code:

@Override
protected void onStart() {
    super.onStart();
    FrameLayout renderingLayout = (FrameLayout) findViewById(R.id.movie_rendering_layout);
    if (renderingLayout != null && mGLView == null) {
        mGLView = new MyGLSurfaceView(getApplicationContext());
        /** [..] **/
        renderingLayout.addView(mGLView, params);
    }
}

/*--------------- OPENGL RELATED ---------------*/

protected class MyGLSurfaceView  extends GLSurfaceView {

    private final MyGLRenderer mRenderer;

    public MyGLSurfaceView(Context context) {
        super(context);
        // Create an OpenGL ES 1.0 context
        setEGLContextClientVersion(1);
        mRenderer = new MyGLRenderer();
        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(mRenderer);
    }
}


protected class MyGLRenderer implements GLSurfaceView.Renderer {
    private int mWidth, mHeight = 0;
    private boolean isFinished = false;


    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
       GLES10.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        init(mMovieIndex, AssetsUtils.getBinPath(getApplicationContext())); // <----- THIS takes long time

    }

    public void onDrawFrame(GL10 pGL10) {

        GLES10.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GLES10.glClear(GLES10.GL_COLOR_BUFFER_BIT);
        /* [...] */
    }
+4
source share
2 answers

The usual answer is to create two EGL contexts in the same stock group, each of which is tied to a separate stream.

, - "", , ..

, . , .. .

, .

0

:

, onDrawFrame onSurfaceCreated, .

, :

setRenderMode(RENDERMODE_WHEN_DIRTY);

, onDrawFrame , .

public void onWindowFocusChanged(boolean hasFocus) 

, . - , ( ?)

@Override
public boolean isDirty()
   return false;
}

queueEvent GLThread

+2

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


All Articles