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 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame^] | 19 | #include "hwui/Bitmap.h" |
| 20 | #include "renderstate/RenderState.h" |
| 21 | #include "renderthread/OpenGLPipeline.h" |
| 22 | #include "pipeline/skia/SkiaOpenGLReadback.h" |
| 23 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
| 24 | #include "pipeline/skia/SkiaVulkanPipeline.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 25 | #include "CanvasContext.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 26 | #include "EglManager.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 27 | #include "OpenGLReadback.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 28 | #include "RenderProxy.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 29 | #include "VulkanManager.h" |
John Reck | 12efa55 | 2016-11-15 10:22:01 -0800 | [diff] [blame] | 30 | #include "utils/FatVector.h" |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 31 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 32 | #include <gui/DisplayEventReceiver.h> |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 33 | #include <gui/ISurfaceComposer.h> |
| 34 | #include <gui/SurfaceComposerClient.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 35 | #include <sys/resource.h> |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 36 | #include <utils/Condition.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 37 | #include <utils/Log.h> |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 38 | #include <utils/Mutex.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 39 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 40 | namespace android { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 41 | namespace uirenderer { |
| 42 | namespace renderthread { |
| 43 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 44 | // Number of events to read at a time from the DisplayEventReceiver pipe. |
| 45 | // The value should be large enough that we can quickly drain the pipe |
| 46 | // using just a few large reads. |
| 47 | static const size_t EVENT_BUFFER_SIZE = 100; |
| 48 | |
| 49 | // 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] | 50 | static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 51 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 52 | TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {} |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 53 | |
| 54 | RenderTask* TaskQueue::next() { |
| 55 | RenderTask* ret = mHead; |
| 56 | if (ret) { |
| 57 | mHead = ret->mNext; |
| 58 | if (!mHead) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 59 | mTail = nullptr; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 60 | } |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 61 | ret->mNext = nullptr; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 62 | } |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | RenderTask* TaskQueue::peek() { |
| 67 | return mHead; |
| 68 | } |
| 69 | |
| 70 | void TaskQueue::queue(RenderTask* task) { |
| 71 | // Since the RenderTask itself forms the linked list it is not allowed |
| 72 | // to have the same task queued twice |
| 73 | LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!"); |
| 74 | if (mTail) { |
| 75 | // Fast path if we can just append |
| 76 | if (mTail->mRunAt <= task->mRunAt) { |
| 77 | mTail->mNext = task; |
| 78 | mTail = task; |
| 79 | } else { |
| 80 | // Need to find the proper insertion point |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 81 | RenderTask* previous = nullptr; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 82 | RenderTask* next = mHead; |
| 83 | while (next && next->mRunAt <= task->mRunAt) { |
| 84 | previous = next; |
| 85 | next = next->mNext; |
| 86 | } |
| 87 | if (!previous) { |
| 88 | task->mNext = mHead; |
| 89 | mHead = task; |
| 90 | } else { |
| 91 | previous->mNext = task; |
| 92 | if (next) { |
| 93 | task->mNext = next; |
| 94 | } else { |
| 95 | mTail = task; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } else { |
| 100 | mTail = mHead = task; |
| 101 | } |
| 102 | } |
| 103 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 104 | void TaskQueue::queueAtFront(RenderTask* task) { |
John Reck | 2f94448 | 2017-03-27 14:34:28 -0700 | [diff] [blame] | 105 | LOG_ALWAYS_FATAL_IF(task->mNext || mHead == task, "Task is already in the queue!"); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 106 | if (mTail) { |
| 107 | task->mNext = mHead; |
| 108 | mHead = task; |
| 109 | } else { |
| 110 | mTail = mHead = task; |
| 111 | } |
| 112 | } |
| 113 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 114 | void TaskQueue::remove(RenderTask* task) { |
| 115 | // TaskQueue is strict here to enforce that users are keeping track of |
| 116 | // their RenderTasks due to how their memory is managed |
| 117 | LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task, |
| 118 | "Cannot remove a task that isn't in the queue!"); |
| 119 | |
| 120 | // If task is the head we can just call next() to pop it off |
| 121 | // Otherwise we need to scan through to find the task before it |
| 122 | if (peek() == task) { |
| 123 | next(); |
| 124 | } else { |
| 125 | RenderTask* previous = mHead; |
| 126 | while (previous->mNext != task) { |
| 127 | previous = previous->mNext; |
| 128 | } |
| 129 | previous->mNext = task->mNext; |
| 130 | if (mTail == task) { |
| 131 | mTail = previous; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 136 | class DispatchFrameCallbacks : public RenderTask { |
| 137 | private: |
| 138 | RenderThread* mRenderThread; |
| 139 | public: |
Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 140 | explicit DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {} |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 141 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 142 | virtual void run() override { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 143 | mRenderThread->dispatchFrameCallbacks(); |
| 144 | } |
| 145 | }; |
| 146 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 147 | static bool gHasRenderThreadInstance = false; |
| 148 | |
| 149 | bool RenderThread::hasInstance() { |
| 150 | return gHasRenderThreadInstance; |
| 151 | } |
| 152 | |
| 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 | |
| 162 | RenderThread::RenderThread() : Thread(true) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 163 | , mNextWakeup(LLONG_MAX) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 164 | , mDisplayEventReceiver(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 | , mFrameCallbackTask(nullptr) |
| 168 | , mRenderState(nullptr) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 169 | , mEglManager(nullptr) |
| 170 | , mVkManager(nullptr) { |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 171 | Properties::load(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 172 | mFrameCallbackTask = new DispatchFrameCallbacks(this); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 173 | mLooper = new Looper(false); |
| 174 | run("RenderThread"); |
| 175 | } |
| 176 | |
| 177 | RenderThread::~RenderThread() { |
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 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 181 | void RenderThread::initializeDisplayEventReceiver() { |
| 182 | LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?"); |
| 183 | mDisplayEventReceiver = new DisplayEventReceiver(); |
| 184 | status_t status = mDisplayEventReceiver->initCheck(); |
| 185 | LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver " |
| 186 | "failed with status: %d", status); |
| 187 | |
| 188 | // Register the FD |
| 189 | mLooper->addFd(mDisplayEventReceiver->getFd(), 0, |
| 190 | Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this); |
| 191 | } |
| 192 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 193 | void RenderThread::initThreadLocals() { |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 194 | sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay( |
| 195 | ISurfaceComposer::eDisplayIdMain)); |
| 196 | status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo); |
| 197 | LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n"); |
| 198 | nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps); |
| 199 | mTimeLord.setFrameInterval(frameIntervalNanos); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 200 | initializeDisplayEventReceiver(); |
| 201 | mEglManager = new EglManager(*this); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 202 | mRenderState = new RenderState(*this); |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 203 | mJankTracker = new JankTracker(mDisplayInfo); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 204 | mVkManager = new VulkanManager(*this); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 207 | Readback& RenderThread::readback() { |
| 208 | |
| 209 | if (!mReadback) { |
| 210 | auto renderType = Properties::getRenderPipelineType(); |
| 211 | switch (renderType) { |
| 212 | case RenderPipelineType::OpenGL: |
| 213 | mReadback = new OpenGLReadbackImpl(*this); |
| 214 | break; |
| 215 | case RenderPipelineType::SkiaGL: |
| 216 | case RenderPipelineType::SkiaVulkan: |
| 217 | // It works to use the OpenGL pipeline for Vulkan but this is not |
| 218 | // ideal as it causes us to create an OpenGL context in addition |
| 219 | // to the Vulkan one. |
| 220 | mReadback = new skiapipeline::SkiaOpenGLReadback(*this); |
| 221 | break; |
| 222 | default: |
| 223 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return *mReadback; |
| 229 | } |
| 230 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 231 | int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) { |
| 232 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 233 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
| 234 | "events=0x%x", events); |
| 235 | return 0; // remove the callback |
| 236 | } |
| 237 | |
| 238 | if (!(events & Looper::EVENT_INPUT)) { |
| 239 | ALOGW("Received spurious callback for unhandled poll event. " |
| 240 | "events=0x%x", events); |
| 241 | return 1; // keep the callback |
| 242 | } |
| 243 | |
| 244 | reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue(); |
| 245 | |
| 246 | return 1; // keep the callback |
| 247 | } |
| 248 | |
| 249 | static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) { |
| 250 | DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; |
| 251 | nsecs_t latest = 0; |
| 252 | ssize_t n; |
| 253 | while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { |
| 254 | for (ssize_t i = 0; i < n; i++) { |
| 255 | const DisplayEventReceiver::Event& ev = buf[i]; |
| 256 | switch (ev.header.type) { |
| 257 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 258 | latest = ev.header.timestamp; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | if (n < 0) { |
| 264 | ALOGW("Failed to get events from display event receiver, status=%d", status_t(n)); |
| 265 | } |
| 266 | return latest; |
| 267 | } |
| 268 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 269 | void RenderThread::drainDisplayEventQueue() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 270 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 271 | nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver); |
| 272 | if (vsyncEvent > 0) { |
| 273 | mVsyncRequested = false; |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 274 | if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 275 | ATRACE_NAME("queue mFrameCallbackTask"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 276 | mFrameCallbackTaskPending = true; |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 277 | nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY); |
| 278 | queueAt(mFrameCallbackTask, runAt); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void RenderThread::dispatchFrameCallbacks() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 284 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 285 | mFrameCallbackTaskPending = false; |
| 286 | |
| 287 | std::set<IFrameCallback*> callbacks; |
| 288 | mFrameCallbacks.swap(callbacks); |
| 289 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 290 | if (callbacks.size()) { |
| 291 | // Assume one of them will probably animate again so preemptively |
| 292 | // request the next vsync in case it occurs mid-frame |
| 293 | requestVsync(); |
| 294 | for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) { |
| 295 | (*it)->doFrame(); |
| 296 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 300 | void RenderThread::requestVsync() { |
| 301 | if (!mVsyncRequested) { |
| 302 | mVsyncRequested = true; |
| 303 | status_t status = mDisplayEventReceiver->requestNextVsync(); |
| 304 | LOG_ALWAYS_FATAL_IF(status != NO_ERROR, |
| 305 | "requestNextVsync failed with status: %d", status); |
| 306 | } |
| 307 | } |
| 308 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 309 | bool RenderThread::threadLoop() { |
John Reck | 21be43e | 2014-08-14 10:25:16 -0700 | [diff] [blame] | 310 | setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 311 | initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 312 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 313 | int timeoutMillis = -1; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 314 | for (;;) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 315 | int result = mLooper->pollOnce(timeoutMillis); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 316 | LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR, |
| 317 | "RenderThread Looper POLL_ERROR!"); |
| 318 | |
| 319 | nsecs_t nextWakeup; |
John Reck | 12efa55 | 2016-11-15 10:22:01 -0800 | [diff] [blame] | 320 | { |
| 321 | FatVector<RenderTask*, 10> workQueue; |
| 322 | // Process our queue, if we have anything. By first acquiring |
| 323 | // all the pending events then processing them we avoid vsync |
| 324 | // starvation if more tasks are queued while we are processing tasks. |
| 325 | while (RenderTask* task = nextTask(&nextWakeup)) { |
| 326 | workQueue.push_back(task); |
| 327 | } |
| 328 | for (auto task : workQueue) { |
| 329 | task->run(); |
| 330 | // task may have deleted itself, do not reference it again |
| 331 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 332 | } |
| 333 | if (nextWakeup == LLONG_MAX) { |
| 334 | timeoutMillis = -1; |
| 335 | } else { |
John Reck | a6260b8 | 2014-01-29 18:31:51 -0800 | [diff] [blame] | 336 | nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC); |
| 337 | timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 338 | if (timeoutMillis < 0) { |
| 339 | timeoutMillis = 0; |
| 340 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 341 | } |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 342 | |
| 343 | if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) { |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 344 | drainDisplayEventQueue(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 345 | mFrameCallbacks.insert( |
| 346 | mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end()); |
| 347 | mPendingRegistrationFrameCallbacks.clear(); |
| 348 | requestVsync(); |
| 349 | } |
John Reck | a22c9b2 | 2015-01-14 10:40:15 -0800 | [diff] [blame] | 350 | |
| 351 | if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) { |
| 352 | // TODO: Clean this up. This is working around an issue where a combination |
| 353 | // of bad timing and slow drawing can result in dropping a stale vsync |
| 354 | // on the floor (correct!) but fails to schedule to listen for the |
| 355 | // next vsync (oops), so none of the callbacks are run. |
| 356 | requestVsync(); |
| 357 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | void RenderThread::queue(RenderTask* task) { |
| 364 | AutoMutex _lock(mLock); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 365 | mQueue.queue(task); |
| 366 | if (mNextWakeup && task->mRunAt < mNextWakeup) { |
| 367 | mNextWakeup = 0; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 368 | mLooper->wake(); |
| 369 | } |
| 370 | } |
| 371 | |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 372 | void RenderThread::queueAndWait(RenderTask* task) { |
John Reck | cba287b | 2015-11-10 12:52:44 -0800 | [diff] [blame] | 373 | // These need to be local to the thread to avoid the Condition |
| 374 | // signaling the wrong thread. The easiest way to achieve that is to just |
| 375 | // make this on the stack, although that has a slight cost to it |
| 376 | Mutex mutex; |
| 377 | Condition condition; |
| 378 | SignalingRenderTask syncTask(task, &mutex, &condition); |
| 379 | |
| 380 | AutoMutex _lock(mutex); |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 381 | queue(&syncTask); |
Tom Cherry | 298a146 | 2017-02-28 14:07:09 -0800 | [diff] [blame] | 382 | while (!syncTask.hasRun()) { |
| 383 | condition.wait(mutex); |
| 384 | } |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 385 | } |
| 386 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 387 | void RenderThread::queueAtFront(RenderTask* task) { |
| 388 | AutoMutex _lock(mLock); |
| 389 | mQueue.queueAtFront(task); |
| 390 | mLooper->wake(); |
| 391 | } |
| 392 | |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 393 | void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) { |
| 394 | task->mRunAt = runAtNs; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 395 | queue(task); |
| 396 | } |
| 397 | |
| 398 | void RenderThread::remove(RenderTask* task) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 399 | AutoMutex _lock(mLock); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 400 | mQueue.remove(task); |
| 401 | } |
| 402 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 403 | void RenderThread::postFrameCallback(IFrameCallback* callback) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 404 | mPendingRegistrationFrameCallbacks.insert(callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 405 | } |
| 406 | |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 407 | bool RenderThread::removeFrameCallback(IFrameCallback* callback) { |
| 408 | size_t erased; |
| 409 | erased = mFrameCallbacks.erase(callback); |
| 410 | erased |= mPendingRegistrationFrameCallbacks.erase(callback); |
| 411 | return erased; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void RenderThread::pushBackFrameCallback(IFrameCallback* callback) { |
| 415 | if (mFrameCallbacks.erase(callback)) { |
| 416 | mPendingRegistrationFrameCallbacks.insert(callback); |
| 417 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 418 | } |
| 419 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 420 | RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) { |
| 421 | AutoMutex _lock(mLock); |
| 422 | RenderTask* next = mQueue.peek(); |
| 423 | if (!next) { |
| 424 | mNextWakeup = LLONG_MAX; |
| 425 | } else { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 426 | mNextWakeup = next->mRunAt; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 427 | // Most tasks won't be delayed, so avoid unnecessary systemTime() calls |
| 428 | if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) { |
| 429 | next = mQueue.next(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 430 | } else { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 431 | next = nullptr; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 432 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 433 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 434 | if (nextWakeup) { |
| 435 | *nextWakeup = mNextWakeup; |
| 436 | } |
| 437 | return next; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame^] | 440 | sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) { |
| 441 | auto renderType = Properties::getRenderPipelineType(); |
| 442 | switch (renderType) { |
| 443 | case RenderPipelineType::OpenGL: |
| 444 | return OpenGLPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 445 | case RenderPipelineType::SkiaGL: |
| 446 | return skiapipeline::SkiaOpenGLPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 447 | case RenderPipelineType::SkiaVulkan: |
| 448 | return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap); |
| 449 | default: |
| 450 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType); |
| 451 | break; |
| 452 | } |
| 453 | return nullptr; |
| 454 | } |
| 455 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 456 | } /* namespace renderthread */ |
| 457 | } /* namespace uirenderer */ |
| 458 | } /* namespace android */ |