src/core/android/SDL_android.cpp
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Feb 2011 17:44:07 -0800
changeset 5222 c66b2a778b7e
parent 5092 ed1d54f1290a
child 5262 b530ef003506
permissions -rw-r--r--
Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
slouken@4964
     1
/*
slouken@4964
     2
    SDL - Simple DirectMedia Layer
slouken@4964
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@4964
     4
slouken@4964
     5
    This library is free software; you can redistribute it and/or
slouken@4964
     6
    modify it under the terms of the GNU Lesser General Public
slouken@4964
     7
    License as published by the Free Software Foundation; either
slouken@4964
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@4964
     9
slouken@4964
    10
    This library is distributed in the hope that it will be useful,
slouken@4964
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@4964
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@4964
    13
    Lesser General Public License for more details.
slouken@4964
    14
slouken@4964
    15
    You should have received a copy of the GNU Lesser General Public
slouken@4964
    16
    License along with this library; if not, write to the Free Software
slouken@4964
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@4964
    18
slouken@4964
    19
    Sam Lantinga
slouken@4964
    20
    slouken@libsdl.org
slouken@4964
    21
*/
slouken@4964
    22
#include "SDL_config.h"
slouken@5222
    23
#include "SDL_stdinc.h"
slouken@4964
    24
slouken@4989
    25
#include "SDL_android.h"
slouken@4989
    26
slouken@4980
    27
extern "C" {
slouken@5092
    28
#include "../../events/SDL_events_c.h"
slouken@5092
    29
#include "../../video/android/SDL_androidkeyboard.h"
slouken@5092
    30
#include "../../video/android/SDL_androidtouch.h"
slouken@5092
    31
#include "../../video/android/SDL_androidvideo.h"
slouken@4995
    32
slouken@4995
    33
/* Impelemented in audio/android/SDL_androidaudio.c */
slouken@4995
    34
extern void Android_RunAudioThread();
slouken@4995
    35
} // C
slouken@4980
    36
slouken@4964
    37
/*******************************************************************************
slouken@4964
    38
 This file links the Java side of Android with libsdl
slouken@4964
    39
*******************************************************************************/
slouken@4964
    40
#include <jni.h>
slouken@4964
    41
#include <android/log.h>
slouken@4964
    42
slouken@4964
    43
slouken@4964
    44
/*******************************************************************************
slouken@4964
    45
                               Globals
slouken@4964
    46
*******************************************************************************/
slouken@4995
    47
static JNIEnv* mEnv = NULL;
slouken@4995
    48
static JNIEnv* mAudioEnv = NULL;
slouken@4964
    49
slouken@4995
    50
// Main activity
slouken@4998
    51
static jclass mActivityClass;
slouken@4964
    52
slouken@4995
    53
// method signatures
slouken@4995
    54
static jmethodID midCreateGLContext;
slouken@4995
    55
static jmethodID midFlipBuffers;
slouken@4995
    56
static jmethodID midAudioInit;
slouken@4995
    57
static jmethodID midAudioWriteShortBuffer;
slouken@4995
    58
static jmethodID midAudioWriteByteBuffer;
slouken@4996
    59
static jmethodID midAudioQuit;
slouken@4964
    60
slouken@4995
    61
// Accelerometer data storage
slouken@5000
    62
static float fLastAccelerometer[3];
slouken@4964
    63
slouken@4964
    64
slouken@4964
    65
/*******************************************************************************
slouken@4964
    66
                 Functions called by JNI
slouken@4964
    67
*******************************************************************************/
slouken@4964
    68
slouken@4964
    69
// Library init
slouken@4964
    70
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
slouken@4964
    71
{
slouken@4964
    72
    return JNI_VERSION_1_4;
slouken@4964
    73
}
slouken@4964
    74
slouken@4964
    75
// Called before SDL_main() to initialize JNI bindings
slouken@4998
    76
extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls)
slouken@4964
    77
{
slouken@4964
    78
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()");
slouken@4964
    79
slouken@4998
    80
    mEnv = env;
slouken@4998
    81
    mActivityClass = cls;
slouken@4998
    82
slouken@4998
    83
    midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass,
slouken@5222
    84
                                "createGLContext","(II)Z");
