Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
| 19 | #include "RenderEngineThreaded.h" |
| 20 | |
| 21 | #include <sched.h> |
| 22 | #include <chrono> |
| 23 | #include <future> |
| 24 | |
| 25 | #include <android-base/stringprintf.h> |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 26 | #include <private/gui/SyncFeatures.h> |
Wei Wang | 976a645 | 2021-06-11 15:26:00 -0700 | [diff] [blame] | 27 | #include <processgroup/processgroup.h> |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 28 | #include <utils/Trace.h> |
| 29 | |
| 30 | #include "gl/GLESRenderEngine.h" |
| 31 | |
| 32 | using namespace std::chrono_literals; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace renderengine { |
| 36 | namespace threaded { |
| 37 | |
Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 38 | std::unique_ptr<RenderEngineThreaded> RenderEngineThreaded::create(CreateInstanceFactory factory, |
| 39 | RenderEngineType type) { |
| 40 | return std::make_unique<RenderEngineThreaded>(std::move(factory), type); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 43 | RenderEngineThreaded::RenderEngineThreaded(CreateInstanceFactory factory, RenderEngineType type) |
| 44 | : RenderEngine(type) { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 45 | ATRACE_CALL(); |
| 46 | |
| 47 | std::lock_guard lockThread(mThreadMutex); |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 48 | mThread = std::thread(&RenderEngineThreaded::threadMain, this, factory); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | RenderEngineThreaded::~RenderEngineThreaded() { |
Alec Mouri | 39d9cb7 | 2021-06-10 10:26:15 -0700 | [diff] [blame] | 52 | mRunning = false; |
| 53 | mCondition.notify_one(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 54 | |
| 55 | if (mThread.joinable()) { |
| 56 | mThread.join(); |
| 57 | } |
| 58 | } |
| 59 | |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 60 | status_t RenderEngineThreaded::setSchedFifo(bool enabled) { |
| 61 | static constexpr int kFifoPriority = 2; |
| 62 | static constexpr int kOtherPriority = 0; |
| 63 | |
| 64 | struct sched_param param = {0}; |
| 65 | int sched_policy; |
| 66 | if (enabled) { |
| 67 | sched_policy = SCHED_FIFO; |
| 68 | param.sched_priority = kFifoPriority; |
| 69 | } else { |
| 70 | sched_policy = SCHED_OTHER; |
| 71 | param.sched_priority = kOtherPriority; |
| 72 | } |
| 73 | |
| 74 | if (sched_setscheduler(0, sched_policy, ¶m) != 0) { |
| 75 | return -errno; |
| 76 | } |
| 77 | return NO_ERROR; |
| 78 | } |
| 79 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 80 | // NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations. |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 81 | void RenderEngineThreaded::threadMain(CreateInstanceFactory factory) NO_THREAD_SAFETY_ANALYSIS { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 82 | ATRACE_CALL(); |
| 83 | |
Wei Wang | 976a645 | 2021-06-11 15:26:00 -0700 | [diff] [blame] | 84 | if (!SetTaskProfiles(0, {"SFRenderEnginePolicy"})) { |
| 85 | ALOGW("Failed to set render-engine task profile!"); |
| 86 | } |
| 87 | |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 88 | if (setSchedFifo(true) != NO_ERROR) { |
| 89 | ALOGW("Couldn't set SCHED_FIFO"); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 92 | mRenderEngine = factory(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 93 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 94 | pthread_setname_np(pthread_self(), mThreadName); |
| 95 | |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 96 | { |
Alec Mouri | 39d9cb7 | 2021-06-10 10:26:15 -0700 | [diff] [blame] | 97 | std::scoped_lock lock(mInitializedMutex); |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 98 | mIsInitialized = true; |
| 99 | } |
| 100 | mInitializedCondition.notify_all(); |
| 101 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 102 | while (mRunning) { |
Alec Mouri | 39d9cb7 | 2021-06-10 10:26:15 -0700 | [diff] [blame] | 103 | const auto getNextTask = [this]() -> std::optional<Work> { |
| 104 | std::scoped_lock lock(mThreadMutex); |
| 105 | if (!mFunctionCalls.empty()) { |
| 106 | Work task = mFunctionCalls.front(); |
| 107 | mFunctionCalls.pop(); |
| 108 | return std::make_optional<Work>(task); |
| 109 | } |
| 110 | return std::nullopt; |
| 111 | }; |
| 112 | |
| 113 | const auto task = getNextTask(); |
| 114 | |
| 115 | if (task) { |
| 116 | (*task)(*mRenderEngine); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 117 | } |
Alec Mouri | 39d9cb7 | 2021-06-10 10:26:15 -0700 | [diff] [blame] | 118 | |
| 119 | std::unique_lock<std::mutex> lock(mThreadMutex); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 120 | mCondition.wait(lock, [this]() REQUIRES(mThreadMutex) { |
| 121 | return !mRunning || !mFunctionCalls.empty(); |
| 122 | }); |
| 123 | } |
Derek Sollenberger | 40dcb02 | 2021-04-14 10:28:27 -0400 | [diff] [blame] | 124 | |
| 125 | // we must release the RenderEngine on the thread that created it |
| 126 | mRenderEngine.reset(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 129 | void RenderEngineThreaded::waitUntilInitialized() const { |
| 130 | std::unique_lock<std::mutex> lock(mInitializedMutex); |
| 131 | mInitializedCondition.wait(lock, [=] { return mIsInitialized; }); |
| 132 | } |
| 133 | |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 134 | std::future<void> RenderEngineThreaded::primeCache() { |
| 135 | const auto resultPromise = std::make_shared<std::promise<void>>(); |
| 136 | std::future<void> resultFuture = resultPromise->get_future(); |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 137 | ATRACE_CALL(); |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 138 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 139 | // for the futures. |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 140 | { |
| 141 | std::lock_guard lock(mThreadMutex); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 142 | mFunctionCalls.push([resultPromise](renderengine::RenderEngine& instance) { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 143 | ATRACE_NAME("REThreaded::primeCache"); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 144 | if (setSchedFifo(false) != NO_ERROR) { |
| 145 | ALOGW("Couldn't set SCHED_OTHER for primeCache"); |
| 146 | } |
| 147 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 148 | instance.primeCache(); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 149 | resultPromise->set_value(); |
| 150 | |
| 151 | if (setSchedFifo(true) != NO_ERROR) { |
| 152 | ALOGW("Couldn't set SCHED_FIFO for primeCache"); |
| 153 | } |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 154 | }); |
| 155 | } |
| 156 | mCondition.notify_one(); |
Ady Abraham | fe2a6db | 2021-06-09 15:41:37 -0700 | [diff] [blame] | 157 | |
| 158 | return resultFuture; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void RenderEngineThreaded::dump(std::string& result) { |
| 162 | std::promise<std::string> resultPromise; |
| 163 | std::future<std::string> resultFuture = resultPromise.get_future(); |
| 164 | { |
| 165 | std::lock_guard lock(mThreadMutex); |
| 166 | mFunctionCalls.push([&resultPromise, &result](renderengine::RenderEngine& instance) { |
| 167 | ATRACE_NAME("REThreaded::dump"); |
| 168 | std::string localResult = result; |
| 169 | instance.dump(localResult); |
| 170 | resultPromise.set_value(std::move(localResult)); |
| 171 | }); |
| 172 | } |
| 173 | mCondition.notify_one(); |
| 174 | // Note: This is an rvalue. |
| 175 | result.assign(resultFuture.get()); |
| 176 | } |
| 177 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 178 | void RenderEngineThreaded::genTextures(size_t count, uint32_t* names) { |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 179 | ATRACE_CALL(); |
Leon Scroggins III | 48b5f3c | 2021-12-22 10:05:06 -0500 | [diff] [blame] | 180 | // This is a no-op in SkiaRenderEngine. |
| 181 | if (getRenderEngineType() != RenderEngineType::THREADED) { |
| 182 | return; |
| 183 | } |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 184 | std::promise<void> resultPromise; |
| 185 | std::future<void> resultFuture = resultPromise.get_future(); |
| 186 | { |
| 187 | std::lock_guard lock(mThreadMutex); |
| 188 | mFunctionCalls.push([&resultPromise, count, names](renderengine::RenderEngine& instance) { |
| 189 | ATRACE_NAME("REThreaded::genTextures"); |
| 190 | instance.genTextures(count, names); |
| 191 | resultPromise.set_value(); |
| 192 | }); |
| 193 | } |
| 194 | mCondition.notify_one(); |
| 195 | resultFuture.wait(); |
| 196 | } |
| 197 | |
| 198 | void RenderEngineThreaded::deleteTextures(size_t count, uint32_t const* names) { |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 199 | ATRACE_CALL(); |
Leon Scroggins III | 48b5f3c | 2021-12-22 10:05:06 -0500 | [diff] [blame] | 200 | // This is a no-op in SkiaRenderEngine. |
| 201 | if (getRenderEngineType() != RenderEngineType::THREADED) { |
| 202 | return; |
| 203 | } |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 204 | std::promise<void> resultPromise; |
| 205 | std::future<void> resultFuture = resultPromise.get_future(); |
| 206 | { |
| 207 | std::lock_guard lock(mThreadMutex); |
| 208 | mFunctionCalls.push([&resultPromise, count, &names](renderengine::RenderEngine& instance) { |
| 209 | ATRACE_NAME("REThreaded::deleteTextures"); |
| 210 | instance.deleteTextures(count, names); |
| 211 | resultPromise.set_value(); |
| 212 | }); |
| 213 | } |
| 214 | mCondition.notify_one(); |
| 215 | resultFuture.wait(); |
| 216 | } |
| 217 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 218 | void RenderEngineThreaded::mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, |
| 219 | bool isRenderable) { |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 220 | ATRACE_CALL(); |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 221 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 222 | // for the futures. |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 223 | { |
| 224 | std::lock_guard lock(mThreadMutex); |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 225 | mFunctionCalls.push([=](renderengine::RenderEngine& instance) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 226 | ATRACE_NAME("REThreaded::mapExternalTextureBuffer"); |
| 227 | instance.mapExternalTextureBuffer(buffer, isRenderable); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 228 | }); |
| 229 | } |
| 230 | mCondition.notify_one(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 233 | void RenderEngineThreaded::unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) { |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 234 | ATRACE_CALL(); |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 235 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 236 | // for the futures. |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 237 | { |
| 238 | std::lock_guard lock(mThreadMutex); |
Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 239 | mFunctionCalls.push([=](renderengine::RenderEngine& instance) { |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 240 | ATRACE_NAME("REThreaded::unmapExternalTextureBuffer"); |
| 241 | instance.unmapExternalTextureBuffer(buffer); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 242 | }); |
| 243 | } |
| 244 | mCondition.notify_one(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 247 | size_t RenderEngineThreaded::getMaxTextureSize() const { |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 248 | waitUntilInitialized(); |
| 249 | return mRenderEngine->getMaxTextureSize(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | size_t RenderEngineThreaded::getMaxViewportDims() const { |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 253 | waitUntilInitialized(); |
| 254 | return mRenderEngine->getMaxViewportDims(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 257 | bool RenderEngineThreaded::supportsProtectedContent() const { |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 258 | waitUntilInitialized(); |
| 259 | return mRenderEngine->supportsProtectedContent(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 262 | void RenderEngineThreaded::cleanupPostRender() { |
| 263 | if (canSkipPostRenderCleanup()) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 268 | // for the futures. |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 269 | { |
| 270 | std::lock_guard lock(mThreadMutex); |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 271 | mFunctionCalls.push([=](renderengine::RenderEngine& instance) { |
Leon Scroggins III | 1e34a70 | 2021-08-24 15:13:32 -0400 | [diff] [blame] | 272 | ATRACE_NAME("REThreaded::cleanupPostRender"); |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 273 | instance.cleanupPostRender(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 274 | }); |
| 275 | } |
| 276 | mCondition.notify_one(); |
Derek Sollenberger | d3f6065 | 2021-06-11 15:34:36 -0400 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | bool RenderEngineThreaded::canSkipPostRenderCleanup() const { |
| 280 | waitUntilInitialized(); |
| 281 | return mRenderEngine->canSkipPostRenderCleanup(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 284 | void RenderEngineThreaded::drawLayersInternal( |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 285 | const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 286 | const DisplaySettings& display, const std::vector<LayerSettings>& layers, |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 287 | const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache, |
| 288 | base::unique_fd&& bufferFence) { |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 289 | resultPromise->set_value(Fence::NO_FENCE); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 293 | ftl::Future<FenceResult> RenderEngineThreaded::drawLayers( |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 294 | const DisplaySettings& display, const std::vector<LayerSettings>& layers, |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 295 | const std::shared_ptr<ExternalTexture>& buffer, const bool useFramebufferCache, |
| 296 | base::unique_fd&& bufferFence) { |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 297 | ATRACE_CALL(); |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 298 | const auto resultPromise = std::make_shared<std::promise<FenceResult>>(); |
| 299 | std::future<FenceResult> resultFuture = resultPromise->get_future(); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 300 | int fd = bufferFence.release(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 301 | { |
| 302 | std::lock_guard lock(mThreadMutex); |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 303 | mFunctionCalls.push([resultPromise, display, layers, buffer, useFramebufferCache, |
| 304 | fd](renderengine::RenderEngine& instance) { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 305 | ATRACE_NAME("REThreaded::drawLayers"); |
Patrick Williams | 8aed5d2 | 2022-10-31 22:18:10 +0000 | [diff] [blame] | 306 | instance.updateProtectedContext(layers, buffer); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 307 | instance.drawLayersInternal(std::move(resultPromise), display, layers, buffer, |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 308 | useFramebufferCache, base::unique_fd(fd)); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 309 | }); |
| 310 | } |
| 311 | mCondition.notify_one(); |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 312 | return resultFuture; |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 315 | void RenderEngineThreaded::cleanFramebufferCache() { |
Ady Abraham | b57e8c2 | 2021-05-07 15:17:29 -0700 | [diff] [blame] | 316 | ATRACE_CALL(); |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 317 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 318 | // for the futures. |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 319 | { |
| 320 | std::lock_guard lock(mThreadMutex); |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 321 | mFunctionCalls.push([](renderengine::RenderEngine& instance) { |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 322 | ATRACE_NAME("REThreaded::cleanFramebufferCache"); |
| 323 | instance.cleanFramebufferCache(); |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 324 | }); |
| 325 | } |
| 326 | mCondition.notify_one(); |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 329 | int RenderEngineThreaded::getContextPriority() { |
| 330 | std::promise<int> resultPromise; |
| 331 | std::future<int> resultFuture = resultPromise.get_future(); |
| 332 | { |
| 333 | std::lock_guard lock(mThreadMutex); |
| 334 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 335 | ATRACE_NAME("REThreaded::getContextPriority"); |
| 336 | int priority = instance.getContextPriority(); |
| 337 | resultPromise.set_value(priority); |
| 338 | }); |
| 339 | } |
| 340 | mCondition.notify_one(); |
| 341 | return resultFuture.get(); |
| 342 | } |
| 343 | |
Derek Sollenberger | b399837 | 2021-02-16 15:16:56 -0500 | [diff] [blame] | 344 | bool RenderEngineThreaded::supportsBackgroundBlur() { |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 345 | waitUntilInitialized(); |
| 346 | return mRenderEngine->supportsBackgroundBlur(); |
Derek Sollenberger | b399837 | 2021-02-16 15:16:56 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 349 | void RenderEngineThreaded::onActiveDisplaySizeChanged(ui::Size size) { |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 350 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 351 | // for the futures. |
Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 352 | { |
| 353 | std::lock_guard lock(mThreadMutex); |
Derek Sollenberger | 4bea01e | 2021-04-09 13:59:37 -0400 | [diff] [blame] | 354 | mFunctionCalls.push([size](renderengine::RenderEngine& instance) { |
Ady Abraham | ed3290f | 2021-05-17 15:12:14 -0700 | [diff] [blame] | 355 | ATRACE_NAME("REThreaded::onActiveDisplaySizeChanged"); |
| 356 | instance.onActiveDisplaySizeChanged(size); |
Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 357 | }); |
| 358 | } |
| 359 | mCondition.notify_one(); |
Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Matt Buckley | ef51fba | 2021-10-12 19:30:12 +0000 | [diff] [blame] | 362 | std::optional<pid_t> RenderEngineThreaded::getRenderEngineTid() const { |
| 363 | std::promise<pid_t> tidPromise; |
| 364 | std::future<pid_t> tidFuture = tidPromise.get_future(); |
| 365 | { |
| 366 | std::lock_guard lock(mThreadMutex); |
| 367 | mFunctionCalls.push([&tidPromise](renderengine::RenderEngine& instance) { |
| 368 | tidPromise.set_value(gettid()); |
| 369 | }); |
| 370 | } |
| 371 | |
| 372 | mCondition.notify_one(); |
| 373 | return std::make_optional(tidFuture.get()); |
| 374 | } |
| 375 | |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 376 | void RenderEngineThreaded::setEnableTracing(bool tracingEnabled) { |
| 377 | // This function is designed so it can run asynchronously, so we do not need to wait |
| 378 | // for the futures. |
| 379 | { |
| 380 | std::lock_guard lock(mThreadMutex); |
| 381 | mFunctionCalls.push([tracingEnabled](renderengine::RenderEngine& instance) { |
| 382 | ATRACE_NAME("REThreaded::setEnableTracing"); |
| 383 | instance.setEnableTracing(tracingEnabled); |
| 384 | }); |
| 385 | } |
| 386 | mCondition.notify_one(); |
| 387 | } |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 388 | } // namespace threaded |
| 389 | } // namespace renderengine |
Alec Mouri | 368e158 | 2020-08-13 10:14:29 -0700 | [diff] [blame] | 390 | } // namespace android |