John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 17 | #include "RenderThread.h" |
| 18 | |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 19 | #include <android-base/properties.h> |
| 20 | #include <dlfcn.h> |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 21 | #include <gui/TraceUtils.h> |
Nolan Scobie | 572d801 | 2024-08-30 13:43:34 -0400 | [diff] [blame] | 22 | #include <include/gpu/ganesh/GrContextOptions.h> |
Jerome Gaillard | 87ef524 | 2024-02-13 16:33:00 +0000 | [diff] [blame] | 23 | #include <include/gpu/ganesh/gl/GrGLDirectContext.h> |
Nolan Scobie | 572d801 | 2024-08-30 13:43:34 -0400 | [diff] [blame] | 24 | #include <include/gpu/ganesh/gl/GrGLInterface.h> |
Jerome Gaillard | 87ef524 | 2024-02-13 16:33:00 +0000 | [diff] [blame] | 25 | #include <private/android/choreographer.h> |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 26 | #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 Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 34 | #include "../HardwareBitmapUploader.h" |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 35 | #include "CacheManager.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 36 | #include "CanvasContext.h" |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 37 | #include "DeviceInfo.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 38 | #include "EglManager.h" |
Adlai Holler | 5636cde | 2021-03-25 16:53:56 +0000 | [diff] [blame] | 39 | #include "Properties.h" |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 40 | #include "Readback.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 41 | #include "RenderProxy.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 42 | #include "VulkanManager.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 43 | #include "hwui/Bitmap.h" |
| 44 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame] | 45 | #include "pipeline/skia/SkiaVulkanPipeline.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | #include "renderstate/RenderState.h" |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 47 | #include "utils/TimeUtils.h" |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 48 | |
| 49 | namespace android { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 50 | namespace uirenderer { |
| 51 | namespace renderthread { |
| 52 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 53 | static bool gHasRenderThreadInstance = false; |
| 54 | |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 55 | static JVMAttachHook gOnStartHook = nullptr; |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 56 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 57 | ASurfaceControlFunctions::ASurfaceControlFunctions() { |
| 58 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 59 | createFunc = (ASC_create)dlsym(handle_, "ASurfaceControl_create"); |
| 60 | LOG_ALWAYS_FATAL_IF(createFunc == nullptr, |
| 61 | "Failed to find required symbol ASurfaceControl_create!"); |
| 62 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 63 | 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 Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 70 | |
| 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 Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 90 | |
| 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 Luo | 540fdf8 | 2021-06-25 13:59:39 -0700 | [diff] [blame] | 103 | transactionReparentFunc = (AST_reparent)dlsym(handle_, "ASurfaceTransaction_reparent"); |
| 104 | LOG_ALWAYS_FATAL_IF(transactionReparentFunc == nullptr, |
| 105 | "Failed to find required symbol transactionReparentFunc!"); |
| 106 | |
Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 107 | transactionSetVisibilityFunc = |
| 108 | (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility"); |
| 109 | LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr, |
| 110 | "Failed to find required symbol ASurfaceTransaction_setVisibility!"); |
Huihong Luo | 34f42fd | 2021-05-03 14:47:36 -0700 | [diff] [blame] | 111 | |
| 112 | transactionSetZOrderFunc = (AST_setZOrder)dlsym(handle_, "ASurfaceTransaction_setZOrder"); |
| 113 | LOG_ALWAYS_FATAL_IF(transactionSetZOrderFunc == nullptr, |
| 114 | "Failed to find required symbol ASurfaceTransaction_setZOrder!"); |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 117 | void RenderThread::extendedFrameCallback(const AChoreographerFrameCallbackData* cbData, |
| 118 | void* data) { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 119 | RenderThread* rt = reinterpret_cast<RenderThread*>(data); |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 120 | size_t preferredFrameTimelineIndex = |
| 121 | AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(cbData); |
Rachel Lee | 5ad064f | 2022-01-14 15:37:02 -0800 | [diff] [blame] | 122 | AVsyncId vsyncId = AChoreographerFrameCallbackData_getFrameTimelineVsyncId( |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 123 | cbData, preferredFrameTimelineIndex); |
Rachel Lee | 3712a2a | 2022-01-12 17:38:52 -0800 | [diff] [blame] | 124 | int64_t frameDeadline = AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos( |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 125 | 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 Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 128 | int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer); |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 129 | rt->frameCallback(vsyncId, frameDeadline, frameTimeNanos, frameInterval); |
| 130 | } |
| 131 | |
| 132 | void 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 Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 138 | mFrameCallbackTaskPending = true; |
Carlos Martinez Romero | d7971e7 | 2023-02-27 23:51:52 +0000 | [diff] [blame] | 139 | |
ramindani | 9b64168 | 2023-05-09 15:01:48 -0700 | [diff] [blame] | 140 | 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 Reck | 2375002 | 2023-11-21 18:30:35 -0500 | [diff] [blame] | 154 | [this]() { dispatchFrameCallbacks(); }); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | void 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 | |
| 165 | class ChoreographerSource : public VsyncSource { |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 166 | public: |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 167 | ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {} |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 168 | |
| 169 | virtual void requestNextVsync() override { |
Rachel Lee | 1a44c62 | 2022-02-15 18:12:01 -0800 | [diff] [blame] | 170 | AChoreographer_postVsyncCallback(mRenderThread->mChoreographer, |
| 171 | RenderThread::extendedFrameCallback, mRenderThread); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 174 | virtual void drainPendingEvents() override { |
| 175 | AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private: |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 179 | RenderThread* mRenderThread; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | class DummyVsyncSource : public VsyncSource { |
| 183 | public: |
| 184 | DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {} |
| 185 | |
| 186 | virtual void requestNextVsync() override { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 187 | mRenderThread->queue().postDelayed(16_ms, [this]() { |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 188 | mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID, |
| 189 | std::numeric_limits<int64_t>::max(), |
| 190 | systemTime(SYSTEM_TIME_MONOTONIC), 16_ms); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 191 | }); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 194 | virtual void drainPendingEvents() override { |
Rachel Lee | 432b691 | 2021-09-01 13:21:44 -0700 | [diff] [blame] | 195 | mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID, |
| 196 | std::numeric_limits<int64_t>::max(), |
| 197 | systemTime(SYSTEM_TIME_MONOTONIC), 16_ms); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 198 | } |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 199 | |
| 200 | private: |
| 201 | RenderThread* mRenderThread; |
| 202 | }; |
| 203 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 204 | bool RenderThread::hasInstance() { |
| 205 | return gHasRenderThreadInstance; |
| 206 | } |
| 207 | |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 208 | void RenderThread::setOnStartHook(JVMAttachHook onStartHook) { |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 209 | LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started..."); |
| 210 | gOnStartHook = onStartHook; |
| 211 | } |
| 212 | |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 213 | JVMAttachHook RenderThread::getOnStartHook() { |
| 214 | return gOnStartHook; |
| 215 | } |
| 216 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 217 | RenderThread& RenderThread::getInstance() { |
Steven Moreland | 76ec382 | 2021-04-02 16:26:03 +0000 | [diff] [blame] | 218 | [[clang::no_destroy]] static sp<RenderThread> sInstance = []() { |
| 219 | sp<RenderThread> thread = sp<RenderThread>::make(); |
| 220 | thread->start("RenderThread"); |
| 221 | return thread; |
| 222 | }(); |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 223 | gHasRenderThreadInstance = true; |
| 224 | return *sInstance; |
| 225 | } |
| 226 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 227 | RenderThread::RenderThread() |
| 228 | : ThreadBase() |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 229 | , mVsyncSource(nullptr) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 230 | , mVsyncRequested(false) |
| 231 | , mFrameCallbackTaskPending(false) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 232 | , mRenderState(nullptr) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 233 | , mEglManager(nullptr) |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 234 | , mFunctorManager(WebViewFunctorManager::instance()) |
| 235 | , mGlobalProfileData(mJankDataMutex) { |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 236 | Properties::load(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | RenderThread::~RenderThread() { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 240 | // Note that if this fatal assertion is removed then member variables must |
| 241 | // be properly destroyed. |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 242 | LOG_ALWAYS_FATAL("Can't destroy the render thread"); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 245 | void RenderThread::initializeChoreographer() { |
| 246 | LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 247 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 248 | if (!Properties::isolatedProcess) { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 249 | mChoreographer = AChoreographer_create(); |
| 250 | LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed"); |
| 251 | AChoreographer_registerRefreshRateCallback(mChoreographer, |
| 252 | RenderThread::refreshRateCallback, this); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 253 | |
| 254 | // Register the FD |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 255 | mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT, |
| 256 | RenderThread::choreographerCallback, this); |
| 257 | mVsyncSource = new ChoreographerSource(this); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 258 | } else { |
| 259 | mVsyncSource = new DummyVsyncSource(this); |
| 260 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 261 | } |
| 262 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 263 | void RenderThread::initThreadLocals() { |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 264 | setupFrameInterval(); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 265 | initializeChoreographer(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 266 | mEglManager = new EglManager(); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 267 | mRenderState = new RenderState(*this); |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 268 | mVkManager = VulkanManager::getInstance(); |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 269 | mCacheManager = new CacheManager(*this); |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void RenderThread::setupFrameInterval() { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 273 | nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod(); |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 274 | mTimeLord.setFrameInterval(frameIntervalNanos); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 275 | } |
| 276 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 277 | void RenderThread::requireGlContext() { |
| 278 | if (mEglManager->hasEglContext()) { |
| 279 | return; |
| 280 | } |
| 281 | mEglManager->initialize(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 282 | |
Brian Salomon | cec2eda | 2022-03-22 20:12:55 +0000 | [diff] [blame] | 283 | sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface(); |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 284 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 285 | |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 286 | GrContextOptions options; |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 287 | initGrContextOptions(options); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 288 | auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION)); |
| 289 | auto size = glesVersion ? strlen(glesVersion) : -1; |
| 290 | cacheManager().configureContext(&options, glesVersion, size); |
Kevin Lubick | e1163cd | 2023-09-27 15:00:48 +0000 | [diff] [blame] | 291 | sk_sp<GrDirectContext> grContext(GrDirectContexts::MakeGL(std::move(glInterface), options)); |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 292 | LOG_ALWAYS_FATAL_IF(!grContext.get()); |
| 293 | setGrContext(grContext); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 296 | void RenderThread::requireVkContext() { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 297 | // the getter creates the context in the event it had been destroyed by destroyRenderingContext |
Alec Mouri | 7e7c3d7 | 2021-01-06 17:46:22 -0800 | [diff] [blame] | 298 | // 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 Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | mVkManager->initialize(); |
| 305 | GrContextOptions options; |
| 306 | initGrContextOptions(options); |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 307 | auto vkDriverVersion = mVkManager->getDriverVersion(); |
| 308 | cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion)); |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 309 | sk_sp<GrDirectContext> grContext = mVkManager->createContext(options); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 310 | LOG_ALWAYS_FATAL_IF(!grContext.get()); |
| 311 | setGrContext(grContext); |
| 312 | } |
| 313 | |
| 314 | void RenderThread::initGrContextOptions(GrContextOptions& options) { |
| 315 | options.fPreferExternalImagesOverES3 = true; |
| 316 | options.fDisableDistanceFieldPaths = true; |
Adlai Holler | 108be5b | 2021-04-20 14:37:29 +0000 | [diff] [blame] | 317 | if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) { |
Adlai Holler | dfc7d4c | 2021-03-24 13:26:56 -0400 | [diff] [blame] | 318 | options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes; |
| 319 | } else { |
| 320 | options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo; |
| 321 | } |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 322 | } |
| 323 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 324 | void RenderThread::destroyRenderingContext() { |
| 325 | mFunctorManager.onContextDestroyed(); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 326 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 327 | if (mEglManager->hasEglContext()) { |
| 328 | setGrContext(nullptr); |
| 329 | mEglManager->destroy(); |
| 330 | } |
| 331 | } else { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 332 | setGrContext(nullptr); |
| 333 | mVkManager.clear(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 337 | VulkanManager& RenderThread::vulkanManager() { |
| 338 | if (!mVkManager.get()) { |
| 339 | mVkManager = VulkanManager::getInstance(); |
| 340 | } |
| 341 | return *mVkManager.get(); |
| 342 | } |
| 343 | |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 344 | static 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 Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 350 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 351 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) { |
| 356 | if (includeProfileData) { |
| 357 | globalProfileData()->dump(fd); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 358 | } |
| 359 | |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 360 | String8 cachesOutput; |
| 361 | mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState); |
Tomasz Wasilczyk | 3f9f863 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 362 | dprintf(fd, "\nPipeline=%s\n%s", pipelineToString(), cachesOutput.c_str()); |
John Reck | 07f3d91 | 2023-08-17 12:46:44 -0400 | [diff] [blame] | 363 | 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 Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 371 | } |
| 372 | |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 373 | void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) { |
| 374 | mCacheManager->getMemoryUsage(cpuUsage, gpuUsage); |
| 375 | } |
| 376 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 377 | Readback& RenderThread::readback() { |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 378 | if (!mReadback) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 379 | mReadback = new Readback(*this); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return *mReadback; |
| 383 | } |
| 384 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 385 | void RenderThread::setGrContext(sk_sp<GrDirectContext> context) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 386 | mCacheManager->reset(context); |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 387 | if (mGrContext) { |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 388 | mRenderState->onContextDestroyed(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 389 | mGrContext->releaseResourcesAndAbandonContext(); |
| 390 | } |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 391 | mGrContext = std::move(context); |
Derek Sollenberger | 1766238 | 2018-09-13 14:14:00 -0400 | [diff] [blame] | 392 | if (mGrContext) { |
| 393 | DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize()); |
| 394 | } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 395 | } |
| 396 | |
John Reck | 2abc549 | 2021-05-18 00:34:26 -0400 | [diff] [blame] | 397 | sk_sp<GrDirectContext> RenderThread::requireGrContext() { |
| 398 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 399 | requireGlContext(); |
| 400 | } else { |
| 401 | requireVkContext(); |
| 402 | } |
| 403 | return mGrContext; |
| 404 | } |
| 405 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 406 | int RenderThread::choreographerCallback(int fd, int events, void* data) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 407 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 408 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 409 | "events=0x%x", |
| 410 | events); |
| 411 | return 0; // remove the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | if (!(events & Looper::EVENT_INPUT)) { |
| 415 | ALOGW("Received spurious callback for unhandled poll event. " |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 416 | "events=0x%x", |
| 417 | events); |
| 418 | return 1; // keep the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 419 | } |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 420 | RenderThread* rt = reinterpret_cast<RenderThread*>(data); |
| 421 | AChoreographer_handlePendingEvents(rt->mChoreographer, data); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 422 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 423 | return 1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void RenderThread::dispatchFrameCallbacks() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 427 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 428 | mFrameCallbackTaskPending = false; |
| 429 | |
| 430 | std::set<IFrameCallback*> callbacks; |
| 431 | mFrameCallbacks.swap(callbacks); |
| 432 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 433 | 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 Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 437 | for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); |
| 438 | it++) { |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 439 | (*it)->doFrame(); |
| 440 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 444 | void RenderThread::requestVsync() { |
| 445 | if (!mVsyncRequested) { |
| 446 | mVsyncRequested = true; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 447 | mVsyncSource->requestNextVsync(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 451 | bool RenderThread::threadLoop() { |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 452 | setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY); |
John Reck | 700079e | 2019-02-19 10:38:50 -0800 | [diff] [blame] | 453 | Looper::setForThread(mLooper); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 454 | if (gOnStartHook) { |
Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame] | 455 | gOnStartHook("RenderThread"); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 456 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 457 | initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 458 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 459 | while (true) { |
| 460 | waitForWork(); |
| 461 | processQueue(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 462 | |
| 463 | if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 464 | mVsyncSource->drainPendingEvents(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 465 | mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(), |
| 466 | mPendingRegistrationFrameCallbacks.end()); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 467 | mPendingRegistrationFrameCallbacks.clear(); |
| 468 | requestVsync(); |
| 469 | } |
John Reck | a22c9b2 | 2015-01-14 10:40:15 -0800 | [diff] [blame] | 470 | |
| 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 Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 478 | |
| 479 | mCacheManager->onThreadIdle(); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return false; |
| 483 | } |
| 484 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 485 | void RenderThread::postFrameCallback(IFrameCallback* callback) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 486 | mPendingRegistrationFrameCallbacks.insert(callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 487 | } |
| 488 | |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 489 | bool RenderThread::removeFrameCallback(IFrameCallback* callback) { |
| 490 | size_t erased; |
| 491 | erased = mFrameCallbacks.erase(callback); |
| 492 | erased |= mPendingRegistrationFrameCallbacks.erase(callback); |
| 493 | return erased; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void RenderThread::pushBackFrameCallback(IFrameCallback* callback) { |
| 497 | if (mFrameCallbacks.erase(callback)) { |
| 498 | mPendingRegistrationFrameCallbacks.insert(callback); |
| 499 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 502 | sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) { |
| 503 | auto renderType = Properties::getRenderPipelineType(); |
| 504 | switch (renderType) { |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 505 | case RenderPipelineType::SkiaVulkan: |
| 506 | return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 507 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 508 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | return nullptr; |
| 512 | } |
| 513 | |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 514 | bool RenderThread::isCurrent() { |
| 515 | return gettid() == getInstance().getTid(); |
| 516 | } |
| 517 | |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 518 | void RenderThread::preload() { |
Stan Iliev | 30b9096 | 2019-03-29 14:25:09 -0400 | [diff] [blame] | 519 | // EGL driver is always preloaded only if HWUI renders with GL. |
| 520 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 521 | std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); }); |
Stan Iliev | 30b9096 | 2019-03-29 14:25:09 -0400 | [diff] [blame] | 522 | eglInitThread.detach(); |
Yiwei Zhang | bf37175 | 2020-07-31 21:03:29 +0000 | [diff] [blame] | 523 | } else { |
| 524 | requireVkContext(); |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 525 | } |
Yiwei Zhang | bf37175 | 2020-07-31 21:03:29 +0000 | [diff] [blame] | 526 | HardwareBitmapUploader::initialize(); |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 527 | } |
| 528 | |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 529 | void RenderThread::trimMemory(TrimLevel level) { |
| 530 | ATRACE_CALL(); |
| 531 | cacheManager().trimMemory(level); |
| 532 | } |
| 533 | |
Jernej Virag | 44db040 | 2023-05-09 20:24:48 +0200 | [diff] [blame] | 534 | void RenderThread::trimCaches(CacheTrimLevel level) { |
| 535 | ATRACE_CALL(); |
| 536 | cacheManager().trimCaches(level); |
| 537 | } |
| 538 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 539 | } /* namespace renderthread */ |
| 540 | } /* namespace uirenderer */ |
| 541 | } /* namespace android */ |