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