blob: 92c6ad10d1c7a5d6375fb7cb2dd384aca3732170 [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 <android-base/properties.h>
20#include <dlfcn.h>
rnleece9762b2021-05-21 15:40:53 -070021#include <gui/TraceUtils.h>
Nolan Scobie572d8012024-08-30 13:43:34 -040022#include <include/gpu/ganesh/GrContextOptions.h>
Jerome Gaillard87ef5242024-02-13 16:33:00 +000023#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Nolan Scobie572d8012024-08-30 13:43:34 -040024#include <include/gpu/ganesh/gl/GrGLInterface.h>
Jerome Gaillard87ef5242024-02-13 16:33:00 +000025#include <private/android/choreographer.h>
Rachel Lee432b6912021-09-01 13:21:44 -070026#include <sys/resource.h>
27#include <ui/FatVector.h>
28#include <utils/Condition.h>
29#include <utils/Log.h>
30#include <utils/Mutex.h>
31
32#include <thread>
33
John Reck0fa0cbc2019-04-05 16:57:46 -070034#include "../HardwareBitmapUploader.h"
Rachel Lee432b6912021-09-01 13:21:44 -070035#include "CacheManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080036#include "CanvasContext.h"
John Reck56428472018-03-16 17:27:17 -070037#include "DeviceInfo.h"
John Reck3b202512014-06-23 13:13:08 -070038#include "EglManager.h"
Adlai Holler5636cde2021-03-25 16:53:56 +000039#include "Properties.h"
Stan Iliev1a025a72018-09-05 16:35:11 -040040#include "Readback.h"
John Reck4f02bf42014-01-03 18:09:17 -080041#include "RenderProxy.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042#include "VulkanManager.h"
John Reck1bcacfd2017-11-03 10:12:19 -070043#include "hwui/Bitmap.h"
44#include "pipeline/skia/SkiaOpenGLPipeline.h"
John Reck1a4a9812018-04-18 16:13:31 -070045#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reck1bcacfd2017-11-03 10:12:19 -070046#include "renderstate/RenderState.h"
John Reck56428472018-03-16 17:27:17 -070047#include "utils/TimeUtils.h"
John Reckcec24ae2013-11-05 13:27:50 -080048
49namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080050namespace uirenderer {
51namespace renderthread {
52
John Reck6b507802015-11-03 10:09:59 -080053static bool gHasRenderThreadInstance = false;
54
Stan Iliev80dbc352019-02-05 15:31:28 -050055static JVMAttachHook gOnStartHook = nullptr;
John Reck259b25a2017-12-01 16:18:53 -080056
Huihong Luo5fdf7b82021-01-15 14:27:06 -080057ASurfaceControlFunctions::ASurfaceControlFunctions() {
58 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
Huihong Luo054b8d32021-02-24 18:48:12 -080059 createFunc = (ASC_create)dlsym(handle_, "ASurfaceControl_create");
60 LOG_ALWAYS_FATAL_IF(createFunc == nullptr,
61 "Failed to find required symbol ASurfaceControl_create!");
62
Huihong Luo5fdf7b82021-01-15 14:27:06 -080063 acquireFunc = (ASC_acquire) dlsym(handle_, "ASurfaceControl_acquire");
64 LOG_ALWAYS_FATAL_IF(acquireFunc == nullptr,
65 "Failed to find required symbol ASurfaceControl_acquire!");
66
67 releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release");
68 LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr,
69 "Failed to find required symbol ASurfaceControl_release!");
Jorim Jaggi71db8892021-02-03 23:19:29 +010070
71 registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_,
72 "ASurfaceControl_registerSurfaceStatsListener");
73 LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr,
74 "Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!");
75
76 unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_,
77 "ASurfaceControl_unregisterSurfaceStatsListener");
78 LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr,
79 "Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!");
80
81 getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_,
82 "ASurfaceControlStats_getAcquireTime");
83 LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr,
84 "Failed to find required symbol ASurfaceControlStats_getAcquireTime!");
85
86 getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_,
87 "ASurfaceControlStats_getFrameNumber");
88 LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr,
89 "Failed to find required symbol ASurfaceControlStats_getFrameNumber!");
Huihong Luo054b8d32021-02-24 18:48:12 -080090
91 transactionCreateFunc = (AST_create)dlsym(handle_, "ASurfaceTransaction_create");
92 LOG_ALWAYS_FATAL_IF(transactionCreateFunc == nullptr,
93 "Failed to find required symbol ASurfaceTransaction_create!");
94
95 transactionDeleteFunc = (AST_delete)dlsym(handle_, "ASurfaceTransaction_delete");
96 LOG_ALWAYS_FATAL_IF(transactionDeleteFunc == nullptr,
97 "Failed to find required symbol ASurfaceTransaction_delete!");
98
99 transactionApplyFunc = (AST_apply)dlsym(handle_, "ASurfaceTransaction_apply");
100 LOG_ALWAYS_FATAL_IF(transactionApplyFunc == nullptr,
101 "Failed to find required symbol ASurfaceTransaction_apply!");
102
Huihong Luo540fdf82021-06-25 13:59:39 -0700103 transactionReparentFunc = (AST_reparent)dlsym(handle_, "ASurfaceTransaction_reparent");
104 LOG_ALWAYS_FATAL_IF(transactionReparentFunc == nullptr,
105 "Failed to find required symbol transactionReparentFunc!");
106
Huihong Luo054b8d32021-02-24 18:48:12 -0800107 transactionSetVisibilityFunc =
108 (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility");
109 LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr,
110 "Failed to find required symbol ASurfaceTransaction_setVisibility!");
Huihong Luo34f42fd2021-05-03 14:47:36 -0700111
112 transactionSetZOrderFunc = (AST_setZOrder)dlsym(handle_, "ASurfaceTransaction_setZOrder");
113 LOG_ALWAYS_FATAL_IF(transactionSetZOrderFunc == nullptr,
114 "Failed to find required symbol ASurfaceTransaction_setZOrder!");
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800115}
116
Rachel Lee432b6912021-09-01 13:21:44 -0700117void RenderThread::extendedFrameCallback(const AChoreographerFrameCallbackData* cbData,
118 void* data) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800119 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
Rachel Lee432b6912021-09-01 13:21:44 -0700120 size_t preferredFrameTimelineIndex =
121 AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(cbData);
Rachel Lee5ad064f2022-01-14 15:37:02 -0800122 AVsyncId vsyncId = AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee432b6912021-09-01 13:21:44 -0700123 cbData, preferredFrameTimelineIndex);
Rachel Lee3712a2a2022-01-12 17:38:52 -0800124 int64_t frameDeadline = AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee432b6912021-09-01 13:21:44 -0700125 cbData, preferredFrameTimelineIndex);
126 int64_t frameTimeNanos = AChoreographerFrameCallbackData_getFrameTimeNanos(cbData);
127 // TODO(b/193273294): Remove when shared memory in use w/ expected present time always current.
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100128 int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer);
Rachel Lee432b6912021-09-01 13:21:44 -0700129 rt->frameCallback(vsyncId, frameDeadline, frameTimeNanos, frameInterval);
130}
131
132void RenderThread::frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
133 int64_t frameInterval) {
134 mVsyncRequested = false;
135 if (timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline,
136 frameInterval) &&
137 !mFrameCallbackTaskPending) {
Rachel Lee432b6912021-09-01 13:21:44 -0700138 mFrameCallbackTaskPending = true;
Carlos Martinez Romerod7971e72023-02-27 23:51:52 +0000139
ramindani9b641682023-05-09 15:01:48 -0700140 using SteadyClock = std::chrono::steady_clock;
141 using Nanos = std::chrono::nanoseconds;
142 using toNsecs_t = std::chrono::duration<nsecs_t, std::nano>;
143 using toFloatMillis = std::chrono::duration<float, std::milli>;
144
145 const auto frameTimeTimePoint = SteadyClock::time_point(Nanos(frameTimeNanos));
146 const auto deadlineTimePoint = SteadyClock::time_point(Nanos(frameDeadline));
147
148 const auto timeUntilDeadline = deadlineTimePoint - frameTimeTimePoint;
149 const auto runAt = (frameTimeTimePoint + (timeUntilDeadline / 4));
150
151 ATRACE_FORMAT("queue mFrameCallbackTask to run after %.2fms",
152 toFloatMillis(runAt - SteadyClock::now()).count());
153 queue().postAt(toNsecs_t(runAt.time_since_epoch()).count(),
John Reck23750022023-11-21 18:30:35 -0500154 [this]() { dispatchFrameCallbacks(); });
Alec Mouri4a818f12019-11-19 16:17:53 -0800155 }
156}
157
158void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
159 ATRACE_NAME("refreshRateCallback");
160 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
161 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
162 rt->setupFrameInterval();
163}
164
165class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700166public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800167 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700168
169 virtual void requestNextVsync() override {
Rachel Lee1a44c622022-02-15 18:12:01 -0800170 AChoreographer_postVsyncCallback(mRenderThread->mChoreographer,
171 RenderThread::extendedFrameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700172 }
173
Alec Mouri4a818f12019-11-19 16:17:53 -0800174 virtual void drainPendingEvents() override {
175 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700176 }
177
178private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800179 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700180};
181
182class DummyVsyncSource : public VsyncSource {
183public:
184 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
185
186 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800187 mRenderThread->queue().postDelayed(16_ms, [this]() {
Rachel Lee432b6912021-09-01 13:21:44 -0700188 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
189 std::numeric_limits<int64_t>::max(),
190 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800191 });
John Reck56428472018-03-16 17:27:17 -0700192 }
193
Alec Mouri4a818f12019-11-19 16:17:53 -0800194 virtual void drainPendingEvents() override {
Rachel Lee432b6912021-09-01 13:21:44 -0700195 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
196 std::numeric_limits<int64_t>::max(),
197 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800198 }
John Reck56428472018-03-16 17:27:17 -0700199
200private:
201 RenderThread* mRenderThread;
202};
203
John Reck6b507802015-11-03 10:09:59 -0800204bool RenderThread::hasInstance() {
205 return gHasRenderThreadInstance;
206}
207
Stan Iliev80dbc352019-02-05 15:31:28 -0500208void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800209 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
210 gOnStartHook = onStartHook;
211}
212
Stan Iliev80dbc352019-02-05 15:31:28 -0500213JVMAttachHook RenderThread::getOnStartHook() {
214 return gOnStartHook;
215}
216
John Reck6b507802015-11-03 10:09:59 -0800217RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000218 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
219 sp<RenderThread> thread = sp<RenderThread>::make();
220 thread->start("RenderThread");
221 return thread;
222 }();
John Reck6b507802015-11-03 10:09:59 -0800223 gHasRenderThreadInstance = true;
224 return *sInstance;
225}
226
John Reck1bcacfd2017-11-03 10:12:19 -0700227RenderThread::RenderThread()
228 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700229 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700230 , mVsyncRequested(false)
231 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800232 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500233 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100234 , mFunctorManager(WebViewFunctorManager::instance())
235 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700236 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800237}
238
239RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800240 // Note that if this fatal assertion is removed then member variables must
241 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700242 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800243}
244
Alec Mouri4a818f12019-11-19 16:17:53 -0800245void RenderThread::initializeChoreographer() {
246 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700247
John Reck56428472018-03-16 17:27:17 -0700248 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800249 mChoreographer = AChoreographer_create();
250 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
251 AChoreographer_registerRefreshRateCallback(mChoreographer,
252 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700253
254 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800255 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
256 RenderThread::choreographerCallback, this);
257 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700258 } else {
259 mVsyncSource = new DummyVsyncSource(this);
260 }
John Recke45b1fd2014-04-15 09:50:16 -0700261}
262
John Reck3b202512014-06-23 13:13:08 -0700263void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700264 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800265 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700266 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700267 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400268 mVkManager = VulkanManager::getInstance();
John Reck5f66fb82022-09-23 17:49:23 -0400269 mCacheManager = new CacheManager(*this);
John Reckcf185f52019-04-11 16:11:24 -0700270}
271
272void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800273 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700274 mTimeLord.setFrameInterval(frameIntervalNanos);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400275}
276
John Reck1e510712018-04-23 08:15:03 -0700277void RenderThread::requireGlContext() {
278 if (mEglManager->hasEglContext()) {
279 return;
280 }
281 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700282
Brian Salomoncec2eda2022-03-22 20:12:55 +0000283 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
John Reckb5fc2092018-04-30 14:43:22 -0700284 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700285
John Reckb5fc2092018-04-30 14:43:22 -0700286 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500287 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800288 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
289 auto size = glesVersion ? strlen(glesVersion) : -1;
290 cacheManager().configureContext(&options, glesVersion, size);
Kevin Lubicke1163cd2023-09-27 15:00:48 +0000291 sk_sp<GrDirectContext> grContext(GrDirectContexts::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700292 LOG_ALWAYS_FATAL_IF(!grContext.get());
293 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700294}
295
Stan Iliev981afe72019-02-13 14:24:33 -0500296void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400297 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800298 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
299 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
300 // in the rendering thread.
301 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500302 return;
303 }
304 mVkManager->initialize();
305 GrContextOptions options;
306 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400307 auto vkDriverVersion = mVkManager->getDriverVersion();
308 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400309 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500310 LOG_ALWAYS_FATAL_IF(!grContext.get());
311 setGrContext(grContext);
312}
313
314void RenderThread::initGrContextOptions(GrContextOptions& options) {
315 options.fPreferExternalImagesOverES3 = true;
316 options.fDisableDistanceFieldPaths = true;
Adlai Holler108be5b2021-04-20 14:37:29 +0000317 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400318 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
319 } else {
320 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
321 }
Stan Iliev981afe72019-02-13 14:24:33 -0500322}
323
John Reck283bb462018-12-13 16:40:14 -0800324void RenderThread::destroyRenderingContext() {
325 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500326 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
327 if (mEglManager->hasEglContext()) {
328 setGrContext(nullptr);
329 mEglManager->destroy();
330 }
331 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400332 setGrContext(nullptr);
333 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700334 }
335}
336
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400337VulkanManager& RenderThread::vulkanManager() {
338 if (!mVkManager.get()) {
339 mVkManager = VulkanManager::getInstance();
340 }
341 return *mVkManager.get();
342}
343
John Reck66e06d42021-05-11 17:04:54 -0400344static const char* pipelineToString() {
345 switch (auto renderType = Properties::getRenderPipelineType()) {
346 case RenderPipelineType::SkiaGL:
347 return "Skia (OpenGL)";
348 case RenderPipelineType::SkiaVulkan:
349 return "Skia (Vulkan)";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400350 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700351 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
John Reck66e06d42021-05-11 17:04:54 -0400352 }
353}
354
355void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
356 if (includeProfileData) {
357 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400358 }
359
John Reck66e06d42021-05-11 17:04:54 -0400360 String8 cachesOutput;
361 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
Tomasz Wasilczyk3f9f8632023-08-10 23:54:44 +0000362 dprintf(fd, "\nPipeline=%s\n%s", pipelineToString(), cachesOutput.c_str());
John Reck07f3d912023-08-17 12:46:44 -0400363 for (auto&& context : mCacheManager->mCanvasContexts) {
364 context->visitAllRenderNodes([&](const RenderNode& node) {
365 if (node.isTextureView()) {
366 dprintf(fd, "TextureView: %dx%d\n", node.getWidth(), node.getHeight());
367 }
368 });
369 }
370 dprintf(fd, "\n");
John Reck3b202512014-06-23 13:13:08 -0700371}
372
John Reck39207682021-05-12 19:10:47 -0400373void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
374 mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
375}
376
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500377Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500378 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400379 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500380 }
381
382 return *mReadback;
383}
384
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400385void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400386 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500387 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400388 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400389 mGrContext->releaseResourcesAndAbandonContext();
390 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500391 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400392 if (mGrContext) {
393 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
394 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400395}
396
John Reck2abc5492021-05-18 00:34:26 -0400397sk_sp<GrDirectContext> RenderThread::requireGrContext() {
398 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
399 requireGlContext();
400 } else {
401 requireVkContext();
402 }
403 return mGrContext;
404}
405
Alec Mouri4a818f12019-11-19 16:17:53 -0800406int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700407 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
408 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700409 "events=0x%x",
410 events);
411 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700412 }
413
414 if (!(events & Looper::EVENT_INPUT)) {
415 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700416 "events=0x%x",
417 events);
418 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700419 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800420 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
421 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700422
Alec Mouri4a818f12019-11-19 16:17:53 -0800423 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700424}
425
426void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700427 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700428 mFrameCallbackTaskPending = false;
429
430 std::set<IFrameCallback*> callbacks;
431 mFrameCallbacks.swap(callbacks);
432
John Recka733f892014-12-19 11:37:21 -0800433 if (callbacks.size()) {
434 // Assume one of them will probably animate again so preemptively
435 // request the next vsync in case it occurs mid-frame
436 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700437 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
438 it++) {
John Recka733f892014-12-19 11:37:21 -0800439 (*it)->doFrame();
440 }
John Recke45b1fd2014-04-15 09:50:16 -0700441 }
442}
443
John Recka5dda642014-05-22 15:43:54 -0700444void RenderThread::requestVsync() {
445 if (!mVsyncRequested) {
446 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700447 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700448 }
449}
450
John Reckcec24ae2013-11-05 13:27:50 -0800451bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700452 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800453 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800454 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500455 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800456 }
John Reck3b202512014-06-23 13:13:08 -0700457 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700458
John Reckf8441e62017-10-23 13:10:41 -0700459 while (true) {
460 waitForWork();
461 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700462
463 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800464 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700465 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
466 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700467 mPendingRegistrationFrameCallbacks.clear();
468 requestVsync();
469 }
John Recka22c9b22015-01-14 10:40:15 -0800470
471 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
472 // TODO: Clean this up. This is working around an issue where a combination
473 // of bad timing and slow drawing can result in dropping a stale vsync
474 // on the floor (correct!) but fails to schedule to listen for the
475 // next vsync (oops), so none of the callbacks are run.
476 requestVsync();
477 }
John Reck5f66fb82022-09-23 17:49:23 -0400478
479 mCacheManager->onThreadIdle();
John Reckcec24ae2013-11-05 13:27:50 -0800480 }
481
482 return false;
483}
484
John Recke45b1fd2014-04-15 09:50:16 -0700485void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700486 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700487}
488
John Reck01a5ea32014-12-03 13:01:07 -0800489bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
490 size_t erased;
491 erased = mFrameCallbacks.erase(callback);
492 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
493 return erased;
John Recka5dda642014-05-22 15:43:54 -0700494}
495
496void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
497 if (mFrameCallbacks.erase(callback)) {
498 mPendingRegistrationFrameCallbacks.insert(callback);
499 }
John Recke45b1fd2014-04-15 09:50:16 -0700500}
501
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400502sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
503 auto renderType = Properties::getRenderPipelineType();
504 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400505 case RenderPipelineType::SkiaVulkan:
506 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
507 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700508 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400509 break;
510 }
511 return nullptr;
512}
513
Stan Iliev6b894d72017-08-23 12:41:41 -0400514bool RenderThread::isCurrent() {
515 return gettid() == getInstance().getTid();
516}
517
Stan Iliev898123b2019-02-14 14:57:44 -0500518void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400519 // EGL driver is always preloaded only if HWUI renders with GL.
520 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700521 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400522 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000523 } else {
524 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500525 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000526 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500527}
528
John Reck5f66fb82022-09-23 17:49:23 -0400529void RenderThread::trimMemory(TrimLevel level) {
530 ATRACE_CALL();
531 cacheManager().trimMemory(level);
532}
533
Jernej Virag44db0402023-05-09 20:24:48 +0200534void RenderThread::trimCaches(CacheTrimLevel level) {
535 ATRACE_CALL();
536 cacheManager().trimCaches(level);
537}
538
John Reckcec24ae2013-11-05 13:27:50 -0800539} /* namespace renderthread */
540} /* namespace uirenderer */
541} /* namespace android */