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