blob: 0124e5b5d9777c9436054e615f1abdcad2166dda [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
262void OutputLayer::updateCompositionState(bool includeGeometry) {
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 Piquea38ea7e2019-04-16 18:10:26 -0700297 if (layerFEState.forceClientComposition || !profile.isDataspaceSupported(state.dataspace)) {
298 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800299 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800300}
301
Lloyd Piquef5275482019-01-29 18:42:42 -0800302void OutputLayer::writeStateToHWC(bool includeGeometry) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700303 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800304 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700305 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800306 return;
307 }
308
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700309 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800310 if (!hwcLayer) {
311 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700312 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800313 return;
314 }
315
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700316 const auto& outputIndependentState = getLayer().getFEState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800317 auto requestedCompositionType = outputIndependentState.compositionType;
318
Lloyd Piquea83776c2019-01-29 18:42:32 -0800319 if (includeGeometry) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800320 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType);
321 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), outputIndependentState);
322 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800323
Lloyd Piquef5275482019-01-29 18:42:42 -0800324 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
325 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800326
Lloyd Piquef5275482019-01-29 18:42:42 -0800327 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType);
328}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800329
Lloyd Piquef5275482019-01-29 18:42:42 -0800330void OutputLayer::writeOutputDependentGeometryStateToHWC(
331 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
332 const auto& outputDependentState = getState();
333
334 if (auto error = hwcLayer->setDisplayFrame(outputDependentState.displayFrame);
335 error != HWC2::Error::None) {
336 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700337 getLayerFE().getDebugName(), outputDependentState.displayFrame.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800338 outputDependentState.displayFrame.top, outputDependentState.displayFrame.right,
339 outputDependentState.displayFrame.bottom, to_string(error).c_str(),
340 static_cast<int32_t>(error));
341 }
342
343 if (auto error = hwcLayer->setSourceCrop(outputDependentState.sourceCrop);
344 error != HWC2::Error::None) {
345 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
346 "%s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700347 getLayerFE().getDebugName(), outputDependentState.sourceCrop.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800348 outputDependentState.sourceCrop.top, outputDependentState.sourceCrop.right,
349 outputDependentState.sourceCrop.bottom, to_string(error).c_str(),
350 static_cast<int32_t>(error));
351 }
352
353 if (auto error = hwcLayer->setZOrder(outputDependentState.z); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700354 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(),
355 outputDependentState.z, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800356 }
357
358 // Solid-color layers should always use an identity transform.
359 const auto bufferTransform =
360 requestedCompositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR
361 ? outputDependentState.bufferTransform
362 : static_cast<Hwc2::Transform>(0);
363 if (auto error = hwcLayer->setTransform(static_cast<HWC2::Transform>(bufferTransform));
364 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700365 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800366 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
367 static_cast<int32_t>(error));
368 }
369}
370
371void OutputLayer::writeOutputIndependentGeometryStateToHWC(
372 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
373 if (auto error = hwcLayer->setBlendMode(
374 static_cast<HWC2::BlendMode>(outputIndependentState.blendMode));
375 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700376 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800377 toString(outputIndependentState.blendMode).c_str(), to_string(error).c_str(),
378 static_cast<int32_t>(error));
379 }
380
381 if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha);
382 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700383 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800384 outputIndependentState.alpha, to_string(error).c_str(), static_cast<int32_t>(error));
385 }
386
387 if (auto error = hwcLayer->setInfo(outputIndependentState.type, outputIndependentState.appId);
388 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700389 ALOGE("[%s] Failed to set info %s (%d)", getLayerFE().getDebugName(),
390 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800391 }
392}
393
394void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
395 const auto& outputDependentState = getState();
396
Lloyd Piquea2468662019-03-07 21:31:06 -0800397 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800398 // state and should not change every frame.
Lloyd Piquea2468662019-03-07 21:31:06 -0800399 if (auto error = hwcLayer->setVisibleRegion(outputDependentState.outputSpaceVisibleRegion);
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 visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800402 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800403 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800404 }
405
406 if (auto error = hwcLayer->setDataspace(outputDependentState.dataspace);
407 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700408 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800409 outputDependentState.dataspace, to_string(error).c_str(),
410 static_cast<int32_t>(error));
411 }
412}
413
414void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
415 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
416 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
417 case HWC2::Error::None:
418 break;
419 case HWC2::Error::Unsupported:
420 editState().forceClientComposition = true;
421 break;
422 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700423 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800424 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800425 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800426
Lloyd Piquef5275482019-01-29 18:42:42 -0800427 if (auto error = hwcLayer->setSurfaceDamage(outputIndependentState.surfaceDamage);
428 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700429 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800430 to_string(error).c_str(), static_cast<int32_t>(error));
431 outputIndependentState.surfaceDamage.dump(LOG_TAG);
432 }
433
434 // Content-specific per-frame state
435 switch (outputIndependentState.compositionType) {
436 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
437 writeSolidColorStateToHWC(hwcLayer, outputIndependentState);
438 break;
439 case Hwc2::IComposerClient::Composition::SIDEBAND:
440 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
441 break;
442 case Hwc2::IComposerClient::Composition::CURSOR:
443 case Hwc2::IComposerClient::Composition::DEVICE:
444 writeBufferStateToHWC(hwcLayer, outputIndependentState);
445 break;
446 case Hwc2::IComposerClient::Composition::INVALID:
447 case Hwc2::IComposerClient::Composition::CLIENT:
448 // Ignored
449 break;
450 }
451}
452
453void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
454 const LayerFECompositionState& outputIndependentState) {
455 hwc_color_t color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
456 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
457 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
458 255};
459
460 if (auto error = hwcLayer->setColor(color); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700461 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800462 to_string(error).c_str(), static_cast<int32_t>(error));
463 }
464}
465
466void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
467 const LayerFECompositionState& outputIndependentState) {
468 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
469 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700470 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800471 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
472 static_cast<int32_t>(error));
473 }
474}
475
476void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
477 const LayerFECompositionState& outputIndependentState) {
478 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700479 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800480 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
481 outputIndependentState.hdrMetadata);
482 error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700483 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800484 to_string(error).c_str(), static_cast<int32_t>(error));
485 }
486
487 uint32_t hwcSlot = 0;
488 sp<GraphicBuffer> hwcBuffer;
489 // We need access to the output-dependent state for the buffer cache there,
490 // though otherwise the buffer is not output-dependent.
491 editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot,
492 outputIndependentState.buffer, &hwcSlot,
493 &hwcBuffer);
494
495 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, outputIndependentState.acquireFence);
496 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700497 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800498 outputIndependentState.buffer->handle, to_string(error).c_str(),
499 static_cast<int32_t>(error));
500 }
501}
502
503void OutputLayer::writeCompositionTypeToHWC(
504 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
505 auto& outputDependentState = editState();
506
507 // If we are forcing client composition, we need to tell the HWC
508 if (outputDependentState.forceClientComposition) {
509 requestedCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
510 }
511
512 // Set the requested composition type with the HWC whenever it changes
513 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) {
514 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
515
516 if (auto error = hwcLayer->setCompositionType(
517 static_cast<HWC2::Composition>(requestedCompositionType));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800518 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700519 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800520 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800521 static_cast<int32_t>(error));
522 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800523 }
524}
525
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800526void OutputLayer::writeCursorPositionToHWC() const {
527 // Skip doing this if there is no HWC interface
528 auto hwcLayer = getHwcLayer();
529 if (!hwcLayer) {
530 return;
531 }
532
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700533 const auto& layerFEState = getLayer().getFEState();
534 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800535
536 Rect frame = layerFEState.cursorFrame;
537 frame.intersect(outputState.viewport, &frame);
538 Rect position = outputState.transform.transform(frame);
539
540 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
541 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700542 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
543 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
544 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800545 }
546}
547
Lloyd Pique66d68602019-02-13 14:23:31 -0800548HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700549 const auto& state = getState();
550 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800551}
552
553bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700554 const auto& state = getState();
555 return !state.hwc ||
556 state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800557}
558
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800559bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700560 const auto& state = getState();
561 return state.hwc && state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800562}
563
Lloyd Pique66d68602019-02-13 14:23:31 -0800564void OutputLayer::detectDisallowedCompositionTypeChange(
565 Hwc2::IComposerClient::Composition from, Hwc2::IComposerClient::Composition to) const {
566 bool result = false;
567 switch (from) {
568 case Hwc2::IComposerClient::Composition::INVALID:
569 case Hwc2::IComposerClient::Composition::CLIENT:
570 result = false;
571 break;
572
573 case Hwc2::IComposerClient::Composition::DEVICE:
574 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
575 result = (to == Hwc2::IComposerClient::Composition::CLIENT);
576 break;
577
578 case Hwc2::IComposerClient::Composition::CURSOR:
579 case Hwc2::IComposerClient::Composition::SIDEBAND:
580 result = (to == Hwc2::IComposerClient::Composition::CLIENT ||
581 to == Hwc2::IComposerClient::Composition::DEVICE);
582 break;
583 }
584
585 if (!result) {
586 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700587 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800588 toString(to).c_str(), static_cast<int>(to));
589 }
590}
591
592void OutputLayer::applyDeviceCompositionTypeChange(
593 Hwc2::IComposerClient::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700594 auto& state = editState();
595 LOG_FATAL_IF(!state.hwc);
596 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800597
598 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
599
600 hwcState.hwcCompositionType = compositionType;
601}
602
603void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700604 auto& state = editState();
605 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800606}
607
608void OutputLayer::applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700609 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800610 switch (request) {
611 case Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700612 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800613 break;
614
615 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700616 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800617 toString(request).c_str(), static_cast<int>(request));
618 break;
619 }
620}
621
Lloyd Pique688abd42019-02-15 15:42:24 -0800622bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700623 const auto& state = getState();
624 const auto& displayFrame = state.displayFrame;
625 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800626 return sourceCrop.getHeight() != displayFrame.getHeight() ||
627 sourceCrop.getWidth() != displayFrame.getWidth();
628}
629
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800630void OutputLayer::dump(std::string& out) const {
631 using android::base::StringAppendF;
632
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700633 StringAppendF(&out, " - Output Layer %p (Composition layer %p) (%s)\n", this, &getLayer(),
634 getLayerFE().getDebugName());
635 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800636}
637
Lloyd Piquecc01a452018-12-04 17:24:00 -0800638} // namespace impl
639} // namespace android::compositionengine