Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
23 #include "SDL_stdinc.h"
25 #include "SDL_android.h"
28 #include "../../events/SDL_events_c.h"
29 #include "../../video/android/SDL_androidkeyboard.h"
30 #include "../../video/android/SDL_androidtouch.h"
31 #include "../../video/android/SDL_androidvideo.h"
33 /* Impelemented in audio/android/SDL_androidaudio.c */
34 extern void Android_RunAudioThread();
37 /*******************************************************************************
38 This file links the Java side of Android with libsdl
39 *******************************************************************************/
41 #include <android/log.h>
44 /*******************************************************************************
46 *******************************************************************************/
47 static JNIEnv* mEnv = NULL;
48 static JNIEnv* mAudioEnv = NULL;
51 static jclass mActivityClass;
54 static jmethodID midCreateGLContext;
55 static jmethodID midFlipBuffers;
56 static jmethodID midAudioInit;
57 static jmethodID midAudioWriteShortBuffer;
58 static jmethodID midAudioWriteByteBuffer;
59 static jmethodID midAudioQuit;
61 // Accelerometer data storage
62 static float fLastAccelerometer[3];
65 /*******************************************************************************
66 Functions called by JNI
67 *******************************************************************************/
70 extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
72 return JNI_VERSION_1_4;
75 // Called before SDL_main() to initialize JNI bindings
76 extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
78 __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
83 midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
84 "createGLContext","(II)Z");
85 midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
87 midAudioInit = mEnv->GetStaticMethodID(mActivityClass,
88 "audioInit", "(IZZI)Ljava/lang/Object;");
89 midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
90 "audioWriteShortBuffer", "([S)V");
91 midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
92 "audioWriteByteBuffer", "([B)V");
93 midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
96 if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
97 !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
98 __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
103 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
104 JNIEnv* env, jclass jcls,
105 jint width, jint height, jint format)
107 Android_SetScreenResolution(width, height, format);
111 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
112 JNIEnv* env, jclass jcls, jint keycode)
114 Android_OnKeyDown(keycode);
118 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
119 JNIEnv* env, jclass jcls, jint keycode)
121 Android_OnKeyUp(keycode);
125 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
126 JNIEnv* env, jclass jcls,
127 jint action, jfloat x, jfloat y, jfloat p)
129 Android_OnTouch(action, x, y, p);
133 extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
134 JNIEnv* env, jclass jcls,
135 jfloat x, jfloat y, jfloat z)
137 fLastAccelerometer[0] = x;
138 fLastAccelerometer[1] = y;
139 fLastAccelerometer[2] = z;
143 extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
144 JNIEnv* env, jclass cls)
146 // Inject a SDL_QUIT event
150 extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
151 JNIEnv* env, jclass cls)
153 /* This is the audio thread, with a different environment */
156 Android_RunAudioThread();
160 /*******************************************************************************
161 Functions called by SDL into Java
162 *******************************************************************************/
163 extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
165 if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
172 extern "C" void Android_JNI_SwapWindow()
174 mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers);
177 extern "C" void Android_JNI_SetActivityTitle(const char *title)
181 mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
183 mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
187 extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
190 for (i = 0; i < 3; ++i) {
191 values[i] = fLastAccelerometer[i];
198 static jboolean audioBuffer16Bit = JNI_FALSE;
199 static jboolean audioBufferStereo = JNI_FALSE;
200 static jobject audioBuffer = NULL;
201 static void* audioBufferPinned = NULL;
203 extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
205 int audioBufferFrames;
207 __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
208 audioBuffer16Bit = is16Bit;
209 audioBufferStereo = channelCount > 1;
211 audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
213 if (audioBuffer == NULL) {
214 __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
217 audioBuffer = mEnv->NewGlobalRef(audioBuffer);
219 jboolean isCopy = JNI_FALSE;
220 if (audioBuffer16Bit) {
221 audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
222 audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
224 audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
225 audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
227 if (audioBufferStereo) {
228 audioBufferFrames /= 2;
231 return audioBufferFrames;
234 extern "C" void * Android_JNI_GetAudioBuffer()
236 return audioBufferPinned;
239 extern "C" void Android_JNI_WriteAudioBuffer()
241 if (audioBuffer16Bit) {
242 mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
243 mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
245 mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
246 mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
249 /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
252 extern "C" void Android_JNI_CloseAudioDevice()
254 mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit);
257 mEnv->DeleteGlobalRef(audioBuffer);
259 audioBufferPinned = NULL;
263 /* vi: set ts=4 sw=4 expandtab: */