From cb3b5afd466559db43935340c9bdce0515d1cfaf Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Mon, 9 Nov 2020 18:32:13 +0000 Subject: [PATCH] Bug 1675887 - Respect the time value that's passed to Choreographer.FrameCallback.doFrame. r=kats This makes the vsync times more consistent. On a Pixel 2 running Android 11, the callback was called between 1ms and 6ms after the vsync timestamp in my tests. Having the vsync timestamp slightly in the past is what the rest of our vsync infrastructure expects. Differential Revision: https://phabricator.services.mozilla.com/D96279 --- gfx/thebes/gfxAndroidPlatform.cpp | 13 +++++++++++-- .../java/org/mozilla/gecko/gfx/VsyncSource.java | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/gfx/thebes/gfxAndroidPlatform.cpp b/gfx/thebes/gfxAndroidPlatform.cpp index 20d10745e193..8b54ed600a72 100644 --- a/gfx/thebes/gfxAndroidPlatform.cpp +++ b/gfx/thebes/gfxAndroidPlatform.cpp @@ -298,9 +298,18 @@ class AndroidVsyncSource final : public VsyncSource { using Base = java::VsyncSource::Natives; using Base::DisposeNative; - static void NotifyVsync() { + static void NotifyVsync(int64_t aFrameTimeNanos) { + // Convert aFrameTimeNanos to a TimeStamp. The value converts trivially to + // the internal ticks representation of TimeStamp_posix; both use the + // monotonic clock and are in nanoseconds. + TimeStamp nativeTime = TimeStamp::FromSystemTime(aFrameTimeNanos); + + // Use the timebase from the frame callback as the vsync time, unless it + // is in the future. + TimeStamp now = TimeStamp::Now(); + TimeStamp vsyncTime = nativeTime < now ? nativeTime : now; + Display& display = GetDisplayInstance(); - TimeStamp vsyncTime = TimeStamp::Now(); TimeStamp outputTime = vsyncTime + display.GetVsyncRate(); display.NotifyVsync(vsyncTime, outputTime); } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/VsyncSource.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/VsyncSource.java index f4a96492276a..d3161b462e06 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/VsyncSource.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/gfx/VsyncSource.java @@ -42,13 +42,13 @@ import org.mozilla.gecko.GeckoAppShell; } @WrapForJNI(stubName = "NotifyVsync") - private static native void nativeNotifyVsync(); + private static native void nativeNotifyVsync(final long frameTimeNanos); // Choreographer callback implementation. public void doFrame(final long frameTimeNanos) { if (mObservingVsync) { mChoreographer.postFrameCallback(this); - nativeNotifyVsync(); + nativeNotifyVsync(frameTimeNanos); } }