blob: f76ea063842f55cdd58823d02b85d9c564d06409 [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>
Kevin Lubicke1163cd2023-09-27 15:00:48 +000020#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Rachel Lee432b6912021-09-01 13:21:44 -070021#include <android-base/properties.h>
22#include <dlfcn.h>
23#include <gl/GrGLInterface.h>
rnleece9762b2021-05-21 15:40:53 -070024#include <gui/TraceUtils.h>
Rachel Lee432b6912021-09-01 13:21:44 -070025#include <sys/resource.h>
26#include <ui/FatVector.h>
27#include <utils/Condition.h>
28#include <utils/Log.h>
29#include <utils/Mutex.h>
30
31#include <thread>
32
John Reck0fa0cbc2019-04-05 16:57:46 -070033#include "../HardwareBitmapUploader.h"
Rachel Lee432b6912021-09-01 13:21:44 -070034#include "CacheManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080035#include "CanvasContext.h"
John Reck56428472018-03-16 17:27:17 -070036#include "DeviceInfo.h"
John Reck3b202512014-06-23 13:13:08 -070037#include "EglManager.h"
Adlai Holler5636cde2021-03-25 16:53:56 +000038#include "Properties.h"
Stan Iliev1a025a72018-09-05 16:35:11 -040039#include "Readback.h"
John Reck4f02bf42014-01-03 18:09:17 -080040#include "RenderProxy.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050041#include "VulkanManager.h"
John Reck1bcacfd2017-11-03 10:12:19 -070042#include "hwui/Bitmap.h"
43#include "pipeline/skia/SkiaOpenGLPipeline.h"
John Reck1a4a9812018-04-18 16:13:31 -070044#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reck1bcacfd2017-11-03 10:12:19 -070045#include "renderstate/RenderState.h"
John Reck56428472018-03-16 17:27:17 -070046#include "utils/TimeUtils.h"
John Reckcec24ae2013-11-05 13:27:50 -080047
48namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080049namespace uirenderer {
50namespace renderthread {
51
John Reck6b507802015-11-03 10:09:59 -080052static bool gHasRenderThreadInstance = false;
53
Stan Iliev80dbc352019-02-05 15:31:28 -050054static JVMAttachHook gOnStartHook = nullptr;
John Reck259b25a2017-12-01 16:18:53 -080055
Huihong Luo5fdf7b82021-01-15 14:27:06 -080056ASurfaceControlFunctions::ASurfaceControlFunctions() {
57 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
Huihong Luo054b8d32021-02-24 18:48:12 -080058 createFunc = (ASC_create)dlsym(handle_, "ASurfaceControl_create");
59 LOG_ALWAYS_FATAL_IF(createFunc == nullptr,
60 "Failed to find required symbol ASurfaceControl_create!");
61
Huihong Luo5fdf7b82021-01-15 14:27:06 -080062 acquireFunc = (ASC_acquire) dlsym(handle_, "ASurfaceControl_acquire");
63 LOG_ALWAYS_FATAL_IF(acquireFunc == nullptr,
64 "Failed to find required symbol ASurfaceControl_acquire!");
65
66 releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release");
67 LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr,
68 "Failed to find required symbol ASurfaceControl_release!");
Jorim Jaggi71db8892021-02-03 23:19:29 +010069
70 registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_,
71 "ASurfaceControl_registerSurfaceStatsListener");
72 LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr,
73 "Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!");
74
75 unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_,
76 "ASurfaceControl_unregisterSurfaceStatsListener");
77 LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr,
78 "Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!");
79
80 getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_,
81 "ASurfaceControlStats_getAcquireTime");
82 LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr,
83 "Failed to find required symbol ASurfaceControlStats_getAcquireTime!");
84
85 getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_,
86 "ASurfaceControlStats_getFrameNumber");
87 LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr,
88 "Failed to find required symbol ASurfaceControlStats_getFrameNumber!");
Huihong Luo054b8d32021-02-24 18:48:12 -080089
90 transactionCreateFunc = (AST_create)dlsym(handle_, "ASurfaceTransaction_create");
91 LOG_ALWAYS_FATAL_IF(transactionCreateFunc == nullptr,
92 "Failed to find required symbol ASurfaceTransaction_create!");
93
94 transactionDeleteFunc = (AST_delete)dlsym(handle_, "ASurfaceTransaction_delete");
95 LOG_ALWAYS_FATAL_IF(transactionDeleteFunc == nullptr,
96 "Failed to find required symbol ASurfaceTransaction_delete!");
97
98 transactionApplyFunc = (AST_apply)dlsym(handle_, "ASurfaceTransaction_apply");
99 LOG_ALWAYS_FATAL_IF(transactionApplyFunc == nullptr,
100 "Failed to find required symbol ASurfaceTransaction_apply!");
101
Huihong Luo540fdf82021-06-25 13:59:39 -0700102 transactionReparentFunc = (AST_reparent)dlsym(handle_, "ASurfaceTransaction_reparent");
103 LOG_ALWAYS_FATAL_IF(transactionReparentFunc == nullptr,
104 "Failed to find required symbol transactionReparentFunc!");
105
Huihong Luo054b8d32021-02-24 18:48:12 -0800106 transactionSetVisibilityFunc =
107 (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility");
108 LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr,
109 "Failed to find required symbol ASurfaceTransaction_setVisibility!");
Huihong Luo34f42fd2021-05-03 14:47:36 -0700110
111 transactionSetZOrderFunc = (AST_setZOrder)dlsym(handle_, "ASurfaceTransaction_setZOrder");
112 LOG_ALWAYS_FATAL_IF(transactionSetZOrderFunc == nullptr,
113 "Failed to find required symbol ASurfaceTransaction_setZOrder!");
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800114}
115
Rachel Lee432b6912021-09-01 13:21:44 -0700116void RenderThread::extendedFrameCallback(const AChoreographerFrameCallbackData* cbData,
117 void* data) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800118 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
Rachel Lee432b6912021-09-01 13:21:44 -0700119 size_t preferredFrameTimelineIndex =
120 AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(cbData);
Rachel Lee5ad064f2022-01-14 15:37:02 -0800121 AVsyncId vsyncId = AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee432b6912021-09-01 13:21:44 -0700122 cbData, preferredFrameTimelineIndex);
Rachel Lee3712a2a2022-01-12 17:38:52 -0800123 int64_t frameDeadline = AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee432b6912021-09-01 13:21:44 -0700124 cbData, preferredFrameTimelineIndex);
125 int64_t frameTimeNanos = AChoreographerFrameCallbackData_getFrameTimeNanos(cbData);
126 // TODO(b/193273294): Remove when shared memory in use w/ expected present time always current.
Jorim Jaggi10f328c2021-01-19 00:08:02 +0100127 int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer);
Rachel Lee432b6912021-09-01 13:21:44 -0700128 rt->frameCallback(vsyncId, frameDeadline, frameTimeNanos, frameInterval);
129}
130
131void RenderThread::frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
132 int64_t frameInterval) {
133 mVsyncRequested = false;
134 if (timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline,
135 frameInterval) &&
136 !mFrameCallbackTaskPending) {
Rachel Lee432b6912021-09-01 13:21:44 -0700137 mFrameCallbackTaskPending = true;
Carlos Martinez Romerod7971e72023-02-27 23:51:52 +0000138
ramindani9b641682023-05-09 15:01:48 -0700139 using SteadyClock = std::chrono::steady_clock;
140 using Nanos = std::chrono::nanoseconds;
141 using toNsecs_t = std::chrono::duration<nsecs_t, std::nano>;
142 using toFloatMillis = std::chrono::duration<float, std::milli>;
143
144 const auto frameTimeTimePoint = SteadyClock::time_point(Nanos(frameTimeNanos));
145 const auto deadlineTimePoint = SteadyClock::time_point(Nanos(frameDeadline));
146
147 const auto timeUntilDeadline = deadlineTimePoint - frameTimeTimePoint;
148 const auto runAt = (frameTimeTimePoint + (timeUntilDeadline / 4));
149
150 ATRACE_FORMAT("queue mFrameCallbackTask to run after %.2fms",
151 toFloatMillis(runAt - SteadyClock::now()).count());
152 queue().postAt(toNsecs_t(runAt.time_since_epoch()).count(),
153 [=]() { dispatchFrameCallbacks(); });
Alec Mouri4a818f12019-11-19 16:17:53 -0800154 }
155}
156
157void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
158 ATRACE_NAME("refreshRateCallback");
159 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
160 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
161 rt->setupFrameInterval();
162}
163
164class ChoreographerSource : public VsyncSource {
John Reck56428472018-03-16 17:27:17 -0700165public:
Alec Mouri4a818f12019-11-19 16:17:53 -0800166 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
John Reck56428472018-03-16 17:27:17 -0700167
168 virtual void requestNextVsync() override {
Rachel Lee1a44c622022-02-15 18:12:01 -0800169 AChoreographer_postVsyncCallback(mRenderThread->mChoreographer,
170 RenderThread::extendedFrameCallback, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700171 }
172
Alec Mouri4a818f12019-11-19 16:17:53 -0800173 virtual void drainPendingEvents() override {
174 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
John Reck56428472018-03-16 17:27:17 -0700175 }
176
177private:
Alec Mouri4a818f12019-11-19 16:17:53 -0800178 RenderThread* mRenderThread;
John Reck56428472018-03-16 17:27:17 -0700179};
180
181class DummyVsyncSource : public VsyncSource {
182public:
183 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
184
185 virtual void requestNextVsync() override {
Alec Mouri4a818f12019-11-19 16:17:53 -0800186 mRenderThread->queue().postDelayed(16_ms, [this]() {
Rachel Lee432b6912021-09-01 13:21:44 -0700187 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
188 std::numeric_limits<int64_t>::max(),
189 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800190 });
John Reck56428472018-03-16 17:27:17 -0700191 }
192
Alec Mouri4a818f12019-11-19 16:17:53 -0800193 virtual void drainPendingEvents() override {
Rachel Lee432b6912021-09-01 13:21:44 -0700194 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
195 std::numeric_limits<int64_t>::max(),
196 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
Alec Mouri4a818f12019-11-19 16:17:53 -0800197 }
John Reck56428472018-03-16 17:27:17 -0700198
199private:
200 RenderThread* mRenderThread;
201};
202
John Reck6b507802015-11-03 10:09:59 -0800203bool RenderThread::hasInstance() {
204 return gHasRenderThreadInstance;
205}
206
Stan Iliev80dbc352019-02-05 15:31:28 -0500207void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
John Reck259b25a2017-12-01 16:18:53 -0800208 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
209 gOnStartHook = onStartHook;
210}
211
Stan Iliev80dbc352019-02-05 15:31:28 -0500212JVMAttachHook RenderThread::getOnStartHook() {
213 return gOnStartHook;
214}
215
John Reck6b507802015-11-03 10:09:59 -0800216RenderThread& RenderThread::getInstance() {
Steven Moreland76ec3822021-04-02 16:26:03 +0000217 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
218 sp<RenderThread> thread = sp<RenderThread>::make();
219 thread->start("RenderThread");
220 return thread;
221 }();
John Reck6b507802015-11-03 10:09:59 -0800222 gHasRenderThreadInstance = true;
223 return *sInstance;
224}
225
John Reck1bcacfd2017-11-03 10:12:19 -0700226RenderThread::RenderThread()
227 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700228 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700229 , mVsyncRequested(false)
230 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800231 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500232 , mEglManager(nullptr)
Jorim Jaggi71db8892021-02-03 23:19:29 +0100233 , mFunctorManager(WebViewFunctorManager::instance())
234 , mGlobalProfileData(mJankDataMutex) {
Chris Craik2507c342015-05-04 14:36:49 -0700235 Properties::load();
John Reckcec24ae2013-11-05 13:27:50 -0800236}
237
238RenderThread::~RenderThread() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800239 // Note that if this fatal assertion is removed then member variables must
240 // be properly destroyed.
John Reck3b202512014-06-23 13:13:08 -0700241 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800242}
243
Alec Mouri4a818f12019-11-19 16:17:53 -0800244void RenderThread::initializeChoreographer() {
245 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
John Recke45b1fd2014-04-15 09:50:16 -0700246
John Reck56428472018-03-16 17:27:17 -0700247 if (!Properties::isolatedProcess) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800248 mChoreographer = AChoreographer_create();
249 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
250 AChoreographer_registerRefreshRateCallback(mChoreographer,
251 RenderThread::refreshRateCallback, this);
John Reck56428472018-03-16 17:27:17 -0700252
253 // Register the FD
Alec Mouri4a818f12019-11-19 16:17:53 -0800254 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
255 RenderThread::choreographerCallback, this);
256 mVsyncSource = new ChoreographerSource(this);
John Reck56428472018-03-16 17:27:17 -0700257 } else {
258 mVsyncSource = new DummyVsyncSource(this);
259 }
John Recke45b1fd2014-04-15 09:50:16 -0700260}
261
John Reck3b202512014-06-23 13:13:08 -0700262void RenderThread::initThreadLocals() {
John Reckcf185f52019-04-11 16:11:24 -0700263 setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800264 initializeChoreographer();
John Reck1e510712018-04-23 08:15:03 -0700265 mEglManager = new EglManager();
John Reck0e89e2b2014-10-31 14:49:06 -0700266 mRenderState = new RenderState(*this);
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400267 mVkManager = VulkanManager::getInstance();
John Reck5f66fb82022-09-23 17:49:23 -0400268 mCacheManager = new CacheManager(*this);
John Reckcf185f52019-04-11 16:11:24 -0700269}
270
271void RenderThread::setupFrameInterval() {
Alec Mouri4a818f12019-11-19 16:17:53 -0800272 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
John Reckcf185f52019-04-11 16:11:24 -0700273 mTimeLord.setFrameInterval(frameIntervalNanos);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400274}
275
John Reck1e510712018-04-23 08:15:03 -0700276void RenderThread::requireGlContext() {
277 if (mEglManager->hasEglContext()) {
278 return;
279 }
280 mEglManager->initialize();
John Reck1e510712018-04-23 08:15:03 -0700281
Brian Salomoncec2eda2022-03-22 20:12:55 +0000282 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
John Reckb5fc2092018-04-30 14:43:22 -0700283 LOG_ALWAYS_FATAL_IF(!glInterface.get());
John Reck1e510712018-04-23 08:15:03 -0700284
John Reckb5fc2092018-04-30 14:43:22 -0700285 GrContextOptions options;
Stan Iliev981afe72019-02-13 14:24:33 -0500286 initGrContextOptions(options);
Yichi Chen9f959552018-03-29 21:21:54 +0800287 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
288 auto size = glesVersion ? strlen(glesVersion) : -1;
289 cacheManager().configureContext(&options, glesVersion, size);
Kevin Lubicke1163cd2023-09-27 15:00:48 +0000290 sk_sp<GrDirectContext> grContext(GrDirectContexts::MakeGL(std::move(glInterface), options));
John Reckb5fc2092018-04-30 14:43:22 -0700291 LOG_ALWAYS_FATAL_IF(!grContext.get());
292 setGrContext(grContext);
John Reck1e510712018-04-23 08:15:03 -0700293}
294
Stan Iliev981afe72019-02-13 14:24:33 -0500295void RenderThread::requireVkContext() {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400296 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
Alec Mouri7e7c3d72021-01-06 17:46:22 -0800297 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
298 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
299 // in the rendering thread.
300 if (vulkanManager().hasVkContext() && mGrContext) {
Stan Iliev981afe72019-02-13 14:24:33 -0500301 return;
302 }
303 mVkManager->initialize();
304 GrContextOptions options;
305 initGrContextOptions(options);
Stan Ilievbf99c442019-03-29 11:09:11 -0400306 auto vkDriverVersion = mVkManager->getDriverVersion();
307 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400308 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
Stan Iliev981afe72019-02-13 14:24:33 -0500309 LOG_ALWAYS_FATAL_IF(!grContext.get());
310 setGrContext(grContext);
311}
312
313void RenderThread::initGrContextOptions(GrContextOptions& options) {
314 options.fPreferExternalImagesOverES3 = true;
315 options.fDisableDistanceFieldPaths = true;
Adlai Holler108be5b2021-04-20 14:37:29 +0000316 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
Adlai Hollerdfc7d4c2021-03-24 13:26:56 -0400317 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
318 } else {
319 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
320 }
Stan Iliev981afe72019-02-13 14:24:33 -0500321}
322
John Reck283bb462018-12-13 16:40:14 -0800323void RenderThread::destroyRenderingContext() {
324 mFunctorManager.onContextDestroyed();
Stan Iliev90276c82019-02-03 18:01:02 -0500325 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
326 if (mEglManager->hasEglContext()) {
327 setGrContext(nullptr);
328 mEglManager->destroy();
329 }
330 } else {
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400331 setGrContext(nullptr);
332 mVkManager.clear();
John Reck1e510712018-04-23 08:15:03 -0700333 }
334}
335
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400336VulkanManager& RenderThread::vulkanManager() {
337 if (!mVkManager.get()) {
338 mVkManager = VulkanManager::getInstance();
339 }
340 return *mVkManager.get();
341}
342
John Reck66e06d42021-05-11 17:04:54 -0400343static const char* pipelineToString() {
344 switch (auto renderType = Properties::getRenderPipelineType()) {
345 case RenderPipelineType::SkiaGL:
346 return "Skia (OpenGL)";
347 case RenderPipelineType::SkiaVulkan:
348 return "Skia (Vulkan)";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400349 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700350 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
John Reck66e06d42021-05-11 17:04:54 -0400351 }
352}
353
354void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
355 if (includeProfileData) {
356 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400357 }
358
John Reck66e06d42021-05-11 17:04:54 -0400359 String8 cachesOutput;
360 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
Tomasz Wasilczyk3f9f8632023-08-10 23:54:44 +0000361 dprintf(fd, "\nPipeline=%s\n%s", pipelineToString(), cachesOutput.c_str());
John Reck07f3d912023-08-17 12:46:44 -0400362 for (auto&& context : mCacheManager->mCanvasContexts) {
363 context->visitAllRenderNodes([&](const RenderNode& node) {
364 if (node.isTextureView()) {
365 dprintf(fd, "TextureView: %dx%d\n", node.getWidth(), node.getHeight());
366 }
367 });
368 }
369 dprintf(fd, "\n");
John Reck3b202512014-06-23 13:13:08 -0700370}
371
John Reck39207682021-05-12 19:10:47 -0400372void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
373 mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
374}
375
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500376Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500377 if (!mReadback) {
Stan Iliev1a025a72018-09-05 16:35:11 -0400378 mReadback = new Readback(*this);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500379 }
380
381 return *mReadback;
382}
383
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400384void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400385 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500386 if (mGrContext) {
Derek Sollenberger5a5a6482018-09-19 13:52:13 -0400387 mRenderState->onContextDestroyed();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400388 mGrContext->releaseResourcesAndAbandonContext();
389 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500390 mGrContext = std::move(context);
Derek Sollenberger17662382018-09-13 14:14:00 -0400391 if (mGrContext) {
392 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
393 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400394}
395
John Reck2abc5492021-05-18 00:34:26 -0400396sk_sp<GrDirectContext> RenderThread::requireGrContext() {
397 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
398 requireGlContext();
399 } else {
400 requireVkContext();
401 }
402 return mGrContext;
403}
404
Alec Mouri4a818f12019-11-19 16:17:53 -0800405int RenderThread::choreographerCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700406 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
407 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700408 "events=0x%x",
409 events);
410 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700411 }
412
413 if (!(events & Looper::EVENT_INPUT)) {
414 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700415 "events=0x%x",
416 events);
417 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700418 }
Alec Mouri4a818f12019-11-19 16:17:53 -0800419 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
420 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
John Recke45b1fd2014-04-15 09:50:16 -0700421
Alec Mouri4a818f12019-11-19 16:17:53 -0800422 return 1;
John Recke45b1fd2014-04-15 09:50:16 -0700423}
424
425void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700426 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700427 mFrameCallbackTaskPending = false;
428
429 std::set<IFrameCallback*> callbacks;
430 mFrameCallbacks.swap(callbacks);
431
John Recka733f892014-12-19 11:37:21 -0800432 if (callbacks.size()) {
433 // Assume one of them will probably animate again so preemptively
434 // request the next vsync in case it occurs mid-frame
435 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700436 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
437 it++) {
John Recka733f892014-12-19 11:37:21 -0800438 (*it)->doFrame();
439 }
John Recke45b1fd2014-04-15 09:50:16 -0700440 }
441}
442
John Recka5dda642014-05-22 15:43:54 -0700443void RenderThread::requestVsync() {
444 if (!mVsyncRequested) {
445 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700446 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700447 }
448}
449
John Reckcec24ae2013-11-05 13:27:50 -0800450bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700451 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck700079e2019-02-19 10:38:50 -0800452 Looper::setForThread(mLooper);
John Reck259b25a2017-12-01 16:18:53 -0800453 if (gOnStartHook) {
Stan Iliev978d5322019-02-06 12:02:28 -0500454 gOnStartHook("RenderThread");
John Reck259b25a2017-12-01 16:18:53 -0800455 }
John Reck3b202512014-06-23 13:13:08 -0700456 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700457
John Reckf8441e62017-10-23 13:10:41 -0700458 while (true) {
459 waitForWork();
460 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700461
462 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
Alec Mouri4a818f12019-11-19 16:17:53 -0800463 mVsyncSource->drainPendingEvents();
John Reck1bcacfd2017-11-03 10:12:19 -0700464 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
465 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700466 mPendingRegistrationFrameCallbacks.clear();
467 requestVsync();
468 }
John Recka22c9b22015-01-14 10:40:15 -0800469
470 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
471 // TODO: Clean this up. This is working around an issue where a combination
472 // of bad timing and slow drawing can result in dropping a stale vsync
473 // on the floor (correct!) but fails to schedule to listen for the
474 // next vsync (oops), so none of the callbacks are run.
475 requestVsync();
476 }
John Reck5f66fb82022-09-23 17:49:23 -0400477
478 mCacheManager->onThreadIdle();
John Reckcec24ae2013-11-05 13:27:50 -0800479 }
480
481 return false;
482}
483
John Recke45b1fd2014-04-15 09:50:16 -0700484void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700485 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700486}
487
John Reck01a5ea32014-12-03 13:01:07 -0800488bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
489 size_t erased;
490 erased = mFrameCallbacks.erase(callback);
491 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
492 return erased;
John Recka5dda642014-05-22 15:43:54 -0700493}
494
495void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
496 if (mFrameCallbacks.erase(callback)) {
497 mPendingRegistrationFrameCallbacks.insert(callback);
498 }
John Recke45b1fd2014-04-15 09:50:16 -0700499}
500
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400501sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
502 auto renderType = Properties::getRenderPipelineType();
503 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400504 case RenderPipelineType::SkiaVulkan:
505 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
506 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700507 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400508 break;
509 }
510 return nullptr;
511}
512
Stan Iliev6b894d72017-08-23 12:41:41 -0400513bool RenderThread::isCurrent() {
514 return gettid() == getInstance().getTid();
515}
516
Stan Iliev898123b2019-02-14 14:57:44 -0500517void RenderThread::preload() {
Stan Iliev30b90962019-03-29 14:25:09 -0400518 // EGL driver is always preloaded only if HWUI renders with GL.
519 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700520 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
Stan Iliev30b90962019-03-29 14:25:09 -0400521 eglInitThread.detach();
Yiwei Zhangbf371752020-07-31 21:03:29 +0000522 } else {
523 requireVkContext();
Stan Iliev898123b2019-02-14 14:57:44 -0500524 }
Yiwei Zhangbf371752020-07-31 21:03:29 +0000525 HardwareBitmapUploader::initialize();
Stan Iliev898123b2019-02-14 14:57:44 -0500526}
527
John Reck5f66fb82022-09-23 17:49:23 -0400528void RenderThread::trimMemory(TrimLevel level) {
529 ATRACE_CALL();
530 cacheManager().trimMemory(level);
531}
532
Jernej Virag44db0402023-05-09 20:24:48 +0200533void RenderThread::trimCaches(CacheTrimLevel level) {
534 ATRACE_CALL();
535 cacheManager().trimCaches(level);
536}
537
John Reckcec24ae2013-11-05 13:27:50 -0800538} /* namespace renderthread */
539} /* namespace uirenderer */
540} /* namespace android */