blob: e31d6848eb136e36f0b1c3d1fbe8f674ccd08761 [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 */
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060016
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>
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060026#include <ui/FloatRect.h>
27#include <ui/HdrRenderTypeUtils.h>
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -040028#include <cstdint>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070029#include "system/graphics-base-v1.0.h"
30
Brian Lindahl1a4ffd82024-10-30 11:00:37 -060031#include <com_android_graphics_libgui_flags.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080032
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080033// TODO(b/129481165): remove the #pragma below and fix conversion issues
34#pragma clang diagnostic push
35#pragma clang diagnostic ignored "-Wconversion"
36
Lloyd Pique07e33212018-12-18 16:33:37 -080037#include "DisplayHardware/HWComposer.h"
38
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080039// TODO(b/129481165): remove the #pragma below and fix conversion issues
40#pragma clang diagnostic pop // ignored "-Wconversion"
41
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050042using aidl::android::hardware::graphics::composer3::Composition;
Sally Qi0abc4a52024-09-26 16:13:06 -070043using aidl::android::hardware::graphics::composer3::Luts;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -050044
Lloyd Piquecc01a452018-12-04 17:24:00 -080045namespace android::compositionengine {
46
47OutputLayer::~OutputLayer() = default;
48
49namespace impl {
50
Lloyd Piquea83776c2019-01-29 18:42:32 -080051namespace {
52
53FloatRect reduce(const FloatRect& win, const Region& exclude) {
54 if (CC_LIKELY(exclude.isEmpty())) {
55 return win;
56 }
57 // Convert through Rect (by rounding) for lack of FloatRegion
58 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
59}
60
61} // namespace
62
Lloyd Piquede196652020-01-22 17:29:58 -080063std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
64 const sp<compositionengine::LayerFE>& layerFE) {
65 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080066}
67
Lloyd Piquecc01a452018-12-04 17:24:00 -080068OutputLayer::~OutputLayer() = default;
69
Lloyd Piquedf336d92019-03-07 21:38:42 -080070void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070071 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080072 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070073 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080074 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070075 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080076 }
Lloyd Pique07e33212018-12-18 16:33:37 -080077}
78
Lloyd Piquea83776c2019-01-29 18:42:32 -080079Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080080 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080081
82 // apply the projection's clipping to the window crop in
83 // layerstack space, and convert-back to layer space.
84 // if there are no window scaling involved, this operation will map to full
85 // pixels in the buffer.
86
87 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080088 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080089
Angel Aguayob084e0c2021-08-04 23:27:28 +000090 const Rect& viewport = getOutput().getState().layerStackSpace.getContent();
Lloyd Piquea83776c2019-01-29 18:42:32 -080091 const ui::Transform& layerTransform = layerState.geomLayerTransform;
92 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
93 // Transform to screen space.
94 activeCropFloat = layerTransform.transform(activeCropFloat);
95 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
96 // Back to layer space to work with the content crop.
97 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
98
99 // This needs to be here as transform.transform(Rect) computes the
100 // transformed rect and then takes the bounding box of the result before
101 // returning. This means
102 // transform.inverse().transform(transform.transform(Rect)) != Rect
103 // in which case we need to make sure the final rect is clipped to the
104 // display bounds.
105 Rect activeCrop{activeCropFloat};
106 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
107 activeCrop.clear();
108 }
109 return activeCrop;
110}
111
ramindani2c043be2022-04-19 20:11:10 +0000112FloatRect OutputLayer::calculateOutputSourceCrop(uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800113 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800114
115 if (!layerState.geomUsesSourceCrop) {
116 return {};
117 }
118
119 // the content crop is the area of the content that gets scaled to the
120 // layer's size. This is in buffer space.
121 FloatRect crop = layerState.geomContentCrop.toFloatRect();
122
123 // In addition there is a WM-specified crop we pull from our drawing state.
124 Rect activeCrop = calculateInitialCrop();
125 const Rect& bufferSize = layerState.geomBufferSize;
126
127 int winWidth = bufferSize.getWidth();
128 int winHeight = bufferSize.getHeight();
129
130 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
131 // if display frame hasn't been set and the parent is an unbounded layer.
132 if (winWidth < 0 && winHeight < 0) {
133 return crop;
134 }
135
136 // Transform the window crop to match the buffer coordinate system,
137 // which means using the inverse of the current transform set on the
138 // SurfaceFlingerConsumer.
139 uint32_t invTransform = layerState.geomBufferTransform;
140 if (layerState.geomBufferUsesDisplayInverseTransform) {
141 /*
142 * the code below applies the primary display's inverse transform to the
143 * buffer
144 */
ramindani2c043be2022-04-19 20:11:10 +0000145 uint32_t invTransformOrient = internalDisplayRotationFlags;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800146 // calculate the inverse transform
147 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
148 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
149 }
150 // and apply to the current transform
151 invTransform =
152 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
153 }
154
155 if (invTransform & HAL_TRANSFORM_ROT_90) {
156 // If the activeCrop has been rotate the ends are rotated but not
157 // the space itself so when transforming ends back we can't rely on
158 // a modification of the axes of rotation. To account for this we
159 // need to reorient the inverse rotation in terms of the current
160 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200161 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
162 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
163 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800164 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
165 }
166 std::swap(winWidth, winHeight);
167 }
168 const Rect winCrop =
169 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
170
171 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200172 const float xScale = crop.getWidth() / float(winWidth);
173 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800174
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200175 const float insetLeft = winCrop.left * xScale;
176 const float insetTop = winCrop.top * yScale;
177 const float insetRight = (winWidth - winCrop.right) * xScale;
178 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800179
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200180 crop.left += insetLeft;
181 crop.top += insetTop;
182 crop.right -= insetRight;
183 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800184
185 return crop;
186}
187
188Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800189 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700190 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800191
Vishnu Naira9123c82024-10-03 03:56:44 +0000192 // Convert from layer space to layerStackSpace
Lloyd Piquea83776c2019-01-29 18:42:32 -0800193 // apply the layer's transform, followed by the display's global transform
194 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquea83776c2019-01-29 18:42:32 -0800195 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Vishnu Naira9123c82024-10-03 03:56:44 +0000196 Region activeTransparentRegion = layerTransform.transform(layerState.transparentRegionHint);
197 if (!layerState.geomCrop.isEmpty() && layerState.geomBufferSize.isValid()) {
198 FloatRect activeCrop = layerTransform.transform(layerState.geomCrop);
199 activeCrop = activeCrop.intersect(outputState.layerStackSpace.getContent().toFloatRect());
200 const FloatRect& bufferSize =
201 layerTransform.transform(layerState.geomBufferSize.toFloatRect());
202 activeCrop = activeCrop.intersect(bufferSize);
203
Lloyd Piquea83776c2019-01-29 18:42:32 -0800204 // mark regions outside the crop as transparent
Vishnu Naira9123c82024-10-03 03:56:44 +0000205 Rect topRegion = Rect(layerTransform.transform(
206 FloatRect(0, 0, layerState.geomBufferSize.getWidth(), layerState.geomCrop.top)));
207 Rect bottomRegion = Rect(layerTransform.transform(
208 FloatRect(0, layerState.geomCrop.bottom, layerState.geomBufferSize.getWidth(),
209 layerState.geomBufferSize.getHeight())));
210 Rect leftRegion = Rect(layerTransform.transform(FloatRect(0, layerState.geomCrop.top,
211 layerState.geomCrop.left,
212 layerState.geomCrop.bottom)));
213 Rect rightRegion = Rect(layerTransform.transform(
214 FloatRect(layerState.geomCrop.right, layerState.geomCrop.top,
215 layerState.geomBufferSize.getWidth(), layerState.geomCrop.bottom)));
216
217 activeTransparentRegion.orSelf(topRegion);
218 activeTransparentRegion.orSelf(bottomRegion);
219 activeTransparentRegion.orSelf(leftRegion);
220 activeTransparentRegion.orSelf(rightRegion);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800221 }
222
223 // reduce uses a FloatRect to provide more accuracy during the
224 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400225 FloatRect geomLayerBounds = layerState.geomLayerBounds;
226
227 // Some HWCs may clip client composited input to its displayFrame. Make sure
228 // that this does not cut off the shadow.
Vishnu Naird9e4f462023-10-06 04:05:45 +0000229 if (layerState.forceClientComposition && layerState.shadowSettings.length > 0.0f) {
Alec Mourida128512024-09-28 23:46:58 +0000230 // RenderEngine currently blurs shadows to smooth out edges, so outset by
231 // 2x the length instead of 1x to compensate
232 const auto outset = layerState.shadowSettings.length * 2;
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400233 geomLayerBounds.left -= outset;
234 geomLayerBounds.top -= outset;
235 geomLayerBounds.right += outset;
236 geomLayerBounds.bottom += outset;
237 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800238
Vishnu Naira9123c82024-10-03 03:56:44 +0000239 geomLayerBounds = layerTransform.transform(geomLayerBounds);
240 FloatRect frame = reduce(geomLayerBounds, activeTransparentRegion);
241 frame = frame.intersect(outputState.layerStackSpace.getContent().toFloatRect());
242
243 // convert from layerStackSpace to displaySpace
244 const ui::Transform displayTransform{outputState.transform};
245 return Rect(displayTransform.transform(frame));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800246}
247
Snild Dolkow9e217d62020-04-22 15:53:42 +0200248uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
249 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800250 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700251 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800252
253 /*
254 * Transformations are applied in this order:
255 * 1) buffer orientation/flip/mirror
256 * 2) state transformation (window manager)
257 * 3) layer orientation (screen orientation)
258 * (NOTE: the matrices are multiplied in reverse order)
259 */
260 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700261 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800262 const ui::Transform bufferTransform{layerState.geomBufferTransform};
263 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
264
265 if (layerState.geomBufferUsesDisplayInverseTransform) {
266 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200267 * We must apply the internal display's inverse transform to the buffer
268 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800269 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200270 uint32_t invTransform = internalDisplayRotationFlags;
271
Lloyd Piquea83776c2019-01-29 18:42:32 -0800272 // calculate the inverse transform
273 if (invTransform & HAL_TRANSFORM_ROT_90) {
274 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
275 }
276
277 /*
278 * Here we cancel out the orientation component of the WM transform.
279 * The scaling and translate components are already included in our bounds
280 * computation so it's enough to just omit it in the composition.
281 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
282 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000283 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800284 }
285
286 // this gives us only the "orientation" component of the transform
287 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200288}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800289
Sally Qi0abc4a52024-09-26 16:13:06 -0700290void OutputLayer::updateLuts(
291 std::shared_ptr<gui::DisplayLuts> layerFEStateLut,
292 const std::optional<std::vector<std::optional<LutProperties>>>& properties) {
293 auto& state = editState();
294
295 if (!properties) {
296 // GPU composition if no Hwc Luts
297 state.forceClientComposition = true;
298 return;
299 }
300
301 std::vector<LutProperties> hwcLutProperties;
302 for (auto& p : *properties) {
303 if (p) {
304 hwcLutProperties.emplace_back(*p);
305 }
306 }
307
308 for (const auto& inputLut : layerFEStateLut->lutProperties) {
309 bool foundInHwcLuts = false;
310 for (const auto& hwcLut : hwcLutProperties) {
311 if (static_cast<int32_t>(hwcLut.dimension) ==
312 static_cast<int32_t>(inputLut.dimension) &&
313 hwcLut.size == inputLut.size &&
314 std::find(hwcLut.samplingKeys.begin(), hwcLut.samplingKeys.end(),
315 static_cast<LutProperties::SamplingKey>(inputLut.samplingKey)) !=
316 hwcLut.samplingKeys.end()) {
317 foundInHwcLuts = true;
318 break;
319 }
320 }
321 // if any lut properties of layerFEStateLut can not be found in hwcLutProperties,
322 // GPU composition instead
323 if (!foundInHwcLuts) {
324 state.forceClientComposition = true;
325 return;
326 }
327 }
328}
329
Snild Dolkow9e217d62020-04-22 15:53:42 +0200330void OutputLayer::updateCompositionState(
331 bool includeGeometry, bool forceClientComposition,
Sally Qi0abc4a52024-09-26 16:13:06 -0700332 ui::Transform::RotationFlags internalDisplayRotationFlags,
333 const std::optional<std::vector<std::optional<LutProperties>>> properties) {
Lloyd Piquede196652020-01-22 17:29:58 -0800334 const auto* layerFEState = getLayerFE().getCompositionState();
335 if (!layerFEState) {
336 return;
337 }
338
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700339 const auto& outputState = getOutput().getState();
340 const auto& profile = *getOutput().getDisplayColorProfile();
341 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800342
Lloyd Piquea83776c2019-01-29 18:42:32 -0800343 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700344 // Clear the forceClientComposition flag before it is set for any
345 // reason. Note that since it can be set by some checks below when
346 // updating the geometry state, we only clear it when updating the
347 // geometry since those conditions for forcing client composition won't
348 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700349 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700350
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700351 state.displayFrame = calculateOutputDisplayFrame();
ramindani2c043be2022-04-19 20:11:10 +0000352 state.sourceCrop = calculateOutputSourceCrop(internalDisplayRotationFlags);
Snild Dolkow9e217d62020-04-22 15:53:42 +0200353 state.bufferTransform = static_cast<Hwc2::Transform>(
354 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800355
Lloyd Piquede196652020-01-22 17:29:58 -0800356 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700357 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
358 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800359 }
360 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800361
Sally Qif6918d42023-08-07 15:28:30 -0700362 auto pixelFormat = layerFEState->buffer ? std::make_optional(static_cast<ui::PixelFormat>(
363 layerFEState->buffer->getPixelFormat()))
364 : std::nullopt;
365
366 auto hdrRenderType =
367 getHdrRenderType(outputState.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio);
368
Lloyd Piquef5275482019-01-29 18:42:42 -0800369 // Determine the output dependent dataspace for this layer. If it is
370 // colorspace agnostic, it just uses the dataspace chosen for the output to
371 // avoid the need for color conversion.
Alec Mouri88790f32023-07-21 01:25:14 +0000372 // For now, also respect the colorspace agnostic flag if we're drawing to HDR, to avoid drastic
373 // luminance shift. TODO(b/292162273): we should check if that's true though.
Sally Qif6918d42023-08-07 15:28:30 -0700374 state.dataspace = layerFEState->isColorspaceAgnostic && hdrRenderType == HdrRenderType::SDR
Alec Mouri88790f32023-07-21 01:25:14 +0000375 ? outputState.dataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800376 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800377
Alec Mouridda07d92022-04-25 22:39:25 +0000378 // Override the dataspace transfer from 170M to sRGB if the device configuration requests this.
379 // We do this here instead of in buffer info so that dumpsys can still report layers that are
380 // using the 170M transfer. Also we only do this if the colorspace is not agnostic for the
381 // layer, in case the color profile uses a 170M transfer function.
382 if (outputState.treat170mAsSrgb && !layerFEState->isColorspaceAgnostic &&
383 (state.dataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_SMPTE_170M) {
384 state.dataspace = static_cast<ui::Dataspace>(
385 (state.dataspace & HAL_DATASPACE_STANDARD_MASK) |
386 (state.dataspace & HAL_DATASPACE_RANGE_MASK) | HAL_DATASPACE_TRANSFER_SRGB);
387 }
388
Sally Qi8f9ed652023-08-17 15:30:29 -0700389 // re-get HdrRenderType after the dataspace gets changed.
390 hdrRenderType =
391 getHdrRenderType(state.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio);
392
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700393 // For hdr content, treat the white point as the display brightness - HDR content should not be
394 // boosted or dimmed.
Sally Qi81d95e62022-03-21 19:41:33 -0700395 // If the layer explicitly requests to disable dimming, then don't dim either.
Sally Qif6918d42023-08-07 15:28:30 -0700396 if (hdrRenderType == HdrRenderType::GENERIC_HDR ||
Alec Mourie8dd3562022-02-11 14:18:57 -0800397 getOutput().getState().displayBrightnessNits == getOutput().getState().sdrWhitePointNits ||
Sally Qi81d95e62022-03-21 19:41:33 -0700398 getOutput().getState().displayBrightnessNits == 0.f || !layerFEState->dimmingEnabled) {
Alec Mouri6da0e272022-02-07 12:45:57 -0800399 state.dimmingRatio = 1.f;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700400 state.whitePointNits = getOutput().getState().displayBrightnessNits;
401 } else {
John Reck68796592023-01-25 13:47:12 -0500402 float layerBrightnessNits = getOutput().getState().sdrWhitePointNits;
403 // RANGE_EXTENDED can "self-promote" to HDR, but is still rendered for a particular
404 // range that we may need to re-adjust to the current display conditions
Sally Qif6918d42023-08-07 15:28:30 -0700405 if (hdrRenderType == HdrRenderType::DISPLAY_HDR) {
Sally Qi963049b2023-03-23 14:06:21 -0700406 layerBrightnessNits *= layerFEState->currentHdrSdrRatio;
John Reck68796592023-01-25 13:47:12 -0500407 }
408 state.dimmingRatio =
409 std::clamp(layerBrightnessNits / getOutput().getState().displayBrightnessNits, 0.f,
410 1.f);
411 state.whitePointNits = layerBrightnessNits;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700412 }
413
Sally Qi0abc4a52024-09-26 16:13:06 -0700414 const auto& layerFEStateLut = layerFEState->luts;
415 if (layerFEStateLut) {
416 updateLuts(layerFEStateLut, properties);
417 }
418
Lloyd Piquef5275482019-01-29 18:42:42 -0800419 // These are evaluated every frame as they can potentially change at any
420 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800421 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700422 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700423 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800424 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425}
426
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600427void OutputLayer::commitPictureProfileToCompositionState() {
428 if (!com_android_graphics_libgui_flags_apply_picture_profiles()) {
429 return;
430 }
431 const auto* layerState = getLayerFE().getCompositionState();
432 if (layerState) {
433 editState().pictureProfileHandle = getLayerFE().getCompositionState()->pictureProfileHandle;
434 }
435}
436
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400437void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
438 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700439 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800440 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800442 return;
443 }
444
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700445 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800446 if (!hwcLayer) {
447 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700448 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800449 return;
450 }
451
Lloyd Piquede196652020-01-22 17:29:58 -0800452 const auto* outputIndependentState = getLayerFE().getCompositionState();
453 if (!outputIndependentState) {
454 return;
455 }
456
457 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800458
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500459 if (requestedCompositionType == Composition::SOLID_COLOR && state.overrideInfo.buffer) {
460 requestedCompositionType = Composition::DEVICE;
Alec Mouri028676a2021-12-02 15:01:48 -0800461 }
462
Yichi Chen413d46a2021-04-07 21:42:09 +0800463 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
464 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400465 const bool isOverridden =
466 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800467 const bool prevOverridden = state.hwc->stateOverridden;
468 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400469 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800470 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
471 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800472 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800473
Lloyd Piquef5275482019-01-29 18:42:42 -0800474 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800475 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
476 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800477
Alec Mourid1bf1b52021-05-05 18:44:58 -0700478 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
479 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700480
Sally Qi0abc4a52024-09-26 16:13:06 -0700481 writeLutToHWC(hwcLayer.get(), *outputIndependentState);
482
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500483 if (requestedCompositionType == Composition::SOLID_COLOR) {
Alec Mouri028676a2021-12-02 15:01:48 -0800484 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
485 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800486
487 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700488 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800489}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800490
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400491void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500492 Composition requestedCompositionType,
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400493 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800494 const auto& outputDependentState = getState();
495
Dan Stoza6166c312021-01-15 16:34:05 -0800496 Rect displayFrame = outputDependentState.displayFrame;
497 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700498
Alec Mouri464352b2021-03-24 16:33:21 -0700499 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800500 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'i50abed02022-05-24 02:24:33 +0000501 sourceCrop =
502 FloatRect(0.f, 0.f,
503 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
504 ->getWidth()),
505 static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer()
506 ->getHeight()));
Lloyd Piquef5275482019-01-29 18:42:42 -0800507 }
508
Dan Stoza6166c312021-01-15 16:34:05 -0800509 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
510 displayFrame.right, displayFrame.bottom);
511
512 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
513 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
514 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
515 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
516 }
517
518 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800519 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
520 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800521 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
522 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800523 }
524
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400525 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
526 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
527 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800528 }
529
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700530 // Solid-color layers and overridden buffers should always use an identity transform.
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500531 const auto bufferTransform = (requestedCompositionType != Composition::SOLID_COLOR &&
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700532 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800533 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700534 : static_cast<hal::Transform>(0);
535 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
536 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700537 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800538 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
539 static_cast<int32_t>(error));
540 }
541}
542
543void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800544 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
545 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400546 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
547 // PREMULTIPLIED so it will peek through.
548 const auto& overrideInfo = getState().overrideInfo;
549 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700550 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
551 : outputIndependentState.blendMode;
552 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700553 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700554 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800555 }
556
Alec Mouriee69a592021-03-23 15:00:45 -0700557 const float alpha = skipLayer
558 ? 0.0f
559 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800560 ALOGV("Writing alpha %f", alpha);
561
562 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
563 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
564 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800565 }
566
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800567 for (const auto& [name, entry] : outputIndependentState.metadata) {
568 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700569 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800570 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
571 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
572 }
573 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800574}
575
Sally Qi0abc4a52024-09-26 16:13:06 -0700576void OutputLayer::writeLutToHWC(HWC2::Layer* hwcLayer,
577 const LayerFECompositionState& outputIndependentState) {
578 if (!outputIndependentState.luts) {
579 return;
580 }
581 auto& lutFileDescriptor = outputIndependentState.luts->getLutFileDescriptor();
582 auto lutOffsets = outputIndependentState.luts->offsets;
583 auto& lutProperties = outputIndependentState.luts->lutProperties;
584
585 std::vector<LutProperties> aidlProperties;
586 aidlProperties.reserve(lutProperties.size());
587 for (size_t i = 0; i < lutOffsets.size(); i++) {
588 LutProperties properties;
589 properties.dimension = static_cast<LutProperties::Dimension>(lutProperties[i].dimension);
590 properties.size = lutProperties[i].size;
591 properties.samplingKeys = {
592 static_cast<LutProperties::SamplingKey>(lutProperties[i].samplingKey)};
593 aidlProperties.emplace_back(properties);
594 }
595
596 Luts luts;
597 luts.pfd = ndk::ScopedFileDescriptor(dup(lutFileDescriptor.get()));
598 luts.offsets = lutOffsets;
599 luts.lutProperties = std::move(aidlProperties);
600
601 switch (auto error = hwcLayer->setLuts(luts)) {
602 case hal::Error::NONE:
603 break;
604 default:
605 ALOGE("[%s] Failed to set Luts: %s (%d)", getLayerFE().getDebugName(),
606 to_string(error).c_str(), static_cast<int32_t>(error));
607 }
608}
609
Lloyd Piquef5275482019-01-29 18:42:42 -0800610void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
611 const auto& outputDependentState = getState();
612
Lloyd Piquea2468662019-03-07 21:31:06 -0800613 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800614 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700615 Region visibleRegion = outputDependentState.overrideInfo.buffer
616 ? Region(outputDependentState.overrideInfo.visibleRegion)
617 : outputDependentState.outputSpaceVisibleRegion;
618 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700619 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800620 to_string(error).c_str(), static_cast<int32_t>(error));
Leon Scroggins IIIf2b8ec42022-01-05 13:23:36 -0500621 visibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800622 }
623
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500624 if (auto error =
625 hwcLayer->setBlockingRegion(outputDependentState.outputSpaceBlockingRegionHint);
626 error != hal::Error::NONE) {
627 ALOGE("[%s] Failed to set blocking region: %s (%d)", getLayerFE().getDebugName(),
628 to_string(error).c_str(), static_cast<int32_t>(error));
629 outputDependentState.outputSpaceBlockingRegionHint.dump(LOG_TAG);
630 }
631
Alec Mourib7edfc22021-03-17 16:20:26 -0700632 const auto dataspace = outputDependentState.overrideInfo.buffer
633 ? outputDependentState.overrideInfo.dataspace
634 : outputDependentState.dataspace;
635
636 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
637 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
638 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800639 }
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700640
Alec Mourie8dd3562022-02-11 14:18:57 -0800641 // Cached layers are not dimmed, which means that composer should attempt to dim.
642 // Note that if the dimming ratio is large, then this may cause the cached layer
643 // to kick back into GPU composition :(
644 // Also note that this assumes that there are no HDR layers that are able to be cached.
645 // Otherwise, this could cause HDR layers to be dimmed twice.
646 const auto dimmingRatio = outputDependentState.overrideInfo.buffer
647 ? (getOutput().getState().displayBrightnessNits != 0.f
648 ? std::clamp(getOutput().getState().sdrWhitePointNits /
649 getOutput().getState().displayBrightnessNits,
650 0.f, 1.f)
651 : 1.f)
652 : outputDependentState.dimmingRatio;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700653
Alec Mouri6da0e272022-02-07 12:45:57 -0800654 if (auto error = hwcLayer->setBrightness(dimmingRatio); error != hal::Error::NONE) {
655 ALOGE("[%s] Failed to set brightness %f: %s (%d)", getLayerFE().getDebugName(),
656 dimmingRatio, to_string(error).c_str(), static_cast<int32_t>(error));
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700657 }
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600658
659 if (com_android_graphics_libgui_flags_apply_picture_profiles() &&
660 outputDependentState.pictureProfileHandle) {
661 if (auto error =
662 hwcLayer->setPictureProfileHandle(outputDependentState.pictureProfileHandle);
663 error != hal::Error::NONE) {
664 ALOGE("[%s] Failed to set picture profile handle: %s (%d)", getLayerFE().getDebugName(),
665 toString(outputDependentState.pictureProfileHandle).c_str(),
666 static_cast<int32_t>(error));
667 }
668 // Reset the picture profile state, as it needs to be re-committed on each present cycle
669 // when Output decides that the limited picture-processing hardware should be used by this
670 // layer.
671 editState().pictureProfileHandle = PictureProfileHandle::NONE;
672 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800673}
674
675void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700676 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500677 Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800678 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700679 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800680 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700681 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800682 editState().forceClientComposition = true;
683 break;
684 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700685 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800686 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800687 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800688
Alec Mouri464352b2021-03-24 16:33:21 -0700689 const Region& surfaceDamage = getState().overrideInfo.buffer
690 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700691 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
692 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700693
694 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700695 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800696 to_string(error).c_str(), static_cast<int32_t>(error));
697 outputIndependentState.surfaceDamage.dump(LOG_TAG);
698 }
699
700 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800701 switch (compositionType) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500702 case Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700703 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800704 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500705 case Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800706 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
707 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500708 case Composition::CURSOR:
709 case Composition::DEVICE:
Leon Scroggins III09c25412021-12-02 14:49:56 -0500710 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800711 case Composition::REFRESH_RATE_INDICATOR:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700712 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800713 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500714 case Composition::INVALID:
715 case Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800716 // Ignored
717 break;
718 }
719}
720
721void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
722 const LayerFECompositionState& outputIndependentState) {
Ady Abraham6e60b142022-01-06 18:10:35 -0800723 aidl::android::hardware::graphics::composer3::Color color = {outputIndependentState.color.r,
724 outputIndependentState.color.g,
725 outputIndependentState.color.b,
726 1.0f};
Lloyd Piquef5275482019-01-29 18:42:42 -0800727
Peiyong Line9d809e2020-04-14 13:10:48 -0700728 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700729 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800730 to_string(error).c_str(), static_cast<int32_t>(error));
731 }
732}
733
734void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
735 const LayerFECompositionState& outputIndependentState) {
736 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700737 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700738 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800739 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
740 static_cast<int32_t>(error));
741 }
742}
743
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700744void OutputLayer::uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700745 auto& state = editState();
746 // Skip doing this if there is no HWC interface
747 if (!state.hwc) {
748 return;
749 }
750
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700751 // Uncache the active buffer last so that it's the first buffer to be purged from the cache
752 // next time a buffer is sent to this layer.
753 bool uncacheActiveBuffer = false;
754
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700755 std::vector<uint32_t> slotsToClear;
756 for (uint64_t bufferId : bufferIdsToUncache) {
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700757 if (bufferId == state.hwc->activeBufferId) {
758 uncacheActiveBuffer = true;
759 } else {
760 uint32_t slot = state.hwc->hwcBufferCache.uncache(bufferId);
761 if (slot != UINT32_MAX) {
762 slotsToClear.push_back(slot);
763 }
Brian Lindahl90553da2022-12-06 13:36:30 -0700764 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700765 }
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700766 if (uncacheActiveBuffer) {
767 slotsToClear.push_back(state.hwc->hwcBufferCache.uncache(state.hwc->activeBufferId));
768 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700769
770 hal::Error error =
771 state.hwc->hwcLayer->setBufferSlotsToClear(slotsToClear, state.hwc->activeBufferSlot);
772 if (error != hal::Error::NONE) {
773 ALOGE("[%s] Failed to clear buffer slots: %s (%d)", getLayerFE().getDebugName(),
774 to_string(error).c_str(), static_cast<int32_t>(error));
775 }
Brian Lindahl439afad2022-11-14 11:16:55 -0700776}
777
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600778int64_t OutputLayer::getPictureProfilePriority() const {
779 const auto* layerState = getLayerFE().getCompositionState();
780 return layerState ? layerState->pictureProfilePriority : 0;
781}
782
783const PictureProfileHandle& OutputLayer::getPictureProfileHandle() const {
784 const auto* layerState = getLayerFE().getCompositionState();
785 return layerState ? layerState->pictureProfileHandle : PictureProfileHandle::NONE;
786}
787
Lloyd Piquef5275482019-01-29 18:42:42 -0800788void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700789 const LayerFECompositionState& outputIndependentState,
790 bool skipLayer) {
Huihong Luo5e161962023-08-18 14:26:32 -0700791 if (skipLayer && outputIndependentState.buffer == nullptr) {
792 return;
793 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800794 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700795 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800796 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
797 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700798 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700799 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800800 to_string(error).c_str(), static_cast<int32_t>(error));
801 }
802
Brian Lindahl439afad2022-11-14 11:16:55 -0700803 HwcSlotAndBuffer hwcSlotAndBuffer;
804 sp<Fence> hwcFence;
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700805 {
806 // Editing the state only because we update the HWC buffer cache and active buffer.
807 auto& state = editState();
808 // Override buffers use a special cache slot so that they don't evict client buffers.
809 if (state.overrideInfo.buffer != nullptr && !skipLayer) {
810 hwcSlotAndBuffer = state.hwc->hwcBufferCache.getOverrideHwcSlotAndBuffer(
811 state.overrideInfo.buffer->getBuffer());
812 hwcFence = state.overrideInfo.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700813 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
814 // slot will be used first, allowing the memory to be freed as soon as possible.
815 state.hwc->activeBufferId = state.overrideInfo.buffer->getBuffer()->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700816 } else {
817 hwcSlotAndBuffer =
818 state.hwc->hwcBufferCache.getHwcSlotAndBuffer(outputIndependentState.buffer);
819 hwcFence = outputIndependentState.acquireFence;
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700820 // Keep track of the active buffer ID so when it's discarded we uncache it last so its
821 // slot will be used first, allowing the memory to be freed as soon as possible.
822 state.hwc->activeBufferId = outputIndependentState.buffer->getId();
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700823 }
Brian Lindahlb158a5c2022-12-15 15:21:13 -0700824 // Keep track of the active buffer slot, so we can restore it after clearing other buffer
825 // slots.
Brian Lindahl3e1e1e62022-12-21 14:28:58 -0700826 state.hwc->activeBufferSlot = hwcSlotAndBuffer.slot;
Dan Stoza6166c312021-01-15 16:34:05 -0800827 }
828
Brian Lindahl439afad2022-11-14 11:16:55 -0700829 if (auto error = hwcLayer->setBuffer(hwcSlotAndBuffer.slot, hwcSlotAndBuffer.buffer, hwcFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700830 error != hal::Error::NONE) {
Brian Lindahl439afad2022-11-14 11:16:55 -0700831 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
832 hwcSlotAndBuffer.buffer->handle, to_string(error).c_str(),
833 static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800834 }
835}
836
Peiyong Line9d809e2020-04-14 13:10:48 -0700837void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500838 Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700839 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800840 auto& outputDependentState = editState();
841
Alec Mourid1bf1b52021-05-05 18:44:58 -0700842 if (isClientCompositionForced(isPeekingThrough)) {
843 // If we are forcing client composition, we need to tell the HWC
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500844 requestedCompositionType = Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800845 }
846
847 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700848 // We also resend the composition type when this layer was previously skipped, to ensure that
849 // the composition type is up-to-date.
850 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
851 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800852 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
853
Peiyong Line9d809e2020-04-14 13:10:48 -0700854 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
855 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700856 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500857 to_string(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800858 static_cast<int32_t>(error));
859 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800860 }
861}
862
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800863void OutputLayer::writeCursorPositionToHWC() const {
864 // Skip doing this if there is no HWC interface
865 auto hwcLayer = getHwcLayer();
866 if (!hwcLayer) {
867 return;
868 }
869
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600870 const auto* layerState = getLayerFE().getCompositionState();
871 if (!layerState) {
Lloyd Piquede196652020-01-22 17:29:58 -0800872 return;
873 }
874
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700875 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800876
Brian Lindahl1a4ffd82024-10-30 11:00:37 -0600877 Rect frame = layerState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000878 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800879 Rect position = outputState.transform.transform(frame);
880
881 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700882 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700883 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
884 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
885 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800886 }
887}
888
Lloyd Pique66d68602019-02-13 14:23:31 -0800889HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700890 const auto& state = getState();
891 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800892}
893
894bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700895 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500896 return !state.hwc || state.hwc->hwcCompositionType == Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800897}
898
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800899bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700900 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500901 return state.hwc && state.hwc->hwcCompositionType == Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800902}
903
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500904void OutputLayer::detectDisallowedCompositionTypeChange(Composition from, Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800905 bool result = false;
906 switch (from) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500907 case Composition::INVALID:
908 case Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800909 result = false;
910 break;
911
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500912 case Composition::DEVICE:
913 case Composition::SOLID_COLOR:
914 result = (to == Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800915 break;
916
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500917 case Composition::CURSOR:
918 case Composition::SIDEBAND:
Leon Scroggins7fd536f2021-12-28 14:43:12 +0000919 case Composition::DISPLAY_DECORATION:
ramindanic1945cb2023-02-03 13:28:15 -0800920 case Composition::REFRESH_RATE_INDICATOR:
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500921 result = (to == Composition::CLIENT || to == Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800922 break;
923 }
924
925 if (!result) {
926 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500927 getLayerFE().getDebugName(), to_string(from).c_str(), static_cast<int>(from),
928 to_string(to).c_str(), static_cast<int>(to));
Lloyd Pique66d68602019-02-13 14:23:31 -0800929 }
930}
931
Alec Mourid1bf1b52021-05-05 18:44:58 -0700932bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
933 return getState().forceClientComposition ||
934 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
935}
936
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500937void OutputLayer::applyDeviceCompositionTypeChange(Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700938 auto& state = editState();
939 LOG_FATAL_IF(!state.hwc);
940 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800941
Alec Mourid1bf1b52021-05-05 18:44:58 -0700942 // Only detected disallowed changes if this was not a skip layer, because the
943 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
944 // fewer GPU layers)
945 if (!hwcState.layerSkipped) {
946 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
947 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800948
949 hwcState.hwcCompositionType = compositionType;
950}
951
952void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700953 auto& state = editState();
954 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800955}
956
Peiyong Line9d809e2020-04-14 13:10:48 -0700957void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700958 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800959 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700960 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700961 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800962 break;
963
964 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700965 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800966 toString(request).c_str(), static_cast<int>(request));
967 break;
968 }
969}
970
Sally Qi0abc4a52024-09-26 16:13:06 -0700971void OutputLayer::applyDeviceLayerLut(
972 ndk::ScopedFileDescriptor lutFileDescriptor,
973 std::vector<std::pair<int, LutProperties>> lutOffsetsAndProperties) {
974 auto& state = editState();
975 LOG_FATAL_IF(!state.hwc);
976 auto& hwcState = *state.hwc;
977 std::vector<int32_t> offsets;
978 std::vector<int32_t> dimensions;
979 std::vector<int32_t> sizes;
980 std::vector<int32_t> samplingKeys;
981 for (const auto& [offset, properties] : lutOffsetsAndProperties) {
982 // The Lut(s) that comes back through CommandResultPayload should be
983 // only one sampling key.
984 if (properties.samplingKeys.size() == 1) {
985 offsets.emplace_back(offset);
986 dimensions.emplace_back(static_cast<int32_t>(properties.dimension));
987 sizes.emplace_back(static_cast<int32_t>(properties.size));
988 samplingKeys.emplace_back(static_cast<int32_t>(properties.samplingKeys[0]));
989 }
990 }
991 hwcState.luts = std::make_shared<gui::DisplayLuts>(base::unique_fd(lutFileDescriptor.release()),
992 std::move(offsets), std::move(dimensions),
993 std::move(sizes), std::move(samplingKeys));
Sally Qi95f669a2024-08-27 11:31:42 -0700994}
995
Lloyd Pique688abd42019-02-15 15:42:24 -0800996bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700997 const auto& state = getState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700998 const auto& sourceCrop = state.sourceCrop;
Alec Mourib6d78932023-09-20 16:05:42 +0000999 auto displayFrameWidth = static_cast<float>(state.displayFrame.getWidth());
1000 auto displayFrameHeight = static_cast<float>(state.displayFrame.getHeight());
1001
1002 if (state.bufferTransform & HAL_TRANSFORM_ROT_90) {
1003 std::swap(displayFrameWidth, displayFrameHeight);
1004 }
1005
1006 return sourceCrop.getHeight() != displayFrameHeight ||
1007 sourceCrop.getWidth() != displayFrameWidth;
Lloyd Pique688abd42019-02-15 15:42:24 -08001008}
1009
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001010std::optional<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionSettings() const {
Dan Stoza6166c312021-01-15 16:34:05 -08001011 if (getState().overrideInfo.buffer == nullptr) {
1012 return {};
1013 }
1014
Alec Mouri7be6c0a2021-03-19 15:22:01 -07001015 // Compute the geometry boundaries in layer stack space: we need to transform from the
1016 // framebuffer space of the override buffer to layer space.
1017 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
1018 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'i50abed02022-05-24 02:24:33 +00001019 const Rect boundaries = transform.transform(getState().overrideInfo.displayFrame);
Alec Mouri7be6c0a2021-03-19 15:22:01 -07001020
Dan Stoza6166c312021-01-15 16:34:05 -08001021 LayerFE::LayerSettings settings;
1022 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -07001023 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -08001024 };
Alec Mouria90a5702021-04-16 16:36:21 +00001025 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -07001026 settings.source = renderengine::PixelSource{
1027 .buffer = renderengine::Buffer{
1028 .buffer = getState().overrideInfo.buffer,
1029 .fence = getState().overrideInfo.acquireFence,
1030 // If the transform from layer space to display space contains a rotation, we
1031 // need to undo the rotation in the texture transform
1032 .textureTransform =
1033 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
1034 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -08001035 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -08001036 settings.alpha = 1.0f;
Alec Mourie8dd3562022-02-11 14:18:57 -08001037 settings.whitePointNits = getOutput().getState().sdrWhitePointNits;
Dan Stoza6166c312021-01-15 16:34:05 -08001038
Patrick Williams16d8b2c2022-08-08 17:29:05 +00001039 return settings;
Dan Stoza6166c312021-01-15 16:34:05 -08001040}
1041
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001042void OutputLayer::dump(std::string& out) const {
1043 using android::base::StringAppendF;
1044
Lloyd Piquede196652020-01-22 17:29:58 -08001045 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -07001046 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001047}
1048
Lloyd Piquecc01a452018-12-04 17:24:00 -08001049} // namespace impl
1050} // namespace android::compositionengine