Store layer snapshot in LayerRenderArea

Accessing the layer snapshot from a RenderArea lessens the
dependency on getting the layer snapshot from SF's
mLayerSnapshotBuilder, which needs to run on the main thread.

Bug: b/294936197
Test: atest SurfaceFlinger_test
Change-Id: I7c2a77a432ddae3f7bdd39311a3a505aa8f763d6
diff --git a/services/surfaceflinger/LayerRenderArea.cpp b/services/surfaceflinger/LayerRenderArea.cpp
index c25ddb6..f323ce7 100644
--- a/services/surfaceflinger/LayerRenderArea.cpp
+++ b/services/surfaceflinger/LayerRenderArea.cpp
@@ -25,13 +25,14 @@
 
 namespace android {
 
-LayerRenderArea::LayerRenderArea(sp<Layer> layer, const Rect& crop, ui::Size reqSize,
-                                 ui::Dataspace reqDataSpace, bool allowSecureLayers,
-                                 const ui::Transform& layerTransform, const Rect& layerBufferSize,
-                                 bool hintForSeamlessTransition)
+LayerRenderArea::LayerRenderArea(sp<Layer> layer, frontend::LayerSnapshot layerSnapshot,
+                                 const Rect& crop, ui::Size reqSize, ui::Dataspace reqDataSpace,
+                                 bool allowSecureLayers, const ui::Transform& layerTransform,
+                                 const Rect& layerBufferSize, bool hintForSeamlessTransition)
       : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, hintForSeamlessTransition,
                    allowSecureLayers),
         mLayer(std::move(layer)),
+        mLayerSnapshot(std::move(layerSnapshot)),
         mLayerBufferSize(layerBufferSize),
         mCrop(crop),
         mTransform(layerTransform) {}
diff --git a/services/surfaceflinger/LayerRenderArea.h b/services/surfaceflinger/LayerRenderArea.h
index b12afe8..a12bfca 100644
--- a/services/surfaceflinger/LayerRenderArea.h
+++ b/services/surfaceflinger/LayerRenderArea.h
@@ -32,19 +32,22 @@
 
 class LayerRenderArea : public RenderArea {
 public:
-    LayerRenderArea(sp<Layer> layer, const Rect& crop, ui::Size reqSize, ui::Dataspace reqDataSpace,
-                    bool allowSecureLayers, const ui::Transform& layerTransform,
-                    const Rect& layerBufferSize, bool hintForSeamlessTransition);
+    LayerRenderArea(sp<Layer> layer, frontend::LayerSnapshot layerSnapshot, const Rect& crop,
+                    ui::Size reqSize, ui::Dataspace reqDataSpace, bool allowSecureLayers,
+                    const ui::Transform& layerTransform, const Rect& layerBufferSize,
+                    bool hintForSeamlessTransition);
 
     const ui::Transform& getTransform() const override;
     bool isSecure() const override;
     sp<const DisplayDevice> getDisplayDevice() const override;
     Rect getSourceCrop() const override;
 
-    virtual sp<Layer> getParentLayer() const { return mLayer; }
+    sp<Layer> getParentLayer() const override { return mLayer; }
+    const frontend::LayerSnapshot* getLayerSnapshot() const override { return &mLayerSnapshot; }
 
 private:
     const sp<Layer> mLayer;
+    const frontend::LayerSnapshot mLayerSnapshot;
     const Rect mLayerBufferSize;
     const Rect mCrop;
 
diff --git a/services/surfaceflinger/RenderArea.h b/services/surfaceflinger/RenderArea.h
index 18a5304..e8d20af 100644
--- a/services/surfaceflinger/RenderArea.h
+++ b/services/surfaceflinger/RenderArea.h
@@ -4,6 +4,8 @@
 #include <ui/Transform.h>
 
 #include <functional>
+
+#include "FrontEnd/LayerSnapshot.h"
 #include "Layer.h"
 
 namespace android {
@@ -82,6 +84,10 @@
     // capture operation.
     virtual sp<Layer> getParentLayer() const { return nullptr; }
 
+    // If this is a LayerRenderArea, return the layer snapshot
+    // of the root layer of the capture operation
+    virtual const frontend::LayerSnapshot* getLayerSnapshot() const { return nullptr; }
+
     // Returns whether the render result may be used for system animations that
     // must preserve the exact colors of the display.
     bool getHintForSeamlessTransition() const { return mHintForSeamlessTransition; }
diff --git a/services/surfaceflinger/RenderAreaBuilder.h b/services/surfaceflinger/RenderAreaBuilder.h
index 012acd2..a25c6e0 100644
--- a/services/surfaceflinger/RenderAreaBuilder.h
+++ b/services/surfaceflinger/RenderAreaBuilder.h
@@ -83,9 +83,12 @@
             layer(layer),
             childrenOnly(childrenOnly) {}
 
-    // Layer that the render area will be on
+    // Root layer of the render area
     sp<Layer> layer;
 
+    // Layer snapshot of the root layer
+    frontend::LayerSnapshot layerSnapshot;
+
     // Transform to be applied on the layers to transform them
     // into the logical render area
     ui::Transform layerTransform{ui::Transform()};
@@ -97,17 +100,18 @@
     bool childrenOnly;
 
     // Uses parent snapshot to determine layer transform and buffer size
-    void setLayerInfo(const frontend::LayerSnapshot* parentSnapshot) {
+    void setLayerSnapshot(const frontend::LayerSnapshot& parentSnapshot) {
+        layerSnapshot = parentSnapshot;
         if (!childrenOnly) {
-            layerTransform = parentSnapshot->localTransform.inverse();
+            layerTransform = parentSnapshot.localTransform.inverse();
         }
-        layerBufferSize = parentSnapshot->bufferSize;
+        layerBufferSize = parentSnapshot.bufferSize;
     }
 
     std::unique_ptr<RenderArea> build() const override {
-        return std::make_unique<LayerRenderArea>(layer, crop, reqSize, reqDataSpace,
-                                                 allowSecureLayers, layerTransform, layerBufferSize,
-                                                 hintForSeamlessTransition);
+        return std::make_unique<LayerRenderArea>(layer, std::move(layerSnapshot), crop, reqSize,
+                                                 reqDataSpace, allowSecureLayers, layerTransform,
+                                                 layerBufferSize, hintForSeamlessTransition);
     }
 };
 
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 5181fb8..b3ddb57 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -8207,7 +8207,7 @@
                 ALOGW("Couldn't find layer snapshot for %d",
                       layerRenderAreaBuilder->layer->getSequence());
             } else {
-                layerRenderAreaBuilder->setLayerInfo(snapshot);
+                layerRenderAreaBuilder->setLayerSnapshot(*snapshot);
             }
         }
 
@@ -8302,9 +8302,8 @@
         Mutex::Autolock lock(mStateLock);
         const DisplayDevice* display = nullptr;
         if (parent) {
-            const frontend::LayerSnapshot* snapshot = mLayerLifecycleManagerEnabled
-                    ? mLayerSnapshotBuilder.getSnapshot(parent->sequence)
-                    : parent->getLayerSnapshot();
+            const frontend::LayerSnapshot* snapshot =
+                    mLayerSnapshotBuilder.getSnapshot(parent->sequence);
             if (snapshot) {
                 display = findDisplay([layerStack = snapshot->outputFilter.layerStack](
                                               const auto& display) {