Fix deadlock of main thread and Perfetto thread (LayerDataSource::OnStart)

The deadly embrace used to happen in this situation:
1. Perfetto shmem buffer full.
2. Main thread blocked while writing into shmem buffer (active tracing),
   i.e. main thread waiting for Perfetto thread to signal available
   shmem chunks.
3. LayerDataSource::OnStart with MODE_DUMP (executed by Perfetto thread)
   waiting for main thread to capture a layers snapshot.

This commit removes the capture of layers snapshot in OnStart.
Now OnStop (Perfetto thread) triggers the layers snapshot capture
to be performed by the main thread. However, the Perfetto thread
doesn't wait for the main thread to complete the capture. The actual
stop of the tracing session is deferred and signalled later by the main
thread through LayerDataSource::StopArgs::HandleStopAsynchronously()
when the capture is complete.

Fix: b/313130597
Test: reproduced the issue forcing the main thread to write 100MB into the shmem buffer:

    mLayerTracing.setTakeLayersSnapshotProtoFunction([&](uint32_t traceFlags) {
        auto snapshot = perfetto::protos::LayersSnapshotProto{};
        mScheduler
                ->schedule([&]() FTL_FAKE_GUARD(mStateLock) FTL_FAKE_GUARD(kMainThreadContext) {
                    snapshot = takeLayersSnapshotProto(traceFlags, TimePoint::now(),
                                                       mLastCommittedVsyncId, true);
+                   LayerDataSource::Trace([&](LayerDataSource::TraceContext context) {
+                       auto packet = context.NewTracePacket();
+                       packet->set_timestamp(0);
+                       packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC);
+                       auto* snapshotProto = packet->set_surfaceflinger_layers_snapshot();
+
+                       auto data = std::string(100000000, 0xaa);
+                       snapshotProto->AppendRawProtoBytes(data.data(), data.size());
+                   });
                })
                .wait();
        return snapshot;
    });

Change-Id: Iea253da2a420d186d092521597f4783e5c8d157d
diff --git a/services/surfaceflinger/Tracing/LayerTracing.h b/services/surfaceflinger/Tracing/LayerTracing.h
index 2895ba7..e99fe4c 100644
--- a/services/surfaceflinger/Tracing/LayerTracing.h
+++ b/services/surfaceflinger/Tracing/LayerTracing.h
@@ -87,6 +87,7 @@
 class LayerTracing {
 public:
     using Mode = perfetto::protos::pbzero::SurfaceFlingerLayersConfig::Mode;
+    using OnLayersSnapshotCallback = std::function<void(perfetto::protos::LayersSnapshotProto&&)>;
 
     enum Flag : uint32_t {
         TRACE_INPUT = 1 << 1,
@@ -102,7 +103,7 @@
     LayerTracing(std::ostream&);
     ~LayerTracing();
     void setTakeLayersSnapshotProtoFunction(
-            const std::function<perfetto::protos::LayersSnapshotProto(uint32_t)>&);
+            const std::function<void(uint32_t, const OnLayersSnapshotCallback&)>&);
     void setTransactionTracing(TransactionTracing&);
 
     // Start event from perfetto data source
@@ -110,7 +111,7 @@
     // Flush event from perfetto data source
     void onFlush(Mode mode, uint32_t flags, bool isBugreport);
     // Stop event from perfetto data source
-    void onStop(Mode mode);
+    void onStop(Mode mode, uint32_t flags, std::function<void()>&& deferredStopDone);
 
     void addProtoSnapshotToOstream(perfetto::protos::LayersSnapshotProto&& snapshot, Mode mode);
     bool isActiveTracingStarted() const;
@@ -123,7 +124,7 @@
     void writeSnapshotToPerfetto(const perfetto::protos::LayersSnapshotProto& snapshot, Mode mode);
     bool checkAndUpdateLastVsyncIdWrittenToPerfetto(Mode mode, std::int64_t vsyncId);
 
-    std::function<perfetto::protos::LayersSnapshotProto(uint32_t)> mTakeLayersSnapshotProto;
+    std::function<void(uint32_t, const OnLayersSnapshotCallback&)> mTakeLayersSnapshotProto;
     TransactionTracing* mTransactionTracing;
 
     std::atomic<bool> mIsActiveTracingStarted{false};