blob: e32fd63e112548ef8a2c7d8d0164d46409730410 [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"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050020#include "../pipeline/skia/SkiaOpenGLReadback.h"
John Reck4f02bf42014-01-03 18:09:17 -080021#include "CanvasContext.h"
John Reck3b202512014-06-23 13:13:08 -070022#include "EglManager.h"
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050023#include "OpenGLReadback.h"
John Reck4f02bf42014-01-03 18:09:17 -080024#include "RenderProxy.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050025#include "VulkanManager.h"
John Reck12efa552016-11-15 10:22:01 -080026#include "utils/FatVector.h"
John Reckcec24ae2013-11-05 13:27:50 -080027
Chris Craik65fe5ee2015-01-26 18:06:29 -080028#include <gui/DisplayEventReceiver.h>
John Reckb36016c2015-03-11 08:50:53 -070029#include <gui/ISurfaceComposer.h>
30#include <gui/SurfaceComposerClient.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080031#include <sys/resource.h>
John Reckcba287b2015-11-10 12:52:44 -080032#include <utils/Condition.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080033#include <utils/Log.h>
John Reckcba287b2015-11-10 12:52:44 -080034#include <utils/Mutex.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080035
John Reckcec24ae2013-11-05 13:27:50 -080036namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080037namespace uirenderer {
38namespace renderthread {
39
John Recke45b1fd2014-04-15 09:50:16 -070040// Number of events to read at a time from the DisplayEventReceiver pipe.
41// The value should be large enough that we can quickly drain the pipe
42// using just a few large reads.
43static const size_t EVENT_BUFFER_SIZE = 100;
44
45// Slight delay to give the UI time to push us a new frame before we replay
John Recka733f892014-12-19 11:37:21 -080046static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070047
Chris Craikd41c4d82015-01-05 15:51:13 -080048TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080049
50RenderTask* TaskQueue::next() {
51 RenderTask* ret = mHead;
52 if (ret) {
53 mHead = ret->mNext;
54 if (!mHead) {
Chris Craikd41c4d82015-01-05 15:51:13 -080055 mTail = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080056 }
Chris Craikd41c4d82015-01-05 15:51:13 -080057 ret->mNext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080058 }
59 return ret;
60}
61
62RenderTask* TaskQueue::peek() {
63 return mHead;
64}
65
66void TaskQueue::queue(RenderTask* task) {
67 // Since the RenderTask itself forms the linked list it is not allowed
68 // to have the same task queued twice
69 LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!");
70 if (mTail) {
71 // Fast path if we can just append
72 if (mTail->mRunAt <= task->mRunAt) {
73 mTail->mNext = task;
74 mTail = task;
75 } else {
76 // Need to find the proper insertion point
Chris Craikd41c4d82015-01-05 15:51:13 -080077 RenderTask* previous = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080078 RenderTask* next = mHead;
79 while (next && next->mRunAt <= task->mRunAt) {
80 previous = next;
81 next = next->mNext;
82 }
83 if (!previous) {
84 task->mNext = mHead;
85 mHead = task;
86 } else {
87 previous->mNext = task;
88 if (next) {
89 task->mNext = next;
90 } else {
91 mTail = task;
92 }
93 }
94 }
95 } else {
96 mTail = mHead = task;
97 }
98}
99
John Recka5dda642014-05-22 15:43:54 -0700100void TaskQueue::queueAtFront(RenderTask* task) {
101 if (mTail) {
102 task->mNext = mHead;
103 mHead = task;
104 } else {
105 mTail = mHead = task;
106 }
107}
108
John Reck4f02bf42014-01-03 18:09:17 -0800109void TaskQueue::remove(RenderTask* task) {
110 // TaskQueue is strict here to enforce that users are keeping track of
111 // their RenderTasks due to how their memory is managed
112 LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task,
113 "Cannot remove a task that isn't in the queue!");
114
115 // If task is the head we can just call next() to pop it off
116 // Otherwise we need to scan through to find the task before it
117 if (peek() == task) {
118 next();
119 } else {
120 RenderTask* previous = mHead;
121 while (previous->mNext != task) {
122 previous = previous->mNext;
123 }
124 previous->mNext = task->mNext;
125 if (mTail == task) {
126 mTail = previous;
127 }
128 }
129}
130
John Recke45b1fd2014-04-15 09:50:16 -0700131class DispatchFrameCallbacks : public RenderTask {
132private:
133 RenderThread* mRenderThread;
134public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700135 explicit DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
John Recke45b1fd2014-04-15 09:50:16 -0700136
Chris Craikd41c4d82015-01-05 15:51:13 -0800137 virtual void run() override {
John Recke45b1fd2014-04-15 09:50:16 -0700138 mRenderThread->dispatchFrameCallbacks();
139 }
140};
141
John Reck6b507802015-11-03 10:09:59 -0800142static bool gHasRenderThreadInstance = false;
143
144bool RenderThread::hasInstance() {
145 return gHasRenderThreadInstance;
146}
147
148RenderThread& RenderThread::getInstance() {
149 // This is a pointer because otherwise __cxa_finalize
150 // will try to delete it like a Good Citizen but that causes us to crash
151 // because we don't want to delete the RenderThread normally.
152 static RenderThread* sInstance = new RenderThread();
153 gHasRenderThreadInstance = true;
154 return *sInstance;
155}
156
157RenderThread::RenderThread() : Thread(true)
John Recke45b1fd2014-04-15 09:50:16 -0700158 , mNextWakeup(LLONG_MAX)
Chris Craikd41c4d82015-01-05 15:51:13 -0800159 , mDisplayEventReceiver(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700160 , mVsyncRequested(false)
161 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800162 , mFrameCallbackTask(nullptr)
163 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500164 , mEglManager(nullptr)
165 , mVkManager(nullptr) {
Chris Craik2507c342015-05-04 14:36:49 -0700166 Properties::load();
John Recke45b1fd2014-04-15 09:50:16 -0700167 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800168 mLooper = new Looper(false);
169 run("RenderThread");
170}
171
172RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700173 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800174}
175
John Recke45b1fd2014-04-15 09:50:16 -0700176void RenderThread::initializeDisplayEventReceiver() {
177 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
178 mDisplayEventReceiver = new DisplayEventReceiver();
179 status_t status = mDisplayEventReceiver->initCheck();
180 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
181 "failed with status: %d", status);
182
183 // Register the FD
184 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
185 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
186}
187
John Reck3b202512014-06-23 13:13:08 -0700188void RenderThread::initThreadLocals() {
John Reckb36016c2015-03-11 08:50:53 -0700189 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
190 ISurfaceComposer::eDisplayIdMain));
191 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo);
192 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
193 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
194 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700195 initializeDisplayEventReceiver();
196 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700197 mRenderState = new RenderState(*this);
John Reck2d5b8d72016-07-28 15:36:11 -0700198 mJankTracker = new JankTracker(mDisplayInfo);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500199 mVkManager = new VulkanManager(*this);
John Reck3b202512014-06-23 13:13:08 -0700200}
201
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500202Readback& RenderThread::readback() {
203
204 if (!mReadback) {
205 auto renderType = Properties::getRenderPipelineType();
206 switch (renderType) {
207 case RenderPipelineType::OpenGL:
208 mReadback = new OpenGLReadbackImpl(*this);
209 break;
210 case RenderPipelineType::SkiaGL:
211 case RenderPipelineType::SkiaVulkan:
212 // It works to use the OpenGL pipeline for Vulkan but this is not
213 // ideal as it causes us to create an OpenGL context in addition
214 // to the Vulkan one.
215 mReadback = new skiapipeline::SkiaOpenGLReadback(*this);
216 break;
217 default:
218 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
219 break;
220 }
221 }
222
223 return *mReadback;
224}
225
John Recke45b1fd2014-04-15 09:50:16 -0700226int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
227 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
228 ALOGE("Display event receiver pipe was closed or an error occurred. "
229 "events=0x%x", events);
230 return 0; // remove the callback
231 }
232
233 if (!(events & Looper::EVENT_INPUT)) {
234 ALOGW("Received spurious callback for unhandled poll event. "
235 "events=0x%x", events);
236 return 1; // keep the callback
237 }
238
239 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
240
241 return 1; // keep the callback
242}
243
244static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
245 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
246 nsecs_t latest = 0;
247 ssize_t n;
248 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
249 for (ssize_t i = 0; i < n; i++) {
250 const DisplayEventReceiver::Event& ev = buf[i];
251 switch (ev.header.type) {
252 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
253 latest = ev.header.timestamp;
254 break;
255 }
256 }
257 }
258 if (n < 0) {
259 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
260 }
261 return latest;
262}
263
John Recka733f892014-12-19 11:37:21 -0800264void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700265 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700266 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
267 if (vsyncEvent > 0) {
268 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800269 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700270 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700271 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800272 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
273 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700274 }
275 }
276}
277
278void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700279 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700280 mFrameCallbackTaskPending = false;
281
282 std::set<IFrameCallback*> callbacks;
283 mFrameCallbacks.swap(callbacks);
284
John Recka733f892014-12-19 11:37:21 -0800285 if (callbacks.size()) {
286 // Assume one of them will probably animate again so preemptively
287 // request the next vsync in case it occurs mid-frame
288 requestVsync();
289 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
290 (*it)->doFrame();
291 }
John Recke45b1fd2014-04-15 09:50:16 -0700292 }
293}
294
John Recka5dda642014-05-22 15:43:54 -0700295void RenderThread::requestVsync() {
296 if (!mVsyncRequested) {
297 mVsyncRequested = true;
298 status_t status = mDisplayEventReceiver->requestNextVsync();
299 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
300 "requestNextVsync failed with status: %d", status);
301 }
302}
303
John Reckcec24ae2013-11-05 13:27:50 -0800304bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700305 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700306 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700307
John Reck4f02bf42014-01-03 18:09:17 -0800308 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800309 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700310 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800311 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
312 "RenderThread Looper POLL_ERROR!");
313
314 nsecs_t nextWakeup;
John Reck12efa552016-11-15 10:22:01 -0800315 {
316 FatVector<RenderTask*, 10> workQueue;
317 // Process our queue, if we have anything. By first acquiring
318 // all the pending events then processing them we avoid vsync
319 // starvation if more tasks are queued while we are processing tasks.
320 while (RenderTask* task = nextTask(&nextWakeup)) {
321 workQueue.push_back(task);
322 }
323 for (auto task : workQueue) {
324 task->run();
325 // task may have deleted itself, do not reference it again
326 }
John Reck4f02bf42014-01-03 18:09:17 -0800327 }
328 if (nextWakeup == LLONG_MAX) {
329 timeoutMillis = -1;
330 } else {
John Recka6260b82014-01-29 18:31:51 -0800331 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
332 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800333 if (timeoutMillis < 0) {
334 timeoutMillis = 0;
335 }
John Reckcec24ae2013-11-05 13:27:50 -0800336 }
John Recka5dda642014-05-22 15:43:54 -0700337
338 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800339 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700340 mFrameCallbacks.insert(
341 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
342 mPendingRegistrationFrameCallbacks.clear();
343 requestVsync();
344 }
John Recka22c9b22015-01-14 10:40:15 -0800345
346 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
347 // TODO: Clean this up. This is working around an issue where a combination
348 // of bad timing and slow drawing can result in dropping a stale vsync
349 // on the floor (correct!) but fails to schedule to listen for the
350 // next vsync (oops), so none of the callbacks are run.
351 requestVsync();
352 }
John Reckcec24ae2013-11-05 13:27:50 -0800353 }
354
355 return false;
356}
357
358void RenderThread::queue(RenderTask* task) {
359 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800360 mQueue.queue(task);
361 if (mNextWakeup && task->mRunAt < mNextWakeup) {
362 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800363 mLooper->wake();
364 }
365}
366
Chris Craik0a24b142015-10-19 17:10:19 -0700367void RenderThread::queueAndWait(RenderTask* task) {
John Reckcba287b2015-11-10 12:52:44 -0800368 // These need to be local to the thread to avoid the Condition
369 // signaling the wrong thread. The easiest way to achieve that is to just
370 // make this on the stack, although that has a slight cost to it
371 Mutex mutex;
372 Condition condition;
373 SignalingRenderTask syncTask(task, &mutex, &condition);
374
375 AutoMutex _lock(mutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700376 queue(&syncTask);
Tom Cherry298a1462017-02-28 14:07:09 -0800377 while (!syncTask.hasRun()) {
378 condition.wait(mutex);
379 }
Chris Craik0a24b142015-10-19 17:10:19 -0700380}
381
John Recka5dda642014-05-22 15:43:54 -0700382void RenderThread::queueAtFront(RenderTask* task) {
383 AutoMutex _lock(mLock);
384 mQueue.queueAtFront(task);
385 mLooper->wake();
386}
387
John Recka733f892014-12-19 11:37:21 -0800388void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
389 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800390 queue(task);
391}
392
393void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800394 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800395 mQueue.remove(task);
396}
397
John Recke45b1fd2014-04-15 09:50:16 -0700398void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700399 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700400}
401
John Reck01a5ea32014-12-03 13:01:07 -0800402bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
403 size_t erased;
404 erased = mFrameCallbacks.erase(callback);
405 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
406 return erased;
John Recka5dda642014-05-22 15:43:54 -0700407}
408
409void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
410 if (mFrameCallbacks.erase(callback)) {
411 mPendingRegistrationFrameCallbacks.insert(callback);
412 }
John Recke45b1fd2014-04-15 09:50:16 -0700413}
414
John Reck4f02bf42014-01-03 18:09:17 -0800415RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
416 AutoMutex _lock(mLock);
417 RenderTask* next = mQueue.peek();
418 if (!next) {
419 mNextWakeup = LLONG_MAX;
420 } else {
John Recka5dda642014-05-22 15:43:54 -0700421 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800422 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
423 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
424 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700425 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800426 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800427 }
John Reckcec24ae2013-11-05 13:27:50 -0800428 }
John Reck4f02bf42014-01-03 18:09:17 -0800429 if (nextWakeup) {
430 *nextWakeup = mNextWakeup;
431 }
432 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800433}
434
John Reckcec24ae2013-11-05 13:27:50 -0800435} /* namespace renderthread */
436} /* namespace uirenderer */
437} /* namespace android */