- MtFrameRate setting (30 / 60 default / display max) as app.Get/SetFrameRateMode, with a "画面帧率" row injected into the system option dialog (mt_framerate); saved in the SDL pref path. - FrameRatePolicy in the live loop: 30 fps after 15 s without input (not while auto hunting), display switched to 60 Hz unless the highest mode, ADPF setPreferPowerEfficiency, vsync pacing when the display already runs at the target. --fps-mode / --idle-fps / --idle-after; --fps-cap / --refresh-rate still pin a test run. - Perf log: battery current/voltage/power, plugged, fps_mode, idle columns; perf_summary groups power by mode. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
207 lines
9.2 KiB
C++
207 lines
9.2 KiB
C++
#pragma once
|
|
// Android frame-rate and CPU-clock plumbing for the live client's render loop.
|
|
//
|
|
// - display_refresh_rate(): the panel's refresh rate now (MainActivity.currentRefreshRate), and
|
|
// render_rate(): the app-vsync rate the system actually gives the app (MainActivity.currentRenderRate),
|
|
// for the perf log's refresh_hz / render_hz columns.
|
|
// - request_frame_rate(): ANativeWindow_setFrameRate (API 30), the surface's preferred rate. The display
|
|
// mode itself is chosen by MainActivity (--refresh-rate -> preferredDisplayModeId); the vendor
|
|
// (ColorOS etc.) can still cap an app it does not know.
|
|
// - PerformanceHint: ADPF APerformanceHint (API 33). The loop reports each frame's CPU work against the
|
|
// frame budget, so the governor raises the clocks before a frame misses instead of idling the CPU at
|
|
// its lowest step between short bursts (the 556-748 MHz seen in perf-20260929-153205.csv).
|
|
// prefer_power_efficiency(): APerformanceHint_setPreferPowerEfficiency (API 35), set by the frame-rate
|
|
// policy (main.cpp FrameRatePolicy) except in the highest mode.
|
|
// - set_display_refresh_rate() / max_refresh_rate(): MainActivity switches the display mode at run time
|
|
// for the frame-rate setting (60 Hz unless the setting wants more).
|
|
// - battery_power(): BatteryManager current + voltage for the perf log's power columns; the power is only
|
|
// meaningful off the charger (USB adb counts as a charger: test over wireless adb).
|
|
//
|
|
// Every NDK entry point is looked up at run time: minSdk is 24. On other platforms all of this is a no-op.
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
#ifdef __ANDROID__
|
|
#include <android/native_window.h>
|
|
#include <dlfcn.h>
|
|
#include <jni.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace android_perf {
|
|
|
|
#ifdef __ANDROID__
|
|
inline void* libandroid() {
|
|
static void* lib = dlopen("libandroid.so", RTLD_NOW);
|
|
return lib;
|
|
}
|
|
|
|
inline double call_activity_float(const char* name) {
|
|
auto* env = static_cast<JNIEnv*>(SDL_GetAndroidJNIEnv());
|
|
auto activity = static_cast<jobject>(SDL_GetAndroidActivity());
|
|
if (!env || !activity) return -1.0;
|
|
double hz = -1.0;
|
|
jclass cls = env->GetObjectClass(activity);
|
|
if (jmethodID method = env->GetMethodID(cls, name, "()F"))
|
|
hz = env->CallFloatMethod(activity, method);
|
|
if (env->ExceptionCheck()) {
|
|
env->ExceptionClear();
|
|
hz = -1.0;
|
|
}
|
|
env->DeleteLocalRef(cls);
|
|
env->DeleteLocalRef(activity);
|
|
return hz;
|
|
}
|
|
|
|
inline void call_activity_set_float(const char* name, float value) {
|
|
auto* env = static_cast<JNIEnv*>(SDL_GetAndroidJNIEnv());
|
|
auto activity = static_cast<jobject>(SDL_GetAndroidActivity());
|
|
if (!env || !activity) return;
|
|
jclass cls = env->GetObjectClass(activity);
|
|
if (jmethodID method = env->GetMethodID(cls, name, "(F)V"))
|
|
env->CallVoidMethod(activity, method, jfloat(value));
|
|
if (env->ExceptionCheck()) env->ExceptionClear();
|
|
env->DeleteLocalRef(cls);
|
|
env->DeleteLocalRef(activity);
|
|
}
|
|
|
|
inline double display_refresh_rate() { return call_activity_float("currentRefreshRate"); }
|
|
// The highest refresh rate the display offers at its current resolution; -1 if unknown.
|
|
inline double max_refresh_rate() { return call_activity_float("maxRefreshRate"); }
|
|
// Switches the display mode to the refresh rate closest to `hz` (MainActivity.preferredDisplayModeId).
|
|
inline void set_display_refresh_rate(float hz) { call_activity_set_float("setDisplayRefreshRate", hz); }
|
|
// The app-vsync rate (Choreographer); -1 until the first measurement.
|
|
inline double render_rate() { return call_activity_float("currentRenderRate"); }
|
|
inline double battery_celsius() { return call_activity_float("batteryTemperature"); }
|
|
|
|
struct BatteryPower {
|
|
double current_ma = 0; // drawn from the battery, positive while discharging
|
|
double voltage_mv = -1;
|
|
double power_mw = -1; // current x voltage; -1 while plugged in or unknown
|
|
bool plugged = false;
|
|
};
|
|
|
|
// BatteryManager's CURRENT_NOW is uA by the docs but mA on some vendors, and its sign convention varies:
|
|
// a game draws well over 20 mA, so a magnitude above 20000 can only be uA; unplugged, the whole current is
|
|
// the draw. Plugged in, the charger supplies part of it and the reading says nothing about the game.
|
|
inline BatteryPower battery_power() {
|
|
BatteryPower out;
|
|
const double raw = call_activity_float("batteryCurrentRaw");
|
|
out.current_ma = std::fabs(raw) > 20000.0 ? std::fabs(raw) / 1000.0 : std::fabs(raw);
|
|
out.voltage_mv = call_activity_float("batteryVoltage");
|
|
out.plugged = call_activity_float("batteryPlugged") > 0.5;
|
|
if (!out.plugged && out.current_ma > 0 && out.voltage_mv > 0)
|
|
out.power_mw = out.current_ma * out.voltage_mv / 1000.0;
|
|
return out;
|
|
}
|
|
|
|
// false when the call is unavailable (API < 30) or rejected.
|
|
inline bool request_frame_rate(SDL_Window* window, float fps) {
|
|
using SetFrameRate = int32_t (*)(ANativeWindow*, float, int8_t);
|
|
static const auto set_frame_rate =
|
|
libandroid() ? reinterpret_cast<SetFrameRate>(dlsym(libandroid(), "ANativeWindow_setFrameRate")) : nullptr;
|
|
if (!set_frame_rate || !window) return false;
|
|
auto* native = static_cast<ANativeWindow*>(SDL_GetPointerProperty(
|
|
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_ANDROID_WINDOW_POINTER, nullptr));
|
|
if (!native) return false;
|
|
// ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT: a game, not fixed-rate video.
|
|
return set_frame_rate(native, fps, 0) == 0;
|
|
}
|
|
|
|
class PerformanceHint {
|
|
public:
|
|
// `thread_ids`: the threads that do each frame's work (render thread + script thread).
|
|
bool open(const std::vector<int32_t>& thread_ids, std::int64_t target_ns) {
|
|
void* lib = libandroid();
|
|
if (!lib || thread_ids.empty()) return false;
|
|
get_manager_ = reinterpret_cast<GetManager>(dlsym(lib, "APerformanceHint_getManager"));
|
|
create_ = reinterpret_cast<CreateSession>(dlsym(lib, "APerformanceHint_createSession"));
|
|
update_target_ = reinterpret_cast<UpdateTarget>(dlsym(lib, "APerformanceHint_updateTargetWorkDuration"));
|
|
report_ = reinterpret_cast<Report>(dlsym(lib, "APerformanceHint_reportActualWorkDuration"));
|
|
close_ = reinterpret_cast<Close>(dlsym(lib, "APerformanceHint_closeSession"));
|
|
prefer_efficiency_ = reinterpret_cast<PreferEfficiency>(
|
|
dlsym(lib, "APerformanceHint_setPreferPowerEfficiency"));
|
|
if (!get_manager_ || !create_ || !report_ || !close_) return false;
|
|
void* manager = get_manager_();
|
|
if (!manager) return false;
|
|
session_ = create_(manager, thread_ids.data(), thread_ids.size(), target_ns);
|
|
target_ns_ = target_ns;
|
|
return session_ != nullptr;
|
|
}
|
|
|
|
~PerformanceHint() {
|
|
if (session_ && close_) close_(session_);
|
|
}
|
|
|
|
bool active() const { return session_ != nullptr; }
|
|
|
|
void set_target(std::int64_t target_ns) {
|
|
if (!session_ || !update_target_ || target_ns == target_ns_ || target_ns <= 0) return;
|
|
update_target_(session_, target_ns);
|
|
target_ns_ = target_ns;
|
|
}
|
|
|
|
void report(std::int64_t actual_ns) {
|
|
if (session_ && actual_ns > 0) report_(session_, actual_ns);
|
|
}
|
|
|
|
// API 35: the scheduler may favour efficiency cores and lower clocks for the session's threads.
|
|
// false when unavailable.
|
|
bool prefer_power_efficiency(bool enabled) {
|
|
if (!session_ || !prefer_efficiency_) return false;
|
|
if (enabled == prefer_efficiency_on_) return true;
|
|
prefer_efficiency_on_ = enabled;
|
|
return prefer_efficiency_(session_, enabled) == 0;
|
|
}
|
|
|
|
private:
|
|
using GetManager = void* (*)();
|
|
using CreateSession = void* (*)(void*, const int32_t*, size_t, int64_t);
|
|
using UpdateTarget = int (*)(void*, int64_t);
|
|
using Report = int (*)(void*, int64_t);
|
|
using Close = void (*)(void*);
|
|
using PreferEfficiency = int (*)(void*, bool);
|
|
GetManager get_manager_ = nullptr;
|
|
CreateSession create_ = nullptr;
|
|
UpdateTarget update_target_ = nullptr;
|
|
Report report_ = nullptr;
|
|
Close close_ = nullptr;
|
|
PreferEfficiency prefer_efficiency_ = nullptr;
|
|
bool prefer_efficiency_on_ = false;
|
|
void* session_ = nullptr;
|
|
std::int64_t target_ns_ = 0;
|
|
};
|
|
|
|
inline int32_t current_thread_id() { return int32_t(gettid()); }
|
|
#else
|
|
inline double display_refresh_rate() {
|
|
const SDL_DisplayMode* mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
|
|
return mode ? double(mode->refresh_rate) : -1.0;
|
|
}
|
|
inline double render_rate() { return display_refresh_rate(); }
|
|
inline double max_refresh_rate() { return display_refresh_rate(); }
|
|
inline void set_display_refresh_rate(float) {}
|
|
inline double battery_celsius() { return -1.0; }
|
|
struct BatteryPower {
|
|
double current_ma = 0, voltage_mv = -1, power_mw = -1;
|
|
bool plugged = false;
|
|
};
|
|
inline BatteryPower battery_power() { return {}; }
|
|
inline bool request_frame_rate(SDL_Window*, float) { return false; }
|
|
class PerformanceHint {
|
|
public:
|
|
bool open(const std::vector<int32_t>&, std::int64_t) { return false; }
|
|
bool active() const { return false; }
|
|
void set_target(std::int64_t) {}
|
|
void report(std::int64_t) {}
|
|
bool prefer_power_efficiency(bool) { return false; }
|
|
};
|
|
inline int32_t current_thread_id() { return 0; }
|
|
#endif
|
|
|
|
} // namespace android_perf
|