Runtime support to call ANativeWindow_setFrameRate for minSDK < 30

This commit is contained in:
Attila Uygun 2023-05-21 02:56:03 +02:00
parent 5cad50bf55
commit 971540dce2
2 changed files with 41 additions and 1 deletions

View File

@ -14,6 +14,7 @@
struct android_app; struct android_app;
struct AInputEvent; struct AInputEvent;
struct ANativeWindow;
#elif defined(__linux__) #elif defined(__linux__)
@ -96,6 +97,21 @@ class Platform {
static int32_t HandleInput(android_app* app, AInputEvent* event); static int32_t HandleInput(android_app* app, AInputEvent* event);
static void HandleCmd(android_app* app, int32_t cmd); static void HandleCmd(android_app* app, int32_t cmd);
using PFN_ANativeWindow_setFrameRate = int32_t (*)(ANativeWindow* window,
float frameRate,
int8_t compatibility);
using PFN_ANativeWindow_setFrameRateWithChangeStrategy =
int32_t (*)(ANativeWindow* window,
float frameRate,
int8_t compatibility,
int8_t changeFrameRateStrategy);
PFN_ANativeWindow_setFrameRate ANativeWindow_setFrameRate = nullptr;
PFN_ANativeWindow_setFrameRateWithChangeStrategy
ANativeWindow_setFrameRateWithChangeStrategy = nullptr;
void SetFrameRate(float frame_rate);
#elif defined(__linux__) #elif defined(__linux__)
Display* display_ = nullptr; Display* display_ = nullptr;

View File

@ -1,6 +1,7 @@
#include "engine/platform/platform.h" #include "engine/platform/platform.h"
#include <android_native_app_glue.h> #include <android_native_app_glue.h>
#include <dlfcn.h>
#include <jni.h> #include <jni.h>
#include <unistd.h> #include <unistd.h>
@ -24,7 +25,6 @@ Java_com_kaliber_base_KaliberActivity_onShowAdResult(JNIEnv* env,
jboolean succeeded) { jboolean succeeded) {
g_showing_interstitial_ad = !!succeeded; g_showing_interstitial_ad = !!succeeded;
} }
} }
std::string GetApkPath(ANativeActivity* activity) { std::string GetApkPath(ANativeActivity* activity) {
@ -294,6 +294,7 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) {
case APP_CMD_INIT_WINDOW: case APP_CMD_INIT_WINDOW:
DLOG << "APP_CMD_INIT_WINDOW"; DLOG << "APP_CMD_INIT_WINDOW";
if (app->window != NULL) { if (app->window != NULL) {
platform->SetFrameRate(60);
if (!platform->renderer_->Initialize(app->window)) { if (!platform->renderer_->Initialize(app->window)) {
LOG << "Failed to initialize the renderer."; LOG << "Failed to initialize the renderer.";
throw internal_error; throw internal_error;
@ -371,6 +372,18 @@ void Platform::Initialize(android_app* app) {
app->onAppCmd = Platform::HandleCmd; app->onAppCmd = Platform::HandleCmd;
app->onInputEvent = Platform::HandleInput; app->onInputEvent = Platform::HandleInput;
// Get pointers for functions that are supported from API > minSdk if
// available.
void* mLibAndroid = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
if (mLibAndroid) {
ANativeWindow_setFrameRate =
reinterpret_cast<PFN_ANativeWindow_setFrameRate>(
dlsym(mLibAndroid, "ANativeWindow_setFrameRate"));
ANativeWindow_setFrameRateWithChangeStrategy =
reinterpret_cast<PFN_ANativeWindow_setFrameRateWithChangeStrategy>(
dlsym(mLibAndroid, "ANativeWindow_setFrameRateWithChangeStrategy"));
}
Update(); Update();
} }
@ -417,6 +430,17 @@ void Platform::SetKeepScreenOn(bool keep_screen_on) {
::SetKeepScreenOn(app_->activity, keep_screen_on); ::SetKeepScreenOn(app_->activity, keep_screen_on);
} }
void Platform::SetFrameRate(float frame_rate) {
if (ANativeWindow_setFrameRateWithChangeStrategy) {
ANativeWindow_setFrameRateWithChangeStrategy(
app_->window, frame_rate,
ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, 1);
} else if (ANativeWindow_setFrameRate) {
ANativeWindow_setFrameRate(app_->window, frame_rate,
ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT);
}
}
} // namespace eng } // namespace eng
void android_main(android_app* app) { void android_main(android_app* app) {