From 15f8982f31fd940d418c2e6d4421177b5f8349bc Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 31 Dec 2012 14:57:36 -0800 Subject: [PATCH] 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). --- .../src/org/libsdl/app/SDLActivity.java | 21 ++-------- src/core/android/SDL_android.cpp | 41 +++++++++++++++---- src/core/android/SDL_android.h | 2 +- src/video/android/SDL_androidgl.c | 11 ++++- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java index dc7aa3198..2277510bb 100644 --- a/android-project/src/org/libsdl/app/SDLActivity.java +++ b/android-project/src/org/libsdl/app/SDLActivity.java @@ -167,8 +167,8 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId, // 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 @@ public static void showTextInput(int x, int y, int w, int h) { // 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 @@ public static boolean initEGL(int majorVersion, int minorVersion) { 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; } diff --git a/src/core/android/SDL_android.cpp b/src/core/android/SDL_android.cpp index fb06cd5c0..3818800ab 100644 --- a/src/core/android/SDL_android.cpp +++ b/src/core/android/SDL_android.cpp @@ -27,6 +27,7 @@ #include "SDL_system.h" #include "SDL_android.h" +#include extern "C" { #include "../../events/SDL_events_c.h" @@ -115,7 +116,7 @@ extern "C" void SDL_Android_Init(JNIEnv* mEnv, jclass cls) 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 @@ class LocalReferenceHolder }; 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() diff --git a/src/core/android/SDL_android.h b/src/core/android/SDL_android.h index d72761bfb..bbfc12a74 100644 --- a/src/core/android/SDL_android.h +++ b/src/core/android/SDL_android.h @@ -30,7 +30,7 @@ extern "C" { #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]); diff --git a/src/video/android/SDL_androidgl.c b/src/video/android/SDL_androidgl.c index df0dfcb55..f4689df0c 100644 --- a/src/video/android/SDL_androidgl.c +++ b/src/video/android/SDL_androidgl.c @@ -74,7 +74,16 @@ SDL_GLContext 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; }