blob: 84f43ec1a206d1e808013cc317f4c01d6bdc7b06 [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"
27#include "pipeline/skia/SkiaVulkanPipeline.h"
John Reckec100972018-04-05 16:41:41 -070028#include "pipeline/skia/SkiaVulkanReadback.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 Reckec100972018-04-05 16:41:41 -070094 mRenderThread->queue().postDelayed(16_ms,
95 [this]() { mRenderThread->drainDisplayEventQueue(); });
John Reck56428472018-03-16 17:27:17 -070096 }
97
John Reckec100972018-04-05 16:41:41 -070098 virtual nsecs_t latestVsyncEvent() override { return systemTime(CLOCK_MONOTONIC); }
John Reck56428472018-03-16 17:27:17 -070099
100private:
101 RenderThread* mRenderThread;
102};
103
John Reck6b507802015-11-03 10:09:59 -0800104bool RenderThread::hasInstance() {
105 return gHasRenderThreadInstance;
106}
107
John Reck259b25a2017-12-01 16:18:53 -0800108void RenderThread::setOnStartHook(void (*onStartHook)()) {
109 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
110 gOnStartHook = onStartHook;
111}
112
John Reck6b507802015-11-03 10:09:59 -0800113RenderThread& RenderThread::getInstance() {
114 // This is a pointer because otherwise __cxa_finalize
115 // will try to delete it like a Good Citizen but that causes us to crash
116 // because we don't want to delete the RenderThread normally.
117 static RenderThread* sInstance = new RenderThread();
118 gHasRenderThreadInstance = true;
119 return *sInstance;
120}
121
John Reck1bcacfd2017-11-03 10:12:19 -0700122RenderThread::RenderThread()
123 : ThreadBase()
John Reck56428472018-03-16 17:27:17 -0700124 , mVsyncSource(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -0700125 , mVsyncRequested(false)
126 , mFrameCallbackTaskPending(false)
Chris Craikd41c4d82015-01-05 15:51:13 -0800127 , mRenderState(nullptr)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500128 , mEglManager(nullptr)
129 , mVkManager(nullptr) {
Chris Craik2507c342015-05-04 14:36:49 -0700130 Properties::load();
John Reckf8441e62017-10-23 13:10:41 -0700131 start("RenderThread");
John Reckcec24ae2013-11-05 13:27:50 -0800132}
133
134RenderThread::~RenderThread() {
John Reck3b202512014-06-23 13:13:08 -0700135 LOG_ALWAYS_FATAL("Can't destroy the render thread");
John Reckcec24ae2013-11-05 13:27:50 -0800136}
137
John Recke45b1fd2014-04-15 09:50:16 -0700138void RenderThread::initializeDisplayEventReceiver() {
John Reck56428472018-03-16 17:27:17 -0700139 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second DisplayEventReceiver?");
John Recke45b1fd2014-04-15 09:50:16 -0700140
John Reck56428472018-03-16 17:27:17 -0700141 if (!Properties::isolatedProcess) {
142 auto receiver = std::make_unique<DisplayEventReceiver>();
143 status_t status = receiver->initCheck();
144 LOG_ALWAYS_FATAL_IF(status != NO_ERROR,
John Reckec100972018-04-05 16:41:41 -0700145 "Initialization of DisplayEventReceiver "
146 "failed with status: %d",
147 status);
John Reck56428472018-03-16 17:27:17 -0700148
149 // Register the FD
150 mLooper->addFd(receiver->getFd(), 0, Looper::EVENT_INPUT,
John Reckec100972018-04-05 16:41:41 -0700151 RenderThread::displayEventReceiverCallback, this);
John Reck56428472018-03-16 17:27:17 -0700152 mVsyncSource = new DisplayEventReceiverWrapper(std::move(receiver));
153 } else {
154 mVsyncSource = new DummyVsyncSource(this);
155 }
John Recke45b1fd2014-04-15 09:50:16 -0700156}
157
John Reck3b202512014-06-23 13:13:08 -0700158void RenderThread::initThreadLocals() {
John Reck56428472018-03-16 17:27:17 -0700159 mDisplayInfo = DeviceInfo::queryDisplayInfo();
John Reckb36016c2015-03-11 08:50:53 -0700160 nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1000000000 / mDisplayInfo.fps);
161 mTimeLord.setFrameInterval(frameIntervalNanos);
John Reck3b202512014-06-23 13:13:08 -0700162 initializeDisplayEventReceiver();
163 mEglManager = new EglManager(*this);
John Reck0e89e2b2014-10-31 14:49:06 -0700164 mRenderState = new RenderState(*this);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500165 mVkManager = new VulkanManager(*this);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400166 mCacheManager = new CacheManager(mDisplayInfo);
167}
168
169void RenderThread::dumpGraphicsMemory(int fd) {
John Reck34781b22017-07-05 16:39:36 -0700170 globalProfileData()->dump(fd);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400171
172 String8 cachesOutput;
173 String8 pipeline;
174 auto renderType = Properties::getRenderPipelineType();
175 switch (renderType) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400176 case RenderPipelineType::SkiaGL: {
177 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
178 pipeline.appendFormat("Skia (OpenGL)");
179 break;
180 }
181 case RenderPipelineType::SkiaVulkan: {
182 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
183 pipeline.appendFormat("Skia (Vulkan)");
184 break;
185 }
186 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700187 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400188 break;
189 }
190
John Reck47f5c3a2017-11-13 11:32:39 -0800191 dprintf(fd, "\n%s\n", cachesOutput.string());
192 dprintf(fd, "\nPipeline=%s\n", pipeline.string());
John Reck3b202512014-06-23 13:13:08 -0700193}
194
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500195Readback& RenderThread::readback() {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500196 if (!mReadback) {
197 auto renderType = Properties::getRenderPipelineType();
198 switch (renderType) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500199 case RenderPipelineType::SkiaGL:
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500200 mReadback = new skiapipeline::SkiaOpenGLReadback(*this);
201 break;
Stan Ilieva31973fe2018-01-12 11:50:29 -0500202 case RenderPipelineType::SkiaVulkan:
203 mReadback = new skiapipeline::SkiaVulkanReadback(*this);
204 break;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500205 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700206 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500207 break;
208 }
209 }
210
211 return *mReadback;
212}
213
Greg Daniel660d6ec2017-12-08 11:44:27 -0500214void RenderThread::setGrContext(sk_sp<GrContext> context) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400215 mCacheManager->reset(context);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500216 if (mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400217 mGrContext->releaseResourcesAndAbandonContext();
218 }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500219 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400220}
221
John Recke45b1fd2014-04-15 09:50:16 -0700222int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
223 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
224 ALOGE("Display event receiver pipe was closed or an error occurred. "
John Reck1bcacfd2017-11-03 10:12:19 -0700225 "events=0x%x",
226 events);
227 return 0; // remove the callback
John Recke45b1fd2014-04-15 09:50:16 -0700228 }
229
230 if (!(events & Looper::EVENT_INPUT)) {
231 ALOGW("Received spurious callback for unhandled poll event. "
John Reck1bcacfd2017-11-03 10:12:19 -0700232 "events=0x%x",
233 events);
234 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700235 }
236
237 reinterpret_cast<RenderThread*>(data)->drainDisplayEventQueue();
238
John Reck1bcacfd2017-11-03 10:12:19 -0700239 return 1; // keep the callback
John Recke45b1fd2014-04-15 09:50:16 -0700240}
241
John Recka733f892014-12-19 11:37:21 -0800242void RenderThread::drainDisplayEventQueue() {
John Recka5dda642014-05-22 15:43:54 -0700243 ATRACE_CALL();
John Reck56428472018-03-16 17:27:17 -0700244 nsecs_t vsyncEvent = mVsyncSource->latestVsyncEvent();
John Recke45b1fd2014-04-15 09:50:16 -0700245 if (vsyncEvent > 0) {
246 mVsyncRequested = false;
John Recka733f892014-12-19 11:37:21 -0800247 if (mTimeLord.vsyncReceived(vsyncEvent) && !mFrameCallbackTaskPending) {
John Recka5dda642014-05-22 15:43:54 -0700248 ATRACE_NAME("queue mFrameCallbackTask");
John Recke45b1fd2014-04-15 09:50:16 -0700249 mFrameCallbackTaskPending = true;
John Recka733f892014-12-19 11:37:21 -0800250 nsecs_t runAt = (vsyncEvent + DISPATCH_FRAME_CALLBACKS_DELAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700251 queue().postAt(runAt, [this]() { dispatchFrameCallbacks(); });
John Recke45b1fd2014-04-15 09:50:16 -0700252 }
253 }
254}
255
256void RenderThread::dispatchFrameCallbacks() {
John Recka5dda642014-05-22 15:43:54 -0700257 ATRACE_CALL();
John Recke45b1fd2014-04-15 09:50:16 -0700258 mFrameCallbackTaskPending = false;
259
260 std::set<IFrameCallback*> callbacks;
261 mFrameCallbacks.swap(callbacks);
262
John Recka733f892014-12-19 11:37:21 -0800263 if (callbacks.size()) {
264 // Assume one of them will probably animate again so preemptively
265 // request the next vsync in case it occurs mid-frame
266 requestVsync();
John Reck1bcacfd2017-11-03 10:12:19 -0700267 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
268 it++) {
John Recka733f892014-12-19 11:37:21 -0800269 (*it)->doFrame();
270 }
John Recke45b1fd2014-04-15 09:50:16 -0700271 }
272}
273
John Recka5dda642014-05-22 15:43:54 -0700274void RenderThread::requestVsync() {
275 if (!mVsyncRequested) {
276 mVsyncRequested = true;
John Reck56428472018-03-16 17:27:17 -0700277 mVsyncSource->requestNextVsync();
John Recka5dda642014-05-22 15:43:54 -0700278 }
279}
280
John Reckcec24ae2013-11-05 13:27:50 -0800281bool RenderThread::threadLoop() {
John Reck21be43e2014-08-14 10:25:16 -0700282 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
John Reck259b25a2017-12-01 16:18:53 -0800283 if (gOnStartHook) {
284 gOnStartHook();
285 }
John Reck3b202512014-06-23 13:13:08 -0700286 initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700287
John Reckf8441e62017-10-23 13:10:41 -0700288 while (true) {
289 waitForWork();
290 processQueue();
John Recka5dda642014-05-22 15:43:54 -0700291
292 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
John Recka733f892014-12-19 11:37:21 -0800293 drainDisplayEventQueue();
John Reck1bcacfd2017-11-03 10:12:19 -0700294 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
295 mPendingRegistrationFrameCallbacks.end());
John Recka5dda642014-05-22 15:43:54 -0700296 mPendingRegistrationFrameCallbacks.clear();
297 requestVsync();
298 }
John Recka22c9b22015-01-14 10:40:15 -0800299
300 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
301 // TODO: Clean this up. This is working around an issue where a combination
302 // of bad timing and slow drawing can result in dropping a stale vsync
303 // on the floor (correct!) but fails to schedule to listen for the
304 // next vsync (oops), so none of the callbacks are run.
305 requestVsync();
306 }
John Reckcec24ae2013-11-05 13:27:50 -0800307 }
308
309 return false;
310}
311
John Recke45b1fd2014-04-15 09:50:16 -0700312void RenderThread::postFrameCallback(IFrameCallback* callback) {
John Recka5dda642014-05-22 15:43:54 -0700313 mPendingRegistrationFrameCallbacks.insert(callback);
John Recke45b1fd2014-04-15 09:50:16 -0700314}
315
John Reck01a5ea32014-12-03 13:01:07 -0800316bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
317 size_t erased;
318 erased = mFrameCallbacks.erase(callback);
319 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
320 return erased;
John Recka5dda642014-05-22 15:43:54 -0700321}
322
323void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
324 if (mFrameCallbacks.erase(callback)) {
325 mPendingRegistrationFrameCallbacks.insert(callback);
326 }
John Recke45b1fd2014-04-15 09:50:16 -0700327}
328
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400329sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
330 auto renderType = Properties::getRenderPipelineType();
331 switch (renderType) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400332 case RenderPipelineType::SkiaGL:
333 return skiapipeline::SkiaOpenGLPipeline::allocateHardwareBitmap(*this, skBitmap);
334 case RenderPipelineType::SkiaVulkan:
335 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
336 default:
John Reck1bcacfd2017-11-03 10:12:19 -0700337 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400338 break;
339 }
340 return nullptr;
341}
342
Stan Iliev6b894d72017-08-23 12:41:41 -0400343bool RenderThread::isCurrent() {
344 return gettid() == getInstance().getTid();
345}
346
John Reckcec24ae2013-11-05 13:27:50 -0800347} /* namespace renderthread */
348} /* namespace uirenderer */
349} /* namespace android */