Skip to content

Commit

Permalink
Track android device panel width & height as well as window surface &…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
slouken committed Jun 8, 2018
1 parent db86e7a commit fe196db
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
21 changes: 18 additions & 3 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Expand Up @@ -556,7 +556,7 @@ boolean sendCommand(int command, Object data) {
public static native void nativePause();
public static native void nativeResume();
public static native void onNativeDropFile(String filename);
public static native void onNativeResize(int x, int y, int format, float rate);
public static native void onNativeResize(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, int format, float rate);
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeKeyboardFocusLost();
Expand Down Expand Up @@ -1378,8 +1378,23 @@ public void surfaceChanged(SurfaceHolder holder,

mWidth = width;
mHeight = height;
SDLActivity.onNativeResize(width, height, sdlFormat, mDisplay.getRefreshRate());
Log.v("SDL", "Window size: " + width + "x" + height);
int nDeviceWidth = width;
int nDeviceHeight = height;
try
{
if ( android.os.Build.VERSION.SDK_INT >= 17 )
{
android.util.DisplayMetrics realMetrics = new android.util.DisplayMetrics();
mDisplay.getRealMetrics( realMetrics );
nDeviceWidth = realMetrics.widthPixels;
nDeviceHeight = realMetrics.heightPixels;
}
}
catch ( java.lang.Throwable throwable ) {}

Log.v("SDL", "Window size: " + width + "x" + height);
Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight);
SDLActivity.onNativeResize(width, height, nDeviceWidth, nDeviceHeight, sdlFormat, mDisplay.getRefreshRate());


boolean skip = false;
Expand Down
8 changes: 5 additions & 3 deletions src/core/android/SDL_android.c
Expand Up @@ -76,7 +76,8 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(

JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
JNIEnv* env, jclass jcls,
jint width, jint height, jint format, jfloat rate);
jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jint format, jfloat rate);

JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeSurfaceChanged)(
JNIEnv* env, jclass jcls);
Expand Down Expand Up @@ -518,9 +519,10 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(
/* Resize */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
JNIEnv* env, jclass jcls,
jint width, jint height, jint format, jfloat rate)
jint surfaceWidth, jint surfaceHeight,
jint deviceWidth, jint deviceHeight, jint format, jfloat rate)
{
Android_SetScreenResolution(width, height, format, rate);
Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, format, rate);
}

/* Paddown */
Expand Down
28 changes: 16 additions & 12 deletions src/video/android/SDL_androidvideo.c
Expand Up @@ -60,8 +60,10 @@ int Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float


/* These are filled in with real values in Android_SetScreenResolution on init (before SDL_main()) */
int Android_ScreenWidth = 0;
int Android_ScreenHeight = 0;
int Android_SurfaceWidth = 0;
int Android_SurfaceHeight = 0;
int Android_DeviceWidth = 0;
int Android_DeviceHeight = 0;
Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_UNKNOWN;
static int Android_ScreenRate = 0;

Expand Down Expand Up @@ -176,8 +178,8 @@ Android_VideoInit(_THIS)
SDL_DisplayMode mode;

mode.format = Android_ScreenFormat;
mode.w = Android_ScreenWidth;
mode.h = Android_ScreenHeight;
mode.w = Android_DeviceWidth;
mode.h = Android_DeviceHeight;
mode.refresh_rate = Android_ScreenRate;
mode.driverdata = NULL;
if (SDL_AddBasicVideoDisplay(&mode) < 0) {
Expand Down Expand Up @@ -209,12 +211,14 @@ Android_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * h
}

void
Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate)
{
SDL_VideoDevice* device;
SDL_VideoDisplay *display;
Android_ScreenWidth = width;
Android_ScreenHeight = height;
Android_SurfaceWidth = surfaceWidth;
Android_SurfaceHeight = surfaceHeight;
Android_DeviceWidth = deviceWidth;
Android_DeviceHeight = deviceHeight;
Android_ScreenFormat = format;
Android_ScreenRate = rate;

Expand All @@ -229,8 +233,8 @@ Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
{
display = &device->displays[0];
display->desktop_mode.format = Android_ScreenFormat;
display->desktop_mode.w = Android_ScreenWidth;
display->desktop_mode.h = Android_ScreenHeight;
display->desktop_mode.w = Android_DeviceWidth;
display->desktop_mode.h = Android_DeviceHeight;
display->desktop_mode.refresh_rate = Android_ScreenRate;
}

Expand All @@ -240,12 +244,12 @@ Android_SetScreenResolution(int width, int height, Uint32 format, float rate)
display = SDL_GetDisplayForWindow(Android_Window);

display->display_modes[0].format = format;
display->display_modes[0].w = width;
display->display_modes[0].h = height;
display->display_modes[0].w = Android_DeviceWidth;
display->display_modes[0].h = Android_DeviceHeight;
display->display_modes[0].refresh_rate = rate;
display->current_mode = display->display_modes[0];

SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, width, height);
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESIZED, surfaceWidth, surfaceHeight);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/video/android/SDL_androidvideo.h
Expand Up @@ -28,7 +28,7 @@
#include "../SDL_sysvideo.h"

/* Called by the JNI layer when the screen changes size or format */
extern void Android_SetScreenResolution(int width, int height, Uint32 format, float rate);
extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, Uint32 format, float rate);

/* Private display data */

Expand All @@ -37,8 +37,10 @@ typedef struct SDL_VideoData
SDL_Rect textRect;
} SDL_VideoData;

extern int Android_ScreenWidth;
extern int Android_ScreenHeight;
extern int Android_SurfaceWidth;
extern int Android_SurfaceHeight;
extern int Android_DeviceWidth;
extern int Android_DeviceHeight;
extern Uint32 Android_ScreenFormat;
extern SDL_sem *Android_PauseSem, *Android_ResumeSem;
extern SDL_Window *Android_Window;
Expand Down
4 changes: 2 additions & 2 deletions src/video/android/SDL_androidwindow.c
Expand Up @@ -49,8 +49,8 @@ Android_CreateWindow(_THIS, SDL_Window * window)
/* Adjust the window data to match the screen */
window->x = 0;
window->y = 0;
window->w = Android_ScreenWidth;
window->h = Android_ScreenHeight;
window->w = Android_SurfaceWidth;
window->h = Android_SurfaceHeight;

window->flags &= ~SDL_WINDOW_RESIZABLE; /* window is NEVER resizeable */
window->flags &= ~SDL_WINDOW_HIDDEN;
Expand Down

0 comments on commit fe196db

Please sign in to comment.