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