Skip to content

Commit d217da2

Browse files
authored
Added code to request Android to set the display refresh rate based on Retroarch vertical refresh rate config value
1 parent e15bd1d commit d217da2

5 files changed

Lines changed: 246 additions & 17 deletions

File tree

configuration.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656

5757
#include "list_special.h"
5858

59+
#if defined(ANDROID)
60+
#include "pkg/android/phoenix-common/jni/ra_android_bridge.h"
61+
#endif
62+
5963
#if defined(__WINRT__) || defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
6064
#include "uwp/uwp_func.h"
6165
#endif
@@ -2383,7 +2387,11 @@ static struct config_float_setting *populate_settings_float(
23832387
#endif
23842388

23852389
*size = count;
2386-
2390+
#if defined(__ANDROID__) || defined(ANDROID)
2391+
if (settings && settings->floats.video_refresh_rate > 0.0f) {
2392+
ra_notify_refresh_rate(settings->floats.video_refresh_rate);
2393+
}
2394+
#endif
23872395
return tmp;
23882396
}
23892397

@@ -2995,6 +3003,7 @@ void config_set_defaults(void *data)
29953003
g_defaults.settings_video_refresh_rate != DEFAULT_REFRESH_RATE)
29963004
settings->floats.video_refresh_rate = g_defaults.settings_video_refresh_rate;
29973005

3006+
29983007
if (DEFAULT_AUDIO_DEVICE)
29993008
configuration_set_string(settings,
30003009
settings->arrays.audio_device,
@@ -4543,6 +4552,12 @@ static bool config_load_file(global_t *global,
45434552
if (size_settings)
45444553
free(size_settings);
45454554
first_load = false;
4555+
4556+
#if defined(__ANDROID__) || defined(ANDROID)
4557+
if (settings && settings->floats.video_refresh_rate > 0.0f) {
4558+
ra_notify_refresh_rate(settings->floats.video_refresh_rate);
4559+
}
4560+
#endif
45464561
return true;
45474562
}
45484563

@@ -4746,6 +4761,12 @@ bool config_load_override(void *data)
47464761
else
47474762
runloop_state_get_ptr()->flags &= ~RUNLOOP_FLAG_OVERRIDES_ACTIVE;
47484763

4764+
4765+
#if defined(__ANDROID__) || defined(ANDROID)
4766+
if (settings && settings->floats.video_refresh_rate > 0.0f) {
4767+
ra_notify_refresh_rate(settings->floats.video_refresh_rate);
4768+
}
4769+
#endif
47494770
return true;
47504771
}
47514772

@@ -4788,6 +4809,11 @@ bool config_load_override_file(const char *config_path)
47884809
else
47894810
runloop_state_get_ptr()->flags &= ~RUNLOOP_FLAG_OVERRIDES_ACTIVE;
47904811

4812+
#if defined(__ANDROID__) || defined(ANDROID)
4813+
if (settings && settings->floats.video_refresh_rate > 0.0f) {
4814+
ra_notify_refresh_rate(settings->floats.video_refresh_rate);
4815+
}
4816+
#endif
47914817
return true;
47924818
}
47934819

pkg/android/phoenix-common/jni/Android.mk

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ LOCAL_PATH := $(call my-dir)
33
include $(CLEAR_VARS)
44

55
RARCH_DIR := ../../../..
6+
RA_ROOT := $(LOCAL_PATH)/$(RARCH_DIR)
67

78
HAVE_NEON := 1
89
HAVE_LOGGER := 0
@@ -57,6 +58,14 @@ LOCAL_MODULE := retroarch-activity
5758
LOCAL_SRC_FILES += $(RARCH_DIR)/griffin/griffin.c \
5859
$(RARCH_DIR)/griffin/griffin_cpp.cpp
5960

61+
LOCAL_SRC_FILES += \
62+
ra_android_bridge.c
63+
64+
LOCAL_C_INCLUDES += \
65+
$(RA_ROOT) \
66+
$(RA_ROOT)/libretro-common/include \
67+
$(RA_ROOT)/gfx/include
68+
6069
ifeq ($(HAVE_LOGGER), 1)
6170
DEFINES += -DHAVE_LOGGER
6271
endif
@@ -173,13 +182,14 @@ LOCAL_CPPFLAGS := -fexceptions -fpermissive -std=gnu++11 -fno-rtti -Wno-reorder
173182
LOCAL_CFLAGS := $(subst -O3,-O2,$(LOCAL_CFLAGS))
174183

