blob: 5e067dafed03927e3540cc96203475dae00f59a1 [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
John Reck4f02bf42014-01-03 18:09:17 -080019#include "CanvasContext.h"
John Reck56428472018-03-16 17:27:17 -070020#include "DeviceInfo.h"
John Reck3b202512014-06-23 13:13:08 -070021#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080022#include "RenderProxy.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050023#include "VulkanManager.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "hwui/Bitmap.h"
25#include "pipeline/skia/SkiaOpenGLPipeline.h"
26#include "pipeline/skia/SkiaOpenGLReadback.h"
John Reckec100972018-04-05 16:41:41 -070027#include "pipeline/skia/SkiaVulkanReadback.h"
John Reck1a4a9812018-04-18 16:13:31 -070028#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reck1bcacfd2017-11-03 10:12:19 -070029#include "renderstate/RenderState.h"
John Reck12efa552016-11-15 10:22:01 -080030#include "utils/FatVector.h"
John Reck56428472018-03-16 17:27:17 -070031#include "utils/TimeUtils.h"
John Reckcec24ae2013-11-05 13:27:50 -080032
Chris Craik65fe5ee2015-01-26 18:06:29 -080033#include <gui/DisplayEventReceiver.h>
34#include <sys/resource.h>
John Reckcba287b2015-11-10 12:52:44 -080035#include <utils/Condition.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080036#include <utils/Log.h>
John Reckcba287b2015-11-10 12:52:44 -080037#include <utils/Mutex.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080038
John Reckcec24ae2013-11-05 13:27:50 -080039namespace android {
John Reckcec24ae2013-11-05 13:27:50 -080040namespace uirenderer {
41namespace renderthread {
42
John Recke45b1fd2014-04-15 09:50:16 -070043// Number of events to read at a time from the DisplayEventReceiver pipe.
44// The value should be large enough that we can quickly drain the pipe
45// using just a few large reads.
46static const size_t EVENT_BUFFER_SIZE = 100;
47
48// Slight delay to give the UI time to push us a new frame before we replay
John Recka733f892014-12-19 11:37:21 -080049static const nsecs_t DISPATCH_FRAME_CALLBACKS_DELAY = milliseconds_to_nanoseconds(4);
John Recke45b1fd2014-04-15 09:50:16 -070050
John Reck6b507802015-11-03 10:09:59 -080051static bool gHasRenderThreadInstance = false;
52
John Reck259b25a2017-12-01 16:18:53 -080053static void (*gOnStartHook)() = nullptr;
54
John Reck56428472018-03-16 17:27:17 -070055class DisplayEventReceiverWrapper : public VsyncSource {
56public:
57 DisplayEventReceiverWrapper(std::unique_ptr<DisplayEventReceiver>&& receiver)
58 : mDisplayEventReceiver(std::move(receiver)) {}
59
60 virtual void requestNextVsync() override {
61 status_t status = mDisplayEventReceiver->requestNextVsync();
62 LOG_ALWAYS_FATAL_IF(status != NO_ERROR, "requestNextVsync failed with status: %d", status);
63 }
64
65 virtual nsecs_t latestVsyncEvent() override {
66 DisplayEventReceiver::Event buf[EVENT_BUFFER_SIZE];
67 nsecs_t latest = 0;
68 ssize_t n;
69 while ((n = mDisplayEventReceiver->getEvents(buf, EVENT_BUFFER_SIZE)) > 0) {
70 for (ssize_t i = 0; i < n; i++) {
71 const DisplayEventReceiver::Event& ev = buf[i];
72 switch (ev.header.type) {
73 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
74 latest = ev.header.timestamp;
75 break;
76 }
77 }
78 }
79 if (n < 0) {
80 ALOGW("Failed to get events from display event receiver, status=%d", status_t(n));
81 }
82 return latest;
83 }
84
85private:
86 std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
87};
88
89class DummyVsyncSource : public VsyncSource {
90public:
91 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
92
93 virtual void requestNextVsync() override {
John Reck1a4a9812018-04-18 16:13:31 -070094 mRenderThread->queue().postDelayed(16_ms, [this]() {
95 mRenderThread->drainDisplayEventQueue();
96 });
John Reck56428472018-03-16 17:27:17 -070097 }
98
John Reck1a4a9812018-04-18 16:13:31 -070099 virtual nsecs_t latestVsyncEvent() override {
100 return systemTime(CLOCK_MONOTONIC);
101 }
John Reck56428472018-03-16 17:27:17 -0700102
103private:
104 RenderThread* mRenderThread;
105};
106
John Reck6b507802015-11-03 10:09:59 -0800107bool RenderThread::hasInstance() {
108 return gHasRenderThreadInstance;
109}
110
John Reck259b25a2017-12-01 16:18:53 -0800111void RenderThread::setOnStartHook(void (*onStartHook)()) {
112 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
113 gOnStartHook = onStartHook;
114}
115
John Reck6b507802015-11-03 10:09:59 -0800116RenderThread& RenderThread::getInstance() {
117 // This is a pointer because otherwise __cxa_finalize
118 // will try to delete it like a Good Citizen but that causes us to crash
119 // because we don't want to delete the RenderThread normally.
120 static RenderThread* sInstance = new RenderThread();
121 gHasRenderThreadInstance = true;
122 return *sInstance;
123}
124
John Reck1bcacfd2017-11-03 10:12:19 -0700125RenderThread::RenderThread()
126 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700127 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700128 , mVsyncRequested(false)
129 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800130 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500131 , mEglManager(nullptr)
132 , mVkManager(nullptr) {
Chris Craik2507c342015-05-04 14:36:49 -0700133 Properties::load();
John Reckf8441e62017-10-23 13:10:41 -0700134 start("RenderThread");
John Reckcec24ae2013-11-05 13:27:50 -0800135}
136
137RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700138 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800139}
140
John Recke45b1fd2014-04-15 09:50:16 -0700141void RenderThread::initializeDisplayEventReceiver() {
John Reck56428472018-03-16 17:27:17 -0700142 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second DisplayEventReceiver?");
John Recke45b1fd2014-04-15 09:50:16 -0700143
John Reck56428472018-03-16 17:27:17 -0700144 if (!Properties::isolatedProcess) {
145 auto receiver = std::make_unique<DisplayEventReceiver>();
146 status_t status = receiver->initCheck();
147 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
John Reck1a4a9812018-04-18 16:13:31 -0700148 "Initialization of DisplayEventReceiver "
149 "failed with status: %d",
150 status);
John Reck56428472018-03-16 17:27:17 -0700151
152 // Register the FD
153 mLooper->addFd(receiver->getFd(), 0, Looper::EVENT_INPUT,
John Reck1a4a9812018-04-18 16:13:31 -0700154 RenderThread::displayEventReceiverCallback, this);
John Reck56428472018-03-16 17:27:17 -0700155 mVsyncSource = new DisplayEventReceiverWrapper(std::move(receiver));
156 } else {
157 mVsyncSource = new DummyVsyncSource(this);
158 }
John Recke45b1fd2014-04-15 09:50:16 -0700159}
160
John Reck3b202512014-06-23 13:13:08 -0700161void RenderThread::initThreadLocals() {
John Reck56428472018-03-16 17:27:17 -0700162 mDisplayInfo = DeviceInfo::queryDisplayInfo();
John Reckb36016c2015-03-11 08:50:53 -0700163 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
164 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700165 initializeDisplayEventReceiver();
166 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700167 mRenderState = new RenderState(*this);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500168 mVkManager = new VulkanManager(*this);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400169 mCacheManager = new CacheManager(mDisplayInfo);
170}
171
172void RenderThread::dumpGraphicsMemory(int fd) {
John Reck34781b22017-07-05 16:39:36 -0700173 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400174
175 String8 cachesOutput;
176 String8 pipeline;
177 auto renderType = Properties::getRenderPipelineType();
178 switch (renderType) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400179 case RenderPipelineType::SkiaGL: {
180 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
181 pipeline.appendFormat("Skia (OpenGL)");
182 break;
183 }
184 case RenderPipelineType::SkiaVulkan: {
185 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
186 pipeline.appendFormat("Skia (Vulkan)");
187 break;
188 }
189 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700190 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400191 break;
192 }
193
John Reck47f5c3a2017-11-13 11:32:39 -0800194 dprintf(fd, "\n%s\n", cachesOutput.string());
195 dprintf(fd, "\nPipeline=%s\n", pipeline.string());
John Reck3b202512014-06-23 13:13:08 -0700196}
197
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500198Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500199 if (!mReadback) {
200 auto renderType = Properties::getRenderPipelineType();
201 switch (renderType) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500202 case RenderPipelineType::SkiaGL:
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500203 mReadback = new skiapipeline::SkiaOpenGLReadback(*this);
204 break;
Stan Ilieva31973fe2018-01-12 11:50:29 -0500205 case RenderPipelineType::SkiaVulkan:
206 mReadback = new skiapipeline::SkiaVulkanReadback(*this);
207 break;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500208 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700209 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500210 break;
211 }
212 }
213
214 return *mReadback;
215}
216
Greg Daniel660d6ec2017-12-08 11:44:27 -0500217void RenderThread::setGrContext(sk_sp<GrContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400218 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500219 if (mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400220 mGrContext->releaseResourcesAndAbandonContext();
221 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500222 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400223}
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. "
John Reck1bcacfd2017-11-03 10:12:19 -0700228 "events=0x%x",
229 events);
230 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700231 }
232
233 if (!(events & Looper::EVENT_INPUT)) {
234 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700235 "events=0x%x",
236 events);
237 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700238 }
239
240 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
241
John Reck1bcacfd2017-11-03 10:12:19 -0700242 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700243}
244
John Recka733f892014-12-19 11:37:21 -0800245void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700246 ATRACE_CALL();
John Reck56428472018-03-16 17:27:17 -0700247 nsecs_t vsyncEvent = mVsyncSource->latestVsyncEvent();
John Recke45b1fd2014-04-15 09:50:16 -0700248 if (vsyncEvent > 0) {
249 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800250 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700251 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700252 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800253 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700254 queue().postAt(runAt, [this]() { dispatchFrameCallbacks(); });
John Recke45b1fd2014-04-15 09:50:16 -0700255 }
256 }
257}
258
259void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700260 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700261 mFrameCallbackTaskPending = false;
262
263 std::set<IFrameCallback*> callbacks;
264 mFrameCallbacks.swap(callbacks);
265
John Recka733f892014-12-19 11:37:21 -0800266 if (callbacks.size()) {
267 // Assume one of them will probably animate again so preemptively
268 // request the next vsync in case it occurs mid-frame
269 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700270 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
271 it++) {
John Recka733f892014-12-19 11:37:21 -0800272 (*it)->doFrame();
273 }
John Recke45b1fd2014-04-15 09:50:16 -0700274 }
275}
276
John Recka5dda642014-05-22 15:43:54 -0700277void RenderThread::requestVsync() {
278 if (!mVsyncRequested) {
279 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700280 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700281 }
282}
283
John Reckcec24ae2013-11-05 13:27:50 -0800284bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700285 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck259b25a2017-12-01 16:18:53 -0800286 if (gOnStartHook) {
287 gOnStartHook();
288 }
John Reck3b202512014-06-23 13:13:08 -0700289 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700290
John Reckf8441e62017-10-23 13:10:41 -0700291 while (true) {
292 waitForWork();
293 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700294
295 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800296 drainDisplayEventQueue();
John Reck1bcacfd2017-11-03 10:12:19 -0700297 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
298 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700299 mPendingRegistrationFrameCallbacks.clear();
300 requestVsync();
301 }
John Recka22c9b22015-01-14 10:40:15 -0800302
303 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
304 // TODO: Clean this up. This is working around an issue where a combination
305 // of bad timing and slow drawing can result in dropping a stale vsync
306 // on the floor (correct!) but fails to schedule to listen for the
307 // next vsync (oops), so none of the callbacks are run.
308 requestVsync();
309 }
John Reckcec24ae2013-11-05 13:27:50 -0800310 }
311
312 return false;
313}
314
John Recke45b1fd2014-04-15 09:50:16 -0700315void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700316 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700317}
318
John Reck01a5ea32014-12-03 13:01:07 -0800319bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
320 size_t erased;
321 erased = mFrameCallbacks.erase(callback);
322 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
323 return erased;
John Recka5dda642014-05-22 15:43:54 -0700324}
325
326void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
327 if (mFrameCallbacks.erase(callback)) {
328 mPendingRegistrationFrameCallbacks.insert(callback);
329 }
John Recke45b1fd2014-04-15 09:50:16 -0700330}
331
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400332sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
333 auto renderType = Properties::getRenderPipelineType();
334 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400335 case RenderPipelineType::SkiaGL:
336 return skiapipeline::SkiaOpenGLPipeline::allocateHardwareBitmap(*this, skBitmap);
337 case RenderPipelineType::SkiaVulkan:
338 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
339 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700340 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400341 break;
342 }
343 return nullptr;
344}
345
Stan Iliev6b894d72017-08-23 12:41:41 -0400346bool RenderThread::isCurrent() {
347 return gettid() == getInstance().getTid();
348}
349
John Reckcec24ae2013-11-05 13:27:50 -0800350} /* namespace renderthread */
351} /* namespace uirenderer */
352} /* namespace android */