在glThread上运行长任务而不阻塞Android上的UI线程

Ant*_*tzi 5 multithreading android opengl-es surfaceview

在我可以渲染任何内容之前,我必须经过大量的初始化 GLSurfaceView

这些必须在OpenGL线程上完成。

但是,这在初始化期间挂了我的主线程。

这是我的代码:

@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);
        /* [...] */
    }
Run Code Online (Sandbox Code Playgroud)

sol*_*xel 0

通常的答案是在同一个共享组中创建两个 EGL 上下文,每个上下文绑定到一个单独的线程。

渲染循环中的主线程对屏幕进行任何渲染,这在资源加载期间通常是一些“无聊”的东西 - 即加载图形、进度条等。

第二个线程在后台加载任何批量资源,例如加载纹理文件、模型网格等并上传它们。

一旦加载完成,主线程就可以使用辅助异步加载线程加载的所有数据资源。