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