blob: 79b938841bc2952c99812685d8280f2d2972167b [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);
Alec Mouri4a818f12019-11-19 16:17:53 -080090 rt->mVsyncRequested = false;
Ady Abrahamdfb13982020-10-05 17:59:09 -070091 if (rt->timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline) &&
Steven Thomas6fabb5a2020-08-21 16:56:08 -070092 !rt->mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -080093 ATRACE_NAME("queue mFrameCallbackTask");
94 rt->mFrameCallbackTaskPending = true;
95 nsecs_t runAt = (frameTimeNanos + rt->mDispatchFrameDelay);
96 rt->queue().postAt(runAt, [=]() { rt->dispatchFrameCallbacks(); });
97 }
98}
99
100void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
101 ATRACE_NAME("refreshRateCallback");
102 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
103 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
104 rt->setupFrameInterval();
105}
106
107class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700108public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800109 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700110
111 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800112 AChoreographer_postFrameCallback64(mRenderThread->mChoreographer,
113 RenderThread::frameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700114 }
115
Alec Mouri4a818f12019-11-19 16:17:53 -0800116 virtual void drainPendingEvents() override {
117 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700118 }
119
120private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800121 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700122};
123
124class DummyVsyncSource : public VsyncSource {
125public:
126 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
127
128 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800129 mRenderThread->queue().postDelayed(16_ms, [this]() {
130 RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread);
131 });
John Reck56428472018-03-16 17:27:17 -0700132 }
133
Alec Mouri4a818f12019-11-19 16:17:53 -0800134 virtual void drainPendingEvents() override {
135 RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread);
136 }
John Reck56428472018-03-16 17:27:17 -0700137
138private:
139 RenderThread* mRenderThread;
140};
141
John Reck6b507802015-11-03 10:09:59 -0800142bool RenderThread::hasInstance() {
143 return gHasRenderThreadInstance;
144}
145
Stan Iliev80dbc352019-02-05 15:31:28 -0500146void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800147 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
148 gOnStartHook = onStartHook;
149}
150
Stan Iliev80dbc352019-02-05 15:31:28 -0500151JVMAttachHook RenderThread::getOnStartHook() {
152 return gOnStartHook;
153}
154
John Reck6b507802015-11-03 10:09:59 -0800155RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000156 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
157 sp<RenderThread> thread = sp<RenderThread>::make();
158 thread->start("RenderThread");
159 return thread;
160 }();
John Reck6b507802015-11-03 10:09:59 -0800161 gHasRenderThreadInstance = true;
162 return *sInstance;
163}
164
John Reck1bcacfd2017-11-03 10:12:19 -0700165RenderThread::RenderThread()
166 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700167 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700168 , mVsyncRequested(false)
169 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800170 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500171 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100172 , mFunctorManager(WebViewFunctorManager::instance())
173 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700174 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800175}
176
177RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800178 // Note that if this fatal assertion is removed then member variables must
179 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700180 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800181}
182
Alec Mouri4a818f12019-11-19 16:17:53 -0800183void RenderThread::initializeChoreographer() {
184 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700185
John Reck56428472018-03-16 17:27:17 -0700186 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800187 mChoreographer = AChoreographer_create();
188 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
189 AChoreographer_registerRefreshRateCallback(mChoreographer,
190 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700191
192 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800193 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
194 RenderThread::choreographerCallback, this);
195 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700196 } else {
197 mVsyncSource = new DummyVsyncSource(this);
198 }
John Recke45b1fd2014-04-15 09:50:16 -0700199}
200
John Reck3b202512014-06-23 13:13:08 -0700201void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700202 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800203 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700204 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700205 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400206 mVkManager = VulkanManager::getInstance();
Alec Mouri22d753f2019-09-05 17:11:45 -0700207 mCacheManager = new CacheManager();
John Reckcf185f52019-04-11 16:11:24 -0700208}
209
210void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800211 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700212 mTimeLord.setFrameInterval(frameIntervalNanos);
213 mDispatchFrameDelay = static_cast<nsecs_t>(frameIntervalNanos * .25f);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400214}
215
John Reck1e510712018-04-23 08:15:03 -0700216void RenderThread::requireGlContext() {
217 if (mEglManager->hasEglContext()) {
218 return;
219 }
220 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700221
John Reckb5fc2092018-04-30 14:43:22 -0700222 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
John Reckb5fc2092018-04-30 14:43:22 -0700223 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700224
John Reckb5fc2092018-04-30 14:43:22 -0700225 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500226 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800227 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
228 auto size = glesVersion ? strlen(glesVersion) : -1;
229 cacheManager().configureContext(&options, glesVersion, size);
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400230 sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700231 LOG_ALWAYS_FATAL_IF(!grContext.get());
232 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700233}
234
Stan Iliev981afe72019-02-13 14:24:33 -0500235void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400236 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800237 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
238 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
239 // in the rendering thread.
240 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500241 return;
242 }
243 mVkManager->initialize();
244 GrContextOptions options;
245 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400246 auto vkDriverVersion = mVkManager->getDriverVersion();
247 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400248 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500249 LOG_ALWAYS_FATAL_IF(!grContext.get());
250 setGrContext(grContext);
251}
252
253void RenderThread::initGrContextOptions(GrContextOptions& options) {
254 options.fPreferExternalImagesOverES3 = true;
255 options.fDisableDistanceFieldPaths = true;
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400256 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, false)) {
257 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
258 } else {
259 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
260 }
Stan Iliev981afe72019-02-13 14:24:33 -0500261}
262
John Reck283bb462018-12-13 16:40:14 -0800263void RenderThread::destroyRenderingContext() {
264 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500265 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
266 if (mEglManager->hasEglContext()) {
267 setGrContext(nullptr);
268 mEglManager->destroy();
269 }
270 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400271 setGrContext(nullptr);
272 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700273 }
274}
275
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400276VulkanManager& RenderThread::vulkanManager() {
277 if (!mVkManager.get()) {
278 mVkManager = VulkanManager::getInstance();
279 }
280 return *mVkManager.get();
281}
282
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400283void RenderThread::dumpGraphicsMemory(int fd) {
John Reck34781b22017-07-05 16:39:36 -0700284 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400285
286 String8 cachesOutput;
287 String8 pipeline;
288 auto renderType = Properties::getRenderPipelineType();
289 switch (renderType) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400290 case RenderPipelineType::SkiaGL: {
291 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
292 pipeline.appendFormat("Skia (OpenGL)");
293 break;
294 }
295 case RenderPipelineType::SkiaVulkan: {
296 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
297 pipeline.appendFormat("Skia (Vulkan)");
298 break;
299 }
300 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700301 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400302 break;
303 }
304
John Reck47f5c3a2017-11-13 11:32:39 -0800305 dprintf(fd, "\n%s\n", cachesOutput.string());
306 dprintf(fd, "\nPipeline=%s\n", pipeline.string());
John Reck3b202512014-06-23 13:13:08 -0700307}
308
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500309Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500310 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400311 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500312 }
313
314 return *mReadback;
315}
316
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400317void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400318 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500319 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400320 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400321 mGrContext->releaseResourcesAndAbandonContext();
322 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500323 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400324 if (mGrContext) {
325 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
326 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400327}
328
Alec Mouri4a818f12019-11-19 16:17:53 -0800329int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700330 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
331 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700332 "events=0x%x",
333 events);
334 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700335 }
336
337 if (!(events & Looper::EVENT_INPUT)) {
338 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700339 "events=0x%x",
340 events);
341 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700342 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800343 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
344 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700345
Alec Mouri4a818f12019-11-19 16:17:53 -0800346 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700347}
348
349void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700350 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700351 mFrameCallbackTaskPending = false;
352
353 std::set<IFrameCallback*> callbacks;
354 mFrameCallbacks.swap(callbacks);
355
John Recka733f892014-12-19 11:37:21 -0800356 if (callbacks.size()) {
357 // Assume one of them will probably animate again so preemptively
358 // request the next vsync in case it occurs mid-frame
359 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700360 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
361 it++) {
John Recka733f892014-12-19 11:37:21 -0800362 (*it)->doFrame();
363 }
John Recke45b1fd2014-04-15 09:50:16 -0700364 }
365}
366
John Recka5dda642014-05-22 15:43:54 -0700367void RenderThread::requestVsync() {
368 if (!mVsyncRequested) {
369 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700370 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700371 }
372}
373
John Reckcec24ae2013-11-05 13:27:50 -0800374bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700375 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800376 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800377 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500378 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800379 }
John Reck3b202512014-06-23 13:13:08 -0700380 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700381
John Reckf8441e62017-10-23 13:10:41 -0700382 while (true) {
383 waitForWork();
384 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700385
386 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800387 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700388 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
389 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700390 mPendingRegistrationFrameCallbacks.clear();
391 requestVsync();
392 }
John Recka22c9b22015-01-14 10:40:15 -0800393
394 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
395 // TODO: Clean this up. This is working around an issue where a combination
396 // of bad timing and slow drawing can result in dropping a stale vsync
397 // on the floor (correct!) but fails to schedule to listen for the
398 // next vsync (oops), so none of the callbacks are run.
399 requestVsync();
400 }
John Reckcec24ae2013-11-05 13:27:50 -0800401 }
402
403 return false;
404}
405
John Recke45b1fd2014-04-15 09:50:16 -0700406void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700407 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700408}
409
John Reck01a5ea32014-12-03 13:01:07 -0800410bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
411 size_t erased;
412 erased = mFrameCallbacks.erase(callback);
413 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
414 return erased;
John Recka5dda642014-05-22 15:43:54 -0700415}
416
417void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
418 if (mFrameCallbacks.erase(callback)) {
419 mPendingRegistrationFrameCallbacks.insert(callback);
420 }
John Recke45b1fd2014-04-15 09:50:16 -0700421}
422
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400423sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
424 auto renderType = Properties::getRenderPipelineType();
425 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400426 case RenderPipelineType::SkiaVulkan:
427 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
428 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700429 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400430 break;
431 }
432 return nullptr;
433}
434
Stan Iliev6b894d72017-08-23 12:41:41 -0400435bool RenderThread::isCurrent() {
436 return gettid() == getInstance().getTid();
437}
438
Stan Iliev898123b2019-02-14 14:57:44 -0500439void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400440 // EGL driver is always preloaded only if HWUI renders with GL.
441 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700442 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400443 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000444 } else {
445 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500446 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000447 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500448}
449
John Reckcec24ae2013-11-05 13:27:50 -0800450} /* namespace renderthread */
451} /* namespace uirenderer */
452} /* namespace android */