blob: 223958a3c319dbfa6ff903b8c04080bf708a30c2 [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 Reckcec24ae2013-11-05 13:27:50 -080026
Chris Craik65fe5ee2015-01-26 18:06:29 -080027#include <gui/DisplayEventReceiver.h>
John Reckb36016c2015-03-11 08:50:53 -070028#include <gui/ISurfaceComposer.h>
29#include <gui/SurfaceComposerClient.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080030#include <sys/resource.h>
John Reckcba287b2015-11-10 12:52:44 -080031#include <utils/Condition.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080032#include <utils/Log.h>
John Reckcba287b2015-11-10 12:52:44 -080033#include <utils/Mutex.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080034
John Reckcec24ae2013-11-05 13:27:50 -080035namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080036namespace uirenderer {
37namespace renderthread {
38
John Recke45b1fd2014-04-15 09:50:16 -070039// Number of events to read at a time from the DisplayEventReceiver pipe.
40// The value should be large enough that we can quickly drain the pipe
41// using just a few large reads.
42static const size_t EVENT_BUFFER_SIZE = 100;
43
44// Slight delay to give the UI time to push us a new frame before we replay
John Recka733f892014-12-19 11:37:21 -080045static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070046
Chris Craikd41c4d82015-01-05 15:51:13 -080047TaskQueue::TaskQueue() : mHead(nullptr), mTail(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080048
49RenderTask* TaskQueue::next() {
50 RenderTask* ret = mHead;
51 if (ret) {
52 mHead = ret->mNext;
53 if (!mHead) {
Chris Craikd41c4d82015-01-05 15:51:13 -080054 mTail = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080055 }
Chris Craikd41c4d82015-01-05 15:51:13 -080056 ret->mNext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080057 }
58 return ret;
59}
60
61RenderTask* TaskQueue::peek() {
62 return mHead;
63}
64
65void TaskQueue::queue(RenderTask* task) {
66 // Since the RenderTask itself forms the linked list it is not allowed
67 // to have the same task queued twice
68 LOG_ALWAYS_FATAL_IF(task->mNext || mTail == task, "Task is already in the queue!");
69 if (mTail) {
70 // Fast path if we can just append
71 if (mTail->mRunAt <= task->mRunAt) {
72 mTail->mNext = task;
73 mTail = task;
74 } else {
75 // Need to find the proper insertion point
Chris Craikd41c4d82015-01-05 15:51:13 -080076 RenderTask* previous = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080077 RenderTask* next = mHead;
78 while (next && next->mRunAt <= task->mRunAt) {
79 previous = next;
80 next = next->mNext;
81 }
82 if (!previous) {
83 task->mNext = mHead;
84 mHead = task;
85 } else {
86 previous->mNext = task;
87 if (next) {
88 task->mNext = next;
89 } else {
90 mTail = task;
91 }
92 }
93 }
94 } else {
95 mTail = mHead = task;
96 }
97}
98
John Recka5dda642014-05-22 15:43:54 -070099void TaskQueue::queueAtFront(RenderTask* task) {
100 if (mTail) {
101 task->mNext = mHead;
102 mHead = task;
103 } else {
104 mTail = mHead = task;
105 }
106}
107
John Reck4f02bf42014-01-03 18:09:17 -0800108void TaskQueue::remove(RenderTask* task) {
109 // TaskQueue is strict here to enforce that users are keeping track of
110 // their RenderTasks due to how their memory is managed
111 LOG_ALWAYS_FATAL_IF(!task->mNext && mTail != task,
112 "Cannot remove a task that isn't in the queue!");
113
114 // If task is the head we can just call next() to pop it off
115 // Otherwise we need to scan through to find the task before it
116 if (peek() == task) {
117 next();
118 } else {
119 RenderTask* previous = mHead;
120 while (previous->mNext != task) {
121 previous = previous->mNext;
122 }
123 previous->mNext = task->mNext;
124 if (mTail == task) {
125 mTail = previous;
126 }
127 }
128}
129
John Recke45b1fd2014-04-15 09:50:16 -0700130class DispatchFrameCallbacks : public RenderTask {
131private:
132 RenderThread* mRenderThread;
133public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700134 explicit DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {}
John Recke45b1fd2014-04-15 09:50:16 -0700135
Chris Craikd41c4d82015-01-05 15:51:13 -0800136 virtual void run() override {
John Recke45b1fd2014-04-15 09:50:16 -0700137 mRenderThread->dispatchFrameCallbacks();
138 }
139};
140
John Reck6b507802015-11-03 10:09:59 -0800141static bool gHasRenderThreadInstance = false;
142
143bool RenderThread::hasInstance() {
144 return gHasRenderThreadInstance;
145}
146
147RenderThread& RenderThread::getInstance() {
148 // This is a pointer because otherwise __cxa_finalize
149 // will try to delete it like a Good Citizen but that causes us to crash
150 // because we don't want to delete the RenderThread normally.
151 static RenderThread* sInstance = new RenderThread();
152 gHasRenderThreadInstance = true;
153 return *sInstance;
154}
155
156RenderThread::RenderThread() : Thread(true)
John Recke45b1fd2014-04-15 09:50:16 -0700157 , mNextWakeup(LLONG_MAX)
Chris Craikd41c4d82015-01-05 15:51:13 -0800158 , mDisplayEventReceiver(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700159 , mVsyncRequested(false)
160 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800161 , mFrameCallbackTask(nullptr)
162 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500163 , mEglManager(nullptr)
164 , mVkManager(nullptr) {
Chris Craik2507c342015-05-04 14:36:49 -0700165 Properties::load();
John Recke45b1fd2014-04-15 09:50:16 -0700166 mFrameCallbackTask = new DispatchFrameCallbacks(this);
John Reckcec24ae2013-11-05 13:27:50 -0800167 mLooper = new Looper(false);
168 run("RenderThread");
169}
170
171RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700172 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800173}
174
John Recke45b1fd2014-04-15 09:50:16 -0700175void RenderThread::initializeDisplayEventReceiver() {
176 LOG_ALWAYS_FATAL_IF(mDisplayEventReceiver, "Initializing a second DisplayEventReceiver?");
177 mDisplayEventReceiver = new DisplayEventReceiver();
178 status_t status = mDisplayEventReceiver->initCheck();
179 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver "
180 "failed with status: %d", status);
181
182 // Register the FD
183 mLooper->addFd(mDisplayEventReceiver->getFd(), 0,
184 Looper::EVENT_INPUT, RenderThread::displayEventReceiverCallback, this);
185}
186
John Reck3b202512014-06-23 13:13:08 -0700187void RenderThread::initThreadLocals() {
John Reckb36016c2015-03-11 08:50:53 -0700188 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
189 ISurfaceComposer::eDisplayIdMain));
190 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &mDisplayInfo);
191 LOG_ALWAYS_FATAL_IF(status, "Failed to get display info\n");
192 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
193 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700194 initializeDisplayEventReceiver();
195 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700196 mRenderState = new RenderState(*this);
John Reck2d5b8d72016-07-28 15:36:11 -0700197 mJankTracker = new JankTracker(mDisplayInfo);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500198 mVkManager = new VulkanManager(*this);
John Reck3b202512014-06-23 13:13:08 -0700199}
200
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500201Readback& RenderThread::readback() {
202
203 if (!mReadback) {
204 auto renderType = Properties::getRenderPipelineType();
205 switch (renderType) {
206 case RenderPipelineType::OpenGL:
207 mReadback = new OpenGLReadbackImpl(*this);
208 break;
209 case RenderPipelineType::SkiaGL:
210 case RenderPipelineType::SkiaVulkan:
211 // It works to use the OpenGL pipeline for Vulkan but this is not
212 // ideal as it causes us to create an OpenGL context in addition
213 // to the Vulkan one.
214 mReadback = new skiapipeline::SkiaOpenGLReadback(*this);
215 break;
216 default:
217 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
218 break;
219 }
220 }
221
222 return *mReadback;
223}
224
John Recke45b1fd2014-04-15 09:50:16 -0700225int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
226 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
227 ALOGE("Display event receiver pipe was closed or an error occurred. "
228 "events=0x%x", events);
229 return 0; // remove the callback
230 }
231
232 if (!(events & Looper::EVENT_INPUT)) {
233 ALOGW("Received spurious callback for unhandled poll event. "
234 "events=0x%x", events);
235 return 1; // keep the callback
236 }
237
238 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
239
240 return 1; // keep the callback
241}
242
243static nsecs_t latestVsyncEvent(DisplayEventReceiver* receiver) {
244 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
245 nsecs_t latest = 0;
246 ssize_t n;
247 while ((n = receiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
248 for (ssize_t i = 0; i < n; i++) {
249 const DisplayEventReceiver::Event& ev = buf[i];
250 switch (ev.header.type) {
251 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
252 latest = ev.header.timestamp;
253 break;
254 }
255 }
256 }
257 if (n < 0) {
258 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
259 }
260 return latest;
261}
262
John Recka733f892014-12-19 11:37:21 -0800263void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700264 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700265 nsecs_t vsyncEvent = latestVsyncEvent(mDisplayEventReceiver);
266 if (vsyncEvent > 0) {
267 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800268 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700269 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700270 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800271 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
272 queueAt(mFrameCallbackTask, runAt);
John Recke45b1fd2014-04-15 09:50:16 -0700273 }
274 }
275}
276
277void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700278 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700279 mFrameCallbackTaskPending = false;
280
281 std::set<IFrameCallback*> callbacks;
282 mFrameCallbacks.swap(callbacks);
283
John Recka733f892014-12-19 11:37:21 -0800284 if (callbacks.size()) {
285 // Assume one of them will probably animate again so preemptively
286 // request the next vsync in case it occurs mid-frame
287 requestVsync();
288 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end(); it++) {
289 (*it)->doFrame();
290 }
John Recke45b1fd2014-04-15 09:50:16 -0700291 }
292}
293
John Recka5dda642014-05-22 15:43:54 -0700294void RenderThread::requestVsync() {
295 if (!mVsyncRequested) {
296 mVsyncRequested = true;
297 status_t status = mDisplayEventReceiver->requestNextVsync();
298 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
299 "requestNextVsync failed with status: %d", status);
300 }
301}
302
John Reckcec24ae2013-11-05 13:27:50 -0800303bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700304 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck3b202512014-06-23 13:13:08 -0700305 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700306
John Reck4f02bf42014-01-03 18:09:17 -0800307 int timeoutMillis = -1;
John Reckcec24ae2013-11-05 13:27:50 -0800308 for (;;) {
John Recke45b1fd2014-04-15 09:50:16 -0700309 int result = mLooper->pollOnce(timeoutMillis);
John Reck4f02bf42014-01-03 18:09:17 -0800310 LOG_ALWAYS_FATAL_IF(result == Looper::POLL_ERROR,
311 "RenderThread Looper POLL_ERROR!");
312
313 nsecs_t nextWakeup;
John Reckcec24ae2013-11-05 13:27:50 -0800314 // Process our queue, if we have anything
John Reck4f02bf42014-01-03 18:09:17 -0800315 while (RenderTask* task = nextTask(&nextWakeup)) {
John Reckcec24ae2013-11-05 13:27:50 -0800316 task->run();
John Reck4f02bf42014-01-03 18:09:17 -0800317 // task may have deleted itself, do not reference it again
318 }
319 if (nextWakeup == LLONG_MAX) {
320 timeoutMillis = -1;
321 } else {
John Recka6260b82014-01-29 18:31:51 -0800322 nsecs_t timeoutNanos = nextWakeup - systemTime(SYSTEM_TIME_MONOTONIC);
323 timeoutMillis = nanoseconds_to_milliseconds(timeoutNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800324 if (timeoutMillis < 0) {
325 timeoutMillis = 0;
326 }
John Reckcec24ae2013-11-05 13:27:50 -0800327 }
John Recka5dda642014-05-22 15:43:54 -0700328
329 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800330 drainDisplayEventQueue();
John Recka5dda642014-05-22 15:43:54 -0700331 mFrameCallbacks.insert(
332 mPendingRegistrationFrameCallbacks.begin(), mPendingRegistrationFrameCallbacks.end());
333 mPendingRegistrationFrameCallbacks.clear();
334 requestVsync();
335 }
John Recka22c9b22015-01-14 10:40:15 -0800336
337 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
338 // TODO: Clean this up. This is working around an issue where a combination
339 // of bad timing and slow drawing can result in dropping a stale vsync
340 // on the floor (correct!) but fails to schedule to listen for the
341 // next vsync (oops), so none of the callbacks are run.
342 requestVsync();
343 }
John Reckcec24ae2013-11-05 13:27:50 -0800344 }
345
346 return false;
347}
348
349void RenderThread::queue(RenderTask* task) {
350 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800351 mQueue.queue(task);
352 if (mNextWakeup && task->mRunAt < mNextWakeup) {
353 mNextWakeup = 0;
John Reckcec24ae2013-11-05 13:27:50 -0800354 mLooper->wake();
355 }
356}
357
Chris Craik0a24b142015-10-19 17:10:19 -0700358void RenderThread::queueAndWait(RenderTask* task) {
John Reckcba287b2015-11-10 12:52:44 -0800359 // These need to be local to the thread to avoid the Condition
360 // signaling the wrong thread. The easiest way to achieve that is to just
361 // make this on the stack, although that has a slight cost to it
362 Mutex mutex;
363 Condition condition;
364 SignalingRenderTask syncTask(task, &mutex, &condition);
365
366 AutoMutex _lock(mutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700367 queue(&syncTask);
John Reckcba287b2015-11-10 12:52:44 -0800368 condition.wait(mutex);
Chris Craik0a24b142015-10-19 17:10:19 -0700369}
370
John Recka5dda642014-05-22 15:43:54 -0700371void RenderThread::queueAtFront(RenderTask* task) {
372 AutoMutex _lock(mLock);
373 mQueue.queueAtFront(task);
374 mLooper->wake();
375}
376
John Recka733f892014-12-19 11:37:21 -0800377void RenderThread::queueAt(RenderTask* task, nsecs_t runAtNs) {
378 task->mRunAt = runAtNs;
John Reck4f02bf42014-01-03 18:09:17 -0800379 queue(task);
380}
381
382void RenderThread::remove(RenderTask* task) {
John Reckcec24ae2013-11-05 13:27:50 -0800383 AutoMutex _lock(mLock);
John Reck4f02bf42014-01-03 18:09:17 -0800384 mQueue.remove(task);
385}
386
John Recke45b1fd2014-04-15 09:50:16 -0700387void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700388 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700389}
390
John Reck01a5ea32014-12-03 13:01:07 -0800391bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
392 size_t erased;
393 erased = mFrameCallbacks.erase(callback);
394 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
395 return erased;
John Recka5dda642014-05-22 15:43:54 -0700396}
397
398void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
399 if (mFrameCallbacks.erase(callback)) {
400 mPendingRegistrationFrameCallbacks.insert(callback);
401 }
John Recke45b1fd2014-04-15 09:50:16 -0700402}
403
John Reck4f02bf42014-01-03 18:09:17 -0800404RenderTask* RenderThread::nextTask(nsecs_t* nextWakeup) {
405 AutoMutex _lock(mLock);
406 RenderTask* next = mQueue.peek();
407 if (!next) {
408 mNextWakeup = LLONG_MAX;
409 } else {
John Recka5dda642014-05-22 15:43:54 -0700410 mNextWakeup = next->mRunAt;
John Reck4f02bf42014-01-03 18:09:17 -0800411 // Most tasks won't be delayed, so avoid unnecessary systemTime() calls
412 if (next->mRunAt <= 0 || next->mRunAt <= systemTime(SYSTEM_TIME_MONOTONIC)) {
413 next = mQueue.next();
John Recka5dda642014-05-22 15:43:54 -0700414 } else {
Chris Craikd41c4d82015-01-05 15:51:13 -0800415 next = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800416 }
John Reckcec24ae2013-11-05 13:27:50 -0800417 }
John Reck4f02bf42014-01-03 18:09:17 -0800418 if (nextWakeup) {
419 *nextWakeup = mNextWakeup;
420 }
421 return next;
John Reckcec24ae2013-11-05 13:27:50 -0800422}
423
John Reckcec24ae2013-11-05 13:27:50 -0800424} /* namespace renderthread */
425} /* namespace uirenderer */
426} /* namespace android */