Replace CLOCK_MONOTONIC with SYSTEM_TIME_MONOTONIC

Using SYSTEM_TIME_MONOTONIC works for Android (where it translates to
CLOCK_MONOTONIC) and host targets, while CLOCK_MONOTONIC is not defined
on macOS.

Bug: 117921091
Test: existing tests should pass
Change-Id: I1fad472881830fb0701a320cf37319e083932ad4
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index ccadc7d..0a7bc30 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -485,7 +485,7 @@
 
 static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
-    nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
+    nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
     UiFrameInfoBuilder(proxy->frameInfo())
             .setVsync(vsync, vsync)
             .addFlag(FrameInfoFlags::SurfaceCanvas);
diff --git a/core/jni/android_view_ThreadedRenderer.cpp b/core/jni/android_view_ThreadedRenderer.cpp
index f7e9b24..84c3b3a 100644
--- a/core/jni/android_view_ThreadedRenderer.cpp
+++ b/core/jni/android_view_ThreadedRenderer.cpp
@@ -957,7 +957,7 @@
         // to all 0s.
         proxy.setLightAlpha(0, 0);
         proxy.setLightGeometry((Vector3){0, 0, 0}, 0);
-        nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
+        nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
         UiFrameInfoBuilder(proxy.frameInfo())
                 .setVsync(vsync, vsync)
                 .addFlag(FrameInfoFlags::SurfaceCanvas);
diff --git a/libs/hwui/FrameInfo.h b/libs/hwui/FrameInfo.h
index 0aab58c..b75192f 100644
--- a/libs/hwui/FrameInfo.h
+++ b/libs/hwui/FrameInfo.h
@@ -100,15 +100,15 @@
 public:
     void importUiThreadInfo(int64_t* info);
 
-    void markSyncStart() { set(FrameInfoIndex::SyncStart) = systemTime(CLOCK_MONOTONIC); }
+    void markSyncStart() { set(FrameInfoIndex::SyncStart) = systemTime(SYSTEM_TIME_MONOTONIC); }
 
     void markIssueDrawCommandsStart() {
-        set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(CLOCK_MONOTONIC);
+        set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(SYSTEM_TIME_MONOTONIC);
     }
 
-    void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(CLOCK_MONOTONIC); }
+    void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); }
 
-    void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(CLOCK_MONOTONIC); }
+    void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); }
 
     void addFlag(int frameInfoFlag) {
         set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag);
diff --git a/libs/hwui/ProfileData.cpp b/libs/hwui/ProfileData.cpp
index 70ca4e3..c7f9232 100644
--- a/libs/hwui/ProfileData.cpp
+++ b/libs/hwui/ProfileData.cpp
@@ -143,7 +143,7 @@
     mSlowFrameCounts.fill(0);
     mTotalFrameCount = 0;
     mJankFrameCount = 0;
