blob: f4b44164b84ec67768378b8f4c3880f15f652fb6 [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>
John Reckcba287b2015-11-10 12:52:44 -080028#include <utils/Condition.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include <utils/Log.h>
John Reckcba287b2015-11-10 12:52:44 -080030#include <utils/Mutex.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080031
John Reckcec24ae2013-11-05 13:27:50 -080032namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080033namespace uirenderer {
34namespace renderthread {
35
John Recke45b1fd2014-04-15 09:50:16 -070036// Number of events to read at a time from the DisplayEventReceiver pipe.
37// The value should be large enough that we can quickly drain the pipe
38// using just a few large reads.
39static const size_t EVENT_BUFFER_SIZE = 100;
40
41// Slight delay to give the UI time to push us a new frame before we replay
John Recka733f892014-12-19 11:37:21 -080042static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070043
Chris Craikd41c4d82015-01-05 15:51:13 -080044TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080045
46RenderTask* TaskQueue::next() {
47 RenderTask* ret = mHead;
48 if (ret) {
49 mHead = ret->mNext;
50 if (!mHead) {
Chris Craikd41c4d82015-01-05 15:51:13 -080051 mTail = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080052 }
Chris Craikd41c4d82015-01-05 15:51:13 -080053 ret->mNext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080054 }
55 return ret;
56}
57
58RenderTask* TaskQueue::peek() {
59 return mHead;
60}
61
62void TaskQueue::queue(RenderTask* task) {
63 // Since the RenderTask itself forms the linked list it is not allowed
64 // to have the same task queued twice
65 LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!");
66 if (mTail) {
67 // Fast path if we can just append
68 if (mTail->mRunAt <= task->mRunAt) {
69 mTail->mNext = task;
70 mTail = task;
71 } else {
72 // Need to find the proper insertion point
Chris Craikd41c4d82015-01-05 15:51:13 -080073 RenderTask* previous = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080074 RenderTask* next = mHead;
75 while (next && next->mRunAt <= task->mRunAt) {
76 previous = next;
77 next = next->mNext;
78 }
79 if (!previous) {
80 task->mNext = mHead;
81 mHead = task;
82 } else {
83 previous->mNext = task;
84 if (next) {
85 task->mNext = next;
86 } else {
87 mTail = task;
88 }
89 }
90 }
91 } else {
92 mTail = mHead = task;
93 }
94}
95
John Recka5dda642014-05-22 15:43:54 -070096void TaskQueue::queueAtFront(RenderTask* task) {
97 if (mTail) {
98 task->mNext = mHead;
99 mHead = task;
100 } else {
101 mTail = mHead = task;
102 }
103}
104
John Reck4f02bf42014-01-03 18:09:17 -0800105void TaskQueue::remove(RenderTask* task) {
106 // TaskQueue is strict here to enforce that users are keeping track of
107 // their RenderTasks due to how their memory is managed
108 LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task,
109 "Cannot remove a task that isn't in the queue!");
110
111 // If task is the head we can just call next() to pop it off
112 // Otherwise we need to scan through to find the task before it
113 if (peek() == task) {
114 next();
115 } else {
116 RenderTask* previous = mHead;
117 while (previous->mNext != task) {
118 previous = previous->mNext;
119 }
120 previous->mNext = task->mNext;
121 if (mTail == task) {
122 mTail = previous;
123 }
124 }
125}
126
John Recke45b1fd2014-04-15 09:50:16 -0700127class DispatchFrameCallbacks : public RenderTask {
128private:
129 RenderThread* mRenderThread;
130public:
131 DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
132
Chris Craikd41c4d82015-01-05 15:51:13 -0800133 virtual void run() override {
John Recke45b1fd2014-04-15 09:50:16 -0700134 mRenderThread->dispatchFrameCallbacks();
135 }
136};
137
John Reck6b507802015-11-03 10:09:59 -0800138static bool gHasRenderThreadInstance = false;
139
140bool RenderThread::hasInstance() {
141 return gHasRenderThreadInstance;
142}
143
144RenderThread& RenderThread::getInstance() {
145 // This is a pointer because otherwise __cxa_finalize
146 // will try to delete it like a Good Citizen but that causes us to crash
147 // because we don't want to delete the RenderThread normally.
148 static RenderThread* sInstance = new RenderThread();
149 gHasRenderThreadInstance = true;
150 return *sInstance;
151}
152
153RenderThread::RenderThread() : Thread(true)
John Recke45b1fd2014-04-15 09:50:16 -0700154 , mNextWakeup(LLONG_MAX)
Chris Craikd41c4d82015-01-05 15:51:13 -0800155 , mDisplayEventReceiver(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700156 , mVsyncRequested(false)
157 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800158 , mFrameCallbackTask(nullptr)
159 , mRenderState(nullptr)
160 , mEglManager(nullptr) {
Chris Craik2507c342015-05-04 14:36:49 -0700161 Properties::load();
John Recke45b1fd2014-04-15 09:50:16 -0700162 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800163 mLooper = new Looper(false);
164 run("RenderThread");
165}
166
167RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700168 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800169}
170
John Recke45b1fd2014-04-15 09:50:16 -0700171void RenderThread::initializeDisplayEventReceiver() {
172 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
173 mDisplayEventReceiver = new DisplayEventReceiver();
174 status_t status = mDisplayEventReceiver->initCheck();
175 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
176 "failed with status: %d", status);
177
178 // Register the FD
179 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
180 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
181}
182
John Reck3b202512014-06-23 13:13:08 -0700183void RenderThread::initThreadLocals() {
John Reckb36016c2015-03-11 08:50:53 -0700184 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
185 ISurfaceComposer::eDisplayIdMain));
186 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo);
187 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
188 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
189 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700190 initializeDisplayEventReceiver();
191 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700192 mRenderState = new RenderState(*this);
John Reck2d5b8d72016-07-28 15:36:11 -0700193 mJankTracker = new JankTracker(mDisplayInfo);
John Reck3b202512014-06-23 13:13:08 -0700194}
195
John Recke45b1fd2014-04-15 09:50:16 -0700196int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
197 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
198 ALOGE("Display event receiver pipe was closed or an error occurred. "
199 "events=0x%x", events);
200 return 0; // remove the callback
201 }
202
203 if (!(events & Looper::EVENT_INPUT)) {
204 ALOGW("Received spurious callback for unhandled poll event. "
205 "events=0x%x", events);
206 return 1; // keep the callback
207 }
208
209 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
210
211 return 1; // keep the callback
212}
213
214static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
215 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
216 nsecs_t latest = 0;
217 ssize_t n;
218 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
219 for (ssize_t i = 0; i < n; i++) {
220 const DisplayEventReceiver::Event& ev = buf[i];
221 switch (ev.header.type) {
222 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
223 latest = ev.header.timestamp;
224 break;
225 }
226 }
227 }
228 if (n < 0) {
229 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
230 }
231 return latest;
232}
233
John Recka733f892014-12-19 11:37:21 -0800234void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700235 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700236 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
237 if (vsyncEvent > 0) {
238 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800239 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700240 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700241 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800242 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
243 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700244 }
245 }
246}
247
248void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700249 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700250 mFrameCallbackTaskPending = false;
251
252 std::set<IFrameCallback*> callbacks;
253 mFrameCallbacks.swap(callbacks);
254
John Recka733f892014-12-19 11:37:21 -0800255 if (callbacks.size()) {
256 // Assume one of them will probably animate again so preemptively
257 // request the next vsync in case it occurs mid-frame
258 requestVsync();
259 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
260 (*it)->doFrame();
261 }
John Recke45b1fd2014-04-15 09:50:16 -0700262 }
263}
264
John Recka5dda642014-05-22 15:43:54 -0700265void RenderThread::requestVsync() {
266 if (!mVsyncRequested) {
267 mVsyncRequested = true;
268 status_t status = mDisplayEventReceiver->requestNextVsync();
269 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
270 "requestNextVsync failed with status: %d", status);
271 }
272}
273
John Reckcec24ae2013-11-05 13:27:50 -0800274bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700275 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700276 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700277
John Reck4f02bf42014-01-03 18:09:17 -0800278 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800279 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700280 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800281 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
282 "RenderThread Looper POLL_ERROR!");
283
284 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800285 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800286 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800287 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800288 // task may have deleted itself, do not reference it again
289 }
290 if (nextWakeup == LLONG_MAX) {
291 timeoutMillis = -1;
292 } else {
John Recka6260b82014-01-29 18:31:51 -0800293 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
294 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800295 if (timeoutMillis < 0) {
296 timeoutMillis = 0;
297 }
John Reckcec24ae2013-11-05 13:27:50 -0800298 }
John Recka5dda642014-05-22 15:43:54 -0700299
300 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800301 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700302 mFrameCallbacks.insert(
303 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
304 mPendingRegistrationFrameCallbacks.clear();
305 requestVsync();
306 }
John Recka22c9b22015-01-14 10:40:15 -0800307
308 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
309 // TODO: Clean this up. This is working around an issue where a combination
310 // of bad timing and slow drawing can result in dropping a stale vsync
311 // on the floor (correct!) but fails to schedule to listen for the
312 // next vsync (oops), so none of the callbacks are run.
313 requestVsync();
314 }
John Reckcec24ae2013-11-05 13:27:50 -0800315 }
316
317 return false;
318}
319
320void RenderThread::queue(RenderTask* task) {
321 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800322 mQueue.queue(task);
323 if (mNextWakeup && task->mRunAt < mNextWakeup) {
324 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800325 mLooper->wake();
326 }
327}
328
Chris Craik0a24b142015-10-19 17:10:19 -0700329void RenderThread::queueAndWait(RenderTask* task) {
John Reckcba287b2015-11-10 12:52:44 -0800330 // These need to be local to the thread to avoid the Condition
331 // signaling the wrong thread. The easiest way to achieve that is to just
332 // make this on the stack, although that has a slight cost to it
333 Mutex mutex;
334 Condition condition;
335 SignalingRenderTask syncTask(task, &mutex, &condition);
336
337 AutoMutex _lock(mutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700338 queue(&syncTask);
John Reckcba287b2015-11-10 12:52:44 -0800339 condition.wait(mutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700340}
341
John Recka5dda642014-05-22 15:43:54 -0700342void RenderThread::queueAtFront(RenderTask* task) {
343 AutoMutex _lock(mLock);
344 mQueue.queueAtFront(task);
345 mLooper->wake();
346}
347
John Recka733f892014-12-19 11:37:21 -0800348void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
349 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800350 queue(task);
351}
352
353void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800354 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800355 mQueue.remove(task);
356}
357
John Recke45b1fd2014-04-15 09:50:16 -0700358void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700359 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700360}
361
John Reck01a5ea32014-12-03 13:01:07 -0800362bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
363 size_t erased;
364 erased = mFrameCallbacks.erase(callback);
365 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
366 return erased;
John Recka5dda642014-05-22 15:43:54 -0700367}
368
369void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
370 if (mFrameCallbacks.erase(callback)) {
371 mPendingRegistrationFrameCallbacks.insert(callback);
372 }
John Recke45b1fd2014-04-15 09:50:16 -0700373}
374
John Reck4f02bf42014-01-03 18:09:17 -0800375RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
376 AutoMutex _lock(mLock);
377 RenderTask* next = mQueue.peek();
378 if (!next) {
379 mNextWakeup = LLONG_MAX;
380 } else {
John Recka5dda642014-05-22 15:43:54 -0700381 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800382 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
383 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
384 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700385 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800386 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800387 }
John Reckcec24ae2013-11-05 13:27:50 -0800388 }
John Reck4f02bf42014-01-03 18:09:17 -0800389 if (nextWakeup) {
390 *nextWakeup = mNextWakeup;
391 }
392 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800393}
394
John Reckcec24ae2013-11-05 13:27:50 -0800395} /* namespace renderthread */
396} /* namespace uirenderer */
397} /* namespace android */