Fixes bug #1889, allows for GL Context deletion/recreation on Android
authorGabriel Jacobo <gabomdq@gmail.com>
Sat, 03 Aug 2013 12:54:39 -0300
changeset 756752da75545aaa
parent 7566 b14d00c30095
child 7568 d50ca5920421
Fixes bug #1889, allows for GL Context deletion/recreation on Android
Thanks ny00!
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/video/android/SDL_androidgl.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Sat Aug 03 02:20:00 2013 -0400
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Sat Aug 03 12:54:39 2013 -0300
     1.3 @@ -72,6 +72,7 @@
     1.4          // Set up the surface
     1.5          mEGLSurface = EGL10.EGL_NO_SURFACE;
     1.6          mSurface = new SDLSurface(getApplication());
     1.7 +        mEGLContext = EGL10.EGL_NO_CONTEXT;
     1.8  
     1.9          mLayout = new AbsoluteLayout(this);
    1.10          mLayout.addView(mSurface);
    1.11 @@ -249,6 +250,20 @@
    1.12      public static boolean createGLContext(int majorVersion, int minorVersion, int[] attribs) {
    1.13          return initEGL(majorVersion, minorVersion, attribs);
    1.14      }
    1.15 +    
    1.16 +    public static void deleteGLContext() {
    1.17 +        if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext != EGL10.EGL_NO_CONTEXT) {
    1.18 +            EGL10 egl = (EGL10)EGLContext.getEGL();
    1.19 +            egl.eglMakeCurrent(SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
    1.20 +            egl.eglDestroyContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLContext);
    1.21 +            SDLActivity.mEGLContext = EGL10.EGL_NO_CONTEXT;
    1.22 +
    1.23 +            if (SDLActivity.mEGLSurface != EGL10.EGL_NO_SURFACE) {
    1.24 +                egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
    1.25 +                SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE;
    1.26 +            }
    1.27 +        }
    1.28 +    }
    1.29  
    1.30      public static void flipBuffers() {
    1.31          flipEGL();
    1.32 @@ -314,19 +329,20 @@
    1.33      // EGL functions
    1.34      public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
    1.35          try {
    1.36 +            EGL10 egl = (EGL10)EGLContext.getEGL();
    1.37 +            
    1.38              if (SDLActivity.mEGLDisplay == null) {
    1.39 +                SDLActivity.mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    1.40 +                int[] version = new int[2];
    1.41 +                egl.eglInitialize(SDLActivity.mEGLDisplay, version);
    1.42 +            }
    1.43 +            
    1.44 +            if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
    1.45 +                // No current GL context exists, we will create a new one.
    1.46                  Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
    1.47 -
    1.48 -                EGL10 egl = (EGL10)EGLContext.getEGL();
    1.49 -
    1.50 -                EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    1.51 -
    1.52 -                int[] version = new int[2];
    1.53 -                egl.eglInitialize(dpy, version);
    1.54 -
    1.55                  EGLConfig[] configs = new EGLConfig[128];
    1.56                  int[] num_config = new int[1];
    1.57 -                if (!egl.eglChooseConfig(dpy, attribs, configs, 1, num_config) || num_config[0] == 0) {
    1.58 +                if (!egl.eglChooseConfig(SDLActivity.mEGLDisplay, attribs, configs, 1, num_config) || num_config[0] == 0) {
    1.59                      Log.e("SDL", "No EGL config available");
    1.60                      return false;
    1.61                  }
    1.62 @@ -350,7 +366,7 @@
    1.63                              attribs[j] == EGL10.EGL_ALPHA_SIZE ||
    1.64                              attribs[j] == EGL10.EGL_DEPTH_SIZE ||
    1.65                              attribs[j] == EGL10.EGL_STENCIL_SIZE)) {
    1.66 -                            egl.eglGetConfigAttrib(dpy, configs[i], attribs[j], value);
    1.67 +                            egl.eglGetConfigAttrib(SDLActivity.mEGLDisplay, configs[i], attribs[j], value);
    1.68                              bitdiff += value[0] - attribs[j + 1]; // value is always >= attrib
    1.69                          }
    1.70                      }
    1.71 @@ -364,13 +380,12 @@
    1.72                  }
    1.73                  
    1.74                  Log.d("SDL", "Selected mode with a total bit difference of " + bestdiff);
    1.75 -               
    1.76  
    1.77 -                SDLActivity.mEGLDisplay = dpy;
    1.78                  SDLActivity.mEGLConfig = config;
    1.79                  SDLActivity.mGLMajor = majorVersion;
    1.80                  SDLActivity.mGLMinor = minorVersion;
    1.81              }
    1.82 +            
    1.83              return SDLActivity.createEGLSurface();
    1.84  
    1.85          } catch(Exception e) {
    1.86 @@ -397,7 +412,7 @@
    1.87      public static boolean createEGLSurface() {
    1.88          if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
    1.89              EGL10 egl = (EGL10)EGLContext.getEGL();
    1.90 -            if (SDLActivity.mEGLContext == null) createEGLContext();
    1.91 +            if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) createEGLContext();
    1.92  
    1.93              if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
    1.94                  Log.v("SDL", "Creating new EGL Surface");
     2.1 --- a/src/core/android/SDL_android.c	Sat Aug 03 02:20:00 2013 -0400
     2.2 +++ b/src/core/android/SDL_android.c	Sat Aug 03 12:54:39 2013 -0300
     2.3 @@ -71,6 +71,7 @@
     2.4  
     2.5  // method signatures
     2.6  static jmethodID midCreateGLContext;
     2.7 +static jmethodID midDeleteGLContext;
     2.8  static jmethodID midFlipBuffers;
     2.9  static jmethodID midAudioInit;
    2.10  static jmethodID midAudioWriteShortBuffer;
    2.11 @@ -120,6 +121,8 @@
    2.12  
    2.13      midCreateGLContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.14                                  "createGLContext","(II[I)Z");
    2.15 +    midDeleteGLContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.16 +                                "deleteGLContext","()V");
    2.17      midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.18                                  "flipBuffers","()V");
    2.19      midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    2.20 @@ -361,6 +364,13 @@
    2.21      return success ? SDL_TRUE : SDL_FALSE;
    2.22  }
    2.23  
    2.24 +SDL_bool Android_JNI_DeleteContext(SDL_GLContext context) 
    2.25 +{
    2.26 +    /* There's only one context, so the parameter is ignored for now */
    2.27 +    JNIEnv *env = Android_JNI_GetEnv();
    2.28 +    (*env)->CallStaticBooleanMethod(env, mActivityClass, midDeleteGLContext);
    2.29 +}
    2.30 +
    2.31  void Android_JNI_SwapWindow()
    2.32  {
    2.33      JNIEnv *mEnv = Android_JNI_GetEnv();
     3.1 --- a/src/video/android/SDL_androidgl.c	Sat Aug 03 02:20:00 2013 -0400
     3.2 +++ b/src/video/android/SDL_androidgl.c	Sat Aug 03 12:54:39 2013 -0300
     3.3 @@ -119,7 +119,9 @@
     3.4  void
     3.5  Android_GL_DeleteContext(_THIS, SDL_GLContext context)
     3.6  {
     3.7 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
     3.8 +    if (context) {
     3.9 +        Android_JNI_DeleteContext(context);
    3.10 +    }
    3.11  }
    3.12  
    3.13  #endif /* SDL_VIDEO_DRIVER_ANDROID */