Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 17 | #include <android-base/stringprintf.h> |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 18 | #include <compositionengine/DisplayColorProfile.h> |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 19 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 20 | #include <compositionengine/Output.h> |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 21 | #include <compositionengine/impl/OutputCompositionState.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 22 | #include <compositionengine/impl/OutputLayer.h> |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 23 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 24 | #include <cstdint> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 25 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 26 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 27 | #pragma clang diagnostic push |
| 28 | #pragma clang diagnostic ignored "-Wconversion" |
| 29 | |
Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 30 | #include "DisplayHardware/HWComposer.h" |
| 31 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 32 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 33 | #pragma clang diagnostic pop // ignored "-Wconversion" |
| 34 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 35 | namespace android::compositionengine { |
| 36 | |
| 37 | OutputLayer::~OutputLayer() = default; |
| 38 | |
| 39 | namespace impl { |
| 40 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 41 | namespace { |
| 42 | |
| 43 | FloatRect reduce(const FloatRect& win, const Region& exclude) { |
| 44 | if (CC_LIKELY(exclude.isEmpty())) { |
| 45 | return win; |
| 46 | } |
| 47 | // Convert through Rect (by rounding) for lack of FloatRegion |
| 48 | return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect(); |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 53 | std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output, |
| 54 | const sp<compositionengine::LayerFE>& layerFE) { |
| 55 | return createOutputLayerTemplated<OutputLayer>(output, layerFE); |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 58 | OutputLayer::~OutputLayer() = default; |
| 59 | |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 60 | void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 61 | auto& state = editState(); |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 62 | if (hwcLayer) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 63 | state.hwc.emplace(std::move(hwcLayer)); |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 64 | } else { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 65 | state.hwc.reset(); |
Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 66 | } |
Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 69 | Rect OutputLayer::calculateInitialCrop() const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 70 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 71 | |
| 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 Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 78 | reduce(layerState.geomLayerBounds, layerState.transparentRegionHint); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 79 | |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 80 | const Rect& viewport = getOutput().getState().layerStackSpace.content; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 81 | 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 | |
| 102 | FloatRect OutputLayer::calculateOutputSourceCrop() const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 103 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 104 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 105 | |
| 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 | */ |
Marin Shalamanov | 68933fb | 2020-09-10 17:58:12 +0200 | [diff] [blame] | 136 | uint32_t invTransformOrient = |
| 137 | ui::Transform::toRotationFlags(outputState.displaySpace.orientation); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 138 | // calculate the inverse transform |
| 139 | if (invTransformOrient & HAL_TRANSFORM_ROT_90) { |
| 140 | invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H; |
| 141 | } |
| 142 | // and apply to the current transform |
| 143 | invTransform = |
| 144 | (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation(); |
| 145 | } |
| 146 | |
| 147 | if (invTransform & HAL_TRANSFORM_ROT_90) { |
| 148 | // If the activeCrop has been rotate the ends are rotated but not |
| 149 | // the space itself so when transforming ends back we can't rely on |
| 150 | // a modification of the axes of rotation. To account for this we |
| 151 | // need to reorient the inverse rotation in terms of the current |
| 152 | // axes of rotation. |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 153 | bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0; |
| 154 | bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0; |
| 155 | if (isHFlipped == isVFlipped) { |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 156 | invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H; |
| 157 | } |
| 158 | std::swap(winWidth, winHeight); |
| 159 | } |
| 160 | const Rect winCrop = |
| 161 | activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight()); |
| 162 | |
| 163 | // below, crop is intersected with winCrop expressed in crop's coordinate space |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 164 | const float xScale = crop.getWidth() / float(winWidth); |
| 165 | const float yScale = crop.getHeight() / float(winHeight); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 166 | |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 167 | const float insetLeft = winCrop.left * xScale; |
| 168 | const float insetTop = winCrop.top * yScale; |
| 169 | const float insetRight = (winWidth - winCrop.right) * xScale; |
| 170 | const float insetBottom = (winHeight - winCrop.bottom) * yScale; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 171 | |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 172 | crop.left += insetLeft; |
| 173 | crop.top += insetTop; |
| 174 | crop.right -= insetRight; |
| 175 | crop.bottom -= insetBottom; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 176 | |
| 177 | return crop; |
| 178 | } |
| 179 | |
| 180 | Rect OutputLayer::calculateOutputDisplayFrame() const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 181 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 182 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 183 | |
| 184 | // apply the layer's transform, followed by the display's global transform |
| 185 | // here we're guaranteed that the layer's transform preserves rects |
Lloyd Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 186 | Region activeTransparentRegion = layerState.transparentRegionHint; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 187 | const ui::Transform& layerTransform = layerState.geomLayerTransform; |
| 188 | const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform; |
| 189 | const Rect& bufferSize = layerState.geomBufferSize; |
| 190 | Rect activeCrop = layerState.geomCrop; |
| 191 | if (!activeCrop.isEmpty() && bufferSize.isValid()) { |
| 192 | activeCrop = layerTransform.transform(activeCrop); |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 193 | if (!activeCrop.intersect(outputState.layerStackSpace.content, &activeCrop)) { |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 194 | activeCrop.clear(); |
| 195 | } |
| 196 | activeCrop = inverseLayerTransform.transform(activeCrop, true); |
| 197 | // This needs to be here as transform.transform(Rect) computes the |
| 198 | // transformed rect and then takes the bounding box of the result before |
| 199 | // returning. This means |
| 200 | // transform.inverse().transform(transform.transform(Rect)) != Rect |
| 201 | // in which case we need to make sure the final rect is clipped to the |
| 202 | // display bounds. |
| 203 | if (!activeCrop.intersect(bufferSize, &activeCrop)) { |
| 204 | activeCrop.clear(); |
| 205 | } |
| 206 | // mark regions outside the crop as transparent |
| 207 | activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top)); |
| 208 | activeTransparentRegion.orSelf( |
| 209 | Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight())); |
| 210 | activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom)); |
| 211 | activeTransparentRegion.orSelf( |
| 212 | Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom)); |
| 213 | } |
| 214 | |
| 215 | // reduce uses a FloatRect to provide more accuracy during the |
| 216 | // transformation. We then round upon constructing 'frame'. |
| 217 | Rect frame{ |
| 218 | layerTransform.transform(reduce(layerState.geomLayerBounds, activeTransparentRegion))}; |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 219 | if (!frame.intersect(outputState.layerStackSpace.content, &frame)) { |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 220 | frame.clear(); |
| 221 | } |
| 222 | const ui::Transform displayTransform{outputState.transform}; |
| 223 | |
| 224 | return displayTransform.transform(frame); |
| 225 | } |
| 226 | |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 227 | uint32_t OutputLayer::calculateOutputRelativeBufferTransform( |
| 228 | uint32_t internalDisplayRotationFlags) const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 229 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 230 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * Transformations are applied in this order: |
| 234 | * 1) buffer orientation/flip/mirror |
| 235 | * 2) state transformation (window manager) |
| 236 | * 3) layer orientation (screen orientation) |
| 237 | * (NOTE: the matrices are multiplied in reverse order) |
| 238 | */ |
| 239 | const ui::Transform& layerTransform = layerState.geomLayerTransform; |
Rashed Abdel-Tawab | 6643cd8 | 2019-10-29 10:01:56 -0700 | [diff] [blame] | 240 | const ui::Transform displayTransform{outputState.transform}; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 241 | const ui::Transform bufferTransform{layerState.geomBufferTransform}; |
| 242 | ui::Transform transform(displayTransform * layerTransform * bufferTransform); |
| 243 | |
| 244 | if (layerState.geomBufferUsesDisplayInverseTransform) { |
| 245 | /* |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 246 | * We must apply the internal display's inverse transform to the buffer |
| 247 | * transform, and not the one for the output this layer is on. |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 248 | */ |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 249 | uint32_t invTransform = internalDisplayRotationFlags; |
| 250 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 251 | // calculate the inverse transform |
| 252 | if (invTransform & HAL_TRANSFORM_ROT_90) { |
| 253 | invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Here we cancel out the orientation component of the WM transform. |
| 258 | * The scaling and translate components are already included in our bounds |
| 259 | * computation so it's enough to just omit it in the composition. |
| 260 | * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why. |
| 261 | */ |
Lloyd Pique | 546a245 | 2019-03-18 20:53:27 +0000 | [diff] [blame] | 262 | transform = ui::Transform(invTransform) * displayTransform * bufferTransform; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // this gives us only the "orientation" component of the transform |
| 266 | return transform.getOrientation(); |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 267 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 268 | |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 269 | void OutputLayer::updateCompositionState( |
| 270 | bool includeGeometry, bool forceClientComposition, |
| 271 | ui::Transform::RotationFlags internalDisplayRotationFlags) { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 272 | const auto* layerFEState = getLayerFE().getCompositionState(); |
| 273 | if (!layerFEState) { |
| 274 | return; |
| 275 | } |
| 276 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 277 | const auto& outputState = getOutput().getState(); |
| 278 | const auto& profile = *getOutput().getDisplayColorProfile(); |
| 279 | auto& state = editState(); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 280 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 281 | if (includeGeometry) { |
Lloyd Pique | fe67102 | 2019-09-24 10:43:03 -0700 | [diff] [blame] | 282 | // Clear the forceClientComposition flag before it is set for any |
| 283 | // reason. Note that since it can be set by some checks below when |
| 284 | // updating the geometry state, we only clear it when updating the |
| 285 | // geometry since those conditions for forcing client composition won't |
| 286 | // go away otherwise. |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 287 | state.forceClientComposition = false; |
Lloyd Pique | fe67102 | 2019-09-24 10:43:03 -0700 | [diff] [blame] | 288 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 289 | state.displayFrame = calculateOutputDisplayFrame(); |
| 290 | state.sourceCrop = calculateOutputSourceCrop(); |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 291 | state.bufferTransform = static_cast<Hwc2::Transform>( |
| 292 | calculateOutputRelativeBufferTransform(internalDisplayRotationFlags)); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 293 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 294 | if ((layerFEState->isSecure && !outputState.isSecure) || |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 295 | (state.bufferTransform & ui::Transform::ROT_INVALID)) { |
| 296 | state.forceClientComposition = true; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 299 | |
| 300 | // Determine the output dependent dataspace for this layer. If it is |
| 301 | // colorspace agnostic, it just uses the dataspace chosen for the output to |
| 302 | // avoid the need for color conversion. |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 303 | state.dataspace = layerFEState->isColorspaceAgnostic && |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 304 | outputState.targetDataspace != ui::Dataspace::UNKNOWN |
| 305 | ? outputState.targetDataspace |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 306 | : layerFEState->dataspace; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 307 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 308 | // These are evaluated every frame as they can potentially change at any |
| 309 | // time. |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 310 | if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) || |
Lloyd Pique | 7a23491 | 2019-10-03 11:54:27 -0700 | [diff] [blame] | 311 | forceClientComposition) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 312 | state.forceClientComposition = true; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 313 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame^] | 316 | void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z, |
| 317 | bool zIsOverridden, bool isPeekingThrough) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 318 | const auto& state = getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 319 | // Skip doing this if there is no HWC interface |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 320 | if (!state.hwc) { |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 324 | auto& hwcLayer = (*state.hwc).hwcLayer; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 325 | if (!hwcLayer) { |
| 326 | ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s", |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 327 | getLayerFE().getDebugName(), getOutput().getName().c_str()); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 328 | return; |
| 329 | } |
| 330 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 331 | const auto* outputIndependentState = getLayerFE().getCompositionState(); |
| 332 | if (!outputIndependentState) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | auto requestedCompositionType = outputIndependentState->compositionType; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 337 | |
Yichi Chen | 413d46a | 2021-04-07 21:42:09 +0800 | [diff] [blame] | 338 | // TODO(b/181172795): We now update geometry for all flattened layers. We should update it |
| 339 | // only when the geometry actually changes |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame^] | 340 | const bool isOverridden = |
| 341 | state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden; |
Yichi Chen | 413d46a | 2021-04-07 21:42:09 +0800 | [diff] [blame] | 342 | const bool prevOverridden = state.hwc->stateOverridden; |
| 343 | if (isOverridden || prevOverridden || skipLayer || includeGeometry) { |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 344 | writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z); |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 345 | writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState, |
| 346 | skipLayer); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 347 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 348 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 349 | writeOutputDependentPerFrameStateToHWC(hwcLayer.get()); |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 350 | writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 351 | |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame^] | 352 | writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough); |
Lloyd Pique | 46b72df | 2019-10-29 13:19:27 -0700 | [diff] [blame] | 353 | |
| 354 | // Always set the layer color after setting the composition type. |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 355 | writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState); |
Yichi Chen | 413d46a | 2021-04-07 21:42:09 +0800 | [diff] [blame] | 356 | |
| 357 | editState().hwc->stateOverridden = isOverridden; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 358 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 359 | |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 360 | void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer, |
| 361 | hal::Composition requestedCompositionType, |
| 362 | uint32_t z) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 363 | const auto& outputDependentState = getState(); |
| 364 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 365 | Rect displayFrame = outputDependentState.displayFrame; |
| 366 | FloatRect sourceCrop = outputDependentState.sourceCrop; |
Huihong Luo | a582511 | 2021-03-24 12:28:29 -0700 | [diff] [blame] | 367 | |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 368 | if (outputDependentState.overrideInfo.buffer != nullptr) { |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 369 | displayFrame = outputDependentState.overrideInfo.displayFrame; |
| 370 | sourceCrop = displayFrame.toFloatRect(); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 373 | ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top, |
| 374 | displayFrame.right, displayFrame.bottom); |
| 375 | |
| 376 | if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) { |
| 377 | ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", |
| 378 | getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right, |
| 379 | displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error)); |
| 380 | } |
| 381 | |
| 382 | if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 383 | ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: " |
| 384 | "%s (%d)", |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 385 | getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right, |
| 386 | sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 389 | if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) { |
| 390 | ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z, |
| 391 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 394 | // Solid-color layers and overridden buffers should always use an identity transform. |
| 395 | const auto bufferTransform = (requestedCompositionType != hal::Composition::SOLID_COLOR && |
| 396 | getState().overrideInfo.buffer == nullptr) |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 397 | ? outputDependentState.bufferTransform |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 398 | : static_cast<hal::Transform>(0); |
| 399 | if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform)); |
| 400 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 401 | ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 402 | toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(), |
| 403 | static_cast<int32_t>(error)); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void OutputLayer::writeOutputIndependentGeometryStateToHWC( |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 408 | HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState, |
| 409 | bool skipLayer) { |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 410 | // If there is a peekThroughLayer, then this layer has a hole in it. We need to use |
| 411 | // PREMULTIPLIED so it will peek through. |
| 412 | const auto& overrideInfo = getState().overrideInfo; |
| 413 | const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer |
Alec Mouri | ee69a59 | 2021-03-23 15:00:45 -0700 | [diff] [blame] | 414 | ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED |
| 415 | : outputIndependentState.blendMode; |
| 416 | if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 417 | ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(), |
Alec Mouri | ee69a59 | 2021-03-23 15:00:45 -0700 | [diff] [blame] | 418 | toString(blendMode).c_str(), to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Alec Mouri | ee69a59 | 2021-03-23 15:00:45 -0700 | [diff] [blame] | 421 | const float alpha = skipLayer |
| 422 | ? 0.0f |
| 423 | : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha); |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 424 | ALOGV("Writing alpha %f", alpha); |
| 425 | |
| 426 | if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) { |
| 427 | ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha, |
| 428 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 431 | for (const auto& [name, entry] : outputIndependentState.metadata) { |
| 432 | if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 433 | error != hal::Error::NONE) { |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 434 | ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(), |
| 435 | name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error)); |
| 436 | } |
| 437 | } |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) { |
| 441 | const auto& outputDependentState = getState(); |
| 442 | |
Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 443 | // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 444 | // state and should not change every frame. |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 445 | Region visibleRegion = outputDependentState.overrideInfo.buffer |
| 446 | ? Region(outputDependentState.overrideInfo.visibleRegion) |
| 447 | : outputDependentState.outputSpaceVisibleRegion; |
| 448 | if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 449 | ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 450 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 451 | outputDependentState.outputSpaceVisibleRegion.dump(LOG_TAG); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Alec Mouri | b7edfc2 | 2021-03-17 16:20:26 -0700 | [diff] [blame] | 454 | const auto dataspace = outputDependentState.overrideInfo.buffer |
| 455 | ? outputDependentState.overrideInfo.dataspace |
| 456 | : outputDependentState.dataspace; |
| 457 | |
| 458 | if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) { |
| 459 | ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace, |
| 460 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | void OutputLayer::writeOutputIndependentPerFrameStateToHWC( |
| 465 | HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState) { |
| 466 | switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 467 | case hal::Error::NONE: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 468 | break; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 469 | case hal::Error::UNSUPPORTED: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 470 | editState().forceClientComposition = true; |
| 471 | break; |
| 472 | default: |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 473 | ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 474 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 475 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 476 | |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 477 | const Region& surfaceDamage = getState().overrideInfo.buffer |
| 478 | ? getState().overrideInfo.damageRegion |
| 479 | : outputIndependentState.surfaceDamage; |
| 480 | |
| 481 | if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 482 | ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 483 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 484 | outputIndependentState.surfaceDamage.dump(LOG_TAG); |
| 485 | } |
| 486 | |
| 487 | // Content-specific per-frame state |
| 488 | switch (outputIndependentState.compositionType) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 489 | case hal::Composition::SOLID_COLOR: |
Lloyd Pique | 46b72df | 2019-10-29 13:19:27 -0700 | [diff] [blame] | 490 | // For compatibility, should be written AFTER the composition type. |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 491 | break; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 492 | case hal::Composition::SIDEBAND: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 493 | writeSidebandStateToHWC(hwcLayer, outputIndependentState); |
| 494 | break; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 495 | case hal::Composition::CURSOR: |
| 496 | case hal::Composition::DEVICE: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 497 | writeBufferStateToHWC(hwcLayer, outputIndependentState); |
| 498 | break; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 499 | case hal::Composition::INVALID: |
| 500 | case hal::Composition::CLIENT: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 501 | // Ignored |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer, |
| 507 | const LayerFECompositionState& outputIndependentState) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 508 | if (outputIndependentState.compositionType != hal::Composition::SOLID_COLOR) { |
Lloyd Pique | 46b72df | 2019-10-29 13:19:27 -0700 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 512 | hal::Color color = {static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.r)), |
| 513 | static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.g)), |
| 514 | static_cast<uint8_t>(std::round(255.0f * outputIndependentState.color.b)), |
| 515 | 255}; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 516 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 517 | if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 518 | ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 519 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer, |
| 524 | const LayerFECompositionState& outputIndependentState) { |
| 525 | if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle()); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 526 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 527 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 528 | outputIndependentState.sidebandStream->handle(), to_string(error).c_str(), |
| 529 | static_cast<int32_t>(error)); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer, |
| 534 | const LayerFECompositionState& outputIndependentState) { |
| 535 | auto supportedPerFrameMetadata = |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 536 | getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata(); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 537 | if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, |
| 538 | outputIndependentState.hdrMetadata); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 539 | error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 540 | ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 541 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 542 | } |
| 543 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 544 | sp<GraphicBuffer> buffer = outputIndependentState.buffer; |
| 545 | sp<Fence> acquireFence = outputIndependentState.acquireFence; |
| 546 | if (getState().overrideInfo.buffer != nullptr) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 547 | buffer = getState().overrideInfo.buffer->getBuffer(); |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 548 | acquireFence = getState().overrideInfo.acquireFence; |
| 549 | } |
| 550 | |
| 551 | ALOGV("Writing buffer %p", buffer.get()); |
| 552 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 553 | uint32_t hwcSlot = 0; |
| 554 | sp<GraphicBuffer> hwcBuffer; |
| 555 | // We need access to the output-dependent state for the buffer cache there, |
| 556 | // though otherwise the buffer is not output-dependent. |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 557 | editState().hwc->hwcBufferCache.getHwcBuffer(outputIndependentState.bufferSlot, buffer, |
| 558 | &hwcSlot, &hwcBuffer); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 559 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 560 | if (auto error = hwcLayer->setBuffer(hwcSlot, hwcBuffer, acquireFence); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 561 | error != hal::Error::NONE) { |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 562 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(), buffer->handle, |
| 563 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 567 | void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer, |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame^] | 568 | hal::Composition requestedCompositionType, |
| 569 | bool isPeekingThrough) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 570 | auto& outputDependentState = editState(); |
| 571 | |
| 572 | // If we are forcing client composition, we need to tell the HWC |
Leon Scroggins III | d305ef2 | 2021-04-06 09:53:26 -0400 | [diff] [blame] | 573 | if (outputDependentState.forceClientComposition || |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame^] | 574 | (!isPeekingThrough && getLayerFE().hasRoundedCorners())) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 575 | requestedCompositionType = hal::Composition::CLIENT; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | // Set the requested composition type with the HWC whenever it changes |
| 579 | if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType) { |
| 580 | outputDependentState.hwc->hwcCompositionType = requestedCompositionType; |
| 581 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 582 | if (auto error = hwcLayer->setCompositionType(requestedCompositionType); |
| 583 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 584 | ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 585 | toString(requestedCompositionType).c_str(), to_string(error).c_str(), |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 586 | static_cast<int32_t>(error)); |
| 587 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 591 | void OutputLayer::writeCursorPositionToHWC() const { |
| 592 | // Skip doing this if there is no HWC interface |
| 593 | auto hwcLayer = getHwcLayer(); |
| 594 | if (!hwcLayer) { |
| 595 | return; |
| 596 | } |
| 597 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 598 | const auto* layerFEState = getLayerFE().getCompositionState(); |
| 599 | if (!layerFEState) { |
| 600 | return; |
| 601 | } |
| 602 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 603 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 604 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 605 | Rect frame = layerFEState->cursorFrame; |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 606 | frame.intersect(outputState.layerStackSpace.content, &frame); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 607 | Rect position = outputState.transform.transform(frame); |
| 608 | |
| 609 | if (auto error = hwcLayer->setCursorPosition(position.left, position.top); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 610 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 611 | ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)", |
| 612 | getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(), |
| 613 | static_cast<int32_t>(error)); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 617 | HWC2::Layer* OutputLayer::getHwcLayer() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 618 | const auto& state = getState(); |
| 619 | return state.hwc ? state.hwc->hwcLayer.get() : nullptr; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | bool OutputLayer::requiresClientComposition() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 623 | const auto& state = getState(); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 624 | return !state.hwc || state.hwc->hwcCompositionType == hal::Composition::CLIENT; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 627 | bool OutputLayer::isHardwareCursor() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 628 | const auto& state = getState(); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 629 | return state.hwc && state.hwc->hwcCompositionType == hal::Composition::CURSOR; |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 632 | void OutputLayer::detectDisallowedCompositionTypeChange(hal::Composition from, |
| 633 | hal::Composition to) const { |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 634 | bool result = false; |
| 635 | switch (from) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 636 | case hal::Composition::INVALID: |
| 637 | case hal::Composition::CLIENT: |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 638 | result = false; |
| 639 | break; |
| 640 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 641 | case hal::Composition::DEVICE: |
| 642 | case hal::Composition::SOLID_COLOR: |
| 643 | result = (to == hal::Composition::CLIENT); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 644 | break; |
| 645 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 646 | case hal::Composition::CURSOR: |
| 647 | case hal::Composition::SIDEBAND: |
| 648 | result = (to == hal::Composition::CLIENT || to == hal::Composition::DEVICE); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | |
| 652 | if (!result) { |
| 653 | ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)", |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 654 | getLayerFE().getDebugName(), toString(from).c_str(), static_cast<int>(from), |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 655 | toString(to).c_str(), static_cast<int>(to)); |
| 656 | } |
| 657 | } |
| 658 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 659 | void OutputLayer::applyDeviceCompositionTypeChange(hal::Composition compositionType) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 660 | auto& state = editState(); |
| 661 | LOG_FATAL_IF(!state.hwc); |
| 662 | auto& hwcState = *state.hwc; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 663 | |
| 664 | detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType); |
| 665 | |
| 666 | hwcState.hwcCompositionType = compositionType; |
| 667 | } |
| 668 | |
| 669 | void OutputLayer::prepareForDeviceLayerRequests() { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 670 | auto& state = editState(); |
| 671 | state.clearClientTarget = false; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 674 | void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 675 | auto& state = editState(); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 676 | switch (request) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 677 | case hal::LayerRequest::CLEAR_CLIENT_TARGET: |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 678 | state.clearClientTarget = true; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 679 | break; |
| 680 | |
| 681 | default: |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 682 | ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 683 | toString(request).c_str(), static_cast<int>(request)); |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 688 | bool OutputLayer::needsFiltering() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 689 | const auto& state = getState(); |
| 690 | const auto& displayFrame = state.displayFrame; |
| 691 | const auto& sourceCrop = state.sourceCrop; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 692 | return sourceCrop.getHeight() != displayFrame.getHeight() || |
| 693 | sourceCrop.getWidth() != displayFrame.getWidth(); |
| 694 | } |
| 695 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 696 | std::vector<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionList() const { |
| 697 | if (getState().overrideInfo.buffer == nullptr) { |
| 698 | return {}; |
| 699 | } |
| 700 | |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 701 | // Compute the geometry boundaries in layer stack space: we need to transform from the |
| 702 | // framebuffer space of the override buffer to layer space. |
| 703 | const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace; |
| 704 | const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace); |
| 705 | const Rect boundaries = transform.transform(getState().overrideInfo.displayFrame); |
| 706 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 707 | LayerFE::LayerSettings settings; |
| 708 | settings.geometry = renderengine::Geometry{ |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 709 | .boundaries = boundaries.toFloatRect(), |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 710 | }; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 711 | settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId(); |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 712 | settings.source = renderengine::PixelSource{ |
| 713 | .buffer = renderengine::Buffer{ |
| 714 | .buffer = getState().overrideInfo.buffer, |
| 715 | .fence = getState().overrideInfo.acquireFence, |
| 716 | // If the transform from layer space to display space contains a rotation, we |
| 717 | // need to undo the rotation in the texture transform |
| 718 | .textureTransform = |
| 719 | ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(), |
| 720 | }}; |
Alec Mouri | 9c8fce0 | 2021-03-12 18:30:42 -0800 | [diff] [blame] | 721 | settings.sourceDataspace = getState().overrideInfo.dataspace; |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 722 | settings.alpha = 1.0f; |
| 723 | |
| 724 | return {static_cast<LayerFE::LayerSettings>(settings)}; |
| 725 | } |
| 726 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 727 | void OutputLayer::dump(std::string& out) const { |
| 728 | using android::base::StringAppendF; |
| 729 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 730 | StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName()); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 731 | dumpState(out); |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 734 | } // namespace impl |
| 735 | } // namespace android::compositionengine |