A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(android): ask the system for 60 fps and clock frames by the vsync timestamp
yhh8e8dae6391b5
diff --git a/native/host/Host.cpp b/native/host/Host.cppindex fcbb315d8..ac70b4695 100644--- a/native/host/Host.cpp+++ b/native/host/Host.cpp@@ -284,7 +284,7 @@ bool boot(Platform& platform) {return true;}-void frame() {+void frame(long long vsyncNanos) {if (!booted()) return;HostState& h = host();// Skipping is normal (the screen is off, the window is going away) and being@@ -328,7 +328,11 @@ void frame() {// stall wherever it runs.constexpr double kMaxDelta = 0.25;constexpr double kFirstFrameDelta = 1.0 / 60.0;- const auto nowAt = std::chrono::steady_clock::now();+ // The vsync being drawn for when the display supplied one — the web loop's rule too (rAF's timestamp, not callback+ // entry). Android's Choreographer stamps on CLOCK_MONOTONIC, which is the steady clock there.+ const auto nowAt = vsyncNanos > 0+ ? std::chrono::steady_clock::time_point(std::chrono::nanoseconds(vsyncNanos))+ : std::chrono::steady_clock::now();double deltaSeconds = kFirstFrameDelta;if (h.haveLastFrame) {deltaSeconds = std::chrono::duration<double>(nowAt - h.lastFrameAt).count();diff --git a/native/host/Host.hpp b/native/host/Host.hppindex 0cb9462f8..b0cecbc0e 100644--- a/native/host/Host.hpp+++ b/native/host/Host.hpp@@ -306,8 +306,13 @@ bool bindSurface();void surfaceLost();/** @brief Runs the game's update + one rendered, presented frame. No-op until- * boot() has succeeded and a surface is bound. */-void frame();+ * boot() has succeeded and a surface is bound.+ *+ * @param vsyncNanos The vsync this frame is for, on the steady clock's timeline+ * (Android's Choreographer hands one over), or 0 to measure the wall clock.+ * Frame to frame it is the display's cadence; the wall clock at callback+ * entry carries the scheduler's jitter, which reads as uneven motion. */+void frame(long long vsyncNanos = 0);/** @brief Feeds one touch to the game. @p type: 0 start / 1 move / 2 end / 3 cancel.* Coordinates are in surface pixels, top-left origin (as on the web). */diff --git a/native/host/platform/android.cpp b/native/host/platform/android.cppindex e92905a89..398917d32 100644--- a/native/host/platform/android.cpp+++ b/native/host/platform/android.cpp@@ -509,6 +509,25 @@ int32_t onInput(android_app*, AInputEvent* ev) {void framesSurface(bool has);void framesResumed(bool is);+// Tells the system what rate the game is meant to run at, so the panel and the Choreographer settle on a cadence the game can+// hold. Without it a 120 Hz panel calls the frame every 8.3 ms, a frame that costs 10 ms plus a blocked present misses by a+// vsync, and the period lands on 16.6 / 25 / 33 ms at random — 45 to 55 fps and uneven motion. 60 Hz is an exact fraction of+// 120 Hz and of 60 Hz, so the system can hold it on either. ANativeWindow_setFrameRate is API 30; the manifest floor is 29,+// so it is looked up like PerformanceHints' symbols rather than made a load-time requirement.+void hintFrameRate(ANativeWindow* window) {+ constexpr float kTargetFps = 60.0f;+ constexpr int8_t kCompatibilityDefault = 0; // ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT+ using SetFrameRateFn = int32_t (*)(ANativeWindow*, float, int8_t);+ const auto set = reinterpret_cast<SetFrameRateFn>(dlsym(RTLD_DEFAULT, "ANativeWindow_setFrameRate"));+ if (!window || !set) {+ if (window) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, "frame rate hint: unavailable (API < 30)");+ return;+ }+ const int32_t result = set(window, kTargetFps, kCompatibilityDefault);+ __android_log_print(result == 0 ? ANDROID_LOG_INFO : ANDROID_LOG_WARN, LOG_TAG,+ "frame rate hint: %.0f fps (%s)", kTargetFps, result == 0 ? "accepted" : "refused");+}+void onAppCmd(android_app* app, int32_t cmd) {// NativeActivity supplies a content rectangle in window pixels; the glue// updates it before delivering CONTENT_RECT_CHANGED, including rotation.@@ -523,6 +542,7 @@ void onAppCmd(android_app* app, int32_t cmd) {else eshost::bindSurface();// Ask for frames only once there is something to present to, the// same order iOS unpauses its display link in.+ hintFrameRate(app->window); // per window: a recreated surface forgets itframesSurface(eshost::surfaceBound());}break;@@ -706,18 +726,19 @@ struct FrameDriver {}private:- void tick() {+ void tick(int64_t vsyncNanos) {posted = false;if (!running) return;// Timed around the frame alone, and re-posted after it: asking for the// next vsync first would let a frame that overran queue behind itself.const auto begin = std::chrono::steady_clock::now();- eshost::frame();+ eshost::frame(vsyncNanos);if (hints) hints->report(std::chrono::steady_clock::now() - begin);post();}- static void onVsync64(int64_t, void* data) { static_cast<FrameDriver*>(data)->tick(); }+ // The vsync's own timestamp is the game clock's tick (Host::frame), not the moment this callback happened to run.+ static void onVsync64(int64_t frameTimeNanos, void* data) { static_cast<FrameDriver*>(data)->tick(frameTimeNanos); }};FrameDriver g_frames;