Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 17 | #include <common/trace.h> |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 18 | #include <compositionengine/CompositionRefreshArgs.h> |
| 19 | #include <compositionengine/LayerFE.h> |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 20 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 21 | #include <compositionengine/OutputLayer.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 22 | #include <compositionengine/impl/CompositionEngine.h> |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 23 | #include <compositionengine/impl/Display.h> |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 24 | #include <ui/DisplayMap.h> |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 25 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 26 | #include <renderengine/RenderEngine.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 27 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 28 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 29 | #pragma clang diagnostic push |
| 30 | #pragma clang diagnostic ignored "-Wconversion" |
| 31 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 32 | #include "DisplayHardware/HWComposer.h" |
| 33 | |
Lloyd Pique | 3b5a69e | 2020-01-16 17:51:01 -0800 | [diff] [blame] | 34 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 35 | #pragma clang diagnostic pop // ignored "-Wconversion" |
| 36 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 37 | namespace android::compositionengine { |
| 38 | |
| 39 | CompositionEngine::~CompositionEngine() = default; |
| 40 | |
| 41 | namespace impl { |
| 42 | |
| 43 | std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() { |
| 44 | return std::make_unique<CompositionEngine>(); |
| 45 | } |
| 46 | |
| 47 | CompositionEngine::CompositionEngine() = default; |
| 48 | CompositionEngine::~CompositionEngine() = default; |
| 49 | |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 50 | std::shared_ptr<compositionengine::Display> CompositionEngine::createDisplay( |
Lloyd Pique | a38ea7e | 2019-04-16 18:10:26 -0700 | [diff] [blame] | 51 | const DisplayCreationArgs& args) { |
| 52 | return compositionengine::impl::createDisplay(*this, args); |
Lloyd Pique | 45a165a | 2018-10-19 11:54:47 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 55 | std::unique_ptr<compositionengine::LayerFECompositionState> |
| 56 | CompositionEngine::createLayerFECompositionState() { |
| 57 | return std::make_unique<compositionengine::LayerFECompositionState>(); |
Lloyd Pique | feb73d7 | 2018-12-04 17:23:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 60 | HWComposer& CompositionEngine::getHwComposer() const { |
| 61 | return *mHwComposer.get(); |
| 62 | } |
| 63 | |
| 64 | void CompositionEngine::setHwComposer(std::unique_ptr<HWComposer> hwComposer) { |
| 65 | mHwComposer = std::move(hwComposer); |
| 66 | } |
| 67 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 68 | renderengine::RenderEngine& CompositionEngine::getRenderEngine() const { |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 69 | return *mRenderEngine; |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 72 | void CompositionEngine::setRenderEngine(renderengine::RenderEngine* renderEngine) { |
| 73 | mRenderEngine = renderEngine; |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Patrick Williams | 74c0bf6 | 2022-11-02 23:59:26 +0000 | [diff] [blame] | 76 | TimeStats* CompositionEngine::getTimeStats() const { |
| 77 | return mTimeStats.get(); |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void CompositionEngine::setTimeStats(const std::shared_ptr<TimeStats>& timeStats) { |
| 81 | mTimeStats = timeStats; |
| 82 | } |
| 83 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 84 | bool CompositionEngine::needsAnotherUpdate() const { |
| 85 | return mNeedsAnotherUpdate; |
| 86 | } |
| 87 | |
| 88 | nsecs_t CompositionEngine::getLastFrameRefreshTimestamp() const { |
| 89 | return mRefreshStartTime; |
| 90 | } |
| 91 | |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 92 | namespace { |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 93 | void offloadOutputs(Outputs& outputs) { |
| 94 | if (!FlagManager::getInstance().multithreaded_present() || outputs.size() < 2) { |
| 95 | return; |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 98 | ui::PhysicalDisplayVector<compositionengine::Output*> outputsToOffload; |
| 99 | for (const auto& output : outputs) { |
| 100 | if (!ftl::Optional(output->getDisplayId()).and_then(HalDisplayId::tryCast)) { |
| 101 | // Not HWC-enabled, so it is always client-composited. No need to offload. |
| 102 | continue; |
| 103 | } |
Leon Scroggins III | cbc929d | 2023-12-01 16:21:37 -0500 | [diff] [blame] | 104 | if (!output->getState().isEnabled) { |
| 105 | continue; |
| 106 | } |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 107 | |
| 108 | // Only run present in multiple threads if all HWC-enabled displays |
| 109 | // being refreshed support it. |
| 110 | if (!output->supportsOffloadPresent()) { |
| 111 | return; |
| 112 | } |
| 113 | outputsToOffload.push_back(output.get()); |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 114 | } |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 115 | |
| 116 | if (outputsToOffload.size() < 2) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Leave the last eligible display on the main thread, which will |
| 121 | // allow it to run concurrently without an extra thread hop. |
| 122 | outputsToOffload.pop_back(); |
| 123 | |
| 124 | for (compositionengine::Output* output : outputsToOffload) { |
| 125 | output->offloadPresentNextFrame(); |
| 126 | } |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 127 | } |
| 128 | } // namespace |
| 129 | |
Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 130 | void CompositionEngine::present(CompositionRefreshArgs& args) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 131 | SFTRACE_CALL(); |
Lloyd Pique | c29e4c6 | 2019-03-07 21:48:19 -0800 | [diff] [blame] | 132 | ALOGV(__FUNCTION__); |
| 133 | |
| 134 | preComposition(args); |
| 135 | |
| 136 | { |
| 137 | // latchedLayers is used to track the set of front-end layer state that |
| 138 | // has been latched across all outputs for the prepare step, and is not |
| 139 | // needed for anything else. |
| 140 | LayerFESet latchedLayers; |
| 141 | |
| 142 | for (const auto& output : args.outputs) { |
| 143 | output->prepare(args, latchedLayers); |
| 144 | } |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 147 | // Offloading the HWC call for `present` allows us to simultaneously call it |
| 148 | // on multiple displays. This is desirable because these calls block and can |
| 149 | // be slow. |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 150 | offloadOutputs(args.outputs); |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 151 | |
| 152 | ui::DisplayVector<ftl::Future<std::monostate>> presentFutures; |
Lloyd Pique | 3eb1b21 | 2019-03-07 21:15:40 -0800 | [diff] [blame] | 153 | for (const auto& output : args.outputs) { |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 154 | presentFutures.push_back(output->present(args)); |
| 155 | } |
| 156 | |
| 157 | { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 158 | SFTRACE_NAME("Waiting on HWC"); |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 159 | for (auto& future : presentFutures) { |
| 160 | // TODO(b/185536303): Call ftl::Future::wait() once it exists, since |
| 161 | // we do not need the return value of get(). |
| 162 | future.get(); |
| 163 | } |
Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 164 | } |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 165 | postComposition(args); |
Lloyd Pique | d7b429f | 2019-03-07 21:11:02 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 168 | void CompositionEngine::updateCursorAsync(CompositionRefreshArgs& args) { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 169 | |
| 170 | for (const auto& output : args.outputs) { |
Lloyd Pique | 01c77c1 | 2019-04-17 12:48:32 -0700 | [diff] [blame] | 171 | for (auto* layer : output->getOutputLayersOrderedByZ()) { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 172 | if (layer->isHardwareCursor()) { |
Lloyd Pique | c7b0c75 | 2019-03-07 20:59:59 -0800 | [diff] [blame] | 173 | layer->writeCursorPositionToHWC(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 179 | void CompositionEngine::preComposition(CompositionRefreshArgs& args) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 180 | SFTRACE_CALL(); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 181 | ALOGV(__FUNCTION__); |
| 182 | |
| 183 | bool needsAnotherUpdate = false; |
| 184 | |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 185 | mRefreshStartTime = args.refreshStartTime; |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 186 | |
| 187 | for (auto& layer : args.layers) { |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 188 | if (layer->onPreComposition(args.updatingOutputGeometryThisFrame)) { |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 189 | needsAnotherUpdate = true; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | mNeedsAnotherUpdate = needsAnotherUpdate; |
| 194 | } |
| 195 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 196 | // If a buffer is latched but the layer is not presented, such as when |
| 197 | // obscured by another layer, the previous buffer needs to be released. We find |
| 198 | // these buffers and fire a NO_FENCE to release it. This ensures that all |
| 199 | // promises for buffer releases are fulfilled at the end of composition. |
| 200 | void CompositionEngine::postComposition(CompositionRefreshArgs& args) { |
| 201 | if (FlagManager::getInstance().ce_fence_promise()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 202 | SFTRACE_CALL(); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 203 | ALOGV(__FUNCTION__); |
| 204 | |
| 205 | for (auto& layerFE : args.layers) { |
| 206 | if (layerFE->getReleaseFencePromiseStatus() == |
| 207 | LayerFE::ReleaseFencePromiseStatus::INITIALIZED) { |
| 208 | layerFE->setReleaseFence(Fence::NO_FENCE); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // List of layersWithQueuedFrames does not necessarily overlap with |
| 213 | // list of layers, so those layersWithQueuedFrames also need any |
| 214 | // unfulfilled promises to be resolved for completeness. |
| 215 | for (auto& layerFE : args.layersWithQueuedFrames) { |
| 216 | if (layerFE->getReleaseFencePromiseStatus() == |
| 217 | LayerFE::ReleaseFencePromiseStatus::INITIALIZED) { |
| 218 | layerFE->setReleaseFence(Fence::NO_FENCE); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Vishnu Nair | 0a4fb00 | 2022-08-08 02:40:42 +0000 | [diff] [blame] | 224 | FeatureFlags CompositionEngine::getFeatureFlags() const { |
| 225 | return {}; |
| 226 | } |
| 227 | |
Lloyd Pique | c3cb729 | 2019-05-17 15:25:14 -0700 | [diff] [blame] | 228 | void CompositionEngine::dump(std::string&) const { |
| 229 | // The base class has no state to dump, but derived classes might. |
| 230 | } |
| 231 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 232 | void CompositionEngine::setNeedsAnotherUpdateForTest(bool value) { |
| 233 | mNeedsAnotherUpdate = value; |
| 234 | } |
| 235 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 236 | } // namespace impl |
| 237 | } // namespace android::compositionengine |