blob: 526a84861d98bd99313f50ba52558c6e4a629171 [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 {
John Reckcec24ae2013-11-05 13:27:50 -080031namespace uirenderer {
32namespace renderthread {
33
John Recke45b1fd2014-04-15 09:50:16 -070034// Number of events to read at a time from the DisplayEventReceiver pipe.
35// The value should be large enough that we can quickly drain the pipe
36// using just a few large reads.
37static const size_t EVENT_BUFFER_SIZE = 100;
38
39// Slight delay to give the UI time to push us a new frame before we replay
John Recka733f892014-12-19 11:37:21 -080040static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070041
Chris Craikd41c4d82015-01-05 15:51:13 -080042TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080043
44RenderTask* TaskQueue::next() {
45 RenderTask* ret = mHead;
46 if (ret) {
47 mHead = ret->mNext;
48 if (!mHead) {
Chris Craikd41c4d82015-01-05 15:51:13 -080049 mTail = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080050 }
Chris Craikd41c4d82015-01-05 15:51:13 -080051 ret->mNext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080052 }
53 return ret;
54}
55
56RenderTask* TaskQueue::peek() {
57 return mHead;
58}
59
60void TaskQueue::queue(RenderTask* task) {
61 // Since the RenderTask itself forms the linked list it is not allowed
62 // to have the same task queued twice
63 LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!");
64 if (mTail) {
65 // Fast path if we can just append
66 if (mTail->mRunAt <= task->mRunAt) {
67 mTail->mNext = task;
68 mTail = task;
69 } else {
70 // Need to find the proper insertion point
Chris Craikd41c4d82015-01-05 15:51:13 -080071 RenderTask* previous = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080072 RenderTask* next = mHead;
73 while (next && next->mRunAt <= task->mRunAt) {
74 previous = next;
75 next = next->mNext;
76 }
77 if (!previous) {
78 task->mNext = mHead;
79 mHead = task;
80 } else {
81 previous->mNext = task;
82 if (next) {
83 task->mNext = next;
84 } else {
85 mTail = task;
86 }
87 }
88 }
89 } else {
90 mTail = mHead = task;
91 }
92}
93
John Recka5dda642014-05-22 15:43:54 -070094void TaskQueue::queueAtFront(RenderTask* task) {
95 if (mTail) {
96 task->mNext = mHead;
97 mHead = task;
98 } else {
99 mTail = mHead = task;
100 }
101}
102
John Reck4f02bf42014-01-03 18:09:17 -0800103void TaskQueue::remove(RenderTask* task) {
104 // TaskQueue is strict here to enforce that users are keeping track of
105 // their RenderTasks due to how their memory is managed
106 LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task,
107 "Cannot remove a task that isn't in the queue!");
108
109 // If task is the head we can just call next() to pop it off
110 // Otherwise we need to scan through to find the task before it
111 if (peek() == task) {
112 next();
113 } else {
114 RenderTask* previous = mHead;
115 while (previous->mNext != task) {
116 previous = previous->mNext;
117 }
118 previous->mNext = task->mNext;
119 if (mTail == task) {
120 mTail = previous;
121 }
122 }
123}
124
John Recke45b1fd2014-04-15 09:50:16 -0700125class DispatchFrameCallbacks : public RenderTask {
126private:
127 RenderThread* mRenderThread;
128public:
129 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
130
Chris Craikd41c4d82015-01-05 15:51:13 -0800131 virtual void run() override {
John Recke45b1fd2014-04-15 09:50:16 -0700132 mRenderThread->dispatchFrameCallbacks();
133 }
134};
135
John Reck6b507802015-11-03 10:09:59 -0800136static bool gHasRenderThreadInstance = false;
137
138bool RenderThread::hasInstance() {
139 return gHasRenderThreadInstance;
140}
141
142RenderThread& RenderThread::getInstance() {
143 // This is a pointer because otherwise __cxa_finalize
144 // will try to delete it like a Good Citizen but that causes us to crash
145 // because we don't want to delete the RenderThread normally.
146 static RenderThread* sInstance = new RenderThread();
147 gHasRenderThreadInstance = true;
148 return *sInstance;
149}
150
151RenderThread::RenderThread() : Thread(true)
John Recke45b1fd2014-04-15 09:50:16 -0700152 , mNextWakeup(LLONG_MAX)
Chris Craikd41c4d82015-01-05 15:51:13 -0800153 , mDisplayEventReceiver(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700154 , mVsyncRequested(false)
155 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800156 , mFrameCallbackTask(nullptr)
157 , mRenderState(nullptr)
158 , mEglManager(nullptr) {
Chris Craik2507c342015-05-04 14:36:49 -0700159 Properties::load();
John Recke45b1fd2014-04-15 09:50:16 -0700160 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800161 mLooper = new Looper(false);
162 run("RenderThread");
163}
164
165RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700166 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800167}
168
John Recke45b1fd2014-04-15 09:50:16 -0700169void RenderThread::initializeDisplayEventReceiver() {
170 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
171 mDisplayEventReceiver = new DisplayEventReceiver();
172 status_t status = mDisplayEventReceiver->initCheck();
173 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
174 "failed with status: %d", status);
175
176 // Register the FD
177 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
178 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
179}
180
John Reck3b202512014-06-23 13:13:08 -0700181void RenderThread::initThreadLocals() {
John Reckb36016c2015-03-11 08:50:53 -0700182 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
183 ISurfaceComposer::eDisplayIdMain));
184 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo);
185 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
186 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
187 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700188 initializeDisplayEventReceiver();
189 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700190 mRenderState = new RenderState(*this);
John Reckb36016c2015-03-11 08:50:53 -0700191 mJankTracker = new JankTracker(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700192}
193
John Recke45b1fd2014-04-15 09:50:16 -0700194int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
195 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
196 ALOGE("Display event receiver pipe was closed or an error occurred. "
197 "events=0x%x", events);
198 return 0; // remove the callback
199 }
200
201 if (!(events & Looper::EVENT_INPUT)) {
202 ALOGW("Received spurious callback for unhandled poll event. "
203 "events=0x%x", events);
204 return 1; // keep the callback
205 }
206
207 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
208
209 return 1; // keep the callback
210}
211
212static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
213 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
214 nsecs_t latest = 0;
215 ssize_t n;
216 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
217 for (ssize_t i = 0; i < n; i++) {
218 const DisplayEventReceiver::Event& ev = buf[i];
219 switch (ev.header.type) {
220 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
221 latest = ev.header.timestamp;
222 break;
223 }
224 }
225 }
226 if (n < 0) {
227 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
228 }
229 return latest;
230}
231
John Recka733f892014-12-19 11:37:21 -0800232void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700233 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700234 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
235 if (vsyncEvent > 0) {
236 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800237 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700238 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700239 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800240 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
241 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700242 }
243 }
244}
245
246void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700247 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700248 mFrameCallbackTaskPending = false;
249
250 std::set<IFrameCallback*> callbacks;
251 mFrameCallbacks.swap(callbacks);
252
John Recka733f892014-12-19 11:37:21 -0800253 if (callbacks.size()) {
254 // Assume one of them will probably animate again so preemptively
255 // request the next vsync in case it occurs mid-frame
256 requestVsync();
257 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
258 (*it)->doFrame();
259 }
John Recke45b1fd2014-04-15 09:50:16 -0700260 }
261}
262
John Recka5dda642014-05-22 15:43:54 -0700263void RenderThread::requestVsync() {
264 if (!mVsyncRequested) {
265 mVsyncRequested = true;
266 status_t status = mDisplayEventReceiver->requestNextVsync();
267 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
268 "requestNextVsync failed with status: %d", status);
269 }
270}
271
John Reckcec24ae2013-11-05 13:27:50 -0800272bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700273 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700274 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700275
John Reck4f02bf42014-01-03 18:09:17 -0800276 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800277 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700278 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800279 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
280 "RenderThread Looper POLL_ERROR!");
281
282 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800283 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800284 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800285 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800286 // task may have deleted itself, do not reference it again
287 }
288 if (nextWakeup == LLONG_MAX) {
289 timeoutMillis = -1;
290 } else {
John Recka6260b82014-01-29 18:31:51 -0800291 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
292 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800293 if (timeoutMillis < 0) {
294 timeoutMillis = 0;
295 }
John Reckcec24ae2013-11-05 13:27:50 -0800296 }
John Recka5dda642014-05-22 15:43:54 -0700297
298 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800299 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700300 mFrameCallbacks.insert(
301 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
302 mPendingRegistrationFrameCallbacks.clear();
303 requestVsync();
304 }
John Recka22c9b22015-01-14 10:40:15 -0800305
306 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
307 // TODO: Clean this up. This is working around an issue where a combination
308 // of bad timing and slow drawing can result in dropping a stale vsync
309 // on the floor (correct!) but fails to schedule to listen for the
310 // next vsync (oops), so none of the callbacks are run.
311 requestVsync();
312 }
John Reckcec24ae2013-11-05 13:27:50 -0800313 }
314
315 return false;
316}
317
318void RenderThread::queue(RenderTask* task) {
319 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800320 mQueue.queue(task);
321 if (mNextWakeup && task->mRunAt < mNextWakeup) {
322 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800323 mLooper->wake();
324 }
325}
326
Chris Craik0a24b142015-10-19 17:10:19 -0700327void RenderThread::queueAndWait(RenderTask* task) {
John Reck6b507802015-11-03 10:09:59 -0800328 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
329 AutoMutex _lock(mSyncMutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700330 queue(&syncTask);
John Reck6b507802015-11-03 10:09:59 -0800331 mSyncCondition.wait(mSyncMutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700332}
333
John Recka5dda642014-05-22 15:43:54 -0700334void RenderThread::queueAtFront(RenderTask* task) {
335 AutoMutex _lock(mLock);
336 mQueue.queueAtFront(task);
337 mLooper->wake();
338}
339
John Recka733f892014-12-19 11:37:21 -0800340void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
341 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800342 queue(task);
343}
344
345void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800346 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800347 mQueue.remove(task);
348}
349
John Recke45b1fd2014-04-15 09:50:16 -0700350void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700351 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700352}
353
John Reck01a5ea32014-12-03 13:01:07 -0800354bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
355 size_t erased;
356 erased = mFrameCallbacks.erase(callback);
357 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
358 return erased;
John Recka5dda642014-05-22 15:43:54 -0700359}
360
361void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
362 if (mFrameCallbacks.erase(callback)) {
363 mPendingRegistrationFrameCallbacks.insert(callback);
364 }
John Recke45b1fd2014-04-15 09:50:16 -0700365}
366
John Reck4f02bf42014-01-03 18:09:17 -0800367RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
368 AutoMutex _lock(mLock);
369 RenderTask* next = mQueue.peek();
370 if (!next) {
371 mNextWakeup = LLONG_MAX;
372 } else {
John Recka5dda642014-05-22 15:43:54 -0700373 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800374 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
375 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
376 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700377 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800378 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800379 }
John Reckcec24ae2013-11-05 13:27:50 -0800380 }
John Reck4f02bf42014-01-03 18:09:17 -0800381 if (nextWakeup) {
382 *nextWakeup = mNextWakeup;
383 }
384 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800385}
386
John Reckcec24ae2013-11-05 13:27:50 -0800387} /* namespace renderthread */
388} /* namespace uirenderer */
389} /* namespace android */