blob: 2d46dc0702d295789bedaaa4756b9163c874edd5 [file] [log] [blame]
Lloyd Piquecc01a452018-12-04 17:24:00 -08001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Alec Mourid1bf1b52021-05-05 18:44:58 -070016#include <DisplayHardware/Hal.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080017#include <android-base/stringprintf.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080018#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070019#include <compositionengine/LayerFECompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080020#include <compositionengine/Output.h>
Alec Mourie7cc1c22021-04-27 15:23:26 -070021#include <compositionengine/impl/HwcBufferCache.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080022#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080024#include <compositionengine/impl/OutputLayerCompositionState.h>
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -040025#include <cstdint>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070026#include "system/graphics-base-v1.0.h"
Vishnu Naira9123c82024-10-03 03:56:44 +000027#include "ui/FloatRect.h"
Alec Mouricdf6cbc2021-11-01 17:21:15 -070028
Sally Qif6918d42023-08-07 15:28:30 -070029#include <ui/HdrRenderTypeUtils.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080030
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080031// TODO(b/129481165): remove the #pragma below and fix conversion issues
32#pragma clang diagnostic push
33#pragma clang diagnostic ignored "-Wconversion"
34
Lloyd Pique07e33212018-12-18 16:33:37 -080035#include "DisplayHardware/HWComposer.h"
36
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080037// TODO(b/129481165): remove the #pragma below and fix conversion issues
38#pragma clang diagnostic pop // ignored "-Wconversion"
39
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050040using aidl::android::hardware::graphics::composer3::Composition;
Sally Qi95f669a2024-08-27 11:31:42 -070041using aidl::android::hardware::graphics::composer3::LutProperties;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050042
Lloyd Piquecc01a452018-12-04 17:24:00 -080043namespace android::compositionengine {
44
45OutputLayer::~OutputLayer() = default;
46
47namespace impl {
48
Lloyd Piquea83776c2019-01-29 18:42:32 -080049namespace {
50
51FloatRect reduce(const FloatRect& win, const Region& exclude) {
52 if (CC_LIKELY(exclude.isEmpty())) {
53 return win;
54 }
55 // Convert through Rect (by rounding) for lack of FloatRegion
56 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
57}
58
59} // namespace
60
Lloyd Piquede196652020-01-22 17:29:58 -080061std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
62 const sp<compositionengine::LayerFE>& layerFE) {
63 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080064}
65
Lloyd Piquecc01a452018-12-04 17:24:00 -080066OutputLayer::~OutputLayer() = default;
67
Lloyd Piquedf336d92019-03-07 21:38:42 -080068void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070069 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080070 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070071 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080072 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070073 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080074 }
Lloyd Pique07e33212018-12-18 16:33:37 -080075}
76
Lloyd Piquea83776c2019-01-29 18:42:32 -080077Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080078 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080079
80 // apply the projection's clipping to the window crop in
81 // layerstack space, and convert-back to layer space.
82 // if there are no window scaling involved, this operation will map to full
83 // pixels in the buffer.
84
85 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080086 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080087
Angel Aguayob084e0c2021-08-04 23:27:28 +000088 const Rect& viewport = getOutput().getState().layerStackSpace.getContent();
Lloyd Piquea83776c2019-01-29 18:42:32 -080089 const ui::Transform& layerTransform = layerState.geomLayerTransform;
90 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
91 // Transform to screen space.
92 activeCropFloat = layerTransform.transform(activeCropFloat);
93 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
94 // Back to layer space to work with the content crop.
95 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
96
97 // This needs to be here as transform.transform(Rect) computes the
98 // transformed rect and then takes the bounding box of the result before
99 // returning. This means
100 // transform.inverse().transform(transform.transform(Rect)) != Rect
101 // in which case we need to make sure the final rect is clipped to the
102 // display bounds.
103 Rect activeCrop{activeCropFloat};
104 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
105 activeCrop.clear();
106 }
107 return activeCrop;
108}
109
ramindani2c043be2022-04-19 20:11:10 +0000110FloatRect OutputLayer::calculateOutputSourceCrop(uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800111 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800112
113 if (!layerState.geomUsesSourceCrop) {
114 return {};
115 }
116
117 // the content crop is the area of the content that gets scaled to the
118 // layer's size. This is in buffer space.
119 FloatRect crop = layerState.geomContentCrop.toFloatRect();
120
121 // In addition there is a WM-specified crop we pull from our drawing state.
122 Rect activeCrop = calculateInitialCrop();
123 const Rect& bufferSize = layerState.geomBufferSize;
124
125 int winWidth = bufferSize.getWidth();
126 int winHeight = bufferSize.getHeight();
127
128 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
129 // if display frame hasn't been set and the parent is an unbounded layer.
130 if (winWidth < 0 && winHeight < 0) {
131 return crop;
132 }
133
134 // Transform the window crop to match the buffer coordinate system,
135 // which means using the inverse of the current transform set on the
136 // SurfaceFlingerConsumer.
137 uint32_t invTransform = layerState.geomBufferTransform;
138 if (layerState.geomBufferUsesDisplayInverseTransform) {
139 /*
140 * the code below applies the primary display's inverse transform to the
141 * buffer
142 */
ramindani2c043be2022-04-19 20:11:10 +0000143 uint32_t invTransformOrient = internalDisplayRotationFlags;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800144 // calculate the inverse transform
145 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
146 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
147 }
148 // and apply to the current transform
149 invTransform =
150 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
151 }
152
153 if (invTransform & HAL_TRANSFORM_ROT_90) {
154 // If the activeCrop has been rotate the ends are rotated but not
155 // the space itself so when transforming ends back we can't rely on
156 // a modification of the axes of rotation. To account for this we
157 // need to reorient the inverse rotation in terms of the current
158 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200159 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
160 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
161 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800162 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
163 }
164 std::swap(winWidth, winHeight);
165 }
166 const Rect winCrop =
167 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
168
169 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200170 const float xScale = crop.getWidth() / float(winWidth);
171 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800172
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200173 const float insetLeft = winCrop.left * xScale;
174 const float insetTop = winCrop.top * yScale;
175 const float insetRight = (winWidth - winCrop.right) * xScale;
176 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800177
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200178 crop.left += insetLeft;
179 crop.top += insetTop;
180 crop.right -= insetRight;
181 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800182
183 return crop;
184}
185
186Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800187 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700188 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800189
Vishnu Naira9123c82024-10-03 03:56:44 +0000190 // Convert from layer space to layerStackSpace
Lloyd Piquea83776c2019-01-29 18:42:32 -0800191 // apply the layer's transform, followed by the display's global transform
192 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquea83776c2019-01-29 18:42:32 -0800193 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Vishnu Naira9123c82024-10-03 03:56:44 +0000194 Region activeTransparentRegion = layerTransform.transform(layerState.transparentRegionHint);
195 if (!layerState.geomCrop.isEmpty() && layerState.geomBufferSize.isValid()) {
196 FloatRect activeCrop = layerTransform.transform(layerState.geomCrop);
197 activeCrop = activeCrop.intersect(outputState.layerStackSpace.getContent().toFloatRect());
198 const FloatRect& bufferSize =
199 layerTransform.transform(layerState.geomBufferSize.toFloatRect());
200 activeCrop = activeCrop.intersect(bufferSize);
201
Lloyd Piquea83776c2019-01-29 18:42:32 -0800202 // mark regions outside the crop as transparent
Vishnu Naira9123c82024-10-03 03:56:44 +0000203 Rect topRegion = Rect(layerTransform.transform(
204 FloatRect(0, 0, layerState.geomBufferSize.getWidth(), layerState.geomCrop.top)));
205 Rect bottomRegion = Rect(layerTransform.transform(
206 FloatRect(0, layerState.geomCrop.bottom, layerState.geomBufferSize.getWidth(),
207 layerState.geomBufferSize.getHeight())));
208 Rect leftRegion = Rect(layerTransform.transform(FloatRect(0, layerState.geomCrop.top,
209 layerState.geomCrop.left,
210 layerState.geomCrop.bottom)));
211 Rect rightRegion = Rect(layerTransform.transform(
212 FloatRect(layerState.geomCrop.right, layerState.geomCrop.top,
213 layerState.geomBufferSize.getWidth(), layerState.geomCrop.bottom)));
214
215 activeTransparentRegion.orSelf(topRegion);
216 activeTransparentRegion.orSelf(bottomRegion);
217 activeTransparentRegion.orSelf(leftRegion);
218 activeTransparentRegion.orSelf(rightRegion);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800219 }
220
221 // reduce uses a FloatRect to provide more accuracy during the
222 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400223 FloatRect geomLayerBounds = layerState.geomLayerBounds;
224
225 // Some HWCs may clip client composited input to its displayFrame. Make sure
226 // that this does not cut off the shadow.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000227 if (layerState.forceClientComposition && layerState.shadowSettings.length > 0.0f) {
Alec Mourida128512024-09-28 23:46:58 +0000228 // RenderEngine currently blurs shadows to smooth out edges, so outset by
229 // 2x the length instead of 1x to compensate
230 const auto outset = layerState.shadowSettings.length * 2;
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400231 geomLayerBounds.left -= outset;
232 geomLayerBounds.top -= outset;
233 geomLayerBounds.right += outset;
234 geomLayerBounds.bottom += outset;
235 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800236
Vishnu Naira9123c82024-10-03 03:56:44 +0000237 geomLayerBounds = layerTransform.transform(geomLayerBounds);
238 FloatRect frame = reduce(geomLayerBounds, activeTransparentRegion);
239 frame = frame.intersect(outputState.layerStackSpace.getContent().toFloatRect());
240
241 // convert from layerStackSpace to displaySpace
242 const ui::Transform displayTransform{outputState.transform};
243 return Rect(displayTransform.transform(frame));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800244}
245
Snild Dolkow9e217d62020-04-22 15:53:42 +0200246uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
247 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800248 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700249 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800250
251 /*
252 * Transformations are applied in this order:
253 * 1) buffer orientation/flip/mirror
254 * 2) state transformation (window manager)
255 * 3) layer orientation (screen orientation)
256 * (NOTE: the matrices are multiplied in reverse order)
257 */
258 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700259 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800260 const ui::Transform bufferTransform{layerState.geomBufferTransform};
261 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
262
263 if (layerState.geomBufferUsesDisplayInverseTransform) {
264 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200265 * We must apply the internal display's inverse transform to the buffer
266 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800267 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200268 uint32_t invTransform = internalDisplayRotationFlags;
269
Lloyd Piquea83776c2019-01-29 18:42:32 -0800270 // calculate the inverse transform
271 if (invTransform & HAL_TRANSFORM_ROT_90) {
272 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
273 }
274
275 /*
276 * Here we cancel out the orientation component of the WM transform.
277 * The scaling and translate components are already included in our bounds
278 * computation so it's enough to just omit it in the composition.
279 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
280 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000281 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800282 }
283
284 // this gives us only the "orientation" component of the transform
285 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200286}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800287
Snild Dolkow9e217d62020-04-22 15:53:42 +0200288void OutputLayer::updateCompositionState(
289 bool includeGeometry, bool forceClientComposition,
290 ui::Transform::RotationFlags internalDisplayRotationFlags) {
Lloyd Piquede196652020-01-22 17:29:58 -0800291 const auto* layerFEState = getLayerFE().getCompositionState();
292 if (!layerFEState) {
293 return;
294 }
295
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700296 const auto& outputState = getOutput().getState();
297 const auto& profile = *getOutput().getDisplayColorProfile();
298 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800299
Lloyd Piquea83776c2019-01-29 18:42:32 -0800300 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700301 // Clear the forceClientComposition flag before it is set for any
302 // reason. Note that since it can be set by some checks below when
303 // updating the geometry state, we only clear it when updating the
304 // geometry since those conditions for forcing client composition won't
305 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700306 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700307
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700308 state.displayFrame = calculateOutputDisplayFrame();
ramindani2c043be2022-04-19 20:11:10 +0000309 state.sourceCrop = calculateOutputSourceCrop(internalDisplayRotationFlags);
Snild Dolkow9e217d62020-04-22 15:53:42 +0200310 state.bufferTransform = static_cast<Hwc2::Transform>(
311 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800312
Lloyd Piquede196652020-01-22 17:29:58 -0800313 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700314 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
315 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800316 }
317 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800318
Sally Qif6918d42023-08-07 15:28:30 -0700319 auto pixelFormat = layerFEState->buffer ? std::make_optional(static_cast<ui::PixelFormat>(
320 layerFEState->buffer->getPixelFormat()))
321 : std::nullopt;
322
323 auto hdrRenderType =
324 getHdrRenderType(outputState.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio);
325
Lloyd Piquef5275482019-01-29 18:42:42 -0800326 // Determine the output dependent dataspace for this layer. If it is
327 // colorspace agnostic, it just uses the dataspace chosen for the output to
328 // avoid the need for color conversion.
Alec Mouri88790f32023-07-21 01:25:14 +0000329 // For now, also respect the colorspace agnostic flag if we're drawing to HDR, to avoid drastic
330 // luminance shift. TODO(b/292162273): we should check if that's true though.
Sally Qif6918d42023-08-07 15:28:30 -0700331 state.dataspace = layerFEState->isColorspaceAgnostic && hdrRenderType == HdrRenderType::SDR
Alec Mouri88790f32023-07-21 01:25:14 +0000332 ? outputState.dataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800333 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800334
Alec Mouridda07d92022-04-25 22:39:25 +0000335 // Override the dataspace transfer from 170M to sRGB if the device configuration requests this.
336 // We do this here instead of in buffer info so that dumpsys can still report layers that are
337 // using the 170M transfer. Also we only do this if the colorspace is not agnostic for the
338 // layer, in case the color profile uses a 170M transfer function.
339 if (outputState.treat170mAsSrgb && !layerFEState->isColorspaceAgnostic &&
340 (state.dataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_SMPTE_170M) {
341 state.dataspace = static_cast<ui::Dataspace>(
342 (state.dataspace & HAL_DATASPACE_STANDARD_MASK) |
343 (state.dataspace & HAL_DATASPACE_RANGE_MASK) | HAL_DATASPACE_TRANSFER_SRGB);
344 }
345
Sally Qi8f9ed652023-08-17 15:30:29 -0700346 // re-get HdrRenderType after the dataspace gets changed.
347 hdrRenderType =
348 getHdrRenderType(state.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio);
349
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700350 // For hdr content, treat the white point as the display brightness - HDR content should not be
351 // boosted or dimmed.
Sally Qi81d95e62022-03-21 19:41:33 -0700352 // If the layer explicitly requests to disable dimming, then don't dim either.
Sally Qif6918d42023-08-07 15:28:30 -0700353 if (hdrRenderType == HdrRenderType::GENERIC_HDR ||
Alec Mourie8dd3562022-02-11 14:18:57 -0800354 getOutput().getState().displayBrightnessNits == getOutput().getState().sdrWhitePointNits ||
Sally Qi81d95e62022-03-21 19:41:33 -0700355 getOutput().getState().displayBrightnessNits == 0.f || !layerFEState->dimmingEnabled) {
Alec Mouri6da0e272022-02-07 12:45:57 -0800356 state.dimmingRatio = 1.f;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700357 state.whitePointNits = getOutput().getState().displayBrightnessNits;
358 } else {
John Reck68796592023-01-25 13:47:12 -0500359 float layerBrightnessNits = getOutput().getState().sdrWhitePointNits;
360 // RANGE_EXTENDED can "self-promote" to HDR, but is still rendered for a particular
361 // range that we may need to re-adjust to the current display conditions
Sally Qif6918d42023-08-07 15:28:30 -0700362 if (hdrRenderType == HdrRenderType::DISPLAY_HDR) {
Sally Qi963049b2023-03-23 14:06:21 -0700363 layerBrightnessNits *= layerFEState->currentHdrSdrRatio;
John Reck68796592023-01-25 13:47:12 -0500364 }
365 state.dimmingRatio =
366 std::clamp(layerBrightnessNits / getOutput().getState().displayBrightnessNits, 0.f,
367 1.f);
368 state.whitePointNits = layerBrightnessNits;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700369 }
370
Lloyd Piquef5275482019-01-29 18:42:42 -0800371 // These are evaluated every frame as they can potentially change at any
372 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800373 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700374 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700375 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800376 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800377}
378
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400379void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
380 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700381 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800382 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700383 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800384 return;
385 }
386
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700387 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800388 if (!hwcLayer) {
389 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700390 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800391 return;
392 }
393
Lloyd Piquede196652020-01-22 17:29:58 -0800394 const auto* outputIndependentState = getLayerFE().getCompositionState();
395 if (!outputIndependentState) {
396 return;
397 }
398
399 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800400
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500401 if (requestedCompositionType == Composition::SOLID_COLOR && state.overrideInfo.buffer) {
402 requestedCompositionType = Composition::DEVICE;
Alec Mouri028676a2021-12-02 15:01:48 -0800403 }
404
Yichi Chen413d46a2021-04-07 21:42:09 +0800405 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
406 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400407 const bool isOverridden =
408 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800409 const bool prevOverridden = state.hwc->stateOverridden;
410 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400411 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800412 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
413 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800414 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800415
Lloyd Piquef5275482019-01-29 18:42:42 -0800416 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800417 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
418 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800419
Alec Mourid1bf1b52021-05-05 18:44:58 -0700420 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
421 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700422
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500423 if (requestedCompositionType == Composition::SOLID_COLOR) {
Alec Mouri028676a2021-12-02 15:01:48 -0800424 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
425 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800426
427 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700428 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800429}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800430
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400431void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500432 Composition requestedCompositionType,
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400433 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800434 const auto& outputDependentState = getState();
435
Dan Stoza6166c312021-01-15 16:34:05 -0800436 Rect displayFrame = outputDependentState.displayFrame;
437 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700438
Alec Mouri464352b2021-03-24 16:33:21 -0700439 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800440 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000441 sourceCrop =
442 FloatRect(0.f, 0.f,
443 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
444 ->getWidth()),
445 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
446 ->getHeight()));
Lloyd Piquef5275482019-01-29 18:42:42 -0800447 }
448
Dan Stoza6166c312021-01-15 16:34:05 -0800449 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
450 displayFrame.right, displayFrame.bottom);
451
452 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
453 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
454 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
455 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
456 }
457
458 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800459 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
460 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800461 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
462 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800463 }
464
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400465 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
466 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
467 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800468 }
469
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700470 // Solid-color layers and overridden buffers should always use an identity transform.
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500471 const auto bufferTransform = (requestedCompositionType != Composition::SOLID_COLOR &&
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700472 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800473 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700474 : static_cast<hal::Transform>(0);
475 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
476 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700477 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800478 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
479 static_cast<int32_t>(error));
480 }
481}
482
483void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800484 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
485 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400486 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
487 // PREMULTIPLIED so it will peek through.
488 const auto& overrideInfo = getState().overrideInfo;
489 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700490 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
491 : outputIndependentState.blendMode;
492 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700493 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700494 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800495 }
496
Alec Mouriee69a592021-03-23 15:00:45 -0700497 const float alpha = skipLayer
498 ? 0.0f
499 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800500 ALOGV("Writing alpha %f", alpha);
501
502 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
503 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
504 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800505 }
506
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800507 for (const auto& [name, entry] : outputIndependentState.metadata) {
508 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700509 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800510 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
511 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
512 }
513 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800514}
515
516void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
517 const auto& outputDependentState = getState();
518
Lloyd Piquea2468662019-03-07 21:31:06 -0800519 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800520 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700521 Region visibleRegion = outputDependentState.overrideInfo.buffer
522 ? Region(outputDependentState.overrideInfo.visibleRegion)
523 : outputDependentState.outputSpaceVisibleRegion;
524 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700525 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800526 to_string(error).c_str(), static_cast<int32_t>(error));
Leon Scroggins IIIf2b8ec42022-01-05 13:23:36 -0500527 visibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800528 }
529
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500530 if (auto error =
531 hwcLayer->setBlockingRegion(outputDependentState.outputSpaceBlockingRegionHint);
532 error != hal::Error::NONE) {
533 ALOGE("[%s] Failed to set blocking region: %s (%d)", getLayerFE().getDebugName(),
534 to_string(error).c_str(), static_cast<int32_t>(error));
535 outputDependentState.outputSpaceBlockingRegionHint.dump(LOG_TAG);
536 }
537
Alec Mourib7edfc22021-03-17 16:20:26 -0700538 const auto dataspace = outputDependentState.overrideInfo.buffer
539 ? outputDependentState.overrideInfo.dataspace
540 : outputDependentState.dataspace;
541
542 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
543 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
544 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800545 }
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700546
Alec Mourie8dd3562022-02-11 14:18:57 -0800547 // Cached layers are not dimmed, which means that composer should attempt to dim.
548 // Note that if the dimming ratio is large, then this may cause the cached layer
549 // to kick back into GPU composition :(
550 // Also note that this assumes that there are no HDR layers that are able to be cached.
551 // Otherwise, this could cause HDR layers to be dimmed twice.
552 const auto dimmingRatio = outputDependentState.overrideInfo.buffer
553 ? (getOutput().getState().displayBrightnessNits != 0.f
554 ? std::clamp(getOutput().getState().sdrWhitePointNits /
555 getOutput().getState().displayBrightnessNits,
556 0.f, 1.f)
557 : 1.f)
558 : outputDependentState.dimmingRatio;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700559
Alec Mouri6da0e272022-02-07 12:45:57 -0800560 if (auto error = hwcLayer->setBrightness(dimmingRatio); error != hal::Error::NONE) {
561 ALOGE("[%s] Failed to set brightness %f: %s (%d)", getLayerFE().getDebugName(),
562 dimmingRatio, to_string(error).c_str(), static_cast<int32_t>(error));
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700563 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800564}
565
566void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700567 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500568 Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800569 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700570 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800571 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700572 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800573 editState().forceClientComposition = true;
574 break;
575 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700576 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800577 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800578 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800579
Alec Mouri464352b2021-03-24 16:33:21 -0700580 const Region& surfaceDamage = getState().overrideInfo.buffer
581 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700582 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
583 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700584
585 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700586 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800587 to_string(error).c_str(), static_cast<int32_t>(error));
588 outputIndependentState.surfaceDamage.dump(LOG_TAG);
589 }
590
591 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800592 switch (compositionType) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500593 case Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700594 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800595 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500596 case Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800597 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
598 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500599 case Composition::CURSOR:
600 case Composition::DEVICE:
Leon Scroggins III09c25412021-12-02 14:49:56 -0500601 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800602 case Composition::REFRESH_RATE_INDICATOR:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700603 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800604 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500605 case Composition::INVALID:
606 case Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800607 // Ignored
608 break;
609 }
610}
611
612void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
613 const LayerFECompositionState& outputIndependentState) {
Ady Abraham6e60b142022-01-06 18:10:35 -0800614 aidl::android::hardware::graphics::composer3::Color color = {outputIndependentState.color.r,
615 outputIndependentState.color.g,
616 outputIndependentState.color.b,
617 1.0f};
Lloyd Piquef5275482019-01-29 18:42:42 -0800618
Peiyong Line9d809e2020-04-14 13:10:48 -0700619 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700620 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800621 to_string(error).c_str(), static_cast<int32_t>(error));
622 }
623}
624
625void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
626 const LayerFECompositionState& outputIndependentState) {
627 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700628 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700629 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800630 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
631 static_cast<int32_t>(error));
632 }
633}
634
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700635void OutputLayer::uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700636 auto& state = editState();
637 // Skip doing this if there is no HWC interface
638 if (!state.hwc) {
639 return;
640 }
641
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700642 // Uncache the active buffer last so that it's the first buffer to be purged from the cache
643 // next time a buffer is sent to this layer.
644 bool uncacheActiveBuffer = false;
645
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700646 std::vector<uint32_t> slotsToClear;
647 for (uint64_t bufferId : bufferIdsToUncache) {
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700648 if (bufferId == state.hwc->activeBufferId) {
649 uncacheActiveBuffer = true;
650 } else {
651 uint32_t slot = state.hwc->hwcBufferCache.uncache(bufferId);
652 if (slot != UINT32_MAX) {
653 slotsToClear.push_back(slot);
654 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700655 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700656 }
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700657 if (uncacheActiveBuffer) {
658 slotsToClear.push_back(state.hwc->hwcBufferCache.uncache(state.hwc->activeBufferId));
659 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700660
661 hal::Error error =
662 state.hwc->hwcLayer->setBufferSlotsToClear(slotsToClear, state.hwc->activeBufferSlot);
663 if (error != hal::Error::NONE) {
664 ALOGE("[%s] Failed to clear buffer slots: %s (%d)", getLayerFE().getDebugName(),
665 to_string(error).c_str(), static_cast<int32_t>(error));
666 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700667}
668
Lloyd Piquef5275482019-01-29 18:42:42 -0800669void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700670 const LayerFECompositionState& outputIndependentState,
671 bool skipLayer) {
Huihong Luo5e161962023-08-18 14:26:32 -0700672 if (skipLayer && outputIndependentState.buffer == nullptr) {
673 return;
674 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800675 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700676 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800677 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
678 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700679 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700680 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800681 to_string(error).c_str(), static_cast<int32_t>(error));
682 }
683
Brian Lindahl439afad2022-11-14 11:16:55 -0700684 HwcSlotAndBuffer hwcSlotAndBuffer;
685 sp<Fence> hwcFence;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700686 {
687 // Editing the state only because we update the HWC buffer cache and active buffer.
688 auto& state = editState();
689 // Override buffers use a special cache slot so that they don't evict client buffers.
690 if (state.overrideInfo.buffer != nullptr && !skipLayer) {
691 hwcSlotAndBuffer = state.hwc->hwcBufferCache.getOverrideHwcSlotAndBuffer(
692 state.overrideInfo.buffer->getBuffer());
693 hwcFence = state.overrideInfo.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700694 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
695 // slot will be used first, allowing the memory to be freed as soon as possible.
696 state.hwc->activeBufferId = state.overrideInfo.buffer->getBuffer()->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700697 } else {
698 hwcSlotAndBuffer =
699 state.hwc->hwcBufferCache.getHwcSlotAndBuffer(outputIndependentState.buffer);
700 hwcFence = outputIndependentState.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700701 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
702 // slot will be used first, allowing the memory to be freed as soon as possible.
703 state.hwc->activeBufferId = outputIndependentState.buffer->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700704 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700705 // Keep track of the active buffer slot, so we can restore it after clearing other buffer
706 // slots.
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700707 state.hwc->activeBufferSlot = hwcSlotAndBuffer.slot;
Dan Stoza6166c312021-01-15 16:34:05 -0800708 }
709
Brian Lindahl439afad2022-11-14 11:16:55 -0700710 if (auto error = hwcLayer->setBuffer(hwcSlotAndBuffer.slot, hwcSlotAndBuffer.buffer, hwcFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700711 error != hal::Error::NONE) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700712 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
713 hwcSlotAndBuffer.buffer->handle, to_string(error).c_str(),
714 static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800715 }
716}
717
Peiyong Line9d809e2020-04-14 13:10:48 -0700718void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500719 Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700720 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800721 auto& outputDependentState = editState();
722
Alec Mourid1bf1b52021-05-05 18:44:58 -0700723 if (isClientCompositionForced(isPeekingThrough)) {
724 // If we are forcing client composition, we need to tell the HWC
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500725 requestedCompositionType = Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800726 }
727
728 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700729 // We also resend the composition type when this layer was previously skipped, to ensure that
730 // the composition type is up-to-date.
731 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
732 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800733 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
734
Peiyong Line9d809e2020-04-14 13:10:48 -0700735 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
736 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700737 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500738 to_string(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800739 static_cast<int32_t>(error));
740 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800741 }
742}
743
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800744void OutputLayer::writeCursorPositionToHWC() const {
745 // Skip doing this if there is no HWC interface
746 auto hwcLayer = getHwcLayer();
747 if (!hwcLayer) {
748 return;
749 }
750
Lloyd Piquede196652020-01-22 17:29:58 -0800751 const auto* layerFEState = getLayerFE().getCompositionState();
752 if (!layerFEState) {
753 return;
754 }
755
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700756 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800757
Lloyd Piquede196652020-01-22 17:29:58 -0800758 Rect frame = layerFEState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000759 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800760 Rect position = outputState.transform.transform(frame);
761
762 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700763 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700764 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
765 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
766 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800767 }
768}
769
Lloyd Pique66d68602019-02-13 14:23:31 -0800770HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700771 const auto& state = getState();
772 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800773}
774
775bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700776 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500777 return !state.hwc || state.hwc->hwcCompositionType == Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800778}
779
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800780bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700781 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500782 return state.hwc && state.hwc->hwcCompositionType == Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800783}
784
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500785void OutputLayer::detectDisallowedCompositionTypeChange(Composition from, Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800786 bool result = false;
787 switch (from) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500788 case Composition::INVALID:
789 case Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800790 result = false;
791 break;
792
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500793 case Composition::DEVICE:
794 case Composition::SOLID_COLOR:
795 result = (to == Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800796 break;
797
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500798 case Composition::CURSOR:
799 case Composition::SIDEBAND:
Leon Scroggins7fd536f2021-12-28 14:43:12 +0000800 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800801 case Composition::REFRESH_RATE_INDICATOR:
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500802 result = (to == Composition::CLIENT || to == Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800803 break;
804 }
805
806 if (!result) {
807 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500808 getLayerFE().getDebugName(), to_string(from).c_str(), static_cast<int>(from),
809 to_string(to).c_str(), static_cast<int>(to));
Lloyd Pique66d68602019-02-13 14:23:31 -0800810 }
811}
812
Alec Mourid1bf1b52021-05-05 18:44:58 -0700813bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
814 return getState().forceClientComposition ||
815 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
816}
817
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500818void OutputLayer::applyDeviceCompositionTypeChange(Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700819 auto& state = editState();
820 LOG_FATAL_IF(!state.hwc);
821 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800822
Alec Mourid1bf1b52021-05-05 18:44:58 -0700823 // Only detected disallowed changes if this was not a skip layer, because the
824 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
825 // fewer GPU layers)
826 if (!hwcState.layerSkipped) {
827 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
828 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800829
830 hwcState.hwcCompositionType = compositionType;
831}
832
833void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700834 auto& state = editState();
835 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800836}
837
Peiyong Line9d809e2020-04-14 13:10:48 -0700838void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700839 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800840 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700841 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700842 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800843 break;
844
845 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700846 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800847 toString(request).c_str(), static_cast<int>(request));
848 break;
849 }
850}
851
Sally Qi95f669a2024-08-27 11:31:42 -0700852void OutputLayer::applyDeviceLayerLut(LutProperties /*lutProperties*/,
853 ndk::ScopedFileDescriptor /*lutPfd*/) {
854 // TODO(b/329472856): decode the shared memory of the pfd, and store the lut data into
855 // OutputLayerCompositionState#hwc struct
856}
857
Lloyd Pique688abd42019-02-15 15:42:24 -0800858bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700859 const auto& state = getState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700860 const auto& sourceCrop = state.sourceCrop;
Alec Mourib6d78932023-09-20 16:05:42 +0000861 auto displayFrameWidth = static_cast<float>(state.displayFrame.getWidth());
862 auto displayFrameHeight = static_cast<float>(state.displayFrame.getHeight());
863
864 if (state.bufferTransform & HAL_TRANSFORM_ROT_90) {
865 std::swap(displayFrameWidth, displayFrameHeight);
866 }
867
868 return sourceCrop.getHeight() != displayFrameHeight ||
869 sourceCrop.getWidth() != displayFrameWidth;
Lloyd Pique688abd42019-02-15 15:42:24 -0800870}
871
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000872std::optional<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionSettings() const {
Dan Stoza6166c312021-01-15 16:34:05 -0800873 if (getState().overrideInfo.buffer == nullptr) {
874 return {};
875 }
876
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700877 // Compute the geometry boundaries in layer stack space: we need to transform from the
878 // framebuffer space of the override buffer to layer space.
879 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
880 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000881 const Rect boundaries = transform.transform(getState().overrideInfo.displayFrame);
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700882
Dan Stoza6166c312021-01-15 16:34:05 -0800883 LayerFE::LayerSettings settings;
884 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700885 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -0800886 };
Alec Mouria90a5702021-04-16 16:36:21 +0000887 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700888 settings.source = renderengine::PixelSource{
889 .buffer = renderengine::Buffer{
890 .buffer = getState().overrideInfo.buffer,
891 .fence = getState().overrideInfo.acquireFence,
892 // If the transform from layer space to display space contains a rotation, we
893 // need to undo the rotation in the texture transform
894 .textureTransform =
895 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
896 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -0800897 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -0800898 settings.alpha = 1.0f;
Alec Mourie8dd3562022-02-11 14:18:57 -0800899 settings.whitePointNits = getOutput().getState().sdrWhitePointNits;
Dan Stoza6166c312021-01-15 16:34:05 -0800900
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000901 return settings;
Dan Stoza6166c312021-01-15 16:34:05 -0800902}
903
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800904void OutputLayer::dump(std::string& out) const {
905 using android::base::StringAppendF;
906
Lloyd Piquede196652020-01-22 17:29:58 -0800907 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700908 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800909}
910
Lloyd Piquecc01a452018-12-04 17:24:00 -0800911} // namespace impl
912} // namespace android::compositionengine