Track android device panel width & height as well as window surface & height.
authorSam Lantinga <slouken@libsdl.org>
Thu, 07 Jun 2018 17:07:03 -0700
changeset 120126de756c9975a
parent 12011 3eab73f3e17e
child 12013 270e17c85d11
Track android device panel width & height as well as window surface & height.

Expand SDLActivity::SDLSurface::surfaceChanged() callback to grab the panel width and height at the same time and pass that along to the native code. Only works on API 17+. Duplicates surface dimensions whenever it fails.

Add Android_DeviceWidth/Android_DeviceHeight globals to native code.
Disambiguate Android_ScreenWidth/Android_ScreenHeight -> Android_SurfaceWidth/Android_SurfaceHeight
Use device width/height for all display mode settings.
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
src/video/android/SDL_androidwindow.c
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jun 07 10:54:54 2018 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Thu Jun 07 17:07:03 2018 -0700
     1.3 @@ -556,7 +556,7 @@
     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 x, int y, int format, float rate);
     1.8 +    public static native void onNativeResize(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, int format, float rate);
     1.9      public static native void onNativeKeyDown(int keycode);
    1.10      public static native void onNativeKeyUp(int keycode);
    1.11      public static native void onNativeKeyboardFocusLost();
    1.12 @@ -1378,8 +1378,23 @@
    1.13  
    1.14          mWidth = width;
    1.15          mHeight = height;
    1.16 -        SDLActivity.onNativeResize(width, height, sdlFormat, mDisplay.getRefreshRate());
    1.17 -        Log.v("SDL", "Window size: " + width + "x" + height);
    1.18 +		int nDeviceWidth = width;
    1.19 +		int nDeviceHeight = height;
    1.20 +		try
    1.21 +		{
    1.22 +			if ( android.os.Build.VERSION.SDK_INT >= 17 )
    1.23 +			{
    1.24 +				android.util.DisplayMetrics realMetrics = new android.util.DisplayMetrics();
    1.25 +				mDisplay.getRealMetrics( realMetrics );
    1.26 +				nDeviceWidth = realMetrics.widthPixels;
    1.27 +				nDeviceHeight = realMetrics.heightPixels;
    1.28 +			}
    1.29 +		}
    1.30 +		catch ( java.lang.Throwable throwable ) {}
    1.31 +
    1.32 +		Log.v("SDL", "Window size: " + width + "x" + height);
    1.33 +		Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight);
    1.34 +        SDLActivity.onNativeResize(width, height, nDeviceWidth, nDeviceHeight, sdlFormat, mDisplay.getRefreshRate());
    1.35  
    1.36  
    1.37          boolean skip = false;
     2.1 --- a/src/core/android/SDL_android.c	Thu Jun 07 10:54:54 2018 -0700
     2.2 +++ b/src/core/android/SDL_android.c	Thu Jun 07 17:07:03 2018 -0700
     2.3 @@ -76,7 +76,8 @@
     2.4  
     2.5  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
     2.6          JNIEnv* env, jclass jcls,
     2.7 -        jint width, jint height, jint format, jfloat rate);
     2.8 +        jint surfaceWidth, jint surfaceHeight,
     2.9 +		jint deviceWidth, jint deviceHeight, jint format, jfloat rate);
    2.10  
    2.11  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(
    2.12          JNIEnv* env, jclass jcls);
    2.13 @@ -518,9 +519,10 @@
    2.14  /* Resize */
    2.15  JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
    2.16                                      JNIEnv* env, jclass jcls,
    2.17 -                                    jint width, jint height, jint format, jfloat rate)
    2.18 +                                    jint surfaceWidth, jint surfaceHeight,
    2.19 +									jint deviceWidth, jint deviceHeight, jint format, jfloat rate)
    2.20  {
    2.21 -    Android_SetScreenResolution(width, height, format, rate);
    2.22 +    Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
    2.23  }
    2.24  
    2.25  /* Paddown */
     3.1 --- a/src/video/android/SDL_androidvideo.c	Thu Jun 07 10:54:54 2018 -0700
     3.2 +++ b/src/video/android/SDL_androidvideo.c	Thu Jun 07 17:07:03 2018 -0700
     3.3 @@ -60,8 +60,10 @@
     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_ScreenWidth = 0;
     3.8 -int Android_ScreenHeight = 0;
     3.9 +int Android_SurfaceWidth = 0;
    3.10 +int Android_SurfaceHeight = 0;
    3.11 +int Android_DeviceWidth = 0;
    3.12 +int Android_DeviceHeight = 0;
    3.13  Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
    3.14  static int Android_ScreenRate = 0;
    3.15  
    3.16 @@ -176,8 +178,8 @@
    3.17      SDL_DisplayMode mode;
    3.18  
    3.19      mode.format = Android_ScreenFormat;
    3.20 -    mode.w = Android_ScreenWidth;
    3.21 -    mode.h = Android_ScreenHeight;
    3.22 +    mode.w = Android_DeviceWidth;
    3.23 +    mode.h = Android_DeviceHeight;
    3.24      mode.refresh_rate = Android_ScreenRate;
    3.25      mode.driverdata = NULL;
    3.26      if (SDL_AddBasicVideoDisplay(&mode) < 0) {
    3.27 @@ -209,12 +211,14 @@
    3.28  }
    3.29  
    3.30  void
    3.31 -Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
    3.32 +Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
    3.33  {
    3.34  	SDL_VideoDevice* device;
    3.35  	SDL_VideoDisplay *display;
    3.36 -    Android_ScreenWidth = width;
    3.37 -    Android_ScreenHeight = height;
    3.38 +    Android_SurfaceWidth = surfaceWidth;
    3.39 +    Android_SurfaceHeight = surfaceHeight;
    3.40 +	Android_DeviceWidth = deviceWidth;
    3.41 +	Android_DeviceHeight = deviceHeight;
    3.42      Android_ScreenFormat = format;
    3.43      Android_ScreenRate = rate;
    3.44  
    3.45 @@ -229,8 +233,8 @@
    3.46      {
    3.47          display = &device->displays[0];
    3.48          display->desktop_mode.format = Android_ScreenFormat;
    3.49 -        display->desktop_mode.w = Android_ScreenWidth;
    3.50 -        display->desktop_mode.h = Android_ScreenHeight;
    3.51 +        display->desktop_mode.w = Android_DeviceWidth;
    3.52 +        display->desktop_mode.h = Android_DeviceHeight;
    3.53          display->desktop_mode.refresh_rate  = Android_ScreenRate;
    3.54      }
    3.55  
    3.56 @@ -240,12 +244,12 @@
    3.57          display = SDL_GetDisplayForWindow(Android_Window);
    3.58  
    3.59          display->display_modes[0].format = format;
    3.60 -        display->display_modes[0].w = width;
    3.61 -        display->display_modes[0].h = height;
    3.62 +        display->display_modes[0].w = Android_DeviceWidth;
    3.63 +        display->display_modes[0].h = Android_DeviceHeight;
    3.64          display->display_modes[0].refresh_rate = rate;
    3.65          display->current_mode = display->display_modes[0];
    3.66  
    3.67 -        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
    3.68 +        SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
    3.69      }
    3.70  }
    3.71  
     4.1 --- a/src/video/android/SDL_androidvideo.h	Thu Jun 07 10:54:54 2018 -0700
     4.2 +++ b/src/video/android/SDL_androidvideo.h	Thu Jun 07 17:07:03 2018 -0700
     4.3 @@ -28,7 +28,7 @@
     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(int width, int height, Uint32 format, float rate);
     4.8 +extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);
     4.9  
    4.10  /* Private display data */
    4.11  
    4.12 @@ -37,8 +37,10 @@
    4.13      SDL_Rect        textRect;
    4.14  } SDL_VideoData;
    4.15  
    4.16 -extern int Android_ScreenWidth;
    4.17 -extern int Android_ScreenHeight;
    4.18 +extern int Android_SurfaceWidth;
    4.19 +extern int Android_SurfaceHeight;
    4.20 +extern int Android_DeviceWidth;
    4.21 +extern int Android_DeviceHeight;
    4.22  extern Uint32 Android_ScreenFormat;
    4.23  extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
    4.24  extern SDL_Window *Android_Window;
     5.1 --- a/src/video/android/SDL_androidwindow.c	Thu Jun 07 10:54:54 2018 -0700
     5.2 +++ b/src/video/android/SDL_androidwindow.c	Thu Jun 07 17:07:03 2018 -0700
     5.3 @@ -49,8 +49,8 @@
     5.4      /* Adjust the window data to match the screen */
     5.5      window->x = 0;
     5.6      window->y = 0;
     5.7 -    window->w = Android_ScreenWidth;
     5.8 -    window->h = Android_ScreenHeight;
     5.9 +    window->w = Android_SurfaceWidth;
    5.10 +    window->h = Android_SurfaceHeight;
    5.11  
    5.12      window->flags &= ~SDL_WINDOW_RESIZABLE;     /* window is NEVER resizeable */
    5.13      window->flags &= ~SDL_WINDOW_HIDDEN;