Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed bug 1616 - SDL does not use values set with SDL_GL_SetAttribute…
Browse files Browse the repository at this point in the history
… 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).
  • Loading branch information
slouken committed Dec 31, 2012
1 parent 7418aed commit 15f8982
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
21 changes: 4 additions & 17 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
41 changes: 33 additions & 8 deletions src/core/android/SDL_android.cpp
Expand Up @@ -27,6 +27,7 @@

#include "SDL_system.h"
#include "SDL_android.h"
#include <EGL/egl.h>

extern "C" {
#include "../../events/SDL_events_c.h"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/core/android/SDL_android.h
Expand Up @@ -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]);
Expand Down
11 changes: 10 additions & 1 deletion src/video/android/SDL_androidgl.c
Expand Up @@ -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;
}
Expand Down

0 comments on commit 15f8982

Please sign in to comment.