-    mStatStartTime = systemTime(CLOCK_MONOTONIC);
+    mStatStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
 }
 
 void ProfileData::reportFrame(int64_t duration) {
diff --git a/libs/hwui/hwui/AnimatedImageDrawable.cpp b/libs/hwui/hwui/AnimatedImageDrawable.cpp
index 7677f9c..4544bea 100644
--- a/libs/hwui/hwui/AnimatedImageDrawable.cpp
+++ b/libs/hwui/hwui/AnimatedImageDrawable.cpp
@@ -24,12 +24,6 @@
 
 #include <optional>
 
-#ifdef __APPLE__
-    // macOS SDK 10.10 does not support CLOCK_MONOTONIC, which is not an issue since
-    // the value of the argument is not used in the host definition of systemTime
-#define CLOCK_MONOTONIC
-#endif
-
 namespace android {
 
 AnimatedImageDrawable::AnimatedImageDrawable(sk_sp<SkAnimatedImage> animatedImage, size_t bytesUsed)
@@ -70,7 +64,7 @@
 // Only called on the RenderThread while UI thread is locked.
 bool AnimatedImageDrawable::isDirty(nsecs_t* outDelay) {
     *outDelay = 0;
-    const nsecs_t currentTime = systemTime(CLOCK_MONOTONIC);
+    const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
     const nsecs_t lastWallTime = mLastWallTime;
 
     mLastWallTime = currentTime;
@@ -250,7 +244,7 @@
 
     bool update = false;
     {
-        const nsecs_t currentTime = systemTime(CLOCK_MONOTONIC);
+        const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
         std::unique_lock lock{mSwapLock};
         // mLastWallTime starts off at 0. If it is still 0, just update it to
         // the current time and avoid updating
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index f326ce8..c9203d9 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -463,7 +463,7 @@
         }
         SwapHistory& swap = mSwapHistory.next();
         swap.damage = windowDirty;
-        swap.swapCompletedTime = systemTime(CLOCK_MONOTONIC);
+        swap.swapCompletedTime = systemTime(SYSTEM_TIME_MONOTONIC);
         swap.vsyncTime = mRenderThread.timeLord().latestVsync();
         if (mNativeSurface.get()) {
             int durationUs;
@@ -549,7 +549,7 @@
     UiFrameInfoBuilder(frameInfo).addFlag(FrameInfoFlags::RTAnimation).setVsync(vsync, vsync);
 
     TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
-    prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
+    prepareTree(info, frameInfo, systemTime(SYSTEM_TIME_MONOTONIC), node);
     if (info.out.canDrawThisFrame) {
         draw();
     } else {
diff --git a/libs/hwui/renderthread/DrawFrameTask.cpp b/libs/hwui/renderthread/DrawFrameTask.cpp
index 91dc3bc..1e59338 100644
--- a/libs/hwui/renderthread/DrawFrameTask.cpp
+++ b/libs/hwui/renderthread/DrawFrameTask.cpp
@@ -69,7 +69,7 @@
     LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!");
 
     mSyncResult = SyncResult::OK;
-    mSyncQueued = systemTime(CLOCK_MONOTONIC);
+    mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC);
     postAndWait();
 
     return mSyncResult;
diff --git a/libs/hwui/renderthread/RenderProxy.cpp b/libs/hwui/renderthread/RenderProxy.cpp
index 1a1b9da..20247dc 100644
--- a/libs/hwui/renderthread/RenderProxy.cpp
+++ b/libs/hwui/renderthread/RenderProxy.cpp
@@ -339,7 +339,7 @@
     };
     nsecs_t lastVsync = renderThread->timeLord().latestVsync();
     nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
-    nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
+    nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
     // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
     // VSYNC+12ms or so, so aim for the gap during which RT is expected to
     // be idle
diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp
index 8638142..ee1a7ce 100644
--- a/libs/hwui/renderthread/RenderThread.cpp
+++ b/libs/hwui/renderthread/RenderThread.cpp
@@ -103,7 +103,7 @@
                                            [this]() { mRenderThread->drainDisplayEventQueue(); });
     }
 
-    virtual nsecs_t latestVsyncEvent() override { return systemTime(CLOCK_MONOTONIC); }
+    virtual nsecs_t latestVsyncEvent() override { return systemTime(SYSTEM_TIME_MONOTONIC); }
 
 private:
     RenderThread* mRenderThread;
diff --git a/libs/hwui/renderthread/TimeLord.cpp b/libs/hwui/renderthread/TimeLord.cpp
index b82c5d1..784068f 100644
--- a/libs/hwui/renderthread/TimeLord.cpp
+++ b/libs/hwui/renderthread/TimeLord.cpp
@@ -31,7 +31,7 @@
 
 nsecs_t TimeLord::computeFrameTimeNanos() {
     // Logic copied from Choreographer.java
-    nsecs_t now = systemTime(CLOCK_MONOTONIC);
+    nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
     nsecs_t jitterNanos = now - mFrameTimeNanos;
     if (jitterNanos >= mFrameIntervalNanos) {
         nsecs_t lastFrameOffset = jitterNanos % mFrameIntervalNanos;
diff --git a/libs/hwui/tests/macrobench/TestSceneRunner.cpp b/libs/hwui/tests/macrobench/TestSceneRunner.cpp
index 9c845f0..22d5abb 100644
--- a/libs/hwui/tests/macrobench/TestSceneRunner.cpp
+++ b/libs/hwui/tests/macrobench/TestSceneRunner.cpp
@@ -146,7 +146,7 @@
     }
     for (int i = 0; i < warmupFrameCount; i++) {
         testContext.waitForVsync();
-        nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
+        nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
         UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync);
         proxy->syncAndDrawFrame();
     }
@@ -161,10 +161,10 @@
 
     ModifiedMovingAverage<double> avgMs(opts.reportFrametimeWeight);
 
-    nsecs_t start = systemTime(CLOCK_MONOTONIC);
+    nsecs_t start = systemTime(SYSTEM_TIME_MONOTONIC);
     for (int i = 0; i < opts.count; i++) {
         testContext.waitForVsync();
-        nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
+        nsecs_t vsync = systemTime(SYSTEM_TIME_MONOTONIC);
         {
             ATRACE_NAME("UI-Draw Frame");
             UiFrameInfoBuilder(proxy->frameInfo()).setVsync(vsync, vsync);
@@ -173,7 +173,7 @@
         }
         if (opts.reportFrametimeWeight) {
             proxy->fence();
-            nsecs_t done = systemTime(CLOCK_MONOTONIC);
+            nsecs_t done = systemTime(SYSTEM_TIME_MONOTONIC);
             avgMs.add((done - vsync) / 1000000.0);
             if (i % 10 == 9) {
                 printf("Average frametime %.3fms\n", avgMs.average());
@@ -181,7 +181,7 @@
         }
     }
     proxy->fence();
-    nsecs_t end = systemTime(CLOCK_MONOTONIC);
+    nsecs_t end = systemTime(SYSTEM_TIME_MONOTONIC);
 
     if (reporter) {
         outputBenchmarkReport(info, opts, reporter, proxy.get(), (end - start) / (double)s2ns(1));
diff --git a/libs/hwui/thread/WorkQueue.h b/libs/hwui/thread/WorkQueue.h
index 42f8503..46b8bc0 100644
--- a/libs/hwui/thread/WorkQueue.h
+++ b/libs/hwui/thread/WorkQueue.h
@@ -31,7 +31,7 @@
 namespace android::uirenderer {
 
 struct MonotonicClock {
-    static nsecs_t now() { return systemTime(CLOCK_MONOTONIC); }
+    static nsecs_t now() { return systemTime(SYSTEM_TIME_MONOTONIC); }
 };
 
 class WorkQueue {