RenderThread.cpp revision e45b1fd03b524d2b57cc6c222d89076a31a08bea
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 21#include <gui/DisplayEventReceiver.h> 22#include <utils/Log.h> 23 24#include "CanvasContext.h" 25#include "RenderProxy.h" 26 27namespace android { 28using namespace uirenderer::renderthread; 29ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread); 30 31namespace uirenderer { 32namespace renderthread { 33 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. 37static 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 40static const int DISPATCH_FRAME_CALLBACKS_DELAY = 0; 41 42TaskQueue::TaskQueue() : mHead(0), mTail(0) {} 43 44RenderTask* 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 56RenderTask* TaskQueue::peek() { 57 return mHead; 58} 59 60void 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 94void TaskQueue::remove(RenderTask* task) { 95 // TaskQueue is strict here to enforce that users are keeping track of 96 // their RenderTasks due to how their memory is managed 97 LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task, 98 "Cannot remove a task that isn't in the queue!"); 99 100 // If task is the head we can just call next() to pop it off 101 // Otherwise we need to scan through to find the task before it 102 if (peek() == task) { 103 next(); 104 } else { 105 RenderTask* previous = mHead; 106 while (previous->mNext != task) { 107 previous = previous->mNext; 108 } 109 previous->mNext = task->mNext; 110 if (mTail == task) { 111 mTail = previous; 112 } 113 } 114} 115 116class DispatchFrameCallbacks : public RenderTask { 117private: 118 RenderThread* mRenderThread; 119public: 120 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {} 121 122 virtual void run() { 123 mRenderThread->dispatchFrameCallbacks(); 124 } 125}; 126 127RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>() 128 , mNextWakeup(LLONG_MAX) 129 , mDisplayEventReceiver(0) 130 , mVsyncRequested(false) 131 , mFrameCallbackTaskPending(false) 132 , mFrameCallbackTask(0) 133 , mFrameTime(0) { 134 mFrameCallbackTask = new DispatchFrameCallbacks(this); 135 mLooper = new Looper(false); 136 run("RenderThread"); 137} 138 139RenderThread::~RenderThread() { 140} 141 142void RenderThread::initializeDisplayEventReceiver() { 143 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?"); 144 mDisplayEventReceiver = new DisplayEventReceiver(); 145 status_t status = mDisplayEventReceiver->initCheck(); 146 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver " 147 "failed with status: %d", status); 148 149 // Register the FD 150 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, 151 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this); 152} 153 154int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) { 155 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) { 156 ALOGE("Display event receiver pipe was closed or an error occurred. " 157 "events=0x%x", events); 158 return 0; // remove the callback 159 } 160 161 if (!(events & Looper::EVENT_INPUT)) { 162 ALOGW("Received spurious callback for unhandled poll event. " 163 "events=0x%x", events); 164 return 1; // keep the callback 165 } 166 167 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue(); 168 169 return 1; // keep the callback 170} 171 172static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) { 173 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE]; 174 nsecs_t latest = 0; 175 ssize_t n; 176 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) { 177 for (ssize_t i = 0; i < n; i++) { 178 const DisplayEventReceiver::Event& ev = buf[i]; 179 switch (ev.header.type) { 180 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: 181 latest = ev.header.timestamp; 182 break; 183 } 184 } 185 } 186 if (n < 0) { 187 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n)); 188 } 189 return latest; 190} 191 192void RenderThread::drainDisplayEventQueue() { 193 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver); 194 if (vsyncEvent > 0) { 195 mVsyncRequested = false; 196 mFrameTime = vsyncEvent; 197 if (!mFrameCallbackTaskPending) { 198 mFrameCallbackTaskPending = true; 199 //queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY); 200 queue(mFrameCallbackTask); 201 } 202 } 203} 204 205void RenderThread::dispatchFrameCallbacks() { 206 mFrameCallbackTaskPending = false; 207 208 std::set<IFrameCallback*> callbacks; 209 mFrameCallbacks.swap(callbacks); 210 211 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) { 212 (*it)->doFrame(mFrameTime); 213 } 214} 215 216bool RenderThread::threadLoop() { 217 initializeDisplayEventReceiver(); 218 219 int timeoutMillis = -1; 220 for (;;) { 221 int result = mLooper->pollOnce(timeoutMillis); 222 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR, 223 "RenderThread Looper POLL_ERROR!"); 224 225 nsecs_t nextWakeup; 226 // Process our queue, if we have anything 227 while (RenderTask* task = nextTask(&nextWakeup)) { 228 task->run(); 229 // task may have deleted itself, do not reference it again 230 } 231 if (nextWakeup == LLONG_MAX) { 232 timeoutMillis = -1; 233 } else { 234 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC); 235 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos); 236 if (timeoutMillis < 0) { 237 timeoutMillis = 0; 238 } 239 } 240 } 241 242 return false; 243} 244 245void RenderThread::queue(RenderTask* task) { 246 AutoMutex _lock(mLock); 247 mQueue.queue(task); 248 if (mNextWakeup && task->mRunAt < mNextWakeup) { 249 mNextWakeup = 0; 250 mLooper->wake(); 251 } 252} 253 254void RenderThread::queueDelayed(RenderTask* task, int delayMs) { 255 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); 256 task->mRunAt = now + milliseconds_to_nanoseconds(delayMs); 257 queue(task); 258} 259 260void RenderThread::remove(RenderTask* task) { 261 AutoMutex _lock(mLock); 262 mQueue.remove(task); 263} 264 265void RenderThread::postFrameCallback(IFrameCallback* callback) { 266 mFrameCallbacks.insert(callback); 267 if (!mVsyncRequested) { 268 mVsyncRequested = true; 269 status_t status = mDisplayEventReceiver->requestNextVsync(); 270 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, 271 "requestNextVsync failed with status: %d", status); 272 } 273} 274 275void RenderThread::removeFrameCallback(IFrameCallback* callback) { 276 mFrameCallbacks.erase(callback); 277} 278 279RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) { 280 AutoMutex _lock(mLock); 281 RenderTask* next = mQueue.peek(); 282 if (!next) { 283 mNextWakeup = LLONG_MAX; 284 } else { 285 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls 286 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) { 287 next = mQueue.next(); 288 } 289 mNextWakeup = next->mRunAt; 290 } 291 if (nextWakeup) { 292 *nextWakeup = mNextWakeup; 293 } 294 return next; 295} 296 297} /* namespace renderthread */ 298} /* namespace uirenderer */ 299} /* namespace android */ 300