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