drm_hwcomposer: Move vsync tracking into VSyncWorker

Track the timestamp of the last vsync event in VSyncWorker, and provide
an interface for HwcDisplay to enable/disable tracking, and to query the
most recent timestamp.

Moving this into VSyncWorker avoids the need for the VSyncWorker thread
to acquire the global lock.

Change-Id: Ib79d9a6dc5e0f7d6a36ca01dbcda5434a0f582a4
Signed-off-by: Drew Davenport <ddavenport@google.com>
diff --git a/drm/VSyncWorker.cpp b/drm/VSyncWorker.cpp
index 851e87c..c120af6 100644
--- a/drm/VSyncWorker.cpp
+++ b/drm/VSyncWorker.cpp
@@ -63,6 +63,21 @@
   vsync_period_ns_ = vsync_period_ns;
 }
 
+void VSyncWorker::SetVsyncTimestampTracking(bool enabled) {
+  const std::lock_guard<std::mutex> lock(mutex_);
+  enable_vsync_timestamps_ = enabled;
+  if (enabled) {
+    // Reset the last timestamp so the caller knows if a vsync timestamp is
+    // fresh or not.
+    last_vsync_timestamp_ = 0;
+  }
+}
+
+uint32_t VSyncWorker::GetLastVsyncTimestamp() {
+  const std::lock_guard<std::mutex> lock(mutex_);
+  return last_vsync_timestamp_;
+}
+
 void VSyncWorker::StopThread() {
   {
     const std::lock_guard<std::mutex> lock(mutex_);
@@ -164,6 +179,9 @@
       if (!enabled_)
         continue;
       callback = callbacks_.out_event;
+      if (enable_vsync_timestamps_) {
+        last_vsync_timestamp_ = timestamp;
+      }
     }
 
     if (callback)