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