• 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 "RenderProxy.h"
18 
19 #include <SkBitmap.h>
20 #include <SkImage.h>
21 #include <SkPicture.h>
22 #include <gui/TraceUtils.h>
23 #include <pthread.h>
24 #include <ui/GraphicBufferAllocator.h>
25 
26 #include "DeferredLayerUpdater.h"
27 #include "DisplayList.h"
28 #include "Properties.h"
29 #include "Readback.h"
30 #include "Rect.h"
31 #include "WebViewFunctorManager.h"
32 #include "renderthread/CanvasContext.h"
33 #include "renderthread/RenderTask.h"
34 #include "renderthread/RenderThread.h"
35 #include "utils/Macros.h"
36 #include "utils/TimeUtils.h"
37 
38 namespace android {
39 namespace uirenderer {
40 namespace renderthread {
41 
RenderProxy(bool translucent,RenderNode * rootRenderNode,IContextFactory * contextFactory)42 RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode,
43                          IContextFactory* contextFactory)
44         : mRenderThread(RenderThread::getInstance()), mContext(nullptr) {
45 #ifdef __ANDROID__
46     pid_t uiThreadId = pthread_gettid_np(pthread_self());
47 #else
48     pid_t uiThreadId = 0;
49 #endif
50     pid_t renderThreadId = getRenderThreadTid();
51     mContext = mRenderThread.queue().runSync([=, this]() -> CanvasContext* {
52         CanvasContext* context = CanvasContext::create(mRenderThread, translucent, rootRenderNode,
53                                                        contextFactory, uiThreadId, renderThreadId);
54         if (context != nullptr) {
55             mRenderThread.queue().post([=] { context->startHintSession(); });
56         }
57         return context;
58     });
59     mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
60 }
61 
~RenderProxy()62 RenderProxy::~RenderProxy() {
63     destroyContext();
64 }
65 
destroyContext()66 void RenderProxy::destroyContext() {
67     if (mContext) {
68         mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
69         // This is also a fence as we need to be certain that there are no
70         // outstanding mDrawFrame tasks posted before it is destroyed
71         mRenderThread.queue().runSync([this]() { delete mContext; });
72         mContext = nullptr;
73     }
74 }
75 
setSwapBehavior(SwapBehavior swapBehavior)76 void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
77     mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); });
78 }
79 
loadSystemProperties()80 bool RenderProxy::loadSystemProperties() {
81     return mRenderThread.queue().runSync([this]() -> bool {
82         bool needsRedraw = Properties::load();
83         if (mContext->profiler().consumeProperties()) {
84             needsRedraw = true;
85         }
86         return needsRedraw;
87     });
88 }
89 
setName(const char * name)90 void RenderProxy::setName(const char* name) {
91     // block since name/value pointers owned by caller
92     // TODO: Support move arguments
93     mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); });
94 }
95 
setHardwareBuffer(AHardwareBuffer * buffer)96 void RenderProxy::setHardwareBuffer(AHardwareBuffer* buffer) {
97 #ifdef __ANDROID__
98     if (buffer) {
99         AHardwareBuffer_acquire(buffer);
100     }
101     mRenderThread.queue().post([this, hardwareBuffer = buffer]() mutable {
102         mContext->setHardwareBuffer(hardwareBuffer);
103         if (hardwareBuffer) {
104             AHardwareBuffer_release(hardwareBuffer);
105         }
106     });
107 #endif
108 }
109 
setSurface(ANativeWindow * window,bool enableTimeout)110 void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) {
111     if (window) { ANativeWindow_acquire(window); }
112     mRenderThread.queue().post([this, win = window, enableTimeout]() mutable {
113         mContext->setSurface(win, enableTimeout);
114         if (win) { ANativeWindow_release(win); }
115     });
116 }
117 
setSurfaceControl(ASurfaceControl * surfaceControl)118 void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) {
119     auto funcs = mRenderThread.getASurfaceControlFunctions();
120     if (surfaceControl) {
121         funcs.acquireFunc(surfaceControl);
122     }
123     mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable {
124         mContext->setSurfaceControl(control);
125         if (control) {
126             funcs.releaseFunc(control);
127         }
128     });
129 }
130 
allocateBuffers()131 void RenderProxy::allocateBuffers() {
132     mRenderThread.queue().post([this]() { mContext->allocateBuffers(); });
133 }
134 
pause()135 bool RenderProxy::pause() {
136     return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); });
137 }
138 
setStopped(bool stopped)139 void RenderProxy::setStopped(bool stopped) {
140     mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); });
141 }
142 
setLightAlpha(uint8_t ambientShadowAlpha,uint8_t spotShadowAlpha)143 void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
144     mRenderThread.queue().post(
145             [=, this]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); });
146 }
147 
setLightGeometry(const Vector3 & lightCenter,float lightRadius)148 void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
149     mRenderThread.queue().post(
150             [=, this]() { mContext->setLightGeometry(lightCenter, lightRadius); });
151 }
152 
setOpaque(bool opaque)153 void RenderProxy::setOpaque(bool opaque) {
154     mRenderThread.queue().post([=, this]() { mContext->setOpaque(opaque); });
155 }
156 
setColorMode(ColorMode mode)157 float RenderProxy::setColorMode(ColorMode mode) {
158     // We only need to figure out what the renderer supports for HDR, otherwise this can stay
159     // an async call since we already know the return value
160     if (mode == ColorMode::Hdr || mode == ColorMode::Hdr10) {
161         return mRenderThread.queue().runSync(
162                 [=, this]() -> float { return mContext->setColorMode(mode); });
163     } else {
164         mRenderThread.queue().post([=, this]() { mContext->setColorMode(mode); });
165         return 1.f;
166     }
167 }
168 
setRenderSdrHdrRatio(float ratio)169 void RenderProxy::setRenderSdrHdrRatio(float ratio) {
170     mDrawFrameTask.setRenderSdrHdrRatio(ratio);
171 }
172 
frameInfo()173 int64_t* RenderProxy::frameInfo() {
174     return mDrawFrameTask.frameInfo();
175 }
176 
forceDrawNextFrame()177 void RenderProxy::forceDrawNextFrame() {
178     mDrawFrameTask.forceDrawNextFrame();
179 }
180 
syncAndDrawFrame()181 int RenderProxy::syncAndDrawFrame() {
182     return mDrawFrameTask.drawFrame();
183 }
184 
destroy()185 void RenderProxy::destroy() {
186     // destroyCanvasAndSurface() needs a fence as when it returns the
187     // underlying BufferQueue is going to be released from under
188     // the render thread.
189     mRenderThread.queue().runSync([this]() { mContext->destroy(); });
190 }
191 
destroyFunctor(int functor)192 void RenderProxy::destroyFunctor(int functor) {
193     ATRACE_CALL();
194     RenderThread& thread = RenderThread::getInstance();
195     thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); });
196 }
197 
createTextureLayer()198 DeferredLayerUpdater* RenderProxy::createTextureLayer() {
199     return mRenderThread.queue().runSync([this]() -> auto {
200         return mContext->createTextureLayer();
201     });
202 }
203 
buildLayer(RenderNode * node)204 void RenderProxy::buildLayer(RenderNode* node) {
205     mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); });
206 }
207 
copyLayerInto(DeferredLayerUpdater * layer,SkBitmap & bitmap)208 bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
209     ATRACE_NAME("TextureView#getBitmap");
210     auto& thread = RenderThread::getInstance();
211     return thread.queue().runSync([&]() -> bool {
212         return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success;
213     });
214 }
215 
pushLayerUpdate(DeferredLayerUpdater * layer)216 void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
217     mDrawFrameTask.pushLayerUpdate(layer);
218 }
219 
cancelLayerUpdate(DeferredLayerUpdater * layer)220 void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
221     mDrawFrameTask.removeLayerUpdate(layer);
222 }
223 
detachSurfaceTexture(DeferredLayerUpdater * layer)224 void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
225 #ifdef __ANDROID__
226     return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); });
227 #endif
228 }
229 
destroyHardwareResources()230 void RenderProxy::destroyHardwareResources() {
231     return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); });
232 }
233 
trimMemory(int level)234 void RenderProxy::trimMemory(int level) {
235     // Avoid creating a RenderThread to do a trimMemory.
236     if (RenderThread::hasInstance()) {
237         RenderThread& thread = RenderThread::getInstance();
238         const auto trimLevel = static_cast<TrimLevel>(level);
239         thread.queue().post([&thread, trimLevel]() { thread.trimMemory(trimLevel); });
240     }
241 }
242 
trimCaches(int level)243 void RenderProxy::trimCaches(int level) {
244     // Avoid creating a RenderThread to do a trimMemory.
245     if (RenderThread::hasInstance()) {
246         RenderThread& thread = RenderThread::getInstance();
247         const auto trimLevel = static_cast<CacheTrimLevel>(level);
248         thread.queue().post([&thread, trimLevel]() { thread.trimCaches(trimLevel); });
249     }
250 }
251 
purgeCaches()252 void RenderProxy::purgeCaches() {
253     if (RenderThread::hasInstance()) {
254         RenderThread& thread = RenderThread::getInstance();
255         thread.queue().post([&thread]() {
256             if (thread.getGrContext()) {
257                 thread.cacheManager().trimMemory(TrimLevel::COMPLETE);
258             }
259         });
260     }
261 }
262 
overrideProperty(const char * name,const char * value)263 void RenderProxy::overrideProperty(const char* name, const char* value) {
264     // expensive, but block here since name/value pointers owned by caller
265     RenderThread::getInstance().queue().runSync(
266             [&]() { Properties::overrideProperty(name, value); });
267 }
268 
fence()269 void RenderProxy::fence() {
270     mRenderThread.queue().runSync([]() {});
271 }
272 
maxTextureSize()273 int RenderProxy::maxTextureSize() {
274     static int maxTextureSize = RenderThread::getInstance().queue().runSync(
275             []() { return DeviceInfo::get()->maxTextureSize(); });
276     return maxTextureSize;
277 }
278 
stopDrawing()279 void RenderProxy::stopDrawing() {
280     mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); });
281 }
282 
notifyFramePending()283 void RenderProxy::notifyFramePending() {
284     mRenderThread.queue().post([this]() { mContext->notifyFramePending(); });
285 }
286 
notifyCallbackPending()287 void RenderProxy::notifyCallbackPending() {
288     mRenderThread.queue().post([this]() { mContext->sendLoadResetHint(); });
289 }
290 
notifyExpensiveFrame()291 void RenderProxy::notifyExpensiveFrame() {
292     mRenderThread.queue().post([this]() { mContext->sendLoadIncreaseHint(); });
293 }
294 
dumpProfileInfo(int fd,int dumpFlags)295 void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
296     mRenderThread.queue().runSync([&]() {
297         std::lock_guard lock(mRenderThread.getJankDataMutex());
298         mContext->profiler().dumpData(fd);
299         if (dumpFlags & DumpFlags::FrameStats) {
300             mContext->dumpFrames(fd);
301         }
302         if (dumpFlags & DumpFlags::JankStats) {
303             mRenderThread.globalProfileData()->dump(fd);
304         }
305         if (dumpFlags & DumpFlags::Reset) {
306             mContext->resetFrameStats();
307         }
308     });
309 }
310 
resetProfileInfo()311 void RenderProxy::resetProfileInfo() {
312     mRenderThread.queue().runSync([this]() {
313         std::lock_guard lock(mRenderThread.getJankDataMutex());
314         mContext->resetFrameStats();
315     });
316 }
317 
frameTimePercentile(int percentile)318 uint32_t RenderProxy::frameTimePercentile(int percentile) {
319     return mRenderThread.queue().runSync([&]() -> auto {
320         std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
321         return mRenderThread.globalProfileData()->findPercentile(percentile);
322     });
323 }
324 
dumpGraphicsMemory(int fd,bool includeProfileData,bool resetProfile)325 void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData, bool resetProfile) {
326     if (RenderThread::hasInstance()) {
327         auto& thread = RenderThread::getInstance();
328         thread.queue().runSync([&]() {
329             thread.dumpGraphicsMemory(fd, includeProfileData);
330             if (resetProfile) {
331                 thread.globalProfileData()->reset();
332             }
333         });
334     }
335 #ifdef __ANDROID__
336     if (!Properties::isolatedProcess) {
337         std::string grallocInfo;
338         GraphicBufferAllocator::getInstance().dump(grallocInfo);
339         dprintf(fd, "%s\n", grallocInfo.c_str());
340     }
341 #endif
342 }
343 
getMemoryUsage(size_t * cpuUsage,size_t * gpuUsage)344 void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
345     if (RenderThread::hasInstance()) {
346         auto& thread = RenderThread::getInstance();
347         thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); });
348     }
349 }
350 
setProcessStatsBuffer(int fd)351 void RenderProxy::setProcessStatsBuffer(int fd) {
352     auto& rt = RenderThread::getInstance();
353     rt.queue().post([&rt, fd = dup(fd)]() {
354         rt.globalProfileData().switchStorageToAshmem(fd);
355         close(fd);
356     });
357 }
358 
rotateProcessStatsBuffer()359 void RenderProxy::rotateProcessStatsBuffer() {
360     auto& rt = RenderThread::getInstance();
361     rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); });
362 }
363 
getRenderThreadTid()364 int RenderProxy::getRenderThreadTid() {
365 #ifdef __ANDROID__
366     return mRenderThread.getTid();
367 #else
368     return 0;
369 #endif
370 }
371 
addRenderNode(RenderNode * node,bool placeFront)372 void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
373     mRenderThread.queue().post([=, this]() { mContext->addRenderNode(node, placeFront); });
374 }
375 
removeRenderNode(RenderNode * node)376 void RenderProxy::removeRenderNode(RenderNode* node) {
377     mRenderThread.queue().post([=, this]() { mContext->removeRenderNode(node); });
378 }
379 
drawRenderNode(RenderNode * node)380 void RenderProxy::drawRenderNode(RenderNode* node) {
381     mRenderThread.queue().runSync([=, this]() { mContext->prepareAndDraw(node); });
382 }
383 
setContentDrawBounds(int left,int top,int right,int bottom)384 void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
385     mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
386 }
387 
setHardwareBufferRenderParams(const HardwareBufferRenderParams & params)388 void RenderProxy::setHardwareBufferRenderParams(const HardwareBufferRenderParams& params) {
389     mDrawFrameTask.setHardwareBufferRenderParams(params);
390 }
391 
setPictureCapturedCallback(const std::function<void (sk_sp<SkPicture> &&)> & callback)392 void RenderProxy::setPictureCapturedCallback(
393         const std::function<void(sk_sp<SkPicture>&&)>& callback) {
394     mRenderThread.queue().post(
395             [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); });
396 }
397 
setASurfaceTransactionCallback(const std::function<bool (int64_t,int64_t,int64_t)> & callback)398 void RenderProxy::setASurfaceTransactionCallback(
399         const std::function<bool(int64_t, int64_t, int64_t)>& callback) {
400     mRenderThread.queue().post(
401             [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); });
402 }
403 
setPrepareSurfaceControlForWebviewCallback(const std::function<void ()> & callback)404 void RenderProxy::setPrepareSurfaceControlForWebviewCallback(
405         const std::function<void()>& callback) {
406     mRenderThread.queue().post(
407             [this, cb = callback]() { mContext->setPrepareSurfaceControlForWebviewCallback(cb); });
408 }
409 
setFrameCallback(std::function<std::function<void (bool)> (int32_t,int64_t)> && callback)410 void RenderProxy::setFrameCallback(
411         std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) {
412     mDrawFrameTask.setFrameCallback(std::move(callback));
413 }
414 
setFrameCommitCallback(std::function<void (bool)> && callback)415 void RenderProxy::setFrameCommitCallback(std::function<void(bool)>&& callback) {
416     mDrawFrameTask.setFrameCommitCallback(std::move(callback));
417 }
418 
setFrameCompleteCallback(std::function<void ()> && callback)419 void RenderProxy::setFrameCompleteCallback(std::function<void()>&& callback) {
420     mDrawFrameTask.setFrameCompleteCallback(std::move(callback));
421 }
422 
addFrameMetricsObserver(FrameMetricsObserver * observerPtr)423 void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
424     mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
425         mContext->addFrameMetricsObserver(observer.get());
426     });
427 }
428 
removeFrameMetricsObserver(FrameMetricsObserver * observerPtr)429 void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
430     mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
431         mContext->removeFrameMetricsObserver(observer.get());
432     });
433 }
434 
setForceDark(ForceDarkType type)435 void RenderProxy::setForceDark(ForceDarkType type) {
436     mRenderThread.queue().post([this, type]() { mContext->setForceDark(type); });
437 }
438 
copySurfaceInto(ANativeWindow * window,std::shared_ptr<CopyRequest> && request)439 void RenderProxy::copySurfaceInto(ANativeWindow* window, std::shared_ptr<CopyRequest>&& request) {
440     auto& thread = RenderThread::getInstance();
441     ANativeWindow_acquire(window);
442     thread.queue().post([&thread, window, request = std::move(request)] {
443         thread.readback().copySurfaceInto(window, request);
444         ANativeWindow_release(window);
445     });
446 }
447 
prepareToDraw(Bitmap & bitmap)448 void RenderProxy::prepareToDraw(Bitmap& bitmap) {
449     // If we haven't spun up a hardware accelerated window yet, there's no
450     // point in precaching these bitmaps as it can't impact jank.
451     // We also don't know if we even will spin up a hardware-accelerated
452     // window or not.
453     if (!RenderThread::hasInstance()) return;
454     RenderThread* renderThread = &RenderThread::getInstance();
455     bitmap.ref();
456     auto task = [renderThread, &bitmap]() {
457         CanvasContext::prepareToDraw(*renderThread, &bitmap);
458         bitmap.unref();
459     };
460     nsecs_t lastVsync = renderThread->timeLord().latestVsync();
461     nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
462     nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
463     // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
464     // VSYNC+12ms or so, so aim for the gap during which RT is expected to
465     // be idle
466     // TODO: Make this concept a first-class supported thing? RT could use
467     // knowledge of pending draws to better schedule this task
468     if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
469         renderThread->queue().postAt(estimatedNextVsync + 8_ms, task);
470     } else {
471         renderThread->queue().post(task);
472     }
473 }
474 
copyHWBitmapInto(Bitmap * hwBitmap,SkBitmap * bitmap)475 int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
476     ATRACE_NAME("HardwareBitmap readback");
477     RenderThread& thread = RenderThread::getInstance();
478     if (RenderThread::isCurrent()) {
479         // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
480         return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap);
481     } else {
482         return thread.queue().runSync(
483                 [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); });
484     }
485 }
486 
copyImageInto(const sk_sp<SkImage> & image,SkBitmap * bitmap)487 int RenderProxy::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) {
488     RenderThread& thread = RenderThread::getInstance();
489     if (RenderThread::isCurrent()) {
490         // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
491         return (int)thread.readback().copyImageInto(image, bitmap);
492     } else {
493         return thread.queue().runSync(
494                 [&]() -> int { return (int)thread.readback().copyImageInto(image, bitmap); });
495     }
496 }
497 
disableVsync()498 void RenderProxy::disableVsync() {
499     Properties::disableVsync = true;
500 }
501 
preload()502 void RenderProxy::preload() {
503     // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver.
504     auto& thread = RenderThread::getInstance();
505     thread.queue().post([&thread]() { thread.preload(); });
506 }
507 
setRtAnimationsEnabled(bool enabled)508 void RenderProxy::setRtAnimationsEnabled(bool enabled) {
509     if (RenderThread::hasInstance()) {
510         RenderThread::getInstance().queue().post(
511                 [enabled]() { Properties::enableRTAnimations = enabled; });
512     } else {
513         Properties::enableRTAnimations = enabled;
514     }
515 }
516 
517 } /* namespace renderthread */
518 } /* namespace uirenderer */
519 } /* namespace android */
520