eglSwapBuffers在GLSurfaceView onDrawFrame中被阻止

时间:2014-12-25 14:21:12

标签: android opengl-es glsurfaceview egl textureview

作为我的previous question,我正在尝试" GLSurfaceView + TextureView"在一个GLSurfaceView和多个TextureViews中显示相机预览,但面临一些问题......

在GLSurfaceView渲染线程中,我尝试将内置的EGLContext共享到TextureView,通过TextureView的surfaceTexture创建一个EGL表面,然后使用GLES在其上绘制。

    @Override
    public void onDrawFrame(final GL10 gl) {
        // GLES draw on GLSurfaceView
        renderToTextureView();
    }

    private void renderToTextureView() {
        saveEGLState();
        for(TextureViewItem item : mTextureViewItemList) {
            item.render(mSavedEglContext);
        }
        restoreEGLState();
    }

    private void saveEGLState() {
        mSavedEglDisplay = EGL14.eglGetCurrentDisplay();
        mSavedEglContext = EGL14.eglGetCurrentContext();
        mSavedEglDrawSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
        mSavedEglReadSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_READ);
    }

    private void restoreEGLState() {
        if (!EGL14.eglMakeCurrent(mSavedEglDisplay, mSavedEglDrawSurface, mSavedEglReadSurface, mSavedEglContext)) {
            throw new RuntimeException("eglMakeCurrent failed");
        }
    }

    public class TextureViewItem implements TextureView.SurfaceTextureListener {
        private static EglCore sEglCore;
        private WindowSurface mWindowSurface;

        public void render(EGLContext sharedContext) {
            if(mSavedSurfaceTexture == null) return;
            getWindowSurface(sharedContext).makeCurrent();
            // GLES draw on TextureView
            getWindowSurface(sharedContext).swapBuffers();
        }

        private WindowSurface getWindowSurface(EGLContext sharedContext) {
            if(sEglCore == null) {
                sEglCore = new EglCore(sharedContext, EglCore.FLAG_TRY_GLES3);
            }
            if(mWindowSurface == null) {
                mWindowSurface = new WindowSurface(mEglCore, mSavedSurfaceTexture);
            }
            return mWindowSurface;
        }

        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture st, int width, int height) {
            if (mSavedSurfaceTexture == null) {
                mSavedSurfaceTexture = st;
            }
        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture st) {       
            if (mWindowSurface != null) {
                mWindowSurface.release();
            }
            if (sEglCore != null) {
                sEglCore.release();
            }

            mSavedSurfaceTexture = null;
            return true;
        }
    }

一切正常,除了按"返回"键。当活动暂停时,我调用GLSurfaceView的onPause(),导致swapBuffers(EGL14.eglSwapBuffers)不会返回...

一些可疑的logcat消息也是 W/WindowManager(1077): Window freeze timeout expired.
I/WindowManager(1077): Screen frozen for +2s42ms due to Window ..

任何人都知道为什么?有什么方法可以解决这个问题吗? 感谢。

0 个答案:

没有答案