blob: 8291fa7e4bab989c8a0306b52125124c2a7df87b [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"
John Reckda355962021-06-24 17:09:32 -040029#include "thread/CommonPool.h"
John Reckba6adf62015-02-19 14:36:50 -080030#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070031#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080032
33namespace android {
34namespace uirenderer {
35namespace renderthread {
36
John Reck1bcacfd2017-11-03 10:12:19 -070037RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode,
38 IContextFactory* contextFactory)
39 : mRenderThread(RenderThread::getInstance()), mContext(nullptr) {
John Reckf8441e62017-10-23 13:10:41 -070040 mContext = mRenderThread.queue().runSync([&]() -> CanvasContext* {
41 return CanvasContext::create(mRenderThread, translucent, rootRenderNode, contextFactory);
42 });
Skuhneea7a7fb2015-08-28 07:10:31 -070043 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080044}
45
John Reckda355962021-06-24 17:09:32 -040046void RenderProxy::asyncDelete(RenderProxy* proxy) {
47 if (!proxy) return;
48
49 if (proxy->mContext) {
50 // Use the common pool because ~RenderProxy blocks on calling into RenderThread
51 CommonPool::post([proxy]() { delete proxy; });
52 } else {
53 delete proxy;
54 }
55}
56
John Reck4f02bf42014-01-03 18:09:17 -080057RenderProxy::~RenderProxy() {
58 destroyContext();
59}
60
John Reck4f02bf42014-01-03 18:09:17 -080061void RenderProxy::destroyContext() {
62 if (mContext) {
Skuhneea7a7fb2015-08-28 07:10:31 -070063 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070064 // This is also a fence as we need to be certain that there are no
65 // outstanding mDrawFrame tasks posted before it is destroyed
John Reck1bcacfd2017-11-03 10:12:19 -070066 mRenderThread.queue().runSync([this]() { delete mContext; });
John Reckf8441e62017-10-23 13:10:41 -070067 mContext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080068 }
69}
70
John Reck1125d1f2014-10-23 11:02:19 -070071void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -070072 mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); });
John Recke4280ba2014-05-05 16:39:37 -070073}
74
75bool RenderProxy::loadSystemProperties() {
John Reckf8441e62017-10-23 13:10:41 -070076 return mRenderThread.queue().runSync([this]() -> bool {
John Reckd9d7f122018-05-03 14:40:56 -070077 bool needsRedraw = Properties::load();
John Reckf8441e62017-10-23 13:10:41 -070078 if (mContext->profiler().consumeProperties()) {
79 needsRedraw = true;
80 }
81 return needsRedraw;
82 });
John Reckb36016c2015-03-11 08:50:53 -070083}
84
85void RenderProxy::setName(const char* name) {
John Reckf8441e62017-10-23 13:10:41 -070086 // block since name/value pointers owned by caller
87 // TODO: Support move arguments
John Reck1bcacfd2017-11-03 10:12:19 -070088 mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); });
John Reck4f02bf42014-01-03 18:09:17 -080089}
90
Bo Liu027b2182021-03-18 16:50:38 -040091void RenderProxy::setHintSessionCallbacks(std::function<void(int64_t)> updateTargetWorkDuration,
92 std::function<void(int64_t)> reportActualWorkDuration) {
93 mDrawFrameTask.setHintSessionCallbacks(std::move(updateTargetWorkDuration),
94 std::move(reportActualWorkDuration));
95}
96
Alec Mouri43fe6fc2019-12-23 07:46:19 -080097void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) {
John Recke95c62d2020-08-18 12:37:43 -070098 if (window) { ANativeWindow_acquire(window); }
Alec Mouri43fe6fc2019-12-23 07:46:19 -080099 mRenderThread.queue().post([this, win = window, enableTimeout]() mutable {
100 mContext->setSurface(win, enableTimeout);
John Recke95c62d2020-08-18 12:37:43 -0700101 if (win) { ANativeWindow_release(win); }
John Reckcd18c222019-11-21 14:40:53 -0800102 });
John Reck4f02bf42014-01-03 18:09:17 -0800103}
104
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800105void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) {
106 auto funcs = mRenderThread.getASurfaceControlFunctions();
107 if (surfaceControl) {
108 funcs.acquireFunc(surfaceControl);
109 }
110 mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable {
111 mContext->setSurfaceControl(control);
112 if (control) {
113 funcs.releaseFunc(control);
114 }
115 });
116}
117
John Reck8785ceb2018-10-29 16:45:58 -0700118void RenderProxy::allocateBuffers() {
119 mRenderThread.queue().post([=]() { mContext->allocateBuffers(); });
Jorim Jaggi7823ee72018-07-17 15:24:16 +0200120}
121
John Reck8785ceb2018-10-29 16:45:58 -0700122bool RenderProxy::pause() {
John Reck1bcacfd2017-11-03 10:12:19 -0700123 return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); });
John Reck8afcc762016-04-13 10:24:06 -0700124}
125
126void RenderProxy::setStopped(bool stopped) {
John Reck1bcacfd2017-11-03 10:12:19 -0700127 mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); });
John Reck8afcc762016-04-13 10:24:06 -0700128}
129
John Reck8785ceb2018-10-29 16:45:58 -0700130void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck1bcacfd2017-11-03 10:12:19 -0700131 mRenderThread.queue().post(
John Reck8785ceb2018-10-29 16:45:58 -0700132 [=]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); });
Alan Viverette50210d92015-05-14 18:05:36 -0700133}
134
John Reck8785ceb2018-10-29 16:45:58 -0700135void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
136 mRenderThread.queue().post([=]() { mContext->setLightGeometry(lightCenter, lightRadius); });
John Reck63a06672014-05-07 13:45:54 -0700137}
138
139void RenderProxy::setOpaque(bool opaque) {
John Reck1bcacfd2017-11-03 10:12:19 -0700140 mRenderThread.queue().post([=]() { mContext->setOpaque(opaque); });
Romain Guy26a2b972017-04-17 09:39:51 -0700141}
142
John Reckb36bfdd2020-07-23 13:47:49 -0700143void RenderProxy::setColorMode(ColorMode mode) {
144 mRenderThread.queue().post([=]() { mContext->setColorMode(mode); });
Romain Guy26a2b972017-04-17 09:39:51 -0700145}
146
John Reckba6adf62015-02-19 14:36:50 -0800147int64_t* RenderProxy::frameInfo() {
148 return mDrawFrameTask.frameInfo();
149}
150
John Reck2de950d2017-01-25 10:58:30 -0800151int RenderProxy::syncAndDrawFrame() {
152 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800153}
154
John Reck2de950d2017-01-25 10:58:30 -0800155void RenderProxy::destroy() {
John Reckfae904d2014-04-14 11:01:57 -0700156 // destroyCanvasAndSurface() needs a fence as when it returns the
157 // underlying BufferQueue is going to be released from under
158 // the render thread.
John Reck1bcacfd2017-11-03 10:12:19 -0700159 mRenderThread.queue().runSync([=]() { mContext->destroy(); });
John Reck0d1f6342014-03-28 20:30:27 -0700160}
161
John Reck283bb462018-12-13 16:40:14 -0800162void RenderProxy::destroyFunctor(int functor) {
163 ATRACE_CALL();
164 RenderThread& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800165 thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); });
John Reck283bb462018-12-13 16:40:14 -0800166}
167
John Reck19b6bcf2014-02-14 20:03:38 -0800168DeferredLayerUpdater* RenderProxy::createTextureLayer() {
John Reckf8441e62017-10-23 13:10:41 -0700169 return mRenderThread.queue().runSync([this]() -> auto {
170 return mContext->createTextureLayer();
171 });
John Reck3e824952014-08-20 10:08:39 -0700172}
173
John Reck2de950d2017-01-25 10:58:30 -0800174void RenderProxy::buildLayer(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700175 mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); });
John Reck19b6bcf2014-02-14 20:03:38 -0800176}
177
John Reck3731dc22015-04-13 15:20:29 -0700178bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700179 ATRACE_NAME("TextureView#getBitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400180 auto& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800181 return thread.queue().runSync([&]() -> bool {
182 return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success;
183 });
John Reck19b6bcf2014-02-14 20:03:38 -0800184}
185
John Reckd72e0a32014-05-29 18:56:11 -0700186void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
187 mDrawFrameTask.pushLayerUpdate(layer);
188}
189
190void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
191 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800192}
193
John Reck918ad522014-06-27 14:45:25 -0700194void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700195 return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); });
John Recke1628b72014-05-23 15:11:19 -0700196}
197
John Reck2de950d2017-01-25 10:58:30 -0800198void RenderProxy::destroyHardwareResources() {
John Reck1bcacfd2017-11-03 10:12:19 -0700199 return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); });
John Reckf47a5942014-06-30 16:20:04 -0700200}
201
202void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700203 // Avoid creating a RenderThread to do a trimMemory.
204 if (RenderThread::hasInstance()) {
205 RenderThread& thread = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700206 thread.queue().post([&thread, level]() { CanvasContext::trimMemory(thread, level); });
John Reckcd3a22c2014-08-06 13:33:59 -0700207 }
John Reckf47a5942014-06-30 16:20:04 -0700208}
209
John Reck39207682021-05-12 19:10:47 -0400210void RenderProxy::purgeCaches() {
211 if (RenderThread::hasInstance()) {
212 RenderThread& thread = RenderThread::getInstance();
213 thread.queue().post([&thread]() {
214 if (thread.getGrContext()) {
215 thread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
216 }
217 });
218 }
219}
220
Chris Craik2507c342015-05-04 14:36:49 -0700221void RenderProxy::overrideProperty(const char* name, const char* value) {
John Reckf8441e62017-10-23 13:10:41 -0700222 // expensive, but block here since name/value pointers owned by caller
John Reck1bcacfd2017-11-03 10:12:19 -0700223 RenderThread::getInstance().queue().runSync(
224 [&]() { Properties::overrideProperty(name, value); });
Chris Craik2507c342015-05-04 14:36:49 -0700225}
226
John Reck28ad7b52014-04-07 16:59:25 -0700227void RenderProxy::fence() {
John Reck1bcacfd2017-11-03 10:12:19 -0700228 mRenderThread.queue().runSync([]() {});
John Reck28ad7b52014-04-07 16:59:25 -0700229}
230
John Recke4c1e6c2018-05-24 16:27:35 -0700231int RenderProxy::maxTextureSize() {
John Reck5cca8f22018-12-10 17:06:22 -0800232 static int maxTextureSize = RenderThread::getInstance().queue().runSync(
233 []() { return DeviceInfo::get()->maxTextureSize(); });
John Recke4c1e6c2018-05-24 16:27:35 -0700234 return maxTextureSize;
John Reckf47a5942014-06-30 16:20:04 -0700235}
236
237void RenderProxy::stopDrawing() {
John Reck1bcacfd2017-11-03 10:12:19 -0700238 mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); });
John Recka5dda642014-05-22 15:43:54 -0700239}
240
241void RenderProxy::notifyFramePending() {
John Reck1bcacfd2017-11-03 10:12:19 -0700242 mRenderThread.queue().post([this]() { mContext->notifyFramePending(); });
John Reckfe5e7b72014-05-23 17:42:28 -0700243}
244
John Reckba6adf62015-02-19 14:36:50 -0800245void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckf8441e62017-10-23 13:10:41 -0700246 mRenderThread.queue().runSync([&]() {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100247 std::lock_guard lock(mRenderThread.getJankDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700248 mContext->profiler().dumpData(fd);
249 if (dumpFlags & DumpFlags::FrameStats) {
250 mContext->dumpFrames(fd);
251 }
252 if (dumpFlags & DumpFlags::JankStats) {
253 mRenderThread.globalProfileData()->dump(fd);
254 }
255 if (dumpFlags & DumpFlags::Reset) {
256 mContext->resetFrameStats();
257 }
258 });
John Reck7f2e5e32015-05-05 11:00:53 -0700259}
260
261void RenderProxy::resetProfileInfo() {
Jorim Jaggi33adb572021-02-22 14:27:53 +0100262 mRenderThread.queue().runSync([=]() {
263 std::lock_guard lock(mRenderThread.getJankDataMutex());
264 mContext->resetFrameStats();
265 });
John Reck7f2e5e32015-05-05 11:00:53 -0700266}
267
John Reckf8441e62017-10-23 13:10:41 -0700268uint32_t RenderProxy::frameTimePercentile(int percentile) {
269 return mRenderThread.queue().runSync([&]() -> auto {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100270 std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700271 return mRenderThread.globalProfileData()->findPercentile(percentile);
272 });
John Reck0e89e2b2014-10-31 14:49:06 -0700273}
274
John Reck66e06d42021-05-11 17:04:54 -0400275void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData) {
John Reckba7e9652019-01-23 10:33:41 -0800276 if (RenderThread::hasInstance()) {
277 auto& thread = RenderThread::getInstance();
John Reck66e06d42021-05-11 17:04:54 -0400278 thread.queue().runSync([&]() { thread.dumpGraphicsMemory(fd, includeProfileData); });
John Reckba7e9652019-01-23 10:33:41 -0800279 }
John Reckedc524c2015-03-18 15:24:33 -0700280}
281
John Reck39207682021-05-12 19:10:47 -0400282void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
283 if (RenderThread::hasInstance()) {
284 auto& thread = RenderThread::getInstance();
285 thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); });
286 }
287}
288
John Reckedc524c2015-03-18 15:24:33 -0700289void RenderProxy::setProcessStatsBuffer(int fd) {
John Reckdf1742e2017-01-19 15:56:21 -0800290 auto& rt = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700291 rt.queue().post([&rt, fd = dup(fd)]() {
John Reckf8441e62017-10-23 13:10:41 -0700292 rt.globalProfileData().switchStorageToAshmem(fd);
293 close(fd);
294 });
John Reckdf1742e2017-01-19 15:56:21 -0800295}
296
297void RenderProxy::rotateProcessStatsBuffer() {
John Reckdf1742e2017-01-19 15:56:21 -0800298 auto& rt = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700299 rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); });
John Reckedc524c2015-03-18 15:24:33 -0700300}
301
Tim Murray33eb07f2016-06-10 10:03:20 -0700302int RenderProxy::getRenderThreadTid() {
303 return mRenderThread.getTid();
304}
305
Skuhneea7a7fb2015-08-28 07:10:31 -0700306void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
John Reck1bcacfd2017-11-03 10:12:19 -0700307 mRenderThread.queue().post([=]() { mContext->addRenderNode(node, placeFront); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700308}
309
310void RenderProxy::removeRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700311 mRenderThread.queue().post([=]() { mContext->removeRenderNode(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700312}
313
314void RenderProxy::drawRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700315 mRenderThread.queue().runSync([=]() { mContext->prepareAndDraw(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700316}
317
Skuhneb8160872015-09-22 09:51:39 -0700318void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
John Reckf138b172017-09-08 11:00:42 -0700319 mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700320}
321
John Reck5cca8f22018-12-10 17:06:22 -0800322void RenderProxy::setPictureCapturedCallback(
323 const std::function<void(sk_sp<SkPicture>&&)>& callback) {
324 mRenderThread.queue().post(
John Reck0fa0cbc2019-04-05 16:57:46 -0700325 [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); });
John Reck5cca8f22018-12-10 17:06:22 -0800326}
327
Huihong Luo054b8d32021-02-24 18:48:12 -0800328void RenderProxy::setASurfaceTransactionCallback(
329 const std::function<void(int64_t, int64_t, int64_t)>& callback) {
330 mRenderThread.queue().post(
331 [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); });
332}
333
Huihong Luo34f42fd2021-05-03 14:47:36 -0700334void RenderProxy::setPrepareSurfaceControlForWebviewCallback(
335 const std::function<void()>& callback) {
336 mRenderThread.queue().post(
337 [this, cb = callback]() { mContext->setPrepareSurfaceControlForWebviewCallback(cb); });
338}
339
Mihai Popa95688002018-02-23 16:10:11 +0000340void RenderProxy::setFrameCallback(std::function<void(int64_t)>&& callback) {
341 mDrawFrameTask.setFrameCallback(std::move(callback));
342}
343
John Reckcc2eee82018-05-17 10:44:00 -0700344void RenderProxy::setFrameCompleteCallback(std::function<void(int64_t)>&& callback) {
345 mDrawFrameTask.setFrameCompleteCallback(std::move(callback));
346}
347
John Reckf8441e62017-10-23 13:10:41 -0700348void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700349 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700350 mContext->addFrameMetricsObserver(observer.get());
351 });
Andres Morales06f5bc72015-12-15 15:21:31 -0800352}
353
John Reckf8441e62017-10-23 13:10:41 -0700354void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700355 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700356 mContext->removeFrameMetricsObserver(observer.get());
357 });
John Reck10dd0582016-03-31 16:36:16 -0700358}
359
John Reckbb3a3582018-09-26 11:21:08 -0700360void RenderProxy::setForceDark(bool enable) {
John Reck5cca8f22018-12-10 17:06:22 -0800361 mRenderThread.queue().post([this, enable]() { mContext->setForceDark(enable); });
John Reckbb3a3582018-09-26 11:21:08 -0700362}
363
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800364int RenderProxy::copySurfaceInto(ANativeWindow* window, int left, int top, int right, int bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700365 SkBitmap* bitmap) {
John Reckf8441e62017-10-23 13:10:41 -0700366 auto& thread = RenderThread::getInstance();
367 return static_cast<int>(thread.queue().runSync([&]() -> auto {
Alec Mouri8a82b142019-12-17 09:41:48 -0800368 return thread.readback().copySurfaceInto(window, Rect(left, top, right, bottom), bitmap);
John Reckf8441e62017-10-23 13:10:41 -0700369 }));
John Reck43871902016-08-01 14:39:24 -0700370}
371
sergeyvec4a4b12016-10-20 18:39:04 -0700372void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700373 // If we haven't spun up a hardware accelerated window yet, there's no
374 // point in precaching these bitmaps as it can't impact jank.
375 // We also don't know if we even will spin up a hardware-accelerated
376 // window or not.
377 if (!RenderThread::hasInstance()) return;
378 RenderThread* renderThread = &RenderThread::getInstance();
sergeyvec4a4b12016-10-20 18:39:04 -0700379 bitmap.ref();
John Reckf8441e62017-10-23 13:10:41 -0700380 auto task = [renderThread, &bitmap]() {
381 CanvasContext::prepareToDraw(*renderThread, &bitmap);
382 bitmap.unref();
383 };
John Reck43871902016-08-01 14:39:24 -0700384 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
385 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100386 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
John Reck43871902016-08-01 14:39:24 -0700387 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
388 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
389 // be idle
390 // TODO: Make this concept a first-class supported thing? RT could use
391 // knowledge of pending draws to better schedule this task
392 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
John Reckf8441e62017-10-23 13:10:41 -0700393 renderThread->queue().postAt(estimatedNextVsync + 8_ms, task);
John Reck43871902016-08-01 14:39:24 -0700394 } else {
John Reckf8441e62017-10-23 13:10:41 -0700395 renderThread->queue().post(task);
John Reck43871902016-08-01 14:39:24 -0700396 }
397}
398
Stan Iliev1a025a72018-09-05 16:35:11 -0400399int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700400 ATRACE_NAME("HardwareBitmap readback");
Stan Iliev6983bc42017-02-02 14:11:53 -0500401 RenderThread& thread = RenderThread::getInstance();
John Reck1072fff2018-04-12 15:20:09 -0700402 if (gettid() == thread.getTid()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700403 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
Stan Iliev1a025a72018-09-05 16:35:11 -0400404 return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap);
Stan Iliev6983bc42017-02-02 14:11:53 -0500405 } else {
John Reck5cca8f22018-12-10 17:06:22 -0800406 return thread.queue().runSync(
407 [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); });
Stan Iliev6983bc42017-02-02 14:11:53 -0500408 }
sergeyv59eecb522016-11-17 17:54:57 -0800409}
410
John Reck76005182021-06-09 22:43:05 -0400411int RenderProxy::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) {
412 RenderThread& thread = RenderThread::getInstance();
413 if (gettid() == thread.getTid()) {
414 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
415 return (int)thread.readback().copyImageInto(image, bitmap);
416 } else {
417 return thread.queue().runSync(
418 [&]() -> int { return (int)thread.readback().copyImageInto(image, bitmap); });
419 }
420}
421
John Recka8963062017-06-14 10:47:50 -0700422void RenderProxy::disableVsync() {
423 Properties::disableVsync = true;
424}
425
Stan Iliev898123b2019-02-14 14:57:44 -0500426void RenderProxy::preload() {
427 // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver.
428 auto& thread = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700429 thread.queue().post([&thread]() { thread.preload(); });
Stan Iliev898123b2019-02-14 14:57:44 -0500430}
431
John Reck4f02bf42014-01-03 18:09:17 -0800432} /* namespace renderthread */
433} /* namespace uirenderer */
434} /* namespace android */