private void createSurface() { hasSurface = false; if (surface != null) { egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroySurface(display, surface); } surface = egl.eglCreateWindowSurface(display, config, surfaceHolder, null); egl.eglMakeCurrent(display, surface, surface, eglContext); gl = eglContext.getGL(); hasSurface = true; }
public static boolean createEGLSurface() { if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) { EGL10 egl = (EGL10) EGLContext.getEGL(); if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) createEGLContext(); if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) { Log.v("SDL", "Creating new EGL Surface"); SDLActivity.mEGLSurface = egl.eglCreateWindowSurface( SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null); if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) { Log.e("SDL", "Couldn't create surface"); return false; } } else Log.v("SDL", "EGL Surface remains valid"); if (egl.eglGetCurrentContext() != SDLActivity.mEGLContext) { if (!egl.eglMakeCurrent( SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface, SDLActivity.mEGLSurface, SDLActivity.mEGLContext)) { Log.e("SDL", "Old EGL Context doesnt work, trying with a new one"); // TODO: Notify the user via a message that the old context could not be restored, and // that textures need to be manually restored. createEGLContext(); if (!egl.eglMakeCurrent( SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface, SDLActivity.mEGLSurface, SDLActivity.mEGLContext)) { Log.e("SDL", "Failed making EGL Context current"); return false; } } else Log.v("SDL", "EGL Context made current"); } else Log.v("SDL", "EGL Context remains current"); return true; } else { Log.e( "SDL", "Surface creation failed, display = " + SDLActivity.mEGLDisplay + ", config = " + SDLActivity.mEGLConfig); return false; } }
void destroySurface() { if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) { sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); sEgl.eglDestroySurface(sEglDisplay, mEglSurface); mEglSurface = null; } }
/** * Ensures the current EGL context is the one we expect. * * @return {@link #SURFACE_STATE_ERROR} if the correct EGL context cannot be made current, * {@link #SURFACE_STATE_UPDATED} if the EGL context was changed or {@link * #SURFACE_STATE_SUCCESS} if the EGL context was the correct one */ int checkCurrent() { if (mEglThread != Thread.currentThread()) { throw new IllegalStateException( "Hardware acceleration can only be used with a " + "single UI thread.\nOriginal thread: " + mEglThread + "\n" + "Current thread: " + Thread.currentThread()); } if (!mEglContext.equals(sEgl.eglGetCurrentContext()) || !mEglSurface.equals(sEgl.eglGetCurrentSurface(EGL_DRAW))) { if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) { Log.e(LOG_TAG, "eglMakeCurrent failed " + GLUtils.getEGLErrorString(sEgl.eglGetError())); fallback(true); return SURFACE_STATE_ERROR; } else { if (mUpdateDirtyRegions) { enableDirtyRegions(); mUpdateDirtyRegions = false; } return SURFACE_STATE_UPDATED; } } return SURFACE_STATE_SUCCESS; }
public void destroySurface() { if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) { mEgl.eglMakeCurrent( mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface); mEglSurface = null; } }
private void cleanupgl() { // Unbind and destroy the old EGL surface, if there is one. if (surface != null) { egl.eglMakeCurrent( display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroySurface(display, surface); surface = null; } }
/* * React to the creation of a new surface by creating and returning an OpenGL interface that renders to that * surface. */ public GL createSurface(SurfaceHolder holder) { /* * The window size has changed, so we need to create a new surface. */ if (mEglSurface != null && mEglSurface != EGL10.EGL_NO_SURFACE) { /* * Unbind and destroy the old EGL surface, if there is one. */ mEgl.eglMakeCurrent( mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); mEGLWindowSurfaceFactory.destroySurface(mEgl, mEglDisplay, mEglSurface); } /* * Create an EGL surface we can render into. */ mEglSurface = mEGLWindowSurfaceFactory.createWindowSurface(mEgl, mEglDisplay, mEglConfig, holder); if (mEglSurface == null || mEglSurface == EGL10.EGL_NO_SURFACE) { throw new RuntimeException("createWindowSurface failed"); } /* * Before we can issue GL commands, we need to make sure the context is current and bound to a surface. */ if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) { throw new RuntimeException("eglMakeCurrent failed."); } GL gl = mEglContext.getGL(); if (mGLWrapper != null) { gl = mGLWrapper.wrap(gl); } /* * if ((mDebugFlags & (DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS))!= 0) { int configFlags = 0; Writer log = * null; if ((mDebugFlags & DEBUG_CHECK_GL_ERROR) != 0) { configFlags |= GLDebugHelper.CONFIG_CHECK_GL_ERROR; } * if ((mDebugFlags & DEBUG_LOG_GL_CALLS) != 0) { log = new LogWriter(); } gl = GLDebugHelper.wrap(gl, * configFlags, log); } */ return gl; }
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { EGL10 egl = (EGL10) EGLContext.getEGL(); YabauseRunnable.lockGL(); egl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext); YabauseRunnable.initViewport(width, height); YabauseRunnable.unlockGL(); }
public boolean eglMakeCurrent( EGLDisplay display, EGLSurface draw, EGLSurface read, EGLContext context) { begin("eglMakeCurrent"); arg("display", display); arg("draw", draw); arg("read", read); arg("context", context); end(); boolean result = mEgl10.eglMakeCurrent(display, draw, read, context); returns(result); checkError(); return result; }
/** Some initialization of the OpenGL stuff (that I don't understand...) */ protected void init() { // Much of this code is from GLSurfaceView in the Google API Demos. // I encourage those interested to look there for documentation. egl = (EGL10) EGLContext.getEGL(); dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); int[] version = new int[2]; egl.eglInitialize(dpy, version); int[] configSpec = { EGL10.EGL_RED_SIZE, 5, EGL10.EGL_GREEN_SIZE, 6, EGL10.EGL_BLUE_SIZE, 5, EGL10.EGL_DEPTH_SIZE, 8, EGL10.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] num_config = new int[1]; egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config); EGLConfig config = configs[0]; eglContext = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null); surface = egl.eglCreateWindowSurface(dpy, config, sHolder, null); egl.eglMakeCurrent(dpy, surface, surface, eglContext); gl = (GL10) eglContext.getGL(); // Load the buffer and stuff gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClearDepthf(1.0f); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glEnable(GL10.GL_TEXTURE_2D); // Resize... whatever? gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glViewport(0, 0, width, height); GLU.gluPerspective(gl, 45.0f, ((float) width) / height, 1f, 100f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); GLU.gluLookAt(gl, 0, 0, 5.5f, 0, 0, 0, 0, 1, 0); gl.glNormal3f(0, 0, 1); }
public void initgl() { cleanupgl(); // Create new surface. Must succeed. surface = egl.eglCreateWindowSurface(display, config, getHolder(), null); if (null == surface) { throw new RuntimeException("eglCreateWindowSurface"); } // Bind the rendering context to the surface. if (!egl.eglMakeCurrent(display, surface, surface, ctxt)) { throw new RuntimeException("eglMakeCurrent"); } gl = (GL10) ctxt.getGL(); }
public void surfaceDestroyed(SurfaceHolder arg0) { if (running) { running = false; this.newFrameLock.setValue(true); try { mainLoop.join(); } catch (Exception ex) { } mainLoop = null; egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroySurface(dpy, surface); egl.eglDestroyContext(dpy, eglContext); egl.eglTerminate(dpy); } }
public static void deleteGLContext() { if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext != EGL10.EGL_NO_CONTEXT) { EGL10 egl = (EGL10) EGLContext.getEGL(); egl.eglMakeCurrent( SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroyContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLContext); SDLActivity.mEGLContext = EGL10.EGL_NO_CONTEXT; if (SDLActivity.mEGLSurface != EGL10.EGL_NO_SURFACE) { egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface); SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE; } } }
// Called when we lose the surface @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.v("SDL", "surfaceDestroyed()"); // Call this *before* setting mIsSurfaceReady to 'false' SDLActivity.handlePause(); SDLActivity.mIsSurfaceReady = false; /* We have to clear the current context and destroy the egl surface here * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d */ EGL10 egl = (EGL10) EGLContext.getEGL(); egl.eglMakeCurrent( SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface); SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE; }
private void endGLES() { EGL10 egl = (EGL10) EGLContext.getEGL(); if (mEglSurface != null) { // レンダリングコンテキストとの結びつけは解除 egl.eglMakeCurrent( mEglDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroySurface(mEglDisplay, mEglSurface); mEglSurface = null; } if (mEglContext != null) { egl.eglDestroyContext(mEglDisplay, mEglContext); mEglContext = null; } if (mEglDisplay != null) { egl.eglTerminate(mEglDisplay); mEglDisplay = null; } }
private boolean createSurface(Surface surface) { mEglSurface = sEgl.eglCreateWindowSurface(sEglDisplay, sEglConfig, surface, null); if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) { int error = sEgl.eglGetError(); if (error == EGL_BAD_NATIVE_WINDOW) { Log.e(LOG_TAG, "createWindowSurface returned EGL_BAD_NATIVE_WINDOW."); return false; } throw new RuntimeException( "createWindowSurface failed " + GLUtils.getEGLErrorString(error)); } if (!sEgl.eglMakeCurrent(sEglDisplay, mEglSurface, mEglSurface, mEglContext)) { throw new IllegalStateException( "eglMakeCurrent failed " + GLUtils.getEGLErrorString(sEgl.eglGetError())); } enableDirtyRegions(); return true; }
@Override void invalidate(Surface surface) { // Cancels any existing buffer to ensure we'll get a buffer // of the right size before we call eglSwapBuffers sEgl.eglMakeCurrent(sEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); if (mEglSurface != null && mEglSurface != EGL_NO_SURFACE) { sEgl.eglDestroySurface(sEglDisplay, mEglSurface); mEglSurface = null; setEnabled(false); } if (surface.isValid()) { if (!createSurface(surface)) { return; } mUpdateDirtyRegions = true; if (mCanvas != null) { setEnabled(true); } } }
// EGL functions public boolean initEGL(int majorVersion, int minorVersion) { Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion); try { EGL10 egl = (EGL10) EGLContext.getEGL(); EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); int[] version = new int[2]; egl.eglInitialize(dpy, version); int EGL_OPENGL_ES_BIT = 1; int EGL_OPENGL_ES2_BIT = 4; int renderableType = 0; if (majorVersion == 2) { renderableType = EGL_OPENGL_ES2_BIT; } else if (majorVersion == 1) { renderableType = EGL_OPENGL_ES_BIT; } int[] configSpec = { // EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_RENDERABLE_TYPE, renderableType, EGL10.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] num_config = new int[1]; if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) { Log.e("SDL", "No EGL config available"); return false; } EGLConfig config = configs[0]; EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null); if (ctx == EGL10.EGL_NO_CONTEXT) { Log.e("SDL", "Couldn't create context"); return false; } EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null); if (surface == EGL10.EGL_NO_SURFACE) { Log.e("SDL", "Couldn't create surface"); return false; } if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) { Log.e("SDL", "Couldn't make context current"); return false; } mEGLContext = ctx; mEGLDisplay = dpy; mEGLSurface = surface; } catch (Exception e) { Log.v("SDL", e + ""); for (StackTraceElement s : e.getStackTrace()) { Log.v("SDL", s.toString()); } } return true; }
public void run() { // Much of this code is from GLSurfaceView in the Google API Demos. // I encourage those interested to look there for documentation. EGL10 egl = (EGL10) EGLContext.getEGL(); EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); int[] version = new int[2]; egl.eglInitialize(dpy, version); int[] configSpec = { EGL10.EGL_RED_SIZE, 5, EGL10.EGL_GREEN_SIZE, 6, EGL10.EGL_BLUE_SIZE, 5, EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] num_config = new int[1]; egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config); EGLConfig config = configs[0]; EGLContext context = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null); EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, sHolder, null); egl.eglMakeCurrent(dpy, surface, surface, context); GL10 gl = (GL10) context.getGL(); init(gl); int delta = -1; if (fps > 0) { delta = 1000 / fps; } long time = System.currentTimeMillis(); running = true; while (running) { int w, h; synchronized (this) { w = width; h = height; } if (System.currentTimeMillis() - time < delta) { try { Thread.sleep(System.currentTimeMillis() - time); } catch (InterruptedException ex) { } } drawFrame(gl, w, h); egl.eglSwapBuffers(dpy, surface); if (egl.eglGetError() == EGL11.EGL_CONTEXT_LOST) { Context c = getContext(); if (c instanceof Activity) { ((Activity) c).finish(); } } time = System.currentTimeMillis(); } egl.eglMakeCurrent(dpy, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); egl.eglDestroySurface(dpy, surface); egl.eglDestroyContext(dpy, context); egl.eglTerminate(dpy); }
@Override public void onSurfaceChanged(final GL10 gl, final int width, final int height) { Log.i(TAG, "onSurfaceChanged; %d x %d", width, height); if (null != mMainSurface) { Log.v(TAG, "short-circuiting onSurfaceChanged"); return; } final EGL10 egl = (EGL10) EGLContext.getEGL(); final EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); final EGLContext context = egl.eglGetCurrentContext(); int numAttribs = 0; final int[] configAttribs = new int[16]; Arrays.fill(configAttribs, EGL10.EGL_NONE); Log.v(TAG, "--- window surface configuration ---"); final VrAppSettings appSettings = mActivity.getAppSettings(); if (appSettings.useSrgbFramebuffer) { final int EGL_GL_COLORSPACE_KHR = 0x309D; final int EGL_GL_COLORSPACE_SRGB_KHR = 0x3089; configAttribs[numAttribs++] = EGL_GL_COLORSPACE_KHR; configAttribs[numAttribs++] = EGL_GL_COLORSPACE_SRGB_KHR; } Log.v(TAG, "--- srgb framebuffer: %b", appSettings.useSrgbFramebuffer); if (appSettings.useProtectedFramebuffer) { final int EGL_PROTECTED_CONTENT_EXT = 0x32c0; configAttribs[numAttribs++] = EGL_PROTECTED_CONTENT_EXT; configAttribs[numAttribs++] = EGL14.EGL_TRUE; } Log.v(TAG, "--- protected framebuffer: %b", appSettings.useProtectedFramebuffer); configAttribs[numAttribs++] = EGL10.EGL_NONE; Log.v(TAG, "------------------------------------"); // this is the display surface timewarp will hijack mMainSurface = egl.eglCreateWindowSurface(display, mConfig, mSurfaceView.getHolder(), configAttribs); Log.v(TAG, "mMainSurface: 0x%x", mMainSurface.hashCode()); if (mMainSurface == EGL10.EGL_NO_SURFACE) { throw new IllegalStateException( "eglCreateWindowSurface() failed: 0x" + Integer.toHexString(egl.eglGetError())); } if (!egl.eglMakeCurrent(display, mMainSurface, mMainSurface, context)) { throw new IllegalStateException( "eglMakeCurrent() failed: 0x " + Integer.toHexString(egl.eglGetError())); } nativeOnSurfaceChanged(mPtr); // necessary to explicitly make the pbuffer current for the rendering thread; // TimeWarp took over the window surface if (!egl.eglMakeCurrent(display, mPixelBuffer, mPixelBuffer, context)) { throw new IllegalStateException( "Failed to make context current ; egl error 0x" + Integer.toHexString(egl.eglGetError())); } startChoreographerThreadIfNotStarted(); mViewManager.onSurfaceChanged(width, height); }