Wait for a display resize event before sending orientation changes

Fixes https://github.com/libsdl-org/SDL/issues/9585
This commit is contained in:
Sam Lantinga 2024-12-30 15:49:10 -08:00
parent 60deaf5f0f
commit 8704ab8422
3 changed files with 27 additions and 4 deletions

View file

@ -1063,10 +1063,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeRotationChanged)(
break; break;
} }
if (Android_Window) { Android_SetOrientation(displayCurrentOrientation);
SDL_VideoDisplay *display = SDL_GetVideoDisplay(SDL_GetPrimaryDisplay());
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, displayCurrentOrientation, 0);
}
SDL_UnlockMutex(Android_ActivityMutex); SDL_UnlockMutex(Android_ActivityMutex);
} }

View file

@ -63,6 +63,7 @@ static int Android_DeviceHeight = 0;
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; // Default SurfaceView format, in case this is queried before being filled static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; // Default SurfaceView format, in case this is queried before being filled
float Android_ScreenDensity = 1.0f; float Android_ScreenDensity = 1.0f;
static float Android_ScreenRate = 0.0f; static float Android_ScreenRate = 0.0f;
static SDL_DisplayOrientation Android_ScreenOrientation = SDL_ORIENTATION_UNKNOWN;
int Android_SafeInsetLeft = 0; int Android_SafeInsetLeft = 0;
int Android_SafeInsetRight = 0; int Android_SafeInsetRight = 0;
int Android_SafeInsetTop = 0; int Android_SafeInsetTop = 0;
@ -249,6 +250,29 @@ void Android_SetFormat(int format_wanted, int format_got)
SDL_GetPixelFormatName(pf_got), format_got); SDL_GetPixelFormatName(pf_got), format_got);
} }
static void Android_SendOrientationUpdate(void)
{
/* If we've received a compatible resize event, update the
* orientation immediately, otherwise wait for the display
* resize event.
*/
SDL_VideoDevice *device = SDL_GetVideoDevice();
if (device && device->num_displays > 0) {
SDL_VideoDisplay *display = device->displays[0];
bool mode_landscape = (display->desktop_mode.w > display->desktop_mode.h);
bool sensor_landscape = (Android_ScreenOrientation == SDL_ORIENTATION_LANDSCAPE || Android_ScreenOrientation == SDL_ORIENTATION_LANDSCAPE_FLIPPED);
if (sensor_landscape == mode_landscape) {
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, Android_ScreenOrientation, 0);
}
}
}
void Android_SetOrientation(SDL_DisplayOrientation orientation)
{
Android_ScreenOrientation = orientation;
Android_SendOrientationUpdate();
}
void Android_SendResize(SDL_Window *window) void Android_SendResize(SDL_Window *window)
{ {
/* /*
@ -268,6 +292,7 @@ void Android_SendResize(SDL_Window *window)
desktop_mode.h = Android_DeviceHeight; desktop_mode.h = Android_DeviceHeight;
desktop_mode.refresh_rate = Android_ScreenRate; desktop_mode.refresh_rate = Android_ScreenRate;
SDL_SetDesktopDisplayMode(display, &desktop_mode); SDL_SetDesktopDisplayMode(display, &desktop_mode);
Android_SendOrientationUpdate();
} }
if (window) { if (window) {

View file

@ -28,6 +28,7 @@
// Called by the JNI layer when the screen changes size or format // Called by the JNI layer when the screen changes size or format
extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate); extern void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate);
extern void Android_SetFormat(int format_wanted, int format_got); extern void Android_SetFormat(int format_wanted, int format_got);
extern void Android_SetOrientation(SDL_DisplayOrientation orientation);
extern void Android_SendResize(SDL_Window *window); extern void Android_SendResize(SDL_Window *window);
extern void Android_SetWindowSafeAreaInsets(int left, int right, int top, int bottom); extern void Android_SetWindowSafeAreaInsets(int left, int right, int top, int bottom);
extern void Android_SetDarkMode(bool enabled); extern void Android_SetDarkMode(bool enabled);