blob: 682baa60a2075933a8910dfc04875f456f2d197b [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
2 * Copyright (C) 2013 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
John Reckcec24ae2013-11-05 13:27:50 -080017#include "RenderThread.h"
18
John Reck0fa0cbc2019-04-05 16:57:46 -070019#include "../HardwareBitmapUploader.h"
John Reck4f02bf42014-01-03 18:09:17 -080020#include "CanvasContext.h"
John Reck56428472018-03-16 17:27:17 -070021#include "DeviceInfo.h"
John Reck3b202512014-06-23 13:13:08 -070022#include "EglManager.h"
Adlai Holler5636cde2021-03-25 16:53:56 +000023#include "Properties.h"
Stan Iliev1a025a72018-09-05 16:35:11 -040024#include "Readback.h"
John Reck4f02bf42014-01-03 18:09:17 -080025#include "RenderProxy.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050026#include "VulkanManager.h"
John Reck1bcacfd2017-11-03 10:12:19 -070027#include "hwui/Bitmap.h"
28#include "pipeline/skia/SkiaOpenGLPipeline.h"
John Reck1a4a9812018-04-18 16:13:31 -070029#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reck1bcacfd2017-11-03 10:12:19 -070030#include "renderstate/RenderState.h"
John Reck56428472018-03-16 17:27:17 -070031#include "utils/TimeUtils.h"
John Reck322b8ab2019-03-14 13:15:28 -070032#include "utils/TraceUtils.h"
John Reckcec24ae2013-11-05 13:27:50 -080033
John Reck1e510712018-04-23 08:15:03 -070034#include <GrContextOptions.h>
35#include <gl/GrGLInterface.h>
36
Huihong Luo5fdf7b82021-01-15 14:27:06 -080037#include <dlfcn.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080038#include <sys/resource.h>
John Reckcba287b2015-11-10 12:52:44 -080039#include <utils/Condition.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080040#include <utils/Log.h>
John Reckcba287b2015-11-10 12:52:44 -080041#include <utils/Mutex.h>
Stan Iliev898123b2019-02-14 14:57:44 -050042#include <thread>
Chris Craik65fe5ee2015-01-26 18:06:29 -080043
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -040044#include <android-base/properties.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000045#include <ui/FatVector.h>
46
John Reckcec24ae2013-11-05 13:27:50 -080047namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080048namespace uirenderer {
49namespace renderthread {
50
John Reck6b507802015-11-03 10:09:59 -080051static bool gHasRenderThreadInstance = false;
52
Stan Iliev80dbc352019-02-05 15:31:28 -050053static JVMAttachHook gOnStartHook = nullptr;
John Reck259b25a2017-12-01 16:18:53 -080054
Huihong Luo5fdf7b82021-01-15 14:27:06 -080055ASurfaceControlFunctions::ASurfaceControlFunctions() {
56 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
57 acquireFunc = (ASC_acquire) dlsym(handle_, "ASurfaceControl_acquire");
58 LOG_ALWAYS_FATAL_IF(acquireFunc == nullptr,
59 "Failed to find required symbol ASurfaceControl_acquire!");
60
61 releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release");
62 LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr,
63 "Failed to find required symbol ASurfaceControl_release!");
Jorim Jaggi71db8892021-02-03 23:19:29 +010064
65 registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_,
66 "ASurfaceControl_registerSurfaceStatsListener");
67 LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr,
68 "Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!");
69
70 unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_,
71 "ASurfaceControl_unregisterSurfaceStatsListener");
72 LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr,
73 "Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!");
74
75 getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_,
76 "ASurfaceControlStats_getAcquireTime");
77 LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr,
78 "Failed to find required symbol ASurfaceControlStats_getAcquireTime!");
79
80 getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_,
81 "ASurfaceControlStats_getFrameNumber");
82 LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr,
83 "Failed to find required symbol ASurfaceControlStats_getFrameNumber!");
Huihong Luo5fdf7b82021-01-15 14:27:06 -080084}
85
Alec Mouri4a818f12019-11-19 16:17:53 -080086void RenderThread::frameCallback(int64_t frameTimeNanos, void* data) {
87 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
Steven Thomas6fabb5a2020-08-21 16:56:08 -070088 int64_t vsyncId = AChoreographer_getVsyncId(rt->mChoreographer);
Ady Abrahamdfb13982020-10-05 17:59:09 -070089 int64_t frameDeadline = AChoreographer_getFrameDeadline(rt->mChoreographer);
Jorim Jaggi10f328c2021-01-19 00:08:02 +010090 int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer);
Alec Mouri4a818f12019-11-19 16:17:53 -080091 rt->mVsyncRequested = false;
Jorim Jaggi10f328c2021-01-19 00:08:02 +010092 if (rt->timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline,
93 frameInterval) && !rt->mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -080094 ATRACE_NAME("queue mFrameCallbackTask");
95 rt->mFrameCallbackTaskPending = true;
96 nsecs_t runAt = (frameTimeNanos + rt->mDispatchFrameDelay);
97 rt->queue().postAt(runAt, [=]() { rt->dispatchFrameCallbacks(); });
98 }
99}
100
101void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
102 ATRACE_NAME("refreshRateCallback");
103 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
104 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
105 rt->setupFrameInterval();
106}
107
108class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700109public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800110 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700111
112 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800113 AChoreographer_postFrameCallback64(mRenderThread->mChoreographer,
114 RenderThread::frameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700115 }
116
Alec Mouri4a818f12019-11-19 16:17:53 -0800117 virtual void drainPendingEvents() override {
118 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700119 }
120
121private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800122 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700123};
124
125class DummyVsyncSource : public VsyncSource {
126public:
127 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
128
129 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800130 mRenderThread->queue().postDelayed(16_ms, [this]() {
131 RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread);
132 });
John Reck56428472018-03-16 17:27:17 -0700133 }
134
Alec Mouri4a818f12019-11-19 16:17:53 -0800135 virtual void drainPendingEvents() override {
136 RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread);
137 }
John Reck56428472018-03-16 17:27:17 -0700138
139private:
140 RenderThread* mRenderThread;
141};
142
John Reck6b507802015-11-03 10:09:59 -0800143bool RenderThread::hasInstance() {
144 return gHasRenderThreadInstance;
145}
146
Stan Iliev80dbc352019-02-05 15:31:28 -0500147void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800148 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
149 gOnStartHook = onStartHook;
150}
151
Stan Iliev80dbc352019-02-05 15:31:28 -0500152JVMAttachHook RenderThread::getOnStartHook() {
153 return gOnStartHook;
154}
155
John Reck6b507802015-11-03 10:09:59 -0800156RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000157 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
158 sp<RenderThread> thread = sp<RenderThread>::make();
159 thread->start("RenderThread");
160 return thread;
161 }();
John Reck6b507802015-11-03 10:09:59 -0800162 gHasRenderThreadInstance = true;
163 return *sInstance;
164}
165
John Reck1bcacfd2017-11-03 10:12:19 -0700166RenderThread::RenderThread()
167 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700168 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700169 , mVsyncRequested(false)
170 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800171 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500172 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100173 , mFunctorManager(WebViewFunctorManager::instance())
174 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700175 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800176}
177
178RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800179 // Note that if this fatal assertion is removed then member variables must
180 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700181 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800182}
183
Alec Mouri4a818f12019-11-19 16:17:53 -0800184void RenderThread::initializeChoreographer() {
185 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700186
John Reck56428472018-03-16 17:27:17 -0700187 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800188 mChoreographer = AChoreographer_create();
189 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
190 AChoreographer_registerRefreshRateCallback(mChoreographer,
191 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700192
193 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800194 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
195 RenderThread::choreographerCallback, this);
196 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700197 } else {
198 mVsyncSource = new DummyVsyncSource(this);
199 }
John Recke45b1fd2014-04-15 09:50:16 -0700200}
201
John Reck3b202512014-06-23 13:13:08 -0700202void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700203 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800204 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700205 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700206 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400207 mVkManager = VulkanManager::getInstance();
Alec Mouri22d753f2019-09-05 17:11:45 -0700208 mCacheManager = new CacheManager();
John Reckcf185f52019-04-11 16:11:24 -0700209}
210
211void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800212 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700213 mTimeLord.setFrameInterval(frameIntervalNanos);
214 mDispatchFrameDelay = static_cast<nsecs_t>(frameIntervalNanos * .25f);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400215}
216
John Reck1e510712018-04-23 08:15:03 -0700217void RenderThread::requireGlContext() {
218 if (mEglManager->hasEglContext()) {
219 return;
220 }
221 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700222
John Reckb5fc2092018-04-30 14:43:22 -0700223 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
John Reckb5fc2092018-04-30 14:43:22 -0700224 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700225
John Reckb5fc2092018-04-30 14:43:22 -0700226 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500227 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800228 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
229 auto size = glesVersion ? strlen(glesVersion) : -1;
230 cacheManager().configureContext(&options, glesVersion, size);
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400231 sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700232 LOG_ALWAYS_FATAL_IF(!grContext.get());
233 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700234}
235
Stan Iliev981afe72019-02-13 14:24:33 -0500236void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400237 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800238 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
239 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
240 // in the rendering thread.
241 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500242 return;
243 }
244 mVkManager->initialize();
245 GrContextOptions options;
246 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400247 auto vkDriverVersion = mVkManager->getDriverVersion();
248 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400249 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500250 LOG_ALWAYS_FATAL_IF(!grContext.get());
251 setGrContext(grContext);
252}
253
254void RenderThread::initGrContextOptions(GrContextOptions& options) {
255 options.fPreferExternalImagesOverES3 = true;
256 options.fDisableDistanceFieldPaths = true;
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400257 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, false)) {
258 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
259 } else {
260 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
261 }
Stan Iliev981afe72019-02-13 14:24:33 -0500262}
263
John Reck283bb462018-12-13 16:40:14 -0800264void RenderThread::destroyRenderingContext() {
265 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500266 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
267 if (mEglManager->hasEglContext()) {
268 setGrContext(nullptr);
269 mEglManager->destroy();
270 }
271 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400272 setGrContext(nullptr);
273 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700274 }
275}
276
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400277VulkanManager& RenderThread::vulkanManager() {
278 if (!mVkManager.get()) {
279 mVkManager = VulkanManager::getInstance();
280 }
281 return *mVkManager.get();
282}
283
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400284void RenderThread::dumpGraphicsMemory(int fd) {
John Reck34781b22017-07-05 16:39:36 -0700285 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400286
287 String8 cachesOutput;
288 String8 pipeline;
289 auto renderType = Properties::getRenderPipelineType();
290 switch (renderType) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400291 case RenderPipelineType::SkiaGL: {
292 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
293 pipeline.appendFormat("Skia (OpenGL)");
294 break;
295 }
296 case RenderPipelineType::SkiaVulkan: {
297 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
298 pipeline.appendFormat("Skia (Vulkan)");
299 break;
300 }
301 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700302 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400303 break;
304 }
305
John Reck47f5c3a2017-11-13 11:32:39 -0800306 dprintf(fd, "\n%s\n", cachesOutput.string());
307 dprintf(fd, "\nPipeline=%s\n", pipeline.string());
John Reck3b202512014-06-23 13:13:08 -0700308}
309
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500310Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500311 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400312 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500313 }
314
315 return *mReadback;
316}
317
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400318void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400319 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500320 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400321 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400322 mGrContext->releaseResourcesAndAbandonContext();
323 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500324 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400325 if (mGrContext) {
326 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
327 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400328}
329
Alec Mouri4a818f12019-11-19 16:17:53 -0800330int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700331 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
332 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700333 "events=0x%x",
334 events);
335 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700336 }
337
338 if (!(events & Looper::EVENT_INPUT)) {
339 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700340 "events=0x%x",
341 events);
342 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700343 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800344 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
345 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700346
Alec Mouri4a818f12019-11-19 16:17:53 -0800347 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700348}
349
350void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700351 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700352 mFrameCallbackTaskPending = false;
353
354 std::set<IFrameCallback*> callbacks;
355 mFrameCallbacks.swap(callbacks);
356
John Recka733f892014-12-19 11:37:21 -0800357 if (callbacks.size()) {
358 // Assume one of them will probably animate again so preemptively
359 // request the next vsync in case it occurs mid-frame
360 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700361 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
362 it++) {
John Recka733f892014-12-19 11:37:21 -0800363 (*it)->doFrame();
364 }
John Recke45b1fd2014-04-15 09:50:16 -0700365 }
366}
367
John Recka5dda642014-05-22 15:43:54 -0700368void RenderThread::requestVsync() {
369 if (!mVsyncRequested) {
370 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700371 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700372 }
373}
374
John Reckcec24ae2013-11-05 13:27:50 -0800375bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700376 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800377 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800378 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500379 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800380 }
John Reck3b202512014-06-23 13:13:08 -0700381 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700382
John Reckf8441e62017-10-23 13:10:41 -0700383 while (true) {
384 waitForWork();
385 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700386
387 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800388 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700389 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
390 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700391 mPendingRegistrationFrameCallbacks.clear();
392 requestVsync();
393 }
John Recka22c9b22015-01-14 10:40:15 -0800394
395 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
396 // TODO: Clean this up. This is working around an issue where a combination
397 // of bad timing and slow drawing can result in dropping a stale vsync
398 // on the floor (correct!) but fails to schedule to listen for the
399 // next vsync (oops), so none of the callbacks are run.
400 requestVsync();
401 }
John Reckcec24ae2013-11-05 13:27:50 -0800402 }
403
404 return false;
405}
406
John Recke45b1fd2014-04-15 09:50:16 -0700407void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700408 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700409}
410
John Reck01a5ea32014-12-03 13:01:07 -0800411bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
412 size_t erased;
413 erased = mFrameCallbacks.erase(callback);
414 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
415 return erased;
John Recka5dda642014-05-22 15:43:54 -0700416}
417
418void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
419 if (mFrameCallbacks.erase(callback)) {
420 mPendingRegistrationFrameCallbacks.insert(callback);
421 }
John Recke45b1fd2014-04-15 09:50:16 -0700422}
423
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400424sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
425 auto renderType = Properties::getRenderPipelineType();
426 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400427 case RenderPipelineType::SkiaVulkan:
428 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
429 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700430 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400431 break;
432 }
433 return nullptr;
434}
435
Stan Iliev6b894d72017-08-23 12:41:41 -0400436bool RenderThread::isCurrent() {
437 return gettid() == getInstance().getTid();
438}
439
Stan Iliev898123b2019-02-14 14:57:44 -0500440void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400441 // EGL driver is always preloaded only if HWUI renders with GL.
442 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700443 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400444 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000445 } else {
446 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500447 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000448 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500449}
450
John Reckcec24ae2013-11-05 13:27:50 -0800451} /* namespace renderthread */
452} /* namespace uirenderer */
453} /* namespace android */