surfaceflinger: switch RE color matrices lazily

Client composition in doComposeSurfaces normally needs to handle

  N sRGB layers, when in ColorMode::SRGB
  N P3 layer and then M sRGB layers, when in ColorMode::DISPLAY_P3

Constantly switching and restoring RE color matrices when handling
sRGB layers is less efficient.  Do the switches lazily.

We don't really care about the current RE color matrix when
doComposeSurfaces is called, and we already assume it to be
identity.  Update RenderEngine::setupColorTransform not to return
the current color matrix.

Bug: 78891890
Test: night light under Boosted/Enhanced and sRGB/P3
Change-Id: I3b785c2a05f2e1679e486ab2497fc0e13993a791
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 88262fb..7a4bc2a 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -2873,18 +2873,13 @@
     const DisplayRenderArea renderArea(displayDevice);
     const auto hwcId = displayDevice->getHwcDisplayId();
     const bool hasClientComposition = getBE().mHwc->hasClientComposition(hwcId);
-    const bool hasDeviceComposition = getBE().mHwc->hasDeviceComposition(hwcId);
-    const bool skipClientColorTransform = getBE().mHwc->hasCapability(
-        HWC2::Capability::SkipClientColorTransform);
     ATRACE_INT("hasClientComposition", hasClientComposition);
 
-    mat4 oldColorMatrix;
-    mat4 legacySrgbSaturationMatrix = mLegacySrgbSaturationMatrix;
-    const bool applyColorMatrix = !hasDeviceComposition && !skipClientColorTransform;
-    if (applyColorMatrix) {
-        oldColorMatrix = getRenderEngine().setupColorTransform(mDrawingState.colorMatrix);
-        legacySrgbSaturationMatrix = mDrawingState.colorMatrix * legacySrgbSaturationMatrix;
-    }
+    bool applyColorMatrix = false;
+    bool applyLegacyColorMatrix = false;
+    mat4 colorMatrix;
+    mat4 legacyColorMatrix;
+    const mat4* currentColorMatrix = nullptr;
 
     if (hasClientComposition) {
         ALOGV("hasClientComposition");
@@ -2897,6 +2892,24 @@
         getBE().mRenderEngine->setDisplayMaxLuminance(
                 displayDevice->getHdrCapabilities().getDesiredMaxLuminance());
 
+        const bool hasDeviceComposition = getBE().mHwc->hasDeviceComposition(hwcId);
+        const bool skipClientColorTransform = getBE().mHwc->hasCapability(
+            HWC2::Capability::SkipClientColorTransform);
+
+        applyColorMatrix = !hasDeviceComposition && !skipClientColorTransform;
+        if (applyColorMatrix) {
+            colorMatrix = mDrawingState.colorMatrix;
+        }
+
+        applyLegacyColorMatrix = mDisplayColorSetting == DisplayColorSetting::ENHANCED;
+        if (applyLegacyColorMatrix) {
+            if (applyColorMatrix) {
+                legacyColorMatrix = colorMatrix * mLegacySrgbSaturationMatrix;
+            } else {
+                legacyColorMatrix = mLegacySrgbSaturationMatrix;
+            }
+        }
+
         if (!displayDevice->makeCurrent()) {
             ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
                   displayDevice->getDisplayName().string());
@@ -2982,20 +2995,20 @@
                     break;
                 }
                 case HWC2::Composition::Client: {
-                    // Only apply saturation matrix layer that is legacy SRGB dataspace
-                    // when auto color mode is on.
-                    bool restore = false;
-                    mat4 savedMatrix;
-                    if (mDisplayColorSetting == DisplayColorSetting::ENHANCED &&
-                        layer->isLegacyDataSpace()) {
-                        savedMatrix =
-                            getRenderEngine().setupColorTransform(legacySrgbSaturationMatrix);
-                        restore = true;
+                    // switch color matrices lazily
+                    if (layer->isLegacyDataSpace()) {
+                        if (applyLegacyColorMatrix && currentColorMatrix != &legacyColorMatrix) {
+                            getRenderEngine().setupColorTransform(legacyColorMatrix);
+                            currentColorMatrix = &legacyColorMatrix;
+                        }
+                    } else {
+                        if (applyColorMatrix && currentColorMatrix != &colorMatrix) {
+                            getRenderEngine().setupColorTransform(colorMatrix);
+                            currentColorMatrix = &colorMatrix;
+                        }
                     }
+
                     layer->draw(renderArea, clip);
-                    if (restore) {
-                        getRenderEngine().setupColorTransform(savedMatrix);
-                    }
                     break;
                 }
                 default:
@@ -3007,8 +3020,8 @@
         firstLayer = false;
     }
 
-    if (applyColorMatrix) {
-        getRenderEngine().setupColorTransform(oldColorMatrix);
+    if (applyColorMatrix || applyLegacyColorMatrix) {
+        getRenderEngine().setupColorTransform(mat4());
     }
 
     // disable scissor at the end of the frame