blob: 68033dca7c55323495cc103d5f3b8b40283fa86b [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 Reck21be43e2014-08-14 10:25:16 -070021#if defined(HAVE_PTHREADS)
22#include <sys/resource.h>
23#endif
John Recke45b1fd2014-04-15 09:50:16 -070024#include <gui/DisplayEventReceiver.h>
25#include <utils/Log.h>
26
John Reck3b202512014-06-23 13:13:08 -070027#include "../RenderState.h"
John Reck4f02bf42014-01-03 18:09:17 -080028#include "CanvasContext.h"
John Reck3b202512014-06-23 13:13:08 -070029#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080030#include "RenderProxy.h"
John Reckcec24ae2013-11-05 13:27:50 -080031
32namespace android {
33using namespace uirenderer::renderthread;
34ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread);
35
36namespace uirenderer {
37namespace renderthread {
38
John Recke45b1fd2014-04-15 09:50:16 -070039// 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.
42static 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 Recka5dda642014-05-22 15:43:54 -070045static const int DISPATCH_FRAME_CALLBACKS_DELAY = 4;
John Recke45b1fd2014-04-15 09:50:16 -070046
John Reck4f02bf42014-01-03 18:09:17 -080047TaskQueue::TaskQueue() : mHead(0), mTail(0) {}
48
49RenderTask* 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
61RenderTask* TaskQueue::peek() {
62 return mHead;
63}
64
65void 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 Recka5dda642014-05-22 15:43:54 -070099void TaskQueue::queueAtFront(RenderTask* task) {
100 if (mTail) {
101 task->mNext = mHead;
102 mHead = task;
103 } else {
104 mTail = mHead = task;
105 }
106}
107
John Reck4f02bf42014-01-03 18:09:17 -0800108void 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 Recke45b1fd2014-04-15 09:50:16 -0700130class DispatchFrameCallbacks : public RenderTask {
131private:
132 RenderThread* mRenderThread;
133public:
134 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
135
136 virtual void run() {
137 mRenderThread->dispatchFrameCallbacks();
138 }
139};
140
John Reckcec24ae2013-11-05 13:27:50 -0800141RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>()
John Recke45b1fd2014-04-15 09:50:16 -0700142 , mNextWakeup(LLONG_MAX)
143 , mDisplayEventReceiver(0)
144 , mVsyncRequested(false)
145 , mFrameCallbackTaskPending(false)
John Reck3b202512014-06-23 13:13:08 -0700146 , mFrameCallbackTask(0)
147 , mRenderState(NULL)
148 , mEglManager(NULL) {
John Recke45b1fd2014-04-15 09:50:16 -0700149 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800150 mLooper = new Looper(false);
151 run("RenderThread");
152}
153
154RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700155 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800156}
157
John Recke45b1fd2014-04-15 09:50:16 -0700158void 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 Reck3b202512014-06-23 13:13:08 -0700170void RenderThread::initThreadLocals() {
171 initializeDisplayEventReceiver();
172 mEglManager = new EglManager(*this);
173 mRenderState = new RenderState();
174}
175
John Recke45b1fd2014-04-15 09:50:16 -0700176int 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
194static 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 Recka5dda642014-05-22 15:43:54 -0700214void RenderThread::drainDisplayEventQueue(bool skipCallbacks) {
215 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700216 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
217 if (vsyncEvent > 0) {
218 mVsyncRequested = false;
John Reck18f16e62014-05-02 16:46:41 -0700219 mTimeLord.vsyncReceived(vsyncEvent);
John Recka5dda642014-05-22 15:43:54 -0700220 if (!skipCallbacks && !mFrameCallbackTaskPending) {
221 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700222 mFrameCallbackTaskPending = true;
John Recka5dda642014-05-22 15:43:54 -0700223 queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY);
John Recke45b1fd2014-04-15 09:50:16 -0700224 }
225 }
226}
227
228void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700229 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700230 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 Reck18f16e62014-05-02 16:46:41 -0700236 (*it)->doFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700237 }
238}
239
John Recka5dda642014-05-22 15:43:54 -0700240void 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 Reckcec24ae2013-11-05 13:27:50 -0800249bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700250#if defined(HAVE_PTHREADS)
251 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
252#endif
John Reck3b202512014-06-23 13:13:08 -0700253 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700254
John Reck4f02bf42014-01-03 18:09:17 -0800255 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800256 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700257 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800258 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
259 "RenderThread Looper POLL_ERROR!");
260
261 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800262 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800263 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800264 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800265 // task may have deleted itself, do not reference it again
266 }
267 if (nextWakeup == LLONG_MAX) {
268 timeoutMillis = -1;
269 } else {
John Recka6260b82014-01-29 18:31:51 -0800270 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
271 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800272 if (timeoutMillis < 0) {
273 timeoutMillis = 0;
274 }
John Reckcec24ae2013-11-05 13:27:50 -0800275 }
John Recka5dda642014-05-22 15:43:54 -0700276
277 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
278 drainDisplayEventQueue(true);
279 mFrameCallbacks.insert(
280 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
281 mPendingRegistrationFrameCallbacks.clear();
282 requestVsync();
283 }
John Reckcec24ae2013-11-05 13:27:50 -0800284 }
285
286 return false;
287}
288
289void RenderThread::queue(RenderTask* task) {
290 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800291 mQueue.queue(task);
292 if (mNextWakeup && task->mRunAt < mNextWakeup) {
293 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800294 mLooper->wake();
295 }
296}
297
John Recka5dda642014-05-22 15:43:54 -0700298void RenderThread::queueAtFront(RenderTask* task) {
299 AutoMutex _lock(mLock);
300 mQueue.queueAtFront(task);
301 mLooper->wake();
302}
303
John Reck4f02bf42014-01-03 18:09:17 -0800304void RenderThread::queueDelayed(RenderTask* task, int delayMs) {
305 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Recka6260b82014-01-29 18:31:51 -0800306 task->mRunAt = now + milliseconds_to_nanoseconds(delayMs);
John Reck4f02bf42014-01-03 18:09:17 -0800307 queue(task);
308}
309
310void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800311 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800312 mQueue.remove(task);
313}
314
John Recke45b1fd2014-04-15 09:50:16 -0700315void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700316 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700317}
318
319void RenderThread::removeFrameCallback(IFrameCallback* callback) {
320 mFrameCallbacks.erase(callback);
John Recka5dda642014-05-22 15:43:54 -0700321 mPendingRegistrationFrameCallbacks.erase(callback);
322}
323
324void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
325 if (mFrameCallbacks.erase(callback)) {
326 mPendingRegistrationFrameCallbacks.insert(callback);
327 }
John Recke45b1fd2014-04-15 09:50:16 -0700328}
329
John Reck4f02bf42014-01-03 18:09:17 -0800330RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
331 AutoMutex _lock(mLock);
332 RenderTask* next = mQueue.peek();
333 if (!next) {
334 mNextWakeup = LLONG_MAX;
335 } else {
John Recka5dda642014-05-22 15:43:54 -0700336 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800337 // 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 Recka5dda642014-05-22 15:43:54 -0700340 } else {
341 next = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800342 }
John Reckcec24ae2013-11-05 13:27:50 -0800343 }
John Reck4f02bf42014-01-03 18:09:17 -0800344 if (nextWakeup) {
345 *nextWakeup = mNextWakeup;
346 }
347 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800348}
349
John Reckcec24ae2013-11-05 13:27:50 -0800350} /* namespace renderthread */
351} /* namespace uirenderer */
352} /* namespace android */