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