More Android cleanup:
authorSam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 17:53:06 -0800
changeset 498958b6bb4a45e9
parent 4988 f9af88a9c823
child 4990 397e748d901a
More Android cleanup:
* Formalized the interface with Java methods in SDL_android.h
* We don't need the feature system, at least right now
* Fixed waiting for the SDLMain thread
android-project/src/org/libsdl/app/SDLActivity.java
src/SDL_android.cpp
src/audio/android/SDL_androidaudio.c
src/video/android/SDL_androidgl.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 12 16:35:03 2011 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 12 17:53:06 2011 -0800
     1.3 @@ -30,14 +30,6 @@
     1.4  
     1.5      // Audio
     1.6      private static AudioTrack mAudioTrack;
     1.7 -    private static boolean bAudioIsEnabled;
     1.8 -
     1.9 -    // Sensors
    1.10 -    private static boolean bAccelIsEnabled;
    1.11 -
    1.12 -    // feature IDs. Must match up on the C side as well.
    1.13 -    private static int FEATURE_AUDIO = 1;
    1.14 -    private static int FEATURE_ACCEL = 2;
    1.15  
    1.16      // Load the .so
    1.17      static {
    1.18 @@ -47,6 +39,7 @@
    1.19  
    1.20      // Setup
    1.21      protected void onCreate(Bundle savedInstanceState) {
    1.22 +        //Log.v("SDL", "onCreate()");
    1.23          super.onCreate(savedInstanceState);
    1.24          
    1.25          // So we can call stuff from static callbacks
    1.26 @@ -57,44 +50,16 @@
    1.27          setContentView(mSurface);
    1.28          SurfaceHolder holder = mSurface.getHolder();
    1.29          holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    1.30 -        
    1.31      }
    1.32  
    1.33 -    // Audio
    1.34 -    public static boolean initAudio(){        
    1.35 -
    1.36 -        // blah. Hardcoded things are bad. FIXME when we have more sound stuff
    1.37 -        // working properly. 
    1.38 -        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
    1.39 -                    11025,
    1.40 -                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
    1.41 -                    AudioFormat.ENCODING_PCM_8BIT,
    1.42 -                    2048,
    1.43 -                    AudioTrack.MODE_STREAM);   
    1.44 -        bAudioIsEnabled = true;     
    1.45 -        return true;
    1.46 -    }
    1.47 -
    1.48 -    // Accel
    1.49 -    public static boolean initAccel(){
    1.50 -        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true);
    1.51 -        bAccelIsEnabled = true;
    1.52 -        return true;
    1.53 -    }
    1.54 -    
    1.55 -    public static boolean closeAccel(){
    1.56 -        mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false);
    1.57 -        bAccelIsEnabled = false;
    1.58 -        return true;
    1.59 -    }
    1.60 -    
    1.61 -
    1.62      // Events
    1.63      protected void onPause() {
    1.64 +        //Log.v("SDL", "onPause()");
    1.65          super.onPause();
    1.66      }
    1.67  
    1.68      protected void onResume() {
    1.69 +        //Log.v("SDL", "onResume()");
    1.70          super.onResume();
    1.71      }
    1.72  
    1.73 @@ -121,39 +86,23 @@
    1.74  
    1.75      public static void updateAudio(byte [] buf) {
    1.76      
    1.77 -        if(mAudioTrack == null){
    1.78 -            return;
    1.79 +        if(mAudioTrack == null) {
    1.80 +            // Hardcoded things are bad. FIXME when we have more sound stuff
    1.81 +            // working properly. 
    1.82 +            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
    1.83 +                        11025,
    1.84 +                        AudioFormat.CHANNEL_CONFIGURATION_MONO,
    1.85 +                        AudioFormat.ENCODING_PCM_8BIT,
    1.86 +                        2048,
    1.87 +                        AudioTrack.MODE_STREAM);   
    1.88          }
    1.89 -        
    1.90 +
    1.91          mAudioTrack.write(buf, 0, buf.length);
    1.92          mAudioTrack.play();
    1.93          
    1.94 -        Log.v("SDL","Played some audio");
    1.95 +        Log.v("SDL", "Played some audio");
    1.96      }
    1.97  
    1.98 -    public static void enableFeature(int featureid, int enabled) {
    1.99 -         Log.v("SDL","Feature " + featureid + " = " + enabled);
   1.100 -
   1.101 -        // Yuck. This is all horribly inelegent. If it gets to more than a few
   1.102 -        // 'features' I'll rip this out and make something nicer, I promise :)
   1.103 -        if(featureid == FEATURE_AUDIO){
   1.104 -            if(enabled == 1){
   1.105 -                initAudio();
   1.106 -            }else{
   1.107 -                // We don't have one of these yet...
   1.108 -                //closeAudio(); 
   1.109 -            }
   1.110 -        }
   1.111 -
   1.112 -        else if(featureid == FEATURE_ACCEL){
   1.113 -            if(enabled == 1){
   1.114 -                initAccel();
   1.115 -            }else{
   1.116 -                closeAccel();
   1.117 -            }
   1.118 -        }
   1.119 -    }
   1.120 -    
   1.121  }
   1.122  
   1.123  /**
   1.124 @@ -164,7 +113,7 @@
   1.125          // Runs SDL_main()
   1.126          SDLActivity.nativeInit();
   1.127  
   1.128 -        Log.v("SDL","SDL thread terminated");
   1.129 +        //Log.v("SDL", "SDL thread terminated");
   1.130      }
   1.131  }
   1.132  
   1.133 @@ -205,66 +154,77 @@
   1.134  
   1.135      // Called when we have a valid drawing surface
   1.136      public void surfaceCreated(SurfaceHolder holder) {
   1.137 +        //Log.v("SDL", "surfaceCreated()");
   1.138 +
   1.139 +        enableSensor(Sensor.TYPE_ACCELEROMETER, true);
   1.140      }
   1.141  
   1.142      // Called when we lose the surface
   1.143      public void surfaceDestroyed(SurfaceHolder holder) {
   1.144 +        //Log.v("SDL", "surfaceDestroyed()");
   1.145  
   1.146          // Send a quit message to the application
   1.147          SDLActivity.nativeQuit();
   1.148  
   1.149          // Now wait for the SDL thread to quit
   1.150          if (mSDLThread != null) {
   1.151 -            try {
   1.152 -                mSDLThread.wait();
   1.153 -            } catch(Exception e) {
   1.154 -                Log.v("SDL","Problem stopping thread: " + e);
   1.155 -            }
   1.156 +            //synchronized (mSDLThread) {
   1.157 +                try {
   1.158 +                    mSDLThread.join();
   1.159 +                } catch(Exception e) {
   1.160 +                    Log.v("SDL", "Problem stopping thread: " + e);
   1.161 +                }
   1.162 +            //}
   1.163 +            mSDLThread = null;
   1.164 +
   1.165 +            //Log.v("SDL", "Finished waiting for SDL thread");
   1.166          }
   1.167 +
   1.168 +        enableSensor(Sensor.TYPE_ACCELEROMETER, false);
   1.169      }
   1.170  
   1.171      // Called when the surface is resized
   1.172      public void surfaceChanged(SurfaceHolder holder,
   1.173                                 int format, int width, int height) {
   1.174 -        Log.v("SDL","Surface resized");
   1.175 +        //Log.v("SDL", "surfaceChanged()");
   1.176  
   1.177          int sdlFormat = 0;
   1.178          switch (format) {
   1.179          case PixelFormat.A_8:
   1.180 -            Log.v("SDL","pixel format A_8");
   1.181 +            Log.v("SDL", "pixel format A_8");
   1.182              break;
   1.183          case PixelFormat.LA_88:
   1.184 -            Log.v("SDL","pixel format LA_88");
   1.185 +            Log.v("SDL", "pixel format LA_88");
   1.186              break;
   1.187          case PixelFormat.L_8:
   1.188 -            Log.v("SDL","pixel format L_8");
   1.189 +            Log.v("SDL", "pixel format L_8");
   1.190              break;
   1.191          case PixelFormat.RGBA_4444:
   1.192 -            Log.v("SDL","pixel format RGBA_4444");
   1.193 +            Log.v("SDL", "pixel format RGBA_4444");
   1.194              sdlFormat = 0x85421002; // SDL_PIXELFORMAT_RGBA4444
   1.195              break;
   1.196          case PixelFormat.RGBA_5551:
   1.197 -            Log.v("SDL","pixel format RGBA_5551");
   1.198 +            Log.v("SDL", "pixel format RGBA_5551");
   1.199              sdlFormat = 0x85441002; // SDL_PIXELFORMAT_RGBA5551
   1.200              break;
   1.201          case PixelFormat.RGBA_8888:
   1.202 -            Log.v("SDL","pixel format RGBA_8888");
   1.203 +            Log.v("SDL", "pixel format RGBA_8888");
   1.204              sdlFormat = 0x86462004; // SDL_PIXELFORMAT_RGBA8888
   1.205              break;
   1.206          case PixelFormat.RGBX_8888:
   1.207 -            Log.v("SDL","pixel format RGBX_8888");
   1.208 +            Log.v("SDL", "pixel format RGBX_8888");
   1.209              sdlFormat = 0x86262004; // SDL_PIXELFORMAT_RGBX8888
   1.210              break;
   1.211          case PixelFormat.RGB_332:
   1.212 -            Log.v("SDL","pixel format RGB_332");
   1.213 +            Log.v("SDL", "pixel format RGB_332");
   1.214              sdlFormat = 0x84110801; // SDL_PIXELFORMAT_RGB332
   1.215              break;
   1.216          case PixelFormat.RGB_565:
   1.217 -            Log.v("SDL","pixel format RGB_565");
   1.218 +            Log.v("SDL", "pixel format RGB_565");
   1.219              sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565
   1.220              break;
   1.221          case PixelFormat.RGB_888:
   1.222 -            Log.v("SDL","pixel format RGB_888");
   1.223 +            Log.v("SDL", "pixel format RGB_888");
   1.224              // Not sure this is right, maybe SDL_PIXELFORMAT_RGB24 instead?
   1.225              sdlFormat = 0x86161804; // SDL_PIXELFORMAT_RGB888
   1.226              break;
   1.227 @@ -316,7 +276,7 @@
   1.228  
   1.229          } catch(Exception e) {
   1.230              Log.v("SDL", e + "");
   1.231 -            for(StackTraceElement s : e.getStackTrace()){
   1.232 +            for(StackTraceElement s : e.getStackTrace()) {
   1.233                  Log.v("SDL", s.toString());
   1.234              }
   1.235          }
   1.236 @@ -328,7 +288,6 @@
   1.237      public void flipEGL() {
   1.238          try {
   1.239              EGL10 egl = (EGL10)EGLContext.getEGL();
   1.240 -            GL10 gl = (GL10)mEGLContext.getGL();
   1.241  
   1.242              egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
   1.243  
   1.244 @@ -341,20 +300,22 @@
   1.245              
   1.246          } catch(Exception e) {
   1.247              Log.v("SDL", "flipEGL(): " + e);
   1.248 -            for(StackTraceElement s : e.getStackTrace()){
   1.249 +            for(StackTraceElement s : e.getStackTrace()) {
   1.250                  Log.v("SDL", s.toString());
   1.251              }
   1.252          }
   1.253      }
   1.254  
   1.255      // Key events
   1.256 -    public boolean onKey(View  v, int keyCode, KeyEvent event){
   1.257 +    public boolean onKey(View  v, int keyCode, KeyEvent event) {
   1.258  
   1.259          if (event.getAction() == KeyEvent.ACTION_DOWN) {
   1.260 +            //Log.v("SDL", "key down: " + keyCode);
   1.261              SDLActivity.onNativeKeyDown(keyCode);
   1.262              return true;
   1.263          }
   1.264          else if (event.getAction() == KeyEvent.ACTION_UP) {
   1.265 +            //Log.v("SDL", "key up: " + keyCode);
   1.266              SDLActivity.onNativeKeyUp(keyCode);
   1.267              return true;
   1.268          }
     2.1 --- a/src/SDL_android.cpp	Wed Jan 12 16:35:03 2011 -0800
     2.2 +++ b/src/SDL_android.cpp	Wed Jan 12 17:53:06 2011 -0800
     2.3 @@ -21,6 +21,8 @@
     2.4  */
     2.5  #include "SDL_config.h"
     2.6  
     2.7 +#include "SDL_android.h"
     2.8 +
     2.9  extern "C" {
    2.10  #include "events/SDL_events_c.h"
    2.11  #include "video/android/SDL_androidkeyboard.h"
    2.12 @@ -47,12 +49,8 @@
    2.13  //method signatures
    2.14  jmethodID midCreateGLContext;
    2.15  jmethodID midFlipBuffers;
    2.16 -jmethodID midEnableFeature;
    2.17  jmethodID midUpdateAudio;
    2.18  
    2.19 -//If we're not the active app, don't try to render
    2.20 -bool bRenderingEnabled = false;
    2.21 -
    2.22  //Feature IDs
    2.23  static const int FEATURE_AUDIO = 1;
    2.24  static const int FEATURE_ACCEL = 2;
    2.25 @@ -84,11 +82,9 @@
    2.26      mActivityInstance = cls;
    2.27      midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    2.28      midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    2.29 -    midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    2.30      midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
    2.31  
    2.32 -    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
    2.33 -        !midUpdateAudio) {
    2.34 +    if(!midCreateGLContext || !midFlipBuffers || !midUpdateAudio) {
    2.35          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    2.36      } else {
    2.37  #ifdef DEBUG
    2.38 @@ -136,9 +132,6 @@
    2.39  extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( JNIEnv*  env, 
    2.40                                                                  jobject obj )
    2.41  {    
    2.42 -    // Stop rendering as we're no longer in the foreground
    2.43 -    bRenderingEnabled = false;
    2.44 -
    2.45      // Inject a SDL_QUIT event
    2.46      SDL_SendQuit();
    2.47  }
    2.48 @@ -165,32 +158,17 @@
    2.49  /*******************************************************************************
    2.50               Functions called by SDL into Java
    2.51  *******************************************************************************/
    2.52 -extern "C" void Android_CreateContext()
    2.53 +extern "C" void Android_JNI_CreateContext()
    2.54  {
    2.55 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
    2.56 -
    2.57 -    bRenderingEnabled = true;
    2.58 -
    2.59 -    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
    2.60 +    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext); 
    2.61  }
    2.62  
    2.63 -extern "C" void Android_Render()
    2.64 +extern "C" void Android_JNI_SwapWindow()
    2.65  {
    2.66 -    if (!bRenderingEnabled) {
    2.67 -        return;
    2.68 -    }
    2.69 -
    2.70 -    // When we get here, we've accumulated a full frame    
    2.71      mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers); 
    2.72  }
    2.73  
    2.74 -extern "C" void Android_EnableFeature(int featureid, bool enabled)
    2.75 -{
    2.76 -    mEnv->CallStaticVoidMethod(mActivityInstance, midEnableFeature, 
    2.77 -                                featureid, (int)enabled); 
    2.78 -}
    2.79 -
    2.80 -extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len)
    2.81 +extern "C" void Android_JNI_UpdateAudioBuffer(unsigned char *buf, int len)
    2.82  {
    2.83      //Annoyingly we can't just call into Java from any thread. Because the audio
    2.84      //callback is dispatched from the SDL audio thread (that wasn't made from
     3.1 --- a/src/audio/android/SDL_androidaudio.c	Wed Jan 12 16:35:03 2011 -0800
     3.2 +++ b/src/audio/android/SDL_androidaudio.c	Wed Jan 12 17:53:06 2011 -0800
     3.3 @@ -28,8 +28,7 @@
     3.4  #include "SDL_audio.h"
     3.5  #include "../SDL_audio_c.h"
     3.6  #include "SDL_androidaudio.h"
     3.7 -
     3.8 -extern void Android_UpdateAudioBuffer(unsigned char *buf, int len);
     3.9 +#include "../../SDL_android.h"
    3.10  
    3.11  #include <android/log.h>
    3.12  
    3.13 @@ -94,7 +93,7 @@
    3.14  //    sound->len = this->hidden->mixlen; /* size of raw data pointed to above */
    3.15  
    3.16  
    3.17 -    Android_UpdateAudioBuffer(this->hidden->mixbuf, this->hidden->mixlen);
    3.18 +    Android_JNI_UpdateAudioBuffer(this->hidden->mixbuf, this->hidden->mixlen);
    3.19      
    3.20      return this->hidden->mixbuf;        /* is this right? */
    3.21  }
     4.1 --- a/src/video/android/SDL_androidgl.c	Wed Jan 12 16:35:03 2011 -0800
     4.2 +++ b/src/video/android/SDL_androidgl.c	Wed Jan 12 17:53:06 2011 -0800
     4.3 @@ -26,57 +26,68 @@
     4.4  #include "SDL_video.h"
     4.5  
     4.6  #include "SDL_androidvideo.h"
     4.7 +#include "../../SDL_android.h"
     4.8  
     4.9  #include <android/log.h>
    4.10  
    4.11 -#include <pthread.h>
    4.12 -
    4.13 -/*
    4.14 -These things are in the JNI android support
    4.15 -*/
    4.16 -extern void Android_CreateContext();
    4.17 -extern void Android_Render();
    4.18  
    4.19  /* GL functions */
    4.20 -int Android_GL_LoadLibrary(_THIS, const char *path){
    4.21 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_LoadLibrary\n");
    4.22 -	return 0;
    4.23 +int
    4.24 +Android_GL_LoadLibrary(_THIS, const char *path)
    4.25 +{
    4.26 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_LoadLibrary\n");
    4.27 +    return 0;
    4.28  }
    4.29  
    4.30 -void *Android_GL_GetProcAddress(_THIS, const char *proc){
    4.31 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetProcAddress\n");
    4.32 -	return 0;
    4.33 +void *
    4.34 +Android_GL_GetProcAddress(_THIS, const char *proc)
    4.35 +{
    4.36 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetProcAddress\n");
    4.37 +    return 0;
    4.38  }
    4.39  
    4.40 -void Android_GL_UnloadLibrary(_THIS){
    4.41 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_UnloadLibrary\n");
    4.42 +void
    4.43 +Android_GL_UnloadLibrary(_THIS)
    4.44 +{
    4.45 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_UnloadLibrary\n");
    4.46  }
    4.47  
    4.48 -SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window){
    4.49 -	Android_CreateContext();
    4.50 -	return 1;
    4.51 +SDL_GLContext
    4.52 +Android_GL_CreateContext(_THIS, SDL_Window * window)
    4.53 +{
    4.54 +    Android_JNI_CreateContext();
    4.55 +    return 1;
    4.56  }
    4.57  
    4.58 -int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
    4.59 -                              SDL_GLContext context){
    4.60 -	//__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_MakeCurrent\n");
    4.61 -	return 0;
    4.62 +int
    4.63 +Android_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
    4.64 +{
    4.65 +    /* There's only one context, nothing to do... */
    4.66 +    return 0;
    4.67  }
    4.68  
    4.69 -int Android_GL_SetSwapInterval(_THIS, int interval){
    4.70 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SetSwapInterval\n");
    4.71 -	return 0;
    4.72 +int
    4.73 +Android_GL_SetSwapInterval(_THIS, int interval)
    4.74 +{
    4.75 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SetSwapInterval\n");
    4.76 +    return 0;
    4.77  }
    4.78  
    4.79 -int Android_GL_GetSwapInterval(_THIS){
    4.80 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetSwapInterval\n");
    4.81 -	return 0;
    4.82 +int
    4.83 +Android_GL_GetSwapInterval(_THIS)
    4.84 +{
    4.85 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetSwapInterval\n");
    4.86 +    return 0;
    4.87  }
    4.88  
    4.89 -void Android_GL_SwapWindow(_THIS, SDL_Window * window){
    4.90 -	Android_Render();
    4.91 +void
    4.92 +Android_GL_SwapWindow(_THIS, SDL_Window * window)
    4.93 +{
    4.94 +    Android_JNI_SwapWindow();
    4.95  }
    4.96  
    4.97 -void Android_GL_DeleteContext(_THIS, SDL_GLContext context){
    4.98 -	__android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
    4.99 +void
   4.100 +Android_GL_DeleteContext(_THIS, SDL_GLContext context)
   4.101 +{
   4.102 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_DeleteContext\n");
   4.103  }