Bug 1348310 - Use CLOCK_MONOTONIC as a base for nsWindow::GetEventTimeStamp() on Wayland, r?karlt draft
authorMartin Stransky <stransky@redhat.com>
Thu, 19 Oct 2017 15:28:47 +0200
changeset 690083 f70ccf54e8f5f10fda4497bb3e825b71502ff9a7
parent 690034 cd7217cf05a2332a8fd7b498767a07b2c31ea657
child 738483 d32eaaa77bb3c32f4dfee3cda0b5baefe2a1f4f3
push id87205
push userstransky@redhat.com
push dateWed, 01 Nov 2017 15:06:49 +0000
reviewerskarlt
bugs1348310
milestone58.0a1
Bug 1348310 - Use CLOCK_MONOTONIC as a base for nsWindow::GetEventTimeStamp() on Wayland, r?karlt We assume CLOCK_MONOTONIC as timebase for events on Wayland and use that to translates GDK event times to gecko timestamps. MozReview-Commit-ID: LWd2KWTQeha
mozglue/misc/TimeStamp.h
widget/gtk/nsWindow.cpp
--- a/mozglue/misc/TimeStamp.h
+++ b/mozglue/misc/TimeStamp.h
@@ -417,19 +417,21 @@ public:
    * The system timestamps are the same as the TimeStamp
    * retrieved by mozilla::TimeStamp. Since we need this for
    * vsync timestamps, we enable the creation of mozilla::TimeStamps
    * on platforms that support vsync aligned refresh drivers / compositors
    * Verified true as of Jan 31, 2015: B2G and OS X
    * False on Windows 7
    * Android's event time uses CLOCK_MONOTONIC via SystemClock.uptimeMilles.
    * So it is same value of TimeStamp posix implementation.
+   * Wayland/GTK event time also uses CLOCK_MONOTONIC on Weston/Mutter
+   * compositors.
    * UNTESTED ON OTHER PLATFORMS
    */
-#if defined(XP_DARWIN) || defined(MOZ_WIDGET_ANDROID)
+#if defined(XP_DARWIN) || defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GTK)
   static TimeStamp FromSystemTime(int64_t aSystemTime)
   {
     static_assert(sizeof(aSystemTime) == sizeof(TimeStampValue),
                   "System timestamp should be same units as TimeStampValue");
     return TimeStamp(aSystemTime);
   }
 #endif
 
--- a/widget/gtk/nsWindow.cpp
+++ b/widget/gtk/nsWindow.cpp
@@ -2989,21 +2989,36 @@ nsWindow::GetEventTimeStamp(guint32 aEve
         return TimeStamp::Now();
     }
     if (aEventTime == 0) {
         // Some X11 and GDK events may be received with a time of 0 to indicate
         // that they are synthetic events. Some input method editors do this.
         // In this case too, just return the current timestamp.
         return TimeStamp::Now();
     }
-    CurrentX11TimeGetter* getCurrentTime = GetCurrentTimeGetter();
-    MOZ_ASSERT(getCurrentTime,
-               "Null current time getter despite having a window");
-    return TimeConverter().GetTimeStampFromSystemTime(aEventTime,
-                                                      *getCurrentTime);
+
+    TimeStamp eventTimeStamp;
+
+    if (!mIsX11Display) {
+        // Wayland compositors use monotonic time to set timestamps.
+        int64_t timestampTime = g_get_monotonic_time() / 1000;
+        guint32 refTimeTruncated = guint32(timestampTime);
+
+        timestampTime -= refTimeTruncated - aEventTime;
+        int64_t tick =
+            BaseTimeDurationPlatformUtils::TicksFromMilliseconds(timestampTime);
+        eventTimeStamp = TimeStamp::FromSystemTime(tick);
+    } else {
+        CurrentX11TimeGetter* getCurrentTime = GetCurrentTimeGetter();
+        MOZ_ASSERT(getCurrentTime,
+                   "Null current time getter despite having a window");
+        eventTimeStamp = TimeConverter().GetTimeStampFromSystemTime(aEventTime,
+                                                              *getCurrentTime);
+    }
+    return eventTimeStamp;
 }
 
 mozilla::CurrentX11TimeGetter*
 nsWindow::GetCurrentTimeGetter() {
     MOZ_ASSERT(mGdkWindow, "Expected mGdkWindow to be set");
     if (MOZ_UNLIKELY(!mCurrentTimeGetter)) {
         mCurrentTimeGetter = MakeUnique<CurrentX11TimeGetter>(mGdkWindow);
     }