/**
     * 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;
    }
 @Override
 public void destroySurface(
     final EGL10 egl, final EGLDisplay display, final EGLSurface surface) {
   Log.v(
       TAG,
       "EGLWindowSurfaceFactory.destroySurface " + Integer.toHexString(surface.hashCode()));
   egl.eglDestroySurface(display, surface);
 }
 private void destroySurfaceForTimeWarp() {
   if (null != mMainSurface) {
     Log.v(TAG, "destroying mMainSurface: 0x%x", mMainSurface.hashCode());
     final EGL10 egl = (EGL10) EGLContext.getEGL();
     final EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
     egl.eglDestroySurface(display, mMainSurface);
     mMainSurface = null;
   }
 }
 @Override
 public EGLSurface createWindowSurface(
     final EGL10 egl,
     final EGLDisplay display,
     final EGLConfig config,
     final Object ignoredNativeWindow) {
   final int[] surfaceAttribs = {
     EGL10.EGL_WIDTH, 16,
     EGL10.EGL_HEIGHT, 16,
     EGL10.EGL_NONE
   };
   mPixelBuffer = egl.eglCreatePbufferSurface(display, config, surfaceAttribs);
   if (EGL10.EGL_NO_SURFACE == mPixelBuffer) {
     throw new IllegalStateException(
         "Pixel buffer surface not created; egl error 0x"
             + Integer.toHexString(egl.eglGetError()));
   }
   Log.v(
       TAG,
       "EGLWindowSurfaceFactory.createWindowSurface : "
           + Integer.toHexString(mPixelBuffer.hashCode()));
   return mPixelBuffer;
 }
        @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);
        }