175184
LOCAL_LDLIBS := -landroid -lEGL $(GLES_LIB) $(LOGGER_LDLIBS) -ldl
176-
LOCAL_C_INCLUDES := \
177-
$(LOCAL_PATH)/$(RARCH_DIR)/libretro-common/include \
178-
$(LOCAL_PATH)/$(RARCH_DIR)/deps \
179-
$(LOCAL_PATH)/$(RARCH_DIR)/deps/stb \
180-
$(LOCAL_PATH)/$(RARCH_DIR)/deps/7zip
185+
LOCAL_C_INCLUDES += \
186+
$(RA_ROOT)/libretro-common/include \
187+
$(RA_ROOT)/deps \
188+
$(RA_ROOT)/deps/stb \
189+
$(RA_ROOT)/deps/7zip
190+
181191

182-
INCLUDE_DIRS := \
192+
INCLUDE_DIRS := \
183193
-I$(LOCAL_PATH)/$(DEPS_DIR)/stb/ \
184194
-I$(LOCAL_PATH)/$(DEPS_DIR)/7zip/ \
185195
-I$(LOCAL_PATH)/$(DEPS_DIR)/libFLAC/include
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <jni.h>
2+
#include <libretro.h>
3+
#include "runloop.h"
4+
#include "verbosity.h"
5+
#include "configuration.h"
6+
7+
// ---------- Cached JNI handles ----------
8+
static JavaVM *g_vm = NULL;
9+
static jclass g_cls_RetroActivityFuture = NULL; // GlobalRef
10+
static jmethodID g_mid_nativePushContentFps = NULL; // (F)V
11+
12+
// Small helper: get/attach JNIEnv for the current thread
13+
static JNIEnv* ra_get_env(bool *out_attached)
14+
{
15+
if (out_attached) *out_attached = false;
16+
if (!g_vm) return NULL;
17+
18+
JNIEnv *env = NULL;
19+
jint r = (*g_vm)->GetEnv(g_vm, (void**)&env, JNI_VERSION_1_6);
20+
if (r == JNI_OK && env) return env;
21+
22+
// Not attached -> attach
23+
if ((*g_vm)->AttachCurrentThread(g_vm, &env, NULL) != 0)
24+
return NULL;
25+
if (out_attached) *out_attached = true;
26+
return env;
27+
}
28+
29+
// ---------- JNI load/unload ----------
30+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
31+
{
32+
g_vm = vm;
33+
JNIEnv *env = NULL;
34+
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK || !env)
35+
return JNI_ERR;
36+
37+
jclass local = (*env)->FindClass(env, "com/retroarch/browser/retroactivity/RetroActivityFuture");
38+
if (!local) return JNI_ERR;
39+
40+
g_cls_RetroActivityFuture = (jclass)(*env)->NewGlobalRef(env, local);
41+
(*env)->DeleteLocalRef(env, local);
42+
if (!g_cls_RetroActivityFuture) return JNI_ERR;
43+
44+
// Cache static push method: public static void nativePushContentFps(float)
45+
g_mid_nativePushContentFps = (*env)->GetStaticMethodID(env, g_cls_RetroActivityFuture,
46+
"nativePushContentFps", "(F)V");
47+
// It's fine if Java side doesn't have it yet; we just won't call it
48+
return JNI_VERSION_1_6;
49+
}
50+
51+
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
52+
{
53+
JNIEnv *env = NULL;
54+
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) == JNI_OK && env) {
55+
if (g_cls_RetroActivityFuture) {
56+
(*env)->DeleteGlobalRef(env, g_cls_RetroActivityFuture);
57+
g_cls_RetroActivityFuture = NULL;
58+
}
59+
}
60+
g_vm = NULL;
61+
g_mid_nativePushContentFps = NULL;
62+
}
63+
64+
// ---------- Public C API: push fresh FPS to Java (optional) ----------
65+
void ra_notify_refresh_rate(float fps)
66+
{
67+
if (fps <= 0.f) return;
68+
if (!g_vm || !g_cls_RetroActivityFuture || !g_mid_nativePushContentFps) return;
69+
70+
bool attached = false;
71+
JNIEnv *env = ra_get_env(&attached);
72+
if (!env) return;
73+
74+
(*env)->CallStaticVoidMethod(env, g_cls_RetroActivityFuture,
75+
g_mid_nativePushContentFps, (jfloat)fps);
76+
77+
if (attached)
78+
(*g_vm)->DetachCurrentThread(g_vm);
79+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef RA_ANDROID_BRIDGE_H
2+
#define RA_ANDROID_BRIDGE_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
void ra_notify_refresh_rate(float fps);
9+
10+
#ifdef __cplusplus
11+
}
12+
#endif
13+
#endif

0 commit comments

Comments
 (0)