blob: 9a0fbadb8fd4732fefd6057970af920a937f797a [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 Recke45b1fd2014-04-15 09:50:16 -0700154void RenderThread::initializeDisplayEventReceiver() {
155 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
156 mDisplayEventReceiver = new DisplayEventReceiver();
157 status_t status = mDisplayEventReceiver->initCheck();
158 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
159 "failed with status: %d", status);
160
161 // Register the FD
162 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
163 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
164}
165
John Reck3b202512014-06-23 13:13:08 -0700166void RenderThread::initThreadLocals() {
167 initializeDisplayEventReceiver();
168 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700169 mRenderState = new RenderState(*this);
John Reck3b202512014-06-23 13:13:08 -0700170}
171
John Recke45b1fd2014-04-15 09:50:16 -0700172int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
173 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
174 ALOGE("Display event receiver pipe was closed or an error occurred. "
175 "events=0x%x", events);
176 return 0; // remove the callback
177 }
178
179 if (!(events & Looper::EVENT_INPUT)) {
180 ALOGW("Received spurious callback for unhandled poll event. "
181 "events=0x%x", events);
182 return 1; // keep the callback
183 }
184
185 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
186
187 return 1; // keep the callback
188}
189
190static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
191 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
192 nsecs_t latest = 0;
193 ssize_t n;
194 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
195 for (ssize_t i = 0; i < n; i++) {
196 const DisplayEventReceiver::Event& ev = buf[i];
197 switch (ev.header.type) {
198 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
199 latest = ev.header.timestamp;
200 break;
201 }
202 }
203 }
204 if (n < 0) {
205 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
206 }
207 return latest;
208}
209
John Recka733f892014-12-19 11:37:21 -0800210void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700211 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700212 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
213 if (vsyncEvent > 0) {
214 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800215 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700216 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700217 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800218 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
219 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700220 }
221 }
222}
223
224void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700225 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700226 mFrameCallbackTaskPending = false;
227
228 std::set<IFrameCallback*> callbacks;
229 mFrameCallbacks.swap(callbacks);
230
John Recka733f892014-12-19 11:37:21 -0800231 if (callbacks.size()) {
232 // Assume one of them will probably animate again so preemptively
233 // request the next vsync in case it occurs mid-frame
234 requestVsync();
235 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
236 (*it)->doFrame();
237 }
John Recke45b1fd2014-04-15 09:50:16 -0700238 }
239}
240
John Recka5dda642014-05-22 15:43:54 -0700241void RenderThread::requestVsync() {
242 if (!mVsyncRequested) {
243 mVsyncRequested = true;
244 status_t status = mDisplayEventReceiver->requestNextVsync();
245 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
246 "requestNextVsync failed with status: %d", status);
247 }
248}
249
John Reckcec24ae2013-11-05 13:27:50 -0800250bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700251 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700252 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700253
John Reck4f02bf42014-01-03 18:09:17 -0800254 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800255 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700256 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800257 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
258 "RenderThread Looper POLL_ERROR!");
259
260 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800261 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800262 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800263 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800264 // task may have deleted itself, do not reference it again
265 }
266 if (nextWakeup == LLONG_MAX) {
267 timeoutMillis = -1;
268 } else {
John Recka6260b82014-01-29 18:31:51 -0800269 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
270 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800271 if (timeoutMillis < 0) {
272 timeoutMillis = 0;
273 }
John Reckcec24ae2013-11-05 13:27:50 -0800274 }
John Recka5dda642014-05-22 15:43:54 -0700275
276 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800277 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700278 mFrameCallbacks.insert(
279 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
280 mPendingRegistrationFrameCallbacks.clear();
281 requestVsync();
282 }
John Recka22c9b22015-01-14 10:40:15 -0800283
284 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
285 // TODO: Clean this up. This is working around an issue where a combination
286 // of bad timing and slow drawing can result in dropping a stale vsync
287 // on the floor (correct!) but fails to schedule to listen for the
288 // next vsync (oops), so none of the callbacks are run.
289 requestVsync();
290 }
John Reckcec24ae2013-11-05 13:27:50 -0800291 }
292
293 return false;
294}
295
296void RenderThread::queue(RenderTask* task) {
297 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800298 mQueue.queue(task);
299 if (mNextWakeup && task->mRunAt < mNextWakeup) {
300 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800301 mLooper->wake();
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 Recka733f892014-12-19 11:37:21 -0800311void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
312 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800313 queue(task);
314}
315
316void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800317 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800318 mQueue.remove(task);
319}
320
John Recke45b1fd2014-04-15 09:50:16 -0700321void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700322 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700323}
324
John Reck01a5ea32014-12-03 13:01:07 -0800325bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
326 size_t erased;
327 erased = mFrameCallbacks.erase(callback);
328 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
329 return erased;
John Recka5dda642014-05-22 15:43:54 -0700330}
331
332void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
333 if (mFrameCallbacks.erase(callback)) {
334 mPendingRegistrationFrameCallbacks.insert(callback);
335 }
John Recke45b1fd2014-04-15 09:50:16 -0700336}
337
John Reck4f02bf42014-01-03 18:09:17 -0800338RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
339 AutoMutex _lock(mLock);
340 RenderTask* next = mQueue.peek();
341 if (!next) {
342 mNextWakeup = LLONG_MAX;
343 } else {
John Recka5dda642014-05-22 15:43:54 -0700344 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800345 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
346 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
347 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700348 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800349 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800350 }
John Reckcec24ae2013-11-05 13:27:50 -0800351 }
John Reck4f02bf42014-01-03 18:09:17 -0800352 if (nextWakeup) {
353 *nextWakeup = mNextWakeup;
354 }
355 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800356}
357
John Reckcec24ae2013-11-05 13:27:50 -0800358} /* namespace renderthread */
359} /* namespace uirenderer */
360} /* namespace android */