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 | |
| 17 | #define LOG_TAG "RenderThread" |
| 18 | |
| 19 | #include "RenderThread.h" |
| 20 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 21 | #include <gui/DisplayEventReceiver.h> |
| 22 | #include <utils/Log.h> |
| 23 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 24 | #include "CanvasContext.h" |
| 25 | #include "RenderProxy.h" |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | using namespace uirenderer::renderthread; |
| 29 | ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread); |
| 30 | |
| 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 | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 40 | static const int DISPATCH_FRAME_CALLBACKS_DELAY = 4; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 41 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 42 | TaskQueue::TaskQueue() : mHead(0), mTail(0) {} |
| 43 | |
| 44 | RenderTask* TaskQueue::next() { |
| 45 | RenderTask* ret = mHead; |
| 46 | if (ret) { |
| 47 | mHead = ret->mNext; |
| 48 | if (!mHead) { |
| 49 | mTail = 0; |
| 50 | } |
| 51 | ret->mNext = 0; |
| 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 |
| 71 | RenderTask* previous = 0; |
| 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 | |
| 131 | virtual void run() { |
| 132 | mRenderThread->dispatchFrameCallbacks(); |
| 133 | } |
| 134 | }; |
| 135 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 136 | RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>() |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 137 | , mNextWakeup(LLONG_MAX) |
| 138 | , mDisplayEventReceiver(0) |
| 139 | , mVsyncRequested(false) |
| 140 | , mFrameCallbackTaskPending(false) |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 141 | , mFrameCallbackTask(0) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 142 | mFrameCallbackTask = new DispatchFrameCallbacks(this); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 143 | mLooper = new Looper(false); |
| 144 | run("RenderThread"); |
| 145 | } |
| 146 | |
| 147 | RenderThread::~RenderThread() { |
| 148 | } |
| 149 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 150 | void RenderThread::initializeDisplayEventReceiver() { |
| 151 | LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?"); |
| 152 | mDisplayEventReceiver = new DisplayEventReceiver(); |
| 153 | status_t status = mDisplayEventReceiver->initCheck(); |
| 154 | LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver " |
| 155 | "failed with status: %d", status); |
| 156 | |
| 157 | // Register the FD |
| 158 | mLooper->addFd(mDisplayEventReceiver->getFd(), 0, |
| 159 | Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this); |
| 160 | } |
| 161 | |
| 162 | int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) { |
| 163 | if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { |
| 164 | ALOGE("Display event receiver pipe was closed or an error occurred. " |
| 165 | "events=0x%x", events); |
| 166 | return 0; // remove the callback |
| 167 | } |
| 168 | |
| 169 | if (!(events & Looper::EVENT_INPUT)) { |
| 170 | ALOGW("Received spurious callback for unhandled poll event. " |
| 171 | "events=0x%x", events); |
| 172 | return 1; // keep the callback |
| 173 | } |
| 174 | |
| 175 | reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue(); |
| 176 | |
| 177 | return 1; // keep the callback |
| 178 | } |
| 179 | |
| 180 | static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) { |
| 181 | DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; |
| 182 | nsecs_t latest = 0; |
| 183 | ssize_t n; |
| 184 | while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { |
| 185 | for (ssize_t i = 0; i < n; i++) { |
| 186 | const DisplayEventReceiver::Event& ev = buf[i]; |
| 187 | switch (ev.header.type) { |
| 188 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 189 | latest = ev.header.timestamp; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | if (n < 0) { |
| 195 | ALOGW("Failed to get events from display event receiver, status=%d", status_t(n)); |
| 196 | } |
| 197 | return latest; |
| 198 | } |
| 199 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 200 | void RenderThread::drainDisplayEventQueue(bool skipCallbacks) { |
| 201 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 202 | nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver); |
| 203 | if (vsyncEvent > 0) { |
| 204 | mVsyncRequested = false; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 205 | mTimeLord.vsyncReceived(vsyncEvent); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 206 | if (!skipCallbacks && !mFrameCallbackTaskPending) { |
| 207 | ATRACE_NAME("queue mFrameCallbackTask"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 208 | mFrameCallbackTaskPending = true; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 209 | queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void RenderThread::dispatchFrameCallbacks() { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 215 | ATRACE_CALL(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 216 | mFrameCallbackTaskPending = false; |
| 217 | |
| 218 | std::set<IFrameCallback*> callbacks; |
| 219 | mFrameCallbacks.swap(callbacks); |
| 220 | |
| 221 | for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) { |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 222 | (*it)->doFrame(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 226 | void RenderThread::requestVsync() { |
| 227 | if (!mVsyncRequested) { |
| 228 | mVsyncRequested = true; |
| 229 | status_t status = mDisplayEventReceiver->requestNextVsync(); |
| 230 | LOG_ALWAYS_FATAL_IF(status != NO_ERROR, |
| 231 | "requestNextVsync failed with status: %d", status); |
| 232 | } |
| 233 | } |
| 234 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 235 | bool RenderThread::threadLoop() { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 236 | initializeDisplayEventReceiver(); |
| 237 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 238 | int timeoutMillis = -1; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 239 | for (;;) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 240 | int result = mLooper->pollOnce(timeoutMillis); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 241 | LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR, |
| 242 | "RenderThread Looper POLL_ERROR!"); |
| 243 | |
| 244 | nsecs_t nextWakeup; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 245 | // Process our queue, if we have anything |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 246 | while (RenderTask* task = nextTask(&nextWakeup)) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 247 | task->run(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 248 | // task may have deleted itself, do not reference it again |
| 249 | } |
| 250 | if (nextWakeup == LLONG_MAX) { |
| 251 | timeoutMillis = -1; |
| 252 | } else { |
John Reck | a6260b8 | 2014-01-29 18:31:51 -0800 | [diff] [blame] | 253 | nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC); |
| 254 | timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 255 | if (timeoutMillis < 0) { |
| 256 | timeoutMillis = 0; |
| 257 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 258 | } |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 259 | |
| 260 | if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) { |
| 261 | drainDisplayEventQueue(true); |
| 262 | mFrameCallbacks.insert( |
| 263 | mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end()); |
| 264 | mPendingRegistrationFrameCallbacks.clear(); |
| 265 | requestVsync(); |
| 266 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | void RenderThread::queue(RenderTask* task) { |
| 273 | AutoMutex _lock(mLock); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 274 | mQueue.queue(task); |
| 275 | if (mNextWakeup && task->mRunAt < mNextWakeup) { |
| 276 | mNextWakeup = 0; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 277 | mLooper->wake(); |
| 278 | } |
| 279 | } |
| 280 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 281 | void RenderThread::queueAtFront(RenderTask* task) { |
| 282 | AutoMutex _lock(mLock); |
| 283 | mQueue.queueAtFront(task); |
| 284 | mLooper->wake(); |
| 285 | } |
| 286 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 287 | void RenderThread::queueDelayed(RenderTask* task, int delayMs) { |
| 288 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | a6260b8 | 2014-01-29 18:31:51 -0800 | [diff] [blame] | 289 | task->mRunAt = now + milliseconds_to_nanoseconds(delayMs); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 290 | queue(task); |
| 291 | } |
| 292 | |
| 293 | void RenderThread::remove(RenderTask* task) { |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 294 | AutoMutex _lock(mLock); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 295 | mQueue.remove(task); |
| 296 | } |
| 297 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 298 | void RenderThread::postFrameCallback(IFrameCallback* callback) { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 299 | mPendingRegistrationFrameCallbacks.insert(callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void RenderThread::removeFrameCallback(IFrameCallback* callback) { |
| 303 | mFrameCallbacks.erase(callback); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 304 | mPendingRegistrationFrameCallbacks.erase(callback); |
| 305 | } |
| 306 | |
| 307 | void RenderThread::pushBackFrameCallback(IFrameCallback* callback) { |
| 308 | if (mFrameCallbacks.erase(callback)) { |
| 309 | mPendingRegistrationFrameCallbacks.insert(callback); |
| 310 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 311 | } |
| 312 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 313 | RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) { |
| 314 | AutoMutex _lock(mLock); |
| 315 | RenderTask* next = mQueue.peek(); |
| 316 | if (!next) { |
| 317 | mNextWakeup = LLONG_MAX; |
| 318 | } else { |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 319 | mNextWakeup = next->mRunAt; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 320 | // Most tasks won't be delayed, so avoid unnecessary systemTime() calls |
| 321 | if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) { |
| 322 | next = mQueue.next(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame^] | 323 | } else { |
| 324 | next = 0; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 325 | } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 326 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 327 | if (nextWakeup) { |
| 328 | *nextWakeup = mNextWakeup; |
| 329 | } |
| 330 | return next; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | } /* namespace renderthread */ |
| 334 | } /* namespace uirenderer */ |
| 335 | } /* namespace android */ |