slouken@4998
    85
    midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    86
                                "flipBuffers","()V");
slouken@4998
    87
    midAudioInit = mEnv->GetStaticMethodID(mActivityClass, 
slouken@4998
    88
                                "audioInit", "(IZZI)Ljava/lang/Object;");
slouken@4998
    89
    midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    90
                                "audioWriteShortBuffer", "([S)V");
slouken@4998
    91
    midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    92
                                "audioWriteByteBuffer", "([B)V");
slouken@4998
    93
    midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
slouken@4998
    94
                                "audioQuit", "()V");
slouken@4964
    95
slouken@4995
    96
    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
slouken@4996
    97
       !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
slouken@4996
    98
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
slouken@4964
    99
    }
slouken@4964
   100
}
slouken@4964
   101
slouken@4995
   102
// Resize
slouken@4995
   103
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize(
slouken@4998
   104
                                    JNIEnv* env, jclass jcls,
slouken@4995
   105
                                    jint width, jint height, jint format)
slouken@4995
   106
{
slouken@4995
   107
    Android_SetScreenResolution(width, height, format);
slouken@4995
   108
}
slouken@4995
   109
slouken@4964
   110
// Keydown
slouken@4995
   111
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
slouken@4998
   112
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   113
{
slouken@4980
   114
    Android_OnKeyDown(keycode);
slouken@4964
   115
}
slouken@4964
   116
slouken@4964
   117
// Keyup
slouken@4995
   118
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp(
slouken@4998
   119
                                    JNIEnv* env, jclass jcls, jint keycode)
slouken@4964
   120
{
slouken@4980
   121
    Android_OnKeyUp(keycode);
slouken@4964
   122
}
slouken@4964
   123
slouken@4964
   124
// Touch
slouken@4995
   125
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch(
slouken@4998
   126
                                    JNIEnv* env, jclass jcls,
slouken@4995
   127
                                    jint action, jfloat x, jfloat y, jfloat p)
slouken@4964
   128
{
slouken@5001
   129
    Android_OnTouch(action, x, y, p);
slouken@4964
   130
}
slouken@4964
   131
slouken@4995
   132
// Accelerometer
slouken@4964
   133
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel(
slouken@4998
   134
                                    JNIEnv* env, jclass jcls,
slouken@4995
   135
                                    jfloat x, jfloat y, jfloat z)
slouken@4964
   136
{
slouken@4964
   137
    fLastAccelerometer[0] = x;
slouken@4964
   138
    fLastAccelerometer[1] = y;
slouken@4964
   139
    fLastAccelerometer[2] = z;   
slouken@4964
   140
}
slouken@4964
   141
slouken@4995
   142
// Quit
slouken@4995
   143
extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit(
slouken@4998
   144
                                    JNIEnv* env, jclass cls)
slouken@4995
   145
{    
slouken@4995
   146
    // Inject a SDL_QUIT event
slouken@4995
   147
    SDL_SendQuit();
slouken@4995
   148
}
slouken@4995
   149
slouken@4995
   150
extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
slouken@4998
   151
                                    JNIEnv* env, jclass cls)
slouken@4995
   152
{
slouken@4998
   153
    /* This is the audio thread, with a different environment */
slouken@4998
   154
    mAudioEnv = env;
slouken@4998
   155
slouken@4996
   156
    Android_RunAudioThread();
slouken@4995
   157
}
slouken@4964
   158
slouken@4964
   159
slouken@4964
   160
/*******************************************************************************
slouken@4964
   161
             Functions called by SDL into Java
slouken@4964
   162
*******************************************************************************/
slouken@5222
   163
extern "C" SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion)
slouken@4964
   164
{
slouken@5222
   165
    if (mEnv->CallStaticBooleanMethod(mActivityClass, midCreateGLContext, majorVersion, minorVersion)) {
slouken@5222
   166
        return SDL_TRUE;
slouken@5222
   167
    } else {
slouken@5222
   168
        return SDL_FALSE;
slouken@5222
   169
    }
slouken@4964
   170
}
slouken@4964
   171
