blob: d86237789b111c573453b36edb488f7729173a9d [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
Rachel Lee432b6912021-09-01 13:21:44 -070019#include <GrContextOptions.h>
20#include <android-base/properties.h>
21#include <dlfcn.h>
22#include <gl/GrGLInterface.h>
rnleece9762b2021-05-21 15:40:53 -070023#include <gui/TraceUtils.h>
Rachel Lee432b6912021-09-01 13:21:44 -070024#include <sys/resource.h>
25#include <ui/FatVector.h>
26#include <utils/Condition.h>
27#include <utils/Log.h>
28#include <utils/Mutex.h>
29
30#include <thread>
31
John Reck0fa0cbc2019-04-05 16:57:46 -070032#include "../HardwareBitmapUploader.h"
Rachel Lee432b6912021-09-01 13:21:44 -070033#include "CacheManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080034#include "CanvasContext.h"
John Reck56428472018-03-16 17:27:17 -070035#include "DeviceInfo.h"
John Reck3b202512014-06-23 13:13:08 -070036#include "EglManager.h"
Adlai Holler5636cde2021-03-25 16:53:56 +000037#include "Properties.h"
Stan Iliev1a025a72018-09-05 16:35:11 -040038#include "Readback.h"
John Reck4f02bf42014-01-03 18:09:17 -080039#include "RenderProxy.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050040#include "VulkanManager.h"
John Reck1bcacfd2017-11-03 10:12:19 -070041#include "hwui/Bitmap.h"
42#include "pipeline/skia/SkiaOpenGLPipeline.h"
John Reck1a4a9812018-04-18 16:13:31 -070043#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reck1bcacfd2017-11-03 10:12:19 -070044#include "renderstate/RenderState.h"
John Reck56428472018-03-16 17:27:17 -070045#include "utils/TimeUtils.h"
John Reckcec24ae2013-11-05 13:27:50 -080046
47namespace 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
Huihong Luo540fdf82021-06-25 13:59:39 -0700101 transactionReparentFunc = (AST_reparent)dlsym(handle_, "ASurfaceTransaction_reparent");
102 LOG_ALWAYS_FATAL_IF(transactionReparentFunc == nullptr,
103 "Failed to find required symbol transactionReparentFunc!");
104
Huihong Luo054b8d32021-02-24 18:48:12 -0800105 transactionSetVisibilityFunc =
106 (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility");
107 LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr,
108 "Failed to find required symbol ASurfaceTransaction_setVisibility!");
Huihong Luo34f42fd2021-05-03 14:47:36 -0700109
110 transactionSetZOrderFunc = (AST_setZOrder)dlsym(handle_, "ASurfaceTransaction_setZOrder");
111 LOG_ALWAYS_FATAL_IF(transactionSetZOrderFunc == nullptr,
112 "Failed to find required symbol ASurfaceTransaction_setZOrder!");
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800113}
114
Rachel Lee432b6912021-09-01 13:21:44 -0700115void RenderThread::extendedFrameCallback(const AChoreographerFrameCallbackData* cbData,
116 void* data) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800117 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
Rachel Lee432b6912021-09-01 13:21:44 -0700118 size_t preferredFrameTimelineIndex =
119 AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(cbData);
120 int64_t vsyncId = AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
121 cbData, preferredFrameTimelineIndex);
Rachel Lee3712a2a2022-01-12 17:38:52 -0800122 int64_t frameDeadline = AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee432b6912021-09-01 13:21:44 -0700123 cbData, preferredFrameTimelineIndex);
124 int64_t frameTimeNanos = AChoreographerFrameCallbackData_getFrameTimeNanos(cbData);
125 // TODO(b/193273294): Remove when shared memory in use w/ expected present time always current.
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100126 int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer);
Rachel Lee432b6912021-09-01 13:21:44 -0700127 rt->frameCallback(vsyncId, frameDeadline, frameTimeNanos, frameInterval);
128}
129
130void RenderThread::frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
131 int64_t frameInterval) {
132 mVsyncRequested = false;
133 if (timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline,
134 frameInterval) &&
135 !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800136 ATRACE_NAME("queue mFrameCallbackTask");
Rachel Lee432b6912021-09-01 13:21:44 -0700137 mFrameCallbackTaskPending = true;
138 nsecs_t runAt = (frameTimeNanos + mDispatchFrameDelay);
139 queue().postAt(runAt, [=]() { dispatchFrameCallbacks(); });
Alec Mouri4a818f12019-11-19 16:17:53 -0800140 }
141}
142
143void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
144 ATRACE_NAME("refreshRateCallback");
145 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
146 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
147 rt->setupFrameInterval();
148}
149
150class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700151public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800152 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700153
154 virtual void requestNextVsync() override {
Rachel Lee432b6912021-09-01 13:21:44 -0700155 AChoreographer_postExtendedFrameCallback(
156 mRenderThread->mChoreographer, RenderThread::extendedFrameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700157 }
158
Alec Mouri4a818f12019-11-19 16:17:53 -0800159 virtual void drainPendingEvents() override {
160 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700161 }
162
163private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800164 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700165};
166
167class DummyVsyncSource : public VsyncSource {
168public:
169 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
170
171 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800172 mRenderThread->queue().postDelayed(16_ms, [this]() {
Rachel Lee432b6912021-09-01 13:21:44 -0700173 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
174 std::numeric_limits<int64_t>::max(),
175 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800176 });
John Reck56428472018-03-16 17:27:17 -0700177 }
178
Alec Mouri4a818f12019-11-19 16:17:53 -0800179 virtual void drainPendingEvents() override {
Rachel Lee432b6912021-09-01 13:21:44 -0700180 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
181 std::numeric_limits<int64_t>::max(),
182 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800183 }
John Reck56428472018-03-16 17:27:17 -0700184
185private:
186 RenderThread* mRenderThread;
187};
188
John Reck6b507802015-11-03 10:09:59 -0800189bool RenderThread::hasInstance() {
190 return gHasRenderThreadInstance;
191}
192
Stan Iliev80dbc352019-02-05 15:31:28 -0500193void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800194 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
195 gOnStartHook = onStartHook;
196}
197
Stan Iliev80dbc352019-02-05 15:31:28 -0500198JVMAttachHook RenderThread::getOnStartHook() {
199 return gOnStartHook;
200}
201
John Reck6b507802015-11-03 10:09:59 -0800202RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000203 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
204 sp<RenderThread> thread = sp<RenderThread>::make();
205 thread->start("RenderThread");
206 return thread;
207 }();
John Reck6b507802015-11-03 10:09:59 -0800208 gHasRenderThreadInstance = true;
209 return *sInstance;
210}
211
John Reck1bcacfd2017-11-03 10:12:19 -0700212RenderThread::RenderThread()
213 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700214 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700215 , mVsyncRequested(false)
216 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800217 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500218 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100219 , mFunctorManager(WebViewFunctorManager::instance())
220 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700221 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800222}
223
224RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800225 // Note that if this fatal assertion is removed then member variables must
226 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700227 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800228}
229
Alec Mouri4a818f12019-11-19 16:17:53 -0800230void RenderThread::initializeChoreographer() {
231 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700232
John Reck56428472018-03-16 17:27:17 -0700233 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800234 mChoreographer = AChoreographer_create();
235 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
236 AChoreographer_registerRefreshRateCallback(mChoreographer,
237 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700238
239 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800240 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
241 RenderThread::choreographerCallback, this);
242 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700243 } else {
244 mVsyncSource = new DummyVsyncSource(this);
245 }
John Recke45b1fd2014-04-15 09:50:16 -0700246}
247
John Reck3b202512014-06-23 13:13:08 -0700248void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700249 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800250 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700251 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700252 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400253 mVkManager = VulkanManager::getInstance();
Alec Mouri22d753f2019-09-05 17:11:45 -0700254 mCacheManager = new CacheManager();
John Reckcf185f52019-04-11 16:11:24 -0700255}
256
257void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800258 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700259 mTimeLord.setFrameInterval(frameIntervalNanos);
260 mDispatchFrameDelay = static_cast<nsecs_t>(frameIntervalNanos * .25f);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400261}
262
John Reck1e510712018-04-23 08:15:03 -0700263void RenderThread::requireGlContext() {
264 if (mEglManager->hasEglContext()) {
265 return;
266 }
267 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700268
John Reckb5fc2092018-04-30 14:43:22 -0700269 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
John Reckb5fc2092018-04-30 14:43:22 -0700270 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700271
John Reckb5fc2092018-04-30 14:43:22 -0700272 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500273 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800274 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
275 auto size = glesVersion ? strlen(glesVersion) : -1;
276 cacheManager().configureContext(&options, glesVersion, size);
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400277 sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700278 LOG_ALWAYS_FATAL_IF(!grContext.get());
279 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700280}
281
Stan Iliev981afe72019-02-13 14:24:33 -0500282void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400283 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800284 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
285 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
286 // in the rendering thread.
287 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500288 return;
289 }
290 mVkManager->initialize();
291 GrContextOptions options;
292 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400293 auto vkDriverVersion = mVkManager->getDriverVersion();
294 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400295 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500296 LOG_ALWAYS_FATAL_IF(!grContext.get());
297 setGrContext(grContext);
298}
299
300void RenderThread::initGrContextOptions(GrContextOptions& options) {
301 options.fPreferExternalImagesOverES3 = true;
302 options.fDisableDistanceFieldPaths = true;
Adlai Holler108be5b2021-04-20 14:37:29 +0000303 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400304 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
305 } else {
306 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
307 }
Stan Iliev981afe72019-02-13 14:24:33 -0500308}
309
John Reck283bb462018-12-13 16:40:14 -0800310void RenderThread::destroyRenderingContext() {
311 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500312 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
313 if (mEglManager->hasEglContext()) {
314 setGrContext(nullptr);
315 mEglManager->destroy();
316 }
317 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400318 setGrContext(nullptr);
319 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700320 }
321}
322
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400323VulkanManager& RenderThread::vulkanManager() {
324 if (!mVkManager.get()) {
325 mVkManager = VulkanManager::getInstance();
326 }
327 return *mVkManager.get();
328}
329
John Reck66e06d42021-05-11 17:04:54 -0400330static const char* pipelineToString() {
331 switch (auto renderType = Properties::getRenderPipelineType()) {
332 case RenderPipelineType::SkiaGL:
333 return "Skia (OpenGL)";
334 case RenderPipelineType::SkiaVulkan:
335 return "Skia (Vulkan)";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400336 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700337 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
John Reck66e06d42021-05-11 17:04:54 -0400338 }
339}
340
341void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
342 if (includeProfileData) {
343 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400344 }
345
John Reck66e06d42021-05-11 17:04:54 -0400346 String8 cachesOutput;
347 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
348 dprintf(fd, "\nPipeline=%s\n%s\n", pipelineToString(), cachesOutput.string());
John Reck3b202512014-06-23 13:13:08 -0700349}
350
John Reck39207682021-05-12 19:10:47 -0400351void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
352 mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
353}
354
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500355Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500356 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400357 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500358 }
359
360 return *mReadback;
361}
362
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400363void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400364 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500365 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400366 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400367 mGrContext->releaseResourcesAndAbandonContext();
368 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500369 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400370 if (mGrContext) {
371 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
372 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400373}
374
John Reck2abc5492021-05-18 00:34:26 -0400375sk_sp<GrDirectContext> RenderThread::requireGrContext() {
376 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
377 requireGlContext();
378 } else {
379 requireVkContext();
380 }
381 return mGrContext;
382}
383
Alec Mouri4a818f12019-11-19 16:17:53 -0800384int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700385 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
386 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700387 "events=0x%x",
388 events);
389 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700390 }
391
392 if (!(events & Looper::EVENT_INPUT)) {
393 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700394 "events=0x%x",
395 events);
396 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700397 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800398 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
399 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700400
Alec Mouri4a818f12019-11-19 16:17:53 -0800401 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700402}
403
404void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700405 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700406 mFrameCallbackTaskPending = false;
407
408 std::set<IFrameCallback*> callbacks;
409 mFrameCallbacks.swap(callbacks);
410
John Recka733f892014-12-19 11:37:21 -0800411 if (callbacks.size()) {
412 // Assume one of them will probably animate again so preemptively
413 // request the next vsync in case it occurs mid-frame
414 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700415 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
416 it++) {
John Recka733f892014-12-19 11:37:21 -0800417 (*it)->doFrame();
418 }
John Recke45b1fd2014-04-15 09:50:16 -0700419 }
420}
421
John Recka5dda642014-05-22 15:43:54 -0700422void RenderThread::requestVsync() {
423 if (!mVsyncRequested) {
424 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700425 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700426 }
427}
428
John Reckcec24ae2013-11-05 13:27:50 -0800429bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700430 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800431 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800432 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500433 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800434 }
John Reck3b202512014-06-23 13:13:08 -0700435 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700436
John Reckf8441e62017-10-23 13:10:41 -0700437 while (true) {
438 waitForWork();
439 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700440
441 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800442 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700443 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
444 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700445 mPendingRegistrationFrameCallbacks.clear();
446 requestVsync();
447 }
John Recka22c9b22015-01-14 10:40:15 -0800448
449 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
450 // TODO: Clean this up. This is working around an issue where a combination
451 // of bad timing and slow drawing can result in dropping a stale vsync
452 // on the floor (correct!) but fails to schedule to listen for the
453 // next vsync (oops), so none of the callbacks are run.
454 requestVsync();
455 }
John Reckcec24ae2013-11-05 13:27:50 -0800456 }
457
458 return false;
459}
460
John Recke45b1fd2014-04-15 09:50:16 -0700461void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700462 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700463}
464
John Reck01a5ea32014-12-03 13:01:07 -0800465bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
466 size_t erased;
467 erased = mFrameCallbacks.erase(callback);
468 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
469 return erased;
John Recka5dda642014-05-22 15:43:54 -0700470}
471
472void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
473 if (mFrameCallbacks.erase(callback)) {
474 mPendingRegistrationFrameCallbacks.insert(callback);
475 }
John Recke45b1fd2014-04-15 09:50:16 -0700476}
477
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400478sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
479 auto renderType = Properties::getRenderPipelineType();
480 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400481 case RenderPipelineType::SkiaVulkan:
482 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
483 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700484 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400485 break;
486 }
487 return nullptr;
488}
489
Stan Iliev6b894d72017-08-23 12:41:41 -0400490bool RenderThread::isCurrent() {
491 return gettid() == getInstance().getTid();
492}
493
Stan Iliev898123b2019-02-14 14:57:44 -0500494void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400495 // EGL driver is always preloaded only if HWUI renders with GL.
496 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700497 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400498 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000499 } else {
500 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500501 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000502 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500503}
504
John Reckcec24ae2013-11-05 13:27:50 -0800505} /* namespace renderthread */
506} /* namespace uirenderer */
507} /* namespace android */