1 /*
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
17 #include "RenderThread.h"
18
19 #include <GrContextOptions.h>
20 #include <android-base/properties.h>
21 #include <dlfcn.h>
22 #include <gl/GrGLInterface.h>
23 #include <gui/TraceUtils.h>
24 #include <sys/resource.h>
25 #include <ui/FatVector.h>
26 #include <utils/Condition.h>
27 #include <utils/Log.h>
28 #include <utils/Mutex.h>
29
30 #include <thread>
31
32 #include "../HardwareBitmapUploader.h"
33 #include "CacheManager.h"
34 #include "CanvasContext.h"
35 #include "DeviceInfo.h"
36 #include "EglManager.h"
37 #include "Properties.h"
38 #include "Readback.h"
39 #include "RenderProxy.h"
40 #include "VulkanManager.h"
41 #include "hwui/Bitmap.h"
42 #include "pipeline/skia/SkiaOpenGLPipeline.h"
43 #include "pipeline/skia/SkiaVulkanPipeline.h"
44 #include "renderstate/RenderState.h"
45 #include "utils/TimeUtils.h"
46
47 namespace android {
48 namespace uirenderer {
49 namespace renderthread {
50
51 static bool gHasRenderThreadInstance = false;
52
53 static JVMAttachHook gOnStartHook = nullptr;
54
ASurfaceControlFunctions()55 ASurfaceControlFunctions::ASurfaceControlFunctions() {
56 void* handle_ = dlopen("libandroid.so", RTLD_NOW | RTLD_NODELETE);
57 createFunc = (ASC_create)dlsym(handle_, "ASurfaceControl_create");
58 LOG_ALWAYS_FATAL_IF(createFunc == nullptr,
59 "Failed to find required symbol ASurfaceControl_create!");
60
61 acquireFunc = (ASC_acquire) dlsym(handle_, "ASurfaceControl_acquire");
62 LOG_ALWAYS_FATAL_IF(acquireFunc == nullptr,
63 "Failed to find required symbol ASurfaceControl_acquire!");
64
65 releaseFunc = (ASC_release) dlsym(handle_, "ASurfaceControl_release");
66 LOG_ALWAYS_FATAL_IF(releaseFunc == nullptr,
67 "Failed to find required symbol ASurfaceControl_release!");
68
69 registerListenerFunc = (ASC_registerSurfaceStatsListener) dlsym(handle_,
70 "ASurfaceControl_registerSurfaceStatsListener");
71 LOG_ALWAYS_FATAL_IF(registerListenerFunc == nullptr,
72 "Failed to find required symbol ASurfaceControl_registerSurfaceStatsListener!");
73
74 unregisterListenerFunc = (ASC_unregisterSurfaceStatsListener) dlsym(handle_,
75 "ASurfaceControl_unregisterSurfaceStatsListener");
76 LOG_ALWAYS_FATAL_IF(unregisterListenerFunc == nullptr,
77 "Failed to find required symbol ASurfaceControl_unregisterSurfaceStatsListener!");
78
79 getAcquireTimeFunc = (ASCStats_getAcquireTime) dlsym(handle_,
80 "ASurfaceControlStats_getAcquireTime");
81 LOG_ALWAYS_FATAL_IF(getAcquireTimeFunc == nullptr,
82 "Failed to find required symbol ASurfaceControlStats_getAcquireTime!");
83
84 getFrameNumberFunc = (ASCStats_getFrameNumber) dlsym(handle_,
85 "ASurfaceControlStats_getFrameNumber");
86 LOG_ALWAYS_FATAL_IF(getFrameNumberFunc == nullptr,
87 "Failed to find required symbol ASurfaceControlStats_getFrameNumber!");
88
89 transactionCreateFunc = (AST_create)dlsym(handle_, "ASurfaceTransaction_create");
90 LOG_ALWAYS_FATAL_IF(transactionCreateFunc == nullptr,
91 "Failed to find required symbol ASurfaceTransaction_create!");
92
93 transactionDeleteFunc = (AST_delete)dlsym(handle_, "ASurfaceTransaction_delete");
94 LOG_ALWAYS_FATAL_IF(transactionDeleteFunc == nullptr,
95 "Failed to find required symbol ASurfaceTransaction_delete!");
96
97 transactionApplyFunc = (AST_apply)dlsym(handle_, "ASurfaceTransaction_apply");
98 LOG_ALWAYS_FATAL_IF(transactionApplyFunc == nullptr,
99 "Failed to find required symbol ASurfaceTransaction_apply!");
100
101 transactionReparentFunc = (AST_reparent)dlsym(handle_, "ASurfaceTransaction_reparent");
102 LOG_ALWAYS_FATAL_IF(transactionReparentFunc == nullptr,
103 "Failed to find required symbol transactionReparentFunc!");
104
105 transactionSetVisibilityFunc =
106 (AST_setVisibility)dlsym(handle_, "ASurfaceTransaction_setVisibility");
107 LOG_ALWAYS_FATAL_IF(transactionSetVisibilityFunc == nullptr,
108 "Failed to find required symbol ASurfaceTransaction_setVisibility!");
109
110 transactionSetZOrderFunc = (AST_setZOrder)dlsym(handle_, "ASurfaceTransaction_setZOrder");
111 LOG_ALWAYS_FATAL_IF(transactionSetZOrderFunc == nullptr,
112 "Failed to find required symbol ASurfaceTransaction_setZOrder!");
113 }
114
extendedFrameCallback(const AChoreographerFrameCallbackData * cbData,void * data)115 void RenderThread::extendedFrameCallback(const AChoreographerFrameCallbackData* cbData,
116 void* data) {
117 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
118 size_t preferredFrameTimelineIndex =
119 AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(cbData);
120 AVsyncId vsyncId = AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
121 cbData, preferredFrameTimelineIndex);
122 int64_t frameDeadline = AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
123 cbData, preferredFrameTimelineIndex);
124 int64_t frameTimeNanos = AChoreographerFrameCallbackData_getFrameTimeNanos(cbData);
125 // TODO(b/193273294): Remove when shared memory in use w/ expected present time always current.
126 int64_t frameInterval = AChoreographer_getFrameInterval(rt->mChoreographer);
127 rt->frameCallback(vsyncId, frameDeadline, frameTimeNanos, frameInterval);
128 }
129
frameCallback(int64_t vsyncId,int64_t frameDeadline,int64_t frameTimeNanos,int64_t frameInterval)130 void RenderThread::frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
131 int64_t frameInterval) {
132 mVsyncRequested = false;
133 if (timeLord().vsyncReceived(frameTimeNanos, frameTimeNanos, vsyncId, frameDeadline,
134 frameInterval) &&
135 !mFrameCallbackTaskPending) {
136 ATRACE_NAME("queue mFrameCallbackTask");
137 mFrameCallbackTaskPending = true;
138 nsecs_t runAt = (frameTimeNanos + mDispatchFrameDelay);
139 queue().postAt(runAt, [=]() { dispatchFrameCallbacks(); });
140 }
141 }
142
refreshRateCallback(int64_t vsyncPeriod,void * data)143 void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
144 ATRACE_NAME("refreshRateCallback");
145 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
146 DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
147 rt->setupFrameInterval();
148 }
149
150 class ChoreographerSource : public VsyncSource {
151 public:
ChoreographerSource(RenderThread * renderThread)152 ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
153
requestNextVsync()154 virtual void requestNextVsync() override {
155 AChoreographer_postVsyncCallback(mRenderThread->mChoreographer,
156 RenderThread::extendedFrameCallback, mRenderThread);
157 }
158
drainPendingEvents()159 virtual void drainPendingEvents() override {
160 AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
161 }
162
163 private:
164 RenderThread* mRenderThread;
165 };
166
167 class DummyVsyncSource : public VsyncSource {
168 public:
DummyVsyncSource(RenderThread * renderThread)169 DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
170
requestNextVsync()171 virtual void requestNextVsync() override {
172 mRenderThread->queue().postDelayed(16_ms, [this]() {
173 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
174 std::numeric_limits<int64_t>::max(),
175 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
176 });
177 }
178
drainPendingEvents()179 virtual void drainPendingEvents() override {
180 mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
181 std::numeric_limits<int64_t>::max(),
182 systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
183 }
184
185 private:
186 RenderThread* mRenderThread;
187 };
188
hasInstance()189 bool RenderThread::hasInstance() {
190 return gHasRenderThreadInstance;
191 }
192
setOnStartHook(JVMAttachHook onStartHook)193 void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
194 LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
195 gOnStartHook = onStartHook;
196 }
197
getOnStartHook()198 JVMAttachHook RenderThread::getOnStartHook() {
199 return gOnStartHook;
200 }
201
getInstance()202 RenderThread& RenderThread::getInstance() {
203 [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
204 sp<RenderThread> thread = sp<RenderThread>::make();
205 thread->start("RenderThread");
206 return thread;
207 }();
208 gHasRenderThreadInstance = true;
209 return *sInstance;
210 }
211
RenderThread()212 RenderThread::RenderThread()
213 : ThreadBase()
214 , mVsyncSource(nullptr)
215 , mVsyncRequested(false)
216 , mFrameCallbackTaskPending(false)
217 , mRenderState(nullptr)
218 , mEglManager(nullptr)
219 , mFunctorManager(WebViewFunctorManager::instance())
220 , mGlobalProfileData(mJankDataMutex) {
221 Properties::load();
222 }
223
~RenderThread()224 RenderThread::~RenderThread() {
225 // Note that if this fatal assertion is removed then member variables must
226 // be properly destroyed.
227 LOG_ALWAYS_FATAL("Can't destroy the render thread");
228 }
229
initializeChoreographer()230 void RenderThread::initializeChoreographer() {
231 LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
232
233 if (!Properties::isolatedProcess) {
234 mChoreographer = AChoreographer_create();
235 LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
236 AChoreographer_registerRefreshRateCallback(mChoreographer,
237 RenderThread::refreshRateCallback, this);
238
239 // Register the FD
240 mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
241 RenderThread::choreographerCallback, this);
242 mVsyncSource = new ChoreographerSource(this);
243 } else {
244 mVsyncSource = new DummyVsyncSource(this);
245 }
246 }
247
initThreadLocals()248 void RenderThread::initThreadLocals() {
249 setupFrameInterval();
250 initializeChoreographer();
251 mEglManager = new EglManager();
252 mRenderState = new RenderState(*this);
253 mVkManager = VulkanManager::getInstance();
254 mCacheManager = new CacheManager();
255 }
256
setupFrameInterval()257 void RenderThread::setupFrameInterval() {
258 nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
259 mTimeLord.setFrameInterval(frameIntervalNanos);
260 mDispatchFrameDelay = static_cast<nsecs_t>(frameIntervalNanos * .25f);
261 }
262
requireGlContext()263 void RenderThread::requireGlContext() {
264 if (mEglManager->hasEglContext()) {
265 return;
266 }
267 mEglManager->initialize();
268
269 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
270 LOG_ALWAYS_FATAL_IF(!glInterface.get());
271
272 GrContextOptions options;
273 initGrContextOptions(options);
274 auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
275 auto size = glesVersion ? strlen(glesVersion) : -1;
276 cacheManager().configureContext(&options, glesVersion, size);
277 sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
278 LOG_ALWAYS_FATAL_IF(!grContext.get());
279 setGrContext(grContext);
280 }
281
requireVkContext()282 void RenderThread::requireVkContext() {
283 // the getter creates the context in the event it had been destroyed by destroyRenderingContext
284 // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
285 // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
286 // in the rendering thread.
287 if (vulkanManager().hasVkContext() && mGrContext) {
288 return;
289 }
290 mVkManager->initialize();
291 GrContextOptions options;
292 initGrContextOptions(options);
293 auto vkDriverVersion = mVkManager->getDriverVersion();
294 cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
295 sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
296 LOG_ALWAYS_FATAL_IF(!grContext.get());
297 setGrContext(grContext);
298 }
299
initGrContextOptions(GrContextOptions & options)300 void RenderThread::initGrContextOptions(GrContextOptions& options) {
301 options.fPreferExternalImagesOverES3 = true;
302 options.fDisableDistanceFieldPaths = true;
303 if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
304 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
305 } else {
306 options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
307 }
308 }
309
destroyRenderingContext()310 void RenderThread::destroyRenderingContext() {
311 mFunctorManager.onContextDestroyed();
312 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
313 if (mEglManager->hasEglContext()) {
314 setGrContext(nullptr);
315 mEglManager->destroy();
316 }
317 } else {
318 setGrContext(nullptr);
319 mVkManager.clear();
320 }
321 }
322
vulkanManager()323 VulkanManager& RenderThread::vulkanManager() {
324 if (!mVkManager.get()) {
325 mVkManager = VulkanManager::getInstance();
326 }
327 return *mVkManager.get();
328 }
329
pipelineToString()330 static const char* pipelineToString() {
331 switch (auto renderType = Properties::getRenderPipelineType()) {
332 case RenderPipelineType::SkiaGL:
333 return "Skia (OpenGL)";
334 case RenderPipelineType::SkiaVulkan:
335 return "Skia (Vulkan)";
336 default:
337 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
338 }
339 }
340
dumpGraphicsMemory(int fd,bool includeProfileData)341 void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
342 if (includeProfileData) {
343 globalProfileData()->dump(fd);
344 }
345
346 String8 cachesOutput;
347 mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
348 dprintf(fd, "\nPipeline=%s\n%s\n", pipelineToString(), cachesOutput.string());
349 }
350
getMemoryUsage(size_t * cpuUsage,size_t * gpuUsage)351 void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
352 mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
353 }
354
readback()355 Readback& RenderThread::readback() {
356 if (!mReadback) {
357 mReadback = new Readback(*this);
358 }
359
360 return *mReadback;
361 }
362
setGrContext(sk_sp<GrDirectContext> context)363 void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
364 mCacheManager->reset(context);
365 if (mGrContext) {
366 mRenderState->onContextDestroyed();
367 mGrContext->releaseResourcesAndAbandonContext();
368 }
369 mGrContext = std::move(context);
370 if (mGrContext) {
371 DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
372 }
373 }
374
requireGrContext()375 sk_sp<GrDirectContext> RenderThread::requireGrContext() {
376 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
377 requireGlContext();
378 } else {
379 requireVkContext();
380 }
381 return mGrContext;
382 }
383
choreographerCallback(int fd,int events,void * data)384 int RenderThread::choreographerCallback(int fd, int events, void* data) {
385 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
386 ALOGE("Display event receiver pipe was closed or an error occurred. "
387 "events=0x%x",
388 events);
389 return 0; // remove the callback
390 }
391
392 if (!(events & Looper::EVENT_INPUT)) {
393 ALOGW("Received spurious callback for unhandled poll event. "
394 "events=0x%x",
395 events);
396 return 1; // keep the callback
397 }
398 RenderThread* rt = reinterpret_cast<RenderThread*>(data);
399 AChoreographer_handlePendingEvents(rt->mChoreographer, data);
400
401 return 1;
402 }
403
dispatchFrameCallbacks()404 void RenderThread::dispatchFrameCallbacks() {
405 ATRACE_CALL();
406 mFrameCallbackTaskPending = false;
407
408 std::set<IFrameCallback*> callbacks;
409 mFrameCallbacks.swap(callbacks);
410
411 if (callbacks.size()) {
412 // Assume one of them will probably animate again so preemptively
413 // request the next vsync in case it occurs mid-frame
414 requestVsync();
415 for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
416 it++) {
417 (*it)->doFrame();
418 }
419 }
420 }
421
requestVsync()422 void RenderThread::requestVsync() {
423 if (!mVsyncRequested) {
424 mVsyncRequested = true;
425 mVsyncSource->requestNextVsync();
426 }
427 }
428
threadLoop()429 bool RenderThread::threadLoop() {
430 setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
431 Looper::setForThread(mLooper);
432 if (gOnStartHook) {
433 gOnStartHook("RenderThread");
434 }
435 initThreadLocals();
436
437 while (true) {
438 waitForWork();
439 processQueue();
440
441 if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
442 mVsyncSource->drainPendingEvents();
443 mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
444 mPendingRegistrationFrameCallbacks.end());
445 mPendingRegistrationFrameCallbacks.clear();
446 requestVsync();
447 }
448
449 if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
450 // TODO: Clean this up. This is working around an issue where a combination
451 // of bad timing and slow drawing can result in dropping a stale vsync
452 // on the floor (correct!) but fails to schedule to listen for the
453 // next vsync (oops), so none of the callbacks are run.
454 requestVsync();
455 }
456 }
457
458 return false;
459 }
460
postFrameCallback(IFrameCallback * callback)461 void RenderThread::postFrameCallback(IFrameCallback* callback) {
462 mPendingRegistrationFrameCallbacks.insert(callback);
463 }
464
removeFrameCallback(IFrameCallback * callback)465 bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
466 size_t erased;
467 erased = mFrameCallbacks.erase(callback);
468 erased |= mPendingRegistrationFrameCallbacks.erase(callback);
469 return erased;
470 }
471
pushBackFrameCallback(IFrameCallback * callback)472 void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
473 if (mFrameCallbacks.erase(callback)) {
474 mPendingRegistrationFrameCallbacks.insert(callback);
475 }
476 }
477
allocateHardwareBitmap(SkBitmap & skBitmap)478 sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
479 auto renderType = Properties::getRenderPipelineType();
480 switch (renderType) {
481 case RenderPipelineType::SkiaVulkan:
482 return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
483 default:
484 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
485 break;
486 }
487 return nullptr;
488 }
489
isCurrent()490 bool RenderThread::isCurrent() {
491 return gettid() == getInstance().getTid();
492 }
493
preload()494 void RenderThread::preload() {
495 // EGL driver is always preloaded only if HWUI renders with GL.
496 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
497 std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
498 eglInitThread.detach();
499 } else {
500 requireVkContext();
501 }
502 HardwareBitmapUploader::initialize();
503 }
504
505 } /* namespace renderthread */
506 } /* namespace uirenderer */
507 } /* namespace android */
508