SF: Pass timestamps into layer tracing

This will allow us to recreate layer traces that
reflect the same timeline as the transaction
trace.

Test: presubmit
Bug: 200284593

Change-Id: I693c514ae56ed2137ad53b007a6722ba9396ecde
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index e765c9b..417417d 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2179,6 +2179,11 @@
     updateCursorAsync();
     updateInputFlinger();
 
+    if (mLayerTracingEnabled && !mLayerTracing.flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
+        // This will block and tracing should only be enabled for debugging.
+        mLayerTracing.notify(mVisibleRegionsDirty, frameTime);
+    }
+
     MAIN_THREAD_GUARD(persistDisplayBrightness(mustComposite));
 
     return mustComposite && CC_LIKELY(mBootStage != BootStage::BOOTLOADER);
@@ -2280,13 +2285,9 @@
     modulateVsync(&VsyncModulator::onDisplayRefresh, usedGpuComposition);
 
     mLayersWithQueuedFrames.clear();
-    if (mLayerTracingEnabled) {
+    if (mLayerTracingEnabled && mLayerTracing.flagIsSet(LayerTracing::TRACE_COMPOSITION)) {
         // This will block and should only be used for debugging.
-        if (mVisibleRegionsDirty) {
-            mLayerTracing.notify("visibleRegionsDirty");
-        } else if (mLayerTracing.flagIsSet(LayerTracing::TRACE_BUFFERS)) {
-            mLayerTracing.notify("bufferLatched");
-        }
+        mLayerTracing.notify(mVisibleRegionsDirty, frameTime);
     }
 
     mVisibleRegionsWereDirtyThisFrame = mVisibleRegionsDirty; // Cache value for use in post-comp
@@ -5773,12 +5774,18 @@
             }
             case 1025: { // Set layer tracing
                 n = data.readInt32();
+                int64_t fixedStartingTime = data.readInt64();
                 bool tracingEnabledChanged;
                 if (n) {
                     ALOGD("LayerTracing enabled");
                     tracingEnabledChanged = mLayerTracing.enable();
                     if (tracingEnabledChanged) {
-                        mScheduler->schedule([&]() MAIN_THREAD { mLayerTracing.notify("start"); })
+                        int64_t startingTime =
+                                (fixedStartingTime) ? fixedStartingTime : systemTime();
+                        mScheduler
+                                ->schedule([&]() MAIN_THREAD {
+                                    mLayerTracing.notify("start", startingTime);
+                                })
                                 .wait();
                     }
                 } else {
diff --git a/services/surfaceflinger/Tracing/LayerTracing.cpp b/services/surfaceflinger/Tracing/LayerTracing.cpp
index d136e0b..006efdf 100644
--- a/services/surfaceflinger/Tracing/LayerTracing.cpp
+++ b/services/surfaceflinger/Tracing/LayerTracing.cpp
@@ -98,16 +98,20 @@
     mBuffer->dump(result);
 }
 
-void LayerTracing::notify(const char* where) {
-    ATRACE_CALL();
+void LayerTracing::notify(bool visibleRegionDirty, int64_t time) {
     std::scoped_lock lock(mTraceLock);
     if (!mEnabled) {
         return;
     }
 
+    if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) {
+        return;
+    }
+
     ATRACE_CALL();
     LayersTraceProto entry;
-    entry.set_elapsed_realtime_nanos(elapsedRealtimeNano());
+    entry.set_elapsed_realtime_nanos(time);
+    const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched";
     entry.set_where(where);
     LayersProto layers(mFlinger.dumpDrawingStateProto(mFlags));
 
diff --git a/services/surfaceflinger/Tracing/LayerTracing.h b/services/surfaceflinger/Tracing/LayerTracing.h
index 8ca3587..bd448c9 100644
--- a/services/surfaceflinger/Tracing/LayerTracing.h
+++ b/services/surfaceflinger/Tracing/LayerTracing.h
@@ -47,7 +47,7 @@
     bool isEnabled() const;
     status_t writeToFile();
     LayersTraceFileProto createTraceFileProto() const;
-    void notify(const char* where);
+    void notify(bool visibleRegionDirty, int64_t time);
 
     enum : uint32_t {
         TRACE_INPUT = 1 << 1,