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> |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | #include "gl/GLESRenderEngine.h" |
| 30 | |
| 31 | using namespace std::chrono_literals; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace renderengine { |
| 35 | namespace threaded { |
| 36 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 37 | std::unique_ptr<RenderEngineThreaded> RenderEngineThreaded::create(CreateInstanceFactory factory) { |
| 38 | return std::make_unique<RenderEngineThreaded>(std::move(factory)); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 41 | RenderEngineThreaded::RenderEngineThreaded(CreateInstanceFactory factory) { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 42 | ATRACE_CALL(); |
| 43 | |
| 44 | std::lock_guard lockThread(mThreadMutex); |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 45 | mThread = std::thread(&RenderEngineThreaded::threadMain, this, factory); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | RenderEngineThreaded::~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 Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 61 | void RenderEngineThreaded::threadMain(CreateInstanceFactory factory) NO_THREAD_SAFETY_ANALYSIS { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 62 | ATRACE_CALL(); |
| 63 | |
| 64 | struct sched_param param = {0}; |
| 65 | param.sched_priority = 2; |
| 66 | if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { |
| 67 | ALOGE("Couldn't set SCHED_FIFO"); |
| 68 | } |
| 69 | |
Ana Krulec | 15f7cf3 | 2020-05-12 11:57:42 -0700 | [diff] [blame] | 70 | mRenderEngine = factory(); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 71 | |
| 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 | |
| 87 | void 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 | |
| 102 | void 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 Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 119 | void 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 | |
| 134 | void 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 Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 149 | void RenderEngineThreaded::cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) { |
| 150 | std::promise<void> resultPromise; |
| 151 | std::future<void> resultFuture = resultPromise.get_future(); |
| 152 | { |
| 153 | std::lock_guard lock(mThreadMutex); |
| 154 | mFunctionCalls.push([&resultPromise, &buffer](renderengine::RenderEngine& instance) { |
| 155 | ATRACE_NAME("REThreaded::cacheExternalTextureBuffer"); |
| 156 | instance.cacheExternalTextureBuffer(buffer); |
| 157 | resultPromise.set_value(); |
| 158 | }); |
| 159 | } |
| 160 | mCondition.notify_one(); |
| 161 | resultFuture.wait(); |
| 162 | } |
| 163 | |
| 164 | void RenderEngineThreaded::unbindExternalTextureBuffer(uint64_t bufferId) { |
| 165 | std::promise<void> resultPromise; |
| 166 | std::future<void> resultFuture = resultPromise.get_future(); |
| 167 | { |
| 168 | std::lock_guard lock(mThreadMutex); |
| 169 | mFunctionCalls.push([&resultPromise, &bufferId](renderengine::RenderEngine& instance) { |
| 170 | ATRACE_NAME("REThreaded::unbindExternalTextureBuffer"); |
| 171 | instance.unbindExternalTextureBuffer(bufferId); |
| 172 | resultPromise.set_value(); |
| 173 | }); |
| 174 | } |
| 175 | mCondition.notify_one(); |
| 176 | resultFuture.wait(); |
| 177 | } |
| 178 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 179 | size_t RenderEngineThreaded::getMaxTextureSize() const { |
| 180 | std::promise<size_t> resultPromise; |
| 181 | std::future<size_t> resultFuture = resultPromise.get_future(); |
| 182 | { |
| 183 | std::lock_guard lock(mThreadMutex); |
| 184 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 185 | ATRACE_NAME("REThreaded::getMaxTextureSize"); |
| 186 | size_t size = instance.getMaxTextureSize(); |
| 187 | resultPromise.set_value(size); |
| 188 | }); |
| 189 | } |
| 190 | mCondition.notify_one(); |
| 191 | return resultFuture.get(); |
| 192 | } |
| 193 | |
| 194 | size_t RenderEngineThreaded::getMaxViewportDims() const { |
| 195 | std::promise<size_t> resultPromise; |
| 196 | std::future<size_t> resultFuture = resultPromise.get_future(); |
| 197 | { |
| 198 | std::lock_guard lock(mThreadMutex); |
| 199 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 200 | ATRACE_NAME("REThreaded::getMaxViewportDims"); |
| 201 | size_t size = instance.getMaxViewportDims(); |
| 202 | resultPromise.set_value(size); |
| 203 | }); |
| 204 | } |
| 205 | mCondition.notify_one(); |
| 206 | return resultFuture.get(); |
| 207 | } |
| 208 | |
| 209 | bool RenderEngineThreaded::isProtected() const { |
| 210 | std::promise<bool> resultPromise; |
| 211 | std::future<bool> resultFuture = resultPromise.get_future(); |
| 212 | { |
| 213 | std::lock_guard lock(mThreadMutex); |
| 214 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 215 | ATRACE_NAME("REThreaded::isProtected"); |
| 216 | bool returnValue = instance.isProtected(); |
| 217 | resultPromise.set_value(returnValue); |
| 218 | }); |
| 219 | } |
| 220 | mCondition.notify_one(); |
| 221 | return resultFuture.get(); |
| 222 | } |
| 223 | |
| 224 | bool RenderEngineThreaded::supportsProtectedContent() const { |
| 225 | std::promise<bool> resultPromise; |
| 226 | std::future<bool> resultFuture = resultPromise.get_future(); |
| 227 | { |
| 228 | std::lock_guard lock(mThreadMutex); |
| 229 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 230 | ATRACE_NAME("REThreaded::supportsProtectedContent"); |
| 231 | bool returnValue = instance.supportsProtectedContent(); |
| 232 | resultPromise.set_value(returnValue); |
| 233 | }); |
| 234 | } |
| 235 | mCondition.notify_one(); |
| 236 | return resultFuture.get(); |
| 237 | } |
| 238 | |
| 239 | bool RenderEngineThreaded::useProtectedContext(bool useProtectedContext) { |
| 240 | std::promise<bool> resultPromise; |
| 241 | std::future<bool> resultFuture = resultPromise.get_future(); |
| 242 | { |
| 243 | std::lock_guard lock(mThreadMutex); |
| 244 | mFunctionCalls.push( |
| 245 | [&resultPromise, useProtectedContext](renderengine::RenderEngine& instance) { |
| 246 | ATRACE_NAME("REThreaded::useProtectedContext"); |
| 247 | bool returnValue = instance.useProtectedContext(useProtectedContext); |
| 248 | resultPromise.set_value(returnValue); |
| 249 | }); |
| 250 | } |
| 251 | mCondition.notify_one(); |
| 252 | return resultFuture.get(); |
| 253 | } |
| 254 | |
Alec Mouri | 368e158 | 2020-08-13 10:14:29 -0700 | [diff] [blame] | 255 | bool RenderEngineThreaded::cleanupPostRender(CleanupMode mode) { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 256 | std::promise<bool> resultPromise; |
| 257 | std::future<bool> resultFuture = resultPromise.get_future(); |
| 258 | { |
| 259 | std::lock_guard lock(mThreadMutex); |
Alec Mouri | 368e158 | 2020-08-13 10:14:29 -0700 | [diff] [blame] | 260 | mFunctionCalls.push([&resultPromise, mode](renderengine::RenderEngine& instance) { |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 261 | ATRACE_NAME("REThreaded::cleanupPostRender"); |
Alec Mouri | 368e158 | 2020-08-13 10:14:29 -0700 | [diff] [blame] | 262 | bool returnValue = instance.cleanupPostRender(mode); |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 263 | resultPromise.set_value(returnValue); |
| 264 | }); |
| 265 | } |
| 266 | mCondition.notify_one(); |
| 267 | return resultFuture.get(); |
| 268 | } |
| 269 | |
| 270 | status_t RenderEngineThreaded::drawLayers(const DisplaySettings& display, |
| 271 | const std::vector<const LayerSettings*>& layers, |
| 272 | const sp<GraphicBuffer>& buffer, |
| 273 | const bool useFramebufferCache, |
| 274 | base::unique_fd&& bufferFence, |
| 275 | base::unique_fd* drawFence) { |
| 276 | std::promise<status_t> resultPromise; |
| 277 | std::future<status_t> resultFuture = resultPromise.get_future(); |
| 278 | { |
| 279 | std::lock_guard lock(mThreadMutex); |
| 280 | mFunctionCalls.push([&resultPromise, &display, &layers, &buffer, useFramebufferCache, |
| 281 | &bufferFence, &drawFence](renderengine::RenderEngine& instance) { |
| 282 | ATRACE_NAME("REThreaded::drawLayers"); |
| 283 | status_t status = instance.drawLayers(display, layers, buffer, useFramebufferCache, |
| 284 | std::move(bufferFence), drawFence); |
| 285 | resultPromise.set_value(status); |
| 286 | }); |
| 287 | } |
| 288 | mCondition.notify_one(); |
| 289 | return resultFuture.get(); |
| 290 | } |
| 291 | |
Marin Shalamanov | 23c31af | 2020-09-09 15:01:47 +0200 | [diff] [blame] | 292 | void RenderEngineThreaded::cleanFramebufferCache() { |
| 293 | std::promise<void> resultPromise; |
| 294 | std::future<void> resultFuture = resultPromise.get_future(); |
| 295 | { |
| 296 | std::lock_guard lock(mThreadMutex); |
| 297 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 298 | ATRACE_NAME("REThreaded::cleanFramebufferCache"); |
| 299 | instance.cleanFramebufferCache(); |
| 300 | resultPromise.set_value(); |
| 301 | }); |
| 302 | } |
| 303 | mCondition.notify_one(); |
| 304 | resultFuture.wait(); |
| 305 | } |
| 306 | |
Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 307 | int RenderEngineThreaded::getContextPriority() { |
| 308 | std::promise<int> resultPromise; |
| 309 | std::future<int> resultFuture = resultPromise.get_future(); |
| 310 | { |
| 311 | std::lock_guard lock(mThreadMutex); |
| 312 | mFunctionCalls.push([&resultPromise](renderengine::RenderEngine& instance) { |
| 313 | ATRACE_NAME("REThreaded::getContextPriority"); |
| 314 | int priority = instance.getContextPriority(); |
| 315 | resultPromise.set_value(priority); |
| 316 | }); |
| 317 | } |
| 318 | mCondition.notify_one(); |
| 319 | return resultFuture.get(); |
| 320 | } |
| 321 | |
Ana Krulec | 9bc9dc6 | 2020-02-26 12:16:40 -0800 | [diff] [blame] | 322 | } // namespace threaded |
| 323 | } // namespace renderengine |
Alec Mouri | 368e158 | 2020-08-13 10:14:29 -0700 | [diff] [blame] | 324 | } // namespace android |