public void release() { if (mEGL != null) { if (mEGL.eglGetCurrentContext().equals(mEGLContext)) { mEGL.eglMakeCurrent(mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); } mEGL.eglDestroySurface(mEGLDisplay, mEGLSurface); mEGL.eglDestroyContext(mEGLDisplay, mEGLContext); } mSurface.release(); mEGLDisplay = null; mEGLContext = null; mEGLSurface = null; mEGL = null; mTextureRender = null; mSurface = null; mSurfaceTexture = null; }
public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config, Object nativeWindow) { EGLSurface result = null; try { result = egl.eglCreateWindowSurface(display, config, nativeWindow, null); } 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(TAG, "eglCreateWindowSurface", e); } return result; }
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { // Get the number of minimally matching EGL configurations int[] num_config = new int[1]; egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config); int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No configs match configSpec"); } // Allocate then read the array of minimally matching EGL configs EGLConfig[] configs = new EGLConfig[numConfigs]; egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config); if (DEBUG) { printConfigs(egl, display, configs); } // Now return the "best" one return chooseConfig(egl, display, configs); }
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) { Log.d(LOG_TAG, "createContext " + egl + " " + display + " " + eglConfig); checkEglError("before createContext", egl); int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE}; EGLContext ctx; if (mRenderer.mEGLCurrentContext == null) { mRenderer.mEGLCurrentContext = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list); ctx = mRenderer.mEGLCurrentContext; } else { ctx = mRenderer.mEGLCurrentContext; } checkEglError("after createContext", egl); return ctx; }
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { int[] num_config = new int[1]; if (!egl.eglChooseConfig(display, mConfigSpec, null, 0, num_config)) { throw new IllegalArgumentException("eglChooseConfig failed"); } int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException( "No configs match configSpec"); } EGLConfig[] configs = new EGLConfig[numConfigs]; if (!egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs, num_config)) { throw new IllegalArgumentException("eglChooseConfig#2 failed"); } EGLConfig config = chooseConfig(egl, display, configs); if (config == null) { throw new IllegalArgumentException("No config chosen"); } return config; }
public ComponentSizeChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize) { super(new int[] { EGL10.EGL_RED_SIZE, redSize, EGL10.EGL_GREEN_SIZE, greenSize, EGL10.EGL_BLUE_SIZE, blueSize, EGL10.EGL_ALPHA_SIZE, alphaSize, EGL10.EGL_DEPTH_SIZE, depthSize, EGL10.EGL_STENCIL_SIZE, stencilSize, EGL10.EGL_NONE}); mValue = new int[1]; mRedSize = redSize; mGreenSize = greenSize; mBlueSize = blueSize; mAlphaSize = alphaSize; mDepthSize = depthSize; mStencilSize = stencilSize; }
public void start() { 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"); } mEglConfig = mEGLConfigChooser.chooseConfig(mEgl, mEglDisplay); mEglContext = mEGLContextFactory.createContext(mEgl, mEglDisplay, mEglConfig); if (mEglContext == null || mEglContext == EGL10.EGL_NO_CONTEXT) { mEglContext = null; throwEglException("createContext"); } mEglSurface = null; }
private EGLConfig chooseConfig() { int[] attribList = new int[] { EGL_DEPTH_SIZE, 0, EGL_STENCIL_SIZE, 0, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL10.EGL_RENDERABLE_TYPE, 4, EGL_NONE }; // No error checking performed, minimum required code to elucidate logic // Expand on this logic to be more selective in choosing a configuration int[] numConfig = new int[1]; mEGL.eglChooseConfig(mEGLDisplay, attribList, null, 0, numConfig); int configSize = numConfig[0]; mEGLConfigs = new EGLConfig[configSize]; mEGL.eglChooseConfig(mEGLDisplay, attribList, mEGLConfigs, configSize, numConfig); if (LIST_CONFIGS) { listConfig(); } return mEGLConfigs[0]; // Best match is probably the first configuration }
public void initDrawContext( EGL10 egl, EGLDisplay eglDisplay, EGLSurface eglSurface, EGLContext eglContext) { mEgl = egl; mEglDisplay = eglDisplay; mEglSurface = eglSurface; mEglContext = eglContext; if (0 == mMapId) { return; } // makeCurrent(); Api.ngsMapInit(mMapId); }
public boolean swap() { if (!mEgl.eglSwapBuffers(mEglDisplay, mEglSurface)) { int error = mEgl.eglGetError(); switch (error) { case EGL11.EGL_CONTEXT_LOST: return false; case EGL10.EGL_BAD_NATIVE_WINDOW: Log.e("EglHelper", "eglSwapBuffers returned EGL_BAD_NATIVE_WINDOW. tid=" + Thread.currentThread().getId()); break; case EGL10.EGL_BAD_SURFACE: Log.e("EglHelper", "eglSwapBuffers returned EGL_BAD_SURFACE. tid=" + Thread.currentThread().getId()); return false; default: throwEglException("eglSwapBuffers", error); } } return true; }
public static void throwEglException( EGL10 egl, String function) { int error; StringBuilder sb = new StringBuilder(); while ((error = egl.eglGetError()) != EGL10.EGL_SUCCESS) { sb.append(getErrorString(error)).append("\n"); } String message = function + " failed"; if (sb.length() > 0) { message += ": " + sb.toString(); } Log.e(ConstantsUI.TAG, message); throw new RuntimeException(message); }
public ComponentSizeChooser(int redSize, int greenSize, int blueSize, int alphaSize, int depthSize, int stencilSize) { super(new int[]{ EGL10.EGL_RED_SIZE, redSize, EGL10.EGL_GREEN_SIZE, greenSize, EGL10.EGL_BLUE_SIZE, blueSize, EGL10.EGL_ALPHA_SIZE, alphaSize, EGL10.EGL_DEPTH_SIZE, depthSize, EGL10.EGL_STENCIL_SIZE, stencilSize, EGL10.EGL_NONE}); mValue = new int[1]; mRedSize = redSize; mGreenSize = greenSize; mBlueSize = blueSize; mAlphaSize = alphaSize; mDepthSize = depthSize; mStencilSize = stencilSize; }
@Override public EGLContext createContext( EGL10 egl, EGLDisplay display, EGLConfig eglConfig) { Log.w(ConstantsUI.TAG, "creating OpenGL ES 2.0 context"); checkEglError(egl, "Before eglCreateContext"); int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE}; mEglContext = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list); if (EGL10.EGL_NO_CONTEXT == mEglContext) { throwEglException(egl, "eglCreateContext"); } return mEglContext; }
@Override public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) { for (EGLConfig config : configs) { int d = findConfigAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0); int s = findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0); if ((d >= mDepthSize) && (s >= mStencilSize)) { int r = findConfigAttrib(egl, display, config, EGL10.EGL_RED_SIZE, 0); int g = findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0); int b = findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0); int a = findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0); if ((r == mRedSize) && (g == mGreenSize) && (b == mBlueSize) && (a == mAlphaSize)) { return config; } } } return null; }
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { int[] num_config = new int[1]; egl.eglChooseConfig(display, mConfigSpec, null, 0, num_config); int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No configs match configSpec"); } EGLConfig[] configs = new EGLConfig[numConfigs]; egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs, num_config); EGLConfig config = chooseConfig(egl, display, configs); if (config == null) { throw new IllegalArgumentException("No config chosen"); } return config; }
@Override public EGLConfig chooseConfig(final EGL10 egl, final EGLDisplay display) { //Querying number of configurations final int[] num_conf = new int[1]; // egl.eglGetConfigs(display, null, 0, num_conf); //if configuration array is null it still returns the number of configurations final int configurations = num_conf[0]; //Querying actual configurations final EGLConfig[] conf = new EGLConfig[configurations]; // egl.eglGetConfigs(display, conf, configurations, num_conf); EGLConfig result = null; for (int i = 0; i < configurations; i++) { result = better(result, conf[i], egl, display); /*final Egl config = new Egl(egl, display, conf[i]); if (config.EGL_RED_SIZE + config.EGL_BLUE_SIZE + config.EGL_GREEN_SIZE + config.EGL_ALPHA_SIZE >= 16) info.eglconfigs.add(config);*/ } return result; }
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { int[] num_config = new int[1]; if (!egl.eglChooseConfig(display, mConfigSpec, null, 0, num_config)) { throw new IllegalArgumentException("eglChooseConfig failed"); } int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No configs match configSpec"); } EGLConfig[] configs = new EGLConfig[numConfigs]; if (!egl.eglChooseConfig(display, mConfigSpec, configs, numConfigs, num_config)) { throw new IllegalArgumentException("eglChooseConfig#2 failed"); } EGLConfig config = chooseConfig(egl, display, configs); if (config == null) { throw new IllegalArgumentException("No config chosen"); } return config; }
private void createSurfaceInternal(Object nativeWindow) { if (!(nativeWindow instanceof SurfaceHolder) && !(nativeWindow instanceof SurfaceTexture)) { throw new IllegalStateException("Input must be either a SurfaceHolder or SurfaceTexture"); } checkIsNotReleased(); if (eglSurface != EGL10.EGL_NO_SURFACE) { throw new RuntimeException("Already has an EGLSurface"); } int[] surfaceAttribs = {EGL10.EGL_NONE}; eglSurface = egl.eglCreateWindowSurface(eglDisplay, eglConfig, nativeWindow, surfaceAttribs); if (eglSurface == EGL10.EGL_NO_SURFACE) { throw new RuntimeException( "Failed to create window surface: 0x" + Integer.toHexString(egl.eglGetError())); } }
int[] build(){ return new int[] { EGL10.EGL_SURFACE_TYPE, surfaceType, //渲染类型 EGL10.EGL_RED_SIZE, red, //指定RGB中的R大小(bits) EGL10.EGL_GREEN_SIZE, green, //指定G大小 EGL10.EGL_BLUE_SIZE, blue, //指定B大小 EGL10.EGL_ALPHA_SIZE, alpha, //指定Alpha大小,以上四项实际上指定了像素格式 EGL10.EGL_DEPTH_SIZE, depth, //指定深度缓存(Z Buffer)大小 EGL10.EGL_RENDERABLE_TYPE, renderType, //指定渲染api类别, 如上一小节描述,这里或者是硬编码的4(EGL14.EGL_OPENGL_ES2_BIT) EGL10.EGL_NONE }; //总是以EGL14.EGL_NONE结尾 }
private void destroySurfaceImp() { if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) { mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); GLTextureView view = mGLTextureViewWeakRef.get(); if (view != null) { view.mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface); } mEglSurface = null; } }
private int[] filterConfigSpec(int[] configSpec) { int len = configSpec.length; int[] newConfigSpec = new int[len + 2]; System.arraycopy(configSpec, 0, newConfigSpec, 0, len - 1); newConfigSpec[len - 1] = EGL10.EGL_RENDERABLE_TYPE; newConfigSpec[len] = 4; /* EGL_OPENGL_ES2_BIT */ newConfigSpec[len + 1] = EGL10.EGL_NONE; return newConfigSpec; }
public void finish() { if (eglSurface != null) { egl10.eglMakeCurrent(eglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl10.eglDestroySurface(eglDisplay, eglSurface); eglSurface = null; } if (eglContext != null) { egl10.eglDestroyContext(eglDisplay, eglContext); eglContext = null; } if (eglDisplay != null) { egl10.eglTerminate(eglDisplay); eglDisplay = null; } }
/** * Returns the best of the two EGLConfig passed according to depth and colours * * @param a The first candidate * @param b The second candidate * @return The chosen candidate */ private EGLConfig better(EGLConfig a, EGLConfig b, EGL10 egl, EGLDisplay display) { if (a == null) return b; EGLConfig result; int[] value = new int[1]; egl.eglGetConfigAttrib(display, a, EGL10.EGL_DEPTH_SIZE, value); int depthA = value[0]; egl.eglGetConfigAttrib(display, b, EGL10.EGL_DEPTH_SIZE, value); int depthB = value[0]; if (depthA > depthB) result = a; else if (depthA < depthB) result = b; else //if depthA == depthB { egl.eglGetConfigAttrib(display, a, EGL10.EGL_RED_SIZE, value); int redA = value[0]; egl.eglGetConfigAttrib(display, b, EGL10.EGL_RED_SIZE, value); int redB = value[0]; if (redA > redB) result = a; else if (redA < redB) result = b; else //if redA == redB { //Don't care result = a; } } return result; }
private EGLDisplay getEglDisplay() { EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); if (eglDisplay == EGL10.EGL_NO_DISPLAY) { throw new RuntimeException( "Unable to get EGL10 display: 0x" + Integer.toHexString(egl.eglGetError())); } int[] version = new int[2]; if (!egl.eglInitialize(eglDisplay, version)) { throw new RuntimeException( "Unable to initialize EGL10: 0x" + Integer.toHexString(egl.eglGetError())); } return eglDisplay; }
private int findConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue) { mValue[0] = -1; if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) { return mValue[0]; } Log.w("SDL", "GLSurfaceView_SDL::EGLConfigChooser::findConfigAttrib(): attribute doesn't exist: " + attribute); return defaultValue; }
@Override public void swapBuffers() { checkIsNotReleased(); if (eglSurface == EGL10.EGL_NO_SURFACE) { throw new RuntimeException("No EGLSurface - can't swap buffers"); } synchronized (EglBase.lock) { egl.eglSwapBuffers(eglDisplay, eglSurface); } }
@Override public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) { EGLConfig closestConfig = null; int closestDistance = 1000; for (EGLConfig config : configs) { int d = findConfigAttrib(egl, display, config, EGL10.EGL_DEPTH_SIZE, 0); int s = findConfigAttrib(egl, display, config, EGL10.EGL_STENCIL_SIZE, 0); if (d >= mDepthSize && s >= mStencilSize) { int r = findConfigAttrib(egl, display, config, EGL10.EGL_RED_SIZE, 0); int g = findConfigAttrib(egl, display, config, EGL10.EGL_GREEN_SIZE, 0); int b = findConfigAttrib(egl, display, config, EGL10.EGL_BLUE_SIZE, 0); int a = findConfigAttrib(egl, display, config, EGL10.EGL_ALPHA_SIZE, 0); int distance = Math.abs(r - mRedSize) + Math.abs(g - mGreenSize) + Math.abs(b - mBlueSize) + Math.abs(a - mAlphaSize); if (distance < closestDistance) { closestDistance = distance; closestConfig = config; } } } return closestConfig; }
@Override public void release() { checkIsNotReleased(); releaseSurface(); detachCurrent(); egl.eglDestroyContext(eglDisplay, eglContext); egl.eglTerminate(eglDisplay); eglContext = EGL10.EGL_NO_CONTEXT; eglDisplay = EGL10.EGL_NO_DISPLAY; eglConfig = null; }
public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) { if (!egl.eglDestroyContext(display, context)) { Log.e("DefaultContextFactory", "display:" + display + " context: " + context); if (LOG_THREADS) { Log.i("DefaultContextFactory", "tid=" + Thread.currentThread().getId()); } EglHelper.throwEglException("eglDestroyContex", egl.eglGetError()); } }
private int[] filterConfigSpec(int[] configSpec) { if (mEGLContextClientVersion != 2) { return configSpec; } /* We know none of the subclasses define EGL_RENDERABLE_TYPE. * And we know the configSpec is well formed. */ int len = configSpec.length; int[] newConfigSpec = new int[len + 2]; System.arraycopy(configSpec, 0, newConfigSpec, 0, len-1); newConfigSpec[len-1] = EGL10.EGL_RENDERABLE_TYPE; newConfigSpec[len] = 4; /* EGL_OPENGL_ES2_BIT */ newConfigSpec[len+1] = EGL10.EGL_NONE; return newConfigSpec; }
@Override public void makeCurrent() { checkIsNotReleased(); if (eglSurface == EGL10.EGL_NO_SURFACE) { throw new RuntimeException("No EGLSurface - can't make current"); } synchronized (EglBase.lock) { if (!egl.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) { throw new RuntimeException( "eglMakeCurrent failed: 0x" + Integer.toHexString(egl.eglGetError())); } } }
private int findConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue) { if (egl.eglGetConfigAttrib(display, config, attribute, mValue)) { return mValue[0]; } return defaultValue; }
public PixelBuffer(final int width, final int height) { mWidth = width; mHeight = height; int[] version = new int[2]; int[] attribList = new int[] { EGL_WIDTH, mWidth, EGL_HEIGHT, mHeight, EGL_NONE }; // No error checking performed, minimum required code to elucidate logic mEGL = (EGL10) EGLContext.getEGL(); mEGLDisplay = mEGL.eglGetDisplay(EGL_DEFAULT_DISPLAY); mEGL.eglInitialize(mEGLDisplay, version); mEGLConfig = chooseConfig(); // Choosing a config is a little more // complicated // mEGLContext = mEGL.eglCreateContext(mEGLDisplay, mEGLConfig, // EGL_NO_CONTEXT, null); int EGL_CONTEXT_CLIENT_VERSION = 0x3098; int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; mEGLContext = mEGL.eglCreateContext(mEGLDisplay, mEGLConfig, EGL_NO_CONTEXT, attrib_list); mEGLSurface = mEGL.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, attribList); mEGL.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext); mGL = (GL10) mEGLContext.getGL(); // Record thread owner of OpenGL context mThreadOwner = Thread.currentThread().getName(); }
public void makeCurrent() { if (null != mEgl && EGL10.EGL_NO_DISPLAY == mEgl.eglGetCurrentDisplay() && EGL10.EGL_NO_SURFACE == mEgl.eglGetCurrentSurface(EGL10.EGL_DRAW) && EGL10.EGL_NO_SURFACE == mEgl.eglGetCurrentSurface(EGL10.EGL_READ) && EGL10.EGL_NO_CONTEXT == mEgl.eglGetCurrentContext()) { mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext); } }
@Override public EGLConfig chooseConfig( EGL10 egl, EGLDisplay display) { mEgl = egl; mEglDisplay = display; // Get the number of minimally matching EGL configurations int[] num_config = new int[1]; egl.eglChooseConfig(display, glConfigAttribs, null, 0, num_config); int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No configs match configSpec"); } // Allocate then read the array of minimally matching EGL configs EGLConfig[] configs = new EGLConfig[numConfigs]; egl.eglChooseConfig(display, glConfigAttribs, configs, numConfigs, num_config); if (DEBUG) { printConfigs(egl, display, configs); } // Now return the "best" one EGLConfig config = chooseConfig(egl, display, configs); if (config == null) { throw new IllegalArgumentException("No config chosen"); } return config; }
@Override public void onInit() { super.onInit(); mEGL = (EGL10) EGLContext.getEGL(); mEGLDisplay = mEGL.eglGetCurrentDisplay(); mEGLContext = mEGL.eglGetCurrentContext(); mEGLScreenSurface = mEGL.eglGetCurrentSurface(EGL10.EGL_DRAW); }
public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) { int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, mEGLContextClientVersion, EGL10.EGL_NONE}; return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, mEGLContextClientVersion != 0 ? attrib_list : null); }