Android: prevent concurrency in Android_SetScreenResolution() when exiting
authorSylvain Becker <sylvain.becker@gmail.com>
Thu, 17 Jan 2019 11:05:05 +0100
changeset 125569b2e48b381db
parent 12555 44f55801630b
child 12557 09698ac34c0e
Android: prevent concurrency in Android_SetScreenResolution() when exiting
by checking Android_Window validity

- SDLThread: user application is exiting:
SDL_VideoQuit() and clearing SDL_GetVideoDevice()

- ActivityThread is changing orientation/size
surfaceChanged() > Android_SetScreenResolution() > SDL_GetVideoDevice()

- Separate function into Android_SetScreenResolution() and Android_SendResize(),
formating, and mark Android_DeviceWidth/Heigh as static
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidvideo.h
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jan 17 09:28:30 2019 +0100
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jan 17 11:05:05 2019 +0100
     1.3 @@ -723,7 +723,8 @@
     1.4      public static native void nativePause();
     1.5      public static native void nativeResume();
     1.6      public static native void onNativeDropFile(String filename);
     1.7 -    public static native void onNativeResize(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, int format, float rate);
     1.8 +    public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, int format, float rate);
     1.9 +    public static native void onNativeResize();
    1.10      public static native void onNativeKeyDown(int keycode);
    1.11      public static native void onNativeKeyUp(int keycode);
    1.12      public static native void onNativeKeyboardFocusLost();
    1.13 @@ -1652,7 +1653,8 @@
    1.14  
    1.15          Log.v("SDL", "Window size: " + width + "x" + height);
    1.16          Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight);
    1.17 -        SDLActivity.onNativeResize(width, height, nDeviceWidth, nDeviceHeight, sdlFormat, mDisplay.getRefreshRate());
    1.18 +        SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, sdlFormat, mDisplay.getRefreshRate());
    1.19 +        SDLActivity.onNativeResize();
    1.20  
    1.21          boolean skip = false;
    1.22          int requestedOrientation = SDLActivity.mSingleton.getRequestedOrientation();
     2.1 --- a/src/core/android/SDL_android.c	Thu Jan 17 09:28:30 2019 +0100
     2.2 +++ b/src/core/android/SDL_android.c	Thu Jan 17 11:05:05 2019 +0100
     2.3 @@ -72,11 +72,14 @@
     2.4          JNIEnv *env, jclass jcls,
     2.5          jstring filename);
     2.6  
     2.7 -JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
     2.8 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
     2.9          JNIEnv *env, jclass jcls,
    2.10          jint surfaceWidth, jint surfaceHeight,
    2.11          jint deviceWidth, jint deviceHeight, jint format, jfloat rate);
    2.12  
    2.13 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
    2.14 +        JNIEnv *env, jclass cls);
    2.15 +
    2.16  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceCreated)(
    2.17          JNIEnv *env, jclass jcls);
    2.18  
    2.19 @@ -683,15 +686,29 @@
    2.20      SDL_SendDropComplete(NULL);
    2.21  }
    2.22  
    2.23 -/* Resize */
    2.24 -JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
    2.25 +/* Set screen resolution */
    2.26 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
    2.27                                      JNIEnv *env, jclass jcls,
    2.28                                      jint surfaceWidth, jint surfaceHeight,
    2.29                                      jint deviceWidth, jint deviceHeight, jint format, jfloat rate)
    2.30  {
    2.31      SDL_LockMutex(Android_ActivityMutex);
    2.32  
    2.33 -    Android_SetScreenResolution(Android_Window, surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
    2.34 +    Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
    2.35 +
    2.36 +    SDL_UnlockMutex(Android_ActivityMutex);
    2.37 +}
    2.38 +
    2.39 +/* Resize */
    2.40 +JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
    2.41 +                                    JNIEnv *env, jclass jcls)
    2.42 +{
    2.43 +    SDL_LockMutex(Android_ActivityMutex);
    2.44 +
    2.45 +    if (Android_Window)
    2.46 +    {
    2.47 +        Android_SendResize(Android_Window);
    2.48 +    }
    2.49  
    2.50      SDL_UnlockMutex(Android_ActivityMutex);
    2.51  }
     3.1 --- a/src/video/android/SDL_androidvideo.c	Thu Jan 17 09:28:30 2019 +0100
     3.2 +++ b/src/video/android/SDL_androidvideo.c	Thu Jan 17 11:05:05 2019 +0100
     3.3 @@ -59,15 +59,15 @@
     3.4  
     3.5  
     3.6  /* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
     3.7 -int Android_SurfaceWidth = 0;
     3.8 -int Android_SurfaceHeight = 0;
     3.9 -int Android_DeviceWidth = 0;
    3.10 -int Android_DeviceHeight = 0;
    3.11 +int Android_SurfaceWidth           = 0;
    3.12 +int Android_SurfaceHeight          = 0;
    3.13 +static int Android_DeviceWidth     = 0;
    3.14 +static int Android_DeviceHeight    = 0;
    3.15  static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    3.16 -static int Android_ScreenRate = 0;
    3.17 -
    3.18 -SDL_sem *Android_PauseSem = NULL, *Android_ResumeSem = NULL;
    3.19 -SDL_mutex *Android_ActivityMutex = NULL;
    3.20 +static int Android_ScreenRate      = 0;
    3.21 +SDL_sem *Android_PauseSem          = NULL;
    3.22 +SDL_sem *Android_ResumeSem         = NULL;
    3.23 +SDL_mutex *Android_ActivityMutex   = NULL;
    3.24  
    3.25  static int
    3.26  Android_Available(void)
    3.27 @@ -178,11 +178,12 @@
    3.28      videodata->isPaused  = SDL_FALSE;
    3.29      videodata->isPausing = SDL_FALSE;
    3.30  
    3.31 -    mode.format = Android_ScreenFormat;
    3.32 -    mode.w = Android_DeviceWidth;
    3.33 -    mode.h = Android_DeviceHeight;
    3.34 -    mode.refresh_rate = Android_ScreenRate;
    3.35 -    mode.driverdata = NULL;
    3.36 +    mode.format          = Android_ScreenFormat;
    3.37 +    mode.w               = Android_DeviceWidth;
    3.38 +    mode.h               = Android_DeviceHeight;
    3.39 +    mode.refresh_rate    = Android_ScreenRate;
    3.40 +    mode.driverdata      = NULL;
    3.41 +
    3.42      if (SDL_AddBasicVideoDisplay(&mode) < 0) {
    3.43          return -1;
    3.44      }
    3.45 @@ -213,45 +214,45 @@
    3.46  }
    3.47  
    3.48  void
    3.49 -Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
    3.50 +Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
    3.51  {
    3.52 -    SDL_VideoDevice *device;
    3.53 -    SDL_VideoDisplay *display;
    3.54 -    Android_SurfaceWidth = surfaceWidth;
    3.55 +    Android_SurfaceWidth  = surfaceWidth;
    3.56      Android_SurfaceHeight = surfaceHeight;
    3.57 -    Android_DeviceWidth = deviceWidth;
    3.58 -    Android_DeviceHeight = deviceHeight;
    3.59 -    Android_ScreenFormat = format;
    3.60 -    Android_ScreenRate = (int)rate;
    3.61 +    Android_DeviceWidth   = deviceWidth;
    3.62 +    Android_DeviceHeight  = deviceHeight;
    3.63 +    Android_ScreenFormat  = format;
    3.64 +    Android_ScreenRate    = (int)rate;
    3.65 +}
    3.66  
    3.67 +void Android_SendResize(SDL_Window *window)
    3.68 +{
    3.69      /*
    3.70        Update the resolution of the desktop mode, so that the window
    3.71        can be properly resized. The screen resolution change can for
    3.72        example happen when the Activity enters or exits immersive mode,
    3.73        which can happen after VideoInit().
    3.74      */
    3.75 -    device = SDL_GetVideoDevice();
    3.76 +    SDL_VideoDevice *device = SDL_GetVideoDevice();
    3.77      if (device && device->num_displays > 0)
    3.78      {
    3.79 -        display = &device->displays[0];
    3.80 -        display->desktop_mode.format = Android_ScreenFormat;
    3.81 -        display->desktop_mode.w = Android_DeviceWidth;
    3.82 -        display->desktop_mode.h = Android_DeviceHeight;
    3.83 -        display->desktop_mode.refresh_rate  = Android_ScreenRate;
    3.84 +        SDL_VideoDisplay *display          = &device->displays[0];
    3.85 +        display->desktop_mode.format       = Android_ScreenFormat;
    3.86 +        display->desktop_mode.w            = Android_DeviceWidth;
    3.87 +        display->desktop_mode.h            = Android_DeviceHeight;
    3.88 +        display->desktop_mode.refresh_rate = Android_ScreenRate;
    3.89      }
    3.90  
    3.91      if (window) {
    3.92          /* Force the current mode to match the resize otherwise the SDL_WINDOWEVENT_RESTORED event
    3.93           * will fall back to the old mode */
    3.94 -        display = SDL_GetDisplayForWindow(window);
    3.95 +        SDL_VideoDisplay *display              = SDL_GetDisplayForWindow(window);
    3.96 +        display->display_modes[0].format       = Android_ScreenFormat;
    3.97 +        display->display_modes[0].w            = Android_DeviceWidth;
    3.98 +        display->display_modes[0].h            = Android_DeviceHeight;
    3.99 +        display->display_modes[0].refresh_rate = Android_ScreenRate;
   3.100 +        display->current_mode                  = display->display_modes[0];
   3.101  
   3.102 -        display->display_modes[0].format = format;
   3.103 -        display->display_modes[0].w = Android_DeviceWidth;
   3.104 -        display->display_modes[0].h = Android_DeviceHeight;
   3.105 -        display->display_modes[0].refresh_rate = (int)rate;
   3.106 -        display->current_mode = display->display_modes[0];
   3.107 -
   3.108 -        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
   3.109 +        SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, Android_SurfaceWidth, Android_SurfaceHeight);
   3.110      }
   3.111  }
   3.112  
     4.1 --- a/src/video/android/SDL_androidvideo.h	Thu Jan 17 09:28:30 2019 +0100
     4.2 +++ b/src/video/android/SDL_androidvideo.h	Thu Jan 17 11:05:05 2019 +0100
     4.3 @@ -28,7 +28,8 @@
     4.4  #include "../SDL_sysvideo.h"
     4.5  
     4.6  /* Called by the JNI layer when the screen changes size or format */
     4.7 -extern void Android_SetScreenResolution(SDL_Window *window, int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);
     4.8 +extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);
     4.9 +extern void Android_SendResize(SDL_Window *window);
    4.10  
    4.11  /* Private display data */
    4.12  
    4.13 @@ -41,8 +42,6 @@
    4.14  
    4.15  extern int Android_SurfaceWidth;
    4.16  extern int Android_SurfaceHeight;
    4.17 -extern int Android_DeviceWidth;
    4.18 -extern int Android_DeviceHeight;
    4.19  extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
    4.20  extern SDL_mutex *Android_ActivityMutex;
    4.21