blob: c9f6296b49f3616f524f00ea45b42c533381e766 [file] [log] [blame]
Ana Krulec9bc9dc62020-02-26 12:16:40 -08001/*
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 Krulec15f7cf32020-05-12 11:57:42 -070026#include <private/gui/SyncFeatures.h>
Ana Krulec9bc9dc62020-02-26 12:16:40 -080027#include <utils/Trace.h>
28
29#include "gl/GLESRenderEngine.h"
30
31using namespace std::chrono_literals;
32
33namespace android {
34namespace renderengine {
35namespace threaded {
36
Alec Mouri0d995102021-02-24 16:53:38 -080037std::unique_ptr<RenderEngineThreaded> RenderEngineThreaded::create(CreateInstanceFactory factory,
38 RenderEngineType type) {
39 return std::make_unique<RenderEngineThreaded>(std::move(factory), type);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080040}
41
Alec Mouri0d995102021-02-24 16:53:38 -080042RenderEngineThreaded::RenderEngineThreaded(CreateInstanceFactory factory, RenderEngineType type)
43 : RenderEngine(type) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -080044 ATRACE_CALL();
45
46 std::lock_guard lockThread(mThreadMutex);
Ana Krulec15f7cf32020-05-12 11:57:42 -070047 mThread = std::thread(&RenderEngineThreaded::threadMain, this, factory);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080048}
49
50RenderEngineThreaded::~RenderEngineThreaded() {
51 {
52 std::lock_guard lock(mThreadMutex);
53 mRunning = false;
54 mCondition.notify_one();
55 }
56
57 if (mThread.joinable()) {
58 mThread.join();
59 }
60}
61
62// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
Ana Krulec15f7cf32020-05-12 11:57:42 -070063void RenderEngineThreaded::threadMain(CreateInstanceFactory factory) NO_THREAD_SAFETY_ANALYSIS {
Ana Krulec9bc9dc62020-02-26 12:16:40 -080064 ATRACE_CALL();
65
66 struct sched_param param = {0};
67 param.sched_priority = 2;
68 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
69 ALOGE("Couldn't set SCHED_FIFO");
70 }
71
Ana Krulec15f7cf32020-05-12 11:57:42 -070072 mRenderEngine = factory();
Ana Krulec9bc9dc62020-02-26 12:16:40 -080073
74 std::unique_lock<std::mutex> lock(mThreadMutex);
75 pthread_setname_np(pthread_self(), mThreadName);
76
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040077 {
78 std::unique_lock<std::mutex> lock(mInitializedMutex);
79 mIsInitialized = true;
80 }
81 mInitializedCondition.notify_all();
82
Ana Krulec9bc9dc62020-02-26 12:16:40 -080083 while (mRunning) {
84 if (!mFunctionCalls.empty()) {
85 auto task = mFunctionCalls.front();
86 mFunctionCalls.pop();
87 task(*mRenderEngine);
88 }
89 mCondition.wait(lock, [this]() REQUIRES(mThreadMutex) {
90 return !mRunning || !mFunctionCalls.empty();
91 });
92 }
93}
94
Derek Sollenberger4bea01e2021-04-09 13:59:37 -040095void RenderEngineThreaded::waitUntilInitialized() const {
96 std::unique_lock<std::mutex> lock(mInitializedMutex);
97 mInitializedCondition.wait(lock, [=] { return mIsInitialized; });
98}
99
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500100void RenderEngineThreaded::primeCache() {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400101 // This function is designed so it can run asynchronously, so we do not need to wait
102 // for the futures.
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800103 {
104 std::lock_guard lock(mThreadMutex);
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400105 mFunctionCalls.push([](renderengine::RenderEngine& instance) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800106 ATRACE_NAME("REThreaded::primeCache");
107 instance.primeCache();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800108 });
109 }
110 mCondition.notify_one();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800111}
112
113void RenderEngineThreaded::dump(std::string& result) {
114 std::promise<std::string> resultPromise;
115 std::future<std::string> resultFuture = resultPromise.get_future();
116 {
117 std::lock_guard lock(mThreadMutex);
118 mFunctionCalls.push([&resultPromise, &result](renderengine::RenderEngine& instance) {
119 ATRACE_NAME("REThreaded::dump");
120 std::string localResult = result;
121 instance.dump(localResult);
122 resultPromise.set_value(std::move(localResult));
123 });
124 }
125 mCondition.notify_one();
126 // Note: This is an rvalue.
127 result.assign(resultFuture.get());
128}
129
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800130void RenderEngineThreaded::genTextures(size_t count, uint32_t* names) {
131 std::promise<void> resultPromise;
132 std::future<void> resultFuture = resultPromise.get_future();
133 {
134 std::lock_guard lock(mThreadMutex);
135 mFunctionCalls.push([&resultPromise, count, names](renderengine::RenderEngine& instance) {
136 ATRACE_NAME("REThreaded::genTextures");
137 instance.genTextures(count, names);
138 resultPromise.set_value();
139 });
140 }
141 mCondition.notify_one();
142 resultFuture.wait();
143}
144
145void RenderEngineThreaded::deleteTextures(size_t count, uint32_t const* names) {
146 std::promise<void> resultPromise;
147 std::future<void> resultFuture = resultPromise.get_future();
148 {
149 std::lock_guard lock(mThreadMutex);
150 mFunctionCalls.push([&resultPromise, count, &names](renderengine::RenderEngine& instance) {
151 ATRACE_NAME("REThreaded::deleteTextures");
152 instance.deleteTextures(count, names);
153 resultPromise.set_value();
154 });
155 }
156 mCondition.notify_one();
157 resultFuture.wait();
158}
159
Alec Mouri2daef3c2021-04-02 16:29:27 -0700160void RenderEngineThreaded::mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer,
161 bool isRenderable) {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800162 // This function is designed so it can run asynchronously, so we do not need to wait
163 // for the futures.
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800164 {
165 std::lock_guard lock(mThreadMutex);
Ana Krulecdfec8f52021-01-13 12:51:47 -0800166 mFunctionCalls.push([=](renderengine::RenderEngine& instance) {
Alec Mouri2daef3c2021-04-02 16:29:27 -0700167 ATRACE_NAME("REThreaded::mapExternalTextureBuffer");
168 instance.mapExternalTextureBuffer(buffer, isRenderable);
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800169 });
170 }
171 mCondition.notify_one();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800172}
173
Alec Mouri2daef3c2021-04-02 16:29:27 -0700174void RenderEngineThreaded::unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800175 // This function is designed so it can run asynchronously, so we do not need to wait
176 // for the futures.
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800177 {
178 std::lock_guard lock(mThreadMutex);
Ana Krulecdfec8f52021-01-13 12:51:47 -0800179 mFunctionCalls.push([=](renderengine::RenderEngine& instance) {
Alec Mouri2daef3c2021-04-02 16:29:27 -0700180 ATRACE_NAME("REThreaded::unmapExternalTextureBuffer");
181 instance.unmapExternalTextureBuffer(buffer);
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800182 });
183 }
184 mCondition.notify_one();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800185}
186
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800187size_t RenderEngineThreaded::getMaxTextureSize() const {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400188 waitUntilInitialized();
189 return mRenderEngine->getMaxTextureSize();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800190}
191
192size_t RenderEngineThreaded::getMaxViewportDims() const {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400193 waitUntilInitialized();
194 return mRenderEngine->getMaxViewportDims();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800195}
196
197bool RenderEngineThreaded::isProtected() const {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400198 waitUntilInitialized();
199 // ensure that useProtectedContext is not currently being changed by some
200 // other thread.
201 std::lock_guard lock(mThreadMutex);
202 return mRenderEngine->isProtected();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800203}
204
205bool RenderEngineThreaded::supportsProtectedContent() const {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400206 waitUntilInitialized();
207 return mRenderEngine->supportsProtectedContent();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800208}
209
210bool RenderEngineThreaded::useProtectedContext(bool useProtectedContext) {
211 std::promise<bool> resultPromise;
212 std::future<bool> resultFuture = resultPromise.get_future();
213 {
214 std::lock_guard lock(mThreadMutex);
215 mFunctionCalls.push(
216 [&resultPromise, useProtectedContext](renderengine::RenderEngine& instance) {
217 ATRACE_NAME("REThreaded::useProtectedContext");
218 bool returnValue = instance.useProtectedContext(useProtectedContext);
219 resultPromise.set_value(returnValue);
220 });
221 }
222 mCondition.notify_one();
223 return resultFuture.get();
224}
225
Alec Mouri368e1582020-08-13 10:14:29 -0700226bool RenderEngineThreaded::cleanupPostRender(CleanupMode mode) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800227 std::promise<bool> resultPromise;
228 std::future<bool> resultFuture = resultPromise.get_future();
229 {
230 std::lock_guard lock(mThreadMutex);
Alec Mouri368e1582020-08-13 10:14:29 -0700231 mFunctionCalls.push([&resultPromise, mode](renderengine::RenderEngine& instance) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800232 ATRACE_NAME("REThreaded::cleanupPostRender");
Alec Mouri368e1582020-08-13 10:14:29 -0700233 bool returnValue = instance.cleanupPostRender(mode);
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800234 resultPromise.set_value(returnValue);
235 });
236 }
237 mCondition.notify_one();
238 return resultFuture.get();
239}
240
241status_t RenderEngineThreaded::drawLayers(const DisplaySettings& display,
242 const std::vector<const LayerSettings*>& layers,
Alec Mouri2daef3c2021-04-02 16:29:27 -0700243 const std::shared_ptr<ExternalTexture>& buffer,
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800244 const bool useFramebufferCache,
245 base::unique_fd&& bufferFence,
246 base::unique_fd* drawFence) {
247 std::promise<status_t> resultPromise;
248 std::future<status_t> resultFuture = resultPromise.get_future();
249 {
250 std::lock_guard lock(mThreadMutex);
251 mFunctionCalls.push([&resultPromise, &display, &layers, &buffer, useFramebufferCache,
252 &bufferFence, &drawFence](renderengine::RenderEngine& instance) {
253 ATRACE_NAME("REThreaded::drawLayers");
254 status_t status = instance.drawLayers(display, layers, buffer, useFramebufferCache,
255 std::move(bufferFence), drawFence);
256 resultPromise.set_value(status);
257 });
258 }
259 mCondition.notify_one();
260 return resultFuture.get();
261}
262
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200263void RenderEngineThreaded::cleanFramebufferCache() {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400264 // This function is designed so it can run asynchronously, so we do not need to wait
265 // for the futures.
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200266 {
267 std::lock_guard lock(mThreadMutex);
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400268 mFunctionCalls.push([](renderengine::RenderEngine& instance) {
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200269 ATRACE_NAME("REThreaded::cleanFramebufferCache");
270 instance.cleanFramebufferCache();
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200271 });
272 }
273 mCondition.notify_one();
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200274}
275
Alec Mourid6f09462020-12-07 11:18:17 -0800276int RenderEngineThreaded::getContextPriority() {
277 std::promise<int> resultPromise;
278 std::future<int> resultFuture = resultPromise.get_future();
279 {
280 std::lock_guard lock(mThreadMutex);
281 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
282 ATRACE_NAME("REThreaded::getContextPriority");
283 int priority = instance.getContextPriority();
284 resultPromise.set_value(priority);
285 });
286 }
287 mCondition.notify_one();
288 return resultFuture.get();
289}
290
Derek Sollenbergerb3998372021-02-16 15:16:56 -0500291bool RenderEngineThreaded::supportsBackgroundBlur() {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400292 waitUntilInitialized();
293 return mRenderEngine->supportsBackgroundBlur();
Derek Sollenbergerb3998372021-02-16 15:16:56 -0500294}
295
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400296void RenderEngineThreaded::onPrimaryDisplaySizeChanged(ui::Size size) {
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400297 // This function is designed so it can run asynchronously, so we do not need to wait
298 // for the futures.
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400299 {
300 std::lock_guard lock(mThreadMutex);
Derek Sollenberger4bea01e2021-04-09 13:59:37 -0400301 mFunctionCalls.push([size](renderengine::RenderEngine& instance) {
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400302 ATRACE_NAME("REThreaded::onPrimaryDisplaySizeChanged");
303 instance.onPrimaryDisplaySizeChanged(size);
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400304 });
305 }
306 mCondition.notify_one();
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400307}
308
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800309} // namespace threaded
310} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -0700311} // namespace android