slouken@4989
   172
extern "C" void Android_JNI_SwapWindow()
slouken@4964
   173
{
slouken@4998
   174
    mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); 
slouken@4998
   175
}
slouken@4998
   176
slouken@4998
   177
extern "C" void Android_JNI_SetActivityTitle(const char *title)
slouken@4998
   178
{
slouken@4998
   179
    jmethodID mid;
slouken@4998
   180
slouken@4998
   181
    mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V");
slouken@4998
   182
    if (mid) {
slouken@4998
   183
        mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title));
slouken@4998
   184
    }
slouken@4964
   185
}
slouken@4964
   186
slouken@5000
   187
extern "C" void Android_JNI_GetAccelerometerValues(float values[3])
slouken@5000
   188
{
slouken@5000
   189
    int i;
slouken@5000
   190
    for (i = 0; i < 3; ++i) {
slouken@5000
   191
        values[i] = fLastAccelerometer[i];
slouken@5000
   192
    }
slouken@5000
   193
}
slouken@5000
   194
slouken@4995
   195
//
slouken@4995
   196
// Audio support
slouken@4995
   197
//
slouken@4996
   198
static jboolean audioBuffer16Bit = JNI_FALSE;
slouken@4996
   199
static jboolean audioBufferStereo = JNI_FALSE;
slouken@4996
   200
static jobject audioBuffer = NULL;
slouken@4996
   201
static void* audioBufferPinned = NULL;
slouken@4995
   202
slouken@4995
   203
extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames)
slouken@4964
   204
{
slouken@4996
   205
    int audioBufferFrames;
slouken@4964
   206
slouken@4996
   207
    __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
slouken@4996
   208
    audioBuffer16Bit = is16Bit;
slouken@4996
   209
    audioBufferStereo = channelCount > 1;
slouken@4964
   210
slouken@4998
   211
    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
slouken@4995
   212
slouken@4996
   213
    if (audioBuffer == NULL) {
slouken@4996
   214
        __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
slouken@4996
   215
        return 0;
slouken@4996
   216
    }
slouken@4996
   217
    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
slouken@4995
   218
slouken@4996
   219
    jboolean isCopy = JNI_FALSE;
slouken@4996
   220
    if (audioBuffer16Bit) {
slouken@4996
   221
        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
slouken@4996
   222
        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
slouken@4996
   223
    } else {
slouken@4996
   224
        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
slouken@4996
   225
        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
slouken@4996
   226
    }
slouken@4996
   227
    if (audioBufferStereo) {
slouken@4996
   228
        audioBufferFrames /= 2;
slouken@4996
   229
    }
slouken@4996
   230
slouken@4996
   231
    return audioBufferFrames;
slouken@4995
   232
}
slouken@4995
   233
slouken@4996
   234
extern "C" void * Android_JNI_GetAudioBuffer()
slouken@4995
   235
{
slouken@4996
   236
    return audioBufferPinned;
slouken@4995
   237
}
slouken@4995
   238
slouken@4996
   239
extern "C" void Android_JNI_WriteAudioBuffer()
slouken@4995
   240
{
slouken@4996
   241
    if (audioBuffer16Bit) {
slouken@4996
   242
        mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   243
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer);
slouken@4996
   244
    } else {
slouken@4996
   245
        mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT);
slouken@4998
   246
        mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer);
slouken@4996
   247
    }
slouken@4995
   248
slouken@4996
   249
    /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */
slouken@4995
   250
}
slouken@4995
   251
slouken@4995
   252
extern "C" void Android_JNI_CloseAudioDevice()
slouken@4995
   253
{
slouken@4998
   254
    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
slouken@4964
   255
slouken@4997
   256
    if (audioBuffer) {
slouken@4997
   257
        mEnv->DeleteGlobalRef(audioBuffer);
slouken@4997
   258
        audioBuffer = NULL;
slouken@4997
   259
        audioBufferPinned = NULL;
slouken@4997
   260
    }
slouken@4964
   261
}
slouken@4981
   262
slouken@4981
   263
/* vi: set ts=4 sw=4 expandtab: */