blob: 0268bfd73e55ca904496668e26accf43b1b292cb [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);
Huihong Luo054b8d32021-02-24 18:48:12 -080057 createFunc = (ASC_create)dlsym(handle_, "ASurfaceControl_create");
58 LOG_ALWAYS_FATAL_IF(createFunc == nullptr,
59 "Failed to find required symbol ASurfaceControl_create!");
60
Huihong Luo5fdf7b82021-01-15 14:27:06 -080061 acquireFunc = (ASC_acquire) dlsym(handle_, "ASurfaceControl_acquire");
62 LOG_ALWAYS_FATAL_IF(acquireFunc == nullptr,
63 "Failed to find required symbol ASurfaceControl_acquire!");
64
65 releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release");
66 LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr,
67 "Failed to find required symbol ASurfaceControl_release!");
Jorim Jaggi71db8892021-02-03 23:19:29 +010068
69 registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_,
70 "ASurfaceControl_registerSurfaceStatsListener");
71 LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr,
72 "Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!");
73
74 unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_,
75 "ASurfaceControl_unregisterSurfaceStatsListener");
76 LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr,
77 "Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!");
78
79 getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_,
80 "ASurfaceControlStats_getAcquireTime");
81 LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr,
82 "Failed to find required symbol ASurfaceControlStats_getAcquireTime!");
83
84 getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_,
85 "ASurfaceControlStats_getFrameNumber");
86 LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr,
87 "Failed to find required symbol ASurfaceControlStats_getFrameNumber!");
Huihong Luo054b8d32021-02-24 18:48:12 -080088
89 transactionCreateFunc = (AST_create)dlsym(handle_, "ASurfaceTransaction_create");
90 LOG_ALWAYS_FATAL_IF(transactionCreateFunc == nullptr,
91 "Failed to find required symbol ASurfaceTransaction_create!");
92
93 transactionDeleteFunc = (AST_delete)dlsym(handle_, "ASurfaceTransaction_delete");
94 LOG_ALWAYS_FATAL_IF(transactionDeleteFunc == nullptr,
95 "Failed to find required symbol ASurfaceTransaction_delete!");
96
97 transactionApplyFunc = (AST_apply)dlsym(handle_, "ASurfaceTransaction_apply");
98 LOG_ALWAYS_FATAL_IF(transactionApplyFunc == nullptr,
99 "Failed to find required symbol ASurfaceTransaction_apply!");
100
101 transactionSetVisibilityFunc =
102 (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility");
103 LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr,
104 "Failed to find required symbol ASurfaceTransaction_setVisibility!");
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800105}
106
Alec Mouri4a818f12019-11-19 16:17:53 -0800107void RenderThread::frameCallback(int64_t frameTimeNanos, void* data) {
108 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
Steven Thomas6fabb5a2020-08-21 16:56:08 -0700109 int64_t vsyncId = AChoreographer_getVsyncId(rt->mChoreographer);
Ady Abrahamdfb13982020-10-05 17:59:09 -0700110 int64_t frameDeadline = AChoreographer_getFrameDeadline(rt->mChoreographer);
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100111 int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer);
Alec Mouri4a818f12019-11-19 16:17:53 -0800112 rt->mVsyncRequested = false;
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100113 if (rt->timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline,
114 frameInterval) && !rt->mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800115 ATRACE_NAME("queue mFrameCallbackTask");
116 rt->mFrameCallbackTaskPending = true;
117 nsecs_t runAt = (frameTimeNanos + rt->mDispatchFrameDelay);
118 rt->queue().postAt(runAt, [=]() { rt->dispatchFrameCallbacks(); });
119 }
120}
121
122void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
123 ATRACE_NAME("refreshRateCallback");
124 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
125 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
126 rt->setupFrameInterval();
127}
128
129class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700130public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800131 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700132
133 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800134 AChoreographer_postFrameCallback64(mRenderThread->mChoreographer,
135 RenderThread::frameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700136 }
137
Alec Mouri4a818f12019-11-19 16:17:53 -0800138 virtual void drainPendingEvents() override {
139 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700140 }
141
142private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800143 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700144};
145
146class DummyVsyncSource : public VsyncSource {
147public:
148 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
149
150 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800151 mRenderThread->queue().postDelayed(16_ms, [this]() {
152 RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread);
153 });
John Reck56428472018-03-16 17:27:17 -0700154 }
155
Alec Mouri4a818f12019-11-19 16:17:53 -0800156 virtual void drainPendingEvents() override {
157 RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread);
158 }
John Reck56428472018-03-16 17:27:17 -0700159
160private:
161 RenderThread* mRenderThread;
162};
163
John Reck6b507802015-11-03 10:09:59 -0800164bool RenderThread::hasInstance() {
165 return gHasRenderThreadInstance;
166}
167
Stan Iliev80dbc352019-02-05 15:31:28 -0500168void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800169 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
170 gOnStartHook = onStartHook;
171}
172
Stan Iliev80dbc352019-02-05 15:31:28 -0500173JVMAttachHook RenderThread::getOnStartHook() {
174 return gOnStartHook;
175}
176
John Reck6b507802015-11-03 10:09:59 -0800177RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000178 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
179 sp<RenderThread> thread = sp<RenderThread>::make();
180 thread->start("RenderThread");
181 return thread;
182 }();
John Reck6b507802015-11-03 10:09:59 -0800183 gHasRenderThreadInstance = true;
184 return *sInstance;
185}
186
John Reck1bcacfd2017-11-03 10:12:19 -0700187RenderThread::RenderThread()
188 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700189 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700190 , mVsyncRequested(false)
191 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800192 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500193 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100194 , mFunctorManager(WebViewFunctorManager::instance())
195 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700196 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800197}
198
199RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800200 // Note that if this fatal assertion is removed then member variables must
201 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700202 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800203}
204
Alec Mouri4a818f12019-11-19 16:17:53 -0800205void RenderThread::initializeChoreographer() {
206 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700207
John Reck56428472018-03-16 17:27:17 -0700208 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800209 mChoreographer = AChoreographer_create();
210 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
211 AChoreographer_registerRefreshRateCallback(mChoreographer,
212 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700213
214 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800215 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
216 RenderThread::choreographerCallback, this);
217 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700218 } else {
219 mVsyncSource = new DummyVsyncSource(this);
220 }
John Recke45b1fd2014-04-15 09:50:16 -0700221}
222
John Reck3b202512014-06-23 13:13:08 -0700223void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700224 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800225 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700226 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700227 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400228 mVkManager = VulkanManager::getInstance();
Alec Mouri22d753f2019-09-05 17:11:45 -0700229 mCacheManager = new CacheManager();
John Reckcf185f52019-04-11 16:11:24 -0700230}
231
232void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800233 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700234 mTimeLord.setFrameInterval(frameIntervalNanos);
235 mDispatchFrameDelay = static_cast<nsecs_t>(frameIntervalNanos * .25f);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400236}
237
John Reck1e510712018-04-23 08:15:03 -0700238void RenderThread::requireGlContext() {
239 if (mEglManager->hasEglContext()) {
240 return;
241 }
242 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700243
John Reckb5fc2092018-04-30 14:43:22 -0700244 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
John Reckb5fc2092018-04-30 14:43:22 -0700245 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700246
John Reckb5fc2092018-04-30 14:43:22 -0700247 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500248 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800249 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
250 auto size = glesVersion ? strlen(glesVersion) : -1;
251 cacheManager().configureContext(&options, glesVersion, size);
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400252 sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700253 LOG_ALWAYS_FATAL_IF(!grContext.get());
254 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700255}
256
Stan Iliev981afe72019-02-13 14:24:33 -0500257void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400258 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800259 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
260 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
261 // in the rendering thread.
262 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500263 return;
264 }
265 mVkManager->initialize();
266 GrContextOptions options;
267 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400268 auto vkDriverVersion = mVkManager->getDriverVersion();
269 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400270 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500271 LOG_ALWAYS_FATAL_IF(!grContext.get());
272 setGrContext(grContext);
273}
274
275void RenderThread::initGrContextOptions(GrContextOptions& options) {
276 options.fPreferExternalImagesOverES3 = true;
277 options.fDisableDistanceFieldPaths = true;
Adlai Holler108be5b2021-04-20 14:37:29 +0000278 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400279 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
280 } else {
281 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
282 }
Stan Iliev981afe72019-02-13 14:24:33 -0500283}
284
John Reck283bb462018-12-13 16:40:14 -0800285void RenderThread::destroyRenderingContext() {
286 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500287 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
288 if (mEglManager->hasEglContext()) {
289 setGrContext(nullptr);
290 mEglManager->destroy();
291 }
292 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400293 setGrContext(nullptr);
294 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700295 }
296}
297
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400298VulkanManager& RenderThread::vulkanManager() {
299 if (!mVkManager.get()) {
300 mVkManager = VulkanManager::getInstance();
301 }
302 return *mVkManager.get();
303}
304
John Reck66e06d42021-05-11 17:04:54 -0400305static const char* pipelineToString() {
306 switch (auto renderType = Properties::getRenderPipelineType()) {
307 case RenderPipelineType::SkiaGL:
308 return "Skia (OpenGL)";
309 case RenderPipelineType::SkiaVulkan:
310 return "Skia (Vulkan)";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400311 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700312 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
John Reck66e06d42021-05-11 17:04:54 -0400313 }
314}
315
316void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
317 if (includeProfileData) {
318 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400319 }
320
John Reck66e06d42021-05-11 17:04:54 -0400321 String8 cachesOutput;
322 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
323 dprintf(fd, "\nPipeline=%s\n%s\n", pipelineToString(), cachesOutput.string());
John Reck3b202512014-06-23 13:13:08 -0700324}
325
John Reck39207682021-05-12 19:10:47 -0400326void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
327 mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
328}
329
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500330Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500331 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400332 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500333 }
334
335 return *mReadback;
336}
337
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400338void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400339 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500340 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400341 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400342 mGrContext->releaseResourcesAndAbandonContext();
343 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500344 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400345 if (mGrContext) {
346 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
347 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400348}
349
Alec Mouri4a818f12019-11-19 16:17:53 -0800350int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700351 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
352 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700353 "events=0x%x",
354 events);
355 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700356 }
357
358 if (!(events & Looper::EVENT_INPUT)) {
359 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700360 "events=0x%x",
361 events);
362 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700363 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800364 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
365 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700366
Alec Mouri4a818f12019-11-19 16:17:53 -0800367 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700368}
369
370void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700371 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700372 mFrameCallbackTaskPending = false;
373
374 std::set<IFrameCallback*> callbacks;
375 mFrameCallbacks.swap(callbacks);
376
John Recka733f892014-12-19 11:37:21 -0800377 if (callbacks.size()) {
378 // Assume one of them will probably animate again so preemptively
379 // request the next vsync in case it occurs mid-frame
380 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700381 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
382 it++) {
John Recka733f892014-12-19 11:37:21 -0800383 (*it)->doFrame();
384 }
John Recke45b1fd2014-04-15 09:50:16 -0700385 }
386}
387
John Recka5dda642014-05-22 15:43:54 -0700388void RenderThread::requestVsync() {
389 if (!mVsyncRequested) {
390 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700391 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700392 }
393}
394
John Reckcec24ae2013-11-05 13:27:50 -0800395bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700396 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800397 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800398 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500399 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800400 }
John Reck3b202512014-06-23 13:13:08 -0700401 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700402
John Reckf8441e62017-10-23 13:10:41 -0700403 while (true) {
404 waitForWork();
405 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700406
407 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800408 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700409 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
410 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700411 mPendingRegistrationFrameCallbacks.clear();
412 requestVsync();
413 }
John Recka22c9b22015-01-14 10:40:15 -0800414
415 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
416 // TODO: Clean this up. This is working around an issue where a combination
417 // of bad timing and slow drawing can result in dropping a stale vsync
418 // on the floor (correct!) but fails to schedule to listen for the
419 // next vsync (oops), so none of the callbacks are run.
420 requestVsync();
421 }
John Reckcec24ae2013-11-05 13:27:50 -0800422 }
423
424 return false;
425}
426
John Recke45b1fd2014-04-15 09:50:16 -0700427void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700428 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700429}
430
John Reck01a5ea32014-12-03 13:01:07 -0800431bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
432 size_t erased;
433 erased = mFrameCallbacks.erase(callback);
434 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
435 return erased;
John Recka5dda642014-05-22 15:43:54 -0700436}
437
438void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
439 if (mFrameCallbacks.erase(callback)) {
440 mPendingRegistrationFrameCallbacks.insert(callback);
441 }
John Recke45b1fd2014-04-15 09:50:16 -0700442}
443
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400444sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
445 auto renderType = Properties::getRenderPipelineType();
446 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400447 case RenderPipelineType::SkiaVulkan:
448 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
449 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700450 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400451 break;
452 }
453 return nullptr;
454}
455
Stan Iliev6b894d72017-08-23 12:41:41 -0400456bool RenderThread::isCurrent() {
457 return gettid() == getInstance().getTid();
458}
459
Stan Iliev898123b2019-02-14 14:57:44 -0500460void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400461 // EGL driver is always preloaded only if HWUI renders with GL.
462 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700463 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400464 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000465 } else {
466 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500467 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000468 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500469}
470
John Reckcec24ae2013-11-05 13:27:50 -0800471} /* namespace renderthread */
472} /* namespace uirenderer */
473} /* namespace android */