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

Commit

Permalink
Try to create an OpenGL ES 2.0 context on Android and successfully fa…
Browse files Browse the repository at this point in the history
…ll back to OpenGL ES 1.1 if that fails.
  • Loading branch information
slouken committed Feb 8, 2011
1 parent 564ad83 commit 401f4a8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
40 changes: 31 additions & 9 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -101,8 +101,8 @@ public static native void onNativeTouch(int action, float x,

// Java functions called from C

public static void createGLContext() {
mSurface.initEGL();
public static boolean createGLContext(int majorVersion, int minorVersion) {
return mSurface.initEGL(majorVersion, minorVersion);
}

public static void flipBuffers() {
Expand Down Expand Up @@ -351,32 +351,54 @@ public void onDraw(Canvas canvas) {}


// EGL functions
public boolean initEGL() {
Log.v("SDL", "Starting up");
public boolean initEGL(int majorVersion, int minorVersion) {
Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);

try {

EGL10 egl = (EGL10)EGLContext.getEGL();

EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

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_NONE
//EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_RENDERABLE_TYPE, renderableType,
EGL10.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[1];
egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
Log.e("SDL", "No EGL config available");
return false;
}
EGLConfig config = configs[0];

EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
if (ctx == EGL10.EGL_NO_CONTEXT) {
Log.e("SDL", "Couldn't create context");
return false;
}

EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
if (surface == EGL10.EGL_NO_SURFACE) {
Log.e("SDL", "Couldn't create surface");
return false;
}

egl.eglMakeCurrent(dpy, surface, surface, ctx);
if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) {
Log.e("SDL", "Couldn't make context current");
return false;
}

mEGLContext = ctx;
mEGLDisplay = dpy;
Expand Down
11 changes: 8 additions & 3 deletions src/core/android/SDL_android.cpp
Expand Up @@ -20,6 +20,7 @@
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_stdinc.h"

#include "SDL_android.h"

Expand Down Expand Up @@ -80,7 +81,7 @@ extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
mActivityClass = cls;

midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
"createGLContext","()V");
"createGLContext","(II)Z");
midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
"flipBuffers","()V");
midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
Expand Down Expand Up @@ -159,9 +160,13 @@ extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
/*******************************************************************************
Functions called by SDL into Java
*******************************************************************************/
extern "C" void Android_JNI_CreateContext()
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
{
mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext);
if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
return SDL_TRUE;
} else {
return SDL_FALSE;
}
}

extern "C" void Android_JNI_SwapWindow()
Expand Down
2 changes: 1 addition & 1 deletion src/core/android/SDL_android.h
Expand Up @@ -29,7 +29,7 @@ extern "C" {
#endif

/* Interface from the SDL library into the Android Java activity */
extern void Android_JNI_CreateContext();
extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion);
extern void Android_JNI_SwapWindow();
extern void Android_JNI_SetActivityTitle(const char *title);
extern void Android_JNI_GetAccelerometerValues(float values[3]);
Expand Down
8 changes: 8 additions & 0 deletions src/render/opengles2/SDL_render_gles2.c
Expand Up @@ -1071,11 +1071,19 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
{
SDL_Renderer *renderer;
GLES2_DriverContext *rdata;
Uint32 window_flags;
GLint nFormats;
#ifndef ZUNE_HD
GLboolean hasCompiler;
#endif

window_flags = SDL_GetWindowFlags(window);
if (!(window_flags & SDL_WINDOW_OPENGL)) {
if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
return NULL;
}
}

/* Create the renderer struct */
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
if (!renderer) {
Expand Down
8 changes: 7 additions & 1 deletion src/video/android/SDL_androidgl.c
Expand Up @@ -55,7 +55,11 @@ Android_GL_UnloadLibrary(_THIS)
SDL_GLContext
Android_GL_CreateContext(_THIS, SDL_Window * window)
{
Android_JNI_CreateContext();
if (!Android_JNI_CreateContext(_this->gl_config.major_version,
_this->gl_config.minor_version)) {
SDL_SetError("Couldn't create OpenGL context - see Android log for details");
return NULL;
}
return (SDL_GLContext)1;
}

Expand Down Expand Up @@ -91,3 +95,5 @@ Android_GL_DeleteContext(_THIS, SDL_GLContext context)
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
}

/* vi: set ts=4 sw=4 expandtab: */
6 changes: 6 additions & 0 deletions src/video/android/SDL_androidwindow.c
Expand Up @@ -41,6 +41,12 @@ Android_CreateWindow(_THIS, SDL_Window * window)
window->w = Android_ScreenWidth;
window->h = Android_ScreenHeight;

window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */
window->flags |= SDL_WINDOW_OPENGL; /* window is always OpenGL */
window->flags |= SDL_WINDOW_FULLSCREEN; /* window is always fullscreen */
window->flags |= SDL_WINDOW_SHOWN; /* only one window on Android */
window->flags |= SDL_WINDOW_INPUT_FOCUS; /* always has input focus */

return 0;
}

Expand Down

0 comments on commit 401f4a8

Please sign in to comment.