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