From 1cff8e26f4d9c3da350daa60ca5bcd0b67151c82 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Tue, 20 Jun 2023 08:31:19 +0200 Subject: [PATCH] (Android) Move DPI metric code to dispserv_android.c - and remove duplicate code --- frontend/drivers/platform_unix.c | 27 ---------- frontend/drivers/platform_unix.h | 2 - gfx/display_servers/dispserv_android.c | 68 ++++++++++++++++++++++++++ gfx/drivers_context/android_ctx.c | 44 ++--------------- gfx/drivers_context/android_vk_ctx.c | 44 ++--------------- 5 files changed, 78 insertions(+), 107 deletions(-) diff --git a/frontend/drivers/platform_unix.c b/frontend/drivers/platform_unix.c index c802e5f286..4ab6e45385 100644 --- a/frontend/drivers/platform_unix.c +++ b/frontend/drivers/platform_unix.c @@ -204,33 +204,6 @@ int system_property_get(const char *command, /* forward declaration */ bool android_run_events(void *data); -void android_dpi_get_density(char *s, size_t len) -{ - static bool inited_once = false; - static bool inited2_once = false; - static char string[PROP_VALUE_MAX] = {0}; - static char string2[PROP_VALUE_MAX] = {0}; - if (!inited_once) - { - system_property_get("getprop", "ro.sf.lcd_density", string); - inited_once = true; - } - - if (!string_is_empty(string)) - { - strlcpy(s, string, len); - return; - } - - if (!inited2_once) - { - system_property_get("wm", "density", string2); - inited2_once = true; - } - - strlcpy(s, string2, len); -} - void android_app_write_cmd(struct android_app *android_app, int8_t cmd) { if (android_app) diff --git a/frontend/drivers/platform_unix.h b/frontend/drivers/platform_unix.h index e3991a4e30..95b228ebcd 100644 --- a/frontend/drivers/platform_unix.h +++ b/frontend/drivers/platform_unix.h @@ -361,8 +361,6 @@ extern JNIEnv *jni_thread_getenv(void); void android_app_write_cmd(struct android_app *android_app, int8_t cmd); -void android_dpi_get_density(char *s, size_t len); - extern struct android_app *g_android; #endif diff --git a/gfx/display_servers/dispserv_android.c b/gfx/display_servers/dispserv_android.c index 5f381c9774..ed864fde4a 100644 --- a/gfx/display_servers/dispserv_android.c +++ b/gfx/display_servers/dispserv_android.c @@ -19,6 +19,9 @@ #include "../video_display_server.h" #include "../../frontend/drivers/platform_unix.h" +/* FORWARD DECLARATIONS */ +int system_property_get(const char *cmd, const char *args, char *value); + static void* android_display_server_init(void) { return NULL; } static void android_display_server_destroy(void *data) { } static bool android_display_server_set_window_opacity(void *data, unsigned opacity) { return true; } @@ -38,6 +41,71 @@ static void android_display_server_set_screen_orientation(void *data, g_android->setScreenOrientation, rotation); } +static void android_display_dpi_get_density(char *s, size_t len) +{ + static bool inited_once = false; + static bool inited2_once = false; + static char string[PROP_VALUE_MAX] = {0}; + static char string2[PROP_VALUE_MAX] = {0}; + if (!inited_once) + { + system_property_get("getprop", "ro.sf.lcd_density", string); + inited_once = true; + } + + if (!string_is_empty(string)) + { + strlcpy(s, string, len); + return; + } + + if (!inited2_once) + { + system_property_get("wm", "density", string2); + inited2_once = true; + } + + strlcpy(s, string2, len); +} + +bool android_display_get_metrics(void *data, + enum display_metric_types type, float *value) +{ + static int dpi = -1; + + switch (type) + { + case DISPLAY_METRIC_MM_WIDTH: + case DISPLAY_METRIC_MM_HEIGHT: + return false; + case DISPLAY_METRIC_DPI: + if (dpi == -1) + { + char density[PROP_VALUE_MAX]; + android_display_dpi_get_density(density, sizeof(density)); + if (string_is_empty(density)) + goto dpi_fallback; + if ((dpi = atoi(density)) <= 0) + goto dpi_fallback; + } + *value = (float)dpi; + break; + case DISPLAY_METRIC_NONE: + default: + *value = 0; + return false; + } + + return true; + +dpi_fallback: + /* add a fallback in case the device doesn't report DPI. + * Hopefully fixes issues with the moto G2. */ + dpi = 90; + *value = (float)dpi; + return true; +} + const video_display_server_t dispserv_android = { android_display_server_init, android_display_server_destroy, diff --git a/gfx/drivers_context/android_ctx.c b/gfx/drivers_context/android_ctx.c index 4e71a9c4b4..caf459c33c 100644 --- a/gfx/drivers_context/android_ctx.c +++ b/gfx/drivers_context/android_ctx.c @@ -53,6 +53,10 @@ static enum gfx_ctx_api android_api = GFX_CTX_NONE; static bool g_es3 = false; #endif +/* FORWARD DECLARATION */ +bool android_display_get_metrics(void *data, + enum display_metric_types type, float *value); + static void android_gfx_ctx_destroy(void *data) { android_ctx_data_t *and = (android_ctx_data_t*)data; @@ -246,44 +250,6 @@ static bool android_gfx_ctx_has_focus(void *data) static bool android_gfx_ctx_suppress_screensaver(void *data, bool enable) { return false; } -static bool android_gfx_ctx_get_metrics(void *data, - enum display_metric_types type, float *value) -{ - static int dpi = -1; - - switch (type) - { - case DISPLAY_METRIC_MM_WIDTH: - case DISPLAY_METRIC_MM_HEIGHT: - return false; - case DISPLAY_METRIC_DPI: - if (dpi == -1) - { - char density[PROP_VALUE_MAX]; - android_dpi_get_density(density, sizeof(density)); - if (string_is_empty(density)) - goto dpi_fallback; - if ((dpi = atoi(density)) <= 0) - goto dpi_fallback; - } - *value = (float)dpi; - break; - case DISPLAY_METRIC_NONE: - default: - *value = 0; - return false; - } - - return true; - -dpi_fallback: - /* add a fallback in case the device doesn't report DPI. - * Hopefully fixes issues with the moto G2. */ - dpi = 90; - *value = (float)dpi; - return true; -} - static void android_gfx_ctx_swap_buffers(void *data) { #ifdef HAVE_EGL @@ -333,7 +299,7 @@ const gfx_ctx_driver_t gfx_ctx_android = { NULL, /* get_video_output_size */ NULL, /* get_video_output_prev */ NULL, /* get_video_output_next */ - android_gfx_ctx_get_metrics, + android_display_get_metrics, NULL, NULL, /* update_title */ android_gfx_ctx_check_window, diff --git a/gfx/drivers_context/android_vk_ctx.c b/gfx/drivers_context/android_vk_ctx.c index 97b7302bd5..50767b341d 100644 --- a/gfx/drivers_context/android_vk_ctx.c +++ b/gfx/drivers_context/android_vk_ctx.c @@ -41,6 +41,10 @@ typedef struct int swap_interval; } android_ctx_data_vk_t; +/* FORWARD DECLARATION */ +bool android_display_get_metrics(void *data, + enum display_metric_types type, float *value); + static void android_gfx_ctx_vk_destroy(void *data) { android_ctx_data_vk_t *and = (android_ctx_data_vk_t*)data; @@ -205,44 +209,6 @@ static bool android_gfx_ctx_vk_has_focus(void *data) static bool android_gfx_ctx_vk_suppress_screensaver(void *data, bool enable) { return false; } -static bool android_gfx_ctx_vk_get_metrics(void *data, - enum display_metric_types type, float *value) -{ - static int dpi = -1; - - switch (type) - { - case DISPLAY_METRIC_MM_WIDTH: - case DISPLAY_METRIC_MM_HEIGHT: - return false; - case DISPLAY_METRIC_DPI: - if (dpi == -1) - { - char density[PROP_VALUE_MAX]; - android_dpi_get_density(density, sizeof(density)); - if (string_is_empty(density)) - goto dpi_fallback; - if ((dpi = atoi(density)) <= 0) - goto dpi_fallback; - } - *value = (float)dpi; - break; - case DISPLAY_METRIC_NONE: - default: - *value = 0; - return false; - } - - return true; - -dpi_fallback: - /* Add a fallback in case the device doesn't report DPI. - * Hopefully fixes issues with the moto G2. */ - dpi = 90; - *value = (float)dpi; - return true; -} - static void android_gfx_ctx_vk_swap_buffers(void *data) { android_ctx_data_vk_t *and = (android_ctx_data_vk_t*)data; @@ -307,7 +273,7 @@ const gfx_ctx_driver_t gfx_ctx_vk_android = { NULL, /* get_video_output_size */ NULL, /* get_video_output_prev */ NULL, /* get_video_output_next */ - android_gfx_ctx_vk_get_metrics, + android_display_get_metrics, NULL, NULL, /* update_title */ android_gfx_ctx_vk_check_window,