blob: eab36050896f71a27064997077631d529f9759a9 [file] [log] [blame]
John Reck4f02bf42014-01-03 18:09:17 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Reck4f02bf42014-01-03 18:09:17 -080017#include "RenderProxy.h"
18
John Reck2c9a2db2023-07-21 16:35:41 -040019#include <SkBitmap.h>
20#include <SkImage.h>
21#include <SkPicture.h>
rnleece9762b2021-05-21 15:40:53 -070022#include <gui/TraceUtils.h>
John Reck2c9a2db2023-07-21 16:35:41 -040023#include <pthread.h>
24#include <ui/GraphicBufferAllocator.h>
25
John Reckba6adf62015-02-19 14:36:50 -080026#include "DeferredLayerUpdater.h"
27#include "DisplayList.h"
John Recka8963062017-06-14 10:47:50 -070028#include "Properties.h"
John Reck10dd0582016-03-31 16:36:16 -070029#include "Readback.h"
John Reckba6adf62015-02-19 14:36:50 -080030#include "Rect.h"
John Reck5cca8f22018-12-10 17:06:22 -080031#include "WebViewFunctorManager.h"
John Reckba6adf62015-02-19 14:36:50 -080032#include "renderthread/CanvasContext.h"
33#include "renderthread/RenderTask.h"
34#include "renderthread/RenderThread.h"
35#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070036#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080037
38namespace android {
39namespace uirenderer {
40namespace renderthread {
41
John Reck1bcacfd2017-11-03 10:12:19 -070042RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode,
43 IContextFactory* contextFactory)
44 : mRenderThread(RenderThread::getInstance()), mContext(nullptr) {
Matt Buckleye9023cf2022-11-23 22:39:25 +000045 pid_t uiThreadId = pthread_gettid_np(pthread_self());
46 pid_t renderThreadId = getRenderThreadTid();
47 mContext = mRenderThread.queue().runSync([=, this]() -> CanvasContext* {
Matt Buckleyf5f90f12023-01-28 04:09:14 +000048 CanvasContext* context = CanvasContext::create(mRenderThread, translucent, rootRenderNode,
49 contextFactory, uiThreadId, renderThreadId);
50 if (context != nullptr) {
51 mRenderThread.queue().post([=] { context->startHintSession(); });
52 }
53 return context;
John Reckf8441e62017-10-23 13:10:41 -070054 });
Matt Buckleye9023cf2022-11-23 22:39:25 +000055 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080056}
57
58RenderProxy::~RenderProxy() {
59 destroyContext();
60}
61
John Reck4f02bf42014-01-03 18:09:17 -080062void RenderProxy::destroyContext() {
63 if (mContext) {
Matt Buckleye9023cf2022-11-23 22:39:25 +000064 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070065 // This is also a fence as we need to be certain that there are no
66 // outstanding mDrawFrame tasks posted before it is destroyed
John Reck1bcacfd2017-11-03 10:12:19 -070067 mRenderThread.queue().runSync([this]() { delete mContext; });
John Reckf8441e62017-10-23 13:10:41 -070068 mContext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080069 }
70}
71
John Reck1125d1f2014-10-23 11:02:19 -070072void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -070073 mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); });
John Recke4280ba2014-05-05 16:39:37 -070074}
75
76bool RenderProxy::loadSystemProperties() {
John Reckf8441e62017-10-23 13:10:41 -070077 return mRenderThread.queue().runSync([this]() -> bool {
John Reckd9d7f122018-05-03 14:40:56 -070078 bool needsRedraw = Properties::load();
John Reckf8441e62017-10-23 13:10:41 -070079 if (mContext->profiler().consumeProperties()) {
80 needsRedraw = true;
81 }
82 return needsRedraw;
83 });
John Reckb36016c2015-03-11 08:50:53 -070084}
85
86void RenderProxy::setName(const char* name) {
John Reckf8441e62017-10-23 13:10:41 -070087 // block since name/value pointers owned by caller
88 // TODO: Support move arguments
John Reck1bcacfd2017-11-03 10:12:19 -070089 mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); });
John Reck4f02bf42014-01-03 18:09:17 -080090}
91
Nader Jawada3521852023-01-30 20:23:46 -080092void RenderProxy::setHardwareBuffer(AHardwareBuffer* buffer) {
93 if (buffer) {
94 AHardwareBuffer_acquire(buffer);
95 }
96 mRenderThread.queue().post([this, hardwareBuffer = buffer]() mutable {
97 mContext->setHardwareBuffer(hardwareBuffer);
98 if (hardwareBuffer) {
99 AHardwareBuffer_release(hardwareBuffer);
100 }
101 });
102}
103
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800104void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) {
John Recke95c62d2020-08-18 12:37:43 -0700105 if (window) { ANativeWindow_acquire(window); }
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800106 mRenderThread.queue().post([this, win = window, enableTimeout]() mutable {
107 mContext->setSurface(win, enableTimeout);
John Recke95c62d2020-08-18 12:37:43 -0700108 if (win) { ANativeWindow_release(win); }
John Reckcd18c222019-11-21 14:40:53 -0800109 });
John Reck4f02bf42014-01-03 18:09:17 -0800110}
111
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800112void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) {
113 auto funcs = mRenderThread.getASurfaceControlFunctions();
114 if (surfaceControl) {
115 funcs.acquireFunc(surfaceControl);
116 }
117 mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable {
118 mContext->setSurfaceControl(control);
119 if (control) {
120 funcs.releaseFunc(control);
121 }
122 });
123}
124
John Reck8785ceb2018-10-29 16:45:58 -0700125void RenderProxy::allocateBuffers() {
John Reck23750022023-11-21 18:30:35 -0500126 mRenderThread.queue().post([this]() { mContext->allocateBuffers(); });
Jorim Jaggi7823ee72018-07-17 15:24:16 +0200127}
128
John Reck8785ceb2018-10-29 16:45:58 -0700129bool RenderProxy::pause() {
John Reck1bcacfd2017-11-03 10:12:19 -0700130 return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); });
John Reck8afcc762016-04-13 10:24:06 -0700131}
132
133void RenderProxy::setStopped(bool stopped) {
John Reck1bcacfd2017-11-03 10:12:19 -0700134 mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); });
John Reck8afcc762016-04-13 10:24:06 -0700135}
136
John Reck8785ceb2018-10-29 16:45:58 -0700137void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck1bcacfd2017-11-03 10:12:19 -0700138 mRenderThread.queue().post(
John Reck23750022023-11-21 18:30:35 -0500139 [=, this]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); });
Alan Viverette50210d92015-05-14 18:05:36 -0700140}
141
John Reck8785ceb2018-10-29 16:45:58 -0700142void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
John Reck23750022023-11-21 18:30:35 -0500143 mRenderThread.queue().post(
144 [=, this]() { mContext->setLightGeometry(lightCenter, lightRadius); });
John Reck63a06672014-05-07 13:45:54 -0700145}
146
147void RenderProxy::setOpaque(bool opaque) {
John Reck23750022023-11-21 18:30:35 -0500148 mRenderThread.queue().post([=, this]() { mContext->setOpaque(opaque); });
Romain Guy26a2b972017-04-17 09:39:51 -0700149}
150
John Reck55887762023-01-25 16:51:18 -0500151float RenderProxy::setColorMode(ColorMode mode) {
152 // We only need to figure out what the renderer supports for HDR, otherwise this can stay
153 // an async call since we already know the return value
John Reck0b3f3312023-01-31 16:21:28 -0500154 if (mode == ColorMode::Hdr || mode == ColorMode::Hdr10) {
John Reck55887762023-01-25 16:51:18 -0500155 return mRenderThread.queue().runSync(
John Reck23750022023-11-21 18:30:35 -0500156 [=, this]() -> float { return mContext->setColorMode(mode); });
John Reck55887762023-01-25 16:51:18 -0500157 } else {
John Reck23750022023-11-21 18:30:35 -0500158 mRenderThread.queue().post([=, this]() { mContext->setColorMode(mode); });
John Reck55887762023-01-25 16:51:18 -0500159 return 1.f;
160 }
161}
162
163void RenderProxy::setRenderSdrHdrRatio(float ratio) {
164 mDrawFrameTask.setRenderSdrHdrRatio(ratio);
Romain Guy26a2b972017-04-17 09:39:51 -0700165}
166
John Reckba6adf62015-02-19 14:36:50 -0800167int64_t* RenderProxy::frameInfo() {
168 return mDrawFrameTask.frameInfo();
169}
170
chaviwadba0b12022-03-18 17:42:15 -0500171void RenderProxy::forceDrawNextFrame() {
172 mDrawFrameTask.forceDrawNextFrame();
173}
174
John Reck2de950d2017-01-25 10:58:30 -0800175int RenderProxy::syncAndDrawFrame() {
176 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800177}
178
John Reck2de950d2017-01-25 10:58:30 -0800179void RenderProxy::destroy() {
John Reckfae904d2014-04-14 11:01:57 -0700180 // destroyCanvasAndSurface() needs a fence as when it returns the
181 // underlying BufferQueue is going to be released from under
182 // the render thread.
John Reck23750022023-11-21 18:30:35 -0500183 mRenderThread.queue().runSync([this]() { mContext->destroy(); });
John Reck0d1f6342014-03-28 20:30:27 -0700184}
185
John Reck283bb462018-12-13 16:40:14 -0800186void RenderProxy::destroyFunctor(int functor) {
187 ATRACE_CALL();
188 RenderThread& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800189 thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); });
John Reck283bb462018-12-13 16:40:14 -0800190}
191
John Reck19b6bcf2014-02-14 20:03:38 -0800192DeferredLayerUpdater* RenderProxy::createTextureLayer() {
John Reckf8441e62017-10-23 13:10:41 -0700193 return mRenderThread.queue().runSync([this]() -> auto {
194 return mContext->createTextureLayer();
195 });
John Reck3e824952014-08-20 10:08:39 -0700196}
197
John Reck2de950d2017-01-25 10:58:30 -0800198void RenderProxy::buildLayer(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700199 mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); });
John Reck19b6bcf2014-02-14 20:03:38 -0800200}
201
John Reck3731dc22015-04-13 15:20:29 -0700202bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700203 ATRACE_NAME("TextureView#getBitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400204 auto& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800205 return thread.queue().runSync([&]() -> bool {
206 return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success;
207 });
John Reck19b6bcf2014-02-14 20:03:38 -0800208}
209
John Reckd72e0a32014-05-29 18:56:11 -0700210void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
211 mDrawFrameTask.pushLayerUpdate(layer);
212}
213
214void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
215 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800216}
217
John Reck918ad522014-06-27 14:45:25 -0700218void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700219 return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); });
John Recke1628b72014-05-23 15:11:19 -0700220}
221
John Reck2de950d2017-01-25 10:58:30 -0800222void RenderProxy::destroyHardwareResources() {
John Reck1bcacfd2017-11-03 10:12:19 -0700223 return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); });
John Reckf47a5942014-06-30 16:20:04 -0700224}
225
226void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700227 // Avoid creating a RenderThread to do a trimMemory.
228 if (RenderThread::hasInstance()) {
229 RenderThread& thread = RenderThread::getInstance();
John Reck5f66fb82022-09-23 17:49:23 -0400230 const auto trimLevel = static_cast<TrimLevel>(level);
231 thread.queue().post([&thread, trimLevel]() { thread.trimMemory(trimLevel); });
John Reckcd3a22c2014-08-06 13:33:59 -0700232 }
John Reckf47a5942014-06-30 16:20:04 -0700233}
234
Jernej Virag44db0402023-05-09 20:24:48 +0200235void RenderProxy::trimCaches(int level) {
236 // Avoid creating a RenderThread to do a trimMemory.
237 if (RenderThread::hasInstance()) {
238 RenderThread& thread = RenderThread::getInstance();
239 const auto trimLevel = static_cast<CacheTrimLevel>(level);
240 thread.queue().post([&thread, trimLevel]() { thread.trimCaches(trimLevel); });
241 }
242}
243
John Reck39207682021-05-12 19:10:47 -0400244void RenderProxy::purgeCaches() {
245 if (RenderThread::hasInstance()) {
246 RenderThread& thread = RenderThread::getInstance();
247 thread.queue().post([&thread]() {
248 if (thread.getGrContext()) {
John Reck5f66fb82022-09-23 17:49:23 -0400249 thread.cacheManager().trimMemory(TrimLevel::COMPLETE);
John Reck39207682021-05-12 19:10:47 -0400250 }
251 });
252 }
253}
254
Chris Craik2507c342015-05-04 14:36:49 -0700255void RenderProxy::overrideProperty(const char* name, const char* value) {
John Reckf8441e62017-10-23 13:10:41 -0700256 // expensive, but block here since name/value pointers owned by caller
John Reck1bcacfd2017-11-03 10:12:19 -0700257 RenderThread::getInstance().queue().runSync(
258 [&]() { Properties::overrideProperty(name, value); });
Chris Craik2507c342015-05-04 14:36:49 -0700259}
260
John Reck28ad7b52014-04-07 16:59:25 -0700261void RenderProxy::fence() {
John Reck1bcacfd2017-11-03 10:12:19 -0700262 mRenderThread.queue().runSync([]() {});
John Reck28ad7b52014-04-07 16:59:25 -0700263}
264
John Recke4c1e6c2018-05-24 16:27:35 -0700265int RenderProxy::maxTextureSize() {
John Reck5cca8f22018-12-10 17:06:22 -0800266 static int maxTextureSize = RenderThread::getInstance().queue().runSync(
267 []() { return DeviceInfo::get()->maxTextureSize(); });
John Recke4c1e6c2018-05-24 16:27:35 -0700268 return maxTextureSize;
John Reckf47a5942014-06-30 16:20:04 -0700269}
270
271void RenderProxy::stopDrawing() {
John Reck1bcacfd2017-11-03 10:12:19 -0700272 mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); });
John Recka5dda642014-05-22 15:43:54 -0700273}
274
275void RenderProxy::notifyFramePending() {
John Reck1bcacfd2017-11-03 10:12:19 -0700276 mRenderThread.queue().post([this]() { mContext->notifyFramePending(); });
John Reckfe5e7b72014-05-23 17:42:28 -0700277}
278
Matt Buckleyd98e8052022-10-21 22:13:23 +0000279void RenderProxy::notifyCallbackPending() {
Matt Buckleye9023cf2022-11-23 22:39:25 +0000280 mRenderThread.queue().post([this]() { mContext->sendLoadResetHint(); });
Matt Buckleyd98e8052022-10-21 22:13:23 +0000281}
282
Matt Buckleyac5f7552022-12-19 22:03:27 +0000283void RenderProxy::notifyExpensiveFrame() {
284 mRenderThread.queue().post([this]() { mContext->sendLoadIncreaseHint(); });
285}
286
John Reckba6adf62015-02-19 14:36:50 -0800287void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckf8441e62017-10-23 13:10:41 -0700288 mRenderThread.queue().runSync([&]() {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100289 std::lock_guard lock(mRenderThread.getJankDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700290 mContext->profiler().dumpData(fd);
291 if (dumpFlags & DumpFlags::FrameStats) {
292 mContext->dumpFrames(fd);
293 }
294 if (dumpFlags & DumpFlags::JankStats) {
295 mRenderThread.globalProfileData()->dump(fd);
296 }
297 if (dumpFlags & DumpFlags::Reset) {
298 mContext->resetFrameStats();
299 }
300 });
John Reck7f2e5e32015-05-05 11:00:53 -0700301}
302
303void RenderProxy::resetProfileInfo() {
John Reck23750022023-11-21 18:30:35 -0500304 mRenderThread.queue().runSync([this]() {
Jorim Jaggi33adb572021-02-22 14:27:53 +0100305 std::lock_guard lock(mRenderThread.getJankDataMutex());
306 mContext->resetFrameStats();
307 });
John Reck7f2e5e32015-05-05 11:00:53 -0700308}
309
John Reckf8441e62017-10-23 13:10:41 -0700310uint32_t RenderProxy::frameTimePercentile(int percentile) {
311 return mRenderThread.queue().runSync([&]() -> auto {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100312 std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700313 return mRenderThread.globalProfileData()->findPercentile(percentile);
314 });
John Reck0e89e2b2014-10-31 14:49:06 -0700315}
316
John Reck712eae02021-10-01 15:24:27 -0400317void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData, bool resetProfile) {
John Reckba7e9652019-01-23 10:33:41 -0800318 if (RenderThread::hasInstance()) {
319 auto& thread = RenderThread::getInstance();
John Reck712eae02021-10-01 15:24:27 -0400320 thread.queue().runSync([&]() {
321 thread.dumpGraphicsMemory(fd, includeProfileData);
322 if (resetProfile) {
323 thread.globalProfileData()->reset();
324 }
325 });
John Reckba7e9652019-01-23 10:33:41 -0800326 }
John Reck0341ba52023-08-24 11:37:36 -0400327 if (!Properties::isolatedProcess) {
328 std::string grallocInfo;
329 GraphicBufferAllocator::getInstance().dump(grallocInfo);
330 dprintf(fd, "%s\n", grallocInfo.c_str());
331 }
John Reckedc524c2015-03-18 15:24:33 -0700332}
333
John Reck39207682021-05-12 19:10:47 -0400334void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
335 if (RenderThread::hasInstance()) {
336 auto& thread = RenderThread::getInstance();
337 thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); });
338 }
339}
340
John Reckedc524c2015-03-18 15:24:33 -0700341void RenderProxy::setProcessStatsBuffer(int fd) {
John Reckdf1742e2017-01-19 15:56:21 -0800342 auto& rt = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700343 rt.queue().post([&rt, fd = dup(fd)]() {
John Reckf8441e62017-10-23 13:10:41 -0700344 rt.globalProfileData().switchStorageToAshmem(fd);
345 close(fd);
346 });
John Reckdf1742e2017-01-19 15:56:21 -0800347}
348
349void RenderProxy::rotateProcessStatsBuffer() {
John Reckdf1742e2017-01-19 15:56:21 -0800350 auto& rt = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700351 rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); });
John Reckedc524c2015-03-18 15:24:33 -0700352}
353
Tim Murray33eb07f2016-06-10 10:03:20 -0700354int RenderProxy::getRenderThreadTid() {
355 return mRenderThread.getTid();
356}
357
Skuhneea7a7fb2015-08-28 07:10:31 -0700358void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
John Reck23750022023-11-21 18:30:35 -0500359 mRenderThread.queue().post([=, this]() { mContext->addRenderNode(node, placeFront); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700360}
361
362void RenderProxy::removeRenderNode(RenderNode* node) {
John Reck23750022023-11-21 18:30:35 -0500363 mRenderThread.queue().post([=, this]() { mContext->removeRenderNode(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700364}
365
366void RenderProxy::drawRenderNode(RenderNode* node) {
John Reck23750022023-11-21 18:30:35 -0500367 mRenderThread.queue().runSync([=, this]() { mContext->prepareAndDraw(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700368}
369
Skuhneb8160872015-09-22 09:51:39 -0700370void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
John Reckf138b172017-09-08 11:00:42 -0700371 mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700372}
373
Nader Jawada3521852023-01-30 20:23:46 -0800374void RenderProxy::setHardwareBufferRenderParams(const HardwareBufferRenderParams& params) {
375 mDrawFrameTask.setHardwareBufferRenderParams(params);
376}
377
John Reck5cca8f22018-12-10 17:06:22 -0800378void RenderProxy::setPictureCapturedCallback(
379 const std::function<void(sk_sp<SkPicture>&&)>& callback) {
380 mRenderThread.queue().post(
John Reck0fa0cbc2019-04-05 16:57:46 -0700381 [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); });
John Reck5cca8f22018-12-10 17:06:22 -0800382}
383
Huihong Luo054b8d32021-02-24 18:48:12 -0800384void RenderProxy::setASurfaceTransactionCallback(
Huihong Luo4df41512021-06-24 10:04:32 -0700385 const std::function<bool(int64_t, int64_t, int64_t)>& callback) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800386 mRenderThread.queue().post(
387 [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); });
388}
389
Huihong Luo34f42fd2021-05-03 14:47:36 -0700390void RenderProxy::setPrepareSurfaceControlForWebviewCallback(
391 const std::function<void()>& callback) {
392 mRenderThread.queue().post(
393 [this, cb = callback]() { mContext->setPrepareSurfaceControlForWebviewCallback(cb); });
394}
395
chaviwb6803712021-12-13 15:46:29 -0600396void RenderProxy::setFrameCallback(
397 std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) {
Mihai Popa95688002018-02-23 16:10:11 +0000398 mDrawFrameTask.setFrameCallback(std::move(callback));
399}
400
chaviw9c137532021-08-20 12:15:48 -0500401void RenderProxy::setFrameCommitCallback(std::function<void(bool)>&& callback) {
402 mDrawFrameTask.setFrameCommitCallback(std::move(callback));
403}
404
405void RenderProxy::setFrameCompleteCallback(std::function<void()>&& callback) {
John Reckcc2eee82018-05-17 10:44:00 -0700406 mDrawFrameTask.setFrameCompleteCallback(std::move(callback));
407}
408
John Reckf8441e62017-10-23 13:10:41 -0700409void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700410 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700411 mContext->addFrameMetricsObserver(observer.get());
412 });
Andres Morales06f5bc72015-12-15 15:21:31 -0800413}
414
John Reckf8441e62017-10-23 13:10:41 -0700415void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700416 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700417 mContext->removeFrameMetricsObserver(observer.get());
418 });
John Reck10dd0582016-03-31 16:36:16 -0700419}
420
Tyler Freeman417accd2023-10-31 02:01:06 +0000421void RenderProxy::setForceDark(ForceDarkType type) {
422 mRenderThread.queue().post([this, type]() { mContext->setForceDark(type); });
John Reckbb3a3582018-09-26 11:21:08 -0700423}
424
John Reck4d73cb12022-07-27 10:32:52 -0400425void RenderProxy::copySurfaceInto(ANativeWindow* window, std::shared_ptr<CopyRequest>&& request) {
John Reckf8441e62017-10-23 13:10:41 -0700426 auto& thread = RenderThread::getInstance();
John Reck4d73cb12022-07-27 10:32:52 -0400427 ANativeWindow_acquire(window);
428 thread.queue().post([&thread, window, request = std::move(request)] {
429 thread.readback().copySurfaceInto(window, request);
430 ANativeWindow_release(window);
431 });
John Reck43871902016-08-01 14:39:24 -0700432}
433
sergeyvec4a4b12016-10-20 18:39:04 -0700434void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700435 // If we haven't spun up a hardware accelerated window yet, there's no
436 // point in precaching these bitmaps as it can't impact jank.
437 // We also don't know if we even will spin up a hardware-accelerated
438 // window or not.
439 if (!RenderThread::hasInstance()) return;
440 RenderThread* renderThread = &RenderThread::getInstance();
sergeyvec4a4b12016-10-20 18:39:04 -0700441 bitmap.ref();
John Reckf8441e62017-10-23 13:10:41 -0700442 auto task = [renderThread, &bitmap]() {
443 CanvasContext::prepareToDraw(*renderThread, &bitmap);
444 bitmap.unref();
445 };
John Reck43871902016-08-01 14:39:24 -0700446 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
447 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100448 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
John Reck43871902016-08-01 14:39:24 -0700449 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
450 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
451 // be idle
452 // TODO: Make this concept a first-class supported thing? RT could use
453 // knowledge of pending draws to better schedule this task
454 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
John Reckf8441e62017-10-23 13:10:41 -0700455 renderThread->queue().postAt(estimatedNextVsync + 8_ms, task);
John Reck43871902016-08-01 14:39:24 -0700456 } else {
John Reckf8441e62017-10-23 13:10:41 -0700457 renderThread->queue().post(task);
John Reck43871902016-08-01 14:39:24 -0700458 }
459}
460
Stan Iliev1a025a72018-09-05 16:35:11 -0400461int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700462 ATRACE_NAME("HardwareBitmap readback");
Stan Iliev6983bc42017-02-02 14:11:53 -0500463 RenderThread& thread = RenderThread::getInstance();
John Reck1072fff2018-04-12 15:20:09 -0700464 if (gettid() == thread.getTid()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700465 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
Stan Iliev1a025a72018-09-05 16:35:11 -0400466 return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap);
Stan Iliev6983bc42017-02-02 14:11:53 -0500467 } else {
John Reck5cca8f22018-12-10 17:06:22 -0800468 return thread.queue().runSync(
469 [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); });
Stan Iliev6983bc42017-02-02 14:11:53 -0500470 }
sergeyv59eecb522016-11-17 17:54:57 -0800471}
472
John Reck76005182021-06-09 22:43:05 -0400473int RenderProxy::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) {
474 RenderThread& thread = RenderThread::getInstance();
475 if (gettid() == thread.getTid()) {
476 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
477 return (int)thread.readback().copyImageInto(image, bitmap);
478 } else {
479 return thread.queue().runSync(
480 [&]() -> int { return (int)thread.readback().copyImageInto(image, bitmap); });
481 }
482}
483
John Recka8963062017-06-14 10:47:50 -0700484void RenderProxy::disableVsync() {
485 Properties::disableVsync = true;
486}
487
Stan Iliev898123b2019-02-14 14:57:44 -0500488void RenderProxy::preload() {
489 // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver.
490 auto& thread = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700491 thread.queue().post([&thread]() { thread.preload(); });
Stan Iliev898123b2019-02-14 14:57:44 -0500492}
493
chaviw01053d432022-03-18 17:54:00 -0500494void RenderProxy::setRtAnimationsEnabled(bool enabled) {
495 if (RenderThread::hasInstance()) {
496 RenderThread::getInstance().queue().post(
497 [enabled]() { Properties::enableRTAnimations = enabled; });
498 } else {
499 Properties::enableRTAnimations = enabled;
500 }
501}
502
John Reck4f02bf42014-01-03 18:09:17 -0800503} /* namespace renderthread */
504} /* namespace uirenderer */
505} /* namespace android */