Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.

This commit is contained in:
Sam Lantinga 2011-02-07 17:44:07 -08:00
parent f582f9e58e
commit 9127589299
6 changed files with 61 additions and 14 deletions

View file

@ -101,8 +101,8 @@ public class SDLActivity extends Activity {
// Java functions called from C // Java functions called from C
public static void createGLContext() { public static boolean createGLContext(int majorVersion, int minorVersion) {
mSurface.initEGL(); return mSurface.initEGL(majorVersion, minorVersion);
} }
public static void flipBuffers() { public static void flipBuffers() {
@ -351,11 +351,10 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// EGL functions // EGL functions
public boolean initEGL() { public boolean initEGL(int majorVersion, int minorVersion) {
Log.v("SDL", "Starting up"); Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
try { try {
EGL10 egl = (EGL10)EGLContext.getEGL(); EGL10 egl = (EGL10)EGLContext.getEGL();
EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
@ -363,20 +362,43 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
int[] version = new int[2]; int[] version = new int[2];
egl.eglInitialize(dpy, version); 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 = { int[] configSpec = {
//EGL10.EGL_DEPTH_SIZE, 16, //EGL10.EGL_DEPTH_SIZE, 16,
EGL10.EGL_NONE EGL10.EGL_RENDERABLE_TYPE, renderableType,
EGL10.EGL_NONE
}; };
EGLConfig[] configs = new EGLConfig[1]; EGLConfig[] configs = new EGLConfig[1];
int[] num_config = new int[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]; EGLConfig config = configs[0];
EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null); 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); 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; mEGLContext = ctx;
mEGLDisplay = dpy; mEGLDisplay = dpy;

View file

@ -20,6 +20,7 @@
slouken@libsdl.org slouken@libsdl.org
*/ */
#include "SDL_config.h" #include "SDL_config.h"
#include "SDL_stdinc.h"
#include "SDL_android.h" #include "SDL_android.h"
@ -80,7 +81,7 @@ extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
mActivityClass = cls; mActivityClass = cls;
midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass, midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
"createGLContext","()V"); "createGLContext","(II)Z");
midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass, midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
"flipBuffers","()V"); "flipBuffers","()V");
midAudioInit = mEnv->GetStaticMethodID(mActivityClass, midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
@ -159,9 +160,13 @@ extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
/******************************************************************************* /*******************************************************************************
Functions called by SDL into Java 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() extern "C" void Android_JNI_SwapWindow()

View file

@ -29,7 +29,7 @@ extern "C" {
#endif #endif
/* Interface from the SDL library into the Android Java activity */ /* 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_SwapWindow();
extern void Android_JNI_SetActivityTitle(const char *title); extern void Android_JNI_SetActivityTitle(const char *title);
extern void Android_JNI_GetAccelerometerValues(float values[3]); extern void Android_JNI_GetAccelerometerValues(float values[3]);

View file

@ -1071,11 +1071,19 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
{ {
SDL_Renderer *renderer; SDL_Renderer *renderer;
GLES2_DriverContext *rdata; GLES2_DriverContext *rdata;
Uint32 window_flags;
GLint nFormats; GLint nFormats;
#ifndef ZUNE_HD #ifndef ZUNE_HD
GLboolean hasCompiler; GLboolean hasCompiler;
#endif #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 */ /* Create the renderer struct */
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer)); renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
if (!renderer) { if (!renderer) {

View file

@ -55,7 +55,11 @@ Android_GL_UnloadLibrary(_THIS)
SDL_GLContext SDL_GLContext
Android_GL_CreateContext(_THIS, SDL_Window * window) 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; return (SDL_GLContext)1;
} }
@ -91,3 +95,5 @@ Android_GL_DeleteContext(_THIS, SDL_GLContext context)
{ {
__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n"); __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
} }
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -41,6 +41,12 @@ Android_CreateWindow(_THIS, SDL_Window * window)
window->w = Android_ScreenWidth; window->w = Android_ScreenWidth;
window->h = Android_ScreenHeight; 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; return 0;
} }