blob: 4a4e2540dd6f112b132b4d2ea5c8b9a43c51c316 [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
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 Recke45b1fd2014-04-15 09:50:16 -070021#include <gui/DisplayEventReceiver.h>
22#include <utils/Log.h>
23
John Reck4f02bf42014-01-03 18:09:17 -080024#include "CanvasContext.h"
25#include "RenderProxy.h"
John Reckcec24ae2013-11-05 13:27:50 -080026
27namespace android {
28using namespace uirenderer::renderthread;
29ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread);
30
31namespace uirenderer {
32namespace renderthread {
33
John Recke45b1fd2014-04-15 09:50:16 -070034// 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
John Recka5dda642014-05-22 15:43:54 -070040static const int DISPATCH_FRAME_CALLBACKS_DELAY = 4;
John Recke45b1fd2014-04-15 09:50:16 -070041
John Reck4f02bf42014-01-03 18:09:17 -080042TaskQueue::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
John Recka5dda642014-05-22 15:43:54 -070094void TaskQueue::queueAtFront(RenderTask* task) {
95 if (mTail) {
96 task->mNext = mHead;
97 mHead = task;
98 } else {
99 mTail = mHead = task;
100 }
101}
102
John Reck4f02bf42014-01-03 18:09:17 -0800103void 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 Recke45b1fd2014-04-15 09:50:16 -0700125class DispatchFrameCallbacks : public RenderTask {
126private:
127 RenderThread* mRenderThread;
128public:
129 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
130
131 virtual void run() {
132 mRenderThread->dispatchFrameCallbacks();
133 }
134};
135
John Reckcec24ae2013-11-05 13:27:50 -0800136RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>()
John Recke45b1fd2014-04-15 09:50:16 -0700137 , mNextWakeup(LLONG_MAX)
138 , mDisplayEventReceiver(0)
139 , mVsyncRequested(false)
140 , mFrameCallbackTaskPending(false)
John Reck18f16e62014-05-02 16:46:41 -0700141 , mFrameCallbackTask(0) {
John Recke45b1fd2014-04-15 09:50:16 -0700142 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800143 mLooper = new Looper(false);
144 run("RenderThread");
145}
146
147RenderThread::~RenderThread() {
148}
149
John Recke45b1fd2014-04-15 09:50:16 -0700150void 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
162int 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
180static 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 Recka5dda642014-05-22 15:43:54 -0700200void RenderThread::drainDisplayEventQueue(bool skipCallbacks) {
201 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700202 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
203 if (vsyncEvent > 0) {
204 mVsyncRequested = false;
John Reck18f16e62014-05-02 16:46:41 -0700205 mTimeLord.vsyncReceived(vsyncEvent);
John Recka5dda642014-05-22 15:43:54 -0700206 if (!skipCallbacks && !mFrameCallbackTaskPending) {
207 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700208 mFrameCallbackTaskPending = true;
John Recka5dda642014-05-22 15:43:54 -0700209 queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY);
John Recke45b1fd2014-04-15 09:50:16 -0700210 }
211 }
212}
213
214void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700215 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700216 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 Reck18f16e62014-05-02 16:46:41 -0700222 (*it)->doFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700223 }
224}
225
John Recka5dda642014-05-22 15:43:54 -0700226void 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 Reckcec24ae2013-11-05 13:27:50 -0800235bool RenderThread::threadLoop() {
John Recke45b1fd2014-04-15 09:50:16 -0700236 initializeDisplayEventReceiver();
237
John Reck4f02bf42014-01-03 18:09:17 -0800238 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800239 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700240 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800241 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
242 "RenderThread Looper POLL_ERROR!");
243
244 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800245 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800246 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800247 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800248 // task may have deleted itself, do not reference it again
249 }
250 if (nextWakeup == LLONG_MAX) {
251 timeoutMillis = -1;
252 } else {
John Recka6260b82014-01-29 18:31:51 -0800253 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
254 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800255 if (timeoutMillis < 0) {
256 timeoutMillis = 0;
257 }
John Reckcec24ae2013-11-05 13:27:50 -0800258 }
John Recka5dda642014-05-22 15:43:54 -0700259
260 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
261 drainDisplayEventQueue(true);
262 mFrameCallbacks.insert(
263 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
264 mPendingRegistrationFrameCallbacks.clear();
265 requestVsync();
266 }
John Reckcec24ae2013-11-05 13:27:50 -0800267 }
268
269 return false;
270}
271
272void RenderThread::queue(RenderTask* task) {
273 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800274 mQueue.queue(task);
275 if (mNextWakeup && task->mRunAt < mNextWakeup) {
276 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800277 mLooper->wake();
278 }
279}
280
John Recka5dda642014-05-22 15:43:54 -0700281void RenderThread::queueAtFront(RenderTask* task) {
282 AutoMutex _lock(mLock);
283 mQueue.queueAtFront(task);
284 mLooper->wake();
285}
286
John Reck4f02bf42014-01-03 18:09:17 -0800287void RenderThread::queueDelayed(RenderTask* task, int delayMs) {
288 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Recka6260b82014-01-29 18:31:51 -0800289 task->mRunAt = now + milliseconds_to_nanoseconds(delayMs);
John Reck4f02bf42014-01-03 18:09:17 -0800290 queue(task);
291}
292
293void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800294 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800295 mQueue.remove(task);
296}
297
John Recke45b1fd2014-04-15 09:50:16 -0700298void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700299 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700300}
301
302void RenderThread::removeFrameCallback(IFrameCallback* callback) {
303 mFrameCallbacks.erase(callback);
John Recka5dda642014-05-22 15:43:54 -0700304 mPendingRegistrationFrameCallbacks.erase(callback);
305}
306
307void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
308 if (mFrameCallbacks.erase(callback)) {
309 mPendingRegistrationFrameCallbacks.insert(callback);
310 }
John Recke45b1fd2014-04-15 09:50:16 -0700311}
312
John Reck4f02bf42014-01-03 18:09:17 -0800313RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
314 AutoMutex _lock(mLock);
315 RenderTask* next = mQueue.peek();
316 if (!next) {
317 mNextWakeup = LLONG_MAX;
318 } else {
John Recka5dda642014-05-22 15:43:54 -0700319 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800320 // 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 Recka5dda642014-05-22 15:43:54 -0700323 } else {
324 next = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800325 }
John Reckcec24ae2013-11-05 13:27:50 -0800326 }
John Reck4f02bf42014-01-03 18:09:17 -0800327 if (nextWakeup) {
328 *nextWakeup = mNextWakeup;
329 }
330 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800331}
332
333} /* namespace renderthread */
334} /* namespace uirenderer */
335} /* namespace android */