Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android
authorSam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 14:57:36 -0800
changeset 6792a15821a3471f
parent 6791 c19c5232439d
child 6793 c8d1036975b7
Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute on Android

Philipp Wiesemann 2012-10-06 07:19:57 PDT

SDL does not use values set with SDL_GL_SetAttribute on Android.

I attached a patch which adds this functionality and makes it possible to set
(for example) depth buffer size or anti-aliasing in the actual application
instead of modifying the Java source (which seems currently the only way).
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.cpp
src/core/android/SDL_android.h
src/video/android/SDL_androidgl.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Dec 31 14:14:01 2012 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Dec 31 14:57:36 2012 -0800
     1.3 @@ -167,8 +167,8 @@
     1.4  
     1.5      // Java functions called from C
     1.6  
     1.7 -    public static boolean createGLContext(int majorVersion, int minorVersion) {
     1.8 -        return initEGL(majorVersion, minorVersion);
     1.9 +    public static boolean createGLContext(int majorVersion, int minorVersion, int[] attribs) {
    1.10 +        return initEGL(majorVersion, minorVersion, attribs);
    1.11      }
    1.12  
    1.13      public static void flipBuffers() {
    1.14 @@ -251,7 +251,7 @@
    1.15  
    1.16  
    1.17      // EGL functions
    1.18 -    public static boolean initEGL(int majorVersion, int minorVersion) {
    1.19 +    public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
    1.20          try {
    1.21              if (SDLActivity.mEGLDisplay == null) {
    1.22                  Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
    1.23 @@ -263,22 +263,9 @@
    1.24                  int[] version = new int[2];
    1.25                  egl.eglInitialize(dpy, version);
    1.26  
    1.27 -                int EGL_OPENGL_ES_BIT = 1;
    1.28 -                int EGL_OPENGL_ES2_BIT = 4;
    1.29 -                int renderableType = 0;
    1.30 -                if (majorVersion == 2) {
    1.31 -                    renderableType = EGL_OPENGL_ES2_BIT;
    1.32 -                } else if (majorVersion == 1) {
    1.33 -                    renderableType = EGL_OPENGL_ES_BIT;
    1.34 -                }
    1.35 -                int[] configSpec = {
    1.36 -                    //EGL10.EGL_DEPTH_SIZE,   16,
    1.37 -                    EGL10.EGL_RENDERABLE_TYPE, renderableType,
    1.38 -                    EGL10.EGL_NONE
    1.39 -                };
    1.40                  EGLConfig[] configs = new EGLConfig[1];
    1.41                  int[] num_config = new int[1];
    1.42 -                if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
    1.43 +                if (!egl.eglChooseConfig(dpy, attribs, configs, 1, num_config) || num_config[0] == 0) {
    1.44                      Log.e("SDL", "No EGL config available");
    1.45                      return false;
    1.46                  }
     2.1 --- a/src/core/android/SDL_android.cpp	Mon Dec 31 14:14:01 2012 -0800
     2.2 +++ b/src/core/android/SDL_android.cpp	Mon Dec 31 14:57:36 2012 -0800
     2.3 @@ -27,6 +27,7 @@
     2.4  
     2.5  #include "SDL_system.h"
     2.6  #include "SDL_android.h"
     2.7 +#include <EGL/egl.h>
     2.8  
     2.9  extern "C" {
    2.10  #include "../../events/SDL_events_c.h"
    2.11 @@ -115,7 +116,7 @@
    2.12      mActivityClass = (jclass)mEnv->NewGlobalRef(cls);
    2.13  
    2.14      midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
    2.15 -                                "createGLContext","(II)Z");
    2.16 +                                "createGLContext","(II[I)Z");
    2.17      midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
    2.18                                  "flipBuffers","()V");
    2.19      midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
    2.20 @@ -292,14 +293,38 @@
    2.21  };
    2.22  int LocalReferenceHolder::s_active;
    2.23  
    2.24 -extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
    2.25 +extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion,
    2.26 +                                int red, int green, int blue, int alpha,
    2.27 +                                int buffer, int depth, int stencil,
    2.28 +                                int buffers, int samples)
    2.29  {
    2.30 -    JNIEnv *mEnv = Android_JNI_GetEnv();
    2.31 -    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
    2.32 -        return SDL_TRUE;
    2.33 -    } else {
    2.34 -        return SDL_FALSE;
    2.35 -    }
    2.36 +    JNIEnv *env = Android_JNI_GetEnv();
    2.37 +
    2.38 +    jint attribs[] = {
    2.39 +        EGL_RED_SIZE, red,
    2.40 +        EGL_GREEN_SIZE, green,
    2.41 +        EGL_BLUE_SIZE, blue,
    2.42 +        EGL_ALPHA_SIZE, alpha,
    2.43 +        EGL_BUFFER_SIZE, buffer,
    2.44 +        EGL_DEPTH_SIZE, depth,
    2.45 +        EGL_STENCIL_SIZE, stencil,
    2.46 +        EGL_SAMPLE_BUFFERS, buffers,
    2.47 +        EGL_SAMPLES, samples,
    2.48 +        EGL_RENDERABLE_TYPE, (majorVersion == 1 ? EGL_OPENGL_ES_BIT : EGL_OPENGL_ES2_BIT),
    2.49 +        EGL_NONE
    2.50 +    };
    2.51 +    int len = SDL_arraysize(attribs);
    2.52 +
    2.53 +    jintArray array;
    2.54 +
    2.55 +    array = env->NewIntArray(len);
    2.56 +    env->SetIntArrayRegion(array, 0, len, attribs);
    2.57 +
    2.58 +    jboolean success = env->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion, array);
    2.59 +
    2.60 +    env->DeleteLocalRef(array);
    2.61 +
    2.62 +    return success ? SDL_TRUE : SDL_FALSE;
    2.63  }
    2.64  
    2.65  extern "C" void Android_JNI_SwapWindow()
     3.1 --- a/src/core/android/SDL_android.h	Mon Dec 31 14:14:01 2012 -0800
     3.2 +++ b/src/core/android/SDL_android.h	Mon Dec 31 14:57:36 2012 -0800
     3.3 @@ -30,7 +30,7 @@
     3.4  #include "SDL_rect.h"
     3.5  
     3.6  /* Interface from the SDL library into the Android Java activity */
     3.7 -extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
     3.8 +extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion, int red, int green, int blue, int alpha, int buffer, int depth, int stencil, int buffers, int samples);
     3.9  extern void Android_JNI_SwapWindow();
    3.10  extern void Android_JNI_SetActivityTitle(const char *title);
    3.11  extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
     4.1 --- a/src/video/android/SDL_androidgl.c	Mon Dec 31 14:14:01 2012 -0800
     4.2 +++ b/src/video/android/SDL_androidgl.c	Mon Dec 31 14:57:36 2012 -0800
     4.3 @@ -74,7 +74,16 @@
     4.4  Android_GL_CreateContext(_THIS, SDL_Window * window)
     4.5  {
     4.6      if (!Android_JNI_CreateContext(_this->gl_config.major_version,
     4.7 -                                   _this->gl_config.minor_version)) {
     4.8 +                                   _this->gl_config.minor_version,
     4.9 +                                   _this->gl_config.red_size,
    4.10 +                                   _this->gl_config.green_size,
    4.11 +                                   _this->gl_config.blue_size,
    4.12 +                                   _this->gl_config.alpha_size,
    4.13 +                                   _this->gl_config.buffer_size,
    4.14 +                                   _this->gl_config.depth_size,
    4.15 +                                   _this->gl_config.stencil_size,
    4.16 +                                   _this->gl_config.multisamplebuffers,
    4.17 +                                   _this->gl_config.multisamplesamples)) {
    4.18          SDL_SetError("Couldn't create OpenGL context - see Android log for details");
    4.19          return NULL;
    4.20      }