Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 21 | #include <thread> |
| 22 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| 24 | #include <compositionengine/CompositionEngine.h> |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 25 | #include <compositionengine/CompositionRefreshArgs.h> |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 26 | #include <compositionengine/DisplayColorProfile.h> |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 27 | #include <compositionengine/Layer.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 28 | #include <compositionengine/LayerFE.h> |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 29 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 30 | #include <compositionengine/RenderSurface.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 31 | #include <compositionengine/impl/Output.h> |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 32 | #include <compositionengine/impl/OutputCompositionState.h> |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 33 | #include <compositionengine/impl/OutputLayer.h> |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 34 | #include <compositionengine/impl/OutputLayerCompositionState.h> |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 35 | #include <renderengine/DisplaySettings.h> |
| 36 | #include <renderengine/RenderEngine.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 37 | #include <ui/DebugUtils.h> |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 38 | #include <ui/HdrCapabilities.h> |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 39 | #include <utils/Trace.h> |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 40 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 41 | #include "TracedOrdinal.h" |
| 42 | |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 43 | namespace android::compositionengine { |
| 44 | |
| 45 | Output::~Output() = default; |
| 46 | |
| 47 | namespace impl { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 48 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | template <typename T> |
| 52 | class Reversed { |
| 53 | public: |
| 54 | explicit Reversed(const T& container) : mContainer(container) {} |
| 55 | auto begin() { return mContainer.rbegin(); } |
| 56 | auto end() { return mContainer.rend(); } |
| 57 | |
| 58 | private: |
| 59 | const T& mContainer; |
| 60 | }; |
| 61 | |
| 62 | // Helper for enumerating over a container in reverse order |
| 63 | template <typename T> |
| 64 | Reversed<T> reversed(const T& c) { |
| 65 | return Reversed<T>(c); |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 70 | std::shared_ptr<Output> createOutput( |
| 71 | const compositionengine::CompositionEngine& compositionEngine) { |
| 72 | return createOutputTemplated<Output>(compositionEngine); |
| 73 | } |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 74 | |
| 75 | Output::~Output() = default; |
| 76 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 77 | bool Output::isValid() const { |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 78 | return mDisplayColorProfile && mDisplayColorProfile->isValid() && mRenderSurface && |
| 79 | mRenderSurface->isValid(); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Lloyd Pique | 6c564cf | 2019-05-17 17:31:36 -0700 | [diff] [blame] | 82 | std::optional<DisplayId> Output::getDisplayId() const { |
| 83 | return {}; |
| 84 | } |
| 85 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 86 | const std::string& Output::getName() const { |
| 87 | return mName; |
| 88 | } |
| 89 | |
| 90 | void Output::setName(const std::string& name) { |
| 91 | mName = name; |
| 92 | } |
| 93 | |
| 94 | void Output::setCompositionEnabled(bool enabled) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 95 | auto& outputState = editState(); |
| 96 | if (outputState.isEnabled == enabled) { |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 100 | outputState.isEnabled = enabled; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 101 | dirtyEntireOutput(); |
| 102 | } |
| 103 | |
| 104 | void Output::setProjection(const ui::Transform& transform, int32_t orientation, const Rect& frame, |
| 105 | const Rect& viewport, const Rect& scissor, bool needsFiltering) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 106 | auto& outputState = editState(); |
| 107 | outputState.transform = transform; |
| 108 | outputState.orientation = orientation; |
| 109 | outputState.scissor = scissor; |
| 110 | outputState.frame = frame; |
| 111 | outputState.viewport = viewport; |
| 112 | outputState.needsFiltering = needsFiltering; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 113 | |
| 114 | dirtyEntireOutput(); |
| 115 | } |
| 116 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 117 | // TODO(b/121291683): Rename setSize() once more is moved. |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 118 | void Output::setBounds(const ui::Size& size) { |
| 119 | mRenderSurface->setDisplaySize(size); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 120 | // TODO(b/121291683): Rename outputState.size once more is moved. |
| 121 | editState().bounds = Rect(mRenderSurface->getSize()); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 122 | |
| 123 | dirtyEntireOutput(); |
| 124 | } |
| 125 | |
Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 126 | void Output::setLayerStackFilter(uint32_t layerStackId, bool isInternal) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 127 | auto& outputState = editState(); |
| 128 | outputState.layerStackId = layerStackId; |
| 129 | outputState.layerStackInternal = isInternal; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 130 | |
| 131 | dirtyEntireOutput(); |
| 132 | } |
| 133 | |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 134 | void Output::setColorTransform(const compositionengine::CompositionRefreshArgs& args) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 135 | auto& colorTransformMatrix = editState().colorTransformMatrix; |
| 136 | if (!args.colorTransformMatrix || colorTransformMatrix == args.colorTransformMatrix) { |
Lloyd Pique | 77f79a2 | 2019-04-29 15:55:40 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 140 | colorTransformMatrix = *args.colorTransformMatrix; |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 141 | |
| 142 | dirtyEntireOutput(); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 145 | void Output::setColorProfile(const ColorProfile& colorProfile) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 146 | ui::Dataspace targetDataspace = |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 147 | getDisplayColorProfile()->getTargetDataspace(colorProfile.mode, colorProfile.dataspace, |
| 148 | colorProfile.colorSpaceAgnosticDataspace); |
Lloyd Pique | f527548 | 2019-01-29 18:42:42 -0800 | [diff] [blame] | 149 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 150 | auto& outputState = editState(); |
| 151 | if (outputState.colorMode == colorProfile.mode && |
| 152 | outputState.dataspace == colorProfile.dataspace && |
| 153 | outputState.renderIntent == colorProfile.renderIntent && |
| 154 | outputState.targetDataspace == targetDataspace) { |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 158 | outputState.colorMode = colorProfile.mode; |
| 159 | outputState.dataspace = colorProfile.dataspace; |
| 160 | outputState.renderIntent = colorProfile.renderIntent; |
| 161 | outputState.targetDataspace = targetDataspace; |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 162 | |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 163 | mRenderSurface->setBufferDataspace(colorProfile.dataspace); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 164 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 165 | ALOGV("Set active color mode: %s (%d), active render intent: %s (%d)", |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 166 | decodeColorMode(colorProfile.mode).c_str(), colorProfile.mode, |
| 167 | decodeRenderIntent(colorProfile.renderIntent).c_str(), colorProfile.renderIntent); |
Lloyd Pique | ef95812 | 2019-02-05 18:00:12 -0800 | [diff] [blame] | 168 | |
| 169 | dirtyEntireOutput(); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void Output::dump(std::string& out) const { |
| 173 | using android::base::StringAppendF; |
| 174 | |
| 175 | StringAppendF(&out, " Composition Output State: [\"%s\"]", mName.c_str()); |
| 176 | |
| 177 | out.append("\n "); |
| 178 | |
| 179 | dumpBase(out); |
| 180 | } |
| 181 | |
| 182 | void Output::dumpBase(std::string& out) const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 183 | dumpState(out); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 184 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 185 | if (mDisplayColorProfile) { |
| 186 | mDisplayColorProfile->dump(out); |
| 187 | } else { |
| 188 | out.append(" No display color profile!\n"); |
| 189 | } |
| 190 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 191 | if (mRenderSurface) { |
| 192 | mRenderSurface->dump(out); |
| 193 | } else { |
| 194 | out.append(" No render surface!\n"); |
| 195 | } |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 196 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 197 | android::base::StringAppendF(&out, "\n %zu Layers\n", getOutputLayerCount()); |
| 198 | for (const auto* outputLayer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | 37c2c9b | 2018-12-04 17:25:10 -0800 | [diff] [blame] | 199 | if (!outputLayer) { |
| 200 | continue; |
| 201 | } |
| 202 | outputLayer->dump(out); |
| 203 | } |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 206 | compositionengine::DisplayColorProfile* Output::getDisplayColorProfile() const { |
| 207 | return mDisplayColorProfile.get(); |
| 208 | } |
| 209 | |
| 210 | void Output::setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile> mode) { |
| 211 | mDisplayColorProfile = std::move(mode); |
| 212 | } |
| 213 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 214 | const Output::ReleasedLayers& Output::getReleasedLayersForTest() const { |
| 215 | return mReleasedLayers; |
| 216 | } |
| 217 | |
Lloyd Pique | 3d0c02e | 2018-10-19 18:38:12 -0700 | [diff] [blame] | 218 | void Output::setDisplayColorProfileForTest( |
| 219 | std::unique_ptr<compositionengine::DisplayColorProfile> mode) { |
| 220 | mDisplayColorProfile = std::move(mode); |
| 221 | } |
| 222 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 223 | compositionengine::RenderSurface* Output::getRenderSurface() const { |
| 224 | return mRenderSurface.get(); |
| 225 | } |
| 226 | |
| 227 | void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) { |
| 228 | mRenderSurface = std::move(surface); |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 229 | editState().bounds = Rect(mRenderSurface->getSize()); |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 230 | |
| 231 | dirtyEntireOutput(); |
| 232 | } |
| 233 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 234 | void Output::cacheClientCompositionRequests(uint32_t cacheSize) { |
| 235 | if (cacheSize == 0) { |
| 236 | mClientCompositionRequestCache.reset(); |
| 237 | } else { |
| 238 | mClientCompositionRequestCache = std::make_unique<ClientCompositionRequestCache>(cacheSize); |
| 239 | } |
| 240 | }; |
| 241 | |
Lloyd Pique | 31cb294 | 2018-10-19 17:23:03 -0700 | [diff] [blame] | 242 | void Output::setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface> surface) { |
| 243 | mRenderSurface = std::move(surface); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 246 | Region Output::getDirtyRegion(bool repaintEverything) const { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 247 | const auto& outputState = getState(); |
| 248 | Region dirty(outputState.viewport); |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 249 | if (!repaintEverything) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 250 | dirty.andSelf(outputState.dirtyRegion); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 251 | } |
| 252 | return dirty; |
| 253 | } |
| 254 | |
Lloyd Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 255 | bool Output::belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const { |
Lloyd Pique | ef36b00 | 2019-01-23 17:52:04 -0800 | [diff] [blame] | 256 | // The layerStackId's must match, and also the layer must not be internal |
| 257 | // only when not on an internal output. |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 258 | const auto& outputState = getState(); |
| 259 | return layerStackId && (*layerStackId == outputState.layerStackId) && |
| 260 | (!internalOnly || outputState.layerStackInternal); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Lloyd Pique | 66c20c4 | 2019-03-07 21:44:02 -0800 | [diff] [blame] | 263 | bool Output::belongsInOutput(const compositionengine::Layer* layer) const { |
| 264 | if (!layer) { |
| 265 | return false; |
| 266 | } |
| 267 | |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 268 | const auto& layerFEState = layer->getFEState(); |
Lloyd Pique | 66c20c4 | 2019-03-07 21:44:02 -0800 | [diff] [blame] | 269 | return belongsInOutput(layerFEState.layerStackId, layerFEState.internalOnly); |
| 270 | } |
| 271 | |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 272 | std::unique_ptr<compositionengine::OutputLayer> Output::createOutputLayer( |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 273 | const std::shared_ptr<compositionengine::Layer>& layer, const sp<LayerFE>& layerFE) const { |
Lloyd Pique | df336d9 | 2019-03-07 21:38:42 -0800 | [diff] [blame] | 274 | return impl::createOutputLayer(*this, layer, layerFE); |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 277 | compositionengine::OutputLayer* Output::getOutputLayerForLayer( |
| 278 | compositionengine::Layer* layer) const { |
| 279 | auto index = findCurrentOutputLayerForLayer(layer); |
| 280 | return index ? getOutputLayerOrderedByZByIndex(*index) : nullptr; |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 283 | std::optional<size_t> Output::findCurrentOutputLayerForLayer( |
| 284 | compositionengine::Layer* layer) const { |
| 285 | for (size_t i = 0; i < getOutputLayerCount(); i++) { |
| 286 | auto outputLayer = getOutputLayerOrderedByZByIndex(i); |
| 287 | if (outputLayer && &outputLayer->getLayer() == layer) { |
| 288 | return i; |
| 289 | } |
| 290 | } |
| 291 | return std::nullopt; |
Lloyd Pique | cc01a45 | 2018-12-04 17:24:00 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Lloyd Pique | c7ef21b | 2019-01-29 18:43:00 -0800 | [diff] [blame] | 294 | void Output::setReleasedLayers(Output::ReleasedLayers&& layers) { |
| 295 | mReleasedLayers = std::move(layers); |
| 296 | } |
| 297 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 298 | void Output::prepare(const compositionengine::CompositionRefreshArgs& refreshArgs, |
| 299 | LayerFESet& geomSnapshots) { |
| 300 | ATRACE_CALL(); |
| 301 | ALOGV(__FUNCTION__); |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 302 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 303 | rebuildLayerStacks(refreshArgs, geomSnapshots); |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 306 | void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) { |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 307 | ATRACE_CALL(); |
| 308 | ALOGV(__FUNCTION__); |
| 309 | |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 310 | updateColorProfile(refreshArgs); |
| 311 | updateAndWriteCompositionState(refreshArgs); |
| 312 | setColorTransform(refreshArgs); |
Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 313 | beginFrame(); |
| 314 | prepareFrame(); |
| 315 | devOptRepaintFlash(refreshArgs); |
| 316 | finishFrame(refreshArgs); |
| 317 | postFramebuffer(); |
| 318 | } |
| 319 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 320 | void Output::rebuildLayerStacks(const compositionengine::CompositionRefreshArgs& refreshArgs, |
| 321 | LayerFESet& layerFESet) { |
| 322 | ATRACE_CALL(); |
| 323 | ALOGV(__FUNCTION__); |
| 324 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 325 | auto& outputState = editState(); |
| 326 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 327 | // Do nothing if this output is not enabled or there is no need to perform this update |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 328 | if (!outputState.isEnabled || CC_LIKELY(!refreshArgs.updatingOutputGeometryThisFrame)) { |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | // Process the layers to determine visibility and coverage |
| 333 | compositionengine::Output::CoverageState coverage{layerFESet}; |
| 334 | collectVisibleLayers(refreshArgs, coverage); |
| 335 | |
| 336 | // Compute the resulting coverage for this output, and store it for later |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 337 | const ui::Transform& tr = outputState.transform; |
| 338 | Region undefinedRegion{outputState.bounds}; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 339 | undefinedRegion.subtractSelf(tr.transform(coverage.aboveOpaqueLayers)); |
| 340 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 341 | outputState.undefinedRegion = undefinedRegion; |
| 342 | outputState.dirtyRegion.orSelf(coverage.dirtyRegion); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void Output::collectVisibleLayers(const compositionengine::CompositionRefreshArgs& refreshArgs, |
| 346 | compositionengine::Output::CoverageState& coverage) { |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 347 | // Evaluate the layers from front to back to determine what is visible. This |
| 348 | // also incrementally calculates the coverage information for each layer as |
| 349 | // well as the entire output. |
| 350 | for (auto& layer : reversed(refreshArgs.layers)) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 351 | // Incrementally process the coverage for each layer |
| 352 | ensureOutputLayerIfVisible(layer, coverage); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 353 | |
| 354 | // TODO(b/121291683): Stop early if the output is completely covered and |
| 355 | // no more layers could even be visible underneath the ones on top. |
| 356 | } |
| 357 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 358 | setReleasedLayers(refreshArgs); |
| 359 | |
| 360 | finalizePendingOutputLayers(); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 361 | |
| 362 | // Generate a simple Z-order values to each visible output layer |
| 363 | uint32_t zOrder = 0; |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 364 | for (auto* outputLayer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 365 | outputLayer->editState().z = zOrder++; |
| 366 | } |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 369 | void Output::ensureOutputLayerIfVisible(std::shared_ptr<compositionengine::Layer> layer, |
| 370 | compositionengine::Output::CoverageState& coverage) { |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 371 | // Note: Converts a wp<LayerFE> to a sp<LayerFE> |
| 372 | auto layerFE = layer->getLayerFE(); |
| 373 | if (layerFE == nullptr) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 374 | return; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Ensure we have a snapshot of the basic geometry layer state. Limit the |
| 378 | // snapshots to once per frame for each candidate layer, as layers may |
| 379 | // appear on multiple outputs. |
| 380 | if (!coverage.latchedLayers.count(layerFE)) { |
| 381 | coverage.latchedLayers.insert(layerFE); |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 382 | layerFE->latchCompositionState(layer->editFEState(), |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 383 | compositionengine::LayerFE::StateSubset::BasicGeometry); |
| 384 | } |
| 385 | |
| 386 | // Obtain a read-only reference to the front-end layer state |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 387 | const auto& layerFEState = layer->getFEState(); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 388 | |
| 389 | // Only consider the layers on the given layer stack |
| 390 | if (!belongsInOutput(layer.get())) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 391 | return; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | * opaqueRegion: area of a surface that is fully opaque. |
| 396 | */ |
| 397 | Region opaqueRegion; |
| 398 | |
| 399 | /* |
| 400 | * visibleRegion: area of a surface that is visible on screen and not fully |
| 401 | * transparent. This is essentially the layer's footprint minus the opaque |
| 402 | * regions above it. Areas covered by a translucent surface are considered |
| 403 | * visible. |
| 404 | */ |
| 405 | Region visibleRegion; |
| 406 | |
| 407 | /* |
| 408 | * coveredRegion: area of a surface that is covered by all visible regions |
| 409 | * above it (which includes the translucent areas). |
| 410 | */ |
| 411 | Region coveredRegion; |
| 412 | |
| 413 | /* |
| 414 | * transparentRegion: area of a surface that is hinted to be completely |
| 415 | * transparent. This is only used to tell when the layer has no visible non- |
| 416 | * transparent regions and can be removed from the layer list. It does not |
| 417 | * affect the visibleRegion of this layer or any layers beneath it. The hint |
| 418 | * may not be correct if apps don't respect the SurfaceView restrictions |
| 419 | * (which, sadly, some don't). |
| 420 | */ |
| 421 | Region transparentRegion; |
| 422 | |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 423 | /* |
| 424 | * shadowRegion: Region cast by the layer's shadow. |
| 425 | */ |
| 426 | Region shadowRegion; |
| 427 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 428 | // handle hidden surfaces by setting the visible region to empty |
| 429 | if (CC_UNLIKELY(!layerFEState.isVisible)) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 430 | return; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | const ui::Transform& tr = layerFEState.geomLayerTransform; |
| 434 | |
| 435 | // Get the visible region |
| 436 | // TODO(b/121291683): Is it worth creating helper methods on LayerFEState |
| 437 | // for computations like this? |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 438 | const Rect visibleRect(tr.transform(layerFEState.geomLayerBounds)); |
| 439 | visibleRegion.set(visibleRect); |
| 440 | |
| 441 | if (layerFEState.shadowRadius > 0.0f) { |
| 442 | // if the layer casts a shadow, offset the layers visible region and |
| 443 | // calculate the shadow region. |
| 444 | const int32_t inset = layerFEState.shadowRadius * -1.0f; |
| 445 | Rect visibleRectWithShadows(visibleRect); |
| 446 | visibleRectWithShadows.inset(inset, inset, inset, inset); |
| 447 | visibleRegion.set(visibleRectWithShadows); |
| 448 | shadowRegion = visibleRegion.subtract(visibleRect); |
| 449 | } |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 450 | |
| 451 | if (visibleRegion.isEmpty()) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 452 | return; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // Remove the transparent area from the visible region |
| 456 | if (!layerFEState.isOpaque) { |
| 457 | if (tr.preserveRects()) { |
| 458 | // transform the transparent region |
| 459 | transparentRegion = tr.transform(layerFEState.transparentRegionHint); |
| 460 | } else { |
| 461 | // transformation too complex, can't do the |
| 462 | // transparent region optimization. |
| 463 | transparentRegion.clear(); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // compute the opaque region |
| 468 | const int32_t layerOrientation = tr.getOrientation(); |
| 469 | if (layerFEState.isOpaque && ((layerOrientation & ui::Transform::ROT_INVALID) == 0)) { |
| 470 | // If we one of the simple category of transforms (0/90/180/270 rotation |
| 471 | // + any flip), then the opaque region is the layer's footprint. |
| 472 | // Otherwise we don't try and compute the opaque region since there may |
| 473 | // be errors at the edges, and we treat the entire layer as |
| 474 | // translucent. |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 475 | opaqueRegion.set(visibleRect); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Clip the covered region to the visible region |
| 479 | coveredRegion = coverage.aboveCoveredLayers.intersect(visibleRegion); |
| 480 | |
| 481 | // Update accumAboveCoveredLayers for next (lower) layer |
| 482 | coverage.aboveCoveredLayers.orSelf(visibleRegion); |
| 483 | |
| 484 | // subtract the opaque region covered by the layers above us |
| 485 | visibleRegion.subtractSelf(coverage.aboveOpaqueLayers); |
| 486 | |
| 487 | if (visibleRegion.isEmpty()) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 488 | return; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // Get coverage information for the layer as previously displayed, |
| 492 | // also taking over ownership from mOutputLayersorderedByZ. |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 493 | auto prevOutputLayerIndex = findCurrentOutputLayerForLayer(layer.get()); |
| 494 | auto prevOutputLayer = |
| 495 | prevOutputLayerIndex ? getOutputLayerOrderedByZByIndex(*prevOutputLayerIndex) : nullptr; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 496 | |
| 497 | // Get coverage information for the layer as previously displayed |
| 498 | // TODO(b/121291683): Define kEmptyRegion as a constant in Region.h |
| 499 | const Region kEmptyRegion; |
| 500 | const Region& oldVisibleRegion = |
| 501 | prevOutputLayer ? prevOutputLayer->getState().visibleRegion : kEmptyRegion; |
| 502 | const Region& oldCoveredRegion = |
| 503 | prevOutputLayer ? prevOutputLayer->getState().coveredRegion : kEmptyRegion; |
| 504 | |
| 505 | // compute this layer's dirty region |
| 506 | Region dirty; |
| 507 | if (layerFEState.contentDirty) { |
| 508 | // we need to invalidate the whole region |
| 509 | dirty = visibleRegion; |
| 510 | // as well, as the old visible region |
| 511 | dirty.orSelf(oldVisibleRegion); |
| 512 | } else { |
| 513 | /* compute the exposed region: |
| 514 | * the exposed region consists of two components: |
| 515 | * 1) what's VISIBLE now and was COVERED before |
| 516 | * 2) what's EXPOSED now less what was EXPOSED before |
| 517 | * |
| 518 | * note that (1) is conservative, we start with the whole visible region |
| 519 | * but only keep what used to be covered by something -- which mean it |
| 520 | * may have been exposed. |
| 521 | * |
| 522 | * (2) handles areas that were not covered by anything but got exposed |
| 523 | * because of a resize. |
| 524 | * |
| 525 | */ |
| 526 | const Region newExposed = visibleRegion - coveredRegion; |
| 527 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; |
| 528 | dirty = (visibleRegion & oldCoveredRegion) | (newExposed - oldExposed); |
| 529 | } |
| 530 | dirty.subtractSelf(coverage.aboveOpaqueLayers); |
| 531 | |
| 532 | // accumulate to the screen dirty region |
| 533 | coverage.dirtyRegion.orSelf(dirty); |
| 534 | |
| 535 | // Update accumAboveOpaqueLayers for next (lower) layer |
| 536 | coverage.aboveOpaqueLayers.orSelf(opaqueRegion); |
| 537 | |
| 538 | // Compute the visible non-transparent region |
| 539 | Region visibleNonTransparentRegion = visibleRegion.subtract(transparentRegion); |
| 540 | |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 541 | // Perform the final check to see if this layer is visible on this output |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 542 | // TODO(b/121291683): Why does this not use visibleRegion? (see outputSpaceVisibleRegion below) |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 543 | const auto& outputState = getState(); |
| 544 | Region drawRegion(outputState.transform.transform(visibleNonTransparentRegion)); |
| 545 | drawRegion.andSelf(outputState.bounds); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 546 | if (drawRegion.isEmpty()) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 547 | return; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 550 | Region visibleNonShadowRegion = visibleRegion.subtract(shadowRegion); |
| 551 | |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 552 | // The layer is visible. Either reuse the existing outputLayer if we have |
| 553 | // one, or create a new one if we do not. |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 554 | auto result = ensureOutputLayer(prevOutputLayerIndex, layer, layerFE); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 555 | |
| 556 | // Store the layer coverage information into the layer state as some of it |
| 557 | // is useful later. |
| 558 | auto& outputLayerState = result->editState(); |
| 559 | outputLayerState.visibleRegion = visibleRegion; |
| 560 | outputLayerState.visibleNonTransparentRegion = visibleNonTransparentRegion; |
| 561 | outputLayerState.coveredRegion = coveredRegion; |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 562 | outputLayerState.outputSpaceVisibleRegion = |
| 563 | outputState.transform.transform(visibleNonShadowRegion.intersect(outputState.viewport)); |
| 564 | outputLayerState.shadowRegion = shadowRegion; |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void Output::setReleasedLayers(const compositionengine::CompositionRefreshArgs&) { |
| 568 | // The base class does nothing with this call. |
| 569 | } |
| 570 | |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 571 | void Output::updateLayerStateFromFE(const CompositionRefreshArgs& args) const { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 572 | for (auto* layer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 573 | layer->getLayerFE().latchCompositionState(layer->getLayer().editFEState(), |
Lloyd Pique | c668734 | 2019-03-07 21:34:57 -0800 | [diff] [blame] | 574 | args.updatingGeometryThisFrame |
| 575 | ? LayerFE::StateSubset::GeometryAndContent |
| 576 | : LayerFE::StateSubset::Content); |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | void Output::updateAndWriteCompositionState( |
| 581 | const compositionengine::CompositionRefreshArgs& refreshArgs) { |
| 582 | ATRACE_CALL(); |
| 583 | ALOGV(__FUNCTION__); |
| 584 | |
Alec Mouri | f9a2a2c | 2019-11-12 12:46:02 -0800 | [diff] [blame] | 585 | if (!getState().isEnabled) { |
| 586 | return; |
| 587 | } |
| 588 | |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 589 | mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition(); |
| 590 | bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr; |
| 591 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 592 | for (auto* layer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | 7a23491 | 2019-10-03 11:54:27 -0700 | [diff] [blame] | 593 | layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame, |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 594 | refreshArgs.devOptForceClientComposition || |
| 595 | forceClientComposition); |
| 596 | |
| 597 | if (mLayerRequestingBackgroundBlur == layer) { |
| 598 | forceClientComposition = false; |
| 599 | } |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 600 | |
| 601 | // Send the updated state to the HWC, if appropriate. |
| 602 | layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame); |
| 603 | } |
| 604 | } |
| 605 | |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 606 | compositionengine::OutputLayer* Output::findLayerRequestingBackgroundComposition() const { |
| 607 | compositionengine::OutputLayer* layerRequestingBgComposition = nullptr; |
| 608 | for (auto* layer : getOutputLayersOrderedByZ()) { |
| 609 | if (layer->getLayer().getFEState().backgroundBlurRadius > 0) { |
| 610 | layerRequestingBgComposition = layer; |
| 611 | } |
| 612 | } |
| 613 | return layerRequestingBgComposition; |
| 614 | } |
| 615 | |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 616 | void Output::updateColorProfile(const compositionengine::CompositionRefreshArgs& refreshArgs) { |
| 617 | setColorProfile(pickColorProfile(refreshArgs)); |
| 618 | } |
| 619 | |
| 620 | // Returns a data space that fits all visible layers. The returned data space |
| 621 | // can only be one of |
| 622 | // - Dataspace::SRGB (use legacy dataspace and let HWC saturate when colors are enhanced) |
| 623 | // - Dataspace::DISPLAY_P3 |
| 624 | // - Dataspace::DISPLAY_BT2020 |
| 625 | // The returned HDR data space is one of |
| 626 | // - Dataspace::UNKNOWN |
| 627 | // - Dataspace::BT2020_HLG |
| 628 | // - Dataspace::BT2020_PQ |
| 629 | ui::Dataspace Output::getBestDataspace(ui::Dataspace* outHdrDataSpace, |
| 630 | bool* outIsHdrClientComposition) const { |
| 631 | ui::Dataspace bestDataSpace = ui::Dataspace::V0_SRGB; |
| 632 | *outHdrDataSpace = ui::Dataspace::UNKNOWN; |
| 633 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 634 | for (const auto* layer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 635 | switch (layer->getLayer().getFEState().dataspace) { |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 636 | case ui::Dataspace::V0_SCRGB: |
| 637 | case ui::Dataspace::V0_SCRGB_LINEAR: |
| 638 | case ui::Dataspace::BT2020: |
| 639 | case ui::Dataspace::BT2020_ITU: |
| 640 | case ui::Dataspace::BT2020_LINEAR: |
| 641 | case ui::Dataspace::DISPLAY_BT2020: |
| 642 | bestDataSpace = ui::Dataspace::DISPLAY_BT2020; |
| 643 | break; |
| 644 | case ui::Dataspace::DISPLAY_P3: |
| 645 | bestDataSpace = ui::Dataspace::DISPLAY_P3; |
| 646 | break; |
| 647 | case ui::Dataspace::BT2020_PQ: |
| 648 | case ui::Dataspace::BT2020_ITU_PQ: |
| 649 | bestDataSpace = ui::Dataspace::DISPLAY_P3; |
| 650 | *outHdrDataSpace = ui::Dataspace::BT2020_PQ; |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 651 | *outIsHdrClientComposition = layer->getLayer().getFEState().forceClientComposition; |
Lloyd Pique | 6a3b446 | 2019-03-07 20:58:12 -0800 | [diff] [blame] | 652 | break; |
| 653 | case ui::Dataspace::BT2020_HLG: |
| 654 | case ui::Dataspace::BT2020_ITU_HLG: |
| 655 | bestDataSpace = ui::Dataspace::DISPLAY_P3; |
| 656 | // When there's mixed PQ content and HLG content, we set the HDR |
| 657 | // data space to be BT2020_PQ and convert HLG to PQ. |
| 658 | if (*outHdrDataSpace == ui::Dataspace::UNKNOWN) { |
| 659 | *outHdrDataSpace = ui::Dataspace::BT2020_HLG; |
| 660 | } |
| 661 | break; |
| 662 | default: |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return bestDataSpace; |
| 668 | } |
| 669 | |
| 670 | compositionengine::Output::ColorProfile Output::pickColorProfile( |
| 671 | const compositionengine::CompositionRefreshArgs& refreshArgs) const { |
| 672 | if (refreshArgs.outputColorSetting == OutputColorSetting::kUnmanaged) { |
| 673 | return ColorProfile{ui::ColorMode::NATIVE, ui::Dataspace::UNKNOWN, |
| 674 | ui::RenderIntent::COLORIMETRIC, |
| 675 | refreshArgs.colorSpaceAgnosticDataspace}; |
| 676 | } |
| 677 | |
| 678 | ui::Dataspace hdrDataSpace; |
| 679 | bool isHdrClientComposition = false; |
| 680 | ui::Dataspace bestDataSpace = getBestDataspace(&hdrDataSpace, &isHdrClientComposition); |
| 681 | |
| 682 | switch (refreshArgs.forceOutputColorMode) { |
| 683 | case ui::ColorMode::SRGB: |
| 684 | bestDataSpace = ui::Dataspace::V0_SRGB; |
| 685 | break; |
| 686 | case ui::ColorMode::DISPLAY_P3: |
| 687 | bestDataSpace = ui::Dataspace::DISPLAY_P3; |
| 688 | break; |
| 689 | default: |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | // respect hdrDataSpace only when there is no legacy HDR support |
| 694 | const bool isHdr = hdrDataSpace != ui::Dataspace::UNKNOWN && |
| 695 | !mDisplayColorProfile->hasLegacyHdrSupport(hdrDataSpace) && !isHdrClientComposition; |
| 696 | if (isHdr) { |
| 697 | bestDataSpace = hdrDataSpace; |
| 698 | } |
| 699 | |
| 700 | ui::RenderIntent intent; |
| 701 | switch (refreshArgs.outputColorSetting) { |
| 702 | case OutputColorSetting::kManaged: |
| 703 | case OutputColorSetting::kUnmanaged: |
| 704 | intent = isHdr ? ui::RenderIntent::TONE_MAP_COLORIMETRIC |
| 705 | : ui::RenderIntent::COLORIMETRIC; |
| 706 | break; |
| 707 | case OutputColorSetting::kEnhanced: |
| 708 | intent = isHdr ? ui::RenderIntent::TONE_MAP_ENHANCE : ui::RenderIntent::ENHANCE; |
| 709 | break; |
| 710 | default: // vendor display color setting |
| 711 | intent = static_cast<ui::RenderIntent>(refreshArgs.outputColorSetting); |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | ui::ColorMode outMode; |
| 716 | ui::Dataspace outDataSpace; |
| 717 | ui::RenderIntent outRenderIntent; |
| 718 | mDisplayColorProfile->getBestColorMode(bestDataSpace, intent, &outDataSpace, &outMode, |
| 719 | &outRenderIntent); |
| 720 | |
| 721 | return ColorProfile{outMode, outDataSpace, outRenderIntent, |
| 722 | refreshArgs.colorSpaceAgnosticDataspace}; |
| 723 | } |
| 724 | |
Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 725 | void Output::beginFrame() { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 726 | auto& outputState = editState(); |
Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 727 | const bool dirty = !getDirtyRegion(false).isEmpty(); |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 728 | const bool empty = getOutputLayerCount() == 0; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 729 | const bool wasEmpty = !outputState.lastCompositionHadVisibleLayers; |
Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 730 | |
| 731 | // If nothing has changed (!dirty), don't recompose. |
| 732 | // If something changed, but we don't currently have any visible layers, |
| 733 | // and didn't when we last did a composition, then skip it this time. |
| 734 | // The second rule does two things: |
| 735 | // - When all layers are removed from a display, we'll emit one black |
| 736 | // frame, then nothing more until we get new layers. |
| 737 | // - When a display is created with a private layer stack, we won't |
| 738 | // emit any black frames until a layer is added to the layer stack. |
| 739 | const bool mustRecompose = dirty && !(empty && wasEmpty); |
| 740 | |
| 741 | const char flagPrefix[] = {'-', '+'}; |
| 742 | static_cast<void>(flagPrefix); |
| 743 | ALOGV_IF("%s: %s composition for %s (%cdirty %cempty %cwasEmpty)", __FUNCTION__, |
| 744 | mustRecompose ? "doing" : "skipping", getName().c_str(), flagPrefix[dirty], |
| 745 | flagPrefix[empty], flagPrefix[wasEmpty]); |
| 746 | |
| 747 | mRenderSurface->beginFrame(mustRecompose); |
| 748 | |
| 749 | if (mustRecompose) { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 750 | outputState.lastCompositionHadVisibleLayers = !empty; |
Lloyd Pique | d0a92a0 | 2019-02-19 17:47:26 -0800 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 754 | void Output::prepareFrame() { |
| 755 | ATRACE_CALL(); |
| 756 | ALOGV(__FUNCTION__); |
| 757 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 758 | const auto& outputState = getState(); |
| 759 | if (!outputState.isEnabled) { |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 760 | return; |
| 761 | } |
| 762 | |
| 763 | chooseCompositionStrategy(); |
| 764 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 765 | mRenderSurface->prepareFrame(outputState.usesClientComposition, |
| 766 | outputState.usesDeviceComposition); |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 767 | } |
| 768 | |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 769 | void Output::devOptRepaintFlash(const compositionengine::CompositionRefreshArgs& refreshArgs) { |
| 770 | if (CC_LIKELY(!refreshArgs.devOptFlashDirtyRegionsDelay)) { |
| 771 | return; |
| 772 | } |
| 773 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 774 | if (getState().isEnabled) { |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 775 | // transform the dirty region into this screen's coordinate space |
| 776 | const Region dirtyRegion = getDirtyRegion(refreshArgs.repaintEverything); |
| 777 | if (!dirtyRegion.isEmpty()) { |
| 778 | base::unique_fd readyFence; |
| 779 | // redraw the whole screen |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 780 | static_cast<void>(composeSurfaces(dirtyRegion)); |
Lloyd Pique | f8cf14d | 2019-02-28 16:03:12 -0800 | [diff] [blame] | 781 | |
| 782 | mRenderSurface->queueBuffer(std::move(readyFence)); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | postFramebuffer(); |
| 787 | |
| 788 | std::this_thread::sleep_for(*refreshArgs.devOptFlashDirtyRegionsDelay); |
| 789 | |
| 790 | prepareFrame(); |
| 791 | } |
| 792 | |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 793 | void Output::finishFrame(const compositionengine::CompositionRefreshArgs&) { |
| 794 | ATRACE_CALL(); |
| 795 | ALOGV(__FUNCTION__); |
| 796 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 797 | if (!getState().isEnabled) { |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 798 | return; |
| 799 | } |
| 800 | |
| 801 | // Repaint the framebuffer (if needed), getting the optional fence for when |
| 802 | // the composition completes. |
| 803 | auto optReadyFence = composeSurfaces(Region::INVALID_REGION); |
| 804 | if (!optReadyFence) { |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | // swap buffers (presentation) |
| 809 | mRenderSurface->queueBuffer(std::move(*optReadyFence)); |
| 810 | } |
| 811 | |
| 812 | std::optional<base::unique_fd> Output::composeSurfaces(const Region& debugRegion) { |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 813 | ATRACE_CALL(); |
| 814 | ALOGV(__FUNCTION__); |
| 815 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 816 | const auto& outputState = getState(); |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 817 | OutputCompositionState& outputCompositionState = editState(); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 818 | const TracedOrdinal<bool> hasClientComposition = {"hasClientComposition", |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 819 | outputState.usesClientComposition}; |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 820 | base::unique_fd readyFence; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 821 | if (!hasClientComposition) { |
Lloyd Pique | a76ce46 | 2020-01-14 13:06:37 -0800 | [diff] [blame] | 822 | setExpensiveRenderingExpected(false); |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 823 | return readyFence; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | ALOGV("hasClientComposition"); |
| 827 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 828 | auto& renderEngine = getCompositionEngine().getRenderEngine(); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 829 | const bool supportsProtectedContent = renderEngine.supportsProtectedContent(); |
| 830 | |
| 831 | renderengine::DisplaySettings clientCompositionDisplay; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 832 | clientCompositionDisplay.physicalDisplay = outputState.scissor; |
| 833 | clientCompositionDisplay.clip = outputState.scissor; |
| 834 | clientCompositionDisplay.globalTransform = outputState.transform.asMatrix4(); |
| 835 | clientCompositionDisplay.orientation = outputState.orientation; |
| 836 | clientCompositionDisplay.outputDataspace = mDisplayColorProfile->hasWideColorGamut() |
| 837 | ? outputState.dataspace |
| 838 | : ui::Dataspace::UNKNOWN; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 839 | clientCompositionDisplay.maxLuminance = |
| 840 | mDisplayColorProfile->getHdrCapabilities().getDesiredMaxLuminance(); |
| 841 | |
| 842 | // Compute the global color transform matrix. |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 843 | if (!outputState.usesDeviceComposition && !getSkipColorTransform()) { |
| 844 | clientCompositionDisplay.colorTransform = outputState.colorTransformMatrix; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | // Note: Updated by generateClientCompositionRequests |
| 848 | clientCompositionDisplay.clearRegion = Region::INVALID_REGION; |
| 849 | |
| 850 | // Generate the client composition requests for the layers on this output. |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 851 | std::vector<LayerFE::LayerSettings> clientCompositionLayers = |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 852 | generateClientCompositionRequests(supportsProtectedContent, |
Vishnu Nair | 3a7346c | 2019-12-04 08:09:09 -0800 | [diff] [blame] | 853 | clientCompositionDisplay.clearRegion, |
| 854 | clientCompositionDisplay.outputDataspace); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 855 | appendRegionFlashRequests(debugRegion, clientCompositionLayers); |
| 856 | |
| 857 | // If we the display is secure, protected content support is enabled, and at |
| 858 | // least one layer has protected content, we need to use a secure back |
| 859 | // buffer. |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 860 | if (outputState.isSecure && supportsProtectedContent) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 861 | auto layers = getOutputLayersOrderedByZ(); |
| 862 | bool needsProtected = std::any_of(layers.begin(), layers.end(), [](auto* layer) { |
| 863 | return layer->getLayer().getFEState().hasProtectedContent; |
| 864 | }); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 865 | if (needsProtected != renderEngine.isProtected()) { |
| 866 | renderEngine.useProtectedContext(needsProtected); |
| 867 | } |
| 868 | if (needsProtected != mRenderSurface->isProtected() && |
| 869 | needsProtected == renderEngine.isProtected()) { |
| 870 | mRenderSurface->setProtected(needsProtected); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | base::unique_fd fd; |
| 875 | sp<GraphicBuffer> buf = mRenderSurface->dequeueBuffer(&fd); |
| 876 | if (buf == nullptr) { |
| 877 | ALOGW("Dequeuing buffer for display [%s] failed, bailing out of " |
| 878 | "client composition for this frame", |
| 879 | mName.c_str()); |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 880 | return std::nullopt; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 883 | // Check if the client composition requests were rendered into the provided graphic buffer. If |
| 884 | // so, we can reuse the buffer and avoid client composition. |
| 885 | if (mClientCompositionRequestCache) { |
| 886 | if (mClientCompositionRequestCache->exists(buf->getId(), clientCompositionDisplay, |
| 887 | clientCompositionLayers)) { |
| 888 | outputCompositionState.reusedClientComposition = true; |
| 889 | setExpensiveRenderingExpected(false); |
| 890 | return readyFence; |
| 891 | } |
| 892 | mClientCompositionRequestCache->add(buf->getId(), clientCompositionDisplay, |
| 893 | clientCompositionLayers); |
| 894 | } |
| 895 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 896 | // We boost GPU frequency here because there will be color spaces conversion |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 897 | // or complex GPU shaders and it's expensive. We boost the GPU frequency so that |
| 898 | // GPU composition can finish in time. We must reset GPU frequency afterwards, |
| 899 | // because high frequency consumes extra battery. |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 900 | const bool expensiveRenderingExpected = |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 901 | clientCompositionDisplay.outputDataspace == ui::Dataspace::DISPLAY_P3 || |
| 902 | mLayerRequestingBackgroundBlur != nullptr; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 903 | if (expensiveRenderingExpected) { |
| 904 | setExpensiveRenderingExpected(true); |
| 905 | } |
| 906 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 907 | std::vector<const renderengine::LayerSettings*> clientCompositionLayerPointers; |
| 908 | clientCompositionLayerPointers.reserve(clientCompositionLayers.size()); |
| 909 | std::transform(clientCompositionLayers.begin(), clientCompositionLayers.end(), |
| 910 | std::back_inserter(clientCompositionLayerPointers), |
| 911 | [](LayerFE::LayerSettings& settings) -> renderengine::LayerSettings* { |
| 912 | return &settings; |
| 913 | }); |
| 914 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 915 | const nsecs_t renderEngineStart = systemTime(); |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 916 | status_t status = |
| 917 | renderEngine.drawLayers(clientCompositionDisplay, clientCompositionLayerPointers, |
| 918 | buf->getNativeBuffer(), /*useFramebufferCache=*/true, |
| 919 | std::move(fd), &readyFence); |
| 920 | |
| 921 | if (status != NO_ERROR && mClientCompositionRequestCache) { |
| 922 | // If rendering was not successful, remove the request from the cache. |
| 923 | mClientCompositionRequestCache->remove(buf->getId()); |
| 924 | } |
| 925 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 926 | auto& timeStats = getCompositionEngine().getTimeStats(); |
| 927 | if (readyFence.get() < 0) { |
| 928 | timeStats.recordRenderEngineDuration(renderEngineStart, systemTime()); |
| 929 | } else { |
| 930 | timeStats.recordRenderEngineDuration(renderEngineStart, |
| 931 | std::make_shared<FenceTime>( |
| 932 | new Fence(dup(readyFence.get())))); |
| 933 | } |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 934 | |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 935 | return readyFence; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 938 | std::vector<LayerFE::LayerSettings> Output::generateClientCompositionRequests( |
Vishnu Nair | 3a7346c | 2019-12-04 08:09:09 -0800 | [diff] [blame] | 939 | bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) { |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 940 | std::vector<LayerFE::LayerSettings> clientCompositionLayers; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 941 | ALOGV("Rendering client layers"); |
| 942 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 943 | const auto& outputState = getState(); |
| 944 | const Region viewportRegion(outputState.viewport); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 945 | const bool useIdentityTransform = false; |
| 946 | bool firstLayer = true; |
| 947 | // Used when a layer clears part of the buffer. |
| 948 | Region dummyRegion; |
| 949 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 950 | for (auto* layer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 951 | const auto& layerState = layer->getState(); |
Lloyd Pique | 9755fb7 | 2019-03-26 14:44:40 -0700 | [diff] [blame] | 952 | const auto& layerFEState = layer->getLayer().getFEState(); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 953 | auto& layerFE = layer->getLayerFE(); |
| 954 | |
Lloyd Pique | a246866 | 2019-03-07 21:31:06 -0800 | [diff] [blame] | 955 | const Region clip(viewportRegion.intersect(layerState.visibleRegion)); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 956 | ALOGV("Layer: %s", layerFE.getDebugName()); |
| 957 | if (clip.isEmpty()) { |
| 958 | ALOGV(" Skipping for empty clip"); |
| 959 | firstLayer = false; |
| 960 | continue; |
| 961 | } |
| 962 | |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 963 | const bool clientComposition = layer->requiresClientComposition(); |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 964 | |
| 965 | // We clear the client target for non-client composed layers if |
| 966 | // requested by the HWC. We skip this if the layer is not an opaque |
| 967 | // rectangle, as by definition the layer must blend with whatever is |
| 968 | // underneath. We also skip the first layer as the buffer target is |
| 969 | // guaranteed to start out cleared. |
| 970 | bool clearClientComposition = |
| 971 | layerState.clearClientTarget && layerFEState.isOpaque && !firstLayer; |
| 972 | |
| 973 | ALOGV(" Composition type: client %d clear %d", clientComposition, clearClientComposition); |
| 974 | |
| 975 | if (clientComposition || clearClientComposition) { |
| 976 | compositionengine::LayerFE::ClientCompositionTargetSettings targetSettings{ |
| 977 | clip, |
| 978 | useIdentityTransform, |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 979 | layer->needsFiltering() || outputState.needsFiltering, |
| 980 | outputState.isSecure, |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 981 | supportsProtectedContent, |
| 982 | clientComposition ? clearRegion : dummyRegion, |
| 983 | }; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 984 | if (std::optional<LayerFE::LayerSettings> result = |
| 985 | layerFE.prepareClientComposition(targetSettings)) { |
Lloyd Pique | c2d54d4 | 2019-08-28 18:04:21 -0700 | [diff] [blame] | 986 | if (!clientComposition) { |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 987 | LayerFE::LayerSettings& layerSettings = *result; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 988 | layerSettings.source.buffer.buffer = nullptr; |
| 989 | layerSettings.source.solidColor = half3(0.0, 0.0, 0.0); |
| 990 | layerSettings.alpha = half(0.0); |
| 991 | layerSettings.disableBlending = true; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 992 | layerSettings.frameNumber = 0; |
Vishnu Nair | 3a7346c | 2019-12-04 08:09:09 -0800 | [diff] [blame] | 993 | } else { |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 994 | std::optional<LayerFE::LayerSettings> shadowLayer = |
Vishnu Nair | 3a7346c | 2019-12-04 08:09:09 -0800 | [diff] [blame] | 995 | layerFE.prepareShadowClientComposition(*result, outputState.viewport, |
| 996 | outputDataspace); |
| 997 | if (shadowLayer) { |
| 998 | clientCompositionLayers.push_back(*shadowLayer); |
| 999 | } |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Vishnu Nair | a483b4a | 2019-12-12 15:07:52 -0800 | [diff] [blame] | 1002 | // If the layer casts a shadow but the content casting the shadow is occluded, skip |
| 1003 | // composing the non-shadow content and only draw the shadows. |
| 1004 | const bool skipNonShadowContentComposition = clientComposition && |
| 1005 | layerState.visibleRegion.subtract(layerState.shadowRegion).isEmpty(); |
| 1006 | |
| 1007 | if (!skipNonShadowContentComposition) { |
| 1008 | layer->editState().clientCompositionTimestamp = systemTime(); |
| 1009 | clientCompositionLayers.push_back(*result); |
| 1010 | } |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | firstLayer = false; |
| 1015 | } |
| 1016 | |
| 1017 | return clientCompositionLayers; |
| 1018 | } |
| 1019 | |
| 1020 | void Output::appendRegionFlashRequests( |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 1021 | const Region& flashRegion, std::vector<LayerFE::LayerSettings>& clientCompositionLayers) { |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1022 | if (flashRegion.isEmpty()) { |
| 1023 | return; |
| 1024 | } |
| 1025 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 1026 | LayerFE::LayerSettings layerSettings; |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1027 | layerSettings.source.buffer.buffer = nullptr; |
| 1028 | layerSettings.source.solidColor = half3(1.0, 0.0, 1.0); |
| 1029 | layerSettings.alpha = half(1.0); |
| 1030 | |
| 1031 | for (const auto& rect : flashRegion) { |
| 1032 | layerSettings.geometry.boundaries = rect.toFloatRect(); |
| 1033 | clientCompositionLayers.push_back(layerSettings); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | void Output::setExpensiveRenderingExpected(bool) { |
| 1038 | // The base class does nothing with this call. |
| 1039 | } |
| 1040 | |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1041 | void Output::postFramebuffer() { |
| 1042 | ATRACE_CALL(); |
| 1043 | ALOGV(__FUNCTION__); |
| 1044 | |
| 1045 | if (!getState().isEnabled) { |
| 1046 | return; |
| 1047 | } |
| 1048 | |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1049 | auto& outputState = editState(); |
| 1050 | outputState.dirtyRegion.clear(); |
Lloyd Pique | d3d6988 | 2019-02-28 16:03:46 -0800 | [diff] [blame] | 1051 | mRenderSurface->flip(); |
| 1052 | |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1053 | auto frame = presentAndGetFrameFences(); |
| 1054 | |
Lloyd Pique | 7d90ba5 | 2019-08-08 11:57:53 -0700 | [diff] [blame] | 1055 | mRenderSurface->onPresentDisplayCompleted(); |
| 1056 | |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 1057 | for (auto* layer : getOutputLayersOrderedByZ()) { |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1058 | // The layer buffer from the previous frame (if any) is released |
| 1059 | // by HWC only when the release fence from this frame (if any) is |
| 1060 | // signaled. Always get the release fence from HWC first. |
| 1061 | sp<Fence> releaseFence = Fence::NO_FENCE; |
| 1062 | |
| 1063 | if (auto hwcLayer = layer->getHwcLayer()) { |
| 1064 | if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) { |
| 1065 | releaseFence = f->second; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | // If the layer was client composited in the previous frame, we |
| 1070 | // need to merge with the previous client target acquire fence. |
| 1071 | // Since we do not track that, always merge with the current |
| 1072 | // client target acquire fence when it is available, even though |
| 1073 | // this is suboptimal. |
| 1074 | // TODO(b/121291683): Track previous frame client target acquire fence. |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1075 | if (outputState.usesClientComposition) { |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1076 | releaseFence = |
| 1077 | Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence); |
| 1078 | } |
| 1079 | |
| 1080 | layer->getLayerFE().onLayerDisplayed(releaseFence); |
| 1081 | } |
| 1082 | |
| 1083 | // We've got a list of layers needing fences, that are disjoint with |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 1084 | // OutputLayersOrderedByZ. The best we can do is to |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1085 | // supply them with the present fence. |
| 1086 | for (auto& weakLayer : mReleasedLayers) { |
| 1087 | if (auto layer = weakLayer.promote(); layer != nullptr) { |
| 1088 | layer->onLayerDisplayed(frame.presentFence); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | // Clear out the released layers now that we're done with them. |
| 1093 | mReleasedLayers.clear(); |
| 1094 | } |
| 1095 | |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 1096 | void Output::dirtyEntireOutput() { |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1097 | auto& outputState = editState(); |
| 1098 | outputState.dirtyRegion.set(outputState.bounds); |
Lloyd Pique | 32cbe28 | 2018-10-19 13:09:22 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 1101 | void Output::chooseCompositionStrategy() { |
| 1102 | // The base output implementation can only do client composition |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1103 | auto& outputState = editState(); |
| 1104 | outputState.usesClientComposition = true; |
| 1105 | outputState.usesDeviceComposition = false; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 1106 | outputState.reusedClientComposition = false; |
Lloyd Pique | 66d6860 | 2019-02-13 14:23:31 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Lloyd Pique | 688abd4 | 2019-02-15 15:42:24 -0800 | [diff] [blame] | 1109 | bool Output::getSkipColorTransform() const { |
| 1110 | return true; |
| 1111 | } |
| 1112 | |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1113 | compositionengine::Output::FrameFences Output::presentAndGetFrameFences() { |
| 1114 | compositionengine::Output::FrameFences result; |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 1115 | if (getState().usesClientComposition) { |
Lloyd Pique | 35fca9d | 2019-02-13 14:24:11 -0800 | [diff] [blame] | 1116 | result.clientTargetAcquireFence = mRenderSurface->getClientTargetAcquireFence(); |
| 1117 | } |
| 1118 | return result; |
| 1119 | } |
| 1120 | |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 1121 | } // namespace impl |
| 1122 | } // namespace android::compositionengine |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 1123 | |
| 1124 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 1125 | #pragma clang diagnostic pop // ignored "-Wconversion" |