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