Code example for EGL10

Methods: eglChooseConfig, eglCreateContext, eglCreateWindowSurface, eglGetDisplay, eglInitialize, eglMakeCurrent

0
            mEgl.eglSwapBuffers(mEglDisplay, mEglSurface); 
        } 
 
        private void doInitGL() { 
            // These are copied from GLSurfaceView 
            mEgl = (EGL10) EGLContext.getEGL(); 
            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); 
            if (mEglDisplay == EGL10.EGL_NO_DISPLAY) { 
                throw new RuntimeException("eglGetDisplay failed"); 
            } 
            int[] version = new int[2]; 
            if (!mEgl.eglInitialize(mEglDisplay, version)) { 
                throw new RuntimeException("eglInitialize failed"); 
            } else { 
                Log.v(TAG, "EGL version: " + version[0] + '.' + version[1]); 
            } 
            int[] attribList = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; 
            mEglConfig = chooseConfig(mEgl, mEglDisplay); 
            mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig, EGL10.EGL_NO_CONTEXT, 
                                                attribList); 
 
            if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) { 
                throw new RuntimeException("failed to createContext"); 
            } 
            mEglSurface = mEgl.eglCreateWindowSurface( 
                    mEglDisplay, mEglConfig, mMosaicOutputSurfaceTexture, null); 
            if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) { 
                throw new RuntimeException("failed to createWindowSurface"); 
            } 
 
            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) { 
                throw new RuntimeException("failed to eglMakeCurrent"); 
            } 
 
            mGl = (GL10) mEglContext.getGL(); 
 
            mInputSurfaceTexture = new SurfaceTexture(MosaicRenderer.init()); 
            MosaicRenderer.reset(mWidth, mHeight, mIsLandscape); 
        } 
 
        private void doRelease() { 
            mEgl.eglDestroySurface(mEglDisplay, mEglSurface); 
            mEgl.eglDestroyContext(mEglDisplay, mEglContext); 
            mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, 
                    EGL10.EGL_NO_CONTEXT); 
            mEgl.eglTerminate(mEglDisplay); 
            mEglSurface = null; 
            mEglContext = null; 
            mEglDisplay = null; 
            releaseSurfaceTexture(mInputSurfaceTexture); 
            mEglThread.quit(); 
        } 
 
        @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH) 
        private void releaseSurfaceTexture(SurfaceTexture st) { 
            if (ApiHelper.HAS_RELEASE_SURFACE_TEXTURE) { 
                st.release(); 
            } 
        } 
 
        // Should be called from other thread. 
        public void sendMessageSync(int msg) { 
            mEglThreadBlockVar.close(); 
            sendEmptyMessage(msg); 
            mEglThreadBlockVar.block(); 
        } 
 
    } 
 
    public MosaicPreviewRenderer(SurfaceTexture tex, int w, int h, boolean isLandscape) { 
        mMosaicOutputSurfaceTexture = tex; 
        mWidth = w; 
        mHeight = h; 
        mIsLandscape = isLandscape; 
 
        mEglThread = new HandlerThread("PanoramaRealtimeRenderer"); 
        mEglThread.start(); 
        mEglHandler = new EGLHandler(mEglThread.getLooper()); 
 
        // We need to sync this because the generation of surface texture for input is 
        // done here and the client will continue with the assumption that the 
        // generation is completed. 
        mEglHandler.sendMessageSync(EGLHandler.MSG_INIT_EGL_SYNC); 
    } 
 
    public void release() { 
        mEglHandler.sendMessageSync(EGLHandler.MSG_RELEASE); 
    } 
 
    public void showPreviewFrameSync() { 
        mEglHandler.sendMessageSync(EGLHandler.MSG_SHOW_PREVIEW_FRAME_SYNC); 
    } 
 
    public void showPreviewFrame() { 
        mEglHandler.sendEmptyMessage(EGLHandler.MSG_SHOW_PREVIEW_FRAME); 
    } 
 
    public void alignFrameSync() { 
        mEglHandler.sendMessageSync(EGLHandler.MSG_ALIGN_FRAME_SYNC); 
    } 
 
    public SurfaceTexture getInputSurfaceTexture() { 
        return mInputSurfaceTexture; 
    } 
 
    private void draw() { 
        MosaicRenderer.step(); 
    } 
 
    private static void checkEglError(String prompt, EGL10 egl) { 
        int error; 
        while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) { 
            Log.e(TAG, String.format("%s: EGL error: 0x%x", prompt, error)); 
        } 
    } 
 
    private static final int EGL_OPENGL_ES2_BIT = 4; 
    private static final int[] CONFIG_SPEC = new int[] { 
            EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 
            EGL10.EGL_RED_SIZE, 8, 
            EGL10.EGL_GREEN_SIZE, 8, 
            EGL10.EGL_BLUE_SIZE, 8, 
            EGL10.EGL_NONE 
    }; 
 
    private static EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { 
        int[] numConfig = new int[1]; 
        if (!egl.eglChooseConfig(display, CONFIG_SPEC, null, 0, numConfig)) { 
            throw new IllegalArgumentException("eglChooseConfig failed"); 
        } 
 
        int numConfigs = numConfig[0]; 
        if (numConfigs <= 0) { 
            throw new IllegalArgumentException("No configs match configSpec"); 
        } 
 
        EGLConfig[] configs = new EGLConfig[numConfigs]; 
        if (!egl.eglChooseConfig( 
                display, CONFIG_SPEC, configs, numConfigs, numConfig)) { 
            throw new IllegalArgumentException("eglChooseConfig#2 failed"); 
        }