blob: b538d7520f175cbd975684e65a160d2607cc3be2 [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
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080017#include <android-base/stringprintf.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080018#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080019#include <compositionengine/LayerFE.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>
Lloyd Piquea83776c2019-01-29 18:42:32 -080022#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080024#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080025
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080026// TODO(b/129481165): remove the #pragma below and fix conversion issues
27#pragma clang diagnostic push
28#pragma clang diagnostic ignored "-Wconversion"
29
Lloyd Pique07e33212018-12-18 16:33:37 -080030#include "DisplayHardware/HWComposer.h"
31
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080032// TODO(b/129481165): remove the #pragma below and fix conversion issues
33#pragma clang diagnostic pop // ignored "-Wconversion"
34
Lloyd Piquecc01a452018-12-04 17:24:00 -080035namespace android::compositionengine {
36
37OutputLayer::~OutputLayer() = default;
38
39namespace impl {
40
Lloyd Piquea83776c2019-01-29 18:42:32 -080041namespace {
42
43FloatRect reduce(const FloatRect& win, const Region& exclude) {
44 if (CC_LIKELY(exclude.isEmpty())) {
45 return win;
46 }
47 // Convert through Rect (by rounding) for lack of FloatRegion
48 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
49}
50
51} // namespace
52
Lloyd Piquede196652020-01-22 17:29:58 -080053std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output,
54 const sp<compositionengine::LayerFE>& layerFE) {
55 return createOutputLayerTemplated<OutputLayer>(output, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080056}
57
Lloyd Piquecc01a452018-12-04 17:24:00 -080058OutputLayer::~OutputLayer() = default;
59
Lloyd Piquedf336d92019-03-07 21:38:42 -080060void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070061 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080062 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070063 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080064 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070065 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080066 }
Lloyd Pique07e33212018-12-18 16:33:37 -080067}
68
Lloyd Piquea83776c2019-01-29 18:42:32 -080069Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -080070 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080071
72 // apply the projection's clipping to the window crop in
73 // layerstack space, and convert-back to layer space.
74 // if there are no window scaling involved, this operation will map to full
75 // pixels in the buffer.
76
77 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080078 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080079
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070080 const Rect& viewport = getOutput().getState().viewport;
Lloyd Piquea83776c2019-01-29 18:42:32 -080081 const ui::Transform& layerTransform = layerState.geomLayerTransform;
82 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
83 // Transform to screen space.
84 activeCropFloat = layerTransform.transform(activeCropFloat);
85 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
86 // Back to layer space to work with the content crop.
87 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
88
89 // This needs to be here as transform.transform(Rect) computes the
90 // transformed rect and then takes the bounding box of the result before
91 // returning. This means
92 // transform.inverse().transform(transform.transform(Rect)) != Rect
93 // in which case we need to make sure the final rect is clipped to the
94 // display bounds.
95 Rect activeCrop{activeCropFloat};
96 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
97 activeCrop.clear();
98 }
99 return activeCrop;
100}
101
102FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800103 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700104 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800105
106 if (!layerState.geomUsesSourceCrop) {
107 return {};
108 }
109
110 // the content crop is the area of the content that gets scaled to the
111 // layer's size. This is in buffer space.
112 FloatRect crop = layerState.geomContentCrop.toFloatRect();
113
114 // In addition there is a WM-specified crop we pull from our drawing state.
115 Rect activeCrop = calculateInitialCrop();
116 const Rect& bufferSize = layerState.geomBufferSize;
117
118 int winWidth = bufferSize.getWidth();
119 int winHeight = bufferSize.getHeight();
120
121 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
122 // if display frame hasn't been set and the parent is an unbounded layer.
123 if (winWidth < 0 && winHeight < 0) {
124 return crop;
125 }
126
127 // Transform the window crop to match the buffer coordinate system,
128 // which means using the inverse of the current transform set on the
129 // SurfaceFlingerConsumer.
130 uint32_t invTransform = layerState.geomBufferTransform;
131 if (layerState.geomBufferUsesDisplayInverseTransform) {
132 /*
133 * the code below applies the primary display's inverse transform to the
134 * buffer
135 */
136 uint32_t invTransformOrient = outputState.orientation;
137 // calculate the inverse transform
138 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
139 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
140 }
141 // and apply to the current transform
142 invTransform =
143 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
144 }
145
146 if (invTransform & HAL_TRANSFORM_ROT_90) {
147 // If the activeCrop has been rotate the ends are rotated but not
148 // the space itself so when transforming ends back we can't rely on
149 // a modification of the axes of rotation. To account for this we
150 // need to reorient the inverse rotation in terms of the current
151 // axes of rotation.
152 bool is_h_flipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
153 bool is_v_flipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
154 if (is_h_flipped == is_v_flipped) {
155 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
156 }
157 std::swap(winWidth, winHeight);
158 }
159 const Rect winCrop =
160 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
161
162 // below, crop is intersected with winCrop expressed in crop's coordinate space
163 float xScale = crop.getWidth() / float(winWidth);
164 float yScale = crop.getHeight() / float(winHeight);
165
166 float insetL = winCrop.left * xScale;
167 float insetT = winCrop.top * yScale;
168 float insetR = (winWidth - winCrop.right) * xScale;
169 float insetB = (winHeight - winCrop.bottom) * yScale;
170
171 crop.left += insetL;
172 crop.top += insetT;
173 crop.right -= insetR;
174 crop.bottom -= insetB;
175
176 return crop;
177}
178
179Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800180 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700181 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800182
183 // apply the layer's transform, followed by the display's global transform
184 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800185 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800186 const ui::Transform& layerTransform = layerState.geomLayerTransform;
187 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
188 const Rect& bufferSize = layerState.geomBufferSize;
189 Rect activeCrop = layerState.geomCrop;
190 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
191 activeCrop = layerTransform.transform(activeCrop);
192 if (!activeCrop.intersect(outputState.viewport, &activeCrop)) {
193 activeCrop.clear();
194 }
195 activeCrop = inverseLayerTransform.transform(activeCrop, true);
196 // This needs to be here as transform.transform(Rect) computes the
197 // transformed rect and then takes the bounding box of the result before
198 // returning. This means
199 // transform.inverse().transform(transform.transform(Rect)) != Rect
200 // in which case we need to make sure the final rect is clipped to the
201 // display bounds.
202 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
203 activeCrop.clear();
204 }
205 // mark regions outside the crop as transparent
206 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
207 activeTransparentRegion.orSelf(
208 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
209 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
210 activeTransparentRegion.orSelf(
211 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
212 }
213
214 // reduce uses a FloatRect to provide more accuracy during the
215 // transformation. We then round upon constructing 'frame'.
216 Rect frame{
217 layerTransform.transform(reduce(layerState.geomLayerBounds, activeTransparentRegion))};
218 if (!frame.intersect(outputState.viewport, &frame)) {
219 frame.clear();
220 }
221 const ui::Transform displayTransform{outputState.transform};
222
223 return displayTransform.transform(frame);
224}
225
226uint32_t OutputLayer::calculateOutputRelativeBufferTransform() const {
Lloyd Piquede196652020-01-22 17:29:58 -0800227 const auto& layerState = *getLayerFE().getCompositionState();
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700228 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800229
230 /*
231 * Transformations are applied in this order:
232 * 1) buffer orientation/flip/mirror
233 * 2) state transformation (window manager)
234 * 3) layer orientation (screen orientation)
235 * (NOTE: the matrices are multiplied in reverse order)
236 */
237 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700238 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800239 const ui::Transform bufferTransform{layerState.geomBufferTransform};
240 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
241
242 if (layerState.geomBufferUsesDisplayInverseTransform) {
243 /*
244 * the code below applies the primary display's inverse transform to the
245 * buffer
246 */
247 uint32_t invTransform = outputState.orientation;
248 // calculate the inverse transform
249 if (invTransform & HAL_TRANSFORM_ROT_90) {
250 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
251 }
252
253 /*
254 * Here we cancel out the orientation component of the WM transform.
255 * The scaling and translate components are already included in our bounds
256 * computation so it's enough to just omit it in the composition.
257 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
258 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000259 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800260 }
261
262 // this gives us only the "orientation" component of the transform
263 return transform.getOrientation();
264} // namespace impl
265
Lloyd Pique7a234912019-10-03 11:54:27 -0700266void OutputLayer::updateCompositionState(bool includeGeometry, bool forceClientComposition) {
Lloyd Piquede196652020-01-22 17:29:58 -0800267 const auto* layerFEState = getLayerFE().getCompositionState();
268 if (!layerFEState) {
269 return;
270 }
271
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700272 const auto& outputState = getOutput().getState();
273 const auto& profile = *getOutput().getDisplayColorProfile();
274 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800275
Lloyd Piquea83776c2019-01-29 18:42:32 -0800276 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700277 // Clear the forceClientComposition flag before it is set for any
278 // reason. Note that since it can be set by some checks below when
279 // updating the geometry state, we only clear it when updating the
280 // geometry since those conditions for forcing client composition won't
281 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700282 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700283
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700284 state.displayFrame = calculateOutputDisplayFrame();
285 state.sourceCrop = calculateOutputSourceCrop();
286 state.bufferTransform =
Lloyd Piquea83776c2019-01-29 18:42:32 -0800287 static_cast<Hwc2::Transform>(calculateOutputRelativeBufferTransform());
288
Lloyd Piquede196652020-01-22 17:29:58 -0800289 if ((layerFEState->isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700290 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
291 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800292 }
293 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800294
295 // Determine the output dependent dataspace for this layer. If it is
296 // colorspace agnostic, it just uses the dataspace chosen for the output to
297 // avoid the need for color conversion.
Lloyd Piquede196652020-01-22 17:29:58 -0800298 state.dataspace = layerFEState->isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800299 outputState.targetDataspace != ui::Dataspace::UNKNOWN
300 ? outputState.targetDataspace
Lloyd Piquede196652020-01-22 17:29:58 -0800301 : layerFEState->dataspace;
Lloyd Piquef5275482019-01-29 18:42:42 -0800302
Lloyd Piquef5275482019-01-29 18:42:42 -0800303 // These are evaluated every frame as they can potentially change at any
304 // time.
Lloyd Piquede196652020-01-22 17:29:58 -0800305 if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
Lloyd Pique7a234912019-10-03 11:54:27 -0700306 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700307 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800308 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800309}
310
Lloyd Piquef5275482019-01-29 18:42:42 -0800311void OutputLayer::writeStateToHWC(bool includeGeometry) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700312 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800313 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700314 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800315 return;
316 }
317
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700318 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800319 if (!hwcLayer) {
320 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700321 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800322 return;
323 }
324
Lloyd Piquede196652020-01-22 17:29:58 -0800325 const auto* outputIndependentState = getLayerFE().getCompositionState();
326 if (!outputIndependentState) {
327 return;
328 }
329
330 auto requestedCompositionType = outputIndependentState->compositionType;
Lloyd Piquef5275482019-01-29 18:42:42 -0800331
Lloyd Piquea83776c2019-01-29 18:42:32 -0800332 if (includeGeometry) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800333 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType);
Lloyd Piquede196652020-01-22 17:29:58 -0800334 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState);
Lloyd Piquef5275482019-01-29 18:42:42 -0800335 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800336
Lloyd Piquef5275482019-01-29 18:42:42 -0800337 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
Lloyd Piquede196652020-01-22 17:29:58 -0800338 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800339
Lloyd Piquef5275482019-01-29 18:42:42 -0800340 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700341
342 // Always set the layer color after setting the composition type.
Lloyd Piquede196652020-01-22 17:29:58 -0800343 writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState);
Lloyd Piquef5275482019-01-29 18:42:42 -0800344}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800345
Lloyd Piquef5275482019-01-29 18:42:42 -0800346void OutputLayer::writeOutputDependentGeometryStateToHWC(
347 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
348 const auto& outputDependentState = getState();
349
350 if (auto error = hwcLayer->setDisplayFrame(outputDependentState.displayFrame);
351 error != HWC2::Error::None) {
352 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700353 getLayerFE().getDebugName(), outputDependentState.displayFrame.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800354 outputDependentState.displayFrame.top, outputDependentState.displayFrame.right,
355 outputDependentState.displayFrame.bottom, to_string(error).c_str(),
356 static_cast<int32_t>(error));
357 }
358
359 if (auto error = hwcLayer->setSourceCrop(outputDependentState.sourceCrop);
360 error != HWC2::Error::None) {
361 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
362 "%s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700363 getLayerFE().getDebugName(), outputDependentState.sourceCrop.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800364 outputDependentState.sourceCrop.top, outputDependentState.sourceCrop.right,
365 outputDependentState.sourceCrop.bottom, to_string(error).c_str(),
366 static_cast<int32_t>(error));
367 }
368
369 if (auto error = hwcLayer->setZOrder(outputDependentState.z); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700370 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(),
371 outputDependentState.z, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800372 }
373
374 // Solid-color layers should always use an identity transform.
375 const auto bufferTransform =
376 requestedCompositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR
377 ? outputDependentState.bufferTransform
378 : static_cast<Hwc2::Transform>(0);
379 if (auto error = hwcLayer->setTransform(static_cast<HWC2::Transform>(bufferTransform));
380 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700381 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800382 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
383 static_cast<int32_t>(error));
384 }
385}
386
387void OutputLayer::writeOutputIndependentGeometryStateToHWC(
388 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
389 if (auto error = hwcLayer->setBlendMode(
390 static_cast<HWC2::BlendMode>(outputIndependentState.blendMode));
391 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700392 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800393 toString(outputIndependentState.blendMode).c_str(), to_string(error).c_str(),
394 static_cast<int32_t>(error));
395 }
396
397 if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha);
398 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700399 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800400 outputIndependentState.alpha, to_string(error).c_str(), static_cast<int32_t>(error));
401 }
402
Lloyd Pique0a456232020-01-16 17:51:13 -0800403 if (auto error = hwcLayer->setInfo(static_cast<uint32_t>(outputIndependentState.type),
404 static_cast<uint32_t>(outputIndependentState.appId));
Lloyd Piquef5275482019-01-29 18:42:42 -0800405 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700406 ALOGE("[%s] Failed to set info %s (%d)", getLayerFE().getDebugName(),
407 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800408 }
409}
410
411void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
412 const auto& outputDependentState = getState();
413
Lloyd Piquea2468662019-03-07 21:31:06 -0800414 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800415 // state and should not change every frame.
Lloyd Piquea2468662019-03-07 21:31:06 -0800416 if (auto error = hwcLayer->setVisibleRegion(outputDependentState.outputSpaceVisibleRegion);
Lloyd Piquef5275482019-01-29 18:42:42 -0800417 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700418 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800419 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800420 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800421 }
422
423 if (auto error = hwcLayer->setDataspace(outputDependentState.dataspace);
424 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700425 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800426 outputDependentState.dataspace, to_string(error).c_str(),
427 static_cast<int32_t>(error));
428 }
429}
430
431void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
432 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
433 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
434 case HWC2::Error::None:
435 break;
436 case HWC2::Error::Unsupported:
437 editState().forceClientComposition = true;
438 break;
439 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700440 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800441 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800442 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800443
Lloyd Piquef5275482019-01-29 18:42:42 -0800444 if (auto error = hwcLayer->setSurfaceDamage(outputIndependentState.surfaceDamage);
445 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700446 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800447 to_string(error).c_str(), static_cast<int32_t>(error));
448 outputIndependentState.surfaceDamage.dump(LOG_TAG);
449 }
450
451 // Content-specific per-frame state
452 switch (outputIndependentState.compositionType) {
453 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700454 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800455 break;
456 case Hwc2::IComposerClient::Composition::SIDEBAND:
457 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
458 break;
459 case Hwc2::IComposerClient::Composition::CURSOR:
460 case Hwc2::IComposerClient::Composition::DEVICE:
461 writeBufferStateToHWC(hwcLayer, outputIndependentState);
462 break;
463 case Hwc2::IComposerClient::Composition::INVALID:
464 case Hwc2::IComposerClient::Composition::CLIENT:
465 // Ignored
466 break;
467 }
468}
469
470void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
471 const LayerFECompositionState& outputIndependentState) {
Lloyd Pique46b72df2019-10-29 13:19:27 -0700472 if (outputIndependentState.compositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR) {
473 return;
474 }
475
Lloyd Piquef5275482019-01-29 18:42:42 -0800476 hwc_color_t color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
477 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
478 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
479 255};
480
481 if (auto error = hwcLayer->setColor(color); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700482 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800483 to_string(error).c_str(), static_cast<int32_t>(error));
484 }
485}
486
487void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
488 const LayerFECompositionState& outputIndependentState) {
489 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
490 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700491 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
493 static_cast<int32_t>(error));
494 }
495}
496
497void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
498 const LayerFECompositionState& outputIndependentState) {
499 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700500 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800501 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
502 outputIndependentState.hdrMetadata);
503 error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700504 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800505 to_string(error).c_str(), static_cast<int32_t>(error));
506 }
507
508 uint32_t hwcSlot = 0;
509 sp<GraphicBuffer> hwcBuffer;
510 // We need access to the output-dependent state for the buffer cache there,
511 // though otherwise the buffer is not output-dependent.
512 editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot,
513 outputIndependentState.buffer, &hwcSlot,
514 &hwcBuffer);
515
516 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, outputIndependentState.acquireFence);
517 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700518 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800519 outputIndependentState.buffer->handle, to_string(error).c_str(),
520 static_cast<int32_t>(error));
521 }
522}
523
524void OutputLayer::writeCompositionTypeToHWC(
525 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
526 auto& outputDependentState = editState();
527
528 // If we are forcing client composition, we need to tell the HWC
529 if (outputDependentState.forceClientComposition) {
530 requestedCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
531 }
532
533 // Set the requested composition type with the HWC whenever it changes
534 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) {
535 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
536
537 if (auto error = hwcLayer->setCompositionType(
538 static_cast<HWC2::Composition>(requestedCompositionType));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800539 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700540 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800541 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800542 static_cast<int32_t>(error));
543 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800544 }
545}
546
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800547void OutputLayer::writeCursorPositionToHWC() const {
548 // Skip doing this if there is no HWC interface
549 auto hwcLayer = getHwcLayer();
550 if (!hwcLayer) {
551 return;
552 }
553
Lloyd Piquede196652020-01-22 17:29:58 -0800554 const auto* layerFEState = getLayerFE().getCompositionState();
555 if (!layerFEState) {
556 return;
557 }
558
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700559 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800560
Lloyd Piquede196652020-01-22 17:29:58 -0800561 Rect frame = layerFEState->cursorFrame;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800562 frame.intersect(outputState.viewport, &frame);
563 Rect position = outputState.transform.transform(frame);
564
565 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
566 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700567 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
568 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
569 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800570 }
571}
572
Lloyd Pique66d68602019-02-13 14:23:31 -0800573HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700574 const auto& state = getState();
575 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800576}
577
578bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700579 const auto& state = getState();
580 return !state.hwc ||
581 state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800582}
583
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800584bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700585 const auto& state = getState();
586 return state.hwc && state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800587}
588
Lloyd Pique66d68602019-02-13 14:23:31 -0800589void OutputLayer::detectDisallowedCompositionTypeChange(
590 Hwc2::IComposerClient::Composition from, Hwc2::IComposerClient::Composition to) const {
591 bool result = false;
592 switch (from) {
593 case Hwc2::IComposerClient::Composition::INVALID:
594 case Hwc2::IComposerClient::Composition::CLIENT:
595 result = false;
596 break;
597
598 case Hwc2::IComposerClient::Composition::DEVICE:
599 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
600 result = (to == Hwc2::IComposerClient::Composition::CLIENT);
601 break;
602
603 case Hwc2::IComposerClient::Composition::CURSOR:
604 case Hwc2::IComposerClient::Composition::SIDEBAND:
605 result = (to == Hwc2::IComposerClient::Composition::CLIENT ||
606 to == Hwc2::IComposerClient::Composition::DEVICE);
607 break;
608 }
609
610 if (!result) {
611 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700612 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800613 toString(to).c_str(), static_cast<int>(to));
614 }
615}
616
617void OutputLayer::applyDeviceCompositionTypeChange(
618 Hwc2::IComposerClient::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700619 auto& state = editState();
620 LOG_FATAL_IF(!state.hwc);
621 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800622
623 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
624
625 hwcState.hwcCompositionType = compositionType;
626}
627
628void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700629 auto& state = editState();
630 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800631}
632
633void OutputLayer::applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700634 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800635 switch (request) {
636 case Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700637 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800638 break;
639
640 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700641 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800642 toString(request).c_str(), static_cast<int>(request));
643 break;
644 }
645}
646
Lloyd Pique688abd42019-02-15 15:42:24 -0800647bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700648 const auto& state = getState();
649 const auto& displayFrame = state.displayFrame;
650 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800651 return sourceCrop.getHeight() != displayFrame.getHeight() ||
652 sourceCrop.getWidth() != displayFrame.getWidth();
653}
654
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800655void OutputLayer::dump(std::string& out) const {
656 using android::base::StringAppendF;
657
Lloyd Piquede196652020-01-22 17:29:58 -0800658 StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName());
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700659 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800660}
661
Lloyd Piquecc01a452018-12-04 17:24:00 -0800662} // namespace impl
663} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800664