drm_hwcomposer: Fix race conditions in VSyncWorker

vsync_period_ns_ and last_timestamp_ can be written to from both
threads, so ensure that they are protected by mutex_

Change-Id: Ifbfe0dcf057968fa27158c8a6f6edec186dbd5b4
Signed-off-by: Drew Davenport <ddavenport@google.com>
diff --git a/drm/VSyncWorker.cpp b/drm/VSyncWorker.cpp
index defbe42..6dd6b02 100644
--- a/drm/VSyncWorker.cpp
+++ b/drm/VSyncWorker.cpp
@@ -131,9 +131,13 @@
 static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
 
 int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
-  auto time_now = ResourceManager::GetTimeMonotonicNs();
+  int64_t phased_timestamp = 0;
+  {
+    std::lock_guard<std::mutex> lock(mutex_);
+    int64_t time_now = ResourceManager::GetTimeMonotonicNs();
+    phased_timestamp = GetPhasedVSync(vsync_period_ns_, time_now);
+  }
 
-  auto phased_timestamp = GetPhasedVSync(vsync_period_ns_, time_now);
   struct timespec vsync {};
   vsync.tv_sec = int(phased_timestamp / kOneSecondNs);
   vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
@@ -191,6 +195,7 @@
     }
 
     std::optional<VsyncTimestampCallback> vsync_callback;
+    int64_t vsync_period_ns = 0;
 
     {
       const std::lock_guard<std::mutex> lock(mutex_);
@@ -200,12 +205,13 @@
         last_vsync_timestamp_ = timestamp;
       }
       vsync_callback = callback_;
+      vsync_period_ns = vsync_period_ns_;
+      last_timestamp_ = timestamp;
     }
 
     if (vsync_callback) {
-      vsync_callback.value()(timestamp, vsync_period_ns_);
+      vsync_callback.value()(timestamp, vsync_period_ns);
     }
-    last_timestamp_ = timestamp;
   }
 
   ALOGI("VSyncWorker thread exit");