Merge "sf: Reduce instructions in SmoMo & LayerExt update" into s-keystone-qcom-dev
diff --git a/services/surfaceflinger/CompositionEngine/include/compositionengine/Output.h b/services/surfaceflinger/CompositionEngine/include/compositionengine/Output.h
index 12a5797..68c7c86 100644
--- a/services/surfaceflinger/CompositionEngine/include/compositionengine/Output.h
+++ b/services/surfaceflinger/CompositionEngine/include/compositionengine/Output.h
@@ -267,6 +267,10 @@
     // Latches the front-end layer state for each output layer
     virtual void updateLayerStateFromFE(const CompositionRefreshArgs&) const = 0;
 
+    // Gets Layer IDs and Names of Visible layers managed by this output.
+    virtual void getVisibleLayerInfo(std::vector<std::string> *layerName,
+                                     std::vector<int32_t> *layerSequence) const = 0;
+
 protected:
     virtual void setDisplayColorProfile(std::unique_ptr<DisplayColorProfile>) = 0;
     virtual void setRenderSurface(std::unique_ptr<RenderSurface>) = 0;
diff --git a/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Output.h b/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Output.h
index f832084..2938da5 100644
--- a/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Output.h
+++ b/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Output.h
@@ -101,6 +101,8 @@
     void setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile>);
     void setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface>);
     bool plannerEnabled() const { return mPlanner != nullptr; }
+    void getVisibleLayerInfo(std::vector<std::string> *layerName,
+                             std::vector<int32_t> *layerSequence) const override;
 
 protected:
     std::unique_ptr<compositionengine::OutputLayer> createOutputLayer(const sp<LayerFE>&) const;
diff --git a/services/surfaceflinger/CompositionEngine/include/compositionengine/mock/Output.h b/services/surfaceflinger/CompositionEngine/include/compositionengine/mock/Output.h
index 8e777e3..0a4bd88 100644
--- a/services/surfaceflinger/CompositionEngine/include/compositionengine/mock/Output.h
+++ b/services/surfaceflinger/CompositionEngine/include/compositionengine/mock/Output.h
@@ -118,6 +118,7 @@
                  void(const Region&, std::vector<LayerFE::LayerSettings>&));
     MOCK_METHOD1(setExpensiveRenderingExpected, void(bool));
     MOCK_METHOD1(cacheClientCompositionRequests, void(uint32_t));
+    MOCK_CONST_METHOD2(getVisibleLayerInfo, void(std::vector<std::string>*, std::vector<int32_t>*));
 };
 
 } // namespace android::compositionengine::mock
diff --git a/services/surfaceflinger/CompositionEngine/src/Output.cpp b/services/surfaceflinger/CompositionEngine/src/Output.cpp
index 217b966..fc15bc4 100644
--- a/services/surfaceflinger/CompositionEngine/src/Output.cpp
+++ b/services/surfaceflinger/CompositionEngine/src/Output.cpp
@@ -1404,5 +1404,13 @@
     return result;
 }
 
+void Output::getVisibleLayerInfo(std::vector<std::string> *layerName,
+                                 std::vector<int32_t> *layerSequence) const {
+    for (auto* layer: getOutputLayersOrderedByZ()) {
+        layerName->push_back(layer->getLayerFE().getDebugName());
+        layerSequence->push_back(layer->getLayerFE().getSequence());
+    }
+}
+
 } // namespace impl
 } // namespace android::compositionengine
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 0ced942..90ada59 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2992,17 +2992,22 @@
         }
     }
 
-    if (mSplitLayerExt && mLayerExt) {
-        std::vector<std::string> layerInfo;
-        mDrawingState.traverse([&](Layer* layer) {
-            if (layer->findOutputLayerForDisplay(display)) {
-                layerInfo.push_back(layer->getName());
-            }
-        });
-        mLayerExt->UpdateLayerState(layerInfo, mNumLayers);
+    std::vector<std::string> layerName;
+    std::vector<int32_t> layerSequence;
+    bool layerExtEnabled = (mSplitLayerExt && mLayerExt);
+    bool visibleLayersInfo = false;
+
+    if (mSmoMo || layerExtEnabled) {
+        const auto compositionDisplay = display->getCompositionDisplay();
+        compositionDisplay->getVisibleLayerInfo(&layerName, &layerSequence);
+        visibleLayersInfo = (layerName.size() != 0);
     }
 
-    if (mSmoMo) {
+    if (layerExtEnabled && visibleLayersInfo) {
+        mLayerExt->UpdateLayerState(layerName, mNumLayers);
+    }
+
+    if (mSmoMo && visibleLayersInfo) {
         ATRACE_NAME("SmoMoUpdateState");
         Mutex::Autolock lock(mStateLock);
 
@@ -3011,14 +3016,12 @@
 
         // Disable SmoMo by passing empty layer stack in multiple display case
         if (mDisplays.size() == 1) {
-            mDrawingState.traverse([&](Layer* layer) {
-                if (layer->findOutputLayerForDisplay(display)) {
-                    smomo::SmomoLayerStats layerStats;
-                    layerStats.id = layer->getSequence();
-                    layerStats.name = layer->getName();
-                    layers.push_back(layerStats);
-                }
-            });
+            for (int i = 0; i < layerName.size(); i++) {
+                smomo::SmomoLayerStats layerStats;
+                layerStats.name = layerName.at(i);
+                layerStats.id = layerSequence.at(i);
+                layers.push_back(layerStats);
+            }
 
             fps = mRefreshRateConfigs->getCurrentRefreshRate().getFps().getValue();
         }