blob: 748d87bdc3ed5f15ecf3504b0b0262a6ddb81237 [file] [log] [blame]
Lloyd Pique70d91362018-10-18 16:02:55 -07001/*
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
Lloyd Piqueab039b52019-02-13 14:22:42 -080017#include <compositionengine/CompositionRefreshArgs.h>
18#include <compositionengine/LayerFE.h>
Lloyd Piquede196652020-01-22 17:29:58 -080019#include <compositionengine/LayerFECompositionState.h>
Lloyd Piquec7b0c752019-03-07 20:59:59 -080020#include <compositionengine/OutputLayer.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070021#include <compositionengine/impl/CompositionEngine.h>
Lloyd Pique45a165a2018-10-19 11:54:47 -070022#include <compositionengine/impl/Display.h>
Leon Scroggins III2f60d732022-09-12 14:42:38 -040023#include <ui/DisplayMap.h>
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080024
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070025#include <renderengine/RenderEngine.h>
Lloyd Piqueab039b52019-02-13 14:22:42 -080026#include <utils/Trace.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070027
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080028// TODO(b/129481165): remove the #pragma below and fix conversion issues
29#pragma clang diagnostic push
30#pragma clang diagnostic ignored "-Wconversion"
31
Lloyd Pique441d5042018-10-18 16:49:51 -070032#include "DisplayHardware/HWComposer.h"
33
Lloyd Pique3b5a69e2020-01-16 17:51:01 -080034// TODO(b/129481165): remove the #pragma below and fix conversion issues
35#pragma clang diagnostic pop // ignored "-Wconversion"
36
Lloyd Pique70d91362018-10-18 16:02:55 -070037namespace android::compositionengine {
38
39CompositionEngine::~CompositionEngine() = default;
40
41namespace impl {
42
43std::unique_ptr<compositionengine::CompositionEngine> createCompositionEngine() {
44 return std::make_unique<CompositionEngine>();
45}
46
47CompositionEngine::CompositionEngine() = default;
48CompositionEngine::~CompositionEngine() = default;
49
Lloyd Pique45a165a2018-10-19 11:54:47 -070050std::shared_ptr<compositionengine::Display> CompositionEngine::createDisplay(
Lloyd Piquea38ea7e2019-04-16 18:10:26 -070051 const DisplayCreationArgs& args) {
52 return compositionengine::impl::createDisplay(*this, args);
Lloyd Pique45a165a2018-10-19 11:54:47 -070053}
54
Lloyd Piquede196652020-01-22 17:29:58 -080055std::unique_ptr<compositionengine::LayerFECompositionState>
56CompositionEngine::createLayerFECompositionState() {
57 return std::make_unique<compositionengine::LayerFECompositionState>();
Lloyd Piquefeb73d72018-12-04 17:23:44 -080058}
59
Lloyd Pique441d5042018-10-18 16:49:51 -070060HWComposer& CompositionEngine::getHwComposer() const {
61 return *mHwComposer.get();
62}
63
64void CompositionEngine::setHwComposer(std::unique_ptr<HWComposer> hwComposer) {
65 mHwComposer = std::move(hwComposer);
66}
67
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070068renderengine::RenderEngine& CompositionEngine::getRenderEngine() const {
Patrick Williams0a525a42022-10-26 20:20:50 +000069 return *mRenderEngine;
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070070}
71
Patrick Williams0a525a42022-10-26 20:20:50 +000072void CompositionEngine::setRenderEngine(renderengine::RenderEngine* renderEngine) {
73 mRenderEngine = renderEngine;
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070074}
75
Patrick Williams74c0bf62022-11-02 23:59:26 +000076TimeStats* CompositionEngine::getTimeStats() const {
77 return mTimeStats.get();
Alec Mourie4034bb2019-11-19 12:45:54 -080078}
79
80void CompositionEngine::setTimeStats(const std::shared_ptr<TimeStats>& timeStats) {
81 mTimeStats = timeStats;
82}
83
Lloyd Piqueab039b52019-02-13 14:22:42 -080084bool CompositionEngine::needsAnotherUpdate() const {
85 return mNeedsAnotherUpdate;
86}
87
88nsecs_t CompositionEngine::getLastFrameRefreshTimestamp() const {
89 return mRefreshStartTime;
90}
91
Leon Scroggins III2f60d732022-09-12 14:42:38 -040092namespace {
93int numDisplaysWithOffloadPresentSupport(const CompositionRefreshArgs& args) {
94 if (!FlagManager::getInstance().multithreaded_present() || args.outputs.size() < 2) {
95 return 0;
96 }
97
98 int numEligibleDisplays = 0;
99 // Only run present in multiple threads if all HWC-enabled displays
100 // being refreshed support it.
101 if (!std::all_of(args.outputs.begin(), args.outputs.end(),
102 [&numEligibleDisplays](const auto& output) {
103 if (!ftl::Optional(output->getDisplayId())
104 .and_then(HalDisplayId::tryCast)) {
105 // Not HWC-enabled, so it is always
106 // client-composited.
107 return true;
108 }
109 const bool support = output->supportsOffloadPresent();
110 numEligibleDisplays += static_cast<int>(support);
111 return support;
112 })) {
113 return 0;
114 }
115 return numEligibleDisplays;
116}
117} // namespace
118
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800119void CompositionEngine::present(CompositionRefreshArgs& args) {
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800120 ATRACE_CALL();
121 ALOGV(__FUNCTION__);
122
123 preComposition(args);
124
125 {
126 // latchedLayers is used to track the set of front-end layer state that
127 // has been latched across all outputs for the prepare step, and is not
128 // needed for anything else.
129 LayerFESet latchedLayers;
130
131 for (const auto& output : args.outputs) {
132 output->prepare(args, latchedLayers);
133 }
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800134 }
135
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400136 // Offloading the HWC call for `present` allows us to simultaneously call it
137 // on multiple displays. This is desirable because these calls block and can
138 // be slow.
139 if (const int numEligibleDisplays = numDisplaysWithOffloadPresentSupport(args);
140 numEligibleDisplays > 1) {
141 // Leave the last eligible display on the main thread, which will
142 // allow it to run concurrently without an extra thread hop.
143 int numToOffload = numEligibleDisplays - 1;
144 for (auto& output : args.outputs) {
145 if (output->supportsOffloadPresent()) {
146 output->offloadPresentNextFrame();
147 if (--numToOffload == 0) {
148 break;
149 }
150 }
151 }
152 }
153
154 ui::DisplayVector<ftl::Future<std::monostate>> presentFutures;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800155 for (const auto& output : args.outputs) {
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400156 presentFutures.push_back(output->present(args));
157 }
158
159 {
160 ATRACE_NAME("Waiting on HWC");
161 for (auto& future : presentFutures) {
162 // TODO(b/185536303): Call ftl::Future::wait() once it exists, since
163 // we do not need the return value of get().
164 future.get();
165 }
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800166 }
167}
168
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800169void CompositionEngine::updateCursorAsync(CompositionRefreshArgs& args) {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800170
171 for (const auto& output : args.outputs) {
Lloyd Pique01c77c12019-04-17 12:48:32 -0700172 for (auto* layer : output->getOutputLayersOrderedByZ()) {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800173 if (layer->isHardwareCursor()) {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800174 layer->writeCursorPositionToHWC();
175 }
176 }
177 }
178}
179
Lloyd Piqueab039b52019-02-13 14:22:42 -0800180void CompositionEngine::preComposition(CompositionRefreshArgs& args) {
181 ATRACE_CALL();
182 ALOGV(__FUNCTION__);
183
184 bool needsAnotherUpdate = false;
185
186 mRefreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
187
188 for (auto& layer : args.layers) {
Vishnu Nairba354102022-08-01 00:14:18 +0000189 if (layer->onPreComposition(mRefreshStartTime, args.updatingOutputGeometryThisFrame)) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800190 needsAnotherUpdate = true;
191 }
192 }
193
194 mNeedsAnotherUpdate = needsAnotherUpdate;
195}
196
Vishnu Nair0a4fb002022-08-08 02:40:42 +0000197FeatureFlags CompositionEngine::getFeatureFlags() const {
198 return {};
199}
200
Lloyd Piquec3cb7292019-05-17 15:25:14 -0700201void CompositionEngine::dump(std::string&) const {
202 // The base class has no state to dump, but derived classes might.
203}
204
Lloyd Piqueab039b52019-02-13 14:22:42 -0800205void CompositionEngine::setNeedsAnotherUpdateForTest(bool value) {
206 mNeedsAnotherUpdate = value;
207}
208
Lloyd Pique70d91362018-10-18 16:02:55 -0700209} // namespace impl
210} // namespace android::compositionengine