SF: Use CompositionInfo to program HWComposer

Test: Build and run manually
Merged-Id: I3a4fd676781d0b7db1665430e0d84a6bc9b83f60
Change-Id: I3a4fd676781d0b7db1665430e0d84a6bc9b83f60
diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp
index ba2d0a0..b072213 100644
--- a/services/surfaceflinger/Layer.cpp
+++ b/services/surfaceflinger/Layer.cpp
@@ -490,8 +490,6 @@
         hwcInfo.forceClientComposition = true;
     }
 
-    auto& hwcLayer = hwcInfo.layer;
-
     // this gives us only the "orientation" component of the transform
     const State& s(getDrawingState());
     auto blendMode = HWC2::BlendMode::None;
@@ -499,12 +497,7 @@
         blendMode =
                 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
     }
-    auto error = (*hwcLayer)->setBlendMode(blendMode);
-    ALOGE_IF(error != HWC2::Error::None,
-             "[%s] Failed to set blend mode %s:"
-             " %s (%d)",
-             mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
-             static_cast<int32_t>(error));
+    getBE().compositionInfo.hwc.blendMode = blendMode;
 
     // apply the layer's transform, followed by the display's global transform
     // here we're guaranteed that the layer's transform preserves rects
@@ -547,36 +540,15 @@
     }
     const Transform& tr(displayDevice->getTransform());
     Rect transformedFrame = tr.transform(frame);
-    error = (*hwcLayer)->setDisplayFrame(transformedFrame);
-    if (error != HWC2::Error::None) {
-        ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
-              transformedFrame.left, transformedFrame.top, transformedFrame.right,
-              transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
-    } else {
-        hwcInfo.displayFrame = transformedFrame;
-    }
+    getBE().compositionInfo.hwc.displayFrame = transformedFrame;
 
     FloatRect sourceCrop = computeCrop(displayDevice);
-    error = (*hwcLayer)->setSourceCrop(sourceCrop);
-    if (error != HWC2::Error::None) {
-        ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
-              "%s (%d)",
-              mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
-              to_string(error).c_str(), static_cast<int32_t>(error));
-    } else {
-        hwcInfo.sourceCrop = sourceCrop;
-    }
+    getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
 
     float alpha = static_cast<float>(getAlpha());
-    error = (*hwcLayer)->setPlaneAlpha(alpha);
-    ALOGE_IF(error != HWC2::Error::None,
-             "[%s] Failed to set plane alpha %.3f: "
-             "%s (%d)",
-             mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
+    getBE().compositionInfo.hwc.alpha = alpha;
 
-    error = (*hwcLayer)->setZOrder(z);
-    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
-             to_string(error).c_str(), static_cast<int32_t>(error));
+    getBE().compositionInfo.hwc.z = z;
 
     int type = s.type;
     int appId = s.appId;
@@ -587,9 +559,8 @@
         appId = parentState.appId;
     }
 
