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 6792 a15821a3471f
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
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Dec 31 14:14:01 2012 -0800
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Dec 31 14:57:36 2012 -0800
@@ -167,8 +167,8 @@
 
     // Java functions called from C
 
-    public static boolean createGLContext(int majorVersion, int minorVersion) {
-        return initEGL(majorVersion, minorVersion);
+    public static boolean createGLContext(int majorVersion, int minorVersion, int[] attribs) {
+        return initEGL(majorVersion, minorVersion, attribs);
     }
 
     public static void flipBuffers() {
@@ -251,7 +251,7 @@
 
 
     // EGL functions
-    public static boolean initEGL(int majorVersion, int minorVersion) {
+    public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
         try {
             if (SDLActivity.mEGLDisplay == null) {
                 Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
@@ -263,22 +263,9 @@
                 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) {
+                if (!egl.eglChooseConfig(dpy, attribs, configs, 1, num_config) || num_config[0] == 0) {
                     Log.e("SDL", "No EGL config available");
                     return false;
                 }
--- a/src/core/android/SDL_android.cpp	Mon Dec 31 14:14:01 2012 -0800
+++ b/src/core/android/SDL_android.cpp	Mon Dec 31 14:57:36 2012 -0800
@@ -27,6 +27,7 @@
 
 #include "SDL_system.h"
 #include "SDL_android.h"
+#include <EGL/egl.h>
 
 extern "C" {
 #include "../../events/SDL_events_c.h"
@@ -115,7 +116,7 @@
     mActivityClass = (jclass)mEnv->NewGlobalRef(cls);
 
     midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
-                                "createGLContext","(II)Z");
+                                "createGLContext","(II[I)Z");
     midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
                                 "flipBuffers","()V");
     midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
@@ -292,14 +293,38 @@
 };
 int LocalReferenceHolder::s_active;
 
-extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
+extern "C" 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)
 {
-    JNIEnv *mEnv = Android_JNI_GetEnv();
-    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
-        return SDL_TRUE;
-    } else {
-        return SDL_FALSE;
-    }
+    JNIEnv *env = Android_JNI_GetEnv();
+
+    jint attribs[] = {
+        EGL_RED_SIZE, red,
+        EGL_GREEN_SIZE, green,
+        EGL_BLUE_SIZE, blue,
+        EGL_ALPHA_SIZE, alpha,
+        EGL_BUFFER_SIZE, buffer,
+        EGL_DEPTH_SIZE, depth,
+        EGL_STENCIL_SIZE, stencil,
+        EGL_SAMPLE_BUFFERS, buffers,
+        EGL_SAMPLES, samples,
+        EGL_RENDERABLE_TYPE, (majorVersion == 1 ? EGL_OPENGL_ES_BIT : EGL_OPENGL_ES2_BIT),
+        EGL_NONE
+    };
+    int len = SDL_arraysize(attribs);
+
+    jintArray array;
+
+    array = env->NewIntArray(len);
+    env->SetIntArrayRegion(array, 0, len, attribs);
+
+    jboolean success = env->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion, array);
+
+    env->DeleteLocalRef(array);
+
+    return success ? SDL_TRUE : SDL_FALSE;
 }
 
 extern "C" void Android_JNI_SwapWindow()
--- a/src/core/android/SDL_android.h	Mon Dec 31 14:14:01 2012 -0800
+++ b/src/core/android/SDL_android.h	Mon Dec 31 14:57:36 2012 -0800
@@ -30,7 +30,7 @@
 #include "SDL_rect.h"
 
 /* Interface from the SDL library into the Android Java activity */
-extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
+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);
 extern void Android_JNI_SwapWindow();
 extern void Android_JNI_SetActivityTitle(const char *title);
 extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
--- a/src/video/android/SDL_androidgl.c	Mon Dec 31 14:14:01 2012 -0800
+++ b/src/video/android/SDL_androidgl.c	Mon Dec 31 14:57:36 2012 -0800
@@ -74,7 +74,16 @@
 Android_GL_CreateContext(_THIS, SDL_Window * window)
 {
     if (!Android_JNI_CreateContext(_this->gl_config.major_version,
-                                   _this->gl_config.minor_version)) {
+                                   _this->gl_config.minor_version,
+                                   _this->gl_config.red_size,
+                                   _this->gl_config.green_size,
+                                   _this->gl_config.blue_size,
+                                   _this->gl_config.alpha_size,
+                                   _this->gl_config.buffer_size,
+                                   _this->gl_config.depth_size,
+                                   _this->gl_config.stencil_size,
+                                   _this->gl_config.multisamplebuffers,
+                                   _this->gl_config.multisamplesamples)) {
         SDL_SetError("Couldn't create OpenGL context - see Android log for details");
         return NULL;
     }