blob: ce0222cee51b000c3589b50dae0be8a3a0b98aeb [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 Pique07e33212018-12-18 16:33:37 -080027#include "DisplayHardware/HWComposer.h"
28
Lloyd Piquecc01a452018-12-04 17:24:00 -080029namespace android::compositionengine {
30
31OutputLayer::~OutputLayer() = default;
32
33namespace impl {
34
Lloyd Piquea83776c2019-01-29 18:42:32 -080035namespace {
36
37FloatRect reduce(const FloatRect& win, const Region& exclude) {
38 if (CC_LIKELY(exclude.isEmpty())) {
39 return win;
40 }
41 // Convert through Rect (by rounding) for lack of FloatRegion
42 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
43}
44
45} // namespace
46
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070047std::unique_ptr<OutputLayer> createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -080048 const compositionengine::Output& output,
49 const std::shared_ptr<compositionengine::Layer>& layer,
50 const sp<compositionengine::LayerFE>& layerFE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070051 return createOutputLayerTemplated<OutputLayer>(output, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080052}
53
Lloyd Piquecc01a452018-12-04 17:24:00 -080054OutputLayer::~OutputLayer() = default;
55
Lloyd Piquedf336d92019-03-07 21:38:42 -080056void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070057 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080058 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070059 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080060 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070061 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080062 }
Lloyd Pique07e33212018-12-18 16:33:37 -080063}
64
Lloyd Piquea83776c2019-01-29 18:42:32 -080065Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070066 const auto& layerState = getLayer().getFEState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080067
68 // apply the projection's clipping to the window crop in
69 // layerstack space, and convert-back to layer space.
70 // if there are no window scaling involved, this operation will map to full
71 // pixels in the buffer.
72
73 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080074 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080075
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070076 const Rect& viewport = getOutput().getState().viewport;
Lloyd Piquea83776c2019-01-29 18:42:32 -080077 const ui::Transform& layerTransform = layerState.geomLayerTransform;
78 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
79 // Transform to screen space.
80 activeCropFloat = layerTransform.transform(activeCropFloat);
81 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
82 // Back to layer space to work with the content crop.
83 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
84
85 // This needs to be here as transform.transform(Rect) computes the
86 // transformed rect and then takes the bounding box of the result before
87 // returning. This means
88 // transform.inverse().transform(transform.transform(Rect)) != Rect
89 // in which case we need to make sure the final rect is clipped to the
90 // display bounds.
91 Rect activeCrop{activeCropFloat};
92 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
93 activeCrop.clear();
94 }
95 return activeCrop;
96}
97
98FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070099 const auto& layerState = getLayer().getFEState();
100 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800101
102 if (!layerState.geomUsesSourceCrop) {
103 return {};
104 }
105
106 // the content crop is the area of the content that gets scaled to the
107 // layer's size. This is in buffer space.
108 FloatRect crop = layerState.geomContentCrop.toFloatRect();
109
110 // In addition there is a WM-specified crop we pull from our drawing state.
111 Rect activeCrop = calculateInitialCrop();
112 const Rect& bufferSize = layerState.geomBufferSize;
113
114 int winWidth = bufferSize.getWidth();
115 int winHeight = bufferSize.getHeight();
116
117 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
118 // if display frame hasn't been set and the parent is an unbounded layer.
119 if (winWidth < 0 && winHeight < 0) {
120 return crop;
121 }
122
123 // Transform the window crop to match the buffer coordinate system,
124 // which means using the inverse of the current transform set on the
125 // SurfaceFlingerConsumer.
126 uint32_t invTransform = layerState.geomBufferTransform;
127 if (layerState.geomBufferUsesDisplayInverseTransform) {
128 /*
129 * the code below applies the primary display's inverse transform to the
130 * buffer
131 */
132 uint32_t invTransformOrient = outputState.orientation;
133 // calculate the inverse transform
134 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
135 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
136 }
137 // and apply to the current transform
138 invTransform =
139 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
140 }
141
142 if (invTransform & HAL_TRANSFORM_ROT_90) {
143 // If the activeCrop has been rotate the ends are rotated but not
144 // the space itself so when transforming ends back we can't rely on
145 // a modification of the axes of rotation. To account for this we
146 // need to reorient the inverse rotation in terms of the current
147 // axes of rotation.
148 bool is_h_flipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
149 bool is_v_flipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
150 if (is_h_flipped == is_v_flipped) {
151 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
152 }
153 std::swap(winWidth, winHeight);
154 }
155 const Rect winCrop =
156 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
157
158 // below, crop is intersected with winCrop expressed in crop's coordinate space
159 float xScale = crop.getWidth() / float(winWidth);
160 float yScale = crop.getHeight() / float(winHeight);
161
162 float insetL = winCrop.left * xScale;
163 float insetT = winCrop.top * yScale;
164 float insetR = (winWidth - winCrop.right) * xScale;
165 float insetB = (winHeight - winCrop.bottom) * yScale;
166
167 crop.left += insetL;
168 crop.top += insetT;
169 crop.right -= insetR;
170 crop.bottom -= insetB;
171
172 return crop;
173}
174
175Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700176 const auto& layerState = getLayer().getFEState();
177 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800178
179 // apply the layer's transform, followed by the display's global transform
180 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800181 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800182 const ui::Transform& layerTransform = layerState.geomLayerTransform;
183 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
184 const Rect& bufferSize = layerState.geomBufferSize;
185 Rect activeCrop = layerState.geomCrop;
186 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
187 activeCrop = layerTransform.transform(activeCrop);
188 if (!activeCrop.intersect(outputState.viewport, &activeCrop)) {
189 activeCrop.clear();
190 }
191 activeCrop = inverseLayerTransform.transform(activeCrop, true);
192 // This needs to be here as transform.transform(Rect) computes the
193 // transformed rect and then takes the bounding box of the result before
194 // returning. This means
195 // transform.inverse().transform(transform.transform(Rect)) != Rect
196 // in which case we need to make sure the final rect is clipped to the
197 // display bounds.
198 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
199 activeCrop.clear();
200 }
201 // mark regions outside the crop as transparent
202 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
203 activeTransparentRegion.orSelf(
204 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
205 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
206 activeTransparentRegion.orSelf(
207 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
208 }
209
210 // reduce uses a FloatRect to provide more accuracy during the
211 // transformation. We then round upon constructing 'frame'.
212 Rect frame{
213 layerTransform.transform(reduce(layerState.geomLayerBounds, activeTransparentRegion))};
214 if (!frame.intersect(outputState.viewport, &frame)) {
215 frame.clear();
216 }
217 const ui::Transform displayTransform{outputState.transform};
218
219 return displayTransform.transform(frame);
220}
221
222uint32_t OutputLayer::calculateOutputRelativeBufferTransform() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700223 const auto& layerState = getLayer().getFEState();
224 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800225
226 /*
227 * Transformations are applied in this order:
228 * 1) buffer orientation/flip/mirror
229 * 2) state transformation (window manager)
230 * 3) layer orientation (screen orientation)
231 * (NOTE: the matrices are multiplied in reverse order)
232 */
233 const ui::Transform& layerTransform = layerState.geomLayerTransform;
234 const ui::Transform displayTransform{outputState.orientation};
235 const ui::Transform bufferTransform{layerState.geomBufferTransform};
236 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
237
238 if (layerState.geomBufferUsesDisplayInverseTransform) {
239 /*
240 * the code below applies the primary display's inverse transform to the
241 * buffer
242 */
243 uint32_t invTransform = outputState.orientation;
244 // calculate the inverse transform
245 if (invTransform & HAL_TRANSFORM_ROT_90) {
246 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
247 }
248
249 /*
250 * Here we cancel out the orientation component of the WM transform.
251 * The scaling and translate components are already included in our bounds
252 * computation so it's enough to just omit it in the composition.
253 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
254 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000255 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800256 }
257
258 // this gives us only the "orientation" component of the transform
259 return transform.getOrientation();
260} // namespace impl
261
Lloyd Pique7a234912019-10-03 11:54:27 -0700262void OutputLayer::updateCompositionState(bool includeGeometry, bool forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700263 const auto& layerFEState = getLayer().getFEState();
264 const auto& outputState = getOutput().getState();
265 const auto& profile = *getOutput().getDisplayColorProfile();
266 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800267
Lloyd Piquea83776c2019-01-29 18:42:32 -0800268 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700269 // Clear the forceClientComposition flag before it is set for any
270 // reason. Note that since it can be set by some checks below when
271 // updating the geometry state, we only clear it when updating the
272 // geometry since those conditions for forcing client composition won't
273 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700274 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700275
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700276 state.displayFrame = calculateOutputDisplayFrame();
277 state.sourceCrop = calculateOutputSourceCrop();
278 state.bufferTransform =
Lloyd Piquea83776c2019-01-29 18:42:32 -0800279 static_cast<Hwc2::Transform>(calculateOutputRelativeBufferTransform());
280
Lloyd Piquef5275482019-01-29 18:42:42 -0800281 if ((layerFEState.isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700282 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
283 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800284 }
285 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800286
287 // Determine the output dependent dataspace for this layer. If it is
288 // colorspace agnostic, it just uses the dataspace chosen for the output to
289 // avoid the need for color conversion.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700290 state.dataspace = layerFEState.isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800291 outputState.targetDataspace != ui::Dataspace::UNKNOWN
292 ? outputState.targetDataspace
293 : layerFEState.dataspace;
294
Lloyd Piquef5275482019-01-29 18:42:42 -0800295 // These are evaluated every frame as they can potentially change at any
296 // time.
Lloyd Pique7a234912019-10-03 11:54:27 -0700297 if (layerFEState.forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
298 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700299 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800300 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800301}
302
Lloyd Piquef5275482019-01-29 18:42:42 -0800303void OutputLayer::writeStateToHWC(bool includeGeometry) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700304 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800305 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700306 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800307 return;
308 }
309
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800311 if (!hwcLayer) {
312 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700313 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800314 return;
315 }
316
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700317 const auto& outputIndependentState = getLayer().getFEState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800318 auto requestedCompositionType = outputIndependentState.compositionType;
319
Lloyd Piquea83776c2019-01-29 18:42:32 -0800320 if (includeGeometry) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800321 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType);
322 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), outputIndependentState);
323 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800324
Lloyd Piquef5275482019-01-29 18:42:42 -0800325 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
326 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800327
Lloyd Piquef5275482019-01-29 18:42:42 -0800328 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700329
330 // Always set the layer color after setting the composition type.
331 writeSolidColorStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquef5275482019-01-29 18:42:42 -0800332}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800333
Lloyd Piquef5275482019-01-29 18:42:42 -0800334void OutputLayer::writeOutputDependentGeometryStateToHWC(
335 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
336 const auto& outputDependentState = getState();
337
338 if (auto error = hwcLayer->setDisplayFrame(outputDependentState.displayFrame);
339 error != HWC2::Error::None) {
340 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700341 getLayerFE().getDebugName(), outputDependentState.displayFrame.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800342 outputDependentState.displayFrame.top, outputDependentState.displayFrame.right,
343 outputDependentState.displayFrame.bottom, to_string(error).c_str(),
344 static_cast<int32_t>(error));
345 }
346
347 if (auto error = hwcLayer->setSourceCrop(outputDependentState.sourceCrop);
348 error != HWC2::Error::None) {
349 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
350 "%s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700351 getLayerFE().getDebugName(), outputDependentState.sourceCrop.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800352 outputDependentState.sourceCrop.top, outputDependentState.sourceCrop.right,
353 outputDependentState.sourceCrop.bottom, to_string(error).c_str(),
354 static_cast<int32_t>(error));
355 }
356
357 if (auto error = hwcLayer->setZOrder(outputDependentState.z); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700358 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(),
359 outputDependentState.z, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800360 }
361
362 // Solid-color layers should always use an identity transform.
363 const auto bufferTransform =
364 requestedCompositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR
365 ? outputDependentState.bufferTransform
366 : static_cast<Hwc2::Transform>(0);
367 if (auto error = hwcLayer->setTransform(static_cast<HWC2::Transform>(bufferTransform));
368 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700369 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800370 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
371 static_cast<int32_t>(error));
372 }
373}
374
375void OutputLayer::writeOutputIndependentGeometryStateToHWC(
376 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
377 if (auto error = hwcLayer->setBlendMode(
378 static_cast<HWC2::BlendMode>(outputIndependentState.blendMode));
379 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700380 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800381 toString(outputIndependentState.blendMode).c_str(), to_string(error).c_str(),
382 static_cast<int32_t>(error));
383 }
384
385 if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha);
386 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700387 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800388 outputIndependentState.alpha, to_string(error).c_str(), static_cast<int32_t>(error));
389 }
390
391 if (auto error = hwcLayer->setInfo(outputIndependentState.type, outputIndependentState.appId);
392 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700393 ALOGE("[%s] Failed to set info %s (%d)", getLayerFE().getDebugName(),
394 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800395 }
396}
397
398void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
399 const auto& outputDependentState = getState();
400
Lloyd Piquea2468662019-03-07 21:31:06 -0800401 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800402 // state and should not change every frame.
Lloyd Piquea2468662019-03-07 21:31:06 -0800403 if (auto error = hwcLayer->setVisibleRegion(outputDependentState.outputSpaceVisibleRegion);
Lloyd Piquef5275482019-01-29 18:42:42 -0800404 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700405 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800406 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800407 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800408 }
409
410 if (auto error = hwcLayer->setDataspace(outputDependentState.dataspace);
411 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700412 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800413 outputDependentState.dataspace, to_string(error).c_str(),
414 static_cast<int32_t>(error));
415 }
416}
417
418void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
419 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
420 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
421 case HWC2::Error::None:
422 break;
423 case HWC2::Error::Unsupported:
424 editState().forceClientComposition = true;
425 break;
426 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700427 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800428 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800429 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800430
Lloyd Piquef5275482019-01-29 18:42:42 -0800431 if (auto error = hwcLayer->setSurfaceDamage(outputIndependentState.surfaceDamage);
432 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700433 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800434 to_string(error).c_str(), static_cast<int32_t>(error));
435 outputIndependentState.surfaceDamage.dump(LOG_TAG);
436 }
437
438 // Content-specific per-frame state
439 switch (outputIndependentState.compositionType) {
440 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700441 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800442 break;
443 case Hwc2::IComposerClient::Composition::SIDEBAND:
444 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
445 break;
446 case Hwc2::IComposerClient::Composition::CURSOR:
447 case Hwc2::IComposerClient::Composition::DEVICE:
448 writeBufferStateToHWC(hwcLayer, outputIndependentState);
449 break;
450 case Hwc2::IComposerClient::Composition::INVALID:
451 case Hwc2::IComposerClient::Composition::CLIENT:
452 // Ignored
453 break;
454 }
455}
456
457void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
458 const LayerFECompositionState& outputIndependentState) {
Lloyd Pique46b72df2019-10-29 13:19:27 -0700459 if (outputIndependentState.compositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR) {
460 return;
461 }
462
Lloyd Piquef5275482019-01-29 18:42:42 -0800463 hwc_color_t color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
464 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
465 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
466 255};
467
468 if (auto error = hwcLayer->setColor(color); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700469 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800470 to_string(error).c_str(), static_cast<int32_t>(error));
471 }
472}
473
474void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
475 const LayerFECompositionState& outputIndependentState) {
476 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
477 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700478 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800479 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
480 static_cast<int32_t>(error));
481 }
482}
483
484void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
485 const LayerFECompositionState& outputIndependentState) {
486 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700487 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800488 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
489 outputIndependentState.hdrMetadata);
490 error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700491 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 to_string(error).c_str(), static_cast<int32_t>(error));
493 }
494
495 uint32_t hwcSlot = 0;
496 sp<GraphicBuffer> hwcBuffer;
497 // We need access to the output-dependent state for the buffer cache there,
498 // though otherwise the buffer is not output-dependent.
499 editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot,
500 outputIndependentState.buffer, &hwcSlot,
501 &hwcBuffer);
502
503 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, outputIndependentState.acquireFence);
504 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700505 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800506 outputIndependentState.buffer->handle, to_string(error).c_str(),
507 static_cast<int32_t>(error));
508 }
509}
510
511void OutputLayer::writeCompositionTypeToHWC(
512 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
513 auto& outputDependentState = editState();
514
515 // If we are forcing client composition, we need to tell the HWC
516 if (outputDependentState.forceClientComposition) {
517 requestedCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
518 }
519
520 // Set the requested composition type with the HWC whenever it changes
521 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) {
522 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
523
524 if (auto error = hwcLayer->setCompositionType(
525 static_cast<HWC2::Composition>(requestedCompositionType));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800526 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700527 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800528 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800529 static_cast<int32_t>(error));
530 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800531 }
532}
533
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800534void OutputLayer::writeCursorPositionToHWC() const {
535 // Skip doing this if there is no HWC interface
536 auto hwcLayer = getHwcLayer();
537 if (!hwcLayer) {
538 return;
539 }
540
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700541 const auto& layerFEState = getLayer().getFEState();
542 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800543
544 Rect frame = layerFEState.cursorFrame;
545 frame.intersect(outputState.viewport, &frame);
546 Rect position = outputState.transform.transform(frame);
547
548 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
549 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700550 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
551 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
552 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800553 }
554}
555
Lloyd Pique66d68602019-02-13 14:23:31 -0800556HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700557 const auto& state = getState();
558 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800559}
560
561bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700562 const auto& state = getState();
563 return !state.hwc ||
564 state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800565}
566
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800567bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700568 const auto& state = getState();
569 return state.hwc && state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800570}
571
Lloyd Pique66d68602019-02-13 14:23:31 -0800572void OutputLayer::detectDisallowedCompositionTypeChange(
573 Hwc2::IComposerClient::Composition from, Hwc2::IComposerClient::Composition to) const {
574 bool result = false;
575 switch (from) {
576 case Hwc2::IComposerClient::Composition::INVALID:
577 case Hwc2::IComposerClient::Composition::CLIENT:
578 result = false;
579 break;
580
581 case Hwc2::IComposerClient::Composition::DEVICE:
582 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
583 result = (to == Hwc2::IComposerClient::Composition::CLIENT);
584 break;
585
586 case Hwc2::IComposerClient::Composition::CURSOR:
587 case Hwc2::IComposerClient::Composition::SIDEBAND:
588 result = (to == Hwc2::IComposerClient::Composition::CLIENT ||
589 to == Hwc2::IComposerClient::Composition::DEVICE);
590 break;
591 }
592
593 if (!result) {
594 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700595 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800596 toString(to).c_str(), static_cast<int>(to));
597 }
598}
599
600void OutputLayer::applyDeviceCompositionTypeChange(
601 Hwc2::IComposerClient::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700602 auto& state = editState();
603 LOG_FATAL_IF(!state.hwc);
604 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800605
606 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
607
608 hwcState.hwcCompositionType = compositionType;
609}
610
611void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700612 auto& state = editState();
613 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800614}
615
616void OutputLayer::applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700617 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800618 switch (request) {
619 case Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700620 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800621 break;
622
623 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700624 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800625 toString(request).c_str(), static_cast<int>(request));
626 break;
627 }
628}
629
Lloyd Pique688abd42019-02-15 15:42:24 -0800630bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700631 const auto& state = getState();
632 const auto& displayFrame = state.displayFrame;
633 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800634 return sourceCrop.getHeight() != displayFrame.getHeight() ||
635 sourceCrop.getWidth() != displayFrame.getWidth();
636}
637
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800638void OutputLayer::dump(std::string& out) const {
639 using android::base::StringAppendF;
640
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700641 StringAppendF(&out, " - Output Layer %p (Composition layer %p) (%s)\n", this, &getLayer(),
642 getLayerFE().getDebugName());
643 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800644}
645
Lloyd Piquecc01a452018-12-04 17:24:00 -0800646} // namespace impl
647} // namespace android::compositionengine