blob: 32dc46ee57ebc4a016839be7a12c3e6846850f3c [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>
John Reck73b7a4d2014-07-23 14:54:04 -070023#include <pthread.h>
John Recke45b1fd2014-04-15 09:50:16 -070024
John Reck3b202512014-06-23 13:13:08 -070025#include "../RenderState.h"
John Reck4f02bf42014-01-03 18:09:17 -080026#include "CanvasContext.h"
John Reck3b202512014-06-23 13:13:08 -070027#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080028#include "RenderProxy.h"
John Reckcec24ae2013-11-05 13:27:50 -080029
30namespace android {
31using namespace uirenderer::renderthread;
32ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread);
33
34namespace uirenderer {
35namespace renderthread {
36
John Recke45b1fd2014-04-15 09:50:16 -070037// Number of events to read at a time from the DisplayEventReceiver pipe.
38// The value should be large enough that we can quickly drain the pipe
39// using just a few large reads.
40static const size_t EVENT_BUFFER_SIZE = 100;
41
42// Slight delay to give the UI time to push us a new frame before we replay
John Recka5dda642014-05-22 15:43:54 -070043static const int DISPATCH_FRAME_CALLBACKS_DELAY = 4;
John Recke45b1fd2014-04-15 09:50:16 -070044
John Reck4f02bf42014-01-03 18:09:17 -080045TaskQueue::TaskQueue() : mHead(0), mTail(0) {}
46
47RenderTask* TaskQueue::next() {
48 RenderTask* ret = mHead;
49 if (ret) {
50 mHead = ret->mNext;
51 if (!mHead) {
52 mTail = 0;
53 }
54 ret->mNext = 0;
55 }
56 return ret;
57}
58
59RenderTask* TaskQueue::peek() {
60 return mHead;
61}
62
63void TaskQueue::queue(RenderTask* task) {
64 // Since the RenderTask itself forms the linked list it is not allowed
65 // to have the same task queued twice
66 LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!");
67 if (mTail) {
68 // Fast path if we can just append
69 if (mTail->mRunAt <= task->mRunAt) {
70 mTail->mNext = task;
71 mTail = task;
72 } else {
73 // Need to find the proper insertion point
74 RenderTask* previous = 0;
75 RenderTask* next = mHead;
76 while (next && next->mRunAt <= task->mRunAt) {
77 previous = next;
78 next = next->mNext;
79 }
80 if (!previous) {
81 task->mNext = mHead;
82 mHead = task;
83 } else {
84 previous->mNext = task;
85 if (next) {
86 task->mNext = next;
87 } else {
88 mTail = task;
89 }
90 }
91 }
92 } else {
93 mTail = mHead = task;
94 }
95}
96
John Recka5dda642014-05-22 15:43:54 -070097void TaskQueue::queueAtFront(RenderTask* task) {
98 if (mTail) {
99 task->mNext = mHead;
100 mHead = task;
101 } else {
102 mTail = mHead = task;
103 }
104}
105
John Reck4f02bf42014-01-03 18:09:17 -0800106void TaskQueue::remove(RenderTask* task) {
107 // TaskQueue is strict here to enforce that users are keeping track of
108 // their RenderTasks due to how their memory is managed
109 LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task,
110 "Cannot remove a task that isn't in the queue!");
111
112 // If task is the head we can just call next() to pop it off
113 // Otherwise we need to scan through to find the task before it
114 if (peek() == task) {
115 next();
116 } else {
117 RenderTask* previous = mHead;
118 while (previous->mNext != task) {
119 previous = previous->mNext;
120 }
121 previous->mNext = task->mNext;
122 if (mTail == task) {
123 mTail = previous;
124 }
125 }
126}
127
John Recke45b1fd2014-04-15 09:50:16 -0700128class DispatchFrameCallbacks : public RenderTask {
129private:
130 RenderThread* mRenderThread;
131public:
132 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
133
134 virtual void run() {
135 mRenderThread->dispatchFrameCallbacks();
136 }
137};
138
John Reckcec24ae2013-11-05 13:27:50 -0800139RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>()
John Reck73b7a4d2014-07-23 14:54:04 -0700140 , mThreadId(0)
John Recke45b1fd2014-04-15 09:50:16 -0700141 , mNextWakeup(LLONG_MAX)
142 , mDisplayEventReceiver(0)
143 , mVsyncRequested(false)
144 , mFrameCallbackTaskPending(false)
John Reck3b202512014-06-23 13:13:08 -0700145 , mFrameCallbackTask(0)
146 , mRenderState(NULL)
147 , mEglManager(NULL) {
John Recke45b1fd2014-04-15 09:50:16 -0700148 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800149 mLooper = new Looper(false);
150 run("RenderThread");
151}
152
153RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700154 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800155}
156
John Recke45b1fd2014-04-15 09:50:16 -0700157void RenderThread::initializeDisplayEventReceiver() {
158 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
159 mDisplayEventReceiver = new DisplayEventReceiver();
160 status_t status = mDisplayEventReceiver->initCheck();
161 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
162 "failed with status: %d", status);
163
164 // Register the FD
165 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
166 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
167}
168
John Reck3b202512014-06-23 13:13:08 -0700169void RenderThread::initThreadLocals() {
170 initializeDisplayEventReceiver();
171 mEglManager = new EglManager(*this);
172 mRenderState = new RenderState();
173}
174
John Recke45b1fd2014-04-15 09:50:16 -0700175int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
176 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
177 ALOGE("Display event receiver pipe was closed or an error occurred. "
178 "events=0x%x", events);
179 return 0; // remove the callback
180 }
181
182 if (!(events & Looper::EVENT_INPUT)) {
183 ALOGW("Received spurious callback for unhandled poll event. "
184 "events=0x%x", events);
185 return 1; // keep the callback
186 }
187
188 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
189
190 return 1; // keep the callback
191}
192
193static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
194 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
195 nsecs_t latest = 0;
196 ssize_t n;
197 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
198 for (ssize_t i = 0; i < n; i++) {
199 const DisplayEventReceiver::Event& ev = buf[i];
200 switch (ev.header.type) {
201 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
202 latest = ev.header.timestamp;
203 break;
204 }
205 }
206 }
207 if (n < 0) {
208 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
209 }
210 return latest;
211}
212
John Recka5dda642014-05-22 15:43:54 -0700213void RenderThread::drainDisplayEventQueue(bool skipCallbacks) {
214 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700215 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
216 if (vsyncEvent > 0) {
217 mVsyncRequested = false;
John Reck18f16e62014-05-02 16:46:41 -0700218 mTimeLord.vsyncReceived(vsyncEvent);
John Recka5dda642014-05-22 15:43:54 -0700219 if (!skipCallbacks && !mFrameCallbackTaskPending) {
220 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700221 mFrameCallbackTaskPending = true;
John Recka5dda642014-05-22 15:43:54 -0700222 queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY);
John Recke45b1fd2014-04-15 09:50:16 -0700223 }
224 }
225}
226
227void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700228 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700229 mFrameCallbackTaskPending = false;
230
231 std::set<IFrameCallback*> callbacks;
232 mFrameCallbacks.swap(callbacks);
233
234 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
John Reck18f16e62014-05-02 16:46:41 -0700235 (*it)->doFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700236 }
237}
238
John Recka5dda642014-05-22 15:43:54 -0700239void RenderThread::requestVsync() {
240 if (!mVsyncRequested) {
241 mVsyncRequested = true;
242 status_t status = mDisplayEventReceiver->requestNextVsync();
243 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
244 "requestNextVsync failed with status: %d", status);
245 }
246}
247
John Reckcec24ae2013-11-05 13:27:50 -0800248bool RenderThread::threadLoop() {
John Reck73b7a4d2014-07-23 14:54:04 -0700249 mThreadId = pthread_self();
John Reck3b202512014-06-23 13:13:08 -0700250 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700251
John Reck4f02bf42014-01-03 18:09:17 -0800252 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800253 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700254 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800255 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
256 "RenderThread Looper POLL_ERROR!");
257
258 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800259 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800260 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800261 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800262 // task may have deleted itself, do not reference it again
263 }
264 if (nextWakeup == LLONG_MAX) {
265 timeoutMillis = -1;
266 } else {
John Recka6260b82014-01-29 18:31:51 -0800267 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
268 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800269 if (timeoutMillis < 0) {
270 timeoutMillis = 0;
271 }
John Reckcec24ae2013-11-05 13:27:50 -0800272 }
John Recka5dda642014-05-22 15:43:54 -0700273
274 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
275 drainDisplayEventQueue(true);
276 mFrameCallbacks.insert(
277 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
278 mPendingRegistrationFrameCallbacks.clear();
279 requestVsync();
280 }
John Reckcec24ae2013-11-05 13:27:50 -0800281 }
282
283 return false;
284}
285
286void RenderThread::queue(RenderTask* task) {
287 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800288 mQueue.queue(task);
289 if (mNextWakeup && task->mRunAt < mNextWakeup) {
290 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800291 mLooper->wake();
292 }
293}
294
John Reck73b7a4d2014-07-23 14:54:04 -0700295void RenderThread::queueAndWait(RenderTask* task, Condition& signal, Mutex& lock) {
296 static nsecs_t sTimeout = milliseconds(500);
297 queue(task);
298 status_t err = signal.waitRelative(lock, sTimeout);
299 if (CC_UNLIKELY(err != NO_ERROR)) {
300 ALOGE("Timeout waiting for RenderTherad! err=%d", err);
301 nukeFromOrbit();
302 }
303}
304
John Recka5dda642014-05-22 15:43:54 -0700305void RenderThread::queueAtFront(RenderTask* task) {
306 AutoMutex _lock(mLock);
307 mQueue.queueAtFront(task);
308 mLooper->wake();
309}
310
John Reck4f02bf42014-01-03 18:09:17 -0800311void RenderThread::queueDelayed(RenderTask* task, int delayMs) {
312 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Recka6260b82014-01-29 18:31:51 -0800313 task->mRunAt = now + milliseconds_to_nanoseconds(delayMs);
John Reck4f02bf42014-01-03 18:09:17 -0800314 queue(task);
315}
316
317void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800318 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800319 mQueue.remove(task);
320}
321
John Recke45b1fd2014-04-15 09:50:16 -0700322void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700323 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700324}
325
326void RenderThread::removeFrameCallback(IFrameCallback* callback) {
327 mFrameCallbacks.erase(callback);
John Recka5dda642014-05-22 15:43:54 -0700328 mPendingRegistrationFrameCallbacks.erase(callback);
329}
330
331void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
332 if (mFrameCallbacks.erase(callback)) {
333 mPendingRegistrationFrameCallbacks.insert(callback);
334 }
John Recke45b1fd2014-04-15 09:50:16 -0700335}
336
John Reck4f02bf42014-01-03 18:09:17 -0800337RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
338 AutoMutex _lock(mLock);
339 RenderTask* next = mQueue.peek();
340 if (!next) {
341 mNextWakeup = LLONG_MAX;
342 } else {
John Recka5dda642014-05-22 15:43:54 -0700343 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800344 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
345 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
346 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700347 } else {
348 next = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800349 }
John Reckcec24ae2013-11-05 13:27:50 -0800350 }
John Reck4f02bf42014-01-03 18:09:17 -0800351 if (nextWakeup) {
352 *nextWakeup = mNextWakeup;
353 }
354 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800355}
356
John Reck73b7a4d2014-07-23 14:54:04 -0700357void RenderThread::nukeFromOrbit() {
358 pthread_kill(mThreadId, SIGABRT);
359}
360
John Reckcec24ae2013-11-05 13:27:50 -0800361} /* namespace renderthread */
362} /* namespace uirenderer */
363} /* namespace android */