blob: 721e953d252345fdcde8300125cb979c49a57b6c [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);
329}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800330
Lloyd Piquef5275482019-01-29 18:42:42 -0800331void OutputLayer::writeOutputDependentGeometryStateToHWC(
332 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
333 const auto& outputDependentState = getState();
334
335 if (auto error = hwcLayer->setDisplayFrame(outputDependentState.displayFrame);
336 error != HWC2::Error::None) {
337 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700338 getLayerFE().getDebugName(), outputDependentState.displayFrame.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800339 outputDependentState.displayFrame.top, outputDependentState.displayFrame.right,
340 outputDependentState.displayFrame.bottom, to_string(error).c_str(),
341 static_cast<int32_t>(error));
342 }
343
344 if (auto error = hwcLayer->setSourceCrop(outputDependentState.sourceCrop);
345 error != HWC2::Error::None) {
346 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
347 "%s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700348 getLayerFE().getDebugName(), outputDependentState.sourceCrop.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800349 outputDependentState.sourceCrop.top, outputDependentState.sourceCrop.right,
350 outputDependentState.sourceCrop.bottom, to_string(error).c_str(),
351 static_cast<int32_t>(error));
352 }
353
354 if (auto error = hwcLayer->setZOrder(outputDependentState.z); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700355 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(),
356 outputDependentState.z, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800357 }
358
359 // Solid-color layers should always use an identity transform.
360 const auto bufferTransform =
361 requestedCompositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR
362 ? outputDependentState.bufferTransform
363 : static_cast<Hwc2::Transform>(0);
364 if (auto error = hwcLayer->setTransform(static_cast<HWC2::Transform>(bufferTransform));
365 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700366 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800367 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
368 static_cast<int32_t>(error));
369 }
370}
371
372void OutputLayer::writeOutputIndependentGeometryStateToHWC(
373 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
374 if (auto error = hwcLayer->setBlendMode(
375 static_cast<HWC2::BlendMode>(outputIndependentState.blendMode));
376 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700377 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800378 toString(outputIndependentState.blendMode).c_str(), to_string(error).c_str(),
379 static_cast<int32_t>(error));
380 }
381
382 if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha);
383 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700384 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800385 outputIndependentState.alpha, to_string(error).c_str(), static_cast<int32_t>(error));
386 }
387
388 if (auto error = hwcLayer->setInfo(outputIndependentState.type, outputIndependentState.appId);
389 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700390 ALOGE("[%s] Failed to set info %s (%d)", getLayerFE().getDebugName(),
391 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800392 }
393}
394
395void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
396 const auto& outputDependentState = getState();
397
Lloyd Piquea2468662019-03-07 21:31:06 -0800398 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800399 // state and should not change every frame.
Lloyd Piquea2468662019-03-07 21:31:06 -0800400 if (auto error = hwcLayer->setVisibleRegion(outputDependentState.outputSpaceVisibleRegion);
Lloyd Piquef5275482019-01-29 18:42:42 -0800401 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700402 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800403 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800404 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800405 }
406
407 if (auto error = hwcLayer->setDataspace(outputDependentState.dataspace);
408 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700409 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800410 outputDependentState.dataspace, to_string(error).c_str(),
411 static_cast<int32_t>(error));
412 }
413}
414
415void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
416 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
417 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
418 case HWC2::Error::None:
419 break;
420 case HWC2::Error::Unsupported:
421 editState().forceClientComposition = true;
422 break;
423 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700424 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800426 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800427
Lloyd Piquef5275482019-01-29 18:42:42 -0800428 if (auto error = hwcLayer->setSurfaceDamage(outputIndependentState.surfaceDamage);
429 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700430 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800431 to_string(error).c_str(), static_cast<int32_t>(error));
432 outputIndependentState.surfaceDamage.dump(LOG_TAG);
433 }
434
435 // Content-specific per-frame state
436 switch (outputIndependentState.compositionType) {
437 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
438 writeSolidColorStateToHWC(hwcLayer, outputIndependentState);
439 break;
440 case Hwc2::IComposerClient::Composition::SIDEBAND:
441 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
442 break;
443 case Hwc2::IComposerClient::Composition::CURSOR:
444 case Hwc2::IComposerClient::Composition::DEVICE:
445 writeBufferStateToHWC(hwcLayer, outputIndependentState);
446 break;
447 case Hwc2::IComposerClient::Composition::INVALID:
448 case Hwc2::IComposerClient::Composition::CLIENT:
449 // Ignored
450 break;
451 }
452}
453
454void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
455 const LayerFECompositionState& outputIndependentState) {
456 hwc_color_t color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
457 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
458 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
459 255};
460
461 if (auto error = hwcLayer->setColor(color); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700462 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800463 to_string(error).c_str(), static_cast<int32_t>(error));
464 }
465}
466
467void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
468 const LayerFECompositionState& outputIndependentState) {
469 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
470 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700471 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800472 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
473 static_cast<int32_t>(error));
474 }
475}
476
477void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
478 const LayerFECompositionState& outputIndependentState) {
479 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700480 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
482 outputIndependentState.hdrMetadata);
483 error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700484 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800485 to_string(error).c_str(), static_cast<int32_t>(error));
486 }
487
488 uint32_t hwcSlot = 0;
489 sp<GraphicBuffer> hwcBuffer;
490 // We need access to the output-dependent state for the buffer cache there,
491 // though otherwise the buffer is not output-dependent.
492 editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot,
493 outputIndependentState.buffer, &hwcSlot,
494 &hwcBuffer);
495
496 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, outputIndependentState.acquireFence);
497 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700498 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800499 outputIndependentState.buffer->handle, to_string(error).c_str(),
500 static_cast<int32_t>(error));
501 }
502}
503
504void OutputLayer::writeCompositionTypeToHWC(
505 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
506 auto& outputDependentState = editState();
507
508 // If we are forcing client composition, we need to tell the HWC
509 if (outputDependentState.forceClientComposition) {
510 requestedCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
511 }
512
513 // Set the requested composition type with the HWC whenever it changes
514 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) {
515 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
516
517 if (auto error = hwcLayer->setCompositionType(
518 static_cast<HWC2::Composition>(requestedCompositionType));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800519 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700520 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800521 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800522 static_cast<int32_t>(error));
523 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800524 }
525}
526
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800527void OutputLayer::writeCursorPositionToHWC() const {
528 // Skip doing this if there is no HWC interface
529 auto hwcLayer = getHwcLayer();
530 if (!hwcLayer) {
531 return;
532 }
533
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700534 const auto& layerFEState = getLayer().getFEState();
535 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800536
537 Rect frame = layerFEState.cursorFrame;
538 frame.intersect(outputState.viewport, &frame);
539 Rect position = outputState.transform.transform(frame);
540
541 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
542 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700543 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
544 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
545 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800546 }
547}
548
Lloyd Pique66d68602019-02-13 14:23:31 -0800549HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700550 const auto& state = getState();
551 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800552}
553
554bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700555 const auto& state = getState();
556 return !state.hwc ||
557 state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800558}
559
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800560bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700561 const auto& state = getState();
562 return state.hwc && state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800563}
564
Lloyd Pique66d68602019-02-13 14:23:31 -0800565void OutputLayer::detectDisallowedCompositionTypeChange(
566 Hwc2::IComposerClient::Composition from, Hwc2::IComposerClient::Composition to) const {
567 bool result = false;
568 switch (from) {
569 case Hwc2::IComposerClient::Composition::INVALID:
570 case Hwc2::IComposerClient::Composition::CLIENT:
571 result = false;
572 break;
573
574 case Hwc2::IComposerClient::Composition::DEVICE:
575 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
576 result = (to == Hwc2::IComposerClient::Composition::CLIENT);
577 break;
578
579 case Hwc2::IComposerClient::Composition::CURSOR:
580 case Hwc2::IComposerClient::Composition::SIDEBAND:
581 result = (to == Hwc2::IComposerClient::Composition::CLIENT ||
582 to == Hwc2::IComposerClient::Composition::DEVICE);
583 break;
584 }
585
586 if (!result) {
587 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700588 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800589 toString(to).c_str(), static_cast<int>(to));
590 }
591}
592
593void OutputLayer::applyDeviceCompositionTypeChange(
594 Hwc2::IComposerClient::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700595 auto& state = editState();
596 LOG_FATAL_IF(!state.hwc);
597 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800598
599 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
600
601 hwcState.hwcCompositionType = compositionType;
602}
603
604void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700605 auto& state = editState();
606 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800607}
608
609void OutputLayer::applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700610 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800611 switch (request) {
612 case Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700613 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800614 break;
615
616 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700617 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800618 toString(request).c_str(), static_cast<int>(request));
619 break;
620 }
621}
622
Lloyd Pique688abd42019-02-15 15:42:24 -0800623bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700624 const auto& state = getState();
625 const auto& displayFrame = state.displayFrame;
626 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800627 return sourceCrop.getHeight() != displayFrame.getHeight() ||
628 sourceCrop.getWidth() != displayFrame.getWidth();
629}
630
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800631void OutputLayer::dump(std::string& out) const {
632 using android::base::StringAppendF;
633
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700634 StringAppendF(&out, " - Output Layer %p (Composition layer %p) (%s)\n", this, &getLayer(),
635 getLayerFE().getDebugName());
636 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800637}
638
Lloyd Piquecc01a452018-12-04 17:24:00 -0800639} // namespace impl
640} // namespace android::compositionengine