blob: cf761835a6e7be7a6960fadf1dc84b358aabe59b [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>
Lloyd Piquecc01a452018-12-04 17:24:00 -080027
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080028// TODO(b/129481165): remove the #pragma below and fix conversion issues
29#pragma clang diagnostic push
30#pragma clang diagnostic ignored "-Wconversion"
31
Lloyd Pique07e33212018-12-18 16:33:37 -080032#include "DisplayHardware/HWComposer.h"
33
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080034// TODO(b/129481165): remove the #pragma below and fix conversion issues
35#pragma clang diagnostic pop // ignored "-Wconversion"
36
Lloyd Piquecc01a452018-12-04 17:24:00 -080037namespace android::compositionengine {
38
39OutputLayer::~OutputLayer() = default;
40
41namespace impl {
42
Lloyd Piquea83776c2019-01-29 18:42:32 -080043namespace {
44
45FloatRect reduce(const FloatRect& win, const Region& exclude) {
46 if (CC_LIKELY(exclude.isEmpty())) {
47 return win;
48 }
49 // Convert through Rect (by rounding) for lack of FloatRegion
50 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
51}
52
53} // namespace
54
Lloyd Piquede196652020-01-22 17:29:58 -080055std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
56 const sp<compositionengine::LayerFE>& layerFE) {
57 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080058}
59
Lloyd Piquecc01a452018-12-04 17:24:00 -080060OutputLayer::~OutputLayer() = default;
61
Lloyd Piquedf336d92019-03-07 21:38:42 -080062void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070063 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080064 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070065 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080066 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070067 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080068 }
Lloyd Pique07e33212018-12-18 16:33:37 -080069}
70
Lloyd Piquea83776c2019-01-29 18:42:32 -080071Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080072 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080073
74 // apply the projection's clipping to the window crop in
75 // layerstack space, and convert-back to layer space.
76 // if there are no window scaling involved, this operation will map to full
77 // pixels in the buffer.
78
79 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080080 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080081
Angel Aguayob084e0c2021-08-04 23:27:28 +000082 const Rect& viewport = getOutput().getState().layerStackSpace.getContent();
Lloyd Piquea83776c2019-01-29 18:42:32 -080083 const ui::Transform& layerTransform = layerState.geomLayerTransform;
84 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
85 // Transform to screen space.
86 activeCropFloat = layerTransform.transform(activeCropFloat);
87 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
88 // Back to layer space to work with the content crop.
89 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
90
91 // This needs to be here as transform.transform(Rect) computes the
92 // transformed rect and then takes the bounding box of the result before
93 // returning. This means
94 // transform.inverse().transform(transform.transform(Rect)) != Rect
95 // in which case we need to make sure the final rect is clipped to the
96 // display bounds.
97 Rect activeCrop{activeCropFloat};
98 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
99 activeCrop.clear();
100 }
101 return activeCrop;
102}
103
104FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800105 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700106 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800107
108 if (!layerState.geomUsesSourceCrop) {
109 return {};
110 }
111
112 // the content crop is the area of the content that gets scaled to the
113 // layer's size. This is in buffer space.
114 FloatRect crop = layerState.geomContentCrop.toFloatRect();
115
116 // In addition there is a WM-specified crop we pull from our drawing state.
117 Rect activeCrop = calculateInitialCrop();
118 const Rect& bufferSize = layerState.geomBufferSize;
119
120 int winWidth = bufferSize.getWidth();
121 int winHeight = bufferSize.getHeight();
122
123 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
124 // if display frame hasn't been set and the parent is an unbounded layer.
125 if (winWidth < 0 && winHeight < 0) {
126 return crop;
127 }
128
129 // Transform the window crop to match the buffer coordinate system,
130 // which means using the inverse of the current transform set on the
131 // SurfaceFlingerConsumer.
132 uint32_t invTransform = layerState.geomBufferTransform;
133 if (layerState.geomBufferUsesDisplayInverseTransform) {
134 /*
135 * the code below applies the primary display's inverse transform to the
136 * buffer
137 */
Marin Shalamanov68933fb2020-09-10 17:58:12 +0200138 uint32_t invTransformOrient =
Angel Aguayob084e0c2021-08-04 23:27:28 +0000139 ui::Transform::toRotationFlags(outputState.displaySpace.getOrientation());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800140 // calculate the inverse transform
141 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
142 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
143 }
144 // and apply to the current transform
145 invTransform =
146 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
147 }
148
149 if (invTransform & HAL_TRANSFORM_ROT_90) {
150 // If the activeCrop has been rotate the ends are rotated but not
151 // the space itself so when transforming ends back we can't rely on
152 // a modification of the axes of rotation. To account for this we
153 // need to reorient the inverse rotation in terms of the current
154 // axes of rotation.
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200155 bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
156 bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
157 if (isHFlipped == isVFlipped) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800158 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
159 }
160 std::swap(winWidth, winHeight);
161 }
162 const Rect winCrop =
163 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
164
165 // below, crop is intersected with winCrop expressed in crop's coordinate space
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200166 const float xScale = crop.getWidth() / float(winWidth);
167 const float yScale = crop.getHeight() / float(winHeight);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800168
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200169 const float insetLeft = winCrop.left * xScale;
170 const float insetTop = winCrop.top * yScale;
171 const float insetRight = (winWidth - winCrop.right) * xScale;
172 const float insetBottom = (winHeight - winCrop.bottom) * yScale;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800173
Marin Shalamanovcfeebd42020-05-15 15:23:49 +0200174 crop.left += insetLeft;
175 crop.top += insetTop;
176 crop.right -= insetRight;
177 crop.bottom -= insetBottom;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800178
179 return crop;
180}
181
182Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800183 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700184 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800185
186 // apply the layer's transform, followed by the display's global transform
187 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800188 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800189 const ui::Transform& layerTransform = layerState.geomLayerTransform;
190 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
191 const Rect& bufferSize = layerState.geomBufferSize;
192 Rect activeCrop = layerState.geomCrop;
193 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
194 activeCrop = layerTransform.transform(activeCrop);
Angel Aguayob084e0c2021-08-04 23:27:28 +0000195 if (!activeCrop.intersect(outputState.layerStackSpace.getContent(), &activeCrop)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800196 activeCrop.clear();
197 }
198 activeCrop = inverseLayerTransform.transform(activeCrop, true);
199 // This needs to be here as transform.transform(Rect) computes the
200 // transformed rect and then takes the bounding box of the result before
201 // returning. This means
202 // transform.inverse().transform(transform.transform(Rect)) != Rect
203 // in which case we need to make sure the final rect is clipped to the
204 // display bounds.
205 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
206 activeCrop.clear();
207 }
208 // mark regions outside the crop as transparent
209 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
210 activeTransparentRegion.orSelf(
211 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
212 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
213 activeTransparentRegion.orSelf(
214 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
215 }
216
217 // reduce uses a FloatRect to provide more accuracy during the
218 // transformation. We then round upon constructing 'frame'.
Leon Scroggins IIId394d3c2021-06-24 11:30:32 -0400219 FloatRect geomLayerBounds = layerState.geomLayerBounds;
220
221 // Some HWCs may clip client composited input to its displayFrame. Make sure
222 // that this does not cut off the shadow.
223 if (layerState.forceClientComposition && layerState.shadowRadius > 0.0f) {
224 const auto outset = layerState.shadowRadius;
225 geomLayerBounds.left -= outset;
226 geomLayerBounds.top -= outset;
227 geomLayerBounds.right += outset;
228 geomLayerBounds.bottom += outset;
229 }
230 Rect frame{layerTransform.transform(reduce(geomLayerBounds, activeTransparentRegion))};
Angel Aguayob084e0c2021-08-04 23:27:28 +0000231 if (!frame.intersect(outputState.layerStackSpace.getContent(), &frame)) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800232 frame.clear();
233 }
234 const ui::Transform displayTransform{outputState.transform};
235
236 return displayTransform.transform(frame);
237}
238
Snild Dolkow9e217d62020-04-22 15:53:42 +0200239uint32_t OutputLayer::calculateOutputRelativeBufferTransform(
240 uint32_t internalDisplayRotationFlags) const {
Lloyd Piquede196652020-01-22 17:29:58 -0800241 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700242 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800243
244 /*
245 * Transformations are applied in this order:
246 * 1) buffer orientation/flip/mirror
247 * 2) state transformation (window manager)
248 * 3) layer orientation (screen orientation)
249 * (NOTE: the matrices are multiplied in reverse order)
250 */
251 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700252 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800253 const ui::Transform bufferTransform{layerState.geomBufferTransform};
254 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
255
256 if (layerState.geomBufferUsesDisplayInverseTransform) {
257 /*
Snild Dolkow9e217d62020-04-22 15:53:42 +0200258 * We must apply the internal display's inverse transform to the buffer
259 * transform, and not the one for the output this layer is on.
Lloyd Piquea83776c2019-01-29 18:42:32 -0800260 */
Snild Dolkow9e217d62020-04-22 15:53:42 +0200261 uint32_t invTransform = internalDisplayRotationFlags;
262
Lloyd Piquea83776c2019-01-29 18:42:32 -0800263 // calculate the inverse transform
264 if (invTransform & HAL_TRANSFORM_ROT_90) {
265 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
266 }
267
268 /*
269 * Here we cancel out the orientation component of the WM transform.
270 * The scaling and translate components are already included in our bounds
271 * computation so it's enough to just omit it in the composition.
272 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
273 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000274 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800275 }
276
277 // this gives us only the "orientation" component of the transform
278 return transform.getOrientation();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200279}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800280
Snild Dolkow9e217d62020-04-22 15:53:42 +0200281void OutputLayer::updateCompositionState(
282 bool includeGeometry, bool forceClientComposition,
283 ui::Transform::RotationFlags internalDisplayRotationFlags) {
Lloyd Piquede196652020-01-22 17:29:58 -0800284 const auto* layerFEState = getLayerFE().getCompositionState();
285 if (!layerFEState) {
286 return;
287 }
288
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700289 const auto& outputState = getOutput().getState();
290 const auto& profile = *getOutput().getDisplayColorProfile();
291 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800292
Lloyd Piquea83776c2019-01-29 18:42:32 -0800293 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700294 // Clear the forceClientComposition flag before it is set for any
295 // reason. Note that since it can be set by some checks below when
296 // updating the geometry state, we only clear it when updating the
297 // geometry since those conditions for forcing client composition won't
298 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700299 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700300
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700301 state.displayFrame = calculateOutputDisplayFrame();
302 state.sourceCrop = calculateOutputSourceCrop();
Snild Dolkow9e217d62020-04-22 15:53:42 +0200303 state.bufferTransform = static_cast<Hwc2::Transform>(
304 calculateOutputRelativeBufferTransform(internalDisplayRotationFlags));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800305
Lloyd Piquede196652020-01-22 17:29:58 -0800306 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700307 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
308 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800309 }
310 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800311
312 // Determine the output dependent dataspace for this layer. If it is
313 // colorspace agnostic, it just uses the dataspace chosen for the output to
314 // avoid the need for color conversion.
Lloyd Piquede196652020-01-22 17:29:58 -0800315 state.dataspace = layerFEState->isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800316 outputState.targetDataspace != ui::Dataspace::UNKNOWN
317 ? outputState.targetDataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800318 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800319
Lloyd Piquef5275482019-01-29 18:42:42 -0800320 // These are evaluated every frame as they can potentially change at any
321 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800322 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700323 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700324 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800325 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800326}
327
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400328void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z,
329 bool zIsOverridden, bool isPeekingThrough) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700330 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800331 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700332 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800333 return;
334 }
335
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700336 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800337 if (!hwcLayer) {
338 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700339 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800340 return;
341 }
342
Lloyd Piquede196652020-01-22 17:29:58 -0800343 const auto* outputIndependentState = getLayerFE().getCompositionState();
344 if (!outputIndependentState) {
345 return;
346 }
347
348 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800349
Alec Mouri028676a2021-12-02 15:01:48 -0800350 if (requestedCompositionType == hal::Composition::SOLID_COLOR && state.overrideInfo.buffer) {
351 requestedCompositionType = hal::Composition::DEVICE;
352 }
353
Yichi Chen413d46a2021-04-07 21:42:09 +0800354 // TODO(b/181172795): We now update geometry for all flattened layers. We should update it
355 // only when the geometry actually changes
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400356 const bool isOverridden =
357 state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden;
Yichi Chen413d46a2021-04-07 21:42:09 +0800358 const bool prevOverridden = state.hwc->stateOverridden;
359 if (isOverridden || prevOverridden || skipLayer || includeGeometry) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400360 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z);
Dan Stoza6166c312021-01-15 16:34:05 -0800361 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState,
362 skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800363 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800364
Lloyd Piquef5275482019-01-29 18:42:42 -0800365 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Alec Mouri028676a2021-12-02 15:01:48 -0800366 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState,
367 requestedCompositionType, skipLayer);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800368
Alec Mourid1bf1b52021-05-05 18:44:58 -0700369 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough,
370 skipLayer);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700371
Alec Mouri028676a2021-12-02 15:01:48 -0800372 if (requestedCompositionType == hal::Composition::SOLID_COLOR) {
373 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
374 }
Yichi Chen413d46a2021-04-07 21:42:09 +0800375
376 editState().hwc->stateOverridden = isOverridden;
Alec Mourid1bf1b52021-05-05 18:44:58 -0700377 editState().hwc->layerSkipped = skipLayer;
Lloyd Piquef5275482019-01-29 18:42:42 -0800378}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800379
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400380void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer,
381 hal::Composition requestedCompositionType,
382 uint32_t z) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800383 const auto& outputDependentState = getState();
384
Dan Stoza6166c312021-01-15 16:34:05 -0800385 Rect displayFrame = outputDependentState.displayFrame;
386 FloatRect sourceCrop = outputDependentState.sourceCrop;
Huihong Luoa5825112021-03-24 12:28:29 -0700387
Alec Mouri464352b2021-03-24 16:33:21 -0700388 if (outputDependentState.overrideInfo.buffer != nullptr) {
Dan Stoza6166c312021-01-15 16:34:05 -0800389 displayFrame = outputDependentState.overrideInfo.displayFrame;
Wiwit Rifa'ie30a5852021-06-25 19:20:48 +0800390 sourceCrop = displayFrame.toFloatRect();
Lloyd Piquef5275482019-01-29 18:42:42 -0800391 }
392
Dan Stoza6166c312021-01-15 16:34:05 -0800393 ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top,
394 displayFrame.right, displayFrame.bottom);
395
396 if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) {
397 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
398 getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right,
399 displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
400 }
401
402 if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800403 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
404 "%s (%d)",
Dan Stoza6166c312021-01-15 16:34:05 -0800405 getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right,
406 sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800407 }
408
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400409 if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) {
410 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z,
411 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800412 }
413
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700414 // Solid-color layers and overridden buffers should always use an identity transform.
415 const auto bufferTransform = (requestedCompositionType != hal::Composition::SOLID_COLOR &&
416 getState().overrideInfo.buffer == nullptr)
Lloyd Piquef5275482019-01-29 18:42:42 -0800417 ? outputDependentState.bufferTransform
Peiyong Line9d809e2020-04-14 13:10:48 -0700418 : static_cast<hal::Transform>(0);
419 if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform));
420 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700421 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800422 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
423 static_cast<int32_t>(error));
424 }
425}
426
427void OutputLayer::writeOutputIndependentGeometryStateToHWC(
Dan Stoza6166c312021-01-15 16:34:05 -0800428 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
429 bool skipLayer) {
Leon Scroggins IIIe2ee0402021-04-02 16:59:37 -0400430 // If there is a peekThroughLayer, then this layer has a hole in it. We need to use
431 // PREMULTIPLIED so it will peek through.
432 const auto& overrideInfo = getState().overrideInfo;
433 const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer
Alec Mouriee69a592021-03-23 15:00:45 -0700434 ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED
435 : outputIndependentState.blendMode;
436 if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Alec Mouriee69a592021-03-23 15:00:45 -0700438 toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800439 }
440
Alec Mouriee69a592021-03-23 15:00:45 -0700441 const float alpha = skipLayer
442 ? 0.0f
443 : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha);
Dan Stoza6166c312021-01-15 16:34:05 -0800444 ALOGV("Writing alpha %f", alpha);
445
446 if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) {
447 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha,
448 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800449 }
450
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800451 for (const auto& [name, entry] : outputIndependentState.metadata) {
452 if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value);
Peiyong Line9d809e2020-04-14 13:10:48 -0700453 error != hal::Error::NONE) {
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800454 ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(),
455 name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error));
456 }
457 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800458}
459
460void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
461 const auto& outputDependentState = getState();
462
Lloyd Piquea2468662019-03-07 21:31:06 -0800463 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800464 // state and should not change every frame.
Alec Mouri464352b2021-03-24 16:33:21 -0700465 Region visibleRegion = outputDependentState.overrideInfo.buffer
466 ? Region(outputDependentState.overrideInfo.visibleRegion)
467 : outputDependentState.outputSpaceVisibleRegion;
468 if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700469 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800470 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800471 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800472 }
473
Alec Mourib7edfc22021-03-17 16:20:26 -0700474 const auto dataspace = outputDependentState.overrideInfo.buffer
475 ? outputDependentState.overrideInfo.dataspace
476 : outputDependentState.dataspace;
477
478 if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) {
479 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace,
480 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 }
482}
483
484void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
Alec Mouri96ca45c2021-06-09 17:32:26 -0700485 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState,
Alec Mouri028676a2021-12-02 15:01:48 -0800486 hal::Composition compositionType, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800487 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700488 case hal::Error::NONE:
Lloyd Piquef5275482019-01-29 18:42:42 -0800489 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700490 case hal::Error::UNSUPPORTED:
Lloyd Piquef5275482019-01-29 18:42:42 -0800491 editState().forceClientComposition = true;
492 break;
493 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700494 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800495 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800496 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800497
Alec Mouri464352b2021-03-24 16:33:21 -0700498 const Region& surfaceDamage = getState().overrideInfo.buffer
499 ? getState().overrideInfo.damageRegion
Alec Mourid1bf1b52021-05-05 18:44:58 -0700500 : (getState().hwc->stateOverridden ? Region::INVALID_REGION
501 : outputIndependentState.surfaceDamage);
Alec Mouri464352b2021-03-24 16:33:21 -0700502
503 if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700504 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800505 to_string(error).c_str(), static_cast<int32_t>(error));
506 outputIndependentState.surfaceDamage.dump(LOG_TAG);
507 }
508
509 // Content-specific per-frame state
Alec Mouri028676a2021-12-02 15:01:48 -0800510 switch (compositionType) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700511 case hal::Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700512 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800513 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700514 case hal::Composition::SIDEBAND:
Lloyd Piquef5275482019-01-29 18:42:42 -0800515 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
516 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700517 case hal::Composition::CURSOR:
518 case hal::Composition::DEVICE:
Alec Mouri96ca45c2021-06-09 17:32:26 -0700519 writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800520 break;
Peiyong Line9d809e2020-04-14 13:10:48 -0700521 case hal::Composition::INVALID:
522 case hal::Composition::CLIENT:
Lloyd Piquef5275482019-01-29 18:42:42 -0800523 // Ignored
524 break;
525 }
526}
527
528void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
529 const LayerFECompositionState& outputIndependentState) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700530 hal::Color color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
531 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
532 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
533 255};
Lloyd Piquef5275482019-01-29 18:42:42 -0800534
Peiyong Line9d809e2020-04-14 13:10:48 -0700535 if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700536 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800537 to_string(error).c_str(), static_cast<int32_t>(error));
538 }
539}
540
541void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
542 const LayerFECompositionState& outputIndependentState) {
543 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
Peiyong Line9d809e2020-04-14 13:10:48 -0700544 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700545 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800546 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
547 static_cast<int32_t>(error));
548 }
549}
550
551void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
Alec Mouri96ca45c2021-06-09 17:32:26 -0700552 const LayerFECompositionState& outputIndependentState,
553 bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800554 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700555 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800556 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
557 outputIndependentState.hdrMetadata);
Peiyong Line9d809e2020-04-14 13:10:48 -0700558 error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700559 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800560 to_string(error).c_str(), static_cast<int32_t>(error));
561 }
562
Dan Stoza6166c312021-01-15 16:34:05 -0800563 sp<GraphicBuffer> buffer = outputIndependentState.buffer;
564 sp<Fence> acquireFence = outputIndependentState.acquireFence;
Alec Mourie7cc1c22021-04-27 15:23:26 -0700565 int slot = outputIndependentState.bufferSlot;
Alec Mouri96ca45c2021-06-09 17:32:26 -0700566 if (getState().overrideInfo.buffer != nullptr && !skipLayer) {
Alec Mouria90a5702021-04-16 16:36:21 +0000567 buffer = getState().overrideInfo.buffer->getBuffer();
Dan Stoza6166c312021-01-15 16:34:05 -0800568 acquireFence = getState().overrideInfo.acquireFence;
Alec Mourie7cc1c22021-04-27 15:23:26 -0700569 slot = HwcBufferCache::FLATTENER_CACHING_SLOT;
Dan Stoza6166c312021-01-15 16:34:05 -0800570 }
571
572 ALOGV("Writing buffer %p", buffer.get());
573
Lloyd Piquef5275482019-01-29 18:42:42 -0800574 uint32_t hwcSlot = 0;
575 sp<GraphicBuffer> hwcBuffer;
576 // We need access to the output-dependent state for the buffer cache there,
577 // though otherwise the buffer is not output-dependent.
Alec Mourie7cc1c22021-04-27 15:23:26 -0700578 editState().hwc->hwcBufferCache.getHwcBuffer(slot, buffer, &hwcSlot, &hwcBuffer);
Lloyd Piquef5275482019-01-29 18:42:42 -0800579
Dan Stoza6166c312021-01-15 16:34:05 -0800580 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence);
Peiyong Line9d809e2020-04-14 13:10:48 -0700581 error != hal::Error::NONE) {
Dan Stoza6166c312021-01-15 16:34:05 -0800582 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(), buffer->handle,
583 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800584 }
585}
586
Peiyong Line9d809e2020-04-14 13:10:48 -0700587void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer,
Leon Scroggins III9aa25c22021-04-15 15:30:19 -0400588 hal::Composition requestedCompositionType,
Alec Mourid1bf1b52021-05-05 18:44:58 -0700589 bool isPeekingThrough, bool skipLayer) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800590 auto& outputDependentState = editState();
591
Alec Mourid1bf1b52021-05-05 18:44:58 -0700592 if (isClientCompositionForced(isPeekingThrough)) {
593 // If we are forcing client composition, we need to tell the HWC
Peiyong Line9d809e2020-04-14 13:10:48 -0700594 requestedCompositionType = hal::Composition::CLIENT;
Lloyd Piquef5275482019-01-29 18:42:42 -0800595 }
596
597 // Set the requested composition type with the HWC whenever it changes
Alec Mourid1bf1b52021-05-05 18:44:58 -0700598 // We also resend the composition type when this layer was previously skipped, to ensure that
599 // the composition type is up-to-date.
600 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType ||
601 (outputDependentState.hwc->layerSkipped && !skipLayer)) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800602 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
603
Peiyong Line9d809e2020-04-14 13:10:48 -0700604 if (auto error = hwcLayer->setCompositionType(requestedCompositionType);
605 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700606 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800607 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800608 static_cast<int32_t>(error));
609 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800610 }
611}
612
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800613void OutputLayer::writeCursorPositionToHWC() const {
614 // Skip doing this if there is no HWC interface
615 auto hwcLayer = getHwcLayer();
616 if (!hwcLayer) {
617 return;
618 }
619
Lloyd Piquede196652020-01-22 17:29:58 -0800620 const auto* layerFEState = getLayerFE().getCompositionState();
621 if (!layerFEState) {
622 return;
623 }
624
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700625 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800626
Lloyd Piquede196652020-01-22 17:29:58 -0800627 Rect frame = layerFEState->cursorFrame;
Angel Aguayob084e0c2021-08-04 23:27:28 +0000628 frame.intersect(outputState.layerStackSpace.getContent(), &frame);
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800629 Rect position = outputState.transform.transform(frame);
630
631 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
Peiyong Line9d809e2020-04-14 13:10:48 -0700632 error != hal::Error::NONE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700633 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
634 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
635 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800636 }
637}
638
Lloyd Pique66d68602019-02-13 14:23:31 -0800639HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700640 const auto& state = getState();
641 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800642}
643
644bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700645 const auto& state = getState();
Peiyong Line9d809e2020-04-14 13:10:48 -0700646 return !state.hwc || state.hwc->hwcCompositionType == hal::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800647}
648
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800649bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700650 const auto& state = getState();
Peiyong Line9d809e2020-04-14 13:10:48 -0700651 return state.hwc && state.hwc->hwcCompositionType == hal::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800652}
653
Peiyong Line9d809e2020-04-14 13:10:48 -0700654void OutputLayer::detectDisallowedCompositionTypeChange(hal::Composition from,
655 hal::Composition to) const {
Lloyd Pique66d68602019-02-13 14:23:31 -0800656 bool result = false;
657 switch (from) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700658 case hal::Composition::INVALID:
659 case hal::Composition::CLIENT:
Lloyd Pique66d68602019-02-13 14:23:31 -0800660 result = false;
661 break;
662
Peiyong Line9d809e2020-04-14 13:10:48 -0700663 case hal::Composition::DEVICE:
664 case hal::Composition::SOLID_COLOR:
665 result = (to == hal::Composition::CLIENT);
Lloyd Pique66d68602019-02-13 14:23:31 -0800666 break;
667
Peiyong Line9d809e2020-04-14 13:10:48 -0700668 case hal::Composition::CURSOR:
669 case hal::Composition::SIDEBAND:
670 result = (to == hal::Composition::CLIENT || to == hal::Composition::DEVICE);
Lloyd Pique66d68602019-02-13 14:23:31 -0800671 break;
672 }
673
674 if (!result) {
675 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700676 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800677 toString(to).c_str(), static_cast<int>(to));
678 }
679}
680
Alec Mourid1bf1b52021-05-05 18:44:58 -0700681bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const {
682 return getState().forceClientComposition ||
683 (!isPeekingThrough && getLayerFE().hasRoundedCorners());
684}
685
Peiyong Line9d809e2020-04-14 13:10:48 -0700686void OutputLayer::applyDeviceCompositionTypeChange(hal::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700687 auto& state = editState();
688 LOG_FATAL_IF(!state.hwc);
689 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800690
Alec Mourid1bf1b52021-05-05 18:44:58 -0700691 // Only detected disallowed changes if this was not a skip layer, because the
692 // validated composition type may be arbitrary (usually DEVICE, to reflect that there were
693 // fewer GPU layers)
694 if (!hwcState.layerSkipped) {
695 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
696 }
Lloyd Pique66d68602019-02-13 14:23:31 -0800697
698 hwcState.hwcCompositionType = compositionType;
699}
700
701void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700702 auto& state = editState();
703 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800704}
705
Peiyong Line9d809e2020-04-14 13:10:48 -0700706void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700707 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800708 switch (request) {
Peiyong Line9d809e2020-04-14 13:10:48 -0700709 case hal::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700710 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800711 break;
712
713 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700714 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800715 toString(request).c_str(), static_cast<int>(request));
716 break;
717 }
718}
719
Lloyd Pique688abd42019-02-15 15:42:24 -0800720bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700721 const auto& state = getState();
722 const auto& displayFrame = state.displayFrame;
723 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800724 return sourceCrop.getHeight() != displayFrame.getHeight() ||
725 sourceCrop.getWidth() != displayFrame.getWidth();
726}
727
Dan Stoza6166c312021-01-15 16:34:05 -0800728std::vector<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionList() const {
729 if (getState().overrideInfo.buffer == nullptr) {
730 return {};
731 }
732
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700733 // Compute the geometry boundaries in layer stack space: we need to transform from the
734 // framebuffer space of the override buffer to layer space.
735 const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace;
736 const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace);
Wiwit Rifa'ie30a5852021-06-25 19:20:48 +0800737 const Rect boundaries = transform.transform(getState().overrideInfo.displaySpace.getContent());
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700738
Dan Stoza6166c312021-01-15 16:34:05 -0800739 LayerFE::LayerSettings settings;
740 settings.geometry = renderengine::Geometry{
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700741 .boundaries = boundaries.toFloatRect(),
Dan Stoza6166c312021-01-15 16:34:05 -0800742 };
Alec Mouria90a5702021-04-16 16:36:21 +0000743 settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId();
Alec Mouri7be6c0a2021-03-19 15:22:01 -0700744 settings.source = renderengine::PixelSource{
745 .buffer = renderengine::Buffer{
746 .buffer = getState().overrideInfo.buffer,
747 .fence = getState().overrideInfo.acquireFence,
748 // If the transform from layer space to display space contains a rotation, we
749 // need to undo the rotation in the texture transform
750 .textureTransform =
751 ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(),
752 }};
Alec Mouri9c8fce02021-03-12 18:30:42 -0800753 settings.sourceDataspace = getState().overrideInfo.dataspace;
Dan Stoza6166c312021-01-15 16:34:05 -0800754 settings.alpha = 1.0f;
755
756 return {static_cast<LayerFE::LayerSettings>(settings)};
757}
758
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800759void OutputLayer::dump(std::string& out) const {
760 using android::base::StringAppendF;
761
Lloyd Piquede196652020-01-22 17:29:58 -0800762 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700763 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800764}
765
Lloyd Piquecc01a452018-12-04 17:24:00 -0800766} // namespace impl
767} // namespace android::compositionengine