blob: 332471545f82117f3cee067c58a5b72b7d5cf487 [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
rnleece9762b2021-05-21 15:40:53 -070019#include <gui/TraceUtils.h>
John Reckba6adf62015-02-19 14:36:50 -080020#include "DeferredLayerUpdater.h"
21#include "DisplayList.h"
John Recka8963062017-06-14 10:47:50 -070022#include "Properties.h"
John Reck10dd0582016-03-31 16:36:16 -070023#include "Readback.h"
John Reckba6adf62015-02-19 14:36:50 -080024#include "Rect.h"
John Reck5cca8f22018-12-10 17:06:22 -080025#include "WebViewFunctorManager.h"
John Reckba6adf62015-02-19 14:36:50 -080026#include "renderthread/CanvasContext.h"
27#include "renderthread/RenderTask.h"
28#include "renderthread/RenderThread.h"
29#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070030#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080031
Kevin Lubick1175dc02022-02-28 12:41:27 -050032#include <SkBitmap.h>
33#include <SkImage.h>
34#include <SkPicture.h>
35
Bo Liu6a3fc602021-07-17 16:42:13 -040036#include <pthread.h>
37
John Reck4f02bf42014-01-03 18:09:17 -080038namespace 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) {
John Reckf8441e62017-10-23 13:10:41 -070045 mContext = mRenderThread.queue().runSync([&]() -> CanvasContext* {
46 return CanvasContext::create(mRenderThread, translucent, rootRenderNode, contextFactory);
47 });
Bo Liu6a3fc602021-07-17 16:42:13 -040048 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode,
49 pthread_gettid_np(pthread_self()), getRenderThreadTid());
John Reck4f02bf42014-01-03 18:09:17 -080050}
51
52RenderProxy::~RenderProxy() {
53 destroyContext();
54}
55
John Reck4f02bf42014-01-03 18:09:17 -080056void RenderProxy::destroyContext() {
57 if (mContext) {
Bo Liu6a3fc602021-07-17 16:42:13 -040058 mDrawFrameTask.setContext(nullptr, nullptr, nullptr, -1, -1);
John Reck668f0e32014-03-26 15:10:40 -070059 // This is also a fence as we need to be certain that there are no
60 // outstanding mDrawFrame tasks posted before it is destroyed
John Reck1bcacfd2017-11-03 10:12:19 -070061 mRenderThread.queue().runSync([this]() { delete mContext; });
John Reckf8441e62017-10-23 13:10:41 -070062 mContext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080063 }
64}
65
John Reck1125d1f2014-10-23 11:02:19 -070066void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -070067 mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); });
John Recke4280ba2014-05-05 16:39:37 -070068}
69
70bool RenderProxy::loadSystemProperties() {
John Reckf8441e62017-10-23 13:10:41 -070071 return mRenderThread.queue().runSync([this]() -> bool {
John Reckd9d7f122018-05-03 14:40:56 -070072 bool needsRedraw = Properties::load();
John Reckf8441e62017-10-23 13:10:41 -070073 if (mContext->profiler().consumeProperties()) {
74 needsRedraw = true;
75 }
76 return needsRedraw;
77 });
John Reckb36016c2015-03-11 08:50:53 -070078}
79
80void RenderProxy::setName(const char* name) {
John Reckf8441e62017-10-23 13:10:41 -070081 // block since name/value pointers owned by caller
82 // TODO: Support move arguments
John Reck1bcacfd2017-11-03 10:12:19 -070083 mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); });
John Reck4f02bf42014-01-03 18:09:17 -080084}
85
Alec Mouri43fe6fc2019-12-23 07:46:19 -080086void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) {
John Recke95c62d2020-08-18 12:37:43 -070087 if (window) { ANativeWindow_acquire(window); }
Alec Mouri43fe6fc2019-12-23 07:46:19 -080088 mRenderThread.queue().post([this, win = window, enableTimeout]() mutable {
89 mContext->setSurface(win, enableTimeout);
John Recke95c62d2020-08-18 12:37:43 -070090 if (win) { ANativeWindow_release(win); }
John Reckcd18c222019-11-21 14:40:53 -080091 });
John Reck4f02bf42014-01-03 18:09:17 -080092}
93
Huihong Luo5fdf7b82021-01-15 14:27:06 -080094void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) {
95 auto funcs = mRenderThread.getASurfaceControlFunctions();
96 if (surfaceControl) {
97 funcs.acquireFunc(surfaceControl);
98 }
99 mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable {
100 mContext->setSurfaceControl(control);
101 if (control) {
102 funcs.releaseFunc(control);
103 }
104 });
105}
106
John Reck8785ceb2018-10-29 16:45:58 -0700107void RenderProxy::allocateBuffers() {
108 mRenderThread.queue().post([=]() { mContext->allocateBuffers(); });
Jorim Jaggi7823ee72018-07-17 15:24:16 +0200109}
110
John Reck8785ceb2018-10-29 16:45:58 -0700111bool RenderProxy::pause() {
John Reck1bcacfd2017-11-03 10:12:19 -0700112 return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); });
John Reck8afcc762016-04-13 10:24:06 -0700113}
114
115void RenderProxy::setStopped(bool stopped) {
John Reck1bcacfd2017-11-03 10:12:19 -0700116 mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); });
John Reck8afcc762016-04-13 10:24:06 -0700117}
118
John Reck8785ceb2018-10-29 16:45:58 -0700119void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck1bcacfd2017-11-03 10:12:19 -0700120 mRenderThread.queue().post(
John Reck8785ceb2018-10-29 16:45:58 -0700121 [=]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); });
Alan Viverette50210d92015-05-14 18:05:36 -0700122}
123
John Reck8785ceb2018-10-29 16:45:58 -0700124void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
125 mRenderThread.queue().post([=]() { mContext->setLightGeometry(lightCenter, lightRadius); });
John Reck63a06672014-05-07 13:45:54 -0700126}
127
128void RenderProxy::setOpaque(bool opaque) {
John Reck1bcacfd2017-11-03 10:12:19 -0700129 mRenderThread.queue().post([=]() { mContext->setOpaque(opaque); });
Romain Guy26a2b972017-04-17 09:39:51 -0700130}
131
John Reckb36bfdd2020-07-23 13:47:49 -0700132void RenderProxy::setColorMode(ColorMode mode) {
133 mRenderThread.queue().post([=]() { mContext->setColorMode(mode); });
Romain Guy26a2b972017-04-17 09:39:51 -0700134}
135
John Reckba6adf62015-02-19 14:36:50 -0800136int64_t* RenderProxy::frameInfo() {
137 return mDrawFrameTask.frameInfo();
138}
139
chaviwadba0b12022-03-18 17:42:15 -0500140void RenderProxy::forceDrawNextFrame() {
141 mDrawFrameTask.forceDrawNextFrame();
142}
143
John Reck2de950d2017-01-25 10:58:30 -0800144int RenderProxy::syncAndDrawFrame() {
145 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800146}
147
John Reck2de950d2017-01-25 10:58:30 -0800148void RenderProxy::destroy() {
John Reckfae904d2014-04-14 11:01:57 -0700149 // destroyCanvasAndSurface() needs a fence as when it returns the
150 // underlying BufferQueue is going to be released from under
151 // the render thread.
John Reck1bcacfd2017-11-03 10:12:19 -0700152 mRenderThread.queue().runSync([=]() { mContext->destroy(); });
John Reck0d1f6342014-03-28 20:30:27 -0700153}
154
John Reck283bb462018-12-13 16:40:14 -0800155void RenderProxy::destroyFunctor(int functor) {
156 ATRACE_CALL();
157 RenderThread& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800158 thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); });
John Reck283bb462018-12-13 16:40:14 -0800159}
160
John Reck19b6bcf2014-02-14 20:03:38 -0800161DeferredLayerUpdater* RenderProxy::createTextureLayer() {
John Reckf8441e62017-10-23 13:10:41 -0700162 return mRenderThread.queue().runSync([this]() -> auto {
163 return mContext->createTextureLayer();
164 });
John Reck3e824952014-08-20 10:08:39 -0700165}
166
John Reck2de950d2017-01-25 10:58:30 -0800167void RenderProxy::buildLayer(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700168 mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); });
John Reck19b6bcf2014-02-14 20:03:38 -0800169}
170
John Reck3731dc22015-04-13 15:20:29 -0700171bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700172 ATRACE_NAME("TextureView#getBitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400173 auto& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800174 return thread.queue().runSync([&]() -> bool {
175 return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success;
176 });
John Reck19b6bcf2014-02-14 20:03:38 -0800177}
178
John Reckd72e0a32014-05-29 18:56:11 -0700179void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
180 mDrawFrameTask.pushLayerUpdate(layer);
181}
182
183void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
184 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800185}
186
John Reck918ad522014-06-27 14:45:25 -0700187void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700188 return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); });
John Recke1628b72014-05-23 15:11:19 -0700189}
190
John Reck2de950d2017-01-25 10:58:30 -0800191void RenderProxy::destroyHardwareResources() {
John Reck1bcacfd2017-11-03 10:12:19 -0700192 return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); });
John Reckf47a5942014-06-30 16:20:04 -0700193}
194
195void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700196 // Avoid creating a RenderThread to do a trimMemory.
197 if (RenderThread::hasInstance()) {
198 RenderThread& thread = RenderThread::getInstance();
John Reck5f66fb82022-09-23 17:49:23 -0400199 const auto trimLevel = static_cast<TrimLevel>(level);
200 thread.queue().post([&thread, trimLevel]() { thread.trimMemory(trimLevel); });
John Reckcd3a22c2014-08-06 13:33:59 -0700201 }
John Reckf47a5942014-06-30 16:20:04 -0700202}
203
John Reck39207682021-05-12 19:10:47 -0400204void RenderProxy::purgeCaches() {
205 if (RenderThread::hasInstance()) {
206 RenderThread& thread = RenderThread::getInstance();
207 thread.queue().post([&thread]() {
208 if (thread.getGrContext()) {
John Reck5f66fb82022-09-23 17:49:23 -0400209 thread.cacheManager().trimMemory(TrimLevel::COMPLETE);
John Reck39207682021-05-12 19:10:47 -0400210 }
211 });
212 }
213}
214
Chris Craik2507c342015-05-04 14:36:49 -0700215void RenderProxy::overrideProperty(const char* name, const char* value) {
John Reckf8441e62017-10-23 13:10:41 -0700216 // expensive, but block here since name/value pointers owned by caller
John Reck1bcacfd2017-11-03 10:12:19 -0700217 RenderThread::getInstance().queue().runSync(
218 [&]() { Properties::overrideProperty(name, value); });
Chris Craik2507c342015-05-04 14:36:49 -0700219}
220
John Reck28ad7b52014-04-07 16:59:25 -0700221void RenderProxy::fence() {
John Reck1bcacfd2017-11-03 10:12:19 -0700222 mRenderThread.queue().runSync([]() {});
John Reck28ad7b52014-04-07 16:59:25 -0700223}
224
John Recke4c1e6c2018-05-24 16:27:35 -0700225int RenderProxy::maxTextureSize() {
John Reck5cca8f22018-12-10 17:06:22 -0800226 static int maxTextureSize = RenderThread::getInstance().queue().runSync(
227 []() { return DeviceInfo::get()->maxTextureSize(); });
John Recke4c1e6c2018-05-24 16:27:35 -0700228 return maxTextureSize;
John Reckf47a5942014-06-30 16:20:04 -0700229}
230
231void RenderProxy::stopDrawing() {
John Reck1bcacfd2017-11-03 10:12:19 -0700232 mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); });
John Recka5dda642014-05-22 15:43:54 -0700233}
234
235void RenderProxy::notifyFramePending() {
John Reck1bcacfd2017-11-03 10:12:19 -0700236 mRenderThread.queue().post([this]() { mContext->notifyFramePending(); });
John Reckfe5e7b72014-05-23 17:42:28 -0700237}
238
John Reckba6adf62015-02-19 14:36:50 -0800239void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckf8441e62017-10-23 13:10:41 -0700240 mRenderThread.queue().runSync([&]() {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100241 std::lock_guard lock(mRenderThread.getJankDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700242 mContext->profiler().dumpData(fd);
243 if (dumpFlags & DumpFlags::FrameStats) {
244 mContext->dumpFrames(fd);
245 }
246 if (dumpFlags & DumpFlags::JankStats) {
247 mRenderThread.globalProfileData()->dump(fd);
248 }
249 if (dumpFlags & DumpFlags::Reset) {
250 mContext->resetFrameStats();
251 }
252 });
John Reck7f2e5e32015-05-05 11:00:53 -0700253}
254
255void RenderProxy::resetProfileInfo() {
Jorim Jaggi33adb572021-02-22 14:27:53 +0100256 mRenderThread.queue().runSync([=]() {
257 std::lock_guard lock(mRenderThread.getJankDataMutex());
258 mContext->resetFrameStats();
259 });
John Reck7f2e5e32015-05-05 11:00:53 -0700260}
261
John Reckf8441e62017-10-23 13:10:41 -0700262uint32_t RenderProxy::frameTimePercentile(int percentile) {
263 return mRenderThread.queue().runSync([&]() -> auto {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100264 std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700265 return mRenderThread.globalProfileData()->findPercentile(percentile);
266 });
John Reck0e89e2b2014-10-31 14:49:06 -0700267}
268
John Reck712eae02021-10-01 15:24:27 -0400269void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData, bool resetProfile) {
John Reckba7e9652019-01-23 10:33:41 -0800270 if (RenderThread::hasInstance()) {
271 auto& thread = RenderThread::getInstance();
John Reck712eae02021-10-01 15:24:27 -0400272 thread.queue().runSync([&]() {
273 thread.dumpGraphicsMemory(fd, includeProfileData);
274 if (resetProfile) {
275 thread.globalProfileData()->reset();
276 }
277 });
John Reckba7e9652019-01-23 10:33:41 -0800278 }
John Reckedc524c2015-03-18 15:24:33 -0700279}
280
John Reck39207682021-05-12 19:10:47 -0400281void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
282 if (RenderThread::hasInstance()) {
283 auto& thread = RenderThread::getInstance();
284 thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); });
285 }
286}
287
John Reckedc524c2015-03-18 15:24:33 -0700288void RenderProxy::setProcessStatsBuffer(int fd) {
John Reckdf1742e2017-01-19 15:56:21 -0800289 auto& rt = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700290 rt.queue().post([&rt, fd = dup(fd)]() {
John Reckf8441e62017-10-23 13:10:41 -0700291 rt.globalProfileData().switchStorageToAshmem(fd);
292 close(fd);
293 });
John Reckdf1742e2017-01-19 15:56:21 -0800294}
295
296void RenderProxy::rotateProcessStatsBuffer() {
John Reckdf1742e2017-01-19 15:56:21 -0800297 auto& rt = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700298 rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); });
John Reckedc524c2015-03-18 15:24:33 -0700299}
300
Tim Murray33eb07f2016-06-10 10:03:20 -0700301int RenderProxy::getRenderThreadTid() {
302 return mRenderThread.getTid();
303}
304
Skuhneea7a7fb2015-08-28 07:10:31 -0700305void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
John Reck1bcacfd2017-11-03 10:12:19 -0700306 mRenderThread.queue().post([=]() { mContext->addRenderNode(node, placeFront); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700307}
308
309void RenderProxy::removeRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700310 mRenderThread.queue().post([=]() { mContext->removeRenderNode(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700311}
312
313void RenderProxy::drawRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700314 mRenderThread.queue().runSync([=]() { mContext->prepareAndDraw(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700315}
316
Skuhneb8160872015-09-22 09:51:39 -0700317void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
John Reckf138b172017-09-08 11:00:42 -0700318 mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700319}
320
John Reck5cca8f22018-12-10 17:06:22 -0800321void RenderProxy::setPictureCapturedCallback(
322 const std::function<void(sk_sp<SkPicture>&&)>& callback) {
323 mRenderThread.queue().post(
John Reck0fa0cbc2019-04-05 16:57:46 -0700324 [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); });
John Reck5cca8f22018-12-10 17:06:22 -0800325}
326
Huihong Luo054b8d32021-02-24 18:48:12 -0800327void RenderProxy::setASurfaceTransactionCallback(
Huihong Luo4df41512021-06-24 10:04:32 -0700328 const std::function<bool(int64_t, int64_t, int64_t)>& callback) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800329 mRenderThread.queue().post(
330 [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); });
331}
332
Huihong Luo34f42fd2021-05-03 14:47:36 -0700333void RenderProxy::setPrepareSurfaceControlForWebviewCallback(
334 const std::function<void()>& callback) {
335 mRenderThread.queue().post(
336 [this, cb = callback]() { mContext->setPrepareSurfaceControlForWebviewCallback(cb); });
337}
338
chaviwb6803712021-12-13 15:46:29 -0600339void RenderProxy::setFrameCallback(
340 std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) {
Mihai Popa95688002018-02-23 16:10:11 +0000341 mDrawFrameTask.setFrameCallback(std::move(callback));
342}
343
chaviw9c137532021-08-20 12:15:48 -0500344void RenderProxy::setFrameCommitCallback(std::function<void(bool)>&& callback) {
345 mDrawFrameTask.setFrameCommitCallback(std::move(callback));
346}
347
348void RenderProxy::setFrameCompleteCallback(std::function<void()>&& callback) {
John Reckcc2eee82018-05-17 10:44:00 -0700349 mDrawFrameTask.setFrameCompleteCallback(std::move(callback));
350}
351
John Reckf8441e62017-10-23 13:10:41 -0700352void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700353 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700354 mContext->addFrameMetricsObserver(observer.get());
355 });
Andres Morales06f5bc72015-12-15 15:21:31 -0800356}
357
John Reckf8441e62017-10-23 13:10:41 -0700358void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700359 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700360 mContext->removeFrameMetricsObserver(observer.get());
361 });
John Reck10dd0582016-03-31 16:36:16 -0700362}
363
John Reckbb3a3582018-09-26 11:21:08 -0700364void RenderProxy::setForceDark(bool enable) {
John Reck5cca8f22018-12-10 17:06:22 -0800365 mRenderThread.queue().post([this, enable]() { mContext->setForceDark(enable); });
John Reckbb3a3582018-09-26 11:21:08 -0700366}
367
John Reck4d73cb12022-07-27 10:32:52 -0400368void RenderProxy::copySurfaceInto(ANativeWindow* window, std::shared_ptr<CopyRequest>&& request) {
John Reckf8441e62017-10-23 13:10:41 -0700369 auto& thread = RenderThread::getInstance();
John Reck4d73cb12022-07-27 10:32:52 -0400370 ANativeWindow_acquire(window);
371 thread.queue().post([&thread, window, request = std::move(request)] {
372 thread.readback().copySurfaceInto(window, request);
373 ANativeWindow_release(window);
374 });
John Reck43871902016-08-01 14:39:24 -0700375}
376
sergeyvec4a4b12016-10-20 18:39:04 -0700377void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700378 // If we haven't spun up a hardware accelerated window yet, there's no
379 // point in precaching these bitmaps as it can't impact jank.
380 // We also don't know if we even will spin up a hardware-accelerated
381 // window or not.
382 if (!RenderThread::hasInstance()) return;
383 RenderThread* renderThread = &RenderThread::getInstance();
sergeyvec4a4b12016-10-20 18:39:04 -0700384 bitmap.ref();
John Reckf8441e62017-10-23 13:10:41 -0700385 auto task = [renderThread, &bitmap]() {
386 CanvasContext::prepareToDraw(*renderThread, &bitmap);
387 bitmap.unref();
388 };
John Reck43871902016-08-01 14:39:24 -0700389 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
390 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100391 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
John Reck43871902016-08-01 14:39:24 -0700392 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
393 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
394 // be idle
395 // TODO: Make this concept a first-class supported thing? RT could use
396 // knowledge of pending draws to better schedule this task
397 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
John Reckf8441e62017-10-23 13:10:41 -0700398 renderThread->queue().postAt(estimatedNextVsync + 8_ms, task);
John Reck43871902016-08-01 14:39:24 -0700399 } else {
John Reckf8441e62017-10-23 13:10:41 -0700400 renderThread->queue().post(task);
John Reck43871902016-08-01 14:39:24 -0700401 }
402}
403
Stan Iliev1a025a72018-09-05 16:35:11 -0400404int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700405 ATRACE_NAME("HardwareBitmap readback");
Stan Iliev6983bc42017-02-02 14:11:53 -0500406 RenderThread& thread = RenderThread::getInstance();
John Reck1072fff2018-04-12 15:20:09 -0700407 if (gettid() == thread.getTid()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700408 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
Stan Iliev1a025a72018-09-05 16:35:11 -0400409 return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap);
Stan Iliev6983bc42017-02-02 14:11:53 -0500410 } else {
John Reck5cca8f22018-12-10 17:06:22 -0800411 return thread.queue().runSync(
412 [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); });
Stan Iliev6983bc42017-02-02 14:11:53 -0500413 }
sergeyv59eecb522016-11-17 17:54:57 -0800414}
415
John Reck76005182021-06-09 22:43:05 -0400416int RenderProxy::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) {
417 RenderThread& thread = RenderThread::getInstance();
418 if (gettid() == thread.getTid()) {
419 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
420 return (int)thread.readback().copyImageInto(image, bitmap);
421 } else {
422 return thread.queue().runSync(
423 [&]() -> int { return (int)thread.readback().copyImageInto(image, bitmap); });
424 }
425}
426
John Recka8963062017-06-14 10:47:50 -0700427void RenderProxy::disableVsync() {
428 Properties::disableVsync = true;
429}
430
Stan Iliev898123b2019-02-14 14:57:44 -0500431void RenderProxy::preload() {
432 // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver.
433 auto& thread = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700434 thread.queue().post([&thread]() { thread.preload(); });
Stan Iliev898123b2019-02-14 14:57:44 -0500435}
436
chaviw01053d432022-03-18 17:54:00 -0500437void RenderProxy::setRtAnimationsEnabled(bool enabled) {
438 if (RenderThread::hasInstance()) {
439 RenderThread::getInstance().queue().post(
440 [enabled]() { Properties::enableRTAnimations = enabled; });
441 } else {
442 Properties::enableRTAnimations = enabled;
443 }
444}
445
John Reck4f02bf42014-01-03 18:09:17 -0800446} /* namespace renderthread */
447} /* namespace uirenderer */
448} /* namespace android */