blob: ac66d8cbe3c61a1bb7163fa10847b54e9c971cbb [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080021#include <android-base/stringprintf.h>
Lloyd Piquef5275482019-01-29 18:42:42 -080022#include <compositionengine/DisplayColorProfile.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080023#include <compositionengine/Layer.h>
24#include <compositionengine/LayerFE.h>
Lloyd Pique9755fb72019-03-26 14:44:40 -070025#include <compositionengine/LayerFECompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080026#include <compositionengine/Output.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080027#include <compositionengine/impl/OutputCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080028#include <compositionengine/impl/OutputLayer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080029#include <compositionengine/impl/OutputLayerCompositionState.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080030
Lloyd Pique07e33212018-12-18 16:33:37 -080031#include "DisplayHardware/HWComposer.h"
32
Lloyd Piquecc01a452018-12-04 17:24:00 -080033namespace android::compositionengine {
34
35OutputLayer::~OutputLayer() = default;
36
37namespace impl {
38
Lloyd Piquea83776c2019-01-29 18:42:32 -080039namespace {
40
41FloatRect reduce(const FloatRect& win, const Region& exclude) {
42 if (CC_LIKELY(exclude.isEmpty())) {
43 return win;
44 }
45 // Convert through Rect (by rounding) for lack of FloatRegion
46 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
47}
48
49} // namespace
50
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070051std::unique_ptr<OutputLayer> createOutputLayer(
Lloyd Piquedf336d92019-03-07 21:38:42 -080052 const compositionengine::Output& output,
53 const std::shared_ptr<compositionengine::Layer>& layer,
54 const sp<compositionengine::LayerFE>& layerFE) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070055 return createOutputLayerTemplated<OutputLayer>(output, layer, layerFE);
Lloyd Piquecc01a452018-12-04 17:24:00 -080056}
57
Lloyd Piquecc01a452018-12-04 17:24:00 -080058OutputLayer::~OutputLayer() = default;
59
Lloyd Piquedf336d92019-03-07 21:38:42 -080060void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070061 auto& state = editState();
Lloyd Piquedf336d92019-03-07 21:38:42 -080062 if (hwcLayer) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070063 state.hwc.emplace(std::move(hwcLayer));
Lloyd Piquedf336d92019-03-07 21:38:42 -080064 } else {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070065 state.hwc.reset();
Lloyd Pique07e33212018-12-18 16:33:37 -080066 }
Lloyd Pique07e33212018-12-18 16:33:37 -080067}
68
Lloyd Piquea83776c2019-01-29 18:42:32 -080069Rect OutputLayer::calculateInitialCrop() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070070 const auto& layerState = getLayer().getFEState();
Lloyd Piquea83776c2019-01-29 18:42:32 -080071
72 // apply the projection's clipping to the window crop in
73 // layerstack space, and convert-back to layer space.
74 // if there are no window scaling involved, this operation will map to full
75 // pixels in the buffer.
76
77 FloatRect activeCropFloat =
Lloyd Piquec6687342019-03-07 21:34:57 -080078 reduce(layerState.geomLayerBounds, layerState.transparentRegionHint);
Lloyd Piquea83776c2019-01-29 18:42:32 -080079
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070080 const Rect& viewport = getOutput().getState().viewport;
Lloyd Piquea83776c2019-01-29 18:42:32 -080081 const ui::Transform& layerTransform = layerState.geomLayerTransform;
82 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
83 // Transform to screen space.
84 activeCropFloat = layerTransform.transform(activeCropFloat);
85 activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect());
86 // Back to layer space to work with the content crop.
87 activeCropFloat = inverseLayerTransform.transform(activeCropFloat);
88
89 // This needs to be here as transform.transform(Rect) computes the
90 // transformed rect and then takes the bounding box of the result before
91 // returning. This means
92 // transform.inverse().transform(transform.transform(Rect)) != Rect
93 // in which case we need to make sure the final rect is clipped to the
94 // display bounds.
95 Rect activeCrop{activeCropFloat};
96 if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) {
97 activeCrop.clear();
98 }
99 return activeCrop;
100}
101
102FloatRect OutputLayer::calculateOutputSourceCrop() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700103 const auto& layerState = getLayer().getFEState();
104 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800105
106 if (!layerState.geomUsesSourceCrop) {
107 return {};
108 }
109
110 // the content crop is the area of the content that gets scaled to the
111 // layer's size. This is in buffer space.
112 FloatRect crop = layerState.geomContentCrop.toFloatRect();
113
114 // In addition there is a WM-specified crop we pull from our drawing state.
115 Rect activeCrop = calculateInitialCrop();
116 const Rect& bufferSize = layerState.geomBufferSize;
117
118 int winWidth = bufferSize.getWidth();
119 int winHeight = bufferSize.getHeight();
120
121 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1])
122 // if display frame hasn't been set and the parent is an unbounded layer.
123 if (winWidth < 0 && winHeight < 0) {
124 return crop;
125 }
126
127 // Transform the window crop to match the buffer coordinate system,
128 // which means using the inverse of the current transform set on the
129 // SurfaceFlingerConsumer.
130 uint32_t invTransform = layerState.geomBufferTransform;
131 if (layerState.geomBufferUsesDisplayInverseTransform) {
132 /*
133 * the code below applies the primary display's inverse transform to the
134 * buffer
135 */
136 uint32_t invTransformOrient = outputState.orientation;
137 // calculate the inverse transform
138 if (invTransformOrient & HAL_TRANSFORM_ROT_90) {
139 invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
140 }
141 // and apply to the current transform
142 invTransform =
143 (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation();
144 }
145
146 if (invTransform & HAL_TRANSFORM_ROT_90) {
147 // If the activeCrop has been rotate the ends are rotated but not
148 // the space itself so when transforming ends back we can't rely on
149 // a modification of the axes of rotation. To account for this we
150 // need to reorient the inverse rotation in terms of the current
151 // axes of rotation.
152 bool is_h_flipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0;
153 bool is_v_flipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0;
154 if (is_h_flipped == is_v_flipped) {
155 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
156 }
157 std::swap(winWidth, winHeight);
158 }
159 const Rect winCrop =
160 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
161
162 // below, crop is intersected with winCrop expressed in crop's coordinate space
163 float xScale = crop.getWidth() / float(winWidth);
164 float yScale = crop.getHeight() / float(winHeight);
165
166 float insetL = winCrop.left * xScale;
167 float insetT = winCrop.top * yScale;
168 float insetR = (winWidth - winCrop.right) * xScale;
169 float insetB = (winHeight - winCrop.bottom) * yScale;
170
171 crop.left += insetL;
172 crop.top += insetT;
173 crop.right -= insetR;
174 crop.bottom -= insetB;
175
176 return crop;
177}
178
179Rect OutputLayer::calculateOutputDisplayFrame() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700180 const auto& layerState = getLayer().getFEState();
181 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800182
183 // apply the layer's transform, followed by the display's global transform
184 // here we're guaranteed that the layer's transform preserves rects
Lloyd Piquec6687342019-03-07 21:34:57 -0800185 Region activeTransparentRegion = layerState.transparentRegionHint;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800186 const ui::Transform& layerTransform = layerState.geomLayerTransform;
187 const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform;
188 const Rect& bufferSize = layerState.geomBufferSize;
189 Rect activeCrop = layerState.geomCrop;
190 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
191 activeCrop = layerTransform.transform(activeCrop);
192 if (!activeCrop.intersect(outputState.viewport, &activeCrop)) {
193 activeCrop.clear();
194 }
195 activeCrop = inverseLayerTransform.transform(activeCrop, true);
196 // This needs to be here as transform.transform(Rect) computes the
197 // transformed rect and then takes the bounding box of the result before
198 // returning. This means
199 // transform.inverse().transform(transform.transform(Rect)) != Rect
200 // in which case we need to make sure the final rect is clipped to the
201 // display bounds.
202 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
203 activeCrop.clear();
204 }
205 // mark regions outside the crop as transparent
206 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
207 activeTransparentRegion.orSelf(
208 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
209 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
210 activeTransparentRegion.orSelf(
211 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
212 }
213
214 // reduce uses a FloatRect to provide more accuracy during the
215 // transformation. We then round upon constructing 'frame'.
216 Rect frame{
217 layerTransform.transform(reduce(layerState.geomLayerBounds, activeTransparentRegion))};
218 if (!frame.intersect(outputState.viewport, &frame)) {
219 frame.clear();
220 }
221 const ui::Transform displayTransform{outputState.transform};
222
223 return displayTransform.transform(frame);
224}
225
226uint32_t OutputLayer::calculateOutputRelativeBufferTransform() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700227 const auto& layerState = getLayer().getFEState();
228 const auto& outputState = getOutput().getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800229
230 /*
231 * Transformations are applied in this order:
232 * 1) buffer orientation/flip/mirror
233 * 2) state transformation (window manager)
234 * 3) layer orientation (screen orientation)
235 * (NOTE: the matrices are multiplied in reverse order)
236 */
237 const ui::Transform& layerTransform = layerState.geomLayerTransform;
Rashed Abdel-Tawab6643cd82019-10-29 10:01:56 -0700238 const ui::Transform displayTransform{outputState.transform};
Lloyd Piquea83776c2019-01-29 18:42:32 -0800239 const ui::Transform bufferTransform{layerState.geomBufferTransform};
240 ui::Transform transform(displayTransform * layerTransform * bufferTransform);
241
242 if (layerState.geomBufferUsesDisplayInverseTransform) {
243 /*
244 * the code below applies the primary display's inverse transform to the
245 * buffer
246 */
247 uint32_t invTransform = outputState.orientation;
248 // calculate the inverse transform
249 if (invTransform & HAL_TRANSFORM_ROT_90) {
250 invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H;
251 }
252
253 /*
254 * Here we cancel out the orientation component of the WM transform.
255 * The scaling and translate components are already included in our bounds
256 * computation so it's enough to just omit it in the composition.
257 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
258 */
Lloyd Pique546a2452019-03-18 20:53:27 +0000259 transform = ui::Transform(invTransform) * displayTransform * bufferTransform;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800260 }
261
262 // this gives us only the "orientation" component of the transform
263 return transform.getOrientation();
264} // namespace impl
265
Lloyd Pique7a234912019-10-03 11:54:27 -0700266void OutputLayer::updateCompositionState(bool includeGeometry, bool forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700267 const auto& layerFEState = getLayer().getFEState();
268 const auto& outputState = getOutput().getState();
269 const auto& profile = *getOutput().getDisplayColorProfile();
270 auto& state = editState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800271
Lloyd Piquea83776c2019-01-29 18:42:32 -0800272 if (includeGeometry) {
Lloyd Piquefe671022019-09-24 10:43:03 -0700273 // Clear the forceClientComposition flag before it is set for any
274 // reason. Note that since it can be set by some checks below when
275 // updating the geometry state, we only clear it when updating the
276 // geometry since those conditions for forcing client composition won't
277 // go away otherwise.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700278 state.forceClientComposition = false;
Lloyd Piquefe671022019-09-24 10:43:03 -0700279
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700280 state.displayFrame = calculateOutputDisplayFrame();
281 state.sourceCrop = calculateOutputSourceCrop();
282 state.bufferTransform =
Lloyd Piquea83776c2019-01-29 18:42:32 -0800283 static_cast<Hwc2::Transform>(calculateOutputRelativeBufferTransform());
284
Lloyd Piquef5275482019-01-29 18:42:42 -0800285 if ((layerFEState.isSecure && !outputState.isSecure) ||
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700286 (state.bufferTransform & ui::Transform::ROT_INVALID)) {
287 state.forceClientComposition = true;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800288 }
289 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800290
291 // Determine the output dependent dataspace for this layer. If it is
292 // colorspace agnostic, it just uses the dataspace chosen for the output to
293 // avoid the need for color conversion.
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700294 state.dataspace = layerFEState.isColorspaceAgnostic &&
Lloyd Piquef5275482019-01-29 18:42:42 -0800295 outputState.targetDataspace != ui::Dataspace::UNKNOWN
296 ? outputState.targetDataspace
297 : layerFEState.dataspace;
298
Lloyd Piquef5275482019-01-29 18:42:42 -0800299 // These are evaluated every frame as they can potentially change at any
300 // time.
Lloyd Pique7a234912019-10-03 11:54:27 -0700301 if (layerFEState.forceClientComposition || !profile.isDataspaceSupported(state.dataspace) ||
302 forceClientComposition) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700303 state.forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800304 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800305}
306
Lloyd Piquef5275482019-01-29 18:42:42 -0800307void OutputLayer::writeStateToHWC(bool includeGeometry) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700308 const auto& state = getState();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800309 // Skip doing this if there is no HWC interface
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700310 if (!state.hwc) {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800311 return;
312 }
313
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700314 auto& hwcLayer = (*state.hwc).hwcLayer;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800315 if (!hwcLayer) {
316 ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700317 getLayerFE().getDebugName(), getOutput().getName().c_str());
Lloyd Piquea83776c2019-01-29 18:42:32 -0800318 return;
319 }
320
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700321 const auto& outputIndependentState = getLayer().getFEState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800322 auto requestedCompositionType = outputIndependentState.compositionType;
323
Lloyd Piquea83776c2019-01-29 18:42:32 -0800324 if (includeGeometry) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800325 writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType);
326 writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), outputIndependentState);
327 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800328
Lloyd Piquef5275482019-01-29 18:42:42 -0800329 writeOutputDependentPerFrameStateToHWC(hwcLayer.get());
330 writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800331
Lloyd Piquef5275482019-01-29 18:42:42 -0800332 writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType);
Lloyd Pique46b72df2019-10-29 13:19:27 -0700333
334 // Always set the layer color after setting the composition type.
335 writeSolidColorStateToHWC(hwcLayer.get(), outputIndependentState);
Lloyd Piquef5275482019-01-29 18:42:42 -0800336}
Lloyd Piquea83776c2019-01-29 18:42:32 -0800337
Lloyd Piquef5275482019-01-29 18:42:42 -0800338void OutputLayer::writeOutputDependentGeometryStateToHWC(
339 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
340 const auto& outputDependentState = getState();
341
342 if (auto error = hwcLayer->setDisplayFrame(outputDependentState.displayFrame);
343 error != HWC2::Error::None) {
344 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700345 getLayerFE().getDebugName(), outputDependentState.displayFrame.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800346 outputDependentState.displayFrame.top, outputDependentState.displayFrame.right,
347 outputDependentState.displayFrame.bottom, to_string(error).c_str(),
348 static_cast<int32_t>(error));
349 }
350
351 if (auto error = hwcLayer->setSourceCrop(outputDependentState.sourceCrop);
352 error != HWC2::Error::None) {
353 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
354 "%s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700355 getLayerFE().getDebugName(), outputDependentState.sourceCrop.left,
Lloyd Piquef5275482019-01-29 18:42:42 -0800356 outputDependentState.sourceCrop.top, outputDependentState.sourceCrop.right,
357 outputDependentState.sourceCrop.bottom, to_string(error).c_str(),
358 static_cast<int32_t>(error));
359 }
360
361 if (auto error = hwcLayer->setZOrder(outputDependentState.z); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700362 ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(),
363 outputDependentState.z, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800364 }
365
366 // Solid-color layers should always use an identity transform.
367 const auto bufferTransform =
368 requestedCompositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR
369 ? outputDependentState.bufferTransform
370 : static_cast<Hwc2::Transform>(0);
371 if (auto error = hwcLayer->setTransform(static_cast<HWC2::Transform>(bufferTransform));
372 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700373 ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800374 toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(),
375 static_cast<int32_t>(error));
376 }
377}
378
379void OutputLayer::writeOutputIndependentGeometryStateToHWC(
380 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
381 if (auto error = hwcLayer->setBlendMode(
382 static_cast<HWC2::BlendMode>(outputIndependentState.blendMode));
383 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700384 ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800385 toString(outputIndependentState.blendMode).c_str(), to_string(error).c_str(),
386 static_cast<int32_t>(error));
387 }
388
389 if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha);
390 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700391 ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800392 outputIndependentState.alpha, to_string(error).c_str(), static_cast<int32_t>(error));
393 }
394
395 if (auto error = hwcLayer->setInfo(outputIndependentState.type, outputIndependentState.appId);
396 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700397 ALOGE("[%s] Failed to set info %s (%d)", getLayerFE().getDebugName(),
398 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800399 }
400}
401
402void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) {
403 const auto& outputDependentState = getState();
404
Lloyd Piquea2468662019-03-07 21:31:06 -0800405 // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry
Lloyd Piquef5275482019-01-29 18:42:42 -0800406 // state and should not change every frame.
Lloyd Piquea2468662019-03-07 21:31:06 -0800407 if (auto error = hwcLayer->setVisibleRegion(outputDependentState.outputSpaceVisibleRegion);
Lloyd Piquef5275482019-01-29 18:42:42 -0800408 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700409 ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800410 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquea2468662019-03-07 21:31:06 -0800411 outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG);
Lloyd Piquef5275482019-01-29 18:42:42 -0800412 }
413
414 if (auto error = hwcLayer->setDataspace(outputDependentState.dataspace);
415 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700416 ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800417 outputDependentState.dataspace, to_string(error).c_str(),
418 static_cast<int32_t>(error));
419 }
420}
421
422void OutputLayer::writeOutputIndependentPerFrameStateToHWC(
423 HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) {
424 switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) {
425 case HWC2::Error::None:
426 break;
427 case HWC2::Error::Unsupported:
428 editState().forceClientComposition = true;
429 break;
430 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700431 ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800432 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Piquef5275482019-01-29 18:42:42 -0800433 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800434
Lloyd Piquef5275482019-01-29 18:42:42 -0800435 if (auto error = hwcLayer->setSurfaceDamage(outputIndependentState.surfaceDamage);
436 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700437 ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800438 to_string(error).c_str(), static_cast<int32_t>(error));
439 outputIndependentState.surfaceDamage.dump(LOG_TAG);
440 }
441
442 // Content-specific per-frame state
443 switch (outputIndependentState.compositionType) {
444 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
Lloyd Pique46b72df2019-10-29 13:19:27 -0700445 // For compatibility, should be written AFTER the composition type.
Lloyd Piquef5275482019-01-29 18:42:42 -0800446 break;
447 case Hwc2::IComposerClient::Composition::SIDEBAND:
448 writeSidebandStateToHWC(hwcLayer, outputIndependentState);
449 break;
450 case Hwc2::IComposerClient::Composition::CURSOR:
451 case Hwc2::IComposerClient::Composition::DEVICE:
452 writeBufferStateToHWC(hwcLayer, outputIndependentState);
453 break;
454 case Hwc2::IComposerClient::Composition::INVALID:
455 case Hwc2::IComposerClient::Composition::CLIENT:
456 // Ignored
457 break;
458 }
459}
460
461void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer,
462 const LayerFECompositionState& outputIndependentState) {
Lloyd Pique46b72df2019-10-29 13:19:27 -0700463 if (outputIndependentState.compositionType != Hwc2::IComposerClient::Composition::SOLID_COLOR) {
464 return;
465 }
466
Lloyd Piquef5275482019-01-29 18:42:42 -0800467 hwc_color_t color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)),
468 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)),
469 static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)),
470 255};
471
472 if (auto error = hwcLayer->setColor(color); error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700473 ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800474 to_string(error).c_str(), static_cast<int32_t>(error));
475 }
476}
477
478void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer,
479 const LayerFECompositionState& outputIndependentState) {
480 if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle());
481 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700482 ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800483 outputIndependentState.sidebandStream->handle(), to_string(error).c_str(),
484 static_cast<int32_t>(error));
485 }
486}
487
488void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer,
489 const LayerFECompositionState& outputIndependentState) {
490 auto supportedPerFrameMetadata =
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700491 getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata();
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata,
493 outputIndependentState.hdrMetadata);
494 error != HWC2::Error::None && error != HWC2::Error::Unsupported) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700495 ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800496 to_string(error).c_str(), static_cast<int32_t>(error));
497 }
498
499 uint32_t hwcSlot = 0;
500 sp<GraphicBuffer> hwcBuffer;
501 // We need access to the output-dependent state for the buffer cache there,
502 // though otherwise the buffer is not output-dependent.
503 editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot,
504 outputIndependentState.buffer, &hwcSlot,
505 &hwcBuffer);
506
507 if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, outputIndependentState.acquireFence);
508 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700509 ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800510 outputIndependentState.buffer->handle, to_string(error).c_str(),
511 static_cast<int32_t>(error));
512 }
513}
514
515void OutputLayer::writeCompositionTypeToHWC(
516 HWC2::Layer* hwcLayer, Hwc2::IComposerClient::Composition requestedCompositionType) {
517 auto& outputDependentState = editState();
518
519 // If we are forcing client composition, we need to tell the HWC
520 if (outputDependentState.forceClientComposition) {
521 requestedCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
522 }
523
524 // Set the requested composition type with the HWC whenever it changes
525 if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) {
526 outputDependentState.hwc->hwcCompositionType = requestedCompositionType;
527
528 if (auto error = hwcLayer->setCompositionType(
529 static_cast<HWC2::Composition>(requestedCompositionType));
Lloyd Piquea83776c2019-01-29 18:42:32 -0800530 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700531 ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(),
Lloyd Piquef5275482019-01-29 18:42:42 -0800532 toString(requestedCompositionType).c_str(), to_string(error).c_str(),
Lloyd Piquea83776c2019-01-29 18:42:32 -0800533 static_cast<int32_t>(error));
534 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800535 }
536}
537
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800538void OutputLayer::writeCursorPositionToHWC() const {
539 // Skip doing this if there is no HWC interface
540 auto hwcLayer = getHwcLayer();
541 if (!hwcLayer) {
542 return;
543 }
544
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700545 const auto& layerFEState = getLayer().getFEState();
546 const auto& outputState = getOutput().getState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800547
548 Rect frame = layerFEState.cursorFrame;
549 frame.intersect(outputState.viewport, &frame);
550 Rect position = outputState.transform.transform(frame);
551
552 if (auto error = hwcLayer->setCursorPosition(position.left, position.top);
553 error != HWC2::Error::None) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700554 ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)",
555 getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(),
556 static_cast<int32_t>(error));
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800557 }
558}
559
Lloyd Pique66d68602019-02-13 14:23:31 -0800560HWC2::Layer* OutputLayer::getHwcLayer() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700561 const auto& state = getState();
562 return state.hwc ? state.hwc->hwcLayer.get() : nullptr;
Lloyd Pique66d68602019-02-13 14:23:31 -0800563}
564
565bool OutputLayer::requiresClientComposition() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700566 const auto& state = getState();
567 return !state.hwc ||
568 state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT;
Lloyd Pique66d68602019-02-13 14:23:31 -0800569}
570
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800571bool OutputLayer::isHardwareCursor() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700572 const auto& state = getState();
573 return state.hwc && state.hwc->hwcCompositionType == Hwc2::IComposerClient::Composition::CURSOR;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800574}
575
Lloyd Pique66d68602019-02-13 14:23:31 -0800576void OutputLayer::detectDisallowedCompositionTypeChange(
577 Hwc2::IComposerClient::Composition from, Hwc2::IComposerClient::Composition to) const {
578 bool result = false;
579 switch (from) {
580 case Hwc2::IComposerClient::Composition::INVALID:
581 case Hwc2::IComposerClient::Composition::CLIENT:
582 result = false;
583 break;
584
585 case Hwc2::IComposerClient::Composition::DEVICE:
586 case Hwc2::IComposerClient::Composition::SOLID_COLOR:
587 result = (to == Hwc2::IComposerClient::Composition::CLIENT);
588 break;
589
590 case Hwc2::IComposerClient::Composition::CURSOR:
591 case Hwc2::IComposerClient::Composition::SIDEBAND:
592 result = (to == Hwc2::IComposerClient::Composition::CLIENT ||
593 to == Hwc2::IComposerClient::Composition::DEVICE);
594 break;
595 }
596
597 if (!result) {
598 ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)",
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700599 getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from),
Lloyd Pique66d68602019-02-13 14:23:31 -0800600 toString(to).c_str(), static_cast<int>(to));
601 }
602}
603
604void OutputLayer::applyDeviceCompositionTypeChange(
605 Hwc2::IComposerClient::Composition compositionType) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700606 auto& state = editState();
607 LOG_FATAL_IF(!state.hwc);
608 auto& hwcState = *state.hwc;
Lloyd Pique66d68602019-02-13 14:23:31 -0800609
610 detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType);
611
612 hwcState.hwcCompositionType = compositionType;
613}
614
615void OutputLayer::prepareForDeviceLayerRequests() {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700616 auto& state = editState();
617 state.clearClientTarget = false;
Lloyd Pique66d68602019-02-13 14:23:31 -0800618}
619
620void OutputLayer::applyDeviceLayerRequest(Hwc2::IComposerClient::LayerRequest request) {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700621 auto& state = editState();
Lloyd Pique66d68602019-02-13 14:23:31 -0800622 switch (request) {
623 case Hwc2::IComposerClient::LayerRequest::CLEAR_CLIENT_TARGET:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700624 state.clearClientTarget = true;
Lloyd Pique66d68602019-02-13 14:23:31 -0800625 break;
626
627 default:
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700628 ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(),
Lloyd Pique66d68602019-02-13 14:23:31 -0800629 toString(request).c_str(), static_cast<int>(request));
630 break;
631 }
632}
633
Lloyd Pique688abd42019-02-15 15:42:24 -0800634bool OutputLayer::needsFiltering() const {
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700635 const auto& state = getState();
636 const auto& displayFrame = state.displayFrame;
637 const auto& sourceCrop = state.sourceCrop;
Lloyd Pique688abd42019-02-15 15:42:24 -0800638 return sourceCrop.getHeight() != displayFrame.getHeight() ||
639 sourceCrop.getWidth() != displayFrame.getWidth();
640}
641
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800642void OutputLayer::dump(std::string& out) const {
643 using android::base::StringAppendF;
644
Lloyd Piquea38ea7e2019-04-16 18:10:26 -0700645 StringAppendF(&out, " - Output Layer %p (Composition layer %p) (%s)\n", this, &getLayer(),
646 getLayerFE().getDebugName());
647 dumpState(out);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800648}
649
Lloyd Piquecc01a452018-12-04 17:24:00 -0800650} // namespace impl
651} // namespace android::compositionengine
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800652
653// TODO(b/129481165): remove the #pragma below and fix conversion issues
654#pragma clang diagnostic pop // ignored "-Wconversion"