SF Bounds caching 2/3: Compute and cache layer bounds during invalidate stage

- Compute and cache:
   - effective transform - transform applied to source bounds to get the screen bounds,
     taking into account parent scaling and parent transforms.
   - source bounds - bounds of the layer before any transformation is applied and before
      it has been cropped by its parents.
   - bounds - bounds of the layer cropped to its own crop as well as its parents bounds.
   - screen bounds - bounds in screen space calculated by transforming the layer bounds.
- Rename existing computeBounds and computeScreenBounds to make switch easier in
  future patch.
- Update cached bounds when reparenting layers for screenshots.
Note: this change doesn't use the cached values.

Test: go/wm-smoke
Test: atest -a libinput_tests inputflinger_tests SurfaceFlinger_test libsurfaceflinger_unittest SurfaceParcelable_test libgui_test
Change-Id: I5d6ec6fd4acb0f955de27b713691ed99b029dcc2
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 5f8de83..ae2d4aa 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -1752,9 +1752,13 @@
     ATRACE_CALL();
     bool refreshNeeded = handlePageFlip();
 
+    if (mVisibleRegionsDirty) {
+        computeLayerBounds();
+    }
+
     for (auto& layer : mLayersPendingRefresh) {
         Region visibleReg;
-        visibleReg.set(layer->computeScreenBounds());
+        visibleReg.set(layer->getScreenBounds());
         invalidateLayerStack(layer, visibleReg);
     }
     mLayersPendingRefresh.clear();
@@ -2233,6 +2237,21 @@
 }
 #pragma clang optimize on // b/119477596
 
+void SurfaceFlinger::computeLayerBounds() {
+    for (const auto& pair : mDisplays) {
+        const auto& displayDevice = pair.second;
+        const auto display = displayDevice->getCompositionDisplay();
+        for (const auto& layer : mDrawingState.layersSortedByZ) {
+            // only consider the layers on the given layer stack
+            if (!display->belongsInOutput(layer->getLayerStack(), layer->getPrimaryDisplayOnly())) {
+                return;
+            }
+
+            layer->computeBounds(displayDevice->getViewport().toFloatRect(), ui::Transform());
+        }
+    }
+}
+
 void SurfaceFlinger::rebuildLayerStacks() {
     ATRACE_CALL();
     ALOGV("rebuildLayerStacks");
@@ -2977,7 +2996,7 @@
                 //       compute the actual visible region
                 // TODO: we could cache the transformed region
                 Region visibleReg;
-                visibleReg.set(layer->computeScreenBounds());
+                visibleReg.set(layer->getScreenBounds());
                 invalidateLayerStack(layer, visibleReg);
             }
         });
@@ -3141,7 +3160,7 @@
         // handle hidden surfaces by setting the visible region to empty
         if (CC_LIKELY(layer->isVisible())) {
             const bool translucent = !layer->isOpaque(s);
-            Rect bounds(layer->computeScreenBounds());
+            Rect bounds(layer->getScreenBounds());
 
             visibleRegion.set(bounds);
             ui::Transform tr = layer->getTransform();
@@ -5438,11 +5457,14 @@
             const sp<Layer>& oldParent;
             const sp<Layer>& newParent;
 
-            ReparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent)
+            ReparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent,
+                               const Rect& drawingBounds)
                   : oldParent(oldParent), newParent(newParent) {
+                // Compute and cache the bounds for the new parent layer.
+                newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform());
                 oldParent->setChildrenDrawingParent(newParent);
             }
-            ~ReparentForDrawing() { oldParent->setChildrenDrawingParent(oldParent); }
+            ~ReparentForDrawing() { newParent->setChildrenDrawingParent(oldParent); }
         };
 
         void render(std::function<void()> drawLayers) override {
@@ -5460,7 +5482,7 @@
                         LayerCreationArgs(mFlinger, nullptr, String8("Screenshot Parent"),
                                           bounds.getWidth(), bounds.getHeight(), 0));
 
-                ReparentForDrawing reparent(mLayer, screenshotParentLayer);
+                ReparentForDrawing reparent(mLayer, screenshotParentLayer, sourceCrop);
                 drawLayers();
             }
         }