Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
authorSam Lantinga <slouken@libsdl.org>
Mon, 07 Feb 2011 17:44:07 -0800
changeset 5222c66b2a778b7e
parent 5221 2ee8112bfc6b
child 5223 ab0d7cecc0f6
Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.cpp
src/core/android/SDL_android.h
src/render/opengles2/SDL_render_gles2.c
src/video/android/SDL_androidgl.c
src/video/android/SDL_androidwindow.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Feb 07 16:45:40 2011 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Feb 07 17:44:07 2011 -0800
     1.3 @@ -101,8 +101,8 @@
     1.4  
     1.5      // Java functions called from C
     1.6  
     1.7 -    public static void createGLContext() {
     1.8 -        mSurface.initEGL();
     1.9 +    public static boolean createGLContext(int majorVersion, int minorVersion) {
    1.10 +        return mSurface.initEGL(majorVersion, minorVersion);
    1.11      }
    1.12  
    1.13      public static void flipBuffers() {
    1.14 @@ -351,11 +351,10 @@
    1.15  
    1.16  
    1.17      // EGL functions
    1.18 -    public boolean initEGL() {
    1.19 -        Log.v("SDL", "Starting up");
    1.20 +    public boolean initEGL(int majorVersion, int minorVersion) {
    1.21 +        Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
    1.22  
    1.23          try {
    1.24 -
    1.25              EGL10 egl = (EGL10)EGLContext.getEGL();
    1.26  
    1.27              EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    1.28 @@ -363,20 +362,43 @@
    1.29              int[] version = new int[2];
    1.30              egl.eglInitialize(dpy, version);
    1.31  
    1.32 +            int EGL_OPENGL_ES_BIT = 1;
    1.33 +            int EGL_OPENGL_ES2_BIT = 4;
    1.34 +            int renderableType = 0;
    1.35 +            if (majorVersion == 2) {
    1.36 +                renderableType = EGL_OPENGL_ES2_BIT;
    1.37 +            } else if (majorVersion == 1) {
    1.38 +                renderableType = EGL_OPENGL_ES_BIT;
    1.39 +            }
    1.40              int[] configSpec = {
    1.41 -                    //EGL10.EGL_DEPTH_SIZE,   16,
    1.42 -                    EGL10.EGL_NONE
    1.43 +                //EGL10.EGL_DEPTH_SIZE,   16,
    1.44 +                EGL10.EGL_RENDERABLE_TYPE, renderableType,
    1.45 +                EGL10.EGL_NONE
    1.46              };
    1.47              EGLConfig[] configs = new EGLConfig[1];
    1.48              int[] num_config = new int[1];
    1.49 -            egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
    1.50 +            if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
    1.51 +                Log.e("SDL", "No EGL config available");
    1.52 +                return false;
    1.53 +            }
    1.54              EGLConfig config = configs[0];
    1.55  
    1.56              EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
    1.57 +            if (ctx == EGL10.EGL_NO_CONTEXT) {
    1.58 +                Log.e("SDL", "Couldn't create context");
    1.59 +                return false;
    1.60 +            }
    1.61  
    1.62              EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
    1.63 +            if (surface == EGL10.EGL_NO_SURFACE) {
    1.64 +                Log.e("SDL", "Couldn't create surface");
    1.65 +                return false;
    1.66 +            }
    1.67  
    1.68 -            egl.eglMakeCurrent(dpy, surface, surface, ctx);
    1.69 +            if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) {
    1.70 +                Log.e("SDL", "Couldn't make context current");
    1.71 +                return false;
    1.72 +            }
    1.73  
    1.74              mEGLContext = ctx;
    1.75              mEGLDisplay = dpy;
     2.1 --- a/src/core/android/SDL_android.cpp	Mon Feb 07 16:45:40 2011 -0800
     2.2 +++ b/src/core/android/SDL_android.cpp	Mon Feb 07 17:44:07 2011 -0800
     2.3 @@ -20,6 +20,7 @@
     2.4      slouken@libsdl.org
     2.5  */
     2.6  #include "SDL_config.h"
     2.7 +#include "SDL_stdinc.h"
     2.8  
     2.9  #include "SDL_android.h"
    2.10  
    2.11 @@ -80,7 +81,7 @@
    2.12      mActivityClass = cls;
    2.13  
    2.14      midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
    2.15 -                                "createGLContext","()V");
    2.16 +                                "createGLContext","(II)Z");
    2.17      midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
    2.18                                  "flipBuffers","()V");
    2.19      midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
    2.20 @@ -159,9 +160,13 @@
    2.21  /*******************************************************************************
    2.22               Functions called by SDL into Java
    2.23  *******************************************************************************/
    2.24 -extern "C" void Android_JNI_CreateContext()
    2.25 +extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
    2.26  {
    2.27 -    mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext); 
    2.28 +    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
    2.29 +        return SDL_TRUE;
    2.30 +    } else {
    2.31 +        return SDL_FALSE;
    2.32 +    }
    2.33  }
    2.34  
    2.35  extern "C" void Android_JNI_SwapWindow()
     3.1 --- a/src/core/android/SDL_android.h	Mon Feb 07 16:45:40 2011 -0800
     3.2 +++ b/src/core/android/SDL_android.h	Mon Feb 07 17:44:07 2011 -0800
     3.3 @@ -29,7 +29,7 @@
     3.4  #endif
     3.5  
     3.6  /* Interface from the SDL library into the Android Java activity */
     3.7 -extern void Android_JNI_CreateContext();
     3.8 +extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
     3.9  extern void Android_JNI_SwapWindow();
    3.10  extern void Android_JNI_SetActivityTitle(const char *title);
    3.11  extern void Android_JNI_GetAccelerometerValues(float values[3]);
     4.1 --- a/src/render/opengles2/SDL_render_gles2.c	Mon Feb 07 16:45:40 2011 -0800
     4.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Mon Feb 07 17:44:07 2011 -0800
     4.3 @@ -1071,11 +1071,19 @@
     4.4  {
     4.5      SDL_Renderer *renderer;
     4.6      GLES2_DriverContext *rdata;
     4.7 +    Uint32 window_flags;
     4.8      GLint nFormats;
     4.9  #ifndef ZUNE_HD
    4.10      GLboolean hasCompiler;
    4.11  #endif
    4.12  
    4.13 +    window_flags = SDL_GetWindowFlags(window);
    4.14 +    if (!(window_flags & SDL_WINDOW_OPENGL)) {
    4.15 +        if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
    4.16 +            return NULL;
    4.17 +        }
    4.18 +    }
    4.19 +
    4.20      /* Create the renderer struct */
    4.21      renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
    4.22      if (!renderer) {
     5.1 --- a/src/video/android/SDL_androidgl.c	Mon Feb 07 16:45:40 2011 -0800
     5.2 +++ b/src/video/android/SDL_androidgl.c	Mon Feb 07 17:44:07 2011 -0800
     5.3 @@ -55,7 +55,11 @@
     5.4  SDL_GLContext
     5.5  Android_GL_CreateContext(_THIS, SDL_Window * window)
     5.6  {
     5.7 -    Android_JNI_CreateContext();
     5.8 +    if (!Android_JNI_CreateContext(_this->gl_config.major_version,
     5.9 +                                   _this->gl_config.minor_version)) {
    5.10 +        SDL_SetError("Couldn't create OpenGL context - see Android log for details");
    5.11 +        return NULL;
    5.12 +    }
    5.13      return (SDL_GLContext)1;
    5.14  }
    5.15  
    5.16 @@ -91,3 +95,5 @@
    5.17  {
    5.18      __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
    5.19  }
    5.20 +
    5.21 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/android/SDL_androidwindow.c	Mon Feb 07 16:45:40 2011 -0800
     6.2 +++ b/src/video/android/SDL_androidwindow.c	Mon Feb 07 17:44:07 2011 -0800
     6.3 @@ -41,6 +41,12 @@
     6.4      window->w = Android_ScreenWidth;
     6.5      window->h = Android_ScreenHeight;
     6.6  
     6.7 +    window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
     6.8 +    window->flags |= SDL_WINDOW_OPENGL;         /* window is always OpenGL */
     6.9 +    window->flags |= SDL_WINDOW_FULLSCREEN;     /* window is always fullscreen */
    6.10 +    window->flags |= SDL_WINDOW_SHOWN;          /* only one window on Android */
    6.11 +    window->flags |= SDL_WINDOW_INPUT_FOCUS;    /* always has input focus */    
    6.12 +
    6.13      return 0;
    6.14  }
    6.15