blob: 9dfe75ebf2c700e9a6d1f163f934a99303ce470b [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
John Recke45b1fd2014-04-15 09:50:16 -070019#include <gui/DisplayEventReceiver.h>
Yabin Cui16104862015-01-26 19:43:58 -080020#include <sys/resource.h>
John Recke45b1fd2014-04-15 09:50:16 -070021#include <utils/Log.h>
22
John Reck3b202512014-06-23 13:13:08 -070023#include "../RenderState.h"
John Reck4f02bf42014-01-03 18:09:17 -080024#include "CanvasContext.h"
John Reck3b202512014-06-23 13:13:08 -070025#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080026#include "RenderProxy.h"
John Reckcec24ae2013-11-05 13:27:50 -080027
28namespace 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 Recka5dda642014-05-22 15:43:54 -070041static const int DISPATCH_FRAME_CALLBACKS_DELAY = 4;
John Recke45b1fd2014-04-15 09:50:16 -070042
John Reck4f02bf42014-01-03 18:09:17 -080043TaskQueue::TaskQueue() : mHead(0), mTail(0) {}
44
45RenderTask* TaskQueue::next() {
46 RenderTask* ret = mHead;
47 if (ret) {
48 mHead = ret->mNext;
49 if (!mHead) {
50 mTail = 0;
51 }
52 ret->mNext = 0;
53 }
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
72 RenderTask* previous = 0;
73 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
132 virtual void run() {
133 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)
139 , mDisplayEventReceiver(0)
140 , mVsyncRequested(false)
141 , mFrameCallbackTaskPending(false)
John Reck3b202512014-06-23 13:13:08 -0700142 , mFrameCallbackTask(0)
143 , mRenderState(NULL)
144 , mEglManager(NULL) {
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);
169 mRenderState = new RenderState();
170}
171
John Reck8b59a522014-11-22 00:10:02 +0000172int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
John Recke45b1fd2014-04-15 09:50:16 -0700173 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 Recka5dda642014-05-22 15:43:54 -0700210void RenderThread::drainDisplayEventQueue(bool skipCallbacks) {
211 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700212 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
213 if (vsyncEvent > 0) {
214 mVsyncRequested = false;
John Reck18f16e62014-05-02 16:46:41 -0700215 mTimeLord.vsyncReceived(vsyncEvent);
John Recka5dda642014-05-22 15:43:54 -0700216 if (!skipCallbacks && !mFrameCallbackTaskPending) {
217 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700218 mFrameCallbackTaskPending = true;
John Recka5dda642014-05-22 15:43:54 -0700219 queueDelayed(mFrameCallbackTask, DISPATCH_FRAME_CALLBACKS_DELAY);
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
231 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
John Reck18f16e62014-05-02 16:46:41 -0700232 (*it)->doFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700233 }
234}
235
John Recka5dda642014-05-22 15:43:54 -0700236void RenderThread::requestVsync() {
237 if (!mVsyncRequested) {
238 mVsyncRequested = true;
239 status_t status = mDisplayEventReceiver->requestNextVsync();
240 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
241 "requestNextVsync failed with status: %d", status);
242 }
243}
244
John Reckcec24ae2013-11-05 13:27:50 -0800245bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700246 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700247 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700248
John Reck4f02bf42014-01-03 18:09:17 -0800249 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800250 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700251 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800252 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
253 "RenderThread Looper POLL_ERROR!");
254
255 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800256 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800257 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800258 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800259 // task may have deleted itself, do not reference it again
260 }
261 if (nextWakeup == LLONG_MAX) {
262 timeoutMillis = -1;
263 } else {
John Recka6260b82014-01-29 18:31:51 -0800264 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
265 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800266 if (timeoutMillis < 0) {
267 timeoutMillis = 0;
268 }
John Reckcec24ae2013-11-05 13:27:50 -0800269 }
John Recka5dda642014-05-22 15:43:54 -0700270
271 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
272 drainDisplayEventQueue(true);
273 mFrameCallbacks.insert(
274 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
275 mPendingRegistrationFrameCallbacks.clear();
276 requestVsync();
277 }
John Reckcec24ae2013-11-05 13:27:50 -0800278 }
279
280 return false;
281}
282
283void RenderThread::queue(RenderTask* task) {
284 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800285 mQueue.queue(task);
286 if (mNextWakeup && task->mRunAt < mNextWakeup) {
287 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800288 mLooper->wake();
289 }
290}
291
John Recka5dda642014-05-22 15:43:54 -0700292void RenderThread::queueAtFront(RenderTask* task) {
293 AutoMutex _lock(mLock);
294 mQueue.queueAtFront(task);
295 mLooper->wake();
296}
297
John Reck4f02bf42014-01-03 18:09:17 -0800298void RenderThread::queueDelayed(RenderTask* task, int delayMs) {
299 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
John Recka6260b82014-01-29 18:31:51 -0800300 task->mRunAt = now + milliseconds_to_nanoseconds(delayMs);
John Reck4f02bf42014-01-03 18:09:17 -0800301 queue(task);
302}
303
304void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800305 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800306 mQueue.remove(task);
307}
308
John Recke45b1fd2014-04-15 09:50:16 -0700309void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700310 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700311}
312
313void RenderThread::removeFrameCallback(IFrameCallback* callback) {
314 mFrameCallbacks.erase(callback);
John Recka5dda642014-05-22 15:43:54 -0700315 mPendingRegistrationFrameCallbacks.erase(callback);
316}
317
318void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
319 if (mFrameCallbacks.erase(callback)) {
320 mPendingRegistrationFrameCallbacks.insert(callback);
321 }
John Recke45b1fd2014-04-15 09:50:16 -0700322}
323
John Reck4f02bf42014-01-03 18:09:17 -0800324RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
325 AutoMutex _lock(mLock);
326 RenderTask* next = mQueue.peek();
327 if (!next) {
328 mNextWakeup = LLONG_MAX;
329 } else {
John Recka5dda642014-05-22 15:43:54 -0700330 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800331 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
332 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
333 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700334 } else {
335 next = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800336 }
John Reckcec24ae2013-11-05 13:27:50 -0800337 }
John Reck4f02bf42014-01-03 18:09:17 -0800338 if (nextWakeup) {
339 *nextWakeup = mNextWakeup;
340 }
341 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800342}
343
John Reckcec24ae2013-11-05 13:27:50 -0800344} /* namespace renderthread */
345} /* namespace uirenderer */
346} /* namespace android */