• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         mFrameCallbackTaskPending = true;
137 
138         using SteadyClock = std::chrono::steady_clock;
139         using Nanos = std::chrono::nanoseconds;
140         using toNsecs_t = std::chrono::duration<nsecs_t, std::nano>;
141         using toFloatMillis = std::chrono::duration<float, std::milli>;
142 
143         const auto frameTimeTimePoint = SteadyClock::time_point(Nanos(frameTimeNanos));
144         const auto deadlineTimePoint = SteadyClock::time_point(Nanos(frameDeadline));
145 
146         const auto timeUntilDeadline = deadlineTimePoint - frameTimeTimePoint;
147         const auto runAt = (frameTimeTimePoint + (timeUntilDeadline / 4));
148 
149         ATRACE_FORMAT("queue mFrameCallbackTask to run after %.2fms",
150                       toFloatMillis(runAt - SteadyClock::now()).count());
151         queue().postAt(toNsecs_t(runAt.time_since_epoch()).count(),
152                        [=]() { dispatchFrameCallbacks(); });
153     }
154 }
155 
refreshRateCallback(int64_t vsyncPeriod,void * data)156 void RenderThread::refreshRateCallback(int64_t vsyncPeriod, void* data) {
157     ATRACE_NAME("refreshRateCallback");
158     RenderThread* rt = reinterpret_cast<RenderThread*>(data);
159     DeviceInfo::get()->onRefreshRateChanged(vsyncPeriod);
160     rt->setupFrameInterval();
161 }
162 
163 class ChoreographerSource : public VsyncSource {
164 public:
ChoreographerSource(RenderThread * renderThread)165     ChoreographerSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
166 
requestNextVsync()167     virtual void requestNextVsync() override {
168         AChoreographer_postVsyncCallback(mRenderThread->mChoreographer,
169                                          RenderThread::extendedFrameCallback, mRenderThread);
170     }
171 
drainPendingEvents()172     virtual void drainPendingEvents() override {
173         AChoreographer_handlePendingEvents(mRenderThread->mChoreographer, mRenderThread);
174     }
175 
176 private:
177     RenderThread* mRenderThread;
178 };
179 
180 class DummyVsyncSource : public VsyncSource {
181 public:
DummyVsyncSource(RenderThread * renderThread)182     DummyVsyncSource(RenderThread* renderThread) : mRenderThread(renderThread) {}
183 
requestNextVsync()184     virtual void requestNextVsync() override {
185         mRenderThread->queue().postDelayed(16_ms, [this]() {
186             mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
187                                          std::numeric_limits<int64_t>::max(),
188                                          systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
189         });
190     }
191 
drainPendingEvents()192     virtual void drainPendingEvents() override {
193         mRenderThread->frameCallback(UiFrameInfoBuilder::INVALID_VSYNC_ID,
194                                      std::numeric_limits<int64_t>::max(),
195                                      systemTime(SYSTEM_TIME_MONOTONIC), 16_ms);
196     }
197 
198 private:
199     RenderThread* mRenderThread;
200 };
201 
hasInstance()202 bool RenderThread::hasInstance() {
203     return gHasRenderThreadInstance;
204 }
205 
setOnStartHook(JVMAttachHook onStartHook)206 void RenderThread::setOnStartHook(JVMAttachHook onStartHook) {
207     LOG_ALWAYS_FATAL_IF(hasInstance(), "can't set an onStartHook after we've started...");
208     gOnStartHook = onStartHook;
209 }
210 
getOnStartHook()211 JVMAttachHook RenderThread::getOnStartHook() {
212     return gOnStartHook;
213 }
214 
getInstance()215 RenderThread& RenderThread::getInstance() {
216     [[clang::no_destroy]] static sp<RenderThread> sInstance = []() {
217         sp<RenderThread> thread = sp<RenderThread>::make();
218         thread->start("RenderThread");
219         return thread;
220     }();
221     gHasRenderThreadInstance = true;
222     return *sInstance;
223 }
224 
RenderThread()225 RenderThread::RenderThread()
226         : ThreadBase()
227         , mVsyncSource(nullptr)
228         , mVsyncRequested(false)
229         , mFrameCallbackTaskPending(false)
230         , mRenderState(nullptr)
231         , mEglManager(nullptr)
232         , mFunctorManager(WebViewFunctorManager::instance())
233         , mGlobalProfileData(mJankDataMutex) {
234     Properties::load();
235 }
236 
~RenderThread()237 RenderThread::~RenderThread() {
238     // Note that if this fatal assertion is removed then member variables must
239     // be properly destroyed.
240     LOG_ALWAYS_FATAL("Can't destroy the render thread");
241 }
242 
initializeChoreographer()243 void RenderThread::initializeChoreographer() {
244     LOG_ALWAYS_FATAL_IF(mVsyncSource, "Initializing a second Choreographer?");
245 
246     if (!Properties::isolatedProcess) {
247         mChoreographer = AChoreographer_create();
248         LOG_ALWAYS_FATAL_IF(mChoreographer == nullptr, "Initialization of Choreographer failed");
249         AChoreographer_registerRefreshRateCallback(mChoreographer,
250                                                    RenderThread::refreshRateCallback, this);
251 
252         // Register the FD
253         mLooper->addFd(AChoreographer_getFd(mChoreographer), 0, Looper::EVENT_INPUT,
254                        RenderThread::choreographerCallback, this);
255         mVsyncSource = new ChoreographerSource(this);
256     } else {
257         mVsyncSource = new DummyVsyncSource(this);
258     }
259 }
260 
initThreadLocals()261 void RenderThread::initThreadLocals() {
262     setupFrameInterval();
263     initializeChoreographer();
264     mEglManager = new EglManager();
265     mRenderState = new RenderState(*this);
266     mVkManager = VulkanManager::getInstance();
267     mCacheManager = new CacheManager(*this);
268 }
269 
setupFrameInterval()270 void RenderThread::setupFrameInterval() {
271     nsecs_t frameIntervalNanos = DeviceInfo::getVsyncPeriod();
272     mTimeLord.setFrameInterval(frameIntervalNanos);
273 }
274 
requireGlContext()275 void RenderThread::requireGlContext() {
276     if (mEglManager->hasEglContext()) {
277         return;
278     }
279     mEglManager->initialize();
280 
281     sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
282     LOG_ALWAYS_FATAL_IF(!glInterface.get());
283 
284     GrContextOptions options;
285     initGrContextOptions(options);
286     auto glesVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
287     auto size = glesVersion ? strlen(glesVersion) : -1;
288     cacheManager().configureContext(&options, glesVersion, size);
289     sk_sp<GrDirectContext> grContext(GrDirectContext::MakeGL(std::move(glInterface), options));
290     LOG_ALWAYS_FATAL_IF(!grContext.get());
291     setGrContext(grContext);
292 }
293 
requireVkContext()294 void RenderThread::requireVkContext() {
295     // the getter creates the context in the event it had been destroyed by destroyRenderingContext
296     // Also check if we have a GrContext before returning fast. VulkanManager may be shared with
297     // the HardwareBitmapUploader which initializes the Vk context without persisting the GrContext
298     // in the rendering thread.
299     if (vulkanManager().hasVkContext() && mGrContext) {
300         return;
301     }
302     mVkManager->initialize();
303     GrContextOptions options;
304     initGrContextOptions(options);
305     auto vkDriverVersion = mVkManager->getDriverVersion();
306     cacheManager().configureContext(&options, &vkDriverVersion, sizeof(vkDriverVersion));
307     sk_sp<GrDirectContext> grContext = mVkManager->createContext(options);
308     LOG_ALWAYS_FATAL_IF(!grContext.get());
309     setGrContext(grContext);
310 }
311 
initGrContextOptions(GrContextOptions & options)312 void RenderThread::initGrContextOptions(GrContextOptions& options) {
313     options.fPreferExternalImagesOverES3 = true;
314     options.fDisableDistanceFieldPaths = true;
315     if (android::base::GetBoolProperty(PROPERTY_REDUCE_OPS_TASK_SPLITTING, true)) {
316         options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kYes;
317     } else {
318         options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
319     }
320 }
321 
destroyRenderingContext()322 void RenderThread::destroyRenderingContext() {
323     mFunctorManager.onContextDestroyed();
324     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
325         if (mEglManager->hasEglContext()) {
326             setGrContext(nullptr);
327             mEglManager->destroy();
328         }
329     } else {
330         setGrContext(nullptr);
331         mVkManager.clear();
332     }
333 }
334 
vulkanManager()335 VulkanManager& RenderThread::vulkanManager() {
336     if (!mVkManager.get()) {
337         mVkManager = VulkanManager::getInstance();
338     }
339     return *mVkManager.get();
340 }
341 
pipelineToString()342 static const char* pipelineToString() {
343     switch (auto renderType = Properties::getRenderPipelineType()) {
344         case RenderPipelineType::SkiaGL:
345             return "Skia (OpenGL)";
346         case RenderPipelineType::SkiaVulkan:
347             return "Skia (Vulkan)";
348         default:
349             LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
350     }
351 }
352 
dumpGraphicsMemory(int fd,bool includeProfileData)353 void RenderThread::dumpGraphicsMemory(int fd, bool includeProfileData) {
354     if (includeProfileData) {
355         globalProfileData()->dump(fd);
356     }
357 
358     String8 cachesOutput;
359     mCacheManager->dumpMemoryUsage(cachesOutput, mRenderState);
360     dprintf(fd, "\nPipeline=%s\n%s\n", pipelineToString(), cachesOutput.string());
361 }
362 
getMemoryUsage(size_t * cpuUsage,size_t * gpuUsage)363 void RenderThread::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
364     mCacheManager->getMemoryUsage(cpuUsage, gpuUsage);
365 }
366 
readback()367 Readback& RenderThread::readback() {
368     if (!mReadback) {
369         mReadback = new Readback(*this);
370     }
371 
372     return *mReadback;
373 }
374 
setGrContext(sk_sp<GrDirectContext> context)375 void RenderThread::setGrContext(sk_sp<GrDirectContext> context) {
376     mCacheManager->reset(context);
377     if (mGrContext) {
378         mRenderState->onContextDestroyed();
379         mGrContext->releaseResourcesAndAbandonContext();
380     }
381     mGrContext = std::move(context);
382     if (mGrContext) {
383         DeviceInfo::setMaxTextureSize(mGrContext->maxRenderTargetSize());
384     }
385 }
386 
requireGrContext()387 sk_sp<GrDirectContext> RenderThread::requireGrContext() {
388     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
389         requireGlContext();
390     } else {
391         requireVkContext();
392     }
393     return mGrContext;
394 }
395 
choreographerCallback(int fd,int events,void * data)396 int RenderThread::choreographerCallback(int fd, int events, void* data) {
397     if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
398         ALOGE("Display event receiver pipe was closed or an error occurred.  "
399               "events=0x%x",
400               events);
401         return 0;  // remove the callback
402     }
403 
404     if (!(events & Looper::EVENT_INPUT)) {
405         ALOGW("Received spurious callback for unhandled poll event.  "
406               "events=0x%x",
407               events);
408         return 1;  // keep the callback
409     }
410     RenderThread* rt = reinterpret_cast<RenderThread*>(data);
411     AChoreographer_handlePendingEvents(rt->mChoreographer, data);
412 
413     return 1;
414 }
415 
dispatchFrameCallbacks()416 void RenderThread::dispatchFrameCallbacks() {
417     ATRACE_CALL();
418     mFrameCallbackTaskPending = false;
419 
420     std::set<IFrameCallback*> callbacks;
421     mFrameCallbacks.swap(callbacks);
422 
423     if (callbacks.size()) {
424         // Assume one of them will probably animate again so preemptively
425         // request the next vsync in case it occurs mid-frame
426         requestVsync();
427         for (std::set<IFrameCallback*>::iterator it = callbacks.begin(); it != callbacks.end();
428              it++) {
429             (*it)->doFrame();
430         }
431     }
432 }
433 
requestVsync()434 void RenderThread::requestVsync() {
435     if (!mVsyncRequested) {
436         mVsyncRequested = true;
437         mVsyncSource->requestNextVsync();
438     }
439 }
440 
threadLoop()441 bool RenderThread::threadLoop() {
442     setpriority(PRIO_PROCESS, 0, PRIORITY_DISPLAY);
443     Looper::setForThread(mLooper);
444     if (gOnStartHook) {
445         gOnStartHook("RenderThread");
446     }
447     initThreadLocals();
448 
449     while (true) {
450         waitForWork();
451         processQueue();
452 
453         if (mPendingRegistrationFrameCallbacks.size() && !mFrameCallbackTaskPending) {
454             mVsyncSource->drainPendingEvents();
455             mFrameCallbacks.insert(mPendingRegistrationFrameCallbacks.begin(),
456                                    mPendingRegistrationFrameCallbacks.end());
457             mPendingRegistrationFrameCallbacks.clear();
458             requestVsync();
459         }
460 
461         if (!mFrameCallbackTaskPending && !mVsyncRequested && mFrameCallbacks.size()) {
462             // TODO: Clean this up. This is working around an issue where a combination
463             // of bad timing and slow drawing can result in dropping a stale vsync
464             // on the floor (correct!) but fails to schedule to listen for the
465             // next vsync (oops), so none of the callbacks are run.
466             requestVsync();
467         }
468 
469         mCacheManager->onThreadIdle();
470     }
471 
472     return false;
473 }
474 
postFrameCallback(IFrameCallback * callback)475 void RenderThread::postFrameCallback(IFrameCallback* callback) {
476     mPendingRegistrationFrameCallbacks.insert(callback);
477 }
478 
removeFrameCallback(IFrameCallback * callback)479 bool RenderThread::removeFrameCallback(IFrameCallback* callback) {
480     size_t erased;
481     erased = mFrameCallbacks.erase(callback);
482     erased |= mPendingRegistrationFrameCallbacks.erase(callback);
483     return erased;
484 }
485 
pushBackFrameCallback(IFrameCallback * callback)486 void RenderThread::pushBackFrameCallback(IFrameCallback* callback) {
487     if (mFrameCallbacks.erase(callback)) {
488         mPendingRegistrationFrameCallbacks.insert(callback);
489     }
490 }
491 
allocateHardwareBitmap(SkBitmap & skBitmap)492 sk_sp<Bitmap> RenderThread::allocateHardwareBitmap(SkBitmap& skBitmap) {
493     auto renderType = Properties::getRenderPipelineType();
494     switch (renderType) {
495         case RenderPipelineType::SkiaVulkan:
496             return skiapipeline::SkiaVulkanPipeline::allocateHardwareBitmap(*this, skBitmap);
497         default:
498             LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
499             break;
500     }
501     return nullptr;
502 }
503 
isCurrent()504 bool RenderThread::isCurrent() {
505     return gettid() == getInstance().getTid();
506 }
507 
preload()508 void RenderThread::preload() {
509     // EGL driver is always preloaded only if HWUI renders with GL.
510     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
511         std::thread eglInitThread([]() { eglGetDisplay(EGL_DEFAULT_DISPLAY); });
512         eglInitThread.detach();
513     } else {
514         requireVkContext();
515     }
516     HardwareBitmapUploader::initialize();
517 }
518 
trimMemory(TrimLevel level)519 void RenderThread::trimMemory(TrimLevel level) {
520     ATRACE_CALL();
521     cacheManager().trimMemory(level);
522 }
523 
trimCaches(CacheTrimLevel level)524 void RenderThread::trimCaches(CacheTrimLevel level) {
525     ATRACE_CALL();
526     cacheManager().trimCaches(level);
527 }
528 
529 } /* namespace renderthread */
530 } /* namespace uirenderer */
531 } /* namespace android */
532