blob: 3b97f5659ed27620f9d19eb8ffff69537b473d6d [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
Ana Krulec15f7cf32020-05-12 11:57:42 -070037std::unique_ptr<RenderEngineThreaded> RenderEngineThreaded::create(CreateInstanceFactory factory) {
38 return std::make_unique<RenderEngineThreaded>(std::move(factory));
Ana Krulec9bc9dc62020-02-26 12:16:40 -080039}
40
Ana Krulec15f7cf32020-05-12 11:57:42 -070041RenderEngineThreaded::RenderEngineThreaded(CreateInstanceFactory factory) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -080042 ATRACE_CALL();
43
44 std::lock_guard lockThread(mThreadMutex);
Ana Krulec15f7cf32020-05-12 11:57:42 -070045 mThread = std::thread(&RenderEngineThreaded::threadMain, this, factory);
Ana Krulec9bc9dc62020-02-26 12:16:40 -080046}
47
48RenderEngineThreaded::~RenderEngineThreaded() {
49 {
50 std::lock_guard lock(mThreadMutex);
51 mRunning = false;
52 mCondition.notify_one();
53 }
54
55 if (mThread.joinable()) {
56 mThread.join();
57 }
58}
59
60// NO_THREAD_SAFETY_ANALYSIS is because std::unique_lock presently lacks thread safety annotations.
Ana Krulec15f7cf32020-05-12 11:57:42 -070061void RenderEngineThreaded::threadMain(CreateInstanceFactory factory) NO_THREAD_SAFETY_ANALYSIS {
Ana Krulec9bc9dc62020-02-26 12:16:40 -080062 ATRACE_CALL();
63
64 struct sched_param param = {0};
65 param.sched_priority = 2;
66 if (sched_setscheduler(0, SCHED_FIFO, &param) != 0) {
67 ALOGE("Couldn't set SCHED_FIFO");
68 }
69
Ana Krulec15f7cf32020-05-12 11:57:42 -070070 mRenderEngine = factory();
Ana Krulec9bc9dc62020-02-26 12:16:40 -080071
72 std::unique_lock<std::mutex> lock(mThreadMutex);
73 pthread_setname_np(pthread_self(), mThreadName);
74
75 while (mRunning) {
76 if (!mFunctionCalls.empty()) {
77 auto task = mFunctionCalls.front();
78 mFunctionCalls.pop();
79 task(*mRenderEngine);
80 }
81 mCondition.wait(lock, [this]() REQUIRES(mThreadMutex) {
82 return !mRunning || !mFunctionCalls.empty();
83 });
84 }
85}
86
87void RenderEngineThreaded::primeCache() const {
88 std::promise<void> resultPromise;
89 std::future<void> resultFuture = resultPromise.get_future();
90 {
91 std::lock_guard lock(mThreadMutex);
92 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
93 ATRACE_NAME("REThreaded::primeCache");
94 instance.primeCache();
95 resultPromise.set_value();
96 });
97 }
98 mCondition.notify_one();
99 resultFuture.wait();
100}
101
102void RenderEngineThreaded::dump(std::string& result) {
103 std::promise<std::string> resultPromise;
104 std::future<std::string> resultFuture = resultPromise.get_future();
105 {
106 std::lock_guard lock(mThreadMutex);
107 mFunctionCalls.push([&resultPromise, &result](renderengine::RenderEngine& instance) {
108 ATRACE_NAME("REThreaded::dump");
109 std::string localResult = result;
110 instance.dump(localResult);
111 resultPromise.set_value(std::move(localResult));
112 });
113 }
114 mCondition.notify_one();
115 // Note: This is an rvalue.
116 result.assign(resultFuture.get());
117}
118
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800119void RenderEngineThreaded::genTextures(size_t count, uint32_t* names) {
120 std::promise<void> resultPromise;
121 std::future<void> resultFuture = resultPromise.get_future();
122 {
123 std::lock_guard lock(mThreadMutex);
124 mFunctionCalls.push([&resultPromise, count, names](renderengine::RenderEngine& instance) {
125 ATRACE_NAME("REThreaded::genTextures");
126 instance.genTextures(count, names);
127 resultPromise.set_value();
128 });
129 }
130 mCondition.notify_one();
131 resultFuture.wait();
132}
133
134void RenderEngineThreaded::deleteTextures(size_t count, uint32_t const* names) {
135 std::promise<void> resultPromise;
136 std::future<void> resultFuture = resultPromise.get_future();
137 {
138 std::lock_guard lock(mThreadMutex);
139 mFunctionCalls.push([&resultPromise, count, &names](renderengine::RenderEngine& instance) {
140 ATRACE_NAME("REThreaded::deleteTextures");
141 instance.deleteTextures(count, names);
142 resultPromise.set_value();
143 });
144 }
145 mCondition.notify_one();
146 resultFuture.wait();
147}
148
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800149void RenderEngineThreaded::cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800150 // This function is designed so it can run asynchronously, so we do not need to wait
151 // for the futures.
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800152 {
153 std::lock_guard lock(mThreadMutex);
Ana Krulecdfec8f52021-01-13 12:51:47 -0800154 mFunctionCalls.push([=](renderengine::RenderEngine& instance) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800155 ATRACE_NAME("REThreaded::cacheExternalTextureBuffer");
156 instance.cacheExternalTextureBuffer(buffer);
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800157 });
158 }
159 mCondition.notify_one();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800160}
161
162void RenderEngineThreaded::unbindExternalTextureBuffer(uint64_t bufferId) {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800163 // This function is designed so it can run asynchronously, so we do not need to wait
164 // for the futures.
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800165 {
166 std::lock_guard lock(mThreadMutex);
Ana Krulecdfec8f52021-01-13 12:51:47 -0800167 mFunctionCalls.push([=](renderengine::RenderEngine& instance) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800168 ATRACE_NAME("REThreaded::unbindExternalTextureBuffer");
169 instance.unbindExternalTextureBuffer(bufferId);
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800170 });
171 }
172 mCondition.notify_one();
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800173}
174
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800175size_t RenderEngineThreaded::getMaxTextureSize() const {
176 std::promise<size_t> resultPromise;
177 std::future<size_t> resultFuture = resultPromise.get_future();
178 {
179 std::lock_guard lock(mThreadMutex);
180 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
181 ATRACE_NAME("REThreaded::getMaxTextureSize");
182 size_t size = instance.getMaxTextureSize();
183 resultPromise.set_value(size);
184 });
185 }
186 mCondition.notify_one();
187 return resultFuture.get();
188}
189
190size_t RenderEngineThreaded::getMaxViewportDims() const {
191 std::promise<size_t> resultPromise;
192 std::future<size_t> resultFuture = resultPromise.get_future();
193 {
194 std::lock_guard lock(mThreadMutex);
195 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
196 ATRACE_NAME("REThreaded::getMaxViewportDims");
197 size_t size = instance.getMaxViewportDims();
198 resultPromise.set_value(size);
199 });
200 }
201 mCondition.notify_one();
202 return resultFuture.get();
203}
204
205bool RenderEngineThreaded::isProtected() const {
206 std::promise<bool> resultPromise;
207 std::future<bool> resultFuture = resultPromise.get_future();
208 {
209 std::lock_guard lock(mThreadMutex);
210 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
211 ATRACE_NAME("REThreaded::isProtected");
212 bool returnValue = instance.isProtected();
213 resultPromise.set_value(returnValue);
214 });
215 }
216 mCondition.notify_one();
217 return resultFuture.get();
218}
219
220bool RenderEngineThreaded::supportsProtectedContent() const {
221 std::promise<bool> resultPromise;
222 std::future<bool> resultFuture = resultPromise.get_future();
223 {
224 std::lock_guard lock(mThreadMutex);
225 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
226 ATRACE_NAME("REThreaded::supportsProtectedContent");
227 bool returnValue = instance.supportsProtectedContent();
228 resultPromise.set_value(returnValue);
229 });
230 }
231 mCondition.notify_one();
232 return resultFuture.get();
233}
234
235bool RenderEngineThreaded::useProtectedContext(bool useProtectedContext) {
236 std::promise<bool> resultPromise;
237 std::future<bool> resultFuture = resultPromise.get_future();
238 {
239 std::lock_guard lock(mThreadMutex);
240 mFunctionCalls.push(
241 [&resultPromise, useProtectedContext](renderengine::RenderEngine& instance) {
242 ATRACE_NAME("REThreaded::useProtectedContext");
243 bool returnValue = instance.useProtectedContext(useProtectedContext);
244 resultPromise.set_value(returnValue);
245 });
246 }
247 mCondition.notify_one();
248 return resultFuture.get();
249}
250
Alec Mouri368e1582020-08-13 10:14:29 -0700251bool RenderEngineThreaded::cleanupPostRender(CleanupMode mode) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800252 std::promise<bool> resultPromise;
253 std::future<bool> resultFuture = resultPromise.get_future();
254 {
255 std::lock_guard lock(mThreadMutex);
Alec Mouri368e1582020-08-13 10:14:29 -0700256 mFunctionCalls.push([&resultPromise, mode](renderengine::RenderEngine& instance) {
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800257 ATRACE_NAME("REThreaded::cleanupPostRender");
Alec Mouri368e1582020-08-13 10:14:29 -0700258 bool returnValue = instance.cleanupPostRender(mode);
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800259 resultPromise.set_value(returnValue);
260 });
261 }
262 mCondition.notify_one();
263 return resultFuture.get();
264}
265
266status_t RenderEngineThreaded::drawLayers(const DisplaySettings& display,
267 const std::vector<const LayerSettings*>& layers,
268 const sp<GraphicBuffer>& buffer,
269 const bool useFramebufferCache,
270 base::unique_fd&& bufferFence,
271 base::unique_fd* drawFence) {
272 std::promise<status_t> resultPromise;
273 std::future<status_t> resultFuture = resultPromise.get_future();
274 {
275 std::lock_guard lock(mThreadMutex);
276 mFunctionCalls.push([&resultPromise, &display, &layers, &buffer, useFramebufferCache,
277 &bufferFence, &drawFence](renderengine::RenderEngine& instance) {
278 ATRACE_NAME("REThreaded::drawLayers");
279 status_t status = instance.drawLayers(display, layers, buffer, useFramebufferCache,
280 std::move(bufferFence), drawFence);
281 resultPromise.set_value(status);
282 });
283 }
284 mCondition.notify_one();
285 return resultFuture.get();
286}
287
Marin Shalamanov23c31af2020-09-09 15:01:47 +0200288void RenderEngineThreaded::cleanFramebufferCache() {
289 std::promise<void> resultPromise;
290 std::future<void> resultFuture = resultPromise.get_future();
291 {
292 std::lock_guard lock(mThreadMutex);
293 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
294 ATRACE_NAME("REThreaded::cleanFramebufferCache");
295 instance.cleanFramebufferCache();
296 resultPromise.set_value();
297 });
298 }
299 mCondition.notify_one();
300 resultFuture.wait();
301}
302
Alec Mourid6f09462020-12-07 11:18:17 -0800303int RenderEngineThreaded::getContextPriority() {
304 std::promise<int> resultPromise;
305 std::future<int> resultFuture = resultPromise.get_future();
306 {
307 std::lock_guard lock(mThreadMutex);
308 mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) {
309 ATRACE_NAME("REThreaded::getContextPriority");
310 int priority = instance.getContextPriority();
311 resultPromise.set_value(priority);
312 });
313 }
314 mCondition.notify_one();
315 return resultFuture.get();
316}
317
Ana Krulec9bc9dc62020-02-26 12:16:40 -0800318} // namespace threaded
319} // namespace renderengine
Alec Mouri368e1582020-08-13 10:14:29 -0700320} // namespace android