blob: 3ac2976f97c606208dd0be0c4995d38d71637c2a [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>
John Reckb36016c2015-03-11 08:50:53 -070025#include <gui/ISurfaceComposer.h>
26#include <gui/SurfaceComposerClient.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080027#include <sys/resource.h>
28#include <utils/Log.h>
29
John Reckcec24ae2013-11-05 13:27:50 -080030namespace 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 Recka733f892014-12-19 11:37:21 -080043static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070044
Chris Craikd41c4d82015-01-05 15:51:13 -080045TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080046
47RenderTask* TaskQueue::next() {
48 RenderTask* ret = mHead;
49 if (ret) {
50 mHead = ret->mNext;
51 if (!mHead) {
Chris Craikd41c4d82015-01-05 15:51:13 -080052 mTail = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080053 }
Chris Craikd41c4d82015-01-05 15:51:13 -080054 ret->mNext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080055 }
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
Chris Craikd41c4d82015-01-05 15:51:13 -080074 RenderTask* previous = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080075 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
Chris Craikd41c4d82015-01-05 15:51:13 -0800134 virtual void run() override {
John Recke45b1fd2014-04-15 09:50:16 -0700135 mRenderThread->dispatchFrameCallbacks();
136 }
137};
138
John Reckcec24ae2013-11-05 13:27:50 -0800139RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>()
John Recke45b1fd2014-04-15 09:50:16 -0700140 , mNextWakeup(LLONG_MAX)
Chris Craikd41c4d82015-01-05 15:51:13 -0800141 , mDisplayEventReceiver(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700142 , mVsyncRequested(false)
143 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800144 , mFrameCallbackTask(nullptr)
145 , mRenderState(nullptr)
146 , mEglManager(nullptr) {
John Recke45b1fd2014-04-15 09:50:16 -0700147 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800148 mLooper = new Looper(false);
149 run("RenderThread");
150}
151
152RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700153 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800154}
155
John Recke45b1fd2014-04-15 09:50:16 -0700156void RenderThread::initializeDisplayEventReceiver() {
157 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
158 mDisplayEventReceiver = new DisplayEventReceiver();
159 status_t status = mDisplayEventReceiver->initCheck();
160 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
161 "failed with status: %d", status);
162
163 // Register the FD
164 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
165 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
166}
167
John Reck3b202512014-06-23 13:13:08 -0700168void RenderThread::initThreadLocals() {
John Reckb36016c2015-03-11 08:50:53 -0700169 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
170 ISurfaceComposer::eDisplayIdMain));
171 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo);
172 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
173 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
174 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700175 initializeDisplayEventReceiver();
176 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700177 mRenderState = new RenderState(*this);
John Reckb36016c2015-03-11 08:50:53 -0700178 mJankTracker = new JankTracker(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700179}
180
John Recke45b1fd2014-04-15 09:50:16 -0700181int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
182 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
183 ALOGE("Display event receiver pipe was closed or an error occurred. "
184 "events=0x%x", events);
185 return 0; // remove the callback
186 }
187
188 if (!(events & Looper::EVENT_INPUT)) {
189 ALOGW("Received spurious callback for unhandled poll event. "
190 "events=0x%x", events);
191 return 1; // keep the callback
192 }
193
194 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
195
196 return 1; // keep the callback
197}
198
199static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
200 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
201 nsecs_t latest = 0;
202 ssize_t n;
203 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
204 for (ssize_t i = 0; i < n; i++) {
205 const DisplayEventReceiver::Event& ev = buf[i];
206 switch (ev.header.type) {
207 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
208 latest = ev.header.timestamp;
209 break;
210 }
211 }
212 }
213 if (n < 0) {
214 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
215 }
216 return latest;
217}
218
John Recka733f892014-12-19 11:37:21 -0800219void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700220 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700221 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
222 if (vsyncEvent > 0) {
223 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800224 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700225 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700226 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800227 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
228 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700229 }
230 }
231}
232
233void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700234 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700235 mFrameCallbackTaskPending = false;
236
237 std::set<IFrameCallback*> callbacks;
238 mFrameCallbacks.swap(callbacks);
239
John Recka733f892014-12-19 11:37:21 -0800240 if (callbacks.size()) {
241 // Assume one of them will probably animate again so preemptively
242 // request the next vsync in case it occurs mid-frame
243 requestVsync();
244 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
245 (*it)->doFrame();
246 }
John Recke45b1fd2014-04-15 09:50:16 -0700247 }
248}
249
John Recka5dda642014-05-22 15:43:54 -0700250void RenderThread::requestVsync() {
251 if (!mVsyncRequested) {
252 mVsyncRequested = true;
253 status_t status = mDisplayEventReceiver->requestNextVsync();
254 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
255 "requestNextVsync failed with status: %d", status);
256 }
257}
258
John Reckcec24ae2013-11-05 13:27:50 -0800259bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700260 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700261 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700262
John Reck4f02bf42014-01-03 18:09:17 -0800263 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800264 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700265 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800266 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
267 "RenderThread Looper POLL_ERROR!");
268
269 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800270 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800271 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800272 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800273 // task may have deleted itself, do not reference it again
274 }
275 if (nextWakeup == LLONG_MAX) {
276 timeoutMillis = -1;
277 } else {
John Recka6260b82014-01-29 18:31:51 -0800278 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
279 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800280 if (timeoutMillis < 0) {
281 timeoutMillis = 0;
282 }
John Reckcec24ae2013-11-05 13:27:50 -0800283 }
John Recka5dda642014-05-22 15:43:54 -0700284
285 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800286 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700287 mFrameCallbacks.insert(
288 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
289 mPendingRegistrationFrameCallbacks.clear();
290 requestVsync();
291 }
John Recka22c9b22015-01-14 10:40:15 -0800292
293 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
294 // TODO: Clean this up. This is working around an issue where a combination
295 // of bad timing and slow drawing can result in dropping a stale vsync
296 // on the floor (correct!) but fails to schedule to listen for the
297 // next vsync (oops), so none of the callbacks are run.
298 requestVsync();
299 }
John Reckcec24ae2013-11-05 13:27:50 -0800300 }
301
302 return false;
303}
304
305void RenderThread::queue(RenderTask* task) {
306 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800307 mQueue.queue(task);
308 if (mNextWakeup && task->mRunAt < mNextWakeup) {
309 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800310 mLooper->wake();
311 }
312}
313
John Recka5dda642014-05-22 15:43:54 -0700314void RenderThread::queueAtFront(RenderTask* task) {
315 AutoMutex _lock(mLock);
316 mQueue.queueAtFront(task);
317 mLooper->wake();
318}
319
John Recka733f892014-12-19 11:37:21 -0800320void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
321 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800322 queue(task);
323}
324
325void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800326 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800327 mQueue.remove(task);
328}
329
John Recke45b1fd2014-04-15 09:50:16 -0700330void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700331 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700332}
333
John Reck01a5ea32014-12-03 13:01:07 -0800334bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
335 size_t erased;
336 erased = mFrameCallbacks.erase(callback);
337 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
338 return erased;
John Recka5dda642014-05-22 15:43:54 -0700339}
340
341void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
342 if (mFrameCallbacks.erase(callback)) {
343 mPendingRegistrationFrameCallbacks.insert(callback);
344 }
John Recke45b1fd2014-04-15 09:50:16 -0700345}
346
John Reck4f02bf42014-01-03 18:09:17 -0800347RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
348 AutoMutex _lock(mLock);
349 RenderTask* next = mQueue.peek();
350 if (!next) {
351 mNextWakeup = LLONG_MAX;
352 } else {
John Recka5dda642014-05-22 15:43:54 -0700353 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800354 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
355 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
356 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700357 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800358 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800359 }
John Reckcec24ae2013-11-05 13:27:50 -0800360 }
John Reck4f02bf42014-01-03 18:09:17 -0800361 if (nextWakeup) {
362 *nextWakeup = mNextWakeup;
363 }
364 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800365}
366
John Reckcec24ae2013-11-05 13:27:50 -0800367} /* namespace renderthread */
368} /* namespace uirenderer */
369} /* namespace android */