2

作为我之前的问题,我正在尝试“ GLSurfaceView + TextureView”在一个 GLSurfaceView 和多个 TextureView 中显示相机预览,但面临一些问题......

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

有谁知道为什么?有什么办法可以解决这个问题?
谢谢。

4

0 回答 0