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 | */ |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 16 | #include <DisplayHardware/Hal.h> |
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> |
Alec Mouri | e7cc1c2 | 2021-04-27 15:23:26 -0700 | [diff] [blame] | 21 | #include <compositionengine/impl/HwcBufferCache.h> |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 22 | #include <compositionengine/impl/OutputCompositionState.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 23 | #include <compositionengine/impl/OutputLayer.h> |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 24 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 25 | #include <cstdint> |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 26 | #include "system/graphics-base-v1.0.h" |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 27 | #include "ui/FloatRect.h" |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 28 | |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 29 | #include <ui/HdrRenderTypeUtils.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 30 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 31 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 32 | #pragma clang diagnostic push |
| 33 | #pragma clang diagnostic ignored "-Wconversion" |
| 34 | |
Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 35 | #include "DisplayHardware/HWComposer.h" |
| 36 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 37 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 38 | #pragma clang diagnostic pop // ignored "-Wconversion" |
| 39 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 40 | using aidl::android::hardware::graphics::composer3::Composition; |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 41 | using aidl::android::hardware::graphics::composer3::Luts; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 42 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 43 | namespace android::compositionengine { |
| 44 | |
| 45 | OutputLayer::~OutputLayer() = default; |
| 46 | |
| 47 | namespace impl { |
| 48 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | FloatRect reduce(const FloatRect& win, const Region& exclude) { |
| 52 | if (CC_LIKELY(exclude.isEmpty())) { |
| 53 | return win; |
| 54 | } |
| 55 | // Convert through Rect (by rounding) for lack of FloatRegion |
| 56 | return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect(); |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 61 | std::unique_ptr<OutputLayer> createOutputLayer(const compositionengine::Output& output, |
| 62 | const sp<compositionengine::LayerFE>& layerFE) { |
| 63 | return createOutputLayerTemplated<OutputLayer>(output, layerFE); |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 66 | OutputLayer::~OutputLayer() = default; |
| 67 | |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 68 | void OutputLayer::setHwcLayer(std::shared_ptr<HWC2::Layer> hwcLayer) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 69 | auto& state = editState(); |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 70 | if (hwcLayer) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 71 | state.hwc.emplace(std::move(hwcLayer)); |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 72 | } else { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 73 | state.hwc.reset(); |
Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 74 | } |
Lloyd Pique | 07e3321 | 2018-12-18 16:33:37 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 77 | Rect OutputLayer::calculateInitialCrop() const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 78 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 79 | |
| 80 | // apply the projection's clipping to the window crop in |
| 81 | // layerstack space, and convert-back to layer space. |
| 82 | // if there are no window scaling involved, this operation will map to full |
| 83 | // pixels in the buffer. |
| 84 | |
| 85 | FloatRect activeCropFloat = |
Lloyd Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 86 | reduce(layerState.geomLayerBounds, layerState.transparentRegionHint); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 87 | |
Angel Aguayo | b084e0c | 2021-08-04 23:27:28 +0000 | [diff] [blame] | 88 | const Rect& viewport = getOutput().getState().layerStackSpace.getContent(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 89 | const ui::Transform& layerTransform = layerState.geomLayerTransform; |
| 90 | const ui::Transform& inverseLayerTransform = layerState.geomInverseLayerTransform; |
| 91 | // Transform to screen space. |
| 92 | activeCropFloat = layerTransform.transform(activeCropFloat); |
| 93 | activeCropFloat = activeCropFloat.intersect(viewport.toFloatRect()); |
| 94 | // Back to layer space to work with the content crop. |
| 95 | activeCropFloat = inverseLayerTransform.transform(activeCropFloat); |
| 96 | |
| 97 | // This needs to be here as transform.transform(Rect) computes the |
| 98 | // transformed rect and then takes the bounding box of the result before |
| 99 | // returning. This means |
| 100 | // transform.inverse().transform(transform.transform(Rect)) != Rect |
| 101 | // in which case we need to make sure the final rect is clipped to the |
| 102 | // display bounds. |
| 103 | Rect activeCrop{activeCropFloat}; |
| 104 | if (!activeCrop.intersect(layerState.geomBufferSize, &activeCrop)) { |
| 105 | activeCrop.clear(); |
| 106 | } |
| 107 | return activeCrop; |
| 108 | } |
| 109 | |
ramindani | 2c043be | 2022-04-19 20:11:10 +0000 | [diff] [blame] | 110 | FloatRect OutputLayer::calculateOutputSourceCrop(uint32_t internalDisplayRotationFlags) const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 111 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 112 | |
| 113 | if (!layerState.geomUsesSourceCrop) { |
| 114 | return {}; |
| 115 | } |
| 116 | |
| 117 | // the content crop is the area of the content that gets scaled to the |
| 118 | // layer's size. This is in buffer space. |
| 119 | FloatRect crop = layerState.geomContentCrop.toFloatRect(); |
| 120 | |
| 121 | // In addition there is a WM-specified crop we pull from our drawing state. |
| 122 | Rect activeCrop = calculateInitialCrop(); |
| 123 | const Rect& bufferSize = layerState.geomBufferSize; |
| 124 | |
| 125 | int winWidth = bufferSize.getWidth(); |
| 126 | int winHeight = bufferSize.getHeight(); |
| 127 | |
| 128 | // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1]) |
| 129 | // if display frame hasn't been set and the parent is an unbounded layer. |
| 130 | if (winWidth < 0 && winHeight < 0) { |
| 131 | return crop; |
| 132 | } |
| 133 | |
| 134 | // Transform the window crop to match the buffer coordinate system, |
| 135 | // which means using the inverse of the current transform set on the |
| 136 | // SurfaceFlingerConsumer. |
| 137 | uint32_t invTransform = layerState.geomBufferTransform; |
| 138 | if (layerState.geomBufferUsesDisplayInverseTransform) { |
| 139 | /* |
| 140 | * the code below applies the primary display's inverse transform to the |
| 141 | * buffer |
| 142 | */ |
ramindani | 2c043be | 2022-04-19 20:11:10 +0000 | [diff] [blame] | 143 | uint32_t invTransformOrient = internalDisplayRotationFlags; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 144 | // calculate the inverse transform |
| 145 | if (invTransformOrient & HAL_TRANSFORM_ROT_90) { |
| 146 | invTransformOrient ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H; |
| 147 | } |
| 148 | // and apply to the current transform |
| 149 | invTransform = |
| 150 | (ui::Transform(invTransformOrient) * ui::Transform(invTransform)).getOrientation(); |
| 151 | } |
| 152 | |
| 153 | if (invTransform & HAL_TRANSFORM_ROT_90) { |
| 154 | // If the activeCrop has been rotate the ends are rotated but not |
| 155 | // the space itself so when transforming ends back we can't rely on |
| 156 | // a modification of the axes of rotation. To account for this we |
| 157 | // need to reorient the inverse rotation in terms of the current |
| 158 | // axes of rotation. |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 159 | bool isHFlipped = (invTransform & HAL_TRANSFORM_FLIP_H) != 0; |
| 160 | bool isVFlipped = (invTransform & HAL_TRANSFORM_FLIP_V) != 0; |
| 161 | if (isHFlipped == isVFlipped) { |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 162 | invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H; |
| 163 | } |
| 164 | std::swap(winWidth, winHeight); |
| 165 | } |
| 166 | const Rect winCrop = |
| 167 | activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight()); |
| 168 | |
| 169 | // below, crop is intersected with winCrop expressed in crop's coordinate space |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 170 | const float xScale = crop.getWidth() / float(winWidth); |
| 171 | const float yScale = crop.getHeight() / float(winHeight); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 172 | |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 173 | const float insetLeft = winCrop.left * xScale; |
| 174 | const float insetTop = winCrop.top * yScale; |
| 175 | const float insetRight = (winWidth - winCrop.right) * xScale; |
| 176 | const float insetBottom = (winHeight - winCrop.bottom) * yScale; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 177 | |
Marin Shalamanov | cfeebd4 | 2020-05-15 15:23:49 +0200 | [diff] [blame] | 178 | crop.left += insetLeft; |
| 179 | crop.top += insetTop; |
| 180 | crop.right -= insetRight; |
| 181 | crop.bottom -= insetBottom; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 182 | |
| 183 | return crop; |
| 184 | } |
| 185 | |
| 186 | Rect OutputLayer::calculateOutputDisplayFrame() const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 187 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 188 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 189 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 190 | // Convert from layer space to layerStackSpace |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 191 | // apply the layer's transform, followed by the display's global transform |
| 192 | // here we're guaranteed that the layer's transform preserves rects |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 193 | const ui::Transform& layerTransform = layerState.geomLayerTransform; |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 194 | Region activeTransparentRegion = layerTransform.transform(layerState.transparentRegionHint); |
| 195 | if (!layerState.geomCrop.isEmpty() && layerState.geomBufferSize.isValid()) { |
| 196 | FloatRect activeCrop = layerTransform.transform(layerState.geomCrop); |
| 197 | activeCrop = activeCrop.intersect(outputState.layerStackSpace.getContent().toFloatRect()); |
| 198 | const FloatRect& bufferSize = |
| 199 | layerTransform.transform(layerState.geomBufferSize.toFloatRect()); |
| 200 | activeCrop = activeCrop.intersect(bufferSize); |
| 201 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 202 | // mark regions outside the crop as transparent |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 203 | Rect topRegion = Rect(layerTransform.transform( |
| 204 | FloatRect(0, 0, layerState.geomBufferSize.getWidth(), layerState.geomCrop.top))); |
| 205 | Rect bottomRegion = Rect(layerTransform.transform( |
| 206 | FloatRect(0, layerState.geomCrop.bottom, layerState.geomBufferSize.getWidth(), |
| 207 | layerState.geomBufferSize.getHeight()))); |
| 208 | Rect leftRegion = Rect(layerTransform.transform(FloatRect(0, layerState.geomCrop.top, |
| 209 | layerState.geomCrop.left, |
| 210 | layerState.geomCrop.bottom))); |
| 211 | Rect rightRegion = Rect(layerTransform.transform( |
| 212 | FloatRect(layerState.geomCrop.right, layerState.geomCrop.top, |
| 213 | layerState.geomBufferSize.getWidth(), layerState.geomCrop.bottom))); |
| 214 | |
| 215 | activeTransparentRegion.orSelf(topRegion); |
| 216 | activeTransparentRegion.orSelf(bottomRegion); |
| 217 | activeTransparentRegion.orSelf(leftRegion); |
| 218 | activeTransparentRegion.orSelf(rightRegion); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // reduce uses a FloatRect to provide more accuracy during the |
| 222 | // transformation. We then round upon constructing 'frame'. |
Leon Scroggins III | d394d3c | 2021-06-24 11:30:32 -0400 | [diff] [blame] | 223 | FloatRect geomLayerBounds = layerState.geomLayerBounds; |
| 224 | |
| 225 | // Some HWCs may clip client composited input to its displayFrame. Make sure |
| 226 | // that this does not cut off the shadow. |
Vishnu Nair | d9e4f46 | 2023-10-06 04:05:45 +0000 | [diff] [blame] | 227 | if (layerState.forceClientComposition && layerState.shadowSettings.length > 0.0f) { |
Alec Mouri | da12851 | 2024-09-28 23:46:58 +0000 | [diff] [blame] | 228 | // RenderEngine currently blurs shadows to smooth out edges, so outset by |
| 229 | // 2x the length instead of 1x to compensate |
| 230 | const auto outset = layerState.shadowSettings.length * 2; |
Leon Scroggins III | d394d3c | 2021-06-24 11:30:32 -0400 | [diff] [blame] | 231 | geomLayerBounds.left -= outset; |
| 232 | geomLayerBounds.top -= outset; |
| 233 | geomLayerBounds.right += outset; |
| 234 | geomLayerBounds.bottom += outset; |
| 235 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 236 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 237 | geomLayerBounds = layerTransform.transform(geomLayerBounds); |
| 238 | FloatRect frame = reduce(geomLayerBounds, activeTransparentRegion); |
| 239 | frame = frame.intersect(outputState.layerStackSpace.getContent().toFloatRect()); |
| 240 | |
| 241 | // convert from layerStackSpace to displaySpace |
| 242 | const ui::Transform displayTransform{outputState.transform}; |
| 243 | return Rect(displayTransform.transform(frame)); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 246 | uint32_t OutputLayer::calculateOutputRelativeBufferTransform( |
| 247 | uint32_t internalDisplayRotationFlags) const { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 248 | const auto& layerState = *getLayerFE().getCompositionState(); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 249 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Transformations are applied in this order: |
| 253 | * 1) buffer orientation/flip/mirror |
| 254 | * 2) state transformation (window manager) |
| 255 | * 3) layer orientation (screen orientation) |
| 256 | * (NOTE: the matrices are multiplied in reverse order) |
| 257 | */ |
| 258 | const ui::Transform& layerTransform = layerState.geomLayerTransform; |
Rashed Abdel-Tawab | 6643cd8 | 2019-10-29 10:01:56 -0700 | [diff] [blame] | 259 | const ui::Transform displayTransform{outputState.transform}; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 260 | const ui::Transform bufferTransform{layerState.geomBufferTransform}; |
| 261 | ui::Transform transform(displayTransform * layerTransform * bufferTransform); |
| 262 | |
| 263 | if (layerState.geomBufferUsesDisplayInverseTransform) { |
| 264 | /* |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 265 | * We must apply the internal display's inverse transform to the buffer |
| 266 | * transform, and not the one for the output this layer is on. |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 267 | */ |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 268 | uint32_t invTransform = internalDisplayRotationFlags; |
| 269 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 270 | // calculate the inverse transform |
| 271 | if (invTransform & HAL_TRANSFORM_ROT_90) { |
| 272 | invTransform ^= HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_FLIP_H; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Here we cancel out the orientation component of the WM transform. |
| 277 | * The scaling and translate components are already included in our bounds |
| 278 | * computation so it's enough to just omit it in the composition. |
| 279 | * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why. |
| 280 | */ |
Lloyd Pique | 546a245 | 2019-03-18 20:53:27 +0000 | [diff] [blame] | 281 | transform = ui::Transform(invTransform) * displayTransform * bufferTransform; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // this gives us only the "orientation" component of the transform |
| 285 | return transform.getOrientation(); |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 286 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 287 | |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 288 | void OutputLayer::updateLuts( |
Sally Qi | ef00658 | 2024-10-11 13:23:09 -0700 | [diff] [blame^] | 289 | const LayerFECompositionState& layerFEState, |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 290 | const std::optional<std::vector<std::optional<LutProperties>>>& properties) { |
Sally Qi | ef00658 | 2024-10-11 13:23:09 -0700 | [diff] [blame^] | 291 | auto& luts = layerFEState.luts; |
| 292 | if (!luts) { |
| 293 | return; |
| 294 | } |
| 295 | |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 296 | auto& state = editState(); |
| 297 | |
| 298 | if (!properties) { |
| 299 | // GPU composition if no Hwc Luts |
| 300 | state.forceClientComposition = true; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | std::vector<LutProperties> hwcLutProperties; |
| 305 | for (auto& p : *properties) { |
| 306 | if (p) { |
| 307 | hwcLutProperties.emplace_back(*p); |
| 308 | } |
| 309 | } |
| 310 | |
Sally Qi | ef00658 | 2024-10-11 13:23:09 -0700 | [diff] [blame^] | 311 | for (const auto& inputLut : luts->lutProperties) { |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 312 | bool foundInHwcLuts = false; |
| 313 | for (const auto& hwcLut : hwcLutProperties) { |
| 314 | if (static_cast<int32_t>(hwcLut.dimension) == |
| 315 | static_cast<int32_t>(inputLut.dimension) && |
| 316 | hwcLut.size == inputLut.size && |
| 317 | std::find(hwcLut.samplingKeys.begin(), hwcLut.samplingKeys.end(), |
| 318 | static_cast<LutProperties::SamplingKey>(inputLut.samplingKey)) != |
| 319 | hwcLut.samplingKeys.end()) { |
| 320 | foundInHwcLuts = true; |
| 321 | break; |
| 322 | } |
| 323 | } |
Sally Qi | ef00658 | 2024-10-11 13:23:09 -0700 | [diff] [blame^] | 324 | // if any lut properties of luts can not be found in hwcLutProperties, |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 325 | // GPU composition instead |
| 326 | if (!foundInHwcLuts) { |
| 327 | state.forceClientComposition = true; |
| 328 | return; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 333 | void OutputLayer::updateCompositionState( |
| 334 | bool includeGeometry, bool forceClientComposition, |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 335 | ui::Transform::RotationFlags internalDisplayRotationFlags, |
| 336 | const std::optional<std::vector<std::optional<LutProperties>>> properties) { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 337 | const auto* layerFEState = getLayerFE().getCompositionState(); |
| 338 | if (!layerFEState) { |
| 339 | return; |
| 340 | } |
| 341 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 342 | const auto& outputState = getOutput().getState(); |
| 343 | const auto& profile = *getOutput().getDisplayColorProfile(); |
| 344 | auto& state = editState(); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 345 | |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 346 | if (includeGeometry) { |
Lloyd Pique | fe67102 | 2019-09-24 10:43:03 -0700 | [diff] [blame] | 347 | // Clear the forceClientComposition flag before it is set for any |
| 348 | // reason. Note that since it can be set by some checks below when |
| 349 | // updating the geometry state, we only clear it when updating the |
| 350 | // geometry since those conditions for forcing client composition won't |
| 351 | // go away otherwise. |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 352 | state.forceClientComposition = false; |
Lloyd Pique | fe67102 | 2019-09-24 10:43:03 -0700 | [diff] [blame] | 353 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 354 | state.displayFrame = calculateOutputDisplayFrame(); |
ramindani | 2c043be | 2022-04-19 20:11:10 +0000 | [diff] [blame] | 355 | state.sourceCrop = calculateOutputSourceCrop(internalDisplayRotationFlags); |
Snild Dolkow | 9e217d6 | 2020-04-22 15:53:42 +0200 | [diff] [blame] | 356 | state.bufferTransform = static_cast<Hwc2::Transform>( |
| 357 | calculateOutputRelativeBufferTransform(internalDisplayRotationFlags)); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 358 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 359 | if ((layerFEState->isSecure && !outputState.isSecure) || |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 360 | (state.bufferTransform & ui::Transform::ROT_INVALID)) { |
| 361 | state.forceClientComposition = true; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 362 | } |
| 363 | } |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 364 | |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 365 | auto pixelFormat = layerFEState->buffer ? std::make_optional(static_cast<ui::PixelFormat>( |
| 366 | layerFEState->buffer->getPixelFormat())) |
| 367 | : std::nullopt; |
| 368 | |
| 369 | auto hdrRenderType = |
| 370 | getHdrRenderType(outputState.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio); |
| 371 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 372 | // Determine the output dependent dataspace for this layer. If it is |
| 373 | // colorspace agnostic, it just uses the dataspace chosen for the output to |
| 374 | // avoid the need for color conversion. |
Alec Mouri | 88790f3 | 2023-07-21 01:25:14 +0000 | [diff] [blame] | 375 | // For now, also respect the colorspace agnostic flag if we're drawing to HDR, to avoid drastic |
| 376 | // luminance shift. TODO(b/292162273): we should check if that's true though. |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 377 | state.dataspace = layerFEState->isColorspaceAgnostic && hdrRenderType == HdrRenderType::SDR |
Alec Mouri | 88790f3 | 2023-07-21 01:25:14 +0000 | [diff] [blame] | 378 | ? outputState.dataspace |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 379 | : layerFEState->dataspace; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 380 | |
Alec Mouri | dda07d9 | 2022-04-25 22:39:25 +0000 | [diff] [blame] | 381 | // Override the dataspace transfer from 170M to sRGB if the device configuration requests this. |
| 382 | // We do this here instead of in buffer info so that dumpsys can still report layers that are |
| 383 | // using the 170M transfer. Also we only do this if the colorspace is not agnostic for the |
| 384 | // layer, in case the color profile uses a 170M transfer function. |
| 385 | if (outputState.treat170mAsSrgb && !layerFEState->isColorspaceAgnostic && |
| 386 | (state.dataspace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_SMPTE_170M) { |
| 387 | state.dataspace = static_cast<ui::Dataspace>( |
| 388 | (state.dataspace & HAL_DATASPACE_STANDARD_MASK) | |
| 389 | (state.dataspace & HAL_DATASPACE_RANGE_MASK) | HAL_DATASPACE_TRANSFER_SRGB); |
| 390 | } |
| 391 | |
Sally Qi | 8f9ed65 | 2023-08-17 15:30:29 -0700 | [diff] [blame] | 392 | // re-get HdrRenderType after the dataspace gets changed. |
| 393 | hdrRenderType = |
| 394 | getHdrRenderType(state.dataspace, pixelFormat, layerFEState->desiredHdrSdrRatio); |
| 395 | |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 396 | // For hdr content, treat the white point as the display brightness - HDR content should not be |
| 397 | // boosted or dimmed. |
Sally Qi | 81d95e6 | 2022-03-21 19:41:33 -0700 | [diff] [blame] | 398 | // If the layer explicitly requests to disable dimming, then don't dim either. |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 399 | if (hdrRenderType == HdrRenderType::GENERIC_HDR || |
Alec Mouri | e8dd356 | 2022-02-11 14:18:57 -0800 | [diff] [blame] | 400 | getOutput().getState().displayBrightnessNits == getOutput().getState().sdrWhitePointNits || |
Sally Qi | 81d95e6 | 2022-03-21 19:41:33 -0700 | [diff] [blame] | 401 | getOutput().getState().displayBrightnessNits == 0.f || !layerFEState->dimmingEnabled) { |
Alec Mouri | 6da0e27 | 2022-02-07 12:45:57 -0800 | [diff] [blame] | 402 | state.dimmingRatio = 1.f; |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 403 | state.whitePointNits = getOutput().getState().displayBrightnessNits; |
| 404 | } else { |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 405 | float layerBrightnessNits = getOutput().getState().sdrWhitePointNits; |
| 406 | // RANGE_EXTENDED can "self-promote" to HDR, but is still rendered for a particular |
| 407 | // range that we may need to re-adjust to the current display conditions |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 408 | if (hdrRenderType == HdrRenderType::DISPLAY_HDR) { |
Sally Qi | 963049b | 2023-03-23 14:06:21 -0700 | [diff] [blame] | 409 | layerBrightnessNits *= layerFEState->currentHdrSdrRatio; |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 410 | } |
| 411 | state.dimmingRatio = |
| 412 | std::clamp(layerBrightnessNits / getOutput().getState().displayBrightnessNits, 0.f, |
| 413 | 1.f); |
| 414 | state.whitePointNits = layerBrightnessNits; |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Sally Qi | ef00658 | 2024-10-11 13:23:09 -0700 | [diff] [blame^] | 417 | updateLuts(*layerFEState, properties); |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 418 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 419 | // These are evaluated every frame as they can potentially change at any |
| 420 | // time. |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 421 | if (layerFEState->forceClientComposition || !profile.isDataspaceSupported(state.dataspace) || |
Lloyd Pique | 7a23491 | 2019-10-03 11:54:27 -0700 | [diff] [blame] | 422 | forceClientComposition) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 423 | state.forceClientComposition = true; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 424 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame] | 427 | void OutputLayer::writeStateToHWC(bool includeGeometry, bool skipLayer, uint32_t z, |
| 428 | bool zIsOverridden, bool isPeekingThrough) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 429 | const auto& state = getState(); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 430 | // Skip doing this if there is no HWC interface |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 431 | if (!state.hwc) { |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 435 | auto& hwcLayer = (*state.hwc).hwcLayer; |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 436 | if (!hwcLayer) { |
| 437 | 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] | 438 | getLayerFE().getDebugName(), getOutput().getName().c_str()); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 442 | const auto* outputIndependentState = getLayerFE().getCompositionState(); |
| 443 | if (!outputIndependentState) { |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | auto requestedCompositionType = outputIndependentState->compositionType; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 448 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 449 | if (requestedCompositionType == Composition::SOLID_COLOR && state.overrideInfo.buffer) { |
| 450 | requestedCompositionType = Composition::DEVICE; |
Alec Mouri | 028676a | 2021-12-02 15:01:48 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Yichi Chen | 413d46a | 2021-04-07 21:42:09 +0800 | [diff] [blame] | 453 | // TODO(b/181172795): We now update geometry for all flattened layers. We should update it |
| 454 | // only when the geometry actually changes |
Leon Scroggins III | 9aa25c2 | 2021-04-15 15:30:19 -0400 | [diff] [blame] | 455 | const bool isOverridden = |
| 456 | state.overrideInfo.buffer != nullptr || isPeekingThrough || zIsOverridden; |
Yichi Chen | 413d46a | 2021-04-07 21:42:09 +0800 | [diff] [blame] | 457 | const bool prevOverridden = state.hwc->stateOverridden; |
| 458 | if (isOverridden || prevOverridden || skipLayer || includeGeometry) { |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 459 | writeOutputDependentGeometryStateToHWC(hwcLayer.get(), requestedCompositionType, z); |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 460 | writeOutputIndependentGeometryStateToHWC(hwcLayer.get(), *outputIndependentState, |
| 461 | skipLayer); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 462 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 463 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 464 | writeOutputDependentPerFrameStateToHWC(hwcLayer.get()); |
Alec Mouri | 028676a | 2021-12-02 15:01:48 -0800 | [diff] [blame] | 465 | writeOutputIndependentPerFrameStateToHWC(hwcLayer.get(), *outputIndependentState, |
| 466 | requestedCompositionType, skipLayer); |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 467 | |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 468 | writeCompositionTypeToHWC(hwcLayer.get(), requestedCompositionType, isPeekingThrough, |
| 469 | skipLayer); |
Lloyd Pique | 46b72df | 2019-10-29 13:19:27 -0700 | [diff] [blame] | 470 | |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 471 | writeLutToHWC(hwcLayer.get(), *outputIndependentState); |
| 472 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 473 | if (requestedCompositionType == Composition::SOLID_COLOR) { |
Alec Mouri | 028676a | 2021-12-02 15:01:48 -0800 | [diff] [blame] | 474 | writeSolidColorStateToHWC(hwcLayer.get(), *outputIndependentState); |
| 475 | } |
Yichi Chen | 413d46a | 2021-04-07 21:42:09 +0800 | [diff] [blame] | 476 | |
| 477 | editState().hwc->stateOverridden = isOverridden; |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 478 | editState().hwc->layerSkipped = skipLayer; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 479 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 480 | |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 481 | void OutputLayer::writeOutputDependentGeometryStateToHWC(HWC2::Layer* hwcLayer, |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 482 | Composition requestedCompositionType, |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 483 | uint32_t z) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 484 | const auto& outputDependentState = getState(); |
| 485 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 486 | Rect displayFrame = outputDependentState.displayFrame; |
| 487 | FloatRect sourceCrop = outputDependentState.sourceCrop; |
Huihong Luo | a582511 | 2021-03-24 12:28:29 -0700 | [diff] [blame] | 488 | |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 489 | if (outputDependentState.overrideInfo.buffer != nullptr) { |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 490 | displayFrame = outputDependentState.overrideInfo.displayFrame; |
Wiwit Rifa'i | 50abed0 | 2022-05-24 02:24:33 +0000 | [diff] [blame] | 491 | sourceCrop = |
| 492 | FloatRect(0.f, 0.f, |
| 493 | static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer() |
| 494 | ->getWidth()), |
| 495 | static_cast<float>(outputDependentState.overrideInfo.buffer->getBuffer() |
| 496 | ->getHeight())); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 499 | ALOGV("Writing display frame [%d, %d, %d, %d]", displayFrame.left, displayFrame.top, |
| 500 | displayFrame.right, displayFrame.bottom); |
| 501 | |
| 502 | if (auto error = hwcLayer->setDisplayFrame(displayFrame); error != hal::Error::NONE) { |
| 503 | ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", |
| 504 | getLayerFE().getDebugName(), displayFrame.left, displayFrame.top, displayFrame.right, |
| 505 | displayFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error)); |
| 506 | } |
| 507 | |
| 508 | if (auto error = hwcLayer->setSourceCrop(sourceCrop); error != hal::Error::NONE) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 509 | ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: " |
| 510 | "%s (%d)", |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 511 | getLayerFE().getDebugName(), sourceCrop.left, sourceCrop.top, sourceCrop.right, |
| 512 | sourceCrop.bottom, to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 515 | if (auto error = hwcLayer->setZOrder(z); error != hal::Error::NONE) { |
| 516 | ALOGE("[%s] Failed to set Z %u: %s (%d)", getLayerFE().getDebugName(), z, |
| 517 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 520 | // Solid-color layers and overridden buffers should always use an identity transform. |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 521 | const auto bufferTransform = (requestedCompositionType != Composition::SOLID_COLOR && |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 522 | getState().overrideInfo.buffer == nullptr) |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 523 | ? outputDependentState.bufferTransform |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 524 | : static_cast<hal::Transform>(0); |
| 525 | if (auto error = hwcLayer->setTransform(static_cast<hal::Transform>(bufferTransform)); |
| 526 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 527 | ALOGE("[%s] Failed to set transform %s: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 528 | toString(outputDependentState.bufferTransform).c_str(), to_string(error).c_str(), |
| 529 | static_cast<int32_t>(error)); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void OutputLayer::writeOutputIndependentGeometryStateToHWC( |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 534 | HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState, |
| 535 | bool skipLayer) { |
Leon Scroggins III | e2ee040 | 2021-04-02 16:59:37 -0400 | [diff] [blame] | 536 | // If there is a peekThroughLayer, then this layer has a hole in it. We need to use |
| 537 | // PREMULTIPLIED so it will peek through. |
| 538 | const auto& overrideInfo = getState().overrideInfo; |
| 539 | const auto blendMode = overrideInfo.buffer || overrideInfo.peekThroughLayer |
Alec Mouri | ee69a59 | 2021-03-23 15:00:45 -0700 | [diff] [blame] | 540 | ? hardware::graphics::composer::hal::BlendMode::PREMULTIPLIED |
| 541 | : outputIndependentState.blendMode; |
| 542 | if (auto error = hwcLayer->setBlendMode(blendMode); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 543 | ALOGE("[%s] Failed to set blend mode %s: %s (%d)", getLayerFE().getDebugName(), |
Alec Mouri | ee69a59 | 2021-03-23 15:00:45 -0700 | [diff] [blame] | 544 | 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] | 545 | } |
| 546 | |
Alec Mouri | ee69a59 | 2021-03-23 15:00:45 -0700 | [diff] [blame] | 547 | const float alpha = skipLayer |
| 548 | ? 0.0f |
| 549 | : (getState().overrideInfo.buffer ? 1.0f : outputIndependentState.alpha); |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 550 | ALOGV("Writing alpha %f", alpha); |
| 551 | |
| 552 | if (auto error = hwcLayer->setPlaneAlpha(alpha); error != hal::Error::NONE) { |
| 553 | ALOGE("[%s] Failed to set plane alpha %.3f: %s (%d)", getLayerFE().getDebugName(), alpha, |
| 554 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 557 | for (const auto& [name, entry] : outputIndependentState.metadata) { |
| 558 | if (auto error = hwcLayer->setLayerGenericMetadata(name, entry.mandatory, entry.value); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 559 | error != hal::Error::NONE) { |
Lloyd Pique | 8d9f836 | 2020-02-11 19:13:09 -0800 | [diff] [blame] | 560 | ALOGE("[%s] Failed to set generic metadata %s %s (%d)", getLayerFE().getDebugName(), |
| 561 | name.c_str(), to_string(error).c_str(), static_cast<int32_t>(error)); |
| 562 | } |
| 563 | } |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 566 | void OutputLayer::writeLutToHWC(HWC2::Layer* hwcLayer, |
| 567 | const LayerFECompositionState& outputIndependentState) { |
| 568 | if (!outputIndependentState.luts) { |
| 569 | return; |
| 570 | } |
| 571 | auto& lutFileDescriptor = outputIndependentState.luts->getLutFileDescriptor(); |
| 572 | auto lutOffsets = outputIndependentState.luts->offsets; |
| 573 | auto& lutProperties = outputIndependentState.luts->lutProperties; |
| 574 | |
| 575 | std::vector<LutProperties> aidlProperties; |
| 576 | aidlProperties.reserve(lutProperties.size()); |
| 577 | for (size_t i = 0; i < lutOffsets.size(); i++) { |
| 578 | LutProperties properties; |
| 579 | properties.dimension = static_cast<LutProperties::Dimension>(lutProperties[i].dimension); |
| 580 | properties.size = lutProperties[i].size; |
| 581 | properties.samplingKeys = { |
| 582 | static_cast<LutProperties::SamplingKey>(lutProperties[i].samplingKey)}; |
| 583 | aidlProperties.emplace_back(properties); |
| 584 | } |
| 585 | |
| 586 | Luts luts; |
| 587 | luts.pfd = ndk::ScopedFileDescriptor(dup(lutFileDescriptor.get())); |
| 588 | luts.offsets = lutOffsets; |
| 589 | luts.lutProperties = std::move(aidlProperties); |
| 590 | |
| 591 | switch (auto error = hwcLayer->setLuts(luts)) { |
| 592 | case hal::Error::NONE: |
| 593 | break; |
| 594 | default: |
| 595 | ALOGE("[%s] Failed to set Luts: %s (%d)", getLayerFE().getDebugName(), |
| 596 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 597 | } |
| 598 | } |
| 599 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 600 | void OutputLayer::writeOutputDependentPerFrameStateToHWC(HWC2::Layer* hwcLayer) { |
| 601 | const auto& outputDependentState = getState(); |
| 602 | |
Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 603 | // TODO(lpique): b/121291683 outputSpaceVisibleRegion is output-dependent geometry |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 604 | // state and should not change every frame. |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 605 | Region visibleRegion = outputDependentState.overrideInfo.buffer |
| 606 | ? Region(outputDependentState.overrideInfo.visibleRegion) |
| 607 | : outputDependentState.outputSpaceVisibleRegion; |
| 608 | if (auto error = hwcLayer->setVisibleRegion(visibleRegion); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 609 | ALOGE("[%s] Failed to set visible region: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 610 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Leon Scroggins III | f2b8ec4 | 2022-01-05 13:23:36 -0500 | [diff] [blame] | 611 | visibleRegion.dump(LOG_TAG); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Leon Scroggins III | 9a0afda | 2022-01-11 16:53:09 -0500 | [diff] [blame] | 614 | if (auto error = |
| 615 | hwcLayer->setBlockingRegion(outputDependentState.outputSpaceBlockingRegionHint); |
| 616 | error != hal::Error::NONE) { |
| 617 | ALOGE("[%s] Failed to set blocking region: %s (%d)", getLayerFE().getDebugName(), |
| 618 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 619 | outputDependentState.outputSpaceBlockingRegionHint.dump(LOG_TAG); |
| 620 | } |
| 621 | |
Alec Mouri | b7edfc2 | 2021-03-17 16:20:26 -0700 | [diff] [blame] | 622 | const auto dataspace = outputDependentState.overrideInfo.buffer |
| 623 | ? outputDependentState.overrideInfo.dataspace |
| 624 | : outputDependentState.dataspace; |
| 625 | |
| 626 | if (auto error = hwcLayer->setDataspace(dataspace); error != hal::Error::NONE) { |
| 627 | ALOGE("[%s] Failed to set dataspace %d: %s (%d)", getLayerFE().getDebugName(), dataspace, |
| 628 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 629 | } |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 630 | |
Alec Mouri | e8dd356 | 2022-02-11 14:18:57 -0800 | [diff] [blame] | 631 | // Cached layers are not dimmed, which means that composer should attempt to dim. |
| 632 | // Note that if the dimming ratio is large, then this may cause the cached layer |
| 633 | // to kick back into GPU composition :( |
| 634 | // Also note that this assumes that there are no HDR layers that are able to be cached. |
| 635 | // Otherwise, this could cause HDR layers to be dimmed twice. |
| 636 | const auto dimmingRatio = outputDependentState.overrideInfo.buffer |
| 637 | ? (getOutput().getState().displayBrightnessNits != 0.f |
| 638 | ? std::clamp(getOutput().getState().sdrWhitePointNits / |
| 639 | getOutput().getState().displayBrightnessNits, |
| 640 | 0.f, 1.f) |
| 641 | : 1.f) |
| 642 | : outputDependentState.dimmingRatio; |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 643 | |
Alec Mouri | 6da0e27 | 2022-02-07 12:45:57 -0800 | [diff] [blame] | 644 | if (auto error = hwcLayer->setBrightness(dimmingRatio); error != hal::Error::NONE) { |
| 645 | ALOGE("[%s] Failed to set brightness %f: %s (%d)", getLayerFE().getDebugName(), |
| 646 | dimmingRatio, to_string(error).c_str(), static_cast<int32_t>(error)); |
Alec Mouri | cdf6cbc | 2021-11-01 17:21:15 -0700 | [diff] [blame] | 647 | } |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void OutputLayer::writeOutputIndependentPerFrameStateToHWC( |
Alec Mouri | 96ca45c | 2021-06-09 17:32:26 -0700 | [diff] [blame] | 651 | HWC2::Layer* hwcLayer, const LayerFECompositionState& outputIndependentState, |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 652 | Composition compositionType, bool skipLayer) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 653 | switch (auto error = hwcLayer->setColorTransform(outputIndependentState.colorTransform)) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 654 | case hal::Error::NONE: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 655 | break; |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 656 | case hal::Error::UNSUPPORTED: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 657 | editState().forceClientComposition = true; |
| 658 | break; |
| 659 | default: |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 660 | ALOGE("[%s] Failed to set color transform: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 661 | to_string(error).c_str(), static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 662 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 663 | |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 664 | const Region& surfaceDamage = getState().overrideInfo.buffer |
| 665 | ? getState().overrideInfo.damageRegion |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 666 | : (getState().hwc->stateOverridden ? Region::INVALID_REGION |
| 667 | : outputIndependentState.surfaceDamage); |
Alec Mouri | 464352b | 2021-03-24 16:33:21 -0700 | [diff] [blame] | 668 | |
| 669 | if (auto error = hwcLayer->setSurfaceDamage(surfaceDamage); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 670 | ALOGE("[%s] Failed to set surface damage: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 671 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 672 | outputIndependentState.surfaceDamage.dump(LOG_TAG); |
| 673 | } |
| 674 | |
| 675 | // Content-specific per-frame state |
Alec Mouri | 028676a | 2021-12-02 15:01:48 -0800 | [diff] [blame] | 676 | switch (compositionType) { |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 677 | case Composition::SOLID_COLOR: |
Lloyd Pique | 46b72df | 2019-10-29 13:19:27 -0700 | [diff] [blame] | 678 | // For compatibility, should be written AFTER the composition type. |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 679 | break; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 680 | case Composition::SIDEBAND: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 681 | writeSidebandStateToHWC(hwcLayer, outputIndependentState); |
| 682 | break; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 683 | case Composition::CURSOR: |
| 684 | case Composition::DEVICE: |
Leon Scroggins III | 09c2541 | 2021-12-02 14:49:56 -0500 | [diff] [blame] | 685 | case Composition::DISPLAY_DECORATION: |
ramindani | c1945cb | 2023-02-03 13:28:15 -0800 | [diff] [blame] | 686 | case Composition::REFRESH_RATE_INDICATOR: |
Alec Mouri | 96ca45c | 2021-06-09 17:32:26 -0700 | [diff] [blame] | 687 | writeBufferStateToHWC(hwcLayer, outputIndependentState, skipLayer); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 688 | break; |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 689 | case Composition::INVALID: |
| 690 | case Composition::CLIENT: |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 691 | // Ignored |
| 692 | break; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | void OutputLayer::writeSolidColorStateToHWC(HWC2::Layer* hwcLayer, |
| 697 | const LayerFECompositionState& outputIndependentState) { |
Ady Abraham | 6e60b14 | 2022-01-06 18:10:35 -0800 | [diff] [blame] | 698 | aidl::android::hardware::graphics::composer3::Color color = {outputIndependentState.color.r, |
| 699 | outputIndependentState.color.g, |
| 700 | outputIndependentState.color.b, |
| 701 | 1.0f}; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 702 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 703 | if (auto error = hwcLayer->setColor(color); error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 704 | ALOGE("[%s] Failed to set color: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 705 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | void OutputLayer::writeSidebandStateToHWC(HWC2::Layer* hwcLayer, |
| 710 | const LayerFECompositionState& outputIndependentState) { |
| 711 | if (auto error = hwcLayer->setSidebandStream(outputIndependentState.sidebandStream->handle()); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 712 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 713 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 714 | outputIndependentState.sidebandStream->handle(), to_string(error).c_str(), |
| 715 | static_cast<int32_t>(error)); |
| 716 | } |
| 717 | } |
| 718 | |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 719 | void OutputLayer::uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) { |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 720 | auto& state = editState(); |
| 721 | // Skip doing this if there is no HWC interface |
| 722 | if (!state.hwc) { |
| 723 | return; |
| 724 | } |
| 725 | |
Brian Lindahl | 3e1e1e6 | 2022-12-21 14:28:58 -0700 | [diff] [blame] | 726 | // Uncache the active buffer last so that it's the first buffer to be purged from the cache |
| 727 | // next time a buffer is sent to this layer. |
| 728 | bool uncacheActiveBuffer = false; |
| 729 | |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 730 | std::vector<uint32_t> slotsToClear; |
| 731 | for (uint64_t bufferId : bufferIdsToUncache) { |
Brian Lindahl | 3e1e1e6 | 2022-12-21 14:28:58 -0700 | [diff] [blame] | 732 | if (bufferId == state.hwc->activeBufferId) { |
| 733 | uncacheActiveBuffer = true; |
| 734 | } else { |
| 735 | uint32_t slot = state.hwc->hwcBufferCache.uncache(bufferId); |
| 736 | if (slot != UINT32_MAX) { |
| 737 | slotsToClear.push_back(slot); |
| 738 | } |
Brian Lindahl | 90553da | 2022-12-06 13:36:30 -0700 | [diff] [blame] | 739 | } |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 740 | } |
Brian Lindahl | 3e1e1e6 | 2022-12-21 14:28:58 -0700 | [diff] [blame] | 741 | if (uncacheActiveBuffer) { |
| 742 | slotsToClear.push_back(state.hwc->hwcBufferCache.uncache(state.hwc->activeBufferId)); |
| 743 | } |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 744 | |
| 745 | hal::Error error = |
| 746 | state.hwc->hwcLayer->setBufferSlotsToClear(slotsToClear, state.hwc->activeBufferSlot); |
| 747 | if (error != hal::Error::NONE) { |
| 748 | ALOGE("[%s] Failed to clear buffer slots: %s (%d)", getLayerFE().getDebugName(), |
| 749 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 750 | } |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 753 | void OutputLayer::writeBufferStateToHWC(HWC2::Layer* hwcLayer, |
Alec Mouri | 96ca45c | 2021-06-09 17:32:26 -0700 | [diff] [blame] | 754 | const LayerFECompositionState& outputIndependentState, |
| 755 | bool skipLayer) { |
Huihong Luo | 5e16196 | 2023-08-18 14:26:32 -0700 | [diff] [blame] | 756 | if (skipLayer && outputIndependentState.buffer == nullptr) { |
| 757 | return; |
| 758 | } |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 759 | auto supportedPerFrameMetadata = |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 760 | getOutput().getDisplayColorProfile()->getSupportedPerFrameMetadata(); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 761 | if (auto error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, |
| 762 | outputIndependentState.hdrMetadata); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 763 | error != hal::Error::NONE && error != hal::Error::UNSUPPORTED) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 764 | ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 765 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 766 | } |
| 767 | |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 768 | HwcSlotAndBuffer hwcSlotAndBuffer; |
| 769 | sp<Fence> hwcFence; |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 770 | { |
| 771 | // Editing the state only because we update the HWC buffer cache and active buffer. |
| 772 | auto& state = editState(); |
| 773 | // Override buffers use a special cache slot so that they don't evict client buffers. |
| 774 | if (state.overrideInfo.buffer != nullptr && !skipLayer) { |
| 775 | hwcSlotAndBuffer = state.hwc->hwcBufferCache.getOverrideHwcSlotAndBuffer( |
| 776 | state.overrideInfo.buffer->getBuffer()); |
| 777 | hwcFence = state.overrideInfo.acquireFence; |
Brian Lindahl | 3e1e1e6 | 2022-12-21 14:28:58 -0700 | [diff] [blame] | 778 | // Keep track of the active buffer ID so when it's discarded we uncache it last so its |
| 779 | // slot will be used first, allowing the memory to be freed as soon as possible. |
| 780 | state.hwc->activeBufferId = state.overrideInfo.buffer->getBuffer()->getId(); |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 781 | } else { |
| 782 | hwcSlotAndBuffer = |
| 783 | state.hwc->hwcBufferCache.getHwcSlotAndBuffer(outputIndependentState.buffer); |
| 784 | hwcFence = outputIndependentState.acquireFence; |
Brian Lindahl | 3e1e1e6 | 2022-12-21 14:28:58 -0700 | [diff] [blame] | 785 | // Keep track of the active buffer ID so when it's discarded we uncache it last so its |
| 786 | // slot will be used first, allowing the memory to be freed as soon as possible. |
| 787 | state.hwc->activeBufferId = outputIndependentState.buffer->getId(); |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 788 | } |
Brian Lindahl | b158a5c | 2022-12-15 15:21:13 -0700 | [diff] [blame] | 789 | // Keep track of the active buffer slot, so we can restore it after clearing other buffer |
| 790 | // slots. |
Brian Lindahl | 3e1e1e6 | 2022-12-21 14:28:58 -0700 | [diff] [blame] | 791 | state.hwc->activeBufferSlot = hwcSlotAndBuffer.slot; |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 794 | if (auto error = hwcLayer->setBuffer(hwcSlotAndBuffer.slot, hwcSlotAndBuffer.buffer, hwcFence); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 795 | error != hal::Error::NONE) { |
Brian Lindahl | 439afad | 2022-11-14 11:16:55 -0700 | [diff] [blame] | 796 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", getLayerFE().getDebugName(), |
| 797 | hwcSlotAndBuffer.buffer->handle, to_string(error).c_str(), |
| 798 | static_cast<int32_t>(error)); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 802 | void OutputLayer::writeCompositionTypeToHWC(HWC2::Layer* hwcLayer, |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 803 | Composition requestedCompositionType, |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 804 | bool isPeekingThrough, bool skipLayer) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 805 | auto& outputDependentState = editState(); |
| 806 | |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 807 | if (isClientCompositionForced(isPeekingThrough)) { |
| 808 | // If we are forcing client composition, we need to tell the HWC |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 809 | requestedCompositionType = Composition::CLIENT; |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Set the requested composition type with the HWC whenever it changes |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 813 | // We also resend the composition type when this layer was previously skipped, to ensure that |
| 814 | // the composition type is up-to-date. |
| 815 | if (outputDependentState.hwc->hwcCompositionType != requestedCompositionType || |
| 816 | (outputDependentState.hwc->layerSkipped && !skipLayer)) { |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 817 | outputDependentState.hwc->hwcCompositionType = requestedCompositionType; |
| 818 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 819 | if (auto error = hwcLayer->setCompositionType(requestedCompositionType); |
| 820 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 821 | ALOGE("[%s] Failed to set composition type %s: %s (%d)", getLayerFE().getDebugName(), |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 822 | to_string(requestedCompositionType).c_str(), to_string(error).c_str(), |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 823 | static_cast<int32_t>(error)); |
| 824 | } |
Lloyd Pique | a83776c | 2019-01-29 18:42:32 -0800 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 828 | void OutputLayer::writeCursorPositionToHWC() const { |
| 829 | // Skip doing this if there is no HWC interface |
| 830 | auto hwcLayer = getHwcLayer(); |
| 831 | if (!hwcLayer) { |
| 832 | return; |
| 833 | } |
| 834 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 835 | const auto* layerFEState = getLayerFE().getCompositionState(); |
| 836 | if (!layerFEState) { |
| 837 | return; |
| 838 | } |
| 839 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 840 | const auto& outputState = getOutput().getState(); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 841 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 842 | Rect frame = layerFEState->cursorFrame; |
Angel Aguayo | b084e0c | 2021-08-04 23:27:28 +0000 | [diff] [blame] | 843 | frame.intersect(outputState.layerStackSpace.getContent(), &frame); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 844 | Rect position = outputState.transform.transform(frame); |
| 845 | |
| 846 | if (auto error = hwcLayer->setCursorPosition(position.left, position.top); |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 847 | error != hal::Error::NONE) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 848 | ALOGE("[%s] Failed to set cursor position to (%d, %d): %s (%d)", |
| 849 | getLayerFE().getDebugName(), position.left, position.top, to_string(error).c_str(), |
| 850 | static_cast<int32_t>(error)); |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 854 | HWC2::Layer* OutputLayer::getHwcLayer() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 855 | const auto& state = getState(); |
| 856 | return state.hwc ? state.hwc->hwcLayer.get() : nullptr; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | bool OutputLayer::requiresClientComposition() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 860 | const auto& state = getState(); |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 861 | return !state.hwc || state.hwc->hwcCompositionType == Composition::CLIENT; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 864 | bool OutputLayer::isHardwareCursor() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 865 | const auto& state = getState(); |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 866 | return state.hwc && state.hwc->hwcCompositionType == Composition::CURSOR; |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 869 | void OutputLayer::detectDisallowedCompositionTypeChange(Composition from, Composition to) const { |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 870 | bool result = false; |
| 871 | switch (from) { |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 872 | case Composition::INVALID: |
| 873 | case Composition::CLIENT: |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 874 | result = false; |
| 875 | break; |
| 876 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 877 | case Composition::DEVICE: |
| 878 | case Composition::SOLID_COLOR: |
| 879 | result = (to == Composition::CLIENT); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 880 | break; |
| 881 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 882 | case Composition::CURSOR: |
| 883 | case Composition::SIDEBAND: |
Leon Scroggins | 7fd536f | 2021-12-28 14:43:12 +0000 | [diff] [blame] | 884 | case Composition::DISPLAY_DECORATION: |
ramindani | c1945cb | 2023-02-03 13:28:15 -0800 | [diff] [blame] | 885 | case Composition::REFRESH_RATE_INDICATOR: |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 886 | result = (to == Composition::CLIENT || to == Composition::DEVICE); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | |
| 890 | if (!result) { |
| 891 | ALOGE("[%s] Invalid device requested composition type change: %s (%d) --> %s (%d)", |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 892 | getLayerFE().getDebugName(), to_string(from).c_str(), static_cast<int>(from), |
| 893 | to_string(to).c_str(), static_cast<int>(to)); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 897 | bool OutputLayer::isClientCompositionForced(bool isPeekingThrough) const { |
| 898 | return getState().forceClientComposition || |
| 899 | (!isPeekingThrough && getLayerFE().hasRoundedCorners()); |
| 900 | } |
| 901 | |
Leon Scroggins III | 2e1aa18 | 2021-12-01 17:33:12 -0500 | [diff] [blame] | 902 | void OutputLayer::applyDeviceCompositionTypeChange(Composition compositionType) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 903 | auto& state = editState(); |
| 904 | LOG_FATAL_IF(!state.hwc); |
| 905 | auto& hwcState = *state.hwc; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 906 | |
Alec Mouri | d1bf1b5 | 2021-05-05 18:44:58 -0700 | [diff] [blame] | 907 | // Only detected disallowed changes if this was not a skip layer, because the |
| 908 | // validated composition type may be arbitrary (usually DEVICE, to reflect that there were |
| 909 | // fewer GPU layers) |
| 910 | if (!hwcState.layerSkipped) { |
| 911 | detectDisallowedCompositionTypeChange(hwcState.hwcCompositionType, compositionType); |
| 912 | } |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 913 | |
| 914 | hwcState.hwcCompositionType = compositionType; |
| 915 | } |
| 916 | |
| 917 | void OutputLayer::prepareForDeviceLayerRequests() { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 918 | auto& state = editState(); |
| 919 | state.clearClientTarget = false; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 920 | } |
| 921 | |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 922 | void OutputLayer::applyDeviceLayerRequest(hal::LayerRequest request) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 923 | auto& state = editState(); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 924 | switch (request) { |
Peiyong Lin | e9d809e | 2020-04-14 13:10:48 -0700 | [diff] [blame] | 925 | case hal::LayerRequest::CLEAR_CLIENT_TARGET: |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 926 | state.clearClientTarget = true; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 927 | break; |
| 928 | |
| 929 | default: |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 930 | ALOGE("[%s] Unknown device layer request %s (%d)", getLayerFE().getDebugName(), |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 931 | toString(request).c_str(), static_cast<int>(request)); |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | |
Sally Qi | 0abc4a5 | 2024-09-26 16:13:06 -0700 | [diff] [blame] | 936 | void OutputLayer::applyDeviceLayerLut( |
| 937 | ndk::ScopedFileDescriptor lutFileDescriptor, |
| 938 | std::vector<std::pair<int, LutProperties>> lutOffsetsAndProperties) { |
| 939 | auto& state = editState(); |
| 940 | LOG_FATAL_IF(!state.hwc); |
| 941 | auto& hwcState = *state.hwc; |
| 942 | std::vector<int32_t> offsets; |
| 943 | std::vector<int32_t> dimensions; |
| 944 | std::vector<int32_t> sizes; |
| 945 | std::vector<int32_t> samplingKeys; |
| 946 | for (const auto& [offset, properties] : lutOffsetsAndProperties) { |
| 947 | // The Lut(s) that comes back through CommandResultPayload should be |
| 948 | // only one sampling key. |
| 949 | if (properties.samplingKeys.size() == 1) { |
| 950 | offsets.emplace_back(offset); |
| 951 | dimensions.emplace_back(static_cast<int32_t>(properties.dimension)); |
| 952 | sizes.emplace_back(static_cast<int32_t>(properties.size)); |
| 953 | samplingKeys.emplace_back(static_cast<int32_t>(properties.samplingKeys[0])); |
| 954 | } |
| 955 | } |
| 956 | hwcState.luts = std::make_shared<gui::DisplayLuts>(base::unique_fd(lutFileDescriptor.release()), |
| 957 | std::move(offsets), std::move(dimensions), |
| 958 | std::move(sizes), std::move(samplingKeys)); |
Sally Qi | 95f669a | 2024-08-27 11:31:42 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 961 | bool OutputLayer::needsFiltering() const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 962 | const auto& state = getState(); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 963 | const auto& sourceCrop = state.sourceCrop; |
Alec Mouri | b6d7893 | 2023-09-20 16:05:42 +0000 | [diff] [blame] | 964 | auto displayFrameWidth = static_cast<float>(state.displayFrame.getWidth()); |
| 965 | auto displayFrameHeight = static_cast<float>(state.displayFrame.getHeight()); |
| 966 | |
| 967 | if (state.bufferTransform & HAL_TRANSFORM_ROT_90) { |
| 968 | std::swap(displayFrameWidth, displayFrameHeight); |
| 969 | } |
| 970 | |
| 971 | return sourceCrop.getHeight() != displayFrameHeight || |
| 972 | sourceCrop.getWidth() != displayFrameWidth; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 973 | } |
| 974 | |
Patrick Williams | 16d8b2c | 2022-08-08 17:29:05 +0000 | [diff] [blame] | 975 | std::optional<LayerFE::LayerSettings> OutputLayer::getOverrideCompositionSettings() const { |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 976 | if (getState().overrideInfo.buffer == nullptr) { |
| 977 | return {}; |
| 978 | } |
| 979 | |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 980 | // Compute the geometry boundaries in layer stack space: we need to transform from the |
| 981 | // framebuffer space of the override buffer to layer space. |
| 982 | const ProjectionSpace& layerSpace = getOutput().getState().layerStackSpace; |
| 983 | const ui::Transform transform = getState().overrideInfo.displaySpace.getTransform(layerSpace); |
Wiwit Rifa'i | 50abed0 | 2022-05-24 02:24:33 +0000 | [diff] [blame] | 984 | const Rect boundaries = transform.transform(getState().overrideInfo.displayFrame); |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 985 | |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 986 | LayerFE::LayerSettings settings; |
| 987 | settings.geometry = renderengine::Geometry{ |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 988 | .boundaries = boundaries.toFloatRect(), |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 989 | }; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 990 | settings.bufferId = getState().overrideInfo.buffer->getBuffer()->getId(); |
Alec Mouri | 7be6c0a | 2021-03-19 15:22:01 -0700 | [diff] [blame] | 991 | settings.source = renderengine::PixelSource{ |
| 992 | .buffer = renderengine::Buffer{ |
| 993 | .buffer = getState().overrideInfo.buffer, |
| 994 | .fence = getState().overrideInfo.acquireFence, |
| 995 | // If the transform from layer space to display space contains a rotation, we |
| 996 | // need to undo the rotation in the texture transform |
| 997 | .textureTransform = |
| 998 | ui::Transform(transform.inverse().getOrientation(), 1, 1).asMatrix4(), |
| 999 | }}; |
Alec Mouri | 9c8fce0 | 2021-03-12 18:30:42 -0800 | [diff] [blame] | 1000 | settings.sourceDataspace = getState().overrideInfo.dataspace; |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1001 | settings.alpha = 1.0f; |
Alec Mouri | e8dd356 | 2022-02-11 14:18:57 -0800 | [diff] [blame] | 1002 | settings.whitePointNits = getOutput().getState().sdrWhitePointNits; |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1003 | |
Patrick Williams | 16d8b2c | 2022-08-08 17:29:05 +0000 | [diff] [blame] | 1004 | return settings; |
Dan Stoza | 6166c31 | 2021-01-15 16:34:05 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 1007 | void OutputLayer::dump(std::string& out) const { |
| 1008 | using android::base::StringAppendF; |
| 1009 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 1010 | StringAppendF(&out, " - Output Layer %p(%s)\n", this, getLayerFE().getDebugName()); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1011 | dumpState(out); |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 1014 | } // namespace impl |
| 1015 | } // namespace android::compositionengine |