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