blob: c485ce2781e5a5026f3eed37565a5817fdf66880 [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
Bo Liu6a3fc602021-07-17 16:42:13 -040032#include <pthread.h>
33
John Reck4f02bf42014-01-03 18:09:17 -080034namespace android {
35namespace uirenderer {
36namespace renderthread {
37
John Reck1bcacfd2017-11-03 10:12:19 -070038RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode,
39 IContextFactory* contextFactory)
40 : mRenderThread(RenderThread::getInstance()), mContext(nullptr) {
John Reckf8441e62017-10-23 13:10:41 -070041 mContext = mRenderThread.queue().runSync([&]() -> CanvasContext* {
42 return CanvasContext::create(mRenderThread, translucent, rootRenderNode, contextFactory);
43 });
Bo Liu6a3fc602021-07-17 16:42:13 -040044 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode,
45 pthread_gettid_np(pthread_self()), getRenderThreadTid());
John Reck4f02bf42014-01-03 18:09:17 -080046}
47
48RenderProxy::~RenderProxy() {
49 destroyContext();
50}
51
John Reck4f02bf42014-01-03 18:09:17 -080052void RenderProxy::destroyContext() {
53 if (mContext) {
Bo Liu6a3fc602021-07-17 16:42:13 -040054 mDrawFrameTask.setContext(nullptr, nullptr, nullptr, -1, -1);
John Reck668f0e32014-03-26 15:10:40 -070055 // This is also a fence as we need to be certain that there are no
56 // outstanding mDrawFrame tasks posted before it is destroyed
John Reck1bcacfd2017-11-03 10:12:19 -070057 mRenderThread.queue().runSync([this]() { delete mContext; });
John Reckf8441e62017-10-23 13:10:41 -070058 mContext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080059 }
60}
61
John Reck1125d1f2014-10-23 11:02:19 -070062void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -070063 mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); });
John Recke4280ba2014-05-05 16:39:37 -070064}
65
66bool RenderProxy::loadSystemProperties() {
John Reckf8441e62017-10-23 13:10:41 -070067 return mRenderThread.queue().runSync([this]() -> bool {
John Reckd9d7f122018-05-03 14:40:56 -070068 bool needsRedraw = Properties::load();
John Reckf8441e62017-10-23 13:10:41 -070069 if (mContext->profiler().consumeProperties()) {
70 needsRedraw = true;
71 }
72 return needsRedraw;
73 });
John Reckb36016c2015-03-11 08:50:53 -070074}
75
76void RenderProxy::setName(const char* name) {
John Reckf8441e62017-10-23 13:10:41 -070077 // block since name/value pointers owned by caller
78 // TODO: Support move arguments
John Reck1bcacfd2017-11-03 10:12:19 -070079 mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); });
John Reck4f02bf42014-01-03 18:09:17 -080080}
81
Alec Mouri43fe6fc2019-12-23 07:46:19 -080082void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) {
John Recke95c62d2020-08-18 12:37:43 -070083 if (window) { ANativeWindow_acquire(window); }
Alec Mouri43fe6fc2019-12-23 07:46:19 -080084 mRenderThread.queue().post([this, win = window, enableTimeout]() mutable {
85 mContext->setSurface(win, enableTimeout);
John Recke95c62d2020-08-18 12:37:43 -070086 if (win) { ANativeWindow_release(win); }
John Reckcd18c222019-11-21 14:40:53 -080087 });
John Reck4f02bf42014-01-03 18:09:17 -080088}
89
Huihong Luo5fdf7b82021-01-15 14:27:06 -080090void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) {
91 auto funcs = mRenderThread.getASurfaceControlFunctions();
92 if (surfaceControl) {
93 funcs.acquireFunc(surfaceControl);
94 }
95 mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable {
96 mContext->setSurfaceControl(control);
97 if (control) {
98 funcs.releaseFunc(control);
99 }
100 });
101}
102
John Reck8785ceb2018-10-29 16:45:58 -0700103void RenderProxy::allocateBuffers() {
104 mRenderThread.queue().post([=]() { mContext->allocateBuffers(); });
Jorim Jaggi7823ee72018-07-17 15:24:16 +0200105}
106
John Reck8785ceb2018-10-29 16:45:58 -0700107bool RenderProxy::pause() {
John Reck1bcacfd2017-11-03 10:12:19 -0700108 return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); });
John Reck8afcc762016-04-13 10:24:06 -0700109}
110
111void RenderProxy::setStopped(bool stopped) {
John Reck1bcacfd2017-11-03 10:12:19 -0700112 mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); });
John Reck8afcc762016-04-13 10:24:06 -0700113}
114
John Reck8785ceb2018-10-29 16:45:58 -0700115void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck1bcacfd2017-11-03 10:12:19 -0700116 mRenderThread.queue().post(
John Reck8785ceb2018-10-29 16:45:58 -0700117 [=]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); });
Alan Viverette50210d92015-05-14 18:05:36 -0700118}
119
John Reck8785ceb2018-10-29 16:45:58 -0700120void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
121 mRenderThread.queue().post([=]() { mContext->setLightGeometry(lightCenter, lightRadius); });
John Reck63a06672014-05-07 13:45:54 -0700122}
123
124void RenderProxy::setOpaque(bool opaque) {
John Reck1bcacfd2017-11-03 10:12:19 -0700125 mRenderThread.queue().post([=]() { mContext->setOpaque(opaque); });
Romain Guy26a2b972017-04-17 09:39:51 -0700126}
127
John Reckb36bfdd2020-07-23 13:47:49 -0700128void RenderProxy::setColorMode(ColorMode mode) {
129 mRenderThread.queue().post([=]() { mContext->setColorMode(mode); });
Romain Guy26a2b972017-04-17 09:39:51 -0700130}
131
John Reckba6adf62015-02-19 14:36:50 -0800132int64_t* RenderProxy::frameInfo() {
133 return mDrawFrameTask.frameInfo();
134}
135
John Reck2de950d2017-01-25 10:58:30 -0800136int RenderProxy::syncAndDrawFrame() {
137 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800138}
139
John Reck2de950d2017-01-25 10:58:30 -0800140void RenderProxy::destroy() {
John Reckfae904d2014-04-14 11:01:57 -0700141 // destroyCanvasAndSurface() needs a fence as when it returns the
142 // underlying BufferQueue is going to be released from under
143 // the render thread.
John Reck1bcacfd2017-11-03 10:12:19 -0700144 mRenderThread.queue().runSync([=]() { mContext->destroy(); });
John Reck0d1f6342014-03-28 20:30:27 -0700145}
146
John Reck283bb462018-12-13 16:40:14 -0800147void RenderProxy::destroyFunctor(int functor) {
148 ATRACE_CALL();
149 RenderThread& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800150 thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); });
John Reck283bb462018-12-13 16:40:14 -0800151}
152
John Reck19b6bcf2014-02-14 20:03:38 -0800153DeferredLayerUpdater* RenderProxy::createTextureLayer() {
John Reckf8441e62017-10-23 13:10:41 -0700154 return mRenderThread.queue().runSync([this]() -> auto {
155 return mContext->createTextureLayer();
156 });
John Reck3e824952014-08-20 10:08:39 -0700157}
158
John Reck2de950d2017-01-25 10:58:30 -0800159void RenderProxy::buildLayer(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700160 mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); });
John Reck19b6bcf2014-02-14 20:03:38 -0800161}
162
John Reck3731dc22015-04-13 15:20:29 -0700163bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700164 ATRACE_NAME("TextureView#getBitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400165 auto& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800166 return thread.queue().runSync([&]() -> bool {
167 return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success;
168 });
John Reck19b6bcf2014-02-14 20:03:38 -0800169}
170
John Reckd72e0a32014-05-29 18:56:11 -0700171void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
172 mDrawFrameTask.pushLayerUpdate(layer);
173}
174
175void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
176 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800177}
178
John Reck918ad522014-06-27 14:45:25 -0700179void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700180 return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); });
John Recke1628b72014-05-23 15:11:19 -0700181}
182
John Reck2de950d2017-01-25 10:58:30 -0800183void RenderProxy::destroyHardwareResources() {
John Reck1bcacfd2017-11-03 10:12:19 -0700184 return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); });
John Reckf47a5942014-06-30 16:20:04 -0700185}
186
187void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700188 // Avoid creating a RenderThread to do a trimMemory.
189 if (RenderThread::hasInstance()) {
190 RenderThread& thread = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700191 thread.queue().post([&thread, level]() { CanvasContext::trimMemory(thread, level); });
John Reckcd3a22c2014-08-06 13:33:59 -0700192 }
John Reckf47a5942014-06-30 16:20:04 -0700193}
194
John Reck39207682021-05-12 19:10:47 -0400195void RenderProxy::purgeCaches() {
196 if (RenderThread::hasInstance()) {
197 RenderThread& thread = RenderThread::getInstance();
198 thread.queue().post([&thread]() {
199 if (thread.getGrContext()) {
200 thread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
201 }
202 });
203 }
204}
205
Chris Craik2507c342015-05-04 14:36:49 -0700206void RenderProxy::overrideProperty(const char* name, const char* value) {
John Reckf8441e62017-10-23 13:10:41 -0700207 // expensive, but block here since name/value pointers owned by caller
John Reck1bcacfd2017-11-03 10:12:19 -0700208 RenderThread::getInstance().queue().runSync(
209 [&]() { Properties::overrideProperty(name, value); });
Chris Craik2507c342015-05-04 14:36:49 -0700210}
211
John Reck28ad7b52014-04-07 16:59:25 -0700212void RenderProxy::fence() {
John Reck1bcacfd2017-11-03 10:12:19 -0700213 mRenderThread.queue().runSync([]() {});
John Reck28ad7b52014-04-07 16:59:25 -0700214}
215
John Recke4c1e6c2018-05-24 16:27:35 -0700216int RenderProxy::maxTextureSize() {
John Reck5cca8f22018-12-10 17:06:22 -0800217 static int maxTextureSize = RenderThread::getInstance().queue().runSync(
218 []() { return DeviceInfo::get()->maxTextureSize(); });
John Recke4c1e6c2018-05-24 16:27:35 -0700219 return maxTextureSize;
John Reckf47a5942014-06-30 16:20:04 -0700220}
221
222void RenderProxy::stopDrawing() {
John Reck1bcacfd2017-11-03 10:12:19 -0700223 mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); });
John Recka5dda642014-05-22 15:43:54 -0700224}
225
226void RenderProxy::notifyFramePending() {
John Reck1bcacfd2017-11-03 10:12:19 -0700227 mRenderThread.queue().post([this]() { mContext->notifyFramePending(); });
John Reckfe5e7b72014-05-23 17:42:28 -0700228}
229
John Reckba6adf62015-02-19 14:36:50 -0800230void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckf8441e62017-10-23 13:10:41 -0700231 mRenderThread.queue().runSync([&]() {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100232 std::lock_guard lock(mRenderThread.getJankDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700233 mContext->profiler().dumpData(fd);
234 if (dumpFlags & DumpFlags::FrameStats) {
235 mContext->dumpFrames(fd);
236 }
237 if (dumpFlags & DumpFlags::JankStats) {
238 mRenderThread.globalProfileData()->dump(fd);
239 }
240 if (dumpFlags & DumpFlags::Reset) {
241 mContext->resetFrameStats();
242 }
243 });
John Reck7f2e5e32015-05-05 11:00:53 -0700244}
245
246void RenderProxy::resetProfileInfo() {
Jorim Jaggi33adb572021-02-22 14:27:53 +0100247 mRenderThread.queue().runSync([=]() {
248 std::lock_guard lock(mRenderThread.getJankDataMutex());
249 mContext->resetFrameStats();
250 });
John Reck7f2e5e32015-05-05 11:00:53 -0700251}
252
John Reckf8441e62017-10-23 13:10:41 -0700253uint32_t RenderProxy::frameTimePercentile(int percentile) {
254 return mRenderThread.queue().runSync([&]() -> auto {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100255 std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700256 return mRenderThread.globalProfileData()->findPercentile(percentile);
257 });
John Reck0e89e2b2014-10-31 14:49:06 -0700258}
259
John Reck66e06d42021-05-11 17:04:54 -0400260void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData) {
John Reckba7e9652019-01-23 10:33:41 -0800261 if (RenderThread::hasInstance()) {
262 auto& thread = RenderThread::getInstance();
John Reck66e06d42021-05-11 17:04:54 -0400263 thread.queue().runSync([&]() { thread.dumpGraphicsMemory(fd, includeProfileData); });
John Reckba7e9652019-01-23 10:33:41 -0800264 }
John Reckedc524c2015-03-18 15:24:33 -0700265}
266
John Reck39207682021-05-12 19:10:47 -0400267void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
268 if (RenderThread::hasInstance()) {
269 auto& thread = RenderThread::getInstance();
270 thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); });
271 }
272}
273
John Reckedc524c2015-03-18 15:24:33 -0700274void RenderProxy::setProcessStatsBuffer(int fd) {
John Reckdf1742e2017-01-19 15:56:21 -0800275 auto& rt = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700276 rt.queue().post([&rt, fd = dup(fd)]() {
John Reckf8441e62017-10-23 13:10:41 -0700277 rt.globalProfileData().switchStorageToAshmem(fd);
278 close(fd);
279 });
John Reckdf1742e2017-01-19 15:56:21 -0800280}
281
282void RenderProxy::rotateProcessStatsBuffer() {
John Reckdf1742e2017-01-19 15:56:21 -0800283 auto& rt = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700284 rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); });
John Reckedc524c2015-03-18 15:24:33 -0700285}
286
Tim Murray33eb07f2016-06-10 10:03:20 -0700287int RenderProxy::getRenderThreadTid() {
288 return mRenderThread.getTid();
289}
290
Skuhneea7a7fb2015-08-28 07:10:31 -0700291void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
John Reck1bcacfd2017-11-03 10:12:19 -0700292 mRenderThread.queue().post([=]() { mContext->addRenderNode(node, placeFront); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700293}
294
295void RenderProxy::removeRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700296 mRenderThread.queue().post([=]() { mContext->removeRenderNode(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700297}
298
299void RenderProxy::drawRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700300 mRenderThread.queue().runSync([=]() { mContext->prepareAndDraw(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700301}
302
Skuhneb8160872015-09-22 09:51:39 -0700303void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
John Reckf138b172017-09-08 11:00:42 -0700304 mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700305}
306
John Reck5cca8f22018-12-10 17:06:22 -0800307void RenderProxy::setPictureCapturedCallback(
308 const std::function<void(sk_sp<SkPicture>&&)>& callback) {
309 mRenderThread.queue().post(
John Reck0fa0cbc2019-04-05 16:57:46 -0700310 [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); });
John Reck5cca8f22018-12-10 17:06:22 -0800311}
312
Huihong Luo054b8d32021-02-24 18:48:12 -0800313void RenderProxy::setASurfaceTransactionCallback(
Huihong Luo4df41512021-06-24 10:04:32 -0700314 const std::function<bool(int64_t, int64_t, int64_t)>& callback) {
Huihong Luo054b8d32021-02-24 18:48:12 -0800315 mRenderThread.queue().post(
316 [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); });
317}
318
Huihong Luo34f42fd2021-05-03 14:47:36 -0700319void RenderProxy::setPrepareSurfaceControlForWebviewCallback(
320 const std::function<void()>& callback) {
321 mRenderThread.queue().post(
322 [this, cb = callback]() { mContext->setPrepareSurfaceControlForWebviewCallback(cb); });
323}
324
Mihai Popa95688002018-02-23 16:10:11 +0000325void RenderProxy::setFrameCallback(std::function<void(int64_t)>&& callback) {
326 mDrawFrameTask.setFrameCallback(std::move(callback));
327}
328
John Reckcc2eee82018-05-17 10:44:00 -0700329void RenderProxy::setFrameCompleteCallback(std::function<void(int64_t)>&& callback) {
330 mDrawFrameTask.setFrameCompleteCallback(std::move(callback));
331}
332
John Reckf8441e62017-10-23 13:10:41 -0700333void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700334 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700335 mContext->addFrameMetricsObserver(observer.get());
336 });
Andres Morales06f5bc72015-12-15 15:21:31 -0800337}
338
John Reckf8441e62017-10-23 13:10:41 -0700339void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700340 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700341 mContext->removeFrameMetricsObserver(observer.get());
342 });
John Reck10dd0582016-03-31 16:36:16 -0700343}
344
John Reckbb3a3582018-09-26 11:21:08 -0700345void RenderProxy::setForceDark(bool enable) {
John Reck5cca8f22018-12-10 17:06:22 -0800346 mRenderThread.queue().post([this, enable]() { mContext->setForceDark(enable); });
John Reckbb3a3582018-09-26 11:21:08 -0700347}
348
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800349int RenderProxy::copySurfaceInto(ANativeWindow* window, int left, int top, int right, int bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700350 SkBitmap* bitmap) {
John Reckf8441e62017-10-23 13:10:41 -0700351 auto& thread = RenderThread::getInstance();
352 return static_cast<int>(thread.queue().runSync([&]() -> auto {
Alec Mouri8a82b142019-12-17 09:41:48 -0800353 return thread.readback().copySurfaceInto(window, Rect(left, top, right, bottom), bitmap);
John Reckf8441e62017-10-23 13:10:41 -0700354 }));
John Reck43871902016-08-01 14:39:24 -0700355}
356
sergeyvec4a4b12016-10-20 18:39:04 -0700357void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700358 // If we haven't spun up a hardware accelerated window yet, there's no
359 // point in precaching these bitmaps as it can't impact jank.
360 // We also don't know if we even will spin up a hardware-accelerated
361 // window or not.
362 if (!RenderThread::hasInstance()) return;
363 RenderThread* renderThread = &RenderThread::getInstance();
sergeyvec4a4b12016-10-20 18:39:04 -0700364 bitmap.ref();
John Reckf8441e62017-10-23 13:10:41 -0700365 auto task = [renderThread, &bitmap]() {
366 CanvasContext::prepareToDraw(*renderThread, &bitmap);
367 bitmap.unref();
368 };
John Reck43871902016-08-01 14:39:24 -0700369 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
370 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100371 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
John Reck43871902016-08-01 14:39:24 -0700372 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
373 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
374 // be idle
375 // TODO: Make this concept a first-class supported thing? RT could use
376 // knowledge of pending draws to better schedule this task
377 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
John Reckf8441e62017-10-23 13:10:41 -0700378 renderThread->queue().postAt(estimatedNextVsync + 8_ms, task);
John Reck43871902016-08-01 14:39:24 -0700379 } else {
John Reckf8441e62017-10-23 13:10:41 -0700380 renderThread->queue().post(task);
John Reck43871902016-08-01 14:39:24 -0700381 }
382}
383
Stan Iliev1a025a72018-09-05 16:35:11 -0400384int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700385 ATRACE_NAME("HardwareBitmap readback");
Stan Iliev6983bc42017-02-02 14:11:53 -0500386 RenderThread& thread = RenderThread::getInstance();
John Reck1072fff2018-04-12 15:20:09 -0700387 if (gettid() == thread.getTid()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700388 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
Stan Iliev1a025a72018-09-05 16:35:11 -0400389 return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap);
Stan Iliev6983bc42017-02-02 14:11:53 -0500390 } else {
John Reck5cca8f22018-12-10 17:06:22 -0800391 return thread.queue().runSync(
392 [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); });
Stan Iliev6983bc42017-02-02 14:11:53 -0500393 }
sergeyv59eecb522016-11-17 17:54:57 -0800394}
395
John Reck76005182021-06-09 22:43:05 -0400396int RenderProxy::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) {
397 RenderThread& thread = RenderThread::getInstance();
398 if (gettid() == thread.getTid()) {
399 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
400 return (int)thread.readback().copyImageInto(image, bitmap);
401 } else {
402 return thread.queue().runSync(
403 [&]() -> int { return (int)thread.readback().copyImageInto(image, bitmap); });
404 }
405}
406
John Recka8963062017-06-14 10:47:50 -0700407void RenderProxy::disableVsync() {
408 Properties::disableVsync = true;
409}
410
Stan Iliev898123b2019-02-14 14:57:44 -0500411void RenderProxy::preload() {
412 // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver.
413 auto& thread = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700414 thread.queue().post([&thread]() { thread.preload(); });
Stan Iliev898123b2019-02-14 14:57:44 -0500415}
416
John Reck4f02bf42014-01-03 18:09:17 -0800417} /* namespace renderthread */
418} /* namespace uirenderer */
419} /* namespace android */