From 73dc327c84a0e0c5506364d0e38f242bf2171f01 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Wed, 25 Jan 2023 14:55:50 +0100
Subject: [PATCH] Android get the display Density
---
.../app/src/main/java/org/libsdl/app/SDLActivity.java | 4 ++--
.../app/src/main/java/org/libsdl/app/SDLSurface.java | 4 +++-
src/core/android/SDL_android.c | 8 ++++----
src/video/android/SDL_androidvideo.c | 4 +++-
src/video/android/SDL_androidvideo.h | 2 +-
5 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
index edb42fb55461..65849e27e4ad 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
@@ -311,7 +311,7 @@ public static void initialize() {
mNextNativeState = NativeState.INIT;
mCurrentNativeState = NativeState.INIT;
}
-
+
protected SDLSurface createSDLSurface(Context context) {
return new SDLSurface(context);
}
@@ -909,7 +909,7 @@ boolean sendCommand(int command, Object data) {
public static native void nativeResume();
public static native void nativeFocusChanged(boolean hasFocus);
public static native void onNativeDropFile(String filename);
- public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate);
+ public static native void nativeSetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate);
public static native void onNativeResize();
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
index dcd26d495c87..268ecaecab95 100644
--- a/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
+++ b/android-project/app/src/main/java/org/libsdl/app/SDLSurface.java
@@ -114,6 +114,7 @@ public void surfaceChanged(SurfaceHolder holder,
mHeight = height;
int nDeviceWidth = width;
int nDeviceHeight = height;
+ float density = 1.0f;
try
{
if (Build.VERSION.SDK_INT >= 17) {
@@ -121,6 +122,7 @@ public void surfaceChanged(SurfaceHolder holder,
mDisplay.getRealMetrics( realMetrics );
nDeviceWidth = realMetrics.widthPixels;
nDeviceHeight = realMetrics.heightPixels;
+ density = realMetrics.density;
}
} catch(Exception ignored) {
}
@@ -132,7 +134,7 @@ public void surfaceChanged(SurfaceHolder holder,
Log.v("SDL", "Window size: " + width + "x" + height);
Log.v("SDL", "Device size: " + nDeviceWidth + "x" + nDeviceHeight);
- SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, mDisplay.getRefreshRate());
+ SDLActivity.nativeSetScreenResolution(width, height, nDeviceWidth, nDeviceHeight, density, mDisplay.getRefreshRate());
SDLActivity.onNativeResize();
// Prevent a screen distortion glitch,
diff --git a/src/core/android/SDL_android.c b/src/core/android/SDL_android.c
index 26fb21da76fa..ba45fbc1f16e 100644
--- a/src/core/android/SDL_android.c
+++ b/src/core/android/SDL_android.c
@@ -74,7 +74,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeDropFile)(
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
JNIEnv *env, jclass jcls,
jint surfaceWidth, jint surfaceHeight,
- jint deviceWidth, jint deviceHeight, jfloat rate);
+ jint deviceWidth, jint deviceHeight, jfloat density, jfloat rate);
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeResize)(
JNIEnv *env, jclass cls);
@@ -168,7 +168,7 @@ static JNINativeMethod SDLActivity_tab[] = {
{ "nativeSetupJNI", "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) },
{ "nativeRunMain", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)I", SDL_JAVA_INTERFACE(nativeRunMain) },
{ "onNativeDropFile", "(Ljava/lang/String;)V", SDL_JAVA_INTERFACE(onNativeDropFile) },
- { "nativeSetScreenResolution", "(IIIIF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) },
+ { "nativeSetScreenResolution", "(IIIIFF)V", SDL_JAVA_INTERFACE(nativeSetScreenResolution) },
{ "onNativeResize", "()V", SDL_JAVA_INTERFACE(onNativeResize) },
{ "onNativeSurfaceCreated", "()V", SDL_JAVA_INTERFACE(onNativeSurfaceCreated) },
{ "onNativeSurfaceChanged", "()V", SDL_JAVA_INTERFACE(onNativeSurfaceChanged) },
@@ -870,11 +870,11 @@ void Android_ActivityMutex_Lock_Running()
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
JNIEnv *env, jclass jcls,
jint surfaceWidth, jint surfaceHeight,
- jint deviceWidth, jint deviceHeight, jfloat rate)
+ jint deviceWidth, jint deviceHeight, jfloat density, jfloat rate)
{
SDL_LockMutex(Android_ActivityMutex);
- Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, rate);
+ Android_SetScreenResolution(surfaceWidth, surfaceHeight, deviceWidth, deviceHeight, density, rate);
SDL_UnlockMutex(Android_ActivityMutex);
}
diff --git a/src/video/android/SDL_androidvideo.c b/src/video/android/SDL_androidvideo.c
index 925f3dd10c64..44d628255fa4 100644
--- a/src/video/android/SDL_androidvideo.c
+++ b/src/video/android/SDL_androidvideo.c
@@ -62,6 +62,7 @@ static int Android_DeviceWidth = 0;
static int Android_DeviceHeight = 0;
static Uint32 Android_ScreenFormat = SDL_PIXELFORMAT_RGB565; /* Default SurfaceView format, in case this is queried before being filled */
static float Android_ScreenRate = 0.0f;
+static float Android_ScreenDensity = 1.0f;
SDL_sem *Android_PauseSem = NULL;
SDL_sem *Android_ResumeSem = NULL;
SDL_mutex *Android_ActivityMutex = NULL;
@@ -213,13 +214,14 @@ int Android_GetDisplayPhysicalDPI(_THIS, SDL_VideoDisplay *display, float *ddpi,
return Android_JNI_GetDisplayPhysicalDPI(ddpi, hdpi, vdpi);
}
-void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float rate)
+void Android_SetScreenResolution(int surfaceWidth, int surfaceHeight, int deviceWidth, int deviceHeight, float density, float rate)
{
Android_SurfaceWidth = surfaceWidth;
Android_SurfaceHeight = surfaceHeight;
Android_DeviceWidth = deviceWidth;
Android_DeviceHeight = deviceHeight;
Android_ScreenRate = rate;
+ Android_ScreenDensity = density;
}
static Uint32 format_to_pixelFormat(int format)
diff --git a/src/video/android/SDL_androidvideo.h b/src/video/android/SDL_androidvideo.h
index db49793cf177..81d5c4a27b5e 100644
--- a/src/video/android/SDL_androidvideo.h
+++ b/src/video/android/SDL_androidvideo.h
@@ -26,7 +26,7 @@
#include "../SDL_sysvideo.h"
/* 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 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_SendResize(SDL_Window *window);