blob: c7400743a14b4a308169c7562006191e248d9b7b [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);
Rachel Lee5ad064f2022-01-14 15:37:02 -0800120 AVsyncId vsyncId = AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee432b6912021-09-01 13:21:44 -0700121 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) {
Rachel Lee432b6912021-09-01 13:21:44 -0700136 mFrameCallbackTaskPending = true;
Carlos Martinez Romerod7971e72023-02-27 23:51:52 +0000137
ramindani9b641682023-05-09 15:01:48 -0700138 using SteadyClock = std::chrono::steady_clock;
139 using Nanos = std::chrono::nanoseconds;
140 using toNsecs_t = std::chrono::duration<nsecs_t, std::nano>;
141 using toFloatMillis = std::chrono::duration<float, std::milli>;
142
143 const auto frameTimeTimePoint = SteadyClock::time_point(Nanos(frameTimeNanos));
144 const auto deadlineTimePoint = SteadyClock::time_point(Nanos(frameDeadline));
145
146 const auto timeUntilDeadline = deadlineTimePoint - frameTimeTimePoint;
147 const auto runAt = (frameTimeTimePoint + (timeUntilDeadline / 4));
148
149 ATRACE_FORMAT("queue mFrameCallbackTask to run after %.2fms",
150 toFloatMillis(runAt - SteadyClock::now()).count());
151 queue().postAt(toNsecs_t(runAt.time_since_epoch()).count(),
152 [=]() { dispatchFrameCallbacks(); });
Alec Mouri4a818f12019-11-19 16:17:53 -0800153 }
154}
155
156void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
157 ATRACE_NAME("refreshRateCallback");
158 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
159 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
160 rt->setupFrameInterval();
161}
162
163class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700164public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800165 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700166
167 virtual void requestNextVsync() override {
Rachel Lee1a44c622022-02-15 18:12:01 -0800168 AChoreographer_postVsyncCallback(mRenderThread->mChoreographer,
169 RenderThread::extendedFrameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700170 }
171
Alec Mouri4a818f12019-11-19 16:17:53 -0800172 virtual void drainPendingEvents() override {
173 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700174 }
175
176private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800177 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700178};
179
180class DummyVsyncSource : public VsyncSource {
181public:
182 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
183
184 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800185 mRenderThread->queue().postDelayed(16_ms, [this]() {
Rachel Lee432b6912021-09-01 13:21:44 -0700186 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
187 std::numeric_limits<int64_t>::max(),
188 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800189 });
John Reck56428472018-03-16 17:27:17 -0700190 }
191
Alec Mouri4a818f12019-11-19 16:17:53 -0800192 virtual void drainPendingEvents() override {
Rachel Lee432b6912021-09-01 13:21:44 -0700193 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
194 std::numeric_limits<int64_t>::max(),
195 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800196 }
John Reck56428472018-03-16 17:27:17 -0700197
198private:
199 RenderThread* mRenderThread;
200};
201
John Reck6b507802015-11-03 10:09:59 -0800202bool RenderThread::hasInstance() {
203 return gHasRenderThreadInstance;
204}
205
Stan Iliev80dbc352019-02-05 15:31:28 -0500206void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800207 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
208 gOnStartHook = onStartHook;
209}
210
Stan Iliev80dbc352019-02-05 15:31:28 -0500211JVMAttachHook RenderThread::getOnStartHook() {
212 return gOnStartHook;
213}
214
John Reck6b507802015-11-03 10:09:59 -0800215RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000216 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
217 sp<RenderThread> thread = sp<RenderThread>::make();
218 thread->start("RenderThread");
219 return thread;
220 }();
John Reck6b507802015-11-03 10:09:59 -0800221 gHasRenderThreadInstance = true;
222 return *sInstance;
223}
224
John Reck1bcacfd2017-11-03 10:12:19 -0700225RenderThread::RenderThread()
226 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700227 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700228 , mVsyncRequested(false)
229 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800230 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500231 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100232 , mFunctorManager(WebViewFunctorManager::instance())
233 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700234 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800235}
236
237RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800238 // Note that if this fatal assertion is removed then member variables must
239 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700240 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800241}
242
Alec Mouri4a818f12019-11-19 16:17:53 -0800243void RenderThread::initializeChoreographer() {
244 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700245
John Reck56428472018-03-16 17:27:17 -0700246 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800247 mChoreographer = AChoreographer_create();
248 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
249 AChoreographer_registerRefreshRateCallback(mChoreographer,
250 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700251
252 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800253 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
254 RenderThread::choreographerCallback, this);
255 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700256 } else {
257 mVsyncSource = new DummyVsyncSource(this);
258 }
John Recke45b1fd2014-04-15 09:50:16 -0700259}
260
John Reck3b202512014-06-23 13:13:08 -0700261void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700262 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800263 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700264 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700265 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400266 mVkManager = VulkanManager::getInstance();
John Reck5f66fb82022-09-23 17:49:23 -0400267 mCacheManager = new CacheManager(*this);
John Reckcf185f52019-04-11 16:11:24 -0700268}
269
270void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800271 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700272 mTimeLord.setFrameInterval(frameIntervalNanos);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400273}
274
John Reck1e510712018-04-23 08:15:03 -0700275void RenderThread::requireGlContext() {
276 if (mEglManager->hasEglContext()) {
277 return;
278 }
279 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700280
Brian Salomoncec2eda2022-03-22 20:12:55 +0000281 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
John Reckb5fc2092018-04-30 14:43:22 -0700282 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700283
John Reckb5fc2092018-04-30 14:43:22 -0700284 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500285 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800286 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
287 auto size = glesVersion ? strlen(glesVersion) : -1;
288 cacheManager().configureContext(&options, glesVersion, size);
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400289 sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700290 LOG_ALWAYS_FATAL_IF(!grContext.get());
291 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700292}
293
Stan Iliev981afe72019-02-13 14:24:33 -0500294void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400295 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800296 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
297 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
298 // in the rendering thread.
299 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500300 return;
301 }
302 mVkManager->initialize();
303 GrContextOptions options;
304 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400305 auto vkDriverVersion = mVkManager->getDriverVersion();
306 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400307 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500308 LOG_ALWAYS_FATAL_IF(!grContext.get());
309 setGrContext(grContext);
310}
311
312void RenderThread::initGrContextOptions(GrContextOptions& options) {
313 options.fPreferExternalImagesOverES3 = true;
314 options.fDisableDistanceFieldPaths = true;
Adlai Holler108be5b2021-04-20 14:37:29 +0000315 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400316 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
317 } else {
318 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
319 }
Stan Iliev981afe72019-02-13 14:24:33 -0500320}
321
John Reck283bb462018-12-13 16:40:14 -0800322void RenderThread::destroyRenderingContext() {
323 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500324 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
325 if (mEglManager->hasEglContext()) {
326 setGrContext(nullptr);
327 mEglManager->destroy();
328 }
329 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400330 setGrContext(nullptr);
331 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700332 }
333}
334
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400335VulkanManager& RenderThread::vulkanManager() {
336 if (!mVkManager.get()) {
337 mVkManager = VulkanManager::getInstance();
338 }
339 return *mVkManager.get();
340}
341
John Reck66e06d42021-05-11 17:04:54 -0400342static const char* pipelineToString() {
343 switch (auto renderType = Properties::getRenderPipelineType()) {
344 case RenderPipelineType::SkiaGL:
345 return "Skia (OpenGL)";
346 case RenderPipelineType::SkiaVulkan:
347 return "Skia (Vulkan)";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400348 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700349 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
John Reck66e06d42021-05-11 17:04:54 -0400350 }
351}
352
353void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
354 if (includeProfileData) {
355 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400356 }
357
John Reck66e06d42021-05-11 17:04:54 -0400358 String8 cachesOutput;
359 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
John Reck07f3d912023-08-17 12:46:44 -0400360 dprintf(fd, "\nPipeline=%s\n%s", pipelineToString(), cachesOutput.string());
361 for (auto&& context : mCacheManager->mCanvasContexts) {
362 context->visitAllRenderNodes([&](const RenderNode& node) {
363 if (node.isTextureView()) {
364 dprintf(fd, "TextureView: %dx%d\n", node.getWidth(), node.getHeight());
365 }
366 });
367 }
368 dprintf(fd, "\n");
John Reck3b202512014-06-23 13:13:08 -0700369}
370
John Reck39207682021-05-12 19:10:47 -0400371void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
372 mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
373}
374
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500375Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500376 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400377 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500378 }
379
380 return *mReadback;
381}
382
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400383void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400384 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500385 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400386 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400387 mGrContext->releaseResourcesAndAbandonContext();
388 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500389 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400390 if (mGrContext) {
391 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
392 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400393}
394
John Reck2abc5492021-05-18 00:34:26 -0400395sk_sp<GrDirectContext> RenderThread::requireGrContext() {
396 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
397 requireGlContext();
398 } else {
399 requireVkContext();
400 }
401 return mGrContext;
402}
403
Alec Mouri4a818f12019-11-19 16:17:53 -0800404int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700405 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
406 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700407 "events=0x%x",
408 events);
409 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700410 }
411
412 if (!(events & Looper::EVENT_INPUT)) {
413 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700414 "events=0x%x",
415 events);
416 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700417 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800418 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
419 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700420
Alec Mouri4a818f12019-11-19 16:17:53 -0800421 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700422}
423
424void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700425 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700426 mFrameCallbackTaskPending = false;
427
428 std::set<IFrameCallback*> callbacks;
429 mFrameCallbacks.swap(callbacks);
430
John Recka733f892014-12-19 11:37:21 -0800431 if (callbacks.size()) {
432 // Assume one of them will probably animate again so preemptively
433 // request the next vsync in case it occurs mid-frame
434 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700435 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
436 it++) {
John Recka733f892014-12-19 11:37:21 -0800437 (*it)->doFrame();
438 }
John Recke45b1fd2014-04-15 09:50:16 -0700439 }
440}
441
John Recka5dda642014-05-22 15:43:54 -0700442void RenderThread::requestVsync() {
443 if (!mVsyncRequested) {
444 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700445 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700446 }
447}
448
John Reckcec24ae2013-11-05 13:27:50 -0800449bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700450 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800451 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800452 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500453 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800454 }
John Reck3b202512014-06-23 13:13:08 -0700455 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700456
John Reckf8441e62017-10-23 13:10:41 -0700457 while (true) {
458 waitForWork();
459 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700460
461 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800462 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700463 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
464 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700465 mPendingRegistrationFrameCallbacks.clear();
466 requestVsync();
467 }
John Recka22c9b22015-01-14 10:40:15 -0800468
469 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
470 // TODO: Clean this up. This is working around an issue where a combination
471 // of bad timing and slow drawing can result in dropping a stale vsync
472 // on the floor (correct!) but fails to schedule to listen for the
473 // next vsync (oops), so none of the callbacks are run.
474 requestVsync();
475 }
John Reck5f66fb82022-09-23 17:49:23 -0400476
477 mCacheManager->onThreadIdle();
John Reckcec24ae2013-11-05 13:27:50 -0800478 }
479
480 return false;
481}
482
John Recke45b1fd2014-04-15 09:50:16 -0700483void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700484 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700485}
486
John Reck01a5ea32014-12-03 13:01:07 -0800487bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
488 size_t erased;
489 erased = mFrameCallbacks.erase(callback);
490 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
491 return erased;
John Recka5dda642014-05-22 15:43:54 -0700492}
493
494void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
495 if (mFrameCallbacks.erase(callback)) {
496 mPendingRegistrationFrameCallbacks.insert(callback);
497 }
John Recke45b1fd2014-04-15 09:50:16 -0700498}
499
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400500sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
501 auto renderType = Properties::getRenderPipelineType();
502 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400503 case RenderPipelineType::SkiaVulkan:
504 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
505 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700506 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400507 break;
508 }
509 return nullptr;
510}
511
Stan Iliev6b894d72017-08-23 12:41:41 -0400512bool RenderThread::isCurrent() {
513 return gettid() == getInstance().getTid();
514}
515
Stan Iliev898123b2019-02-14 14:57:44 -0500516void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400517 // EGL driver is always preloaded only if HWUI renders with GL.
518 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700519 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400520 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000521 } else {
522 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500523 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000524 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500525}
526
John Reck5f66fb82022-09-23 17:49:23 -0400527void RenderThread::trimMemory(TrimLevel level) {
528 ATRACE_CALL();
529 cacheManager().trimMemory(level);
530}
531
Jernej Virag44db0402023-05-09 20:24:48 +0200532void RenderThread::trimCaches(CacheTrimLevel level) {
533 ATRACE_CALL();
534 cacheManager().trimCaches(level);
535}
536
John Reckcec24ae2013-11-05 13:27:50 -0800537} /* namespace renderthread */
538} /* namespace uirenderer */
539} /* namespace android */