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 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 19 | #include "../HardwareBitmapUploader.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 20 | #include "CanvasContext.h" |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 21 | #include "DeviceInfo.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 22 | #include "EglManager.h" |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 23 | #include "Readback.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 24 | #include "RenderProxy.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 25 | #include "VulkanManager.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 26 | #include "hwui/Bitmap.h" |
| 27 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame] | 28 | #include "pipeline/skia/SkiaVulkanPipeline.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | #include "renderstate/RenderState.h" |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 30 | #include "utils/TimeUtils.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 31 | #include "utils/TraceUtils.h" |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 32 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 33 | #include <GrContextOptions.h> |
| 34 | #include <gl/GrGLInterface.h> |
| 35 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 36 | #include <dlfcn.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 37 | #include <sys/resource.h> |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 38 | #include <utils/Condition.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 39 | #include <utils/Log.h> |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 40 | #include <utils/Mutex.h> |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 41 | #include <thread> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 42 | |
Jagadeesh Pakaravoor | b624af3 | 2020-05-01 00:01:40 +0000 | [diff] [blame] | 43 | #include <ui/FatVector.h> |
| 44 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 45 | namespace android { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 46 | namespace uirenderer { |
| 47 | namespace renderthread { |
| 48 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 49 | static bool gHasRenderThreadInstance = false; |
| 50 | |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 51 | static JVMAttachHook gOnStartHook = nullptr; |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 52 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 53 | ASurfaceControlFunctions::ASurfaceControlFunctions() { |
| 54 | void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE); |
| 55 | acquireFunc = (ASC_acquire) dlsym(handle_, "ASurfaceControl_acquire"); |
| 56 | LOG_ALWAYS_FATAL_IF(acquireFunc == nullptr, |
| 57 | "Failed to find required symbol ASurfaceControl_acquire!"); |
| 58 | |
| 59 | releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release"); |
| 60 | LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr, |
| 61 | "Failed to find required symbol ASurfaceControl_release!"); |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame^] | 62 | |
| 63 | registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_, |
| 64 | "ASurfaceControl_registerSurfaceStatsListener"); |
| 65 | LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr, |
| 66 | "Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!"); |
| 67 | |
| 68 | unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_, |
| 69 | "ASurfaceControl_unregisterSurfaceStatsListener"); |
| 70 | LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr, |
| 71 | "Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!"); |
| 72 | |
| 73 | getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_, |
| 74 | "ASurfaceControlStats_getAcquireTime"); |
| 75 | LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr, |
| 76 | "Failed to find required symbol ASurfaceControlStats_getAcquireTime!"); |
| 77 | |
| 78 | getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_, |
| 79 | "ASurfaceControlStats_getFrameNumber"); |
| 80 | LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr, |
| 81 | "Failed to find required symbol ASurfaceControlStats_getFrameNumber!"); |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 84 | void RenderThread::frameCallback(int64_t frameTimeNanos, void* data) { |
| 85 | RenderThread* rt = reinterpret_cast<RenderThread*>(data); |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 86 | int64_t vsyncId = AChoreographer_getVsyncId(rt->mChoreographer); |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 87 | int64_t frameDeadline = AChoreographer_getFrameDeadline(rt->mChoreographer); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 88 | rt->mVsyncRequested = false; |
Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 89 | if (rt->timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline) && |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 90 | !rt->mFrameCallbackTaskPending) { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 91 | ATRACE_NAME("queue mFrameCallbackTask"); |
| 92 | rt->mFrameCallbackTaskPending = true; |
| 93 | nsecs_t runAt = (frameTimeNanos + rt->mDispatchFrameDelay); |
| 94 | rt->queue().postAt(runAt, [=]() { rt->dispatchFrameCallbacks(); }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) { |
| 99 | ATRACE_NAME("refreshRateCallback"); |
| 100 | RenderThread* rt = reinterpret_cast<RenderThread*>(data); |
| 101 | DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod); |
| 102 | rt->setupFrameInterval(); |
| 103 | } |
| 104 | |
| 105 | class ChoreographerSource : public VsyncSource { |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 106 | public: |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 107 | ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {} |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 108 | |
| 109 | virtual void requestNextVsync() override { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 110 | AChoreographer_postFrameCallback64(mRenderThread->mChoreographer, |
| 111 | RenderThread::frameCallback, mRenderThread); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 114 | virtual void drainPendingEvents() override { |
| 115 | AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private: |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 119 | RenderThread* mRenderThread; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | class DummyVsyncSource : public VsyncSource { |
| 123 | public: |
| 124 | DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {} |
| 125 | |
| 126 | virtual void requestNextVsync() override { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 127 | mRenderThread->queue().postDelayed(16_ms, [this]() { |
| 128 | RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread); |
| 129 | }); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 132 | virtual void drainPendingEvents() override { |
| 133 | RenderThread::frameCallback(systemTime(SYSTEM_TIME_MONOTONIC), mRenderThread); |
| 134 | } |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | RenderThread* mRenderThread; |
| 138 | }; |
| 139 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 140 | bool RenderThread::hasInstance() { |
| 141 | return gHasRenderThreadInstance; |
| 142 | } |
| 143 | |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 144 | void RenderThread::setOnStartHook(JVMAttachHook onStartHook) { |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 145 | LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started..."); |
| 146 | gOnStartHook = onStartHook; |
| 147 | } |
| 148 | |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 149 | JVMAttachHook RenderThread::getOnStartHook() { |
| 150 | return gOnStartHook; |
| 151 | } |
| 152 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 153 | RenderThread& RenderThread::getInstance() { |
| 154 | // This is a pointer because otherwise __cxa_finalize |
| 155 | // will try to delete it like a Good Citizen but that causes us to crash |
| 156 | // because we don't want to delete the RenderThread normally. |
| 157 | static RenderThread* sInstance = new RenderThread(); |
| 158 | gHasRenderThreadInstance = true; |
| 159 | return *sInstance; |
| 160 | } |
| 161 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | RenderThread::RenderThread() |
| 163 | : ThreadBase() |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 164 | , mVsyncSource(nullptr) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 165 | , mVsyncRequested(false) |
| 166 | , mFrameCallbackTaskPending(false) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 167 | , mRenderState(nullptr) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 168 | , mEglManager(nullptr) |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame^] | 169 | , mFunctorManager(WebViewFunctorManager::instance()) |
| 170 | , mGlobalProfileData(mJankDataMutex) { |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 171 | Properties::load(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 172 | start("RenderThread"); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | RenderThread::~RenderThread() { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 176 | // Note that if this fatal assertion is removed then member variables must |
| 177 | // be properly destroyed. |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 178 | LOG_ALWAYS_FATAL("Can't destroy the render thread"); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 181 | void RenderThread::initializeChoreographer() { |
| 182 | LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 183 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 184 | if (!Properties::isolatedProcess) { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 185 | mChoreographer = AChoreographer_create(); |
| 186 | LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed"); |
| 187 | AChoreographer_registerRefreshRateCallback(mChoreographer, |
| 188 | RenderThread::refreshRateCallback, this); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 189 | |
| 190 | // Register the FD |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 191 | mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT, |
| 192 | RenderThread::choreographerCallback, this); |
| 193 | mVsyncSource = new ChoreographerSource(this); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 194 | } else { |
| 195 | mVsyncSource = new DummyVsyncSource(this); |
| 196 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 199 | void RenderThread::initThreadLocals() { |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 200 | setupFrameInterval(); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 201 | initializeChoreographer(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 202 | mEglManager = new EglManager(); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 203 | mRenderState = new RenderState(*this); |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 204 | mVkManager = VulkanManager::getInstance(); |
Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 205 | mCacheManager = new CacheManager(); |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void RenderThread::setupFrameInterval() { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 209 | nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod(); |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 210 | mTimeLord.setFrameInterval(frameIntervalNanos); |
| 211 | mDispatchFrameDelay = static_cast<nsecs_t>(frameIntervalNanos * .25f); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 212 | } |
| 213 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 214 | void RenderThread::requireGlContext() { |
| 215 | if (mEglManager->hasEglContext()) { |
| 216 | return; |
| 217 | } |
| 218 | mEglManager->initialize(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 219 | |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 220 | sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface()); |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 221 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 222 | |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 223 | GrContextOptions options; |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 224 | initGrContextOptions(options); |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 225 | auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION)); |
| 226 | auto size = glesVersion ? strlen(glesVersion) : -1; |
| 227 | cacheManager().configureContext(&options, glesVersion, size); |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 228 | sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options)); |
John Reck | b5fc209 | 2018-04-30 14:43:22 -0700 | [diff] [blame] | 229 | LOG_ALWAYS_FATAL_IF(!grContext.get()); |
| 230 | setGrContext(grContext); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 233 | void RenderThread::requireVkContext() { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 234 | // 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] | 235 | // Also check if we have a GrContext before returning fast. VulkanManager may be shared with |
| 236 | // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext |
| 237 | // in the rendering thread. |
| 238 | if (vulkanManager().hasVkContext() && mGrContext) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | mVkManager->initialize(); |
| 242 | GrContextOptions options; |
| 243 | initGrContextOptions(options); |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 244 | auto vkDriverVersion = mVkManager->getDriverVersion(); |
| 245 | cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion)); |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 246 | sk_sp<GrDirectContext> grContext = mVkManager->createContext(options); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 247 | LOG_ALWAYS_FATAL_IF(!grContext.get()); |
| 248 | setGrContext(grContext); |
| 249 | } |
| 250 | |
| 251 | void RenderThread::initGrContextOptions(GrContextOptions& options) { |
| 252 | options.fPreferExternalImagesOverES3 = true; |
| 253 | options.fDisableDistanceFieldPaths = true; |
| 254 | } |
| 255 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 256 | void RenderThread::destroyRenderingContext() { |
| 257 | mFunctorManager.onContextDestroyed(); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 258 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
| 259 | if (mEglManager->hasEglContext()) { |
| 260 | setGrContext(nullptr); |
| 261 | mEglManager->destroy(); |
| 262 | } |
| 263 | } else { |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 264 | setGrContext(nullptr); |
| 265 | mVkManager.clear(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 269 | VulkanManager& RenderThread::vulkanManager() { |
| 270 | if (!mVkManager.get()) { |
| 271 | mVkManager = VulkanManager::getInstance(); |
| 272 | } |
| 273 | return *mVkManager.get(); |
| 274 | } |
| 275 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 276 | void RenderThread::dumpGraphicsMemory(int fd) { |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 277 | globalProfileData()->dump(fd); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 278 | |
| 279 | String8 cachesOutput; |
| 280 | String8 pipeline; |
| 281 | auto renderType = Properties::getRenderPipelineType(); |
| 282 | switch (renderType) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 283 | case RenderPipelineType::SkiaGL: { |
| 284 | mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState); |
| 285 | pipeline.appendFormat("Skia (OpenGL)"); |
| 286 | break; |
| 287 | } |
| 288 | case RenderPipelineType::SkiaVulkan: { |
| 289 | mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState); |
| 290 | pipeline.appendFormat("Skia (Vulkan)"); |
| 291 | break; |
| 292 | } |
| 293 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 294 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 298 | dprintf(fd, "\n%s\n", cachesOutput.string()); |
| 299 | dprintf(fd, "\nPipeline=%s\n", pipeline.string()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 302 | Readback& RenderThread::readback() { |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 303 | if (!mReadback) { |
Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 304 | mReadback = new Readback(*this); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return *mReadback; |
| 308 | } |
| 309 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 310 | void RenderThread::setGrContext(sk_sp<GrDirectContext> context) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 311 | mCacheManager->reset(context); |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 312 | if (mGrContext) { |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 313 | mRenderState->onContextDestroyed(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 314 | mGrContext->releaseResourcesAndAbandonContext(); |
| 315 | } |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 316 | mGrContext = std::move(context); |
Derek Sollenberger | 1766238 | 2018-09-13 14:14:00 -0400 | [diff] [blame] | 317 | if (mGrContext) { |
| 318 | DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize()); |
| 319 | } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 322 | int RenderThread::choreographerCallback(int fd, int events, void* data) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 323 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 324 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 325 | "events=0x%x", |
| 326 | events); |
| 327 | return 0; // remove the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (!(events & Looper::EVENT_INPUT)) { |
| 331 | ALOGW("Received spurious callback for unhandled poll event. " |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 332 | "events=0x%x", |
| 333 | events); |
| 334 | return 1; // keep the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 335 | } |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 336 | RenderThread* rt = reinterpret_cast<RenderThread*>(data); |
| 337 | AChoreographer_handlePendingEvents(rt->mChoreographer, data); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 338 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 339 | return 1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void RenderThread::dispatchFrameCallbacks() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 343 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 344 | mFrameCallbackTaskPending = false; |
| 345 | |
| 346 | std::set<IFrameCallback*> callbacks; |
| 347 | mFrameCallbacks.swap(callbacks); |
| 348 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 349 | if (callbacks.size()) { |
| 350 | // Assume one of them will probably animate again so preemptively |
| 351 | // request the next vsync in case it occurs mid-frame |
| 352 | requestVsync(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 353 | for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); |
| 354 | it++) { |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 355 | (*it)->doFrame(); |
| 356 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 360 | void RenderThread::requestVsync() { |
| 361 | if (!mVsyncRequested) { |
| 362 | mVsyncRequested = true; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 363 | mVsyncSource->requestNextVsync(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 367 | bool RenderThread::threadLoop() { |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 368 | setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY); |
John Reck | 700079e | 2019-02-19 10:38:50 -0800 | [diff] [blame] | 369 | Looper::setForThread(mLooper); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 370 | if (gOnStartHook) { |
Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame] | 371 | gOnStartHook("RenderThread"); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 372 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 373 | initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 374 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 375 | while (true) { |
| 376 | waitForWork(); |
| 377 | processQueue(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 378 | |
| 379 | if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) { |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 380 | mVsyncSource->drainPendingEvents(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 381 | mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(), |
| 382 | mPendingRegistrationFrameCallbacks.end()); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 383 | mPendingRegistrationFrameCallbacks.clear(); |
| 384 | requestVsync(); |
| 385 | } |
John Reck | a22c9b2 | 2015-01-14 10:40:15 -0800 | [diff] [blame] | 386 | |
| 387 | if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) { |
| 388 | // TODO: Clean this up. This is working around an issue where a combination |
| 389 | // of bad timing and slow drawing can result in dropping a stale vsync |
| 390 | // on the floor (correct!) but fails to schedule to listen for the |
| 391 | // next vsync (oops), so none of the callbacks are run. |
| 392 | requestVsync(); |
| 393 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | return false; |
| 397 | } |
| 398 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 399 | void RenderThread::postFrameCallback(IFrameCallback* callback) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 400 | mPendingRegistrationFrameCallbacks.insert(callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 401 | } |
| 402 | |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 403 | bool RenderThread::removeFrameCallback(IFrameCallback* callback) { |
| 404 | size_t erased; |
| 405 | erased = mFrameCallbacks.erase(callback); |
| 406 | erased |= mPendingRegistrationFrameCallbacks.erase(callback); |
| 407 | return erased; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void RenderThread::pushBackFrameCallback(IFrameCallback* callback) { |
| 411 | if (mFrameCallbacks.erase(callback)) { |
| 412 | mPendingRegistrationFrameCallbacks.insert(callback); |
| 413 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 416 | sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) { |
| 417 | auto renderType = Properties::getRenderPipelineType(); |
| 418 | switch (renderType) { |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 419 | case RenderPipelineType::SkiaVulkan: |
| 420 | return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 421 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 422 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 423 | break; |
| 424 | } |
| 425 | return nullptr; |
| 426 | } |
| 427 | |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 428 | bool RenderThread::isCurrent() { |
| 429 | return gettid() == getInstance().getTid(); |
| 430 | } |
| 431 | |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 432 | void RenderThread::preload() { |
Stan Iliev | 30b9096 | 2019-03-29 14:25:09 -0400 | [diff] [blame] | 433 | // EGL driver is always preloaded only if HWUI renders with GL. |
| 434 | if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 435 | std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); }); |
Stan Iliev | 30b9096 | 2019-03-29 14:25:09 -0400 | [diff] [blame] | 436 | eglInitThread.detach(); |
Yiwei Zhang | bf37175 | 2020-07-31 21:03:29 +0000 | [diff] [blame] | 437 | } else { |
| 438 | requireVkContext(); |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 439 | } |
Yiwei Zhang | bf37175 | 2020-07-31 21:03:29 +0000 | [diff] [blame] | 440 | HardwareBitmapUploader::initialize(); |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 441 | } |
| 442 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 443 | } /* namespace renderthread */ |
| 444 | } /* namespace uirenderer */ |
| 445 | } /* namespace android */ |