blob: 2a8baa747c5d432bab294f9041ddc78216b24ecb [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
John Reckcec24ae2013-11-05 13:27:50 -080017#include "RenderThread.h"
18
Chris Craik65fe5ee2015-01-26 18:06:29 -080019#include "../renderstate/RenderState.h"
John Reck4f02bf42014-01-03 18:09:17 -080020#include "CanvasContext.h"
John Reck3b202512014-06-23 13:13:08 -070021#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080022#include "RenderProxy.h"
John Reckcec24ae2013-11-05 13:27:50 -080023
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include <gui/DisplayEventReceiver.h>
25#include <sys/resource.h>
26#include <utils/Log.h>
27
John Reckcec24ae2013-11-05 13:27:50 -080028namespace android {
29using namespace uirenderer::renderthread;
30ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread);
31
32namespace uirenderer {
33namespace renderthread {
34
John Recke45b1fd2014-04-15 09:50:16 -070035// 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.
38static 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 Recka733f892014-12-19 11:37:21 -080041static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070042
Chris Craikd41c4d82015-01-05 15:51:13 -080043TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080044
45RenderTask* TaskQueue::next() {
46 RenderTask* ret = mHead;
47 if (ret) {
48 mHead = ret->mNext;
49 if (!mHead) {
Chris Craikd41c4d82015-01-05 15:51:13 -080050 mTail = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080051 }
Chris Craikd41c4d82015-01-05 15:51:13 -080052 ret->mNext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080053 }
54 return ret;
55}
56
57RenderTask* TaskQueue::peek() {
58 return mHead;
59}
60
61void 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
Chris Craikd41c4d82015-01-05 15:51:13 -080072 RenderTask* previous = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080073 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 Recka5dda642014-05-22 15:43:54 -070095void TaskQueue::queueAtFront(RenderTask* task) {
96 if (mTail) {
97 task->mNext = mHead;
98 mHead = task;
99 } else {
100 mTail = mHead = task;
101 }
102}
103
John Reck4f02bf42014-01-03 18:09:17 -0800104void 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 Recke45b1fd2014-04-15 09:50:16 -0700126class DispatchFrameCallbacks : public RenderTask {
127private:
128 RenderThread* mRenderThread;
129public:
130 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
131
Chris Craikd41c4d82015-01-05 15:51:13 -0800132 virtual void run() override {
John Recke45b1fd2014-04-15 09:50:16 -0700133 mRenderThread->dispatchFrameCallbacks();
134 }
135};
136
John Reckcec24ae2013-11-05 13:27:50 -0800137RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>()
John Recke45b1fd2014-04-15 09:50:16 -0700138 , mNextWakeup(LLONG_MAX)
Chris Craikd41c4d82015-01-05 15:51:13 -0800139 , mDisplayEventReceiver(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700140 , mVsyncRequested(false)
141 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800142 , mFrameCallbackTask(nullptr)
143 , mRenderState(nullptr)
144 , mEglManager(nullptr) {
John Recke45b1fd2014-04-15 09:50:16 -0700145 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800146 mLooper = new Looper(false);
147 run("RenderThread");
148}
149
150RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700151 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800152}
153
John Reckba6adf62015-02-19 14:36:50 -0800154void RenderThread::setFrameInterval(nsecs_t frameInterval) {
155 mTimeLord.setFrameInterval(frameInterval);
156 mJankTracker->setFrameInterval(frameInterval);
157}
158
John Recke45b1fd2014-04-15 09:50:16 -0700159void RenderThread::initializeDisplayEventReceiver() {
160 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
161 mDisplayEventReceiver = new DisplayEventReceiver();
162 status_t status = mDisplayEventReceiver->initCheck();
163 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
164 "failed with status: %d", status);
165
166 // Register the FD
167 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
168 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
169}
170
John Reck3b202512014-06-23 13:13:08 -0700171void RenderThread::initThreadLocals() {
172 initializeDisplayEventReceiver();
173 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700174 mRenderState = new RenderState(*this);
John Reckba6adf62015-02-19 14:36:50 -0800175 mJankTracker = new JankTracker(mTimeLord.frameIntervalNanos());
John Reck3b202512014-06-23 13:13:08 -0700176}
177
John Recke45b1fd2014-04-15 09:50:16 -0700178int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
179 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
180 ALOGE("Display event receiver pipe was closed or an error occurred. "
181 "events=0x%x", events);
182 return 0; // remove the callback
183 }
184
185 if (!(events & Looper::EVENT_INPUT)) {
186 ALOGW("Received spurious callback for unhandled poll event. "
187 "events=0x%x", events);
188 return 1; // keep the callback
189 }
190
191 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
192
193 return 1; // keep the callback
194}
195
196static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
197 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
198 nsecs_t latest = 0;
199 ssize_t n;
200 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
201 for (ssize_t i = 0; i < n; i++) {
202 const DisplayEventReceiver::Event& ev = buf[i];
203 switch (ev.header.type) {
204 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
205 latest = ev.header.timestamp;
206 break;
207 }
208 }
209 }
210 if (n < 0) {
211 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
212 }
213 return latest;
214}
215
John Recka733f892014-12-19 11:37:21 -0800216void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700217 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700218 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
219 if (vsyncEvent > 0) {
220 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800221 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700222 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700223 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800224 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
225 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700226 }
227 }
228}
229
230void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700231 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700232 mFrameCallbackTaskPending = false;
233
234 std::set<IFrameCallback*> callbacks;
235 mFrameCallbacks.swap(callbacks);
236
John Recka733f892014-12-19 11:37:21 -0800237 if (callbacks.size()) {
238 // Assume one of them will probably animate again so preemptively
239 // request the next vsync in case it occurs mid-frame
240 requestVsync();
241 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
242 (*it)->doFrame();
243 }
John Recke45b1fd2014-04-15 09:50:16 -0700244 }
245}
246
John Recka5dda642014-05-22 15:43:54 -0700247void RenderThread::requestVsync() {
248 if (!mVsyncRequested) {
249 mVsyncRequested = true;
250 status_t status = mDisplayEventReceiver->requestNextVsync();
251 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
252 "requestNextVsync failed with status: %d", status);
253 }
254}
255
John Reckcec24ae2013-11-05 13:27:50 -0800256bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700257 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700258 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700259
John Reck4f02bf42014-01-03 18:09:17 -0800260 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800261 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700262 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800263 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
264 "RenderThread Looper POLL_ERROR!");
265
266 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800267 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800268 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800269 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800270 // task may have deleted itself, do not reference it again
271 }
272 if (nextWakeup == LLONG_MAX) {
273 timeoutMillis = -1;
274 } else {
John Recka6260b82014-01-29 18:31:51 -0800275 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
276 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800277 if (timeoutMillis < 0) {
278 timeoutMillis = 0;
279 }
John Reckcec24ae2013-11-05 13:27:50 -0800280 }
John Recka5dda642014-05-22 15:43:54 -0700281
282 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800283 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700284 mFrameCallbacks.insert(
285 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
286 mPendingRegistrationFrameCallbacks.clear();
287 requestVsync();
288 }
John Recka22c9b22015-01-14 10:40:15 -0800289
290 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
291 // TODO: Clean this up. This is working around an issue where a combination
292 // of bad timing and slow drawing can result in dropping a stale vsync
293 // on the floor (correct!) but fails to schedule to listen for the
294 // next vsync (oops), so none of the callbacks are run.
295 requestVsync();
296 }
John Reckcec24ae2013-11-05 13:27:50 -0800297 }
298
299 return false;
300}
301
302void RenderThread::queue(RenderTask* task) {
303 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800304 mQueue.queue(task);
305 if (mNextWakeup && task->mRunAt < mNextWakeup) {
306 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800307 mLooper->wake();
308 }
309}
310
John Recka5dda642014-05-22 15:43:54 -0700311void RenderThread::queueAtFront(RenderTask* task) {
312 AutoMutex _lock(mLock);
313 mQueue.queueAtFront(task);
314 mLooper->wake();
315}
316
John Recka733f892014-12-19 11:37:21 -0800317void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
318 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800319 queue(task);
320}
321
322void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800323 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800324 mQueue.remove(task);
325}
326
John Recke45b1fd2014-04-15 09:50:16 -0700327void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700328 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700329}
330
John Reck01a5ea32014-12-03 13:01:07 -0800331bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
332 size_t erased;
333 erased = mFrameCallbacks.erase(callback);
334 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
335 return erased;
John Recka5dda642014-05-22 15:43:54 -0700336}
337
338void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
339 if (mFrameCallbacks.erase(callback)) {
340 mPendingRegistrationFrameCallbacks.insert(callback);
341 }
John Recke45b1fd2014-04-15 09:50:16 -0700342}
343
John Reck4f02bf42014-01-03 18:09:17 -0800344RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
345 AutoMutex _lock(mLock);
346 RenderTask* next = mQueue.peek();
347 if (!next) {
348 mNextWakeup = LLONG_MAX;
349 } else {
John Recka5dda642014-05-22 15:43:54 -0700350 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800351 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
352 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
353 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700354 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800355 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800356 }
John Reckcec24ae2013-11-05 13:27:50 -0800357 }
John Reck4f02bf42014-01-03 18:09:17 -0800358 if (nextWakeup) {
359 *nextWakeup = mNextWakeup;
360 }
361 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800362}
363
John Reckcec24ae2013-11-05 13:27:50 -0800364} /* namespace renderthread */
365} /* namespace uirenderer */
366} /* namespace android */