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 | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 19 | #include "CanvasContext.h" |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 20 | #include "DeviceInfo.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 21 | #include "EglManager.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 22 | #include "RenderProxy.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 23 | #include "VulkanManager.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include "hwui/Bitmap.h" |
| 25 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
| 26 | #include "pipeline/skia/SkiaOpenGLReadback.h" |
John Reck | ec10097 | 2018-04-05 16:41:41 -0700 | [diff] [blame] | 27 | #include "pipeline/skia/SkiaVulkanReadback.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 | 12efa55 | 2016-11-15 10:22:01 -0800 | [diff] [blame] | 30 | #include "utils/FatVector.h" |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 31 | #include "utils/TimeUtils.h" |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 32 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 33 | #include <gui/DisplayEventReceiver.h> |
| 34 | #include <sys/resource.h> |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 35 | #include <utils/Condition.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 36 | #include <utils/Log.h> |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 37 | #include <utils/Mutex.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 38 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 39 | namespace android { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 40 | namespace uirenderer { |
| 41 | namespace renderthread { |
| 42 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 43 | // Number of events to read at a time from the DisplayEventReceiver pipe. |
| 44 | // The value should be large enough that we can quickly drain the pipe |
| 45 | // using just a few large reads. |
| 46 | static const size_t EVENT_BUFFER_SIZE = 100; |
| 47 | |
| 48 | // Slight delay to give the UI time to push us a new frame before we replay |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 49 | static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 50 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 51 | static bool gHasRenderThreadInstance = false; |
| 52 | |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 53 | static void (*gOnStartHook)() = nullptr; |
| 54 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 55 | class DisplayEventReceiverWrapper : public VsyncSource { |
| 56 | public: |
| 57 | DisplayEventReceiverWrapper(std::unique_ptr<DisplayEventReceiver>&& receiver) |
| 58 | : mDisplayEventReceiver(std::move(receiver)) {} |
| 59 | |
| 60 | virtual void requestNextVsync() override { |
| 61 | status_t status = mDisplayEventReceiver->requestNextVsync(); |
| 62 | LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "requestNextVsync failed with status: %d", status); |
| 63 | } |
| 64 | |
| 65 | virtual nsecs_t latestVsyncEvent() override { |
| 66 | DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; |
| 67 | nsecs_t latest = 0; |
| 68 | ssize_t n; |
| 69 | while ((n = mDisplayEventReceiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { |
| 70 | for (ssize_t i = 0; i < n; i++) { |
| 71 | const DisplayEventReceiver::Event& ev = buf[i]; |
| 72 | switch (ev.header.type) { |
| 73 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 74 | latest = ev.header.timestamp; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | if (n < 0) { |
| 80 | ALOGW("Failed to get events from display event receiver, status=%d", status_t(n)); |
| 81 | } |
| 82 | return latest; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver; |
| 87 | }; |
| 88 | |
| 89 | class DummyVsyncSource : public VsyncSource { |
| 90 | public: |
| 91 | DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {} |
| 92 | |
| 93 | virtual void requestNextVsync() override { |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame^] | 94 | mRenderThread->queue().postDelayed(16_ms, [this]() { |
| 95 | mRenderThread->drainDisplayEventQueue(); |
| 96 | }); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame^] | 99 | virtual nsecs_t latestVsyncEvent() override { |
| 100 | return systemTime(CLOCK_MONOTONIC); |
| 101 | } |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 102 | |
| 103 | private: |
| 104 | RenderThread* mRenderThread; |
| 105 | }; |
| 106 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 107 | bool RenderThread::hasInstance() { |
| 108 | return gHasRenderThreadInstance; |
| 109 | } |
| 110 | |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 111 | void RenderThread::setOnStartHook(void (*onStartHook)()) { |
| 112 | LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started..."); |
| 113 | gOnStartHook = onStartHook; |
| 114 | } |
| 115 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 116 | RenderThread& RenderThread::getInstance() { |
| 117 | // This is a pointer because otherwise __cxa_finalize |
| 118 | // will try to delete it like a Good Citizen but that causes us to crash |
| 119 | // because we don't want to delete the RenderThread normally. |
| 120 | static RenderThread* sInstance = new RenderThread(); |
| 121 | gHasRenderThreadInstance = true; |
| 122 | return *sInstance; |
| 123 | } |
| 124 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 125 | RenderThread::RenderThread() |
| 126 | : ThreadBase() |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 127 | , mVsyncSource(nullptr) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 128 | , mVsyncRequested(false) |
| 129 | , mFrameCallbackTaskPending(false) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 130 | , mRenderState(nullptr) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 131 | , mEglManager(nullptr) |
| 132 | , mVkManager(nullptr) { |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 133 | Properties::load(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 134 | start("RenderThread"); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | RenderThread::~RenderThread() { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 138 | LOG_ALWAYS_FATAL("Can't destroy the render thread"); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 139 | } |
| 140 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 141 | void RenderThread::initializeDisplayEventReceiver() { |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 142 | LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second DisplayEventReceiver?"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 143 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 144 | if (!Properties::isolatedProcess) { |
| 145 | auto receiver = std::make_unique<DisplayEventReceiver>(); |
| 146 | status_t status = receiver->initCheck(); |
| 147 | LOG_ALWAYS_FATAL_IF(status != NO_ERROR, |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame^] | 148 | "Initialization of DisplayEventReceiver " |
| 149 | "failed with status: %d", |
| 150 | status); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 151 | |
| 152 | // Register the FD |
| 153 | mLooper->addFd(receiver->getFd(), 0, Looper::EVENT_INPUT, |
John Reck | 1a4a981 | 2018-04-18 16:13:31 -0700 | [diff] [blame^] | 154 | RenderThread::displayEventReceiverCallback, this); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 155 | mVsyncSource = new DisplayEventReceiverWrapper(std::move(receiver)); |
| 156 | } else { |
| 157 | mVsyncSource = new DummyVsyncSource(this); |
| 158 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 161 | void RenderThread::initThreadLocals() { |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 162 | mDisplayInfo = DeviceInfo::queryDisplayInfo(); |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 163 | nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps); |
| 164 | mTimeLord.setFrameInterval(frameIntervalNanos); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 165 | initializeDisplayEventReceiver(); |
| 166 | mEglManager = new EglManager(*this); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 167 | mRenderState = new RenderState(*this); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 168 | mVkManager = new VulkanManager(*this); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 169 | mCacheManager = new CacheManager(mDisplayInfo); |
| 170 | } |
| 171 | |
| 172 | void RenderThread::dumpGraphicsMemory(int fd) { |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 173 | globalProfileData()->dump(fd); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 174 | |
| 175 | String8 cachesOutput; |
| 176 | String8 pipeline; |
| 177 | auto renderType = Properties::getRenderPipelineType(); |
| 178 | switch (renderType) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 179 | case RenderPipelineType::SkiaGL: { |
| 180 | mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState); |
| 181 | pipeline.appendFormat("Skia (OpenGL)"); |
| 182 | break; |
| 183 | } |
| 184 | case RenderPipelineType::SkiaVulkan: { |
| 185 | mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState); |
| 186 | pipeline.appendFormat("Skia (Vulkan)"); |
| 187 | break; |
| 188 | } |
| 189 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 190 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | |
John Reck | 47f5c3a | 2017-11-13 11:32:39 -0800 | [diff] [blame] | 194 | dprintf(fd, "\n%s\n", cachesOutput.string()); |
| 195 | dprintf(fd, "\nPipeline=%s\n", pipeline.string()); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 198 | Readback& RenderThread::readback() { |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 199 | if (!mReadback) { |
| 200 | auto renderType = Properties::getRenderPipelineType(); |
| 201 | switch (renderType) { |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 202 | case RenderPipelineType::SkiaGL: |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 203 | mReadback = new skiapipeline::SkiaOpenGLReadback(*this); |
| 204 | break; |
Stan Iliev | a31973fe | 2018-01-12 11:50:29 -0500 | [diff] [blame] | 205 | case RenderPipelineType::SkiaVulkan: |
| 206 | mReadback = new skiapipeline::SkiaVulkanReadback(*this); |
| 207 | break; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 208 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 209 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return *mReadback; |
| 215 | } |
| 216 | |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 217 | void RenderThread::setGrContext(sk_sp<GrContext> context) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 218 | mCacheManager->reset(context); |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 219 | if (mGrContext) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 220 | mGrContext->releaseResourcesAndAbandonContext(); |
| 221 | } |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 222 | mGrContext = std::move(context); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 223 | } |
| 224 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 225 | int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) { |
| 226 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 227 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 228 | "events=0x%x", |
| 229 | events); |
| 230 | return 0; // remove the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (!(events & Looper::EVENT_INPUT)) { |
| 234 | ALOGW("Received spurious callback for unhandled poll event. " |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 235 | "events=0x%x", |
| 236 | events); |
| 237 | return 1; // keep the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue(); |
| 241 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 242 | return 1; // keep the callback |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 243 | } |
| 244 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 245 | void RenderThread::drainDisplayEventQueue() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 246 | ATRACE_CALL(); |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 247 | nsecs_t vsyncEvent = mVsyncSource->latestVsyncEvent(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 248 | if (vsyncEvent > 0) { |
| 249 | mVsyncRequested = false; |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 250 | if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 251 | ATRACE_NAME("queue mFrameCallbackTask"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 252 | mFrameCallbackTaskPending = true; |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 253 | nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 254 | queue().postAt(runAt, [this]() { dispatchFrameCallbacks(); }); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void RenderThread::dispatchFrameCallbacks() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 260 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 261 | mFrameCallbackTaskPending = false; |
| 262 | |
| 263 | std::set<IFrameCallback*> callbacks; |
| 264 | mFrameCallbacks.swap(callbacks); |
| 265 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 266 | if (callbacks.size()) { |
| 267 | // Assume one of them will probably animate again so preemptively |
| 268 | // request the next vsync in case it occurs mid-frame |
| 269 | requestVsync(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 270 | for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); |
| 271 | it++) { |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 272 | (*it)->doFrame(); |
| 273 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 277 | void RenderThread::requestVsync() { |
| 278 | if (!mVsyncRequested) { |
| 279 | mVsyncRequested = true; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 280 | mVsyncSource->requestNextVsync(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 284 | bool RenderThread::threadLoop() { |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 285 | setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 286 | if (gOnStartHook) { |
| 287 | gOnStartHook(); |
| 288 | } |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 289 | initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 290 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 291 | while (true) { |
| 292 | waitForWork(); |
| 293 | processQueue(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 294 | |
| 295 | if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) { |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 296 | drainDisplayEventQueue(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 297 | mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(), |
| 298 | mPendingRegistrationFrameCallbacks.end()); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 299 | mPendingRegistrationFrameCallbacks.clear(); |
| 300 | requestVsync(); |
| 301 | } |
John Reck | a22c9b2 | 2015-01-14 10:40:15 -0800 | [diff] [blame] | 302 | |
| 303 | if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) { |
| 304 | // TODO: Clean this up. This is working around an issue where a combination |
| 305 | // of bad timing and slow drawing can result in dropping a stale vsync |
| 306 | // on the floor (correct!) but fails to schedule to listen for the |
| 307 | // next vsync (oops), so none of the callbacks are run. |
| 308 | requestVsync(); |
| 309 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 315 | void RenderThread::postFrameCallback(IFrameCallback* callback) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 316 | mPendingRegistrationFrameCallbacks.insert(callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 317 | } |
| 318 | |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 319 | bool RenderThread::removeFrameCallback(IFrameCallback* callback) { |
| 320 | size_t erased; |
| 321 | erased = mFrameCallbacks.erase(callback); |
| 322 | erased |= mPendingRegistrationFrameCallbacks.erase(callback); |
| 323 | return erased; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void RenderThread::pushBackFrameCallback(IFrameCallback* callback) { |
| 327 | if (mFrameCallbacks.erase(callback)) { |
| 328 | mPendingRegistrationFrameCallbacks.insert(callback); |
| 329 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 332 | sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) { |
| 333 | auto renderType = Properties::getRenderPipelineType(); |
| 334 | switch (renderType) { |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 335 | case RenderPipelineType::SkiaGL: |
| 336 | return skiapipeline::SkiaOpenGLPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 337 | case RenderPipelineType::SkiaVulkan: |
| 338 | return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 339 | default: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 340 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | return nullptr; |
| 344 | } |
| 345 | |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 346 | bool RenderThread::isCurrent() { |
| 347 | return gettid() == getInstance().getTid(); |
| 348 | } |
| 349 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 350 | } /* namespace renderthread */ |
| 351 | } /* namespace uirenderer */ |
| 352 | } /* namespace android */ |