blob: d92b7ef0b7b67a1d223ceee1bbdae7c5e58254d2 [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/Layer.h>
20#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070021#include <compositionengine/LayerFECompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080022#include <compositionengine/Output.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>
Lloyd Piquecc01a452018-12-04 17:24:00 -080026
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080027// TODO(b/129481165): remove the #pragma below and fix conversion issues
28#pragma clang diagnostic push
29#pragma clang diagnostic ignored "-Wconversion"
30
Lloyd Pique07e33212018-12-18 16:33:37 -080031#include "DisplayHardware/HWComposer.h"
32
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080033// TODO(b/129481165): remove the #pragma below and fix conversion issues
34#pragma clang diagnostic pop // ignored "-Wconversion"
35
Lloyd Piquecc01a452018-12-04 17:24:00 -080036namespace android::compositionengine {
37
38OutputLayer::~OutputLayer() = default;
39
40namespace impl {
41
Lloyd Piquea83776c2019-01-29 18:42:32 -080042namespace {
43
44FloatRect reduce(const FloatRect& win, const Region& exclude) {
45 if (CC_LIKELY(exclude.isEmpty())) {
46 return win;
47 }
48 // Convert through Rect (by rounding) for lack of FloatRegion
49 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
50}
51
52} // namespace
53
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070054std::unique_ptr<OutputLayer> createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -080055 const compositionengine::Output& output,
56 const std::shared_ptr<compositionengine::Layer>& layer,
57 const sp<compositionengine::LayerFE>& layerFE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070058 return createOutputLayerTemplated<OutputLayer>(output, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080059}
60
Lloyd Piquecc01a452018-12-04 17:24:00 -080061OutputLayer::~OutputLayer() = default;
62
Lloyd Piquedf336d92019-03-07 21:38:42 -080063void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070064 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080065 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070066 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080067 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070068 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080069 }
Lloyd Pique07e33212018-12-18 16:33:37 -080070}
71
Lloyd Piquea83776c2019-01-29 18:42:32 -080072Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070073 const auto& layerState = getLayer().getFEState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080074
75 // apply the projection's clipping to the window crop in
76 // layerstack space, and convert-back to layer space.
77 // if there are no window scaling involved, this operation will map to full
78 // pixels in the buffer.
79
80 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080081 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080082
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070083 const Rect& viewport = getOutput().getState().viewport;
Lloyd Piquea83776c2019-01-29 18:42:32 -080084 const ui::Transform& layerTransform = layerState.geomLayerTransform;
85 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
86 // Transform to screen space.
87 activeCropFloat = layerTransform.transform(activeCropFloat);
88 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
89 // Back to layer space to work with the content crop.
90 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
91
92 // This needs to be here as transform.transform(Rect) computes the
93 // transformed rect and then takes the bounding box of the result before
94 // returning. This means
95 // transform.inverse().transform(transform.transform(Rect)) != Rect
96 // in which case we need to make sure the final rect is clipped to the
97 // display bounds.
98 Rect activeCrop{activeCropFloat};
99 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
100 activeCrop.clear();
101 }
102 return activeCrop;
103}
104
105FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700106 const auto& layerState = getLayer().getFEState();
107 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800108
109 if (!layerState.geomUsesSourceCrop) {
110 return {};
111 }
112
113 // the content crop is the area of the content that gets scaled to the
114 // layer's size. This is in buffer space.
115 FloatRect crop = layerState.geomContentCrop.toFloatRect();
116
117 // In addition there is a WM-specified crop we pull from our drawing state.
118 Rect activeCrop = calculateInitialCrop();
119 const Rect& bufferSize = layerState.geomBufferSize;
120
121 int winWidth = bufferSize.getWidth();
122 int winHeight = bufferSize.getHeight();
123
124 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
125 // if display frame hasn't been set and the parent is an unbounded layer.
126 if (winWidth < 0 && winHeight < 0) {
127 return crop;
128 }
129
130 // Transform the window crop to match the buffer coordinate system,
131 // which means using the inverse of the current transform set on the
132 // SurfaceFlingerConsumer.
133 uint32_t invTransform = layerState.geomBufferTransform;
134 if (layerState.geomBufferUsesDisplayInverseTransform) {
135 /*
136 * the code below applies the primary display's inverse transform to the
137 * buffer
138 */
139 uint32_t invTransformOrient = outputState.orientation;
140 // 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.
155 bool is_h_flipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
156 bool is_v_flipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
157 if (is_h_flipped == is_v_flipped) {
158 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
166 float xScale = crop.getWidth() / float(winWidth);
167 float yScale = crop.getHeight() / float(winHeight);
168
169 float insetL = winCrop.left * xScale;
170 float insetT = winCrop.top * yScale;
171 float insetR = (winWidth - winCrop.right) * xScale;
172 float insetB = (winHeight - winCrop.bottom) * yScale;
173
174 crop.left += insetL;
175 crop.top += insetT;
176 crop.right -= insetR;
177 crop.bottom -= insetB;
178
179 return crop;
180}
181
182Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700183 const auto& layerState = getLayer().getFEState();
184 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);
195 if (!activeCrop.intersect(outputState.viewport, &activeCrop)) {
196 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'.
219 Rect frame{
220 layerTransform.transform(reduce(layerState.geomLayerBounds, activeTransparentRegion))};
221 if (!frame.intersect(outputState.viewport, &frame)) {
222 frame.clear();
223 }
224 const ui::Transform displayTransform{outputState.transform};
225
226 return displayTransform.transform(frame);
227}
228
229uint32_t OutputLayer::calculateOutputRelativeBufferTransform() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700230 const auto& layerState = getLayer().getFEState();
231 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800232
233 /*
234 * Transformations are applied in this order:
235 * 1) buffer orientation/flip/mirror
236 * 2) state transformation (window manager)
237 * 3) layer orientation (screen orientation)
238 * (NOTE: the matrices are multiplied in reverse order)
239 */
240 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700241 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800242 const ui::Transform bufferTransform{layerState.geomBufferTransform};
243 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
244
245 if (layerState.geomBufferUsesDisplayInverseTransform) {
246 /*
247 * the code below applies the primary display's inverse transform to the
248 * buffer
249 */
250 uint32_t invTransform = outputState.orientation;
251 // calculate the inverse transform
252 if (invTransform & HAL_TRANSFORM_ROT_90) {
253 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
254 }
255
256 /*
257 * Here we cancel out the orientation component of the WM transform.
258 * The scaling and translate components are already included in our bounds
259 * computation so it's enough to just omit it in the composition.
260 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
261 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000262 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800263 }
264
265 // this gives us only the "orientation" component of the transform
266 return transform.getOrientation();
267} // namespace impl
268
Lloyd Pique7a234912019-10-03 11:54:27 -0700269void OutputLayer::updateCompositionState(bool includeGeometry, bool forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700270 const auto& layerFEState = getLayer().getFEState();
271 const auto& outputState = getOutput().getState();
272 const auto& profile = *getOutput().getDisplayColorProfile();
273 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800274
Lloyd Piquea83776c2019-01-29 18:42:32 -0800275 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700276 // Clear the forceClientComposition flag before it is set for any
277 // reason. Note that since it can be set by some checks below when
278 // updating the geometry state, we only clear it when updating the
279 // geometry since those conditions for forcing client composition won't
280 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700281 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700282
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700283 state.displayFrame = calculateOutputDisplayFrame();
284 state.sourceCrop = calculateOutputSourceCrop();
285 state.bufferTransform =
Lloyd Piquea83776c2019-01-29 18:42:32 -0800286 static_cast<Hwc2::Transform>(calculateOutputRelativeBufferTransform());
287
Lloyd Piquef5275482019-01-29 18:42:42 -0800288 if ((layerFEState.isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700289 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
290 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800291 }
292 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800293
294 // Determine the output dependent dataspace for this layer. If it is
295 // colorspace agnostic, it just uses the dataspace chosen for the output to
296 // avoid the need for color conversion.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700297 state.dataspace = layerFEState.isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800298 outputState.targetDataspace != ui::Dataspace::UNKNOWN
299 ? outputState.targetDataspace
300 : layerFEState.dataspace;
301
Lloyd Piquef5275482019-01-29 18:42:42 -0800302 // These are evaluated every frame as they can potentially change at any
303 // time.
Lloyd Pique7a234912019-10-03 11:54:27 -0700304 if (layerFEState.forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
305 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700306 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800307 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800308}
309
Lloyd Piquef5275482019-01-29 18:42:42 -0800310void OutputLayer::writeStateToHWC(bool includeGeometry) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700311 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800312 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700313 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800314 return;
315 }
316
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700317 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800318 if (!hwcLayer) {
319 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700320 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800321 return;
322 }
323
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700324 const auto& outputIndependentState = getLayer().getFEState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800325 auto requestedCompositionType = outputIndependentState.compositionType;
326
Lloyd Piquea83776c2019-01-29 18:42:32 -0800327 if (includeGeometry) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800328 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType);
329 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), outputIndependentState);
330 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800331
Lloyd Piquef5275482019-01-29 18:42:42 -0800332 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
333 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800334
Lloyd Piquef5275482019-01-29 18:42:42 -0800335 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700336
337 // Always set the layer color after setting the composition type.
338 writeSolidColorStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquef5275482019-01-29 18:42:42 -0800339}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800340
Lloyd Piquef5275482019-01-29 18:42:42 -0800341void OutputLayer::writeOutputDependentGeometryStateToHWC(
342 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
343 const auto& outputDependentState = getState();
344
345 if (auto error = hwcLayer->setDisplayFrame(outputDependentState.displayFrame);
346 error != HWC2::Error::None) {
347 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700348 getLayerFE().getDebugName(), outputDependentState.displayFrame.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800349 outputDependentState.displayFrame.top, outputDependentState.displayFrame.right,
350 outputDependentState.displayFrame.bottom, to_string(error).c_str(),
351 static_cast<int32_t>(error));
352 }
353
354 if (auto error = hwcLayer->setSourceCrop(outputDependentState.sourceCrop);
355 error != HWC2::Error::None) {
356 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
357 "%s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700358 getLayerFE().getDebugName(), outputDependentState.sourceCrop.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800359 outputDependentState.sourceCrop.top, outputDependentState.sourceCrop.right,
360 outputDependentState.sourceCrop.bottom, to_string(error).c_str(),
361 static_cast<int32_t>(error));
362 }
363
364 if (auto error = hwcLayer->setZOrder(outputDependentState.z); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700365 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(),
366 outputDependentState.z, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800367 }
368
369 // Solid-color layers should always use an identity transform.
370 const auto bufferTransform =
371 requestedCompositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR
372 ? outputDependentState.bufferTransform
373 : static_cast<Hwc2::Transform>(0);
374 if (auto error = hwcLayer->setTransform(static_cast<HWC2::Transform>(bufferTransform));
375 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700376 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800377 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
378 static_cast<int32_t>(error));
379 }
380}
381
382void OutputLayer::writeOutputIndependentGeometryStateToHWC(
383 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
384 if (auto error = hwcLayer->setBlendMode(
385 static_cast<HWC2::BlendMode>(outputIndependentState.blendMode));
386 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700387 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800388 toString(outputIndependentState.blendMode).c_str(), to_string(error).c_str(),
389 static_cast<int32_t>(error));
390 }
391
392 if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha);
393 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700394 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800395 outputIndependentState.alpha, to_string(error).c_str(), static_cast<int32_t>(error));
396 }
397
Lloyd Pique0a456232020-01-16 17:51:13 -0800398 if (auto error = hwcLayer->setInfo(static_cast<uint32_t>(outputIndependentState.type),
399 static_cast<uint32_t>(outputIndependentState.appId));
Lloyd Piquef5275482019-01-29 18:42:42 -0800400 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700401 ALOGE("[%s] Failed to set info %s (%d)", getLayerFE().getDebugName(),
402 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800403 }
404}
405
406void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
407 const auto& outputDependentState = getState();
408
Lloyd Piquea2468662019-03-07 21:31:06 -0800409 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800410 // state and should not change every frame.
Lloyd Piquea2468662019-03-07 21:31:06 -0800411 if (auto error = hwcLayer->setVisibleRegion(outputDependentState.outputSpaceVisibleRegion);
Lloyd Piquef5275482019-01-29 18:42:42 -0800412 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700413 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800414 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800415 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800416 }
417
418 if (auto error = hwcLayer->setDataspace(outputDependentState.dataspace);
419 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700420 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800421 outputDependentState.dataspace, to_string(error).c_str(),
422 static_cast<int32_t>(error));
423 }
424}
425
426void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
427 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
428 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
429 case HWC2::Error::None:
430 break;
431 case HWC2::Error::Unsupported:
432 editState().forceClientComposition = true;
433 break;
434 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700435 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800437 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800438
Lloyd Piquef5275482019-01-29 18:42:42 -0800439 if (auto error = hwcLayer->setSurfaceDamage(outputIndependentState.surfaceDamage);
440 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700441 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800442 to_string(error).c_str(), static_cast<int32_t>(error));
443 outputIndependentState.surfaceDamage.dump(LOG_TAG);
444 }
445
446 // Content-specific per-frame state
447 switch (outputIndependentState.compositionType) {
448 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700449 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800450 break;
451 case Hwc2::IComposerClient::Composition::SIDEBAND:
452 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
453 break;
454 case Hwc2::IComposerClient::Composition::CURSOR:
455 case Hwc2::IComposerClient::Composition::DEVICE:
456 writeBufferStateToHWC(hwcLayer, outputIndependentState);
457 break;
458 case Hwc2::IComposerClient::Composition::INVALID:
459 case Hwc2::IComposerClient::Composition::CLIENT:
460 // Ignored
461 break;
462 }
463}
464
465void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
466 const LayerFECompositionState& outputIndependentState) {
Lloyd Pique46b72df2019-10-29 13:19:27 -0700467 if (outputIndependentState.compositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR) {
468 return;
469 }
470
Lloyd Piquef5275482019-01-29 18:42:42 -0800471 hwc_color_t color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
472 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
473 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
474 255};
475
476 if (auto error = hwcLayer->setColor(color); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700477 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800478 to_string(error).c_str(), static_cast<int32_t>(error));
479 }
480}
481
482void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
483 const LayerFECompositionState& outputIndependentState) {
484 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
485 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700486 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800487 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
488 static_cast<int32_t>(error));
489 }
490}
491
492void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
493 const LayerFECompositionState& outputIndependentState) {
494 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700495 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800496 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
497 outputIndependentState.hdrMetadata);
498 error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700499 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800500 to_string(error).c_str(), static_cast<int32_t>(error));
501 }
502
503 uint32_t hwcSlot = 0;
504 sp<GraphicBuffer> hwcBuffer;
505 // We need access to the output-dependent state for the buffer cache there,
506 // though otherwise the buffer is not output-dependent.
507 editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot,
508 outputIndependentState.buffer, &hwcSlot,
509 &hwcBuffer);
510
511 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, outputIndependentState.acquireFence);
512 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700513 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800514 outputIndependentState.buffer->handle, to_string(error).c_str(),
515 static_cast<int32_t>(error));
516 }
517}
518
519void OutputLayer::writeCompositionTypeToHWC(
520 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
521 auto& outputDependentState = editState();
522
523 // If we are forcing client composition, we need to tell the HWC
524 if (outputDependentState.forceClientComposition) {
525 requestedCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
526 }
527
528 // Set the requested composition type with the HWC whenever it changes
529 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) {
530 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
531
532 if (auto error = hwcLayer->setCompositionType(
533 static_cast<HWC2::Composition>(requestedCompositionType));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800534 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700535 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800536 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800537 static_cast<int32_t>(error));
538 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800539 }
540}
541
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800542void OutputLayer::writeCursorPositionToHWC() const {
543 // Skip doing this if there is no HWC interface
544 auto hwcLayer = getHwcLayer();
545 if (!hwcLayer) {
546 return;
547 }
548
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700549 const auto& layerFEState = getLayer().getFEState();
550 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800551
552 Rect frame = layerFEState.cursorFrame;
553 frame.intersect(outputState.viewport, &frame);
554 Rect position = outputState.transform.transform(frame);
555
556 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
557 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700558 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
559 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
560 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800561 }
562}
563
Lloyd Pique66d68602019-02-13 14:23:31 -0800564HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700565 const auto& state = getState();
566 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800567}
568
569bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700570 const auto& state = getState();
571 return !state.hwc ||
572 state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800573}
574
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800575bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700576 const auto& state = getState();
577 return state.hwc && state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800578}
579
Lloyd Pique66d68602019-02-13 14:23:31 -0800580void OutputLayer::detectDisallowedCompositionTypeChange(
581 Hwc2::IComposerClient::Composition from, Hwc2::IComposerClient::Composition to) const {
582 bool result = false;
583 switch (from) {
584 case Hwc2::IComposerClient::Composition::INVALID:
585 case Hwc2::IComposerClient::Composition::CLIENT:
586 result = false;
587 break;
588
589 case Hwc2::IComposerClient::Composition::DEVICE:
590 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
591 result = (to == Hwc2::IComposerClient::Composition::CLIENT);
592 break;
593
594 case Hwc2::IComposerClient::Composition::CURSOR:
595 case Hwc2::IComposerClient::Composition::SIDEBAND:
596 result = (to == Hwc2::IComposerClient::Composition::CLIENT ||
597 to == Hwc2::IComposerClient::Composition::DEVICE);
598 break;
599 }
600
601 if (!result) {
602 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700603 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800604 toString(to).c_str(), static_cast<int>(to));
605 }
606}
607
608void OutputLayer::applyDeviceCompositionTypeChange(
609 Hwc2::IComposerClient::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700610 auto& state = editState();
611 LOG_FATAL_IF(!state.hwc);
612 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800613
614 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
615
616 hwcState.hwcCompositionType = compositionType;
617}
618
619void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700620 auto& state = editState();
621 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800622}
623
624void OutputLayer::applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700625 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800626 switch (request) {
627 case Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700628 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800629 break;
630
631 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700632 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800633 toString(request).c_str(), static_cast<int>(request));
634 break;
635 }
636}
637
Lloyd Pique688abd42019-02-15 15:42:24 -0800638bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700639 const auto& state = getState();
640 const auto& displayFrame = state.displayFrame;
641 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800642 return sourceCrop.getHeight() != displayFrame.getHeight() ||
643 sourceCrop.getWidth() != displayFrame.getWidth();
644}
645
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800646void OutputLayer::dump(std::string& out) const {
647 using android::base::StringAppendF;
648
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700649 StringAppendF(&out, " - Output Layer %p (Composition layer %p) (%s)\n", this, &getLayer(),
650 getLayerFE().getDebugName());
651 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800652}
653
Lloyd Piquecc01a452018-12-04 17:24:00 -0800654} // namespace impl
655} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800656