/** @return from a new EGL display connection */ @NonNull public static EGLDisplay newDisplay() { final EGLDisplay result = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); if (result == EGL14.EGL_NO_DISPLAY) { logError(); throw new RuntimeException("Unable to get EGL14 display"); } else { final int[] v = new int[2]; if (!EGL14.eglInitialize(result, v, 0, v, 1)) { try { logError(); throw new RuntimeException("Unable to initialize EGL14 display"); } finally { closeDisplay(result); } } else { logDebug(getDisplayString(result) + " created (EGL" + v[0] + "" + v[1] + ")"); return result; } } }
/** * Test for {@link GLTools#setPresentationTime(EGLDisplay, EGLSurface, long)} . * @throws Exception by some fails */ @Test public final void testSetPresentationTime() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); final int txt = GLTools.newTexture(TEXTURE_LEVEL); final SurfaceTexture surfaceTexture = new SurfaceTexture(txt, true); final Surface surface = new Surface(surfaceTexture); final EGLSurface window = GLTools.newSurface(eglDisplay, eglConfig, surface); GLTools.setPresentationTime(eglDisplay, window, PRESENTATION_TIME); GLTools.closeSurface(eglDisplay, window); surface.release(); surfaceTexture.release(); GLTools.closeTexture(txt, TEXTURE_LEVEL); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newTexture(int)} and {@link GLTools#closeTexture(int, int)}. * @throws Exception by some fails */ @Test public final void testTexture() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); GLTools.closeTexture(GLTools.newTexture(TEXTURE_LEVEL), TEXTURE_LEVEL); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newShader(int[])} and {@link GLTools#closeShader(int[])}. * @throws Exception by some fails */ @Test public final void testShader() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); final int[] attrs = new int[5]; GLTools.newShader(attrs); GLTools.closeShader(attrs); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
private static EGLConfig getEglConfig(EGLDisplay eglDisplay, int[] configAttributes) { EGLConfig[] configs = new EGLConfig[1]; int[] numConfigs = new int[1]; if (!EGL14.eglChooseConfig( eglDisplay, configAttributes, 0, configs, 0, configs.length, numConfigs, 0)) { throw new RuntimeException( "eglChooseConfig failed: 0x" + Integer.toHexString(EGL14.eglGetError())); } if (numConfigs[0] <= 0) { throw new RuntimeException("Unable to find any matching EGL config"); } final EGLConfig eglConfig = configs[0]; if (eglConfig == null) { throw new RuntimeException("eglChooseConfig returned null"); } return eglConfig; }
private static EGLContext createEglContext( EglBase14.Context sharedContext, EGLDisplay eglDisplay, EGLConfig eglConfig) { if (sharedContext != null && sharedContext.egl14Context == EGL14.EGL_NO_CONTEXT) { throw new RuntimeException("Invalid sharedContext"); } int[] contextAttributes = {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE}; EGLContext rootContext = sharedContext == null ? EGL14.EGL_NO_CONTEXT : sharedContext.egl14Context; final EGLContext eglContext; synchronized (EglBase.lock) { eglContext = EGL14.eglCreateContext(eglDisplay, eglConfig, rootContext, contextAttributes, 0); } if (eglContext == EGL14.EGL_NO_CONTEXT) { throw new RuntimeException( "Failed to create EGL context: 0x" + Integer.toHexString(EGL14.eglGetError())); } return eglContext; }
private static EGLDisplay getEglDisplay() { EGLDisplay eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); if (eglDisplay == EGL14.EGL_NO_DISPLAY) { throw new RuntimeException( "Unable to get EGL14 display: 0x" + Integer.toHexString(EGL14.eglGetError())); } int[] version = new int[2]; if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) { throw new RuntimeException( "Unable to initialize EGL14: 0x" + Integer.toHexString(EGL14.eglGetError())); } return eglDisplay; }
public EGLSurface createWindowSurface(EGLDisplay display, EGLConfig config, Object nativeWindow) { EGLSurface result = null; try { result = EGL14.eglCreateWindowSurface(display, config, nativeWindow, s_DEFAULT_SURFACE_ATTRIBS, 0); } catch (IllegalArgumentException e) { // This exception indicates that the surface flinger surface // is not valid. This can happen if the surface flinger surface has // been torn down, but the application has not yet been // notified via SurfaceHolder.Callback.surfaceDestroyed. // In theory the application should be notified first, // but in practice sometimes it is not. See b/4588890 Log.e(s_TAG, "eglCreateWindowSurface", e); } return result; }
@Override public EGLConfig chooseConfig(EGLDisplay display, EGLConfig[] configs) { for (EGLConfig config : configs) { int d = findConfigAttrib(display, config, EGL14.EGL_DEPTH_SIZE, 0); int s = findConfigAttrib(display, config, EGL14.EGL_STENCIL_SIZE, 0); if ((d >= depthSize) && (s >= stencilSize)) { int r = findConfigAttrib(display, config, EGL14.EGL_RED_SIZE, 0); int g = findConfigAttrib(display, config, EGL14.EGL_GREEN_SIZE, 0); int b = findConfigAttrib(display, config, EGL14.EGL_BLUE_SIZE, 0); int a = findConfigAttrib(display, config, EGL14.EGL_ALPHA_SIZE, 0); if ((r == redSize) && (g == greenSize) && (b == blueSize) && (a == alphaSize)) { return config; } } } return null; }
/** * Returns whether use of secure dummy surfaces should be enabled. * * @param context Any {@link Context}. */ @TargetApi(24) private static boolean enableSecureDummySurfaceV24(Context context) { EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); String eglExtensions = EGL14.eglQueryString(display, EGL10.EGL_EXTENSIONS); if (eglExtensions == null || !eglExtensions.contains("EGL_EXT_protected_content")) { // EGL_EXT_protected_content is required to enable secure dummy surfaces. return false; } if (Util.SDK_INT == 24 && "samsung".equals(Util.MANUFACTURER)) { // Samsung devices running API level 24 are known to be broken [Internal: b/37197802]. return false; } if (Util.SDK_INT < 26 && !context.getPackageManager().hasSystemFeature( PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE)) { // Pre API level 26 devices were not well tested unless they supported VR mode. return false; } return true; }
/** * Writes the current display, context, and surface to the log. */ public static void logCurrent(String msg) { EGLDisplay display; EGLContext context; EGLSurface surface; display = EGL14.eglGetCurrentDisplay(); context = EGL14.eglGetCurrentContext(); surface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW); Log.i(TAG, "Current EGL (" + msg + "): display=" + display + ", context=" + context + ", surface=" + surface); }
/** * @param display EGL display connection * @return the EGL display handle */ private static long getDisplayHandle(@NonNull EGLDisplay display) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { return getDisplayHandleLollipop(display); } else { return getDisplayHandleBase(display); } }
/** * Close an EGL display connection. * @param display an EGL display connection instance */ public static void closeDisplay(@NonNull EGLDisplay display) { if (!EGL14.eglTerminate(display)) { logError(); throw new RuntimeException("Unable to terminate EGL14 " + getDisplayString(display)); } else { logDebug(getDisplayString(display) + " destroyed"); } }
/** * @param display an EGL display connection instance * @param usePBuffer do not accept rendering through the native window system * @return frame buffer configuration that defines the frame buffer resource available to the * rendering context. **/ @NonNull public static EGLConfig newConfig(@NonNull EGLDisplay display, boolean usePBuffer) { final int surfaceTypeKey = usePBuffer ? EGL14.EGL_SURFACE_TYPE : RECORDABLE_ANDROID; final int surfaceTypeValue = usePBuffer ? EGL14.EGL_PBUFFER_BIT : 1; final EGLConfig[] configs = new EGLConfig[1]; final int[] numConfigs = new int[1]; if (!EGL14.eglChooseConfig(display, new int[] { EGL14.EGL_RED_SIZE, 5, EGL14.EGL_GREEN_SIZE, 6, EGL14.EGL_BLUE_SIZE, 5, /*EGL14.EGL_RED_SIZE, 8, EGL14.EGL_GREEN_SIZE, 8, EGL14.EGL_BLUE_SIZE, 8, EGL14.EGL_ALPHA_SIZE, 8,*/ EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, surfaceTypeKey, surfaceTypeValue, EGL14.EGL_NONE }, 0, configs, 0, configs.length, numConfigs, 0) && numConfigs[0] == 1) { logError(); throw new RuntimeException("Unable to from EGL14 config"); } else { final EGLConfig result = configs[0]; logDebug(getConfigString(result) + " created"); return result; } }
/** * @param display an EGL display connection instance * @return a new EGL rendering context **/ @NonNull public static EGLContext newContext(@NonNull EGLDisplay display, @NonNull EGLConfig config) { final EGLContext result = EGL14.eglCreateContext (display, config, EGL14.EGL_NO_CONTEXT, new int[] {EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE }, 0); if (result == EGL14.EGL_NO_CONTEXT) { logError(); throw new RuntimeException("Unable to from EGL14 context"); } else { logDebug(getContextString(result) + " created"); return result; } }
/** * Close an EGL rendering context. * @param display an EGL display connection instance * @param context an EGL rendering context */ public static void closeContext(@NonNull EGLDisplay display, @NonNull EGLContext context) { if (!EGL14.eglDestroyContext(display, context)) { logError(); throw new RuntimeException("Unable to terminate EGL14 " + getContextString(context)); } else { if (!EGL14.eglReleaseThread()) logError(); logDebug(getContextString(context) + " destroyed"); } }
/** * @param display an EGL display connection instance * @param width horizontal size of virtual surface * @param height vertical size of virtual surface * @param config a frame buffer configuration * @return a new EGL window surface **/ @NonNull public static EGLSurface newSurface(@NonNull EGLDisplay display, @NonNull EGLConfig config, int width, int height) { final EGLSurface result = EGL14.eglCreatePbufferSurface(display, config, new int[] {EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EGL14.EGL_NONE}, 0); if (result == EGL14.EGL_NO_SURFACE) { logError(); throw new RuntimeException("Unable to from EGL14 context"); } else { logDebug(getSurfaceString(result) + " created"); return result; } }
/** * @param display an EGL display connection instance * @param surface a native window. * @param config a frame buffer configuration * @return a new EGL window surface **/ @NonNull public static EGLSurface newSurface(@NonNull EGLDisplay display, @NonNull EGLConfig config, @NonNull Surface surface) { final EGLSurface result = EGL14.eglCreateWindowSurface(display, config, surface, new int[] {EGL14.EGL_NONE}, 0); if (result == EGL14.EGL_NO_SURFACE) { logError(); throw new RuntimeException("Unable to from EGL14 context"); } else { logDebug(getSurfaceString(result) + " created"); return result; } }
/** * Close an EGL rendering context. * @param display an EGL display connection instance * @param surface an EGL rendering surface */ public static void closeSurface(@NonNull EGLDisplay display, @NonNull EGLSurface surface) { if (!EGL14.eglDestroySurface(display, surface)) { logError(); throw new RuntimeException("Unable to terminate EGL14 " + getSurfaceString(surface)); } else { logDebug(getSurfaceString(surface) + " destroyed"); } }
/** * Make an EGL rendering context as current. * @param display an EGL display connection instance * @param surface an EGL rendering surface * @param context an EGL rendering context */ public static void makeCurrent(@NonNull EGLDisplay display, @NonNull EGLSurface surface, @NonNull EGLContext context) { if (!EGL14.eglMakeCurrent(display, surface, surface, context)) { logError(); throw new RuntimeException("Unable to make " + getContextString(context) + " current"); } else { logDebug(getContextString(context) + " set as current"); } }
/** * Calls eglSwapBuffers. Use this to "publish" the current frame. * @param display an EGL display connection instance * @param surface an EGL rendering surface */ public static void swapBuffers(@NonNull EGLDisplay display, @NonNull EGLSurface surface) { //noinspection StatementWithEmptyBody if (!EGL14.eglSwapBuffers(display, surface)) { logError(); throw new RuntimeException("Unable to swap buffers. " + getDisplayString(display) + "; " + getSurfaceString(surface)); } else {/* logDebug("Swap buffers. " + getDisplayString(display) + "; " + getSurfaceString(surface));*/ } }
/** * Sends the presentation time stamp to EGL. * @param display an EGL display connection instance * @param surface an EGL rendering surface * @param nSecs time is expressed in nanoseconds. */ public static void setPresentationTime(@NonNull EGLDisplay display, @NonNull EGLSurface surface, long nSecs) { //noinspection StatementWithEmptyBody if (!EGLExt.eglPresentationTimeANDROID(display, surface, nSecs)) { logError(); throw new RuntimeException("Unable to set presentation time (" + nSecs +"ns)." + getDisplayString(display) + "; " + getSurfaceString(surface)); } else {/* logDebug("Set presentation time (" + nSecs +"ns)." + getDisplayString(display) + "; " + getSurfaceString(surface));*/ } }
/** * Test for {@link GLTools#newConfig(EGLDisplay, boolean)} * @throws Exception by some fails */ @Test public final void testConfigWindow() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); GLTools.newConfig(eglDisplay, false); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newConfig(EGLDisplay, boolean)} * @throws Exception by some fails */ @Test public final void testConfigPBuffer() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); GLTools.newConfig(eglDisplay, true); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newContext(EGLDisplay, EGLConfig)} and * {@link GLTools#closeContext(EGLDisplay, EGLContext)}. * @throws Exception by some fails */ @Test public final void testContextWindow() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, false); GLTools.closeContext(eglDisplay, GLTools.newContext(eglDisplay, eglConfig)); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newContext(EGLDisplay, EGLConfig)} and * {@link GLTools#closeContext(EGLDisplay, EGLContext)}. * @throws Exception by some fails */ @Test public final void testContextPBuffer() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); GLTools.closeContext(eglDisplay, GLTools.newContext(eglDisplay, eglConfig)); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newSurface(EGLDisplay, EGLConfig, int, int)} and * {@link GLTools#closeSurface(EGLDisplay, EGLSurface)}. * @throws Exception by some fails */ @Test public final void testSurface() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); GLTools.closeSurface(eglDisplay, GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE)); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#makeCurrent(EGLDisplay, EGLSurface, EGLContext)}. * @throws Exception by some fails */ @Test public final void testMakeCurrent() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#swapBuffers(EGLDisplay, EGLSurface)}. * @throws Exception by some fails */ @Test public final void testSwapBuffers() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); try { GLTools.swapBuffers(eglDisplay, eglSurface); } catch (RuntimeException exception) { final int txt = GLTools.newTexture(TEXTURE_LEVEL); final SurfaceTexture surfaceTexture = new SurfaceTexture(txt, true); final Surface surface = new Surface(surfaceTexture); final EGLSurface window = GLTools.newSurface(eglDisplay, eglConfig, surface); GLTools.makeCurrent(eglDisplay, window, eglContext); GLTools.swapBuffers(eglDisplay, window); GLTools.closeSurface(eglDisplay, window); surface.release(); surfaceTexture.release(); GLTools.closeTexture(txt, TEXTURE_LEVEL); } GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) private static int getMaxTextureEgl14() { EGLDisplay dpy = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); int[] vers = new int[2]; EGL14.eglInitialize(dpy, vers, 0, vers, 1); int[] configAttr = { EGL14.EGL_COLOR_BUFFER_TYPE, EGL14.EGL_RGB_BUFFER, EGL14.EGL_LEVEL, 0, EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT, EGL14.EGL_SURFACE_TYPE, EGL14.EGL_PBUFFER_BIT, EGL14.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] numConfig = new int[1]; EGL14.eglChooseConfig(dpy, configAttr, 0, configs, 0, 1, numConfig, 0); if (numConfig[0] == 0) { return 0; } EGLConfig config = configs[0]; int[] surfAttr = { EGL14.EGL_WIDTH, 64, EGL14.EGL_HEIGHT, 64, EGL14.EGL_NONE }; EGLSurface surf = EGL14.eglCreatePbufferSurface(dpy, config, surfAttr, 0); int[] ctxAttrib = { EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE }; EGLContext ctx = EGL14.eglCreateContext(dpy, config, EGL14.EGL_NO_CONTEXT, ctxAttrib, 0); EGL14.eglMakeCurrent(dpy, surf, surf, ctx); int[] maxSize = new int[1]; GLES20.glGetIntegerv(GLES20.GL_MAX_TEXTURE_SIZE, maxSize, 0); EGL14.eglMakeCurrent(dpy, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT); EGL14.eglDestroySurface(dpy, surf); EGL14.eglDestroyContext(dpy, ctx); EGL14.eglTerminate(dpy); return maxSize[0]; }