-    error = (*hwcLayer)->setInfo(type, appId);
-    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
-             static_cast<int32_t>(error));
+    getBE().compositionInfo.hwc.type = type;
+    getBE().compositionInfo.hwc.appId = appId;
 
     /*
      * Transformations are applied in this order:
@@ -636,12 +607,109 @@
     } else {
         auto transform = static_cast<HWC2::Transform>(orientation);
         hwcInfo.transform = transform;
-        auto error = (*hwcLayer)->setTransform(transform);
+        getBE().compositionInfo.hwc.transform = transform;
+    }
+}
+
+void Layer::configureHwcLayer(const sp<const DisplayDevice>& displayDevice) {
+    ATRACE_CALL();
+
+    auto hwcId = displayDevice->getHwcDisplayId();
+    auto& hwcInfo = getBE().mHwcLayers[hwcId];
+    auto& hwcLayer = hwcInfo.layer;
+
+    auto error = (*hwcLayer)->setBlendMode(getBE().compositionInfo.hwc.blendMode);
+    ALOGE_IF(error != HWC2::Error::None,
+             "[%s] Failed to set blend mode %s:"
+             " %s (%d)",
+             mName.string(),
+             to_string(getBE().compositionInfo.hwc.blendMode).c_str(), to_string(error).c_str(),
+             static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setDisplayFrame(getBE().compositionInfo.hwc.displayFrame);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set the display frame [%d, %d, %d, %d] %s (%d)",
+            mName.string(),
+            getBE().compositionInfo.hwc.displayFrame.left,
+            getBE().compositionInfo.hwc.displayFrame.right,
+            getBE().compositionInfo.hwc.displayFrame.top,
+            getBE().compositionInfo.hwc.displayFrame.bottom,
+            to_string(error).c_str(), static_cast<int32_t>(error));
+
+
+    error = (*hwcLayer)->setPlaneAlpha(getBE().compositionInfo.hwc.alpha);
+    ALOGE_IF(error != HWC2::Error::None,
+             "[%s] Failed to set plane alpha %.3f: "
+             "%s (%d)",
+             mName.string(), getBE().compositionInfo.hwc.alpha,
+             to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setSourceCrop(getBE().compositionInfo.hwc.sourceCrop);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: %s (%d)",
+            mName.string(),
+            getBE().compositionInfo.hwc.sourceCrop.left,
+            getBE().compositionInfo.hwc.sourceCrop.right,
+            getBE().compositionInfo.hwc.sourceCrop.top,
+            getBE().compositionInfo.hwc.sourceCrop.bottom,
+            to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setTransform(getBE().compositionInfo.hwc.transform);
+    ALOGE_IF(error != HWC2::Error::None,
+             "[%s] Failed to set transform %s: "
+             "%s (%d)",
+             mName.string(),
+             to_string(getBE().compositionInfo.hwc.transform).c_str(), to_string(error).c_str(),
+             static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setZOrder(getBE().compositionInfo.hwc.z);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set Z %u: %s (%d)",
+            mName.string(), getBE().compositionInfo.hwc.z,
+            to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setInfo(getBE().compositionInfo.hwc.type, getBE().compositionInfo.hwc.appId);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set info (%d)",
+            mName.string(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setVisibleRegion(getBE().compositionInfo.hwc.visibleRegion);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set visible region: %s (%d)",
+            mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setSurfaceDamage(getBE().compositionInfo.hwc.surfaceDamage);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set surface damage: %s (%d)",
+            mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setCompositionType(getBE().compositionInfo.compositionType);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set composition type: %s (%d)",
+            mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setDataspace(getBE().compositionInfo.hwc.dataspace);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set dataspace: %s (%d)",
+            mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
+
+    error = (*hwcLayer)->setHdrMetadata(getBE().compositionInfo.hwc.hdrMetadata);
+    if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
+        ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(),
+              to_string(error).c_str(), static_cast<int32_t>(error));
+    }
+
+    error = (*hwcLayer)->setColor(getBE().compositionInfo.hwc.color);
+    ALOGE_IF(error != HWC2::Error::None,
+            "[%s] Failed to set color: %s (%d)",
+            mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
+
+    if (getBE().compositionInfo.hwc.fence) {
+        error = (*hwcLayer)->setBuffer(getBE().compositionInfo.mBufferSlot,
+                getBE().compositionInfo.mBuffer, getBE().compositionInfo.hwc.fence);
         ALOGE_IF(error != HWC2::Error::None,
-                 "[%s] Failed to set transform %s: "
-                 "%s (%d)",
-                 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
-                 static_cast<int32_t>(error));
+                "[%s] Failed to set buffer: %s (%d)",
+                mName.string(), to_string(error).c_str(), static_cast<int32_t>(error));
     }
 }
 
@@ -1488,12 +1556,11 @@
     result.appendFormat(" %s\n", name.string());
 
     const Layer::State& layerState(getDrawingState());
-    const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
     result.appendFormat("  %10d | ", layerState.z);
     result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
-    const Rect& frame = hwcInfo.displayFrame;
+    const Rect& frame = getBE().compositionInfo.hwc.displayFrame;
     result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
-    const FloatRect& crop = hwcInfo.sourceCrop;
+    const FloatRect& crop = getBE().compositionInfo.hwc.sourceCrop;
     result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right, crop.bottom);
 
     result.append("- - - - - - - - - - - - - - - - - - - - ");