blob: b492b6a79ee727bbc3f9754bdd869b3beda5e943 [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 */
16
Alec Mourid1bf1b52021-05-05 18:44:58 -070017#include <DisplayHardware/Hal.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080018#include <android-base/stringprintf.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080019#include <compositionengine/DisplayColorProfile.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070020#include <compositionengine/LayerFECompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080021#include <compositionengine/Output.h>
Alec Mourie7cc1c22021-04-27 15:23:26 -070022#include <compositionengine/impl/HwcBufferCache.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080023#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080024#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080025#include <compositionengine/impl/OutputLayerCompositionState.h>
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -040026#include <cstdint>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070027#include "system/graphics-base-v1.0.h"
28
29#include <ui/DataspaceUtils.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;
41
Lloyd Piquecc01a452018-12-04 17:24:00 -080042namespace android::compositionengine {
43
44OutputLayer::~OutputLayer() = default;
45
46namespace impl {
47
Lloyd Piquea83776c2019-01-29 18:42:32 -080048namespace {
49
50FloatRect reduce(const FloatRect& win, const Region& exclude) {
51 if (CC_LIKELY(exclude.isEmpty())) {
52 return win;
53 }
54 // Convert through Rect (by rounding) for lack of FloatRegion
55 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
56}
57
58} // namespace
59
Lloyd Piquede196652020-01-22 17:29:58 -080060std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
61 const sp<compositionengine::LayerFE>& layerFE) {
62 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080063}
64
Lloyd Piquecc01a452018-12-04 17:24:00 -080065OutputLayer::~OutputLayer() = default;
66
Lloyd Piquedf336d92019-03-07 21:38:42 -080067void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070068 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080069 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070070 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080071 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070072 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080073 }
Lloyd Pique07e33212018-12-18 16:33:37 -080074}
75
Lloyd Piquea83776c2019-01-29 18:42:32 -080076Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080077 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080078
79 // apply the projection's clipping to the window crop in
80 // layerstack space, and convert-back to layer space.
81 // if there are no window scaling involved, this operation will map to full
82 // pixels in the buffer.
83
84 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080085 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080086
Angel Aguayob084e0c2021-08-04 23:27:28 +000087 const Rect& viewport = getOutput().getState().layerStackSpace.getContent();
Lloyd Piquea83776c2019-01-29 18:42:32 -080088 const ui::Transform& layerTransform = layerState.geomLayerTransform;
89 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
90 // Transform to screen space.
91 activeCropFloat = layerTransform.transform(activeCropFloat);
92 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
93 // Back to layer space to work with the content crop.
94 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
95
96 // This needs to be here as transform.transform(Rect) computes the
97 // transformed rect and then takes the bounding box of the result before
98 // returning. This means
99 // transform.inverse().transform(transform.transform(Rect)) != Rect
100 // in which case we need to make sure the final rect is clipped to the
101 // display bounds.
102 Rect activeCrop{activeCropFloat};
103 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
104 activeCrop.clear();
105 }
106 return activeCrop;
107}
108
ramindani2c043be2022-04-19 20:11:10 +0000109FloatRect OutputLayer::calculateOutputSourceCrop(uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800110 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800111
112 if (!layerState.geomUsesSourceCrop) {
113 return {};
114 }
115
116 // the content crop is the area of the content that gets scaled to the
117 // layer's size. This is in buffer space.
118 FloatRect crop = layerState.geomContentCrop.toFloatRect();
119
120 // In addition there is a WM-specified crop we pull from our drawing state.
121 Rect activeCrop = calculateInitialCrop();
122 const Rect& bufferSize = layerState.geomBufferSize;
123
124 int winWidth = bufferSize.getWidth();
125 int winHeight = bufferSize.getHeight();
126
127 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
128 // if display frame hasn't been set and the parent is an unbounded layer.
129 if (winWidth < 0 && winHeight < 0) {
130 return crop;
131 }
132
133 // Transform the window crop to match the buffer coordinate system,
134 // which means using the inverse of the current transform set on the
135 // SurfaceFlingerConsumer.
136 uint32_t invTransform = layerState.geomBufferTransform;
137 if (layerState.geomBufferUsesDisplayInverseTransform) {
138 /*
139 * the code below applies the primary display's inverse transform to the
140 * buffer
141 */
ramindani2c043be2022-04-19 20:11:10 +0000142 uint32_t invTransformOrient = internalDisplayRotationFlags;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800143 // calculate the inverse transform
144 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
145 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
146 }
147 // and apply to the current transform
148 invTransform =
149 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
150 }
151
152 if (invTransform & HAL_TRANSFORM_ROT_90) {
153 // If the activeCrop has been rotate the ends are rotated but not
154 // the space itself so when transforming ends back we can't rely on
155 // a modification of the axes of rotation. To account for this we
156 // need to reorient the inverse rotation in terms of the current
157 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200158 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
159 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
160 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800161 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
162 }
163 std::swap(winWidth, winHeight);
164 }
165 const Rect winCrop =
166 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
167
168 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200169 const float xScale = crop.getWidth() / float(winWidth);
170 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800171
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200172 const float insetLeft = winCrop.left * xScale;
173 const float insetTop = winCrop.top * yScale;
174 const float insetRight = (winWidth - winCrop.right) * xScale;
175 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800176
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200177 crop.left += insetLeft;
178 crop.top += insetTop;
179 crop.right -= insetRight;
180 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800181
182 return crop;
183}
184
185Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800186 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700187 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800188
189 // apply the layer's transform, followed by the display's global transform
190 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800191 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800192 const ui::Transform& layerTransform = layerState.geomLayerTransform;
193 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
194 const Rect& bufferSize = layerState.geomBufferSize;
195 Rect activeCrop = layerState.geomCrop;
196 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
197 activeCrop = layerTransform.transform(activeCrop);
Angel Aguayob084e0c2021-08-04 23:27:28 +0000198 if (!activeCrop.intersect(outputState.layerStackSpace.getContent(), &activeCrop)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800199 activeCrop.clear();
200 }
201 activeCrop = inverseLayerTransform.transform(activeCrop, true);
202 // This needs to be here as transform.transform(Rect) computes the
203 // transformed rect and then takes the bounding box of the result before
204 // returning. This means
205 // transform.inverse().transform(transform.transform(Rect)) != Rect
206 // in which case we need to make sure the final rect is clipped to the
207 // display bounds.
208 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
209 activeCrop.clear();
210 }
211 // mark regions outside the crop as transparent
212 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
213 activeTransparentRegion.orSelf(
214 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
215 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
216 activeTransparentRegion.orSelf(
217 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
218 }
219
220 // reduce uses a FloatRect to provide more accuracy during the
221 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400222 FloatRect geomLayerBounds = layerState.geomLayerBounds;
223
224 // Some HWCs may clip client composited input to its displayFrame. Make sure
225 // that this does not cut off the shadow.
226 if (layerState.forceClientComposition && layerState.shadowRadius > 0.0f) {
227 const auto outset = layerState.shadowRadius;
228 geomLayerBounds.left -= outset;
229 geomLayerBounds.top -= outset;
230 geomLayerBounds.right += outset;
231 geomLayerBounds.bottom += outset;
232 }
233 Rect frame{layerTransform.transform(reduce(geomLayerBounds, activeTransparentRegion))};
Angel Aguayob084e0c2021-08-04 23:27:28 +0000234 if (!frame.intersect(outputState.layerStackSpace.getContent(), &frame)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800235 frame.clear();
236 }
237 const ui::Transform displayTransform{outputState.transform};
238
239 return displayTransform.transform(frame);
240}
241
Snild Dolkow9e217d62020-04-22 15:53:42 +0200242uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
243 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800244 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700245 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800246
247 /*
248 * Transformations are applied in this order:
249 * 1) buffer orientation/flip/mirror
250 * 2) state transformation (window manager)
251 * 3) layer orientation (screen orientation)
252 * (NOTE: the matrices are multiplied in reverse order)
253 */
254 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700255 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800256 const ui::Transform bufferTransform{layerState.geomBufferTransform};
257 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
258
259 if (layerState.geomBufferUsesDisplayInverseTransform) {
260 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200261 * We must apply the internal display's inverse transform to the buffer
262 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800263 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200264 uint32_t invTransform = internalDisplayRotationFlags;
265
Lloyd Piquea83776c2019-01-29 18:42:32 -0800266 // calculate the inverse transform
267 if (invTransform & HAL_TRANSFORM_ROT_90) {
268 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
269 }
270
271 /*
272 * Here we cancel out the orientation component of the WM transform.
273 * The scaling and translate components are already included in our bounds
274 * computation so it's enough to just omit it in the composition.
275 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
276 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000277 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800278 }
279
280 // this gives us only the "orientation" component of the transform
281 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200282}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800283
Snild Dolkow9e217d62020-04-22 15:53:42 +0200284void OutputLayer::updateCompositionState(
285 bool includeGeometry, bool forceClientComposition,
286 ui::Transform::RotationFlags internalDisplayRotationFlags) {
Lloyd Piquede196652020-01-22 17:29:58 -0800287 const auto* layerFEState = getLayerFE().getCompositionState();
288 if (!layerFEState) {
289 return;
290 }
291
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700292 const auto& outputState = getOutput().getState();
293 const auto& profile = *getOutput().getDisplayColorProfile();
294 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800295
Lloyd Piquea83776c2019-01-29 18:42:32 -0800296 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700297 // Clear the forceClientComposition flag before it is set for any
298 // reason. Note that since it can be set by some checks below when
299 // updating the geometry state, we only clear it when updating the
300 // geometry since those conditions for forcing client composition won't
301 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700302 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700303
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 state.displayFrame = calculateOutputDisplayFrame();
ramindani2c043be2022-04-19 20:11:10 +0000305 state.sourceCrop = calculateOutputSourceCrop(internalDisplayRotationFlags);
Snild Dolkow9e217d62020-04-22 15:53:42 +0200306 state.bufferTransform = static_cast<Hwc2::Transform>(
307 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800308
Lloyd Piquede196652020-01-22 17:29:58 -0800309 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
311 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800312 }
313 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800314
315 // Determine the output dependent dataspace for this layer. If it is
316 // colorspace agnostic, it just uses the dataspace chosen for the output to
317 // avoid the need for color conversion.
Alec Mouri88790f32023-07-21 01:25:14 +0000318 // For now, also respect the colorspace agnostic flag if we're drawing to HDR, to avoid drastic
319 // luminance shift. TODO(b/292162273): we should check if that's true though.
320 state.dataspace = layerFEState->isColorspaceAgnostic && !isHdrDataspace(outputState.dataspace)
321 ? outputState.dataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800322 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800323
Alec Mouridda07d92022-04-25 22:39:25 +0000324 // Override the dataspace transfer from 170M to sRGB if the device configuration requests this.
325 // We do this here instead of in buffer info so that dumpsys can still report layers that are
326 // using the 170M transfer. Also we only do this if the colorspace is not agnostic for the
327 // layer, in case the color profile uses a 170M transfer function.
328 if (outputState.treat170mAsSrgb && !layerFEState->isColorspaceAgnostic &&
329 (state.dataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_SMPTE_170M) {
330 state.dataspace = static_cast<ui::Dataspace>(
331 (state.dataspace & HAL_DATASPACE_STANDARD_MASK) |
332 (state.dataspace & HAL_DATASPACE_RANGE_MASK) | HAL_DATASPACE_TRANSFER_SRGB);
333 }
334
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700335 // For hdr content, treat the white point as the display brightness - HDR content should not be
336 // boosted or dimmed.
Sally Qi81d95e62022-03-21 19:41:33 -0700337 // If the layer explicitly requests to disable dimming, then don't dim either.
Alec Mouri6da0e272022-02-07 12:45:57 -0800338 if (isHdrDataspace(state.dataspace) ||
Alec Mourie8dd3562022-02-11 14:18:57 -0800339 getOutput().getState().displayBrightnessNits == getOutput().getState().sdrWhitePointNits ||
Sally Qi81d95e62022-03-21 19:41:33 -0700340 getOutput().getState().displayBrightnessNits == 0.f || !layerFEState->dimmingEnabled) {
Alec Mouri6da0e272022-02-07 12:45:57 -0800341 state.dimmingRatio = 1.f;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700342 state.whitePointNits = getOutput().getState().displayBrightnessNits;
343 } else {
John Reck68796592023-01-25 13:47:12 -0500344 float layerBrightnessNits = getOutput().getState().sdrWhitePointNits;
345 // RANGE_EXTENDED can "self-promote" to HDR, but is still rendered for a particular
346 // range that we may need to re-adjust to the current display conditions
347 if ((state.dataspace & HAL_DATASPACE_RANGE_MASK) == HAL_DATASPACE_RANGE_EXTENDED &&
Sally Qi963049b2023-03-23 14:06:21 -0700348 layerFEState->currentHdrSdrRatio > 1.01f) {
349 layerBrightnessNits *= layerFEState->currentHdrSdrRatio;
John Reck68796592023-01-25 13:47:12 -0500350 }
351 state.dimmingRatio =
352 std::clamp(layerBrightnessNits / getOutput().getState().displayBrightnessNits, 0.f,
353 1.f);
354 state.whitePointNits = layerBrightnessNits;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700355 }
356
Lloyd Piquef5275482019-01-29 18:42:42 -0800357 // These are evaluated every frame as they can potentially change at any
358 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800359 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700360 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700361 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800362 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800363}
364
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400365void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
366 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700367 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800368 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700369 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800370 return;
371 }
372
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700373 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800374 if (!hwcLayer) {
375 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700376 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800377 return;
378 }
379
Lloyd Piquede196652020-01-22 17:29:58 -0800380 const auto* outputIndependentState = getLayerFE().getCompositionState();
381 if (!outputIndependentState) {
382 return;
383 }
384
385 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800386
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500387 if (requestedCompositionType == Composition::SOLID_COLOR && state.overrideInfo.buffer) {
Huihong Luo7a8dc172022-05-26 11:52:19 -0700388 // this should never happen, as SOLID_COLOR is skipped from caching, b/230073351
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500389 requestedCompositionType = Composition::DEVICE;
Alec Mouri028676a2021-12-02 15:01:48 -0800390 }
391
Yichi Chen413d46a2021-04-07 21:42:09 +0800392 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
393 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400394 const bool isOverridden =
395 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800396 const bool prevOverridden = state.hwc->stateOverridden;
397 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400398 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800399 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
400 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800401 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800402
Lloyd Piquef5275482019-01-29 18:42:42 -0800403 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800404 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
405 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800406
Alec Mourid1bf1b52021-05-05 18:44:58 -0700407 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
408 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700409
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500410 if (requestedCompositionType == Composition::SOLID_COLOR) {
Alec Mouri028676a2021-12-02 15:01:48 -0800411 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
412 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800413
414 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700415 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800416}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800417
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400418void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500419 Composition requestedCompositionType,
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400420 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800421 const auto& outputDependentState = getState();
422
Dan Stoza6166c312021-01-15 16:34:05 -0800423 Rect displayFrame = outputDependentState.displayFrame;
424 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700425
Alec Mouri464352b2021-03-24 16:33:21 -0700426 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800427 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000428 sourceCrop =
429 FloatRect(0.f, 0.f,
430 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
431 ->getWidth()),
432 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
433 ->getHeight()));
Lloyd Piquef5275482019-01-29 18:42:42 -0800434 }
435
Dan Stoza6166c312021-01-15 16:34:05 -0800436 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
437 displayFrame.right, displayFrame.bottom);
438
439 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
440 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
441 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
442 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
443 }
444
445 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800446 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
447 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800448 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
449 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800450 }
451
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400452 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
453 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
454 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800455 }
456
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700457 // Solid-color layers and overridden buffers should always use an identity transform.
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500458 const auto bufferTransform = (requestedCompositionType != Composition::SOLID_COLOR &&
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700459 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800460 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700461 : static_cast<hal::Transform>(0);
462 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
463 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700464 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800465 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
466 static_cast<int32_t>(error));
467 }
468}
469
470void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800471 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
472 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400473 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
474 // PREMULTIPLIED so it will peek through.
475 const auto& overrideInfo = getState().overrideInfo;
476 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700477 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
478 : outputIndependentState.blendMode;
479 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700480 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700481 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800482 }
483
Alec Mouriee69a592021-03-23 15:00:45 -0700484 const float alpha = skipLayer
485 ? 0.0f
486 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800487 ALOGV("Writing alpha %f", alpha);
488
489 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
490 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
491 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 }
493
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800494 for (const auto& [name, entry] : outputIndependentState.metadata) {
495 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700496 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800497 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
498 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
499 }
500 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800501}
502
503void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
504 const auto& outputDependentState = getState();
505
Lloyd Piquea2468662019-03-07 21:31:06 -0800506 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800507 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700508 Region visibleRegion = outputDependentState.overrideInfo.buffer
509 ? Region(outputDependentState.overrideInfo.visibleRegion)
510 : outputDependentState.outputSpaceVisibleRegion;
511 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700512 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800513 to_string(error).c_str(), static_cast<int32_t>(error));
Leon Scroggins IIIf2b8ec42022-01-05 13:23:36 -0500514 visibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800515 }
516
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500517 if (auto error =
518 hwcLayer->setBlockingRegion(outputDependentState.outputSpaceBlockingRegionHint);
519 error != hal::Error::NONE) {
520 ALOGE("[%s] Failed to set blocking region: %s (%d)", getLayerFE().getDebugName(),
521 to_string(error).c_str(), static_cast<int32_t>(error));
522 outputDependentState.outputSpaceBlockingRegionHint.dump(LOG_TAG);
523 }
524
Alec Mourib7edfc22021-03-17 16:20:26 -0700525 const auto dataspace = outputDependentState.overrideInfo.buffer
526 ? outputDependentState.overrideInfo.dataspace
527 : outputDependentState.dataspace;
528
529 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
530 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
531 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800532 }
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700533
Alec Mourie8dd3562022-02-11 14:18:57 -0800534 // Cached layers are not dimmed, which means that composer should attempt to dim.
535 // Note that if the dimming ratio is large, then this may cause the cached layer
536 // to kick back into GPU composition :(
537 // Also note that this assumes that there are no HDR layers that are able to be cached.
538 // Otherwise, this could cause HDR layers to be dimmed twice.
539 const auto dimmingRatio = outputDependentState.overrideInfo.buffer
540 ? (getOutput().getState().displayBrightnessNits != 0.f
541 ? std::clamp(getOutput().getState().sdrWhitePointNits /
542 getOutput().getState().displayBrightnessNits,
543 0.f, 1.f)
544 : 1.f)
545 : outputDependentState.dimmingRatio;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700546
Alec Mouri6da0e272022-02-07 12:45:57 -0800547 if (auto error = hwcLayer->setBrightness(dimmingRatio); error != hal::Error::NONE) {
548 ALOGE("[%s] Failed to set brightness %f: %s (%d)", getLayerFE().getDebugName(),
549 dimmingRatio, to_string(error).c_str(), static_cast<int32_t>(error));
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700550 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800551}
552
553void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700554 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500555 Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800556 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700557 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800558 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700559 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800560 editState().forceClientComposition = true;
561 break;
562 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700563 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800564 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800565 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800566
Alec Mouri464352b2021-03-24 16:33:21 -0700567 const Region& surfaceDamage = getState().overrideInfo.buffer
568 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700569 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
570 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700571
572 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700573 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800574 to_string(error).c_str(), static_cast<int32_t>(error));
575 outputIndependentState.surfaceDamage.dump(LOG_TAG);
576 }
577
578 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800579 switch (compositionType) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500580 case Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700581 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800582 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500583 case Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800584 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
585 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500586 case Composition::CURSOR:
587 case Composition::DEVICE:
Leon Scroggins III09c25412021-12-02 14:49:56 -0500588 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800589 case Composition::REFRESH_RATE_INDICATOR:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700590 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800591 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500592 case Composition::INVALID:
593 case Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800594 // Ignored
595 break;
596 }
597}
598
599void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
600 const LayerFECompositionState& outputIndependentState) {
Ady Abraham6e60b142022-01-06 18:10:35 -0800601 aidl::android::hardware::graphics::composer3::Color color = {outputIndependentState.color.r,
602 outputIndependentState.color.g,
603 outputIndependentState.color.b,
604 1.0f};
Lloyd Piquef5275482019-01-29 18:42:42 -0800605
Peiyong Line9d809e2020-04-14 13:10:48 -0700606 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700607 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800608 to_string(error).c_str(), static_cast<int32_t>(error));
609 }
610}
611
612void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
613 const LayerFECompositionState& outputIndependentState) {
614 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700615 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700616 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800617 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
618 static_cast<int32_t>(error));
619 }
620}
621
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700622void OutputLayer::uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700623 auto& state = editState();
624 // Skip doing this if there is no HWC interface
625 if (!state.hwc) {
626 return;
627 }
628
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700629 // Uncache the active buffer last so that it's the first buffer to be purged from the cache
630 // next time a buffer is sent to this layer.
631 bool uncacheActiveBuffer = false;
632
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700633 std::vector<uint32_t> slotsToClear;
634 for (uint64_t bufferId : bufferIdsToUncache) {
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700635 if (bufferId == state.hwc->activeBufferId) {
636 uncacheActiveBuffer = true;
637 } else {
638 uint32_t slot = state.hwc->hwcBufferCache.uncache(bufferId);
639 if (slot != UINT32_MAX) {
640 slotsToClear.push_back(slot);
641 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700642 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700643 }
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700644 if (uncacheActiveBuffer) {
645 slotsToClear.push_back(state.hwc->hwcBufferCache.uncache(state.hwc->activeBufferId));
646 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700647
648 hal::Error error =
649 state.hwc->hwcLayer->setBufferSlotsToClear(slotsToClear, state.hwc->activeBufferSlot);
650 if (error != hal::Error::NONE) {
651 ALOGE("[%s] Failed to clear buffer slots: %s (%d)", getLayerFE().getDebugName(),
652 to_string(error).c_str(), static_cast<int32_t>(error));
653 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700654}
655
Lloyd Piquef5275482019-01-29 18:42:42 -0800656void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700657 const LayerFECompositionState& outputIndependentState,
658 bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800659 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700660 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800661 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
662 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700663 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700664 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800665 to_string(error).c_str(), static_cast<int32_t>(error));
666 }
667
Brian Lindahl439afad2022-11-14 11:16:55 -0700668 HwcSlotAndBuffer hwcSlotAndBuffer;
669 sp<Fence> hwcFence;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700670 {
671 // Editing the state only because we update the HWC buffer cache and active buffer.
672 auto& state = editState();
673 // Override buffers use a special cache slot so that they don't evict client buffers.
674 if (state.overrideInfo.buffer != nullptr && !skipLayer) {
675 hwcSlotAndBuffer = state.hwc->hwcBufferCache.getOverrideHwcSlotAndBuffer(
676 state.overrideInfo.buffer->getBuffer());
677 hwcFence = state.overrideInfo.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700678 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
679 // slot will be used first, allowing the memory to be freed as soon as possible.
680 state.hwc->activeBufferId = state.overrideInfo.buffer->getBuffer()->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700681 } else {
682 hwcSlotAndBuffer =
683 state.hwc->hwcBufferCache.getHwcSlotAndBuffer(outputIndependentState.buffer);
684 hwcFence = outputIndependentState.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700685 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
686 // slot will be used first, allowing the memory to be freed as soon as possible.
687 state.hwc->activeBufferId = outputIndependentState.buffer->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700688 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700689 // Keep track of the active buffer slot, so we can restore it after clearing other buffer
690 // slots.
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700691 state.hwc->activeBufferSlot = hwcSlotAndBuffer.slot;
Dan Stoza6166c312021-01-15 16:34:05 -0800692 }
693
Brian Lindahl439afad2022-11-14 11:16:55 -0700694 if (auto error = hwcLayer->setBuffer(hwcSlotAndBuffer.slot, hwcSlotAndBuffer.buffer, hwcFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700695 error != hal::Error::NONE) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700696 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
697 hwcSlotAndBuffer.buffer->handle, to_string(error).c_str(),
698 static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800699 }
700}
701
Peiyong Line9d809e2020-04-14 13:10:48 -0700702void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500703 Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700704 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800705 auto& outputDependentState = editState();
706
Alec Mourid1bf1b52021-05-05 18:44:58 -0700707 if (isClientCompositionForced(isPeekingThrough)) {
708 // If we are forcing client composition, we need to tell the HWC
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500709 requestedCompositionType = Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800710 }
711
712 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700713 // We also resend the composition type when this layer was previously skipped, to ensure that
714 // the composition type is up-to-date.
715 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
716 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800717 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
718
Peiyong Line9d809e2020-04-14 13:10:48 -0700719 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
720 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700721 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500722 to_string(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800723 static_cast<int32_t>(error));
724 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800725 }
726}
727
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800728void OutputLayer::writeCursorPositionToHWC() const {
729 // Skip doing this if there is no HWC interface
730 auto hwcLayer = getHwcLayer();
731 if (!hwcLayer) {
732 return;
733 }
734
Lloyd Piquede196652020-01-22 17:29:58 -0800735 const auto* layerFEState = getLayerFE().getCompositionState();
736 if (!layerFEState) {
737 return;
738 }
739
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700740 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800741
Lloyd Piquede196652020-01-22 17:29:58 -0800742 Rect frame = layerFEState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000743 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800744 Rect position = outputState.transform.transform(frame);
745
746 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700747 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700748 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
749 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
750 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800751 }
752}
753
Lloyd Pique66d68602019-02-13 14:23:31 -0800754HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700755 const auto& state = getState();
756 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800757}
758
759bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700760 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500761 return !state.hwc || state.hwc->hwcCompositionType == Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800762}
763
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800764bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700765 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500766 return state.hwc && state.hwc->hwcCompositionType == Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800767}
768
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500769void OutputLayer::detectDisallowedCompositionTypeChange(Composition from, Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800770 bool result = false;
771 switch (from) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500772 case Composition::INVALID:
773 case Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800774 result = false;
775 break;
776
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500777 case Composition::DEVICE:
778 case Composition::SOLID_COLOR:
779 result = (to == Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800780 break;
781
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500782 case Composition::CURSOR:
783 case Composition::SIDEBAND:
Leon Scroggins7fd536f2021-12-28 14:43:12 +0000784 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800785 case Composition::REFRESH_RATE_INDICATOR:
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500786 result = (to == Composition::CLIENT || to == Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800787 break;
788 }
789
790 if (!result) {
791 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500792 getLayerFE().getDebugName(), to_string(from).c_str(), static_cast<int>(from),
793 to_string(to).c_str(), static_cast<int>(to));
Lloyd Pique66d68602019-02-13 14:23:31 -0800794 }
795}
796
Alec Mourid1bf1b52021-05-05 18:44:58 -0700797bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
798 return getState().forceClientComposition ||
799 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
800}
801
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500802void OutputLayer::applyDeviceCompositionTypeChange(Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700803 auto& state = editState();
804 LOG_FATAL_IF(!state.hwc);
805 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800806
Alec Mourid1bf1b52021-05-05 18:44:58 -0700807 // Only detected disallowed changes if this was not a skip layer, because the
808 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
809 // fewer GPU layers)
810 if (!hwcState.layerSkipped) {
811 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
812 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800813
814 hwcState.hwcCompositionType = compositionType;
815}
816
817void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700818 auto& state = editState();
819 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800820}
821
Peiyong Line9d809e2020-04-14 13:10:48 -0700822void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700823 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800824 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700825 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700826 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800827 break;
828
829 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700830 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800831 toString(request).c_str(), static_cast<int>(request));
832 break;
833 }
834}
835
Lloyd Pique688abd42019-02-15 15:42:24 -0800836bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700837 const auto& state = getState();
838 const auto& displayFrame = state.displayFrame;
839 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800840 return sourceCrop.getHeight() != displayFrame.getHeight() ||
841 sourceCrop.getWidth() != displayFrame.getWidth();
842}
843
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000844std::optional<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionSettings() const {
Dan Stoza6166c312021-01-15 16:34:05 -0800845 if (getState().overrideInfo.buffer == nullptr) {
846 return {};
847 }
848
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700849 // Compute the geometry boundaries in layer stack space: we need to transform from the
850 // framebuffer space of the override buffer to layer space.
851 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
852 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000853 const Rect boundaries = transform.transform(getState().overrideInfo.displayFrame);
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700854
Dan Stoza6166c312021-01-15 16:34:05 -0800855 LayerFE::LayerSettings settings;
856 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700857 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -0800858 };
Alec Mouria90a5702021-04-16 16:36:21 +0000859 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700860 settings.source = renderengine::PixelSource{
861 .buffer = renderengine::Buffer{
862 .buffer = getState().overrideInfo.buffer,
863 .fence = getState().overrideInfo.acquireFence,
864 // If the transform from layer space to display space contains a rotation, we
865 // need to undo the rotation in the texture transform
866 .textureTransform =
867 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
868 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -0800869 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -0800870 settings.alpha = 1.0f;
Alec Mourie8dd3562022-02-11 14:18:57 -0800871 settings.whitePointNits = getOutput().getState().sdrWhitePointNits;
Dan Stoza6166c312021-01-15 16:34:05 -0800872
Patrick Williams16d8b2c2022-08-08 17:29:05 +0000873 return settings;
Dan Stoza6166c312021-01-15 16:34:05 -0800874}
875
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800876void OutputLayer::dump(std::string& out) const {
877 using android::base::StringAppendF;
878
Lloyd Piquede196652020-01-22 17:29:58 -0800879 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700880 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800881}
882
Lloyd Piquecc01a452018-12-04 17:24:00 -0800883} // namespace impl
884} // namespace android::compositionengine