blob: 3289d55870b1500dfa8a678f88a9cd7aca069484 [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
109FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800110 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700111 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800112
113 if (!layerState.geomUsesSourceCrop) {
114 return {};
115 }
116
117 // the content crop is the area of the content that gets scaled to the
118 // layer's size. This is in buffer space.
119 FloatRect crop = layerState.geomContentCrop.toFloatRect();
120
121 // In addition there is a WM-specified crop we pull from our drawing state.
122 Rect activeCrop = calculateInitialCrop();
123 const Rect& bufferSize = layerState.geomBufferSize;
124
125 int winWidth = bufferSize.getWidth();
126 int winHeight = bufferSize.getHeight();
127
128 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
129 // if display frame hasn't been set and the parent is an unbounded layer.
130 if (winWidth < 0 && winHeight < 0) {
131 return crop;
132 }
133
134 // Transform the window crop to match the buffer coordinate system,
135 // which means using the inverse of the current transform set on the
136 // SurfaceFlingerConsumer.
137 uint32_t invTransform = layerState.geomBufferTransform;
138 if (layerState.geomBufferUsesDisplayInverseTransform) {
139 /*
140 * the code below applies the primary display's inverse transform to the
141 * buffer
142 */
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200143 uint32_t invTransformOrient =
Angel Aguayob084e0c2021-08-04 23:27:28 +0000144 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800145 // calculate the inverse transform
146 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
147 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
148 }
149 // and apply to the current transform
150 invTransform =
151 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
152 }
153
154 if (invTransform & HAL_TRANSFORM_ROT_90) {
155 // If the activeCrop has been rotate the ends are rotated but not
156 // the space itself so when transforming ends back we can't rely on
157 // a modification of the axes of rotation. To account for this we
158 // need to reorient the inverse rotation in terms of the current
159 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200160 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
161 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
162 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800163 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
164 }
165 std::swap(winWidth, winHeight);
166 }
167 const Rect winCrop =
168 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
169
170 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200171 const float xScale = crop.getWidth() / float(winWidth);
172 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800173
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200174 const float insetLeft = winCrop.left * xScale;
175 const float insetTop = winCrop.top * yScale;
176 const float insetRight = (winWidth - winCrop.right) * xScale;
177 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800178
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200179 crop.left += insetLeft;
180 crop.top += insetTop;
181 crop.right -= insetRight;
182 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800183
184 return crop;
185}
186
187Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800188 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700189 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800190
191 // apply the layer's transform, followed by the display's global transform
192 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800193 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800194 const ui::Transform& layerTransform = layerState.geomLayerTransform;
195 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
196 const Rect& bufferSize = layerState.geomBufferSize;
197 Rect activeCrop = layerState.geomCrop;
198 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
199 activeCrop = layerTransform.transform(activeCrop);
Angel Aguayob084e0c2021-08-04 23:27:28 +0000200 if (!activeCrop.intersect(outputState.layerStackSpace.getContent(), &activeCrop)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800201 activeCrop.clear();
202 }
203 activeCrop = inverseLayerTransform.transform(activeCrop, true);
204 // This needs to be here as transform.transform(Rect) computes the
205 // transformed rect and then takes the bounding box of the result before
206 // returning. This means
207 // transform.inverse().transform(transform.transform(Rect)) != Rect
208 // in which case we need to make sure the final rect is clipped to the
209 // display bounds.
210 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
211 activeCrop.clear();
212 }
213 // mark regions outside the crop as transparent
214 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
215 activeTransparentRegion.orSelf(
216 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
217 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
218 activeTransparentRegion.orSelf(
219 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
220 }
221
222 // reduce uses a FloatRect to provide more accuracy during the
223 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400224 FloatRect geomLayerBounds = layerState.geomLayerBounds;
225
226 // Some HWCs may clip client composited input to its displayFrame. Make sure
227 // that this does not cut off the shadow.
228 if (layerState.forceClientComposition && layerState.shadowRadius > 0.0f) {
229 const auto outset = layerState.shadowRadius;
230 geomLayerBounds.left -= outset;
231 geomLayerBounds.top -= outset;
232 geomLayerBounds.right += outset;
233 geomLayerBounds.bottom += outset;
234 }
235 Rect frame{layerTransform.transform(reduce(geomLayerBounds, activeTransparentRegion))};
Angel Aguayob084e0c2021-08-04 23:27:28 +0000236 if (!frame.intersect(outputState.layerStackSpace.getContent(), &frame)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800237 frame.clear();
238 }
239 const ui::Transform displayTransform{outputState.transform};
240
241 return displayTransform.transform(frame);
242}
243
Snild Dolkow9e217d62020-04-22 15:53:42 +0200244uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
245 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800246 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700247 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800248
249 /*
250 * Transformations are applied in this order:
251 * 1) buffer orientation/flip/mirror
252 * 2) state transformation (window manager)
253 * 3) layer orientation (screen orientation)
254 * (NOTE: the matrices are multiplied in reverse order)
255 */
256 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700257 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800258 const ui::Transform bufferTransform{layerState.geomBufferTransform};
259 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
260
261 if (layerState.geomBufferUsesDisplayInverseTransform) {
262 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200263 * We must apply the internal display's inverse transform to the buffer
264 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800265 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200266 uint32_t invTransform = internalDisplayRotationFlags;
267
Lloyd Piquea83776c2019-01-29 18:42:32 -0800268 // calculate the inverse transform
269 if (invTransform & HAL_TRANSFORM_ROT_90) {
270 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
271 }
272
273 /*
274 * Here we cancel out the orientation component of the WM transform.
275 * The scaling and translate components are already included in our bounds
276 * computation so it's enough to just omit it in the composition.
277 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
278 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000279 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800280 }
281
282 // this gives us only the "orientation" component of the transform
283 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200284}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800285
Snild Dolkow9e217d62020-04-22 15:53:42 +0200286void OutputLayer::updateCompositionState(
287 bool includeGeometry, bool forceClientComposition,
288 ui::Transform::RotationFlags internalDisplayRotationFlags) {
Lloyd Piquede196652020-01-22 17:29:58 -0800289 const auto* layerFEState = getLayerFE().getCompositionState();
290 if (!layerFEState) {
291 return;
292 }
293
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700294 const auto& outputState = getOutput().getState();
295 const auto& profile = *getOutput().getDisplayColorProfile();
296 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800297
Lloyd Piquea83776c2019-01-29 18:42:32 -0800298 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700299 // Clear the forceClientComposition flag before it is set for any
300 // reason. Note that since it can be set by some checks below when
301 // updating the geometry state, we only clear it when updating the
302 // geometry since those conditions for forcing client composition won't
303 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700305
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700306 state.displayFrame = calculateOutputDisplayFrame();
307 state.sourceCrop = calculateOutputSourceCrop();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200308 state.bufferTransform = static_cast<Hwc2::Transform>(
309 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800310
Lloyd Piquede196652020-01-22 17:29:58 -0800311 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700312 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
313 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800314 }
315 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800316
317 // Determine the output dependent dataspace for this layer. If it is
318 // colorspace agnostic, it just uses the dataspace chosen for the output to
319 // avoid the need for color conversion.
Lloyd Piquede196652020-01-22 17:29:58 -0800320 state.dataspace = layerFEState->isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800321 outputState.targetDataspace != ui::Dataspace::UNKNOWN
322 ? outputState.targetDataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800323 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800324
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700325 // For hdr content, treat the white point as the display brightness - HDR content should not be
326 // boosted or dimmed.
Sally Qi81d95e62022-03-21 19:41:33 -0700327 // If the layer explicitly requests to disable dimming, then don't dim either.
Alec Mouri6da0e272022-02-07 12:45:57 -0800328 if (isHdrDataspace(state.dataspace) ||
Alec Mourie8dd3562022-02-11 14:18:57 -0800329 getOutput().getState().displayBrightnessNits == getOutput().getState().sdrWhitePointNits ||
Sally Qi81d95e62022-03-21 19:41:33 -0700330 getOutput().getState().displayBrightnessNits == 0.f || !layerFEState->dimmingEnabled) {
Alec Mouri6da0e272022-02-07 12:45:57 -0800331 state.dimmingRatio = 1.f;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700332 state.whitePointNits = getOutput().getState().displayBrightnessNits;
333 } else {
Alec Mouri6da0e272022-02-07 12:45:57 -0800334 state.dimmingRatio = std::clamp(getOutput().getState().sdrWhitePointNits /
335 getOutput().getState().displayBrightnessNits,
336 0.f, 1.f);
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700337 state.whitePointNits = getOutput().getState().sdrWhitePointNits;
338 }
339
Lloyd Piquef5275482019-01-29 18:42:42 -0800340 // These are evaluated every frame as they can potentially change at any
341 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800342 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700343 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700344 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800345 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800346}
347
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400348void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
349 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700350 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800351 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700352 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800353 return;
354 }
355
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700356 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800357 if (!hwcLayer) {
358 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700359 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800360 return;
361 }
362
Lloyd Piquede196652020-01-22 17:29:58 -0800363 const auto* outputIndependentState = getLayerFE().getCompositionState();
364 if (!outputIndependentState) {
365 return;
366 }
367
368 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800369
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500370 if (requestedCompositionType == Composition::SOLID_COLOR && state.overrideInfo.buffer) {
371 requestedCompositionType = Composition::DEVICE;
Alec Mouri028676a2021-12-02 15:01:48 -0800372 }
373
Yichi Chen413d46a2021-04-07 21:42:09 +0800374 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
375 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400376 const bool isOverridden =
377 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800378 const bool prevOverridden = state.hwc->stateOverridden;
379 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400380 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800381 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
382 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800383 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800384
Lloyd Piquef5275482019-01-29 18:42:42 -0800385 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800386 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
387 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800388
Alec Mourid1bf1b52021-05-05 18:44:58 -0700389 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
390 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700391
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500392 if (requestedCompositionType == Composition::SOLID_COLOR) {
Alec Mouri028676a2021-12-02 15:01:48 -0800393 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
394 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800395
396 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700397 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800398}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800399
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400400void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500401 Composition requestedCompositionType,
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400402 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800403 const auto& outputDependentState = getState();
404
Dan Stoza6166c312021-01-15 16:34:05 -0800405 Rect displayFrame = outputDependentState.displayFrame;
406 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700407
Alec Mouri464352b2021-03-24 16:33:21 -0700408 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800409 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'ie30a5852021-06-25 19:20:48 +0800410 sourceCrop = displayFrame.toFloatRect();
Lloyd Piquef5275482019-01-29 18:42:42 -0800411 }
412
Dan Stoza6166c312021-01-15 16:34:05 -0800413 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
414 displayFrame.right, displayFrame.bottom);
415
416 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
417 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
418 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
419 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
420 }
421
422 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800423 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
424 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800425 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
426 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800427 }
428
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400429 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
430 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
431 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800432 }
433
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700434 // Solid-color layers and overridden buffers should always use an identity transform.
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500435 const auto bufferTransform = (requestedCompositionType != Composition::SOLID_COLOR &&
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700436 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800437 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700438 : static_cast<hal::Transform>(0);
439 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
440 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800442 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
443 static_cast<int32_t>(error));
444 }
445}
446
447void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800448 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
449 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400450 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
451 // PREMULTIPLIED so it will peek through.
452 const auto& overrideInfo = getState().overrideInfo;
453 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700454 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
455 : outputIndependentState.blendMode;
456 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700457 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700458 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800459 }
460
Alec Mouriee69a592021-03-23 15:00:45 -0700461 const float alpha = skipLayer
462 ? 0.0f
463 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800464 ALOGV("Writing alpha %f", alpha);
465
466 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
467 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
468 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800469 }
470
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800471 for (const auto& [name, entry] : outputIndependentState.metadata) {
472 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700473 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800474 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
475 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
476 }
477 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800478}
479
480void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
481 const auto& outputDependentState = getState();
482
Lloyd Piquea2468662019-03-07 21:31:06 -0800483 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800484 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700485 Region visibleRegion = outputDependentState.overrideInfo.buffer
486 ? Region(outputDependentState.overrideInfo.visibleRegion)
487 : outputDependentState.outputSpaceVisibleRegion;
488 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700489 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800490 to_string(error).c_str(), static_cast<int32_t>(error));
Leon Scroggins IIIf2b8ec42022-01-05 13:23:36 -0500491 visibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 }
493
Leon Scroggins III9a0afda2022-01-11 16:53:09 -0500494 if (auto error =
495 hwcLayer->setBlockingRegion(outputDependentState.outputSpaceBlockingRegionHint);
496 error != hal::Error::NONE) {
497 ALOGE("[%s] Failed to set blocking region: %s (%d)", getLayerFE().getDebugName(),
498 to_string(error).c_str(), static_cast<int32_t>(error));
499 outputDependentState.outputSpaceBlockingRegionHint.dump(LOG_TAG);
500 }
501
Alec Mourib7edfc22021-03-17 16:20:26 -0700502 const auto dataspace = outputDependentState.overrideInfo.buffer
503 ? outputDependentState.overrideInfo.dataspace
504 : outputDependentState.dataspace;
505
506 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
507 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
508 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800509 }
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700510
Alec Mourie8dd3562022-02-11 14:18:57 -0800511 // Cached layers are not dimmed, which means that composer should attempt to dim.
512 // Note that if the dimming ratio is large, then this may cause the cached layer
513 // to kick back into GPU composition :(
514 // Also note that this assumes that there are no HDR layers that are able to be cached.
515 // Otherwise, this could cause HDR layers to be dimmed twice.
516 const auto dimmingRatio = outputDependentState.overrideInfo.buffer
517 ? (getOutput().getState().displayBrightnessNits != 0.f
518 ? std::clamp(getOutput().getState().sdrWhitePointNits /
519 getOutput().getState().displayBrightnessNits,
520 0.f, 1.f)
521 : 1.f)
522 : outputDependentState.dimmingRatio;
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700523
Alec Mouri6da0e272022-02-07 12:45:57 -0800524 if (auto error = hwcLayer->setBrightness(dimmingRatio); error != hal::Error::NONE) {
525 ALOGE("[%s] Failed to set brightness %f: %s (%d)", getLayerFE().getDebugName(),
526 dimmingRatio, to_string(error).c_str(), static_cast<int32_t>(error));
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700527 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800528}
529
530void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700531 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500532 Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800533 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700534 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800535 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700536 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800537 editState().forceClientComposition = true;
538 break;
539 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700540 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800541 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800542 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800543
Alec Mouri464352b2021-03-24 16:33:21 -0700544 const Region& surfaceDamage = getState().overrideInfo.buffer
545 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700546 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
547 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700548
549 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700550 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800551 to_string(error).c_str(), static_cast<int32_t>(error));
552 outputIndependentState.surfaceDamage.dump(LOG_TAG);
553 }
554
555 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800556 switch (compositionType) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500557 case Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700558 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800559 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500560 case Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800561 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
562 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500563 case Composition::CURSOR:
564 case Composition::DEVICE:
Leon Scroggins III09c25412021-12-02 14:49:56 -0500565 case Composition::DISPLAY_DECORATION:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700566 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800567 break;
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500568 case Composition::INVALID:
569 case Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800570 // Ignored
571 break;
572 }
573}
574
575void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
576 const LayerFECompositionState& outputIndependentState) {
Ady Abraham6e60b142022-01-06 18:10:35 -0800577 aidl::android::hardware::graphics::composer3::Color color = {outputIndependentState.color.r,
578 outputIndependentState.color.g,
579 outputIndependentState.color.b,
580 1.0f};
Lloyd Piquef5275482019-01-29 18:42:42 -0800581
Peiyong Line9d809e2020-04-14 13:10:48 -0700582 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700583 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800584 to_string(error).c_str(), static_cast<int32_t>(error));
585 }
586}
587
588void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
589 const LayerFECompositionState& outputIndependentState) {
590 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700591 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700592 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800593 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
594 static_cast<int32_t>(error));
595 }
596}
597
598void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700599 const LayerFECompositionState& outputIndependentState,
600 bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800601 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700602 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800603 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
604 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700605 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700606 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800607 to_string(error).c_str(), static_cast<int32_t>(error));
608 }
609
Dan Stoza6166c312021-01-15 16:34:05 -0800610 sp<GraphicBuffer> buffer = outputIndependentState.buffer;
611 sp<Fence> acquireFence = outputIndependentState.acquireFence;
Alec Mourie7cc1c22021-04-27 15:23:26 -0700612 int slot = outputIndependentState.bufferSlot;
Alec Mouri96ca45c2021-06-09 17:32:26 -0700613 if (getState().overrideInfo.buffer != nullptr && !skipLayer) {
Alec Mouria90a5702021-04-16 16:36:21 +0000614 buffer = getState().overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800615 acquireFence = getState().overrideInfo.acquireFence;
Alec Mourie7cc1c22021-04-27 15:23:26 -0700616 slot = HwcBufferCache::FLATTENER_CACHING_SLOT;
Dan Stoza6166c312021-01-15 16:34:05 -0800617 }
618
619 ALOGV("Writing buffer %p", buffer.get());
620
Lloyd Piquef5275482019-01-29 18:42:42 -0800621 uint32_t hwcSlot = 0;
622 sp<GraphicBuffer> hwcBuffer;
623 // We need access to the output-dependent state for the buffer cache there,
624 // though otherwise the buffer is not output-dependent.
Alec Mourie7cc1c22021-04-27 15:23:26 -0700625 editState().hwc->hwcBufferCache.getHwcBuffer(slot, buffer, &hwcSlot, &hwcBuffer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800626
Dan Stoza6166c312021-01-15 16:34:05 -0800627 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700628 error != hal::Error::NONE) {
Dan Stoza6166c312021-01-15 16:34:05 -0800629 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(), buffer->handle,
630 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800631 }
632}
633
Peiyong Line9d809e2020-04-14 13:10:48 -0700634void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500635 Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700636 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800637 auto& outputDependentState = editState();
638
Alec Mourid1bf1b52021-05-05 18:44:58 -0700639 if (isClientCompositionForced(isPeekingThrough)) {
640 // If we are forcing client composition, we need to tell the HWC
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500641 requestedCompositionType = Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800642 }
643
644 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700645 // We also resend the composition type when this layer was previously skipped, to ensure that
646 // the composition type is up-to-date.
647 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
648 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800649 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
650
Peiyong Line9d809e2020-04-14 13:10:48 -0700651 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
652 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700653 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500654 to_string(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800655 static_cast<int32_t>(error));
656 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800657 }
658}
659
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800660void OutputLayer::writeCursorPositionToHWC() const {
661 // Skip doing this if there is no HWC interface
662 auto hwcLayer = getHwcLayer();
663 if (!hwcLayer) {
664 return;
665 }
666
Lloyd Piquede196652020-01-22 17:29:58 -0800667 const auto* layerFEState = getLayerFE().getCompositionState();
668 if (!layerFEState) {
669 return;
670 }
671
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700672 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800673
Lloyd Piquede196652020-01-22 17:29:58 -0800674 Rect frame = layerFEState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000675 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800676 Rect position = outputState.transform.transform(frame);
677
678 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700679 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700680 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
681 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
682 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800683 }
684}
685
Lloyd Pique66d68602019-02-13 14:23:31 -0800686HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700687 const auto& state = getState();
688 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800689}
690
691bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700692 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500693 return !state.hwc || state.hwc->hwcCompositionType == Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800694}
695
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800696bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700697 const auto& state = getState();
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500698 return state.hwc && state.hwc->hwcCompositionType == Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800699}
700
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500701void OutputLayer::detectDisallowedCompositionTypeChange(Composition from, Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800702 bool result = false;
703 switch (from) {
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500704 case Composition::INVALID:
705 case Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800706 result = false;
707 break;
708
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500709 case Composition::DEVICE:
710 case Composition::SOLID_COLOR:
711 result = (to == Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800712 break;
713
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500714 case Composition::CURSOR:
715 case Composition::SIDEBAND:
Leon Scroggins7fd536f2021-12-28 14:43:12 +0000716 case Composition::DISPLAY_DECORATION:
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500717 result = (to == Composition::CLIENT || to == Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800718 break;
719 }
720
721 if (!result) {
722 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500723 getLayerFE().getDebugName(), to_string(from).c_str(), static_cast<int>(from),
724 to_string(to).c_str(), static_cast<int>(to));
Lloyd Pique66d68602019-02-13 14:23:31 -0800725 }
726}
727
Alec Mourid1bf1b52021-05-05 18:44:58 -0700728bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
729 return getState().forceClientComposition ||
730 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
731}
732
Leon Scroggins III2e1aa182021-12-01 17:33:12 -0500733void OutputLayer::applyDeviceCompositionTypeChange(Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700734 auto& state = editState();
735 LOG_FATAL_IF(!state.hwc);
736 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800737
Alec Mourid1bf1b52021-05-05 18:44:58 -0700738 // Only detected disallowed changes if this was not a skip layer, because the
739 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
740 // fewer GPU layers)
741 if (!hwcState.layerSkipped) {
742 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
743 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800744
745 hwcState.hwcCompositionType = compositionType;
746}
747
748void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700749 auto& state = editState();
750 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800751}
752
Peiyong Line9d809e2020-04-14 13:10:48 -0700753void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700754 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800755 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700756 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700757 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800758 break;
759
760 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700761 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800762 toString(request).c_str(), static_cast<int>(request));
763 break;
764 }
765}
766
Lloyd Pique688abd42019-02-15 15:42:24 -0800767bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700768 const auto& state = getState();
769 const auto& displayFrame = state.displayFrame;
770 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800771 return sourceCrop.getHeight() != displayFrame.getHeight() ||
772 sourceCrop.getWidth() != displayFrame.getWidth();
773}
774
Dan Stoza6166c312021-01-15 16:34:05 -0800775std::vector<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionList() const {
776 if (getState().overrideInfo.buffer == nullptr) {
777 return {};
778 }
779
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700780 // Compute the geometry boundaries in layer stack space: we need to transform from the
781 // framebuffer space of the override buffer to layer space.
782 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
783 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'ie30a5852021-06-25 19:20:48 +0800784 const Rect boundaries = transform.transform(getState().overrideInfo.displaySpace.getContent());
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700785
Dan Stoza6166c312021-01-15 16:34:05 -0800786 LayerFE::LayerSettings settings;
787 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700788 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -0800789 };
Alec Mouria90a5702021-04-16 16:36:21 +0000790 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700791 settings.source = renderengine::PixelSource{
792 .buffer = renderengine::Buffer{
793 .buffer = getState().overrideInfo.buffer,
794 .fence = getState().overrideInfo.acquireFence,
795 // If the transform from layer space to display space contains a rotation, we
796 // need to undo the rotation in the texture transform
797 .textureTransform =
798 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
799 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -0800800 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -0800801 settings.alpha = 1.0f;
Alec Mourie8dd3562022-02-11 14:18:57 -0800802 settings.whitePointNits = getOutput().getState().sdrWhitePointNits;
Dan Stoza6166c312021-01-15 16:34:05 -0800803
804 return {static_cast<LayerFE::LayerSettings>(settings)};
805}
806
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800807void OutputLayer::dump(std::string& out) const {
808 using android::base::StringAppendF;
809
Lloyd Piquede196652020-01-22 17:29:58 -0800810 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700811 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800812}
813
Lloyd Piquecc01a452018-12-04 17:24:00 -0800814} // namespace impl
815} // namespace android::compositionengine