blob: 9b05084242a9a38d84ec31e7962130fe79ab2440 [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"
27
Sally Qif6918d42023-08-07 15:28:30 -070028#include <ui/HdrRenderTypeUtils.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080029
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080030// TODO(b/129481165): remove the #pragma below and fix conversion issues
31#pragma clang diagnostic push
32#pragma clang diagnostic ignored "-Wconversion"
33
Lloyd Pique07e33212018-12-18 16:33:37 -080034#include "DisplayHardware/HWComposer.h"
35
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080036// TODO(b/129481165): remove the #pragma below and fix conversion issues
37#pragma clang diagnostic pop // ignored "-Wconversion"
38
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050039using aidl::android::hardware::graphics::composer3::Composition;
40
Lloyd Piquecc01a452018-12-04 17:24:00 -080041namespace android::compositionengine {
42
43OutputLayer::~OutputLayer() = default;
44
45namespace impl {
46
Lloyd Piquea83776c2019-01-29 18:42:32 -080047namespace {
48
49FloatRect reduce(const FloatRect& win, const Region& exclude) {
50 if (CC_LIKELY(exclude.isEmpty())) {
51 return win;
52 }
53 // Convert through Rect (by rounding) for lack of FloatRegion
54 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
55}
56
57} // namespace
58
Lloyd Piquede196652020-01-22 17:29:58 -080059std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
60 const sp<compositionengine::LayerFE>& layerFE) {
61 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080062}
63
Lloyd Piquecc01a452018-12-04 17:24:00 -080064OutputLayer::~OutputLayer() = default;
65
Lloyd Piquedf336d92019-03-07 21:38:42 -080066void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070067 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080068 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070069 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080070 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070071 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080072 }
Lloyd Pique07e33212018-12-18 16:33:37 -080073}
74
Lloyd Piquea83776c2019-01-29 18:42:32 -080075Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080076 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080077
78 // apply the projection's clipping to the window crop in
79 // layerstack space, and convert-back to layer space.
80 // if there are no window scaling involved, this operation will map to full
81 // pixels in the buffer.
82
83 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080084 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080085
Angel Aguayob084e0c2021-08-04 23:27:28 +000086 const Rect& viewport = getOutput().getState().layerStackSpace.getContent();
Lloyd Piquea83776c2019-01-29 18:42:32 -080087 const ui::Transform& layerTransform = layerState.geomLayerTransform;
88 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
89 // Transform to screen space.
90 activeCropFloat = layerTransform.transform(activeCropFloat);
91 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
92 // Back to layer space to work with the content crop.
93 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
94
95 // This needs to be here as transform.transform(Rect) computes the
96 // transformed rect and then takes the bounding box of the result before
97 // returning. This means
98 // transform.inverse().transform(transform.transform(Rect)) != Rect
99 // in which case we need to make sure the final rect is clipped to the
100 // display bounds.
101 Rect activeCrop{activeCropFloat};
102 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
103 activeCrop.clear();
104 }
105 return activeCrop;
106}
107
ramindani2c043be2022-04-19 20:11:10 +0000108FloatRect OutputLayer::calculateOutputSourceCrop(uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800109 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800110
111 if (!layerState.geomUsesSourceCrop) {
112 return {};
113 }
114
115 // the content crop is the area of the content that gets scaled to the
116 // layer's size. This is in buffer space.
117 FloatRect crop = layerState.geomContentCrop.toFloatRect();
118
119 // In addition there is a WM-specified crop we pull from our drawing state.
120 Rect activeCrop = calculateInitialCrop();
121 const Rect& bufferSize = layerState.geomBufferSize;
122
123 int winWidth = bufferSize.getWidth();
124 int winHeight = bufferSize.getHeight();
125
126 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
127 // if display frame hasn't been set and the parent is an unbounded layer.
128 if (winWidth < 0 && winHeight < 0) {
129 return crop;
130 }
131
132 // Transform the window crop to match the buffer coordinate system,
133 // which means using the inverse of the current transform set on the
134 // SurfaceFlingerConsumer.
135 uint32_t invTransform = layerState.geomBufferTransform;
136 if (layerState.geomBufferUsesDisplayInverseTransform) {
137 /*
138 * the code below applies the primary display's inverse transform to the
139 * buffer
140 */
ramindani2c043be2022-04-19 20:11:10 +0000141 uint32_t invTransformOrient = internalDisplayRotationFlags;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800142 // calculate the inverse transform
143 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
144 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
145 }
146 // and apply to the current transform
147 invTransform =
148 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
149 }
150
151 if (invTransform & HAL_TRANSFORM_ROT_90) {
152 // If the activeCrop has been rotate the ends are rotated but not
153 // the space itself so when transforming ends back we can't rely on
154 // a modification of the axes of rotation. To account for this we
155 // need to reorient the inverse rotation in terms of the current
156 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200157 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
158 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
159 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800160 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
161 }
162 std::swap(winWidth, winHeight);
163 }
164 const Rect winCrop =
165 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
166
167 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200168 const float xScale = crop.getWidth() / float(winWidth);
169 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800170
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200171 const float insetLeft = winCrop.left * xScale;
172 const float insetTop = winCrop.top * yScale;
173 const float insetRight = (winWidth - winCrop.right) * xScale;
174 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800175
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200176 crop.left += insetLeft;
177 crop.top += insetTop;
178 crop.right -= insetRight;
179 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800180
181 return crop;
182}
183
184Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800185 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700186 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800187
188 // apply the layer's transform, followed by the display's global transform
189 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800190 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800191 const ui::Transform& layerTransform = layerState.geomLayerTransform;
192 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
193 const Rect& bufferSize = layerState.geomBufferSize;
194 Rect activeCrop = layerState.geomCrop;
195 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
196 activeCrop = layerTransform.transform(activeCrop);
Angel Aguayob084e0c2021-08-04 23:27:28 +0000197 if (!activeCrop.intersect(outputState.layerStackSpace.getContent(), &activeCrop)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800198 activeCrop.clear();
199 }
200 activeCrop = inverseLayerTransform.transform(activeCrop, true);
201 // This needs to be here as transform.transform(Rect) computes the
202 // transformed rect and then takes the bounding box of the result before
203 // returning. This means
204 // transform.inverse().transform(transform.transform(Rect)) != Rect
205 // in which case we need to make sure the final rect is clipped to the
206 // display bounds.
207 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
208 activeCrop.clear();
209 }
210 // mark regions outside the crop as transparent
211 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
212 activeTransparentRegion.orSelf(
213 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
214 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
215 activeTransparentRegion.orSelf(
216 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
217 }
218
219 // reduce uses a FloatRect to provide more accuracy during the
220 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400221 FloatRect geomLayerBounds = layerState.geomLayerBounds;
222
223 // Some HWCs may clip client composited input to its displayFrame. Make sure
224 // that this does not cut off the shadow.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000225 if (layerState.forceClientComposition && layerState.shadowSettings.length > 0.0f) {
Alec Mourida128512024-09-28 23:46:58 +0000226 // RenderEngine currently blurs shadows to smooth out edges, so outset by
227 // 2x the length instead of 1x to compensate
228 const auto outset = layerState.shadowSettings.length * 2;
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400229 geomLayerBounds.left -= outset;
230 geomLayerBounds.top -= outset;
231 geomLayerBounds.right += outset;
232 geomLayerBounds.bottom += outset;
233 }
234 Rect frame{layerTransform.transform(reduce(geomLayerBounds, activeTransparentRegion))};
Angel Aguayob084e0c2021-08-04 23:27:28 +0000235 if (!frame.intersect(outputState.layerStackSpace.getContent(), &frame)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800236 frame.clear();
237 }
238 const ui::Transform displayTransform{outputState.transform};
239
240 return displayTransform.transform(frame);
241}
242
Snild Dolkow9e217d62020-04-22 15:53:42 +0200243uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
244 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800245 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700246 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800247
248 /*
249 * Transformations are applied in this order:
250 * 1) buffer orientation/flip/mirror
251 * 2) state transformation (window manager)
252 * 3) layer orientation (screen orientation)
253 * (NOTE: the matrices are multiplied in reverse order)
254 */
255 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700256 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800257 const ui::Transform bufferTransform{layerState.geomBufferTransform};
258 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
259
260 if (layerState.geomBufferUsesDisplayInverseTransform) {
261 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200262 * We must apply the internal display's inverse transform to the buffer
263 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800264 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200265 uint32_t invTransform = internalDisplayRotationFlags;
266
Lloyd Piquea83776c2019-01-29 18:42:32 -0800267 // calculate the inverse transform
268 if (invTransform & HAL_TRANSFORM_ROT_90) {
269 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
270 }
271
272 /*
273 * Here we cancel out the orientation component of the WM transform.
274 * The scaling and translate components are already included in our bounds
275 * computation so it's enough to just omit it in the composition.
276 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
277 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000278 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800279 }
280
281 // this gives us only the "orientation" component of the transform
282 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200283}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800284
Snild Dolkow9e217d62020-04-22 15:53:42 +0200285void OutputLayer::updateCompositionState(
286 bool includeGeometry, bool forceClientComposition,
287 ui::Transform::RotationFlags internalDisplayRotationFlags) {
Lloyd Piquede196652020-01-22 17:29:58 -0800288 const auto* layerFEState = getLayerFE().getCompositionState();
289 if (!layerFEState) {
290 return;
291 }
292
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700293 const auto& outputState = getOutput().getState();
294 const auto& profile = *getOutput().getDisplayColorProfile();
295 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800296
Lloyd Piquea83776c2019-01-29 18:42:32 -0800297 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700298 // Clear the forceClientComposition flag before it is set for any
299 // reason. Note that since it can be set by some checks below when
300 // updating the geometry state, we only clear it when updating the
301 // geometry since those conditions for forcing client composition won't
302 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700303 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700304
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700305 state.displayFrame = calculateOutputDisplayFrame();
ramindani2c043be2022-04-19 20:11:10 +0000306 state.sourceCrop = calculateOutputSourceCrop(internalDisplayRotationFlags);
Snild Dolkow9e217d62020-04-22 15:53:42 +0200307 state.bufferTransform = static_cast<Hwc2::Transform>(
308 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800309
Lloyd Piquede196652020-01-22 17:29:58 -0800310 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700311 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
312 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800313 }
314 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800315
Sally Qif6918d42023-08-07 15:28:30 -0700316 auto pixelFormat = layerFEState->buffer ? std::make_optional(static_cast<ui::PixelFormat>(
317 layerFEState->buffer->getPixelFormat()))
318 : std::nullopt;
319
320 auto hdrRenderType =
321 getHdrRenderType(outputState.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio);
322
Lloyd Piquef5275482019-01-29 18:42:42 -0800323 // Determine the output dependent dataspace for this layer. If it is
324 // colorspace agnostic, it just uses the dataspace chosen for the output to
325 // avoid the need for color conversion.
Alec Mouri88790f32023-07-21 01:25:14 +0000326 // For now, also respect the colorspace agnostic flag if we're drawing to HDR, to avoid drastic
327 // luminance shift. TODO(b/292162273): we should check if that's true though.
Sally Qif6918d42023-08-07 15:28:30 -0700328 state.dataspace = layerFEState->isColorspaceAgnostic && hdrRenderType == HdrRenderType::SDR
Alec Mouri88790f32023-07-21 01:25:14 +0000329 ? outputState.dataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800330 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800331
Alec Mouridda07d92022-04-25 22:39:25 +0000332 // Override the dataspace transfer from 170M to sRGB if the device configuration requests this.
333 // We do this here instead of in buffer info so that dumpsys can still report layers that are
334 // using the 170M transfer. Also we only do this if the colorspace is not agnostic for the
335 // layer, in case the color profile uses a 170M transfer function.
336 if (outputState.treat170mAsSrgb && !layerFEState->isColorspaceAgnostic &&
337 (state.dataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_SMPTE_170M) {
338 state.dataspace = static_cast<ui::Dataspace>(
339 (state.dataspace & HAL_DATASPACE_STANDARD_MASK) |
340 (state.dataspace & HAL_DATASPACE_RANGE_MASK) | HAL_DATASPACE_TRANSFER_SRGB);
341 }
342
Sally Qi8f9ed652023-08-17 15:30:29 -0700343 // re-get HdrRenderType after the dataspace gets changed.
344 hdrRenderType =
345 getHdrRenderType(state.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio);
346
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700347 // For hdr content, treat the white point as the display brightness - HDR content should not be
348 // boosted or dimmed.
Sally Qi81d95e62022-03-21 19:41:33 -0700349 // If the layer explicitly requests to disable dimming, then don't dim either.
Sally Qif6918d42023-08-07 15:28:30 -0700350 if (hdrRenderType == HdrRenderType::GENERIC_HDR ||
Alec Mourie8dd3562022-02-11 14:18:57 -0800351 getOutput().getState().displayBrightnessNits == getOutput().getState().sdrWhitePointNits ||
Sally Qi81d95e62022-03-21 19:41:33 -0700352 getOutput().getState().displayBrightnessNits == 0.f || !layerFEState->dimmingEnabled) {
Alec Mouri6da0e272022-02-07 12:45:57 -0800353 state.dimmingRatio = 1.f;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700354 state.whitePointNits = getOutput().getState().displayBrightnessNits;
355 } else {
John Reck68796592023-01-25 13:47:12 -0500356 float layerBrightnessNits = getOutput().getState().sdrWhitePointNits;
357 // RANGE_EXTENDED can "self-promote" to HDR, but is still rendered for a particular
358 // range that we may need to re-adjust to the current display conditions
Sally Qif6918d42023-08-07 15:28:30 -0700359 if (hdrRenderType == HdrRenderType::DISPLAY_HDR) {
Sally Qi963049b2023-03-23 14:06:21 -0700360 layerBrightnessNits *= layerFEState->currentHdrSdrRatio;
John Reck68796592023-01-25 13:47:12 -0500361 }
362 state.dimmingRatio =
363 std::clamp(layerBrightnessNits / getOutput().getState().displayBrightnessNits, 0.f,
364 1.f);
365 state.whitePointNits = layerBrightnessNits;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700366 }
367
Lloyd Piquef5275482019-01-29 18:42:42 -0800368 // These are evaluated every frame as they can potentially change at any
369 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800370 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700371 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700372 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800373 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800374}
375
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400376void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
377 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700378 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800379 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700380 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800381 return;
382 }
383
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700384 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800385 if (!hwcLayer) {
386 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700387 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800388 return;
389 }
390
Lloyd Piquede196652020-01-22 17:29:58 -0800391 const auto* outputIndependentState = getLayerFE().getCompositionState();
392 if (!outputIndependentState) {
393 return;
394 }
395
396 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800397
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500398 if (requestedCompositionType == Composition::SOLID_COLOR && state.overrideInfo.buffer) {
399 requestedCompositionType = Composition::DEVICE;
Alec Mouri028676a2021-12-02 15:01:48 -0800400 }
401
Yichi Chen413d46a2021-04-07 21:42:09 +0800402 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
403 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400404 const bool isOverridden =
405 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800406 const bool prevOverridden = state.hwc->stateOverridden;
407 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400408 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800409 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
410 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800411 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800412
Lloyd Piquef5275482019-01-29 18:42:42 -0800413 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800414 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
415 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800416
Alec Mourid1bf1b52021-05-05 18:44:58 -0700417 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
418 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700419
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500420 if (requestedCompositionType == Composition::SOLID_COLOR) {
Alec Mouri028676a2021-12-02 15:01:48 -0800421 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
422 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800423
424 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700425 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800426}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800427
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400428void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500429 Composition requestedCompositionType,
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400430 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800431 const auto& outputDependentState = getState();
432
Dan Stoza6166c312021-01-15 16:34:05 -0800433 Rect displayFrame = outputDependentState.displayFrame;
434 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700435
Alec Mouri464352b2021-03-24 16:33:21 -0700436 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800437 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000438 sourceCrop =
439 FloatRect(0.f, 0.f,
440 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
441 ->getWidth()),
442 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
443 ->getHeight()));
Lloyd Piquef5275482019-01-29 18:42:42 -0800444 }
445
Dan Stoza6166c312021-01-15 16:34:05 -0800446 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
447 displayFrame.right, displayFrame.bottom);
448
449 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
450 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
451 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
452 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
453 }
454
455 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800456 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
457 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800458 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
459 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800460 }
461
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400462 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
463 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
464 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800465 }
466
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700467 // Solid-color layers and overridden buffers should always use an identity transform.
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500468 const auto bufferTransform = (requestedCompositionType != Composition::SOLID_COLOR &&
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700469 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800470 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700471 : static_cast<hal::Transform>(0);
472 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
473 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700474 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800475 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
476 static_cast<int32_t>(error));
477 }
478}
479
480void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800481 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
482 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400483 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
484 // PREMULTIPLIED so it will peek through.
485 const auto& overrideInfo = getState().overrideInfo;
486 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700487 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
488 : outputIndependentState.blendMode;
489 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700490 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700491 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 }
493
Alec Mouriee69a592021-03-23 15:00:45 -0700494 const float alpha = skipLayer
495 ? 0.0f
496 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800497 ALOGV("Writing alpha %f", alpha);
498
499 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
500 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
501 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800502 }
503
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800504 for (const auto& [name, entry] : outputIndependentState.metadata) {
505 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700506 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800507 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
508 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
509 }
510 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800511}
512
513void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
514 const auto& outputDependentState = getState();
515
Lloyd Piquea2468662019-03-07 21:31:06 -0800516 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800517 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700518 Region visibleRegion = outputDependentState.overrideInfo.buffer
519 ? Region(outputDependentState.overrideInfo.visibleRegion)
520 : outputDependentState.outputSpaceVisibleRegion;
521 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700522 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800523 to_string(error).c_str(), static_cast<int32_t>(error));
Leon Scroggins IIIf2b8ec42022-01-05 13:23:36 -0500524 visibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800525 }
526
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500527 if (auto error =
528 hwcLayer->setBlockingRegion(outputDependentState.outputSpaceBlockingRegionHint);
529 error != hal::Error::NONE) {
530 ALOGE("[%s] Failed to set blocking region: %s (%d)", getLayerFE().getDebugName(),
531 to_string(error).c_str(), static_cast<int32_t>(error));
532 outputDependentState.outputSpaceBlockingRegionHint.dump(LOG_TAG);
533 }
534
Alec Mourib7edfc22021-03-17 16:20:26 -0700535 const auto dataspace = outputDependentState.overrideInfo.buffer
536 ? outputDependentState.overrideInfo.dataspace
537 : outputDependentState.dataspace;
538
539 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
540 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
541 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800542 }
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700543
Alec Mourie8dd3562022-02-11 14:18:57 -0800544 // Cached layers are not dimmed, which means that composer should attempt to dim.
545 // Note that if the dimming ratio is large, then this may cause the cached layer
546 // to kick back into GPU composition :(
547 // Also note that this assumes that there are no HDR layers that are able to be cached.
548 // Otherwise, this could cause HDR layers to be dimmed twice.
549 const auto dimmingRatio = outputDependentState.overrideInfo.buffer
550 ? (getOutput().getState().displayBrightnessNits != 0.f
551 ? std::clamp(getOutput().getState().sdrWhitePointNits /
552 getOutput().getState().displayBrightnessNits,
553 0.f, 1.f)
554 : 1.f)
555 : outputDependentState.dimmingRatio;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700556
Alec Mouri6da0e272022-02-07 12:45:57 -0800557 if (auto error = hwcLayer->setBrightness(dimmingRatio); error != hal::Error::NONE) {
558 ALOGE("[%s] Failed to set brightness %f: %s (%d)", getLayerFE().getDebugName(),
559 dimmingRatio, to_string(error).c_str(), static_cast<int32_t>(error));
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700560 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800561}
562
563void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700564 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500565 Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800566 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700567 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800568 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700569 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800570 editState().forceClientComposition = true;
571 break;
572 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700573 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800574 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800575 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800576
Alec Mouri464352b2021-03-24 16:33:21 -0700577 const Region& surfaceDamage = getState().overrideInfo.buffer
578 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700579 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
580 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700581
582 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700583 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800584 to_string(error).c_str(), static_cast<int32_t>(error));
585 outputIndependentState.surfaceDamage.dump(LOG_TAG);
586 }
587
588 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800589 switch (compositionType) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500590 case Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700591 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800592 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500593 case Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800594 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
595 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500596 case Composition::CURSOR:
597 case Composition::DEVICE:
Leon Scroggins III09c25412021-12-02 14:49:56 -0500598 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800599 case Composition::REFRESH_RATE_INDICATOR:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700600 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800601 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500602 case Composition::INVALID:
603 case Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800604 // Ignored
605 break;
606 }
607}
608
609void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
610 const LayerFECompositionState& outputIndependentState) {
Ady Abraham6e60b142022-01-06 18:10:35 -0800611 aidl::android::hardware::graphics::composer3::Color color = {outputIndependentState.color.r,
612 outputIndependentState.color.g,
613 outputIndependentState.color.b,
614 1.0f};
Lloyd Piquef5275482019-01-29 18:42:42 -0800615
Peiyong Line9d809e2020-04-14 13:10:48 -0700616 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700617 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800618 to_string(error).c_str(), static_cast<int32_t>(error));
619 }
620}
621
622void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
623 const LayerFECompositionState& outputIndependentState) {
624 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700625 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700626 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800627 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
628 static_cast<int32_t>(error));
629 }
630}
631
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700632void OutputLayer::uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700633 auto& state = editState();
634 // Skip doing this if there is no HWC interface
635 if (!state.hwc) {
636 return;
637 }
638
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700639 // Uncache the active buffer last so that it's the first buffer to be purged from the cache
640 // next time a buffer is sent to this layer.
641 bool uncacheActiveBuffer = false;
642
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700643 std::vector<uint32_t> slotsToClear;
644 for (uint64_t bufferId : bufferIdsToUncache) {
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700645 if (bufferId == state.hwc->activeBufferId) {
646 uncacheActiveBuffer = true;
647 } else {
648 uint32_t slot = state.hwc->hwcBufferCache.uncache(bufferId);
649 if (slot != UINT32_MAX) {
650 slotsToClear.push_back(slot);
651 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700652 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700653 }
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700654 if (uncacheActiveBuffer) {
655 slotsToClear.push_back(state.hwc->hwcBufferCache.uncache(state.hwc->activeBufferId));
656 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700657
658 hal::Error error =
659 state.hwc->hwcLayer->setBufferSlotsToClear(slotsToClear, state.hwc->activeBufferSlot);
660 if (error != hal::Error::NONE) {
661 ALOGE("[%s] Failed to clear buffer slots: %s (%d)", getLayerFE().getDebugName(),
662 to_string(error).c_str(), static_cast<int32_t>(error));
663 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700664}
665
Lloyd Piquef5275482019-01-29 18:42:42 -0800666void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700667 const LayerFECompositionState& outputIndependentState,
668 bool skipLayer) {
Huihong Luo5e161962023-08-18 14:26:32 -0700669 if (skipLayer && outputIndependentState.buffer == nullptr) {
670 return;
671 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800672 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700673 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800674 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
675 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700676 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700677 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800678 to_string(error).c_str(), static_cast<int32_t>(error));
679 }
680
Brian Lindahl439afad2022-11-14 11:16:55 -0700681 HwcSlotAndBuffer hwcSlotAndBuffer;
682 sp<Fence> hwcFence;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700683 {
684 // Editing the state only because we update the HWC buffer cache and active buffer.
685 auto& state = editState();
686 // Override buffers use a special cache slot so that they don't evict client buffers.
687 if (state.overrideInfo.buffer != nullptr && !skipLayer) {
688 hwcSlotAndBuffer = state.hwc->hwcBufferCache.getOverrideHwcSlotAndBuffer(
689 state.overrideInfo.buffer->getBuffer());
690 hwcFence = state.overrideInfo.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700691 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
692 // slot will be used first, allowing the memory to be freed as soon as possible.
693 state.hwc->activeBufferId = state.overrideInfo.buffer->getBuffer()->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700694 } else {
695 hwcSlotAndBuffer =
696 state.hwc->hwcBufferCache.getHwcSlotAndBuffer(outputIndependentState.buffer);
697 hwcFence = outputIndependentState.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700698 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
699 // slot will be used first, allowing the memory to be freed as soon as possible.
700 state.hwc->activeBufferId = outputIndependentState.buffer->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700701 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700702 // Keep track of the active buffer slot, so we can restore it after clearing other buffer
703 // slots.
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700704 state.hwc->activeBufferSlot = hwcSlotAndBuffer.slot;
Dan Stoza6166c312021-01-15 16:34:05 -0800705 }
706
Brian Lindahl439afad2022-11-14 11:16:55 -0700707 if (auto error = hwcLayer->setBuffer(hwcSlotAndBuffer.slot, hwcSlotAndBuffer.buffer, hwcFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700708 error != hal::Error::NONE) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700709 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
710 hwcSlotAndBuffer.buffer->handle, to_string(error).c_str(),
711 static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800712 }
713}
714
Peiyong Line9d809e2020-04-14 13:10:48 -0700715void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500716 Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700717 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800718 auto& outputDependentState = editState();
719
Alec Mourid1bf1b52021-05-05 18:44:58 -0700720 if (isClientCompositionForced(isPeekingThrough)) {
721 // If we are forcing client composition, we need to tell the HWC
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500722 requestedCompositionType = Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800723 }
724
725 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700726 // We also resend the composition type when this layer was previously skipped, to ensure that
727 // the composition type is up-to-date.
728 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
729 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800730 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
731
Peiyong Line9d809e2020-04-14 13:10:48 -0700732 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
733 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700734 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500735 to_string(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800736 static_cast<int32_t>(error));
737 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800738 }
739}
740
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800741void OutputLayer::writeCursorPositionToHWC() const {
742 // Skip doing this if there is no HWC interface
743 auto hwcLayer = getHwcLayer();
744 if (!hwcLayer) {
745 return;
746 }
747
Lloyd Piquede196652020-01-22 17:29:58 -0800748 const auto* layerFEState = getLayerFE().getCompositionState();
749 if (!layerFEState) {
750 return;
751 }
752
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700753 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800754
Lloyd Piquede196652020-01-22 17:29:58 -0800755 Rect frame = layerFEState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000756 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800757 Rect position = outputState.transform.transform(frame);
758
759 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700760 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700761 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
762 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
763 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800764 }
765}
766
Lloyd Pique66d68602019-02-13 14:23:31 -0800767HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700768 const auto& state = getState();
769 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800770}
771
772bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700773 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500774 return !state.hwc || state.hwc->hwcCompositionType == Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800775}
776
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800777bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700778 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500779 return state.hwc && state.hwc->hwcCompositionType == Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800780}
781
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500782void OutputLayer::detectDisallowedCompositionTypeChange(Composition from, Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800783 bool result = false;
784 switch (from) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500785 case Composition::INVALID:
786 case Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800787 result = false;
788 break;
789
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500790 case Composition::DEVICE:
791 case Composition::SOLID_COLOR:
792 result = (to == Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800793 break;
794
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500795 case Composition::CURSOR:
796 case Composition::SIDEBAND:
Leon Scroggins7fd536f2021-12-28 14:43:12 +0000797 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800798 case Composition::REFRESH_RATE_INDICATOR:
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500799 result = (to == Composition::CLIENT || to == Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800800 break;
801 }
802
803 if (!result) {
804 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500805 getLayerFE().getDebugName(), to_string(from).c_str(), static_cast<int>(from),
806 to_string(to).c_str(), static_cast<int>(to));
Lloyd Pique66d68602019-02-13 14:23:31 -0800807 }
808}
809
Alec Mourid1bf1b52021-05-05 18:44:58 -0700810bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
811 return getState().forceClientComposition ||
812 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
813}
814
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500815void OutputLayer::applyDeviceCompositionTypeChange(Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700816 auto& state = editState();
817 LOG_FATAL_IF(!state.hwc);
818 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800819
Alec Mourid1bf1b52021-05-05 18:44:58 -0700820 // Only detected disallowed changes if this was not a skip layer, because the
821 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
822 // fewer GPU layers)
823 if (!hwcState.layerSkipped) {
824 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
825 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800826
827 hwcState.hwcCompositionType = compositionType;
828}
829
830void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700831 auto& state = editState();
832 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800833}
834
Peiyong Line9d809e2020-04-14 13:10:48 -0700835void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700836 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800837 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700838 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700839 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800840 break;
841
842 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700843 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800844 toString(request).c_str(), static_cast<int>(request));
845 break;
846 }
847}
848
Lloyd Pique688abd42019-02-15 15:42:24 -0800849bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700850 const auto& state = getState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700851 const auto& sourceCrop = state.sourceCrop;
Alec Mourib6d78932023-09-20 16:05:42 +0000852 auto displayFrameWidth = static_cast<float>(state.displayFrame.getWidth());
853 auto displayFrameHeight = static_cast<float>(state.displayFrame.getHeight());
854
855 if (state.bufferTransform & HAL_TRANSFORM_ROT_90) {
856 std::swap(displayFrameWidth, displayFrameHeight);
857 }
858
859 return sourceCrop.getHeight() != displayFrameHeight ||
860 sourceCrop.getWidth() != displayFrameWidth;
Lloyd Pique688abd42019-02-15 15:42:24 -0800861}
862
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000863std::optional<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionSettings() const {
Dan Stoza6166c312021-01-15 16:34:05 -0800864 if (getState().overrideInfo.buffer == nullptr) {
865 return {};
866 }
867
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700868 // Compute the geometry boundaries in layer stack space: we need to transform from the
869 // framebuffer space of the override buffer to layer space.
870 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
871 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000872 const Rect boundaries = transform.transform(getState().overrideInfo.displayFrame);
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700873
Dan Stoza6166c312021-01-15 16:34:05 -0800874 LayerFE::LayerSettings settings;
875 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700876 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -0800877 };
Alec Mouria90a5702021-04-16 16:36:21 +0000878 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700879 settings.source = renderengine::PixelSource{
880 .buffer = renderengine::Buffer{
881 .buffer = getState().overrideInfo.buffer,
882 .fence = getState().overrideInfo.acquireFence,
883 // If the transform from layer space to display space contains a rotation, we
884 // need to undo the rotation in the texture transform
885 .textureTransform =
886 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
887 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -0800888 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -0800889 settings.alpha = 1.0f;
Alec Mourie8dd3562022-02-11 14:18:57 -0800890 settings.whitePointNits = getOutput().getState().sdrWhitePointNits;
Dan Stoza6166c312021-01-15 16:34:05 -0800891
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000892 return settings;
Dan Stoza6166c312021-01-15 16:34:05 -0800893}
894
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800895void OutputLayer::dump(std::string& out) const {
896 using android::base::StringAppendF;
897
Lloyd Piquede196652020-01-22 17:29:58 -0800898 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700899 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800900}
901
Lloyd Piquecc01a452018-12-04 17:24:00 -0800902} // namespace impl
903} // namespace android::compositionengine