From 7e9c6135be2472715a3004a5f6d69f63069b1e1a Mon Sep 17 00:00:00 2001 From: Zhongwang Zhang Date: Thu, 25 May 2023 16:28:59 +0800 Subject: [PATCH] c/main: Trigger the display refresh rate change request via android API ANativeWindow_setFrameRate Part-of: --- src/xrt/compositor/main/comp_compositor.c | 27 ++++++++++++++++++- src/xrt/compositor/main/comp_window_android.c | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/xrt/compositor/main/comp_compositor.c b/src/xrt/compositor/main/comp_compositor.c index 09514fdf6..0004692d6 100644 --- a/src/xrt/compositor/main/comp_compositor.c +++ b/src/xrt/compositor/main/comp_compositor.c @@ -85,6 +85,7 @@ #ifdef XRT_OS_ANDROID #include "android/android_custom_surface.h" #include "android/android_globals.h" +#include #endif #define WINDOW_TITLE "Monado" @@ -327,10 +328,15 @@ compositor_layer_commit(struct xrt_compositor *xc, xrt_graphics_sync_handle_t sy static xrt_result_t compositor_get_display_refresh_rate(struct xrt_compositor *xc, float *out_display_refresh_rate_hz) { +#ifdef XRT_OS_ANDROID + *out_display_refresh_rate_hz = + android_custom_surface_get_display_refresh_rate(android_globals_get_vm(), android_globals_get_context()); +#else struct comp_compositor *c = comp_compositor(xc); //! @todo: Implement the method to change display refresh rate. *out_display_refresh_rate_hz = (float)(1. / time_ns_to_s(c->settings.nominal_frame_interval_ns)); +#endif return XRT_SUCCESS; } @@ -338,7 +344,26 @@ compositor_get_display_refresh_rate(struct xrt_compositor *xc, float *out_displa static xrt_result_t compositor_request_display_refresh_rate(struct xrt_compositor *xc, float display_refresh_rate_hz) { - //! @todo: Implement the method to change display refresh rate. +#ifdef XRT_OS_ANDROID + typedef int32_t (*PF_SETFRAMERATE)(ANativeWindow * window, float frameRate, int8_t compatibility); + + // Note that this will just increment the reference count, rather than actually load it again, + // since we are linked for other symbols too. + void *android_handle = dlopen("libandroid.so", RTLD_NOW); + PF_SETFRAMERATE set_frame_rate = (PF_SETFRAMERATE)dlsym(android_handle, "ANativeWindow_setFrameRate"); + if (!set_frame_rate) { + U_LOG_E("ANativeWindow_setFrameRate not found"); + dlclose(android_handle); + return XRT_SUCCESS; + } + struct ANativeWindow *window = (struct ANativeWindow *)android_globals_get_window(); + if (window == NULL || (set_frame_rate(window, display_refresh_rate_hz, 1) != 0)) { + U_LOG_E("set_frame_rate error"); + } + dlclose(android_handle); +#else + // Currently not implemented on other platforms. +#endif return XRT_SUCCESS; } diff --git a/src/xrt/compositor/main/comp_window_android.c b/src/xrt/compositor/main/comp_window_android.c index 67352150a..834e42d00 100644 --- a/src/xrt/compositor/main/comp_window_android.c +++ b/src/xrt/compositor/main/comp_window_android.c @@ -170,6 +170,7 @@ comp_window_android_init_swapchain(struct comp_target *ct, uint32_t width, uint3 COMP_ERROR(cwa->base.base.c, "could not get ANativeWindow"); return false; } + android_globals_store_window((struct _ANativeWindow *)window); ret = comp_window_android_create_surface(cwa, window, &cwa->base.surface.handle); if (ret != VK_SUCCESS) {