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