blob: b010d9f96598aa728c45876f088f7d8371aaa7ca [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
Lloyd Piquecc01a452018-12-04 17:24:00 -080040namespace android::compositionengine {
41
42OutputLayer::~OutputLayer() = default;
43
44namespace impl {
45
Lloyd Piquea83776c2019-01-29 18:42:32 -080046namespace {
47
48FloatRect reduce(const FloatRect& win, const Region& exclude) {
49 if (CC_LIKELY(exclude.isEmpty())) {
50 return win;
51 }
52 // Convert through Rect (by rounding) for lack of FloatRegion
53 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
54}
55
56} // namespace
57
Lloyd Piquede196652020-01-22 17:29:58 -080058std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
59 const sp<compositionengine::LayerFE>& layerFE) {
60 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080061}
62
Lloyd Piquecc01a452018-12-04 17:24:00 -080063OutputLayer::~OutputLayer() = default;
64
Lloyd Piquedf336d92019-03-07 21:38:42 -080065void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070066 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080067 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070068 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080069 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070070 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080071 }
Lloyd Pique07e33212018-12-18 16:33:37 -080072}
73
Lloyd Piquea83776c2019-01-29 18:42:32 -080074Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080075 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080076
77 // apply the projection's clipping to the window crop in
78 // layerstack space, and convert-back to layer space.
79 // if there are no window scaling involved, this operation will map to full
80 // pixels in the buffer.
81
82 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080083 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080084
Angel Aguayob084e0c2021-08-04 23:27:28 +000085 const Rect& viewport = getOutput().getState().layerStackSpace.getContent();
Lloyd Piquea83776c2019-01-29 18:42:32 -080086 const ui::Transform& layerTransform = layerState.geomLayerTransform;
87 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
88 // Transform to screen space.
89 activeCropFloat = layerTransform.transform(activeCropFloat);
90 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
91 // Back to layer space to work with the content crop.
92 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
93
94 // This needs to be here as transform.transform(Rect) computes the
95 // transformed rect and then takes the bounding box of the result before
96 // returning. This means
97 // transform.inverse().transform(transform.transform(Rect)) != Rect
98 // in which case we need to make sure the final rect is clipped to the
99 // display bounds.
100 Rect activeCrop{activeCropFloat};
101 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
102 activeCrop.clear();
103 }
104 return activeCrop;
105}
106
107FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800108 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700109 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800110
111 if (!layerState.geomUsesSourceCrop) {
112 return {};
113 }
114
115 // the content crop is the area of the content that gets scaled to the
116 // layer's size. This is in buffer space.
117 FloatRect crop = layerState.geomContentCrop.toFloatRect();
118
119 // In addition there is a WM-specified crop we pull from our drawing state.
120 Rect activeCrop = calculateInitialCrop();
121 const Rect& bufferSize = layerState.geomBufferSize;
122
123 int winWidth = bufferSize.getWidth();
124 int winHeight = bufferSize.getHeight();
125
126 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
127 // if display frame hasn't been set and the parent is an unbounded layer.
128 if (winWidth < 0 && winHeight < 0) {
129 return crop;
130 }
131
132 // Transform the window crop to match the buffer coordinate system,
133 // which means using the inverse of the current transform set on the
134 // SurfaceFlingerConsumer.
135 uint32_t invTransform = layerState.geomBufferTransform;
136 if (layerState.geomBufferUsesDisplayInverseTransform) {
137 /*
138 * the code below applies the primary display's inverse transform to the
139 * buffer
140 */
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200141 uint32_t invTransformOrient =
Angel Aguayob084e0c2021-08-04 23:27:28 +0000142 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800143 // calculate the inverse transform
144 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
145 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
146 }
147 // and apply to the current transform
148 invTransform =
149 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
150 }
151
152 if (invTransform & HAL_TRANSFORM_ROT_90) {
153 // If the activeCrop has been rotate the ends are rotated but not
154 // the space itself so when transforming ends back we can't rely on
155 // a modification of the axes of rotation. To account for this we
156 // need to reorient the inverse rotation in terms of the current
157 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200158 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
159 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
160 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800161 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
162 }
163 std::swap(winWidth, winHeight);
164 }
165 const Rect winCrop =
166 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
167
168 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200169 const float xScale = crop.getWidth() / float(winWidth);
170 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800171
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200172 const float insetLeft = winCrop.left * xScale;
173 const float insetTop = winCrop.top * yScale;
174 const float insetRight = (winWidth - winCrop.right) * xScale;
175 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800176
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200177 crop.left += insetLeft;
178 crop.top += insetTop;
179 crop.right -= insetRight;
180 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800181
182 return crop;
183}
184
185Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800186 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700187 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800188
189 // apply the layer's transform, followed by the display's global transform
190 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800191 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800192 const ui::Transform& layerTransform = layerState.geomLayerTransform;
193 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
194 const Rect& bufferSize = layerState.geomBufferSize;
195 Rect activeCrop = layerState.geomCrop;
196 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
197 activeCrop = layerTransform.transform(activeCrop);
Angel Aguayob084e0c2021-08-04 23:27:28 +0000198 if (!activeCrop.intersect(outputState.layerStackSpace.getContent(), &activeCrop)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800199 activeCrop.clear();
200 }
201 activeCrop = inverseLayerTransform.transform(activeCrop, true);
202 // This needs to be here as transform.transform(Rect) computes the
203 // transformed rect and then takes the bounding box of the result before
204 // returning. This means
205 // transform.inverse().transform(transform.transform(Rect)) != Rect
206 // in which case we need to make sure the final rect is clipped to the
207 // display bounds.
208 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
209 activeCrop.clear();
210 }
211 // mark regions outside the crop as transparent
212 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
213 activeTransparentRegion.orSelf(
214 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
215 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
216 activeTransparentRegion.orSelf(
217 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
218 }
219
220 // reduce uses a FloatRect to provide more accuracy during the
221 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400222 FloatRect geomLayerBounds = layerState.geomLayerBounds;
223
224 // Some HWCs may clip client composited input to its displayFrame. Make sure
225 // that this does not cut off the shadow.
226 if (layerState.forceClientComposition && layerState.shadowRadius > 0.0f) {
227 const auto outset = layerState.shadowRadius;
228 geomLayerBounds.left -= outset;
229 geomLayerBounds.top -= outset;
230 geomLayerBounds.right += outset;
231 geomLayerBounds.bottom += outset;
232 }
233 Rect frame{layerTransform.transform(reduce(geomLayerBounds, activeTransparentRegion))};
Angel Aguayob084e0c2021-08-04 23:27:28 +0000234 if (!frame.intersect(outputState.layerStackSpace.getContent(), &frame)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800235 frame.clear();
236 }
237 const ui::Transform displayTransform{outputState.transform};
238
239 return displayTransform.transform(frame);
240}
241
Snild Dolkow9e217d62020-04-22 15:53:42 +0200242uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
243 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800244 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700245 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800246
247 /*
248 * Transformations are applied in this order:
249 * 1) buffer orientation/flip/mirror
250 * 2) state transformation (window manager)
251 * 3) layer orientation (screen orientation)
252 * (NOTE: the matrices are multiplied in reverse order)
253 */
254 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700255 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800256 const ui::Transform bufferTransform{layerState.geomBufferTransform};
257 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
258
259 if (layerState.geomBufferUsesDisplayInverseTransform) {
260 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200261 * We must apply the internal display's inverse transform to the buffer
262 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800263 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200264 uint32_t invTransform = internalDisplayRotationFlags;
265
Lloyd Piquea83776c2019-01-29 18:42:32 -0800266 // calculate the inverse transform
267 if (invTransform & HAL_TRANSFORM_ROT_90) {
268 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
269 }
270
271 /*
272 * Here we cancel out the orientation component of the WM transform.
273 * The scaling and translate components are already included in our bounds
274 * computation so it's enough to just omit it in the composition.
275 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
276 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000277 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800278 }
279
280 // this gives us only the "orientation" component of the transform
281 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200282}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800283
Snild Dolkow9e217d62020-04-22 15:53:42 +0200284void OutputLayer::updateCompositionState(
285 bool includeGeometry, bool forceClientComposition,
286 ui::Transform::RotationFlags internalDisplayRotationFlags) {
Lloyd Piquede196652020-01-22 17:29:58 -0800287 const auto* layerFEState = getLayerFE().getCompositionState();
288 if (!layerFEState) {
289 return;
290 }
291
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700292 const auto& outputState = getOutput().getState();
293 const auto& profile = *getOutput().getDisplayColorProfile();
294 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800295
Lloyd Piquea83776c2019-01-29 18:42:32 -0800296 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700297 // Clear the forceClientComposition flag before it is set for any
298 // reason. Note that since it can be set by some checks below when
299 // updating the geometry state, we only clear it when updating the
300 // geometry since those conditions for forcing client composition won't
301 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700302 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700303
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 state.displayFrame = calculateOutputDisplayFrame();
305 state.sourceCrop = calculateOutputSourceCrop();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200306 state.bufferTransform = static_cast<Hwc2::Transform>(
307 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800308
Lloyd Piquede196652020-01-22 17:29:58 -0800309 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
311 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800312 }
313 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800314
315 // Determine the output dependent dataspace for this layer. If it is
316 // colorspace agnostic, it just uses the dataspace chosen for the output to
317 // avoid the need for color conversion.
Lloyd Piquede196652020-01-22 17:29:58 -0800318 state.dataspace = layerFEState->isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800319 outputState.targetDataspace != ui::Dataspace::UNKNOWN
320 ? outputState.targetDataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800321 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800322
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700323 // For hdr content, treat the white point as the display brightness - HDR content should not be
324 // boosted or dimmed.
325 if (isHdrDataspace(state.dataspace)) {
326 state.whitePointNits = getOutput().getState().displayBrightnessNits;
327 } else {
328 state.whitePointNits = getOutput().getState().sdrWhitePointNits;
329 }
330
Lloyd Piquef5275482019-01-29 18:42:42 -0800331 // These are evaluated every frame as they can potentially change at any
332 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800333 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700334 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700335 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800336 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800337}
338
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400339void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
340 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700341 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800342 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700343 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800344 return;
345 }
346
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700347 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800348 if (!hwcLayer) {
349 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700350 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800351 return;
352 }
353
Lloyd Piquede196652020-01-22 17:29:58 -0800354 const auto* outputIndependentState = getLayerFE().getCompositionState();
355 if (!outputIndependentState) {
356 return;
357 }
358
359 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800360
Alec Mouri028676a2021-12-02 15:01:48 -0800361 if (requestedCompositionType == hal::Composition::SOLID_COLOR && state.overrideInfo.buffer) {
362 requestedCompositionType = hal::Composition::DEVICE;
363 }
364
Yichi Chen413d46a2021-04-07 21:42:09 +0800365 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
366 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400367 const bool isOverridden =
368 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800369 const bool prevOverridden = state.hwc->stateOverridden;
370 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400371 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800372 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
373 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800374 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800375
Lloyd Piquef5275482019-01-29 18:42:42 -0800376 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800377 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
378 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800379
Alec Mourid1bf1b52021-05-05 18:44:58 -0700380 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
381 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700382
Alec Mouri028676a2021-12-02 15:01:48 -0800383 if (requestedCompositionType == hal::Composition::SOLID_COLOR) {
384 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
385 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800386
387 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700388 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800389}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800390
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400391void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
392 hal::Composition requestedCompositionType,
393 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800394 const auto& outputDependentState = getState();
395
Dan Stoza6166c312021-01-15 16:34:05 -0800396 Rect displayFrame = outputDependentState.displayFrame;
397 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700398
Alec Mouri464352b2021-03-24 16:33:21 -0700399 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800400 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'ie30a5852021-06-25 19:20:48 +0800401 sourceCrop = displayFrame.toFloatRect();
Lloyd Piquef5275482019-01-29 18:42:42 -0800402 }
403
Dan Stoza6166c312021-01-15 16:34:05 -0800404 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
405 displayFrame.right, displayFrame.bottom);
406
407 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
408 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
409 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
410 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
411 }
412
413 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800414 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
415 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800416 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
417 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800418 }
419
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400420 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
421 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
422 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800423 }
424
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700425 // Solid-color layers and overridden buffers should always use an identity transform.
426 const auto bufferTransform = (requestedCompositionType != hal::Composition::SOLID_COLOR &&
427 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800428 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700429 : static_cast<hal::Transform>(0);
430 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
431 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700432 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800433 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
434 static_cast<int32_t>(error));
435 }
436}
437
438void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800439 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
440 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400441 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
442 // PREMULTIPLIED so it will peek through.
443 const auto& overrideInfo = getState().overrideInfo;
444 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700445 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
446 : outputIndependentState.blendMode;
447 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700448 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700449 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800450 }
451
Alec Mouriee69a592021-03-23 15:00:45 -0700452 const float alpha = skipLayer
453 ? 0.0f
454 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800455 ALOGV("Writing alpha %f", alpha);
456
457 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
458 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
459 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800460 }
461
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800462 for (const auto& [name, entry] : outputIndependentState.metadata) {
463 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700464 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800465 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
466 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
467 }
468 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800469}
470
471void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
472 const auto& outputDependentState = getState();
473
Lloyd Piquea2468662019-03-07 21:31:06 -0800474 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800475 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700476 Region visibleRegion = outputDependentState.overrideInfo.buffer
477 ? Region(outputDependentState.overrideInfo.visibleRegion)
478 : outputDependentState.outputSpaceVisibleRegion;
479 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700480 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800482 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800483 }
484
Alec Mourib7edfc22021-03-17 16:20:26 -0700485 const auto dataspace = outputDependentState.overrideInfo.buffer
486 ? outputDependentState.overrideInfo.dataspace
487 : outputDependentState.dataspace;
488
489 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
490 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
491 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 }
Alec Mouricdf6cbc2021-11-01 17:21:15 -0700493
494 // Don't dim cached layers
495 const auto whitePointNits = outputDependentState.overrideInfo.buffer
496 ? getOutput().getState().displayBrightnessNits
497 : outputDependentState.whitePointNits;
498
499 if (auto error = hwcLayer->setWhitePointNits(whitePointNits); error != hal::Error::NONE) {
500 ALOGE("[%s] Failed to set white point %f: %s (%d)", getLayerFE().getDebugName(),
501 whitePointNits, to_string(error).c_str(), static_cast<int32_t>(error));
502 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800503}
504
505void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700506 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Alec Mouri028676a2021-12-02 15:01:48 -0800507 hal::Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800508 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700509 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800510 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700511 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800512 editState().forceClientComposition = true;
513 break;
514 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700515 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800516 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800517 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800518
Alec Mouri464352b2021-03-24 16:33:21 -0700519 const Region& surfaceDamage = getState().overrideInfo.buffer
520 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700521 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
522 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700523
524 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700525 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800526 to_string(error).c_str(), static_cast<int32_t>(error));
527 outputIndependentState.surfaceDamage.dump(LOG_TAG);
528 }
529
530 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800531 switch (compositionType) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700532 case hal::Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700533 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800534 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700535 case hal::Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800536 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
537 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700538 case hal::Composition::CURSOR:
539 case hal::Composition::DEVICE:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700540 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800541 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700542 case hal::Composition::INVALID:
543 case hal::Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800544 // Ignored
545 break;
546 }
547}
548
549void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
550 const LayerFECompositionState& outputIndependentState) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700551 hal::Color color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
552 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
553 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
554 255};
Lloyd Piquef5275482019-01-29 18:42:42 -0800555
Peiyong Line9d809e2020-04-14 13:10:48 -0700556 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700557 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800558 to_string(error).c_str(), static_cast<int32_t>(error));
559 }
560}
561
562void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
563 const LayerFECompositionState& outputIndependentState) {
564 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700565 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700566 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800567 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
568 static_cast<int32_t>(error));
569 }
570}
571
572void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700573 const LayerFECompositionState& outputIndependentState,
574 bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800575 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700576 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800577 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
578 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700579 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700580 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800581 to_string(error).c_str(), static_cast<int32_t>(error));
582 }
583
Dan Stoza6166c312021-01-15 16:34:05 -0800584 sp<GraphicBuffer> buffer = outputIndependentState.buffer;
585 sp<Fence> acquireFence = outputIndependentState.acquireFence;
Alec Mourie7cc1c22021-04-27 15:23:26 -0700586 int slot = outputIndependentState.bufferSlot;
Alec Mouri96ca45c2021-06-09 17:32:26 -0700587 if (getState().overrideInfo.buffer != nullptr && !skipLayer) {
Alec Mouria90a5702021-04-16 16:36:21 +0000588 buffer = getState().overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800589 acquireFence = getState().overrideInfo.acquireFence;
Alec Mourie7cc1c22021-04-27 15:23:26 -0700590 slot = HwcBufferCache::FLATTENER_CACHING_SLOT;
Dan Stoza6166c312021-01-15 16:34:05 -0800591 }
592
593 ALOGV("Writing buffer %p", buffer.get());
594
Lloyd Piquef5275482019-01-29 18:42:42 -0800595 uint32_t hwcSlot = 0;
596 sp<GraphicBuffer> hwcBuffer;
597 // We need access to the output-dependent state for the buffer cache there,
598 // though otherwise the buffer is not output-dependent.
Alec Mourie7cc1c22021-04-27 15:23:26 -0700599 editState().hwc->hwcBufferCache.getHwcBuffer(slot, buffer, &hwcSlot, &hwcBuffer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800600
Dan Stoza6166c312021-01-15 16:34:05 -0800601 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700602 error != hal::Error::NONE) {
Dan Stoza6166c312021-01-15 16:34:05 -0800603 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(), buffer->handle,
604 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800605 }
606}
607
Peiyong Line9d809e2020-04-14 13:10:48 -0700608void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400609 hal::Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700610 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800611 auto& outputDependentState = editState();
612
Alec Mourid1bf1b52021-05-05 18:44:58 -0700613 if (isClientCompositionForced(isPeekingThrough)) {
614 // If we are forcing client composition, we need to tell the HWC
Peiyong Line9d809e2020-04-14 13:10:48 -0700615 requestedCompositionType = hal::Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800616 }
617
618 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700619 // We also resend the composition type when this layer was previously skipped, to ensure that
620 // the composition type is up-to-date.
621 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
622 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800623 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
624
Peiyong Line9d809e2020-04-14 13:10:48 -0700625 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
626 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700627 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800628 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800629 static_cast<int32_t>(error));
630 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800631 }
632}
633
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800634void OutputLayer::writeCursorPositionToHWC() const {
635 // Skip doing this if there is no HWC interface
636 auto hwcLayer = getHwcLayer();
637 if (!hwcLayer) {
638 return;
639 }
640
Lloyd Piquede196652020-01-22 17:29:58 -0800641 const auto* layerFEState = getLayerFE().getCompositionState();
642 if (!layerFEState) {
643 return;
644 }
645
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700646 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800647
Lloyd Piquede196652020-01-22 17:29:58 -0800648 Rect frame = layerFEState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000649 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800650 Rect position = outputState.transform.transform(frame);
651
652 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700653 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700654 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
655 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
656 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800657 }
658}
659
Lloyd Pique66d68602019-02-13 14:23:31 -0800660HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700661 const auto& state = getState();
662 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800663}
664
665bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700666 const auto& state = getState();
Peiyong Line9d809e2020-04-14 13:10:48 -0700667 return !state.hwc || state.hwc->hwcCompositionType == hal::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800668}
669
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800670bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700671 const auto& state = getState();
Peiyong Line9d809e2020-04-14 13:10:48 -0700672 return state.hwc && state.hwc->hwcCompositionType == hal::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800673}
674
Peiyong Line9d809e2020-04-14 13:10:48 -0700675void OutputLayer::detectDisallowedCompositionTypeChange(hal::Composition from,
676 hal::Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800677 bool result = false;
678 switch (from) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700679 case hal::Composition::INVALID:
680 case hal::Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800681 result = false;
682 break;
683
Peiyong Line9d809e2020-04-14 13:10:48 -0700684 case hal::Composition::DEVICE:
685 case hal::Composition::SOLID_COLOR:
686 result = (to == hal::Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800687 break;
688
Peiyong Line9d809e2020-04-14 13:10:48 -0700689 case hal::Composition::CURSOR:
690 case hal::Composition::SIDEBAND:
691 result = (to == hal::Composition::CLIENT || to == hal::Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800692 break;
693 }
694
695 if (!result) {
696 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700697 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800698 toString(to).c_str(), static_cast<int>(to));
699 }
700}
701
Alec Mourid1bf1b52021-05-05 18:44:58 -0700702bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
703 return getState().forceClientComposition ||
704 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
705}
706
Peiyong Line9d809e2020-04-14 13:10:48 -0700707void OutputLayer::applyDeviceCompositionTypeChange(hal::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700708 auto& state = editState();
709 LOG_FATAL_IF(!state.hwc);
710 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800711
Alec Mourid1bf1b52021-05-05 18:44:58 -0700712 // Only detected disallowed changes if this was not a skip layer, because the
713 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
714 // fewer GPU layers)
715 if (!hwcState.layerSkipped) {
716 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
717 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800718
719 hwcState.hwcCompositionType = compositionType;
720}
721
722void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700723 auto& state = editState();
724 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800725}
726
Peiyong Line9d809e2020-04-14 13:10:48 -0700727void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700728 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800729 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700730 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700731 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800732 break;
733
734 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700735 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800736 toString(request).c_str(), static_cast<int>(request));
737 break;
738 }
739}
740
Lloyd Pique688abd42019-02-15 15:42:24 -0800741bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700742 const auto& state = getState();
743 const auto& displayFrame = state.displayFrame;
744 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800745 return sourceCrop.getHeight() != displayFrame.getHeight() ||
746 sourceCrop.getWidth() != displayFrame.getWidth();
747}
748
Dan Stoza6166c312021-01-15 16:34:05 -0800749std::vector<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionList() const {
750 if (getState().overrideInfo.buffer == nullptr) {
751 return {};
752 }
753
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700754 // Compute the geometry boundaries in layer stack space: we need to transform from the
755 // framebuffer space of the override buffer to layer space.
756 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
757 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'ie30a5852021-06-25 19:20:48 +0800758 const Rect boundaries = transform.transform(getState().overrideInfo.displaySpace.getContent());
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700759
Dan Stoza6166c312021-01-15 16:34:05 -0800760 LayerFE::LayerSettings settings;
761 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700762 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -0800763 };
Alec Mouria90a5702021-04-16 16:36:21 +0000764 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700765 settings.source = renderengine::PixelSource{
766 .buffer = renderengine::Buffer{
767 .buffer = getState().overrideInfo.buffer,
768 .fence = getState().overrideInfo.acquireFence,
769 // If the transform from layer space to display space contains a rotation, we
770 // need to undo the rotation in the texture transform
771 .textureTransform =
772 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
773 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -0800774 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -0800775 settings.alpha = 1.0f;
776
777 return {static_cast<LayerFE::LayerSettings>(settings)};
778}
779
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800780void OutputLayer::dump(std::string& out) const {
781 using android::base::StringAppendF;
782
Lloyd Piquede196652020-01-22 17:29:58 -0800783 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700784 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800785}
786
Lloyd Piquecc01a452018-12-04 17:24:00 -0800787} // namespace impl
788} // namespace android::compositionengine