Skip to content

Instantly share code, notes, and snippets.

@HugoGresse
Created May 28, 2015 09:57
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HugoGresse/5ca05821444353a823bb to your computer and use it in GitHub Desktop.
Save HugoGresse/5ca05821444353a823bb to your computer and use it in GitHub Desktop.
ClearSurface API16
private void clearSurface(SurfaceTexture texture) {
EGL10 egl = (EGL10) EGLContext.getEGL();
EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
egl.eglInitialize(display, null);
int[] attribList = {
EGL10.EGL_RED_SIZE, 8,
EGL10.EGL_GREEN_SIZE, 8,
EGL10.EGL_BLUE_SIZE, 8,
EGL10.EGL_ALPHA_SIZE, 8,
EGL10.EGL_RENDERABLE_TYPE, EGL10.EGL_WINDOW_BIT,
EGL10.EGL_NONE, 0, // placeholder for recordable [@-3]
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
egl.eglChooseConfig(display, attribList, configs, configs.length, numConfigs);
EGLConfig config = configs[0];
EGLContext context = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, new int[]{
12440, 2,
EGL10.EGL_NONE
});
EGLSurface eglSurface = egl.eglCreateWindowSurface(display, config, texture,
new int[]{
EGL10.EGL_NONE
});
egl.eglMakeCurrent(display, eglSurface, eglSurface, context);
GLES20.glClearColor(0, 0, 0, 1);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
egl.eglSwapBuffers(display, eglSurface);
egl.eglDestroySurface(display, eglSurface);
egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE,
EGL10.EGL_NO_CONTEXT);
egl.eglDestroyContext(display, context);
egl.eglTerminate(display);
}
@kaidul
Copy link

kaidul commented Jan 8, 2016

Great! It works :)

@SlyOtis
Copy link

SlyOtis commented Aug 2, 2018

Fantastic!

@nambmt97s
Copy link

the best solution for clear surfaceView :)

@andreyshmelev
Copy link

Defenitly works for clearing SurfaceView,
isung like that:

if (binding.surfaceView.getHolder().getSurface().isValid()) {
            try {
                clearSurface(binding.surfaceView);
            }catch (RuntimeException re){
                re.printStackTrace();
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment