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