blob: ac19a153b6fbc3b74a02cd9c8eabfc77ef838aee [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
32namespace android {
33namespace uirenderer {
34namespace renderthread {
35
John Reck1bcacfd2017-11-03 10:12:19 -070036RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode,
37 IContextFactory* contextFactory)
38 : mRenderThread(RenderThread::getInstance()), mContext(nullptr) {
John Reckf8441e62017-10-23 13:10:41 -070039 mContext = mRenderThread.queue().runSync([&]() -> CanvasContext* {
40 return CanvasContext::create(mRenderThread, translucent, rootRenderNode, contextFactory);
41 });
Skuhneea7a7fb2015-08-28 07:10:31 -070042 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080043}
44
45RenderProxy::~RenderProxy() {
46 destroyContext();
47}
48
John Reck4f02bf42014-01-03 18:09:17 -080049void RenderProxy::destroyContext() {
50 if (mContext) {
Skuhneea7a7fb2015-08-28 07:10:31 -070051 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070052 // This is also a fence as we need to be certain that there are no
53 // outstanding mDrawFrame tasks posted before it is destroyed
John Reck1bcacfd2017-11-03 10:12:19 -070054 mRenderThread.queue().runSync([this]() { delete mContext; });
John Reckf8441e62017-10-23 13:10:41 -070055 mContext = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080056 }
57}
58
John Reck1125d1f2014-10-23 11:02:19 -070059void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -070060 mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); });
John Recke4280ba2014-05-05 16:39:37 -070061}
62
63bool RenderProxy::loadSystemProperties() {
John Reckf8441e62017-10-23 13:10:41 -070064 return mRenderThread.queue().runSync([this]() -> bool {
John Reckd9d7f122018-05-03 14:40:56 -070065 bool needsRedraw = Properties::load();
John Reckf8441e62017-10-23 13:10:41 -070066 if (mContext->profiler().consumeProperties()) {
67 needsRedraw = true;
68 }
69 return needsRedraw;
70 });
John Reckb36016c2015-03-11 08:50:53 -070071}
72
73void RenderProxy::setName(const char* name) {
John Reckf8441e62017-10-23 13:10:41 -070074 // block since name/value pointers owned by caller
75 // TODO: Support move arguments
John Reck1bcacfd2017-11-03 10:12:19 -070076 mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); });
John Reck4f02bf42014-01-03 18:09:17 -080077}
78
Bo Liu027b2182021-03-18 16:50:38 -040079void RenderProxy::setHintSessionCallbacks(std::function<void(int64_t)> updateTargetWorkDuration,
80 std::function<void(int64_t)> reportActualWorkDuration) {
81 mDrawFrameTask.setHintSessionCallbacks(std::move(updateTargetWorkDuration),
82 std::move(reportActualWorkDuration));
83}
84
Alec Mouri43fe6fc2019-12-23 07:46:19 -080085void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) {
John Recke95c62d2020-08-18 12:37:43 -070086 if (window) { ANativeWindow_acquire(window); }
Alec Mouri43fe6fc2019-12-23 07:46:19 -080087 mRenderThread.queue().post([this, win = window, enableTimeout]() mutable {
88 mContext->setSurface(win, enableTimeout);
John Recke95c62d2020-08-18 12:37:43 -070089 if (win) { ANativeWindow_release(win); }
John Reckcd18c222019-11-21 14:40:53 -080090 });
John Reck4f02bf42014-01-03 18:09:17 -080091}
92
Huihong Luo5fdf7b82021-01-15 14:27:06 -080093void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) {
94 auto funcs = mRenderThread.getASurfaceControlFunctions();
95 if (surfaceControl) {
96 funcs.acquireFunc(surfaceControl);
97 }
98 mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable {
99 mContext->setSurfaceControl(control);
100 if (control) {
101 funcs.releaseFunc(control);
102 }
103 });
104}
105
John Reck8785ceb2018-10-29 16:45:58 -0700106void RenderProxy::allocateBuffers() {
107 mRenderThread.queue().post([=]() { mContext->allocateBuffers(); });
Jorim Jaggi7823ee72018-07-17 15:24:16 +0200108}
109
John Reck8785ceb2018-10-29 16:45:58 -0700110bool RenderProxy::pause() {
John Reck1bcacfd2017-11-03 10:12:19 -0700111 return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); });
John Reck8afcc762016-04-13 10:24:06 -0700112}
113
114void RenderProxy::setStopped(bool stopped) {
John Reck1bcacfd2017-11-03 10:12:19 -0700115 mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); });
John Reck8afcc762016-04-13 10:24:06 -0700116}
117
John Reck8785ceb2018-10-29 16:45:58 -0700118void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck1bcacfd2017-11-03 10:12:19 -0700119 mRenderThread.queue().post(
John Reck8785ceb2018-10-29 16:45:58 -0700120 [=]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); });
Alan Viverette50210d92015-05-14 18:05:36 -0700121}
122
John Reck8785ceb2018-10-29 16:45:58 -0700123void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
124 mRenderThread.queue().post([=]() { mContext->setLightGeometry(lightCenter, lightRadius); });
John Reck63a06672014-05-07 13:45:54 -0700125}
126
127void RenderProxy::setOpaque(bool opaque) {
John Reck1bcacfd2017-11-03 10:12:19 -0700128 mRenderThread.queue().post([=]() { mContext->setOpaque(opaque); });
Romain Guy26a2b972017-04-17 09:39:51 -0700129}
130
John Reckb36bfdd2020-07-23 13:47:49 -0700131void RenderProxy::setColorMode(ColorMode mode) {
132 mRenderThread.queue().post([=]() { mContext->setColorMode(mode); });
Romain Guy26a2b972017-04-17 09:39:51 -0700133}
134
John Reckba6adf62015-02-19 14:36:50 -0800135int64_t* RenderProxy::frameInfo() {
136 return mDrawFrameTask.frameInfo();
137}
138
John Reck2de950d2017-01-25 10:58:30 -0800139int RenderProxy::syncAndDrawFrame() {
140 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800141}
142
John Reck2de950d2017-01-25 10:58:30 -0800143void RenderProxy::destroy() {
John Reckfae904d2014-04-14 11:01:57 -0700144 // destroyCanvasAndSurface() needs a fence as when it returns the
145 // underlying BufferQueue is going to be released from under
146 // the render thread.
John Reck1bcacfd2017-11-03 10:12:19 -0700147 mRenderThread.queue().runSync([=]() { mContext->destroy(); });
John Reck0d1f6342014-03-28 20:30:27 -0700148}
149
John Reck283bb462018-12-13 16:40:14 -0800150void RenderProxy::destroyFunctor(int functor) {
151 ATRACE_CALL();
152 RenderThread& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800153 thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); });
John Reck283bb462018-12-13 16:40:14 -0800154}
155
John Reck19b6bcf2014-02-14 20:03:38 -0800156DeferredLayerUpdater* RenderProxy::createTextureLayer() {
John Reckf8441e62017-10-23 13:10:41 -0700157 return mRenderThread.queue().runSync([this]() -> auto {
158 return mContext->createTextureLayer();
159 });
John Reck3e824952014-08-20 10:08:39 -0700160}
161
John Reck2de950d2017-01-25 10:58:30 -0800162void RenderProxy::buildLayer(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700163 mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); });
John Reck19b6bcf2014-02-14 20:03:38 -0800164}
165
John Reck3731dc22015-04-13 15:20:29 -0700166bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700167 ATRACE_NAME("TextureView#getBitmap");
Stan Iliev1a025a72018-09-05 16:35:11 -0400168 auto& thread = RenderThread::getInstance();
John Reck5cca8f22018-12-10 17:06:22 -0800169 return thread.queue().runSync([&]() -> bool {
170 return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success;
171 });
John Reck19b6bcf2014-02-14 20:03:38 -0800172}
173
John Reckd72e0a32014-05-29 18:56:11 -0700174void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
175 mDrawFrameTask.pushLayerUpdate(layer);
176}
177
178void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
179 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800180}
181
John Reck918ad522014-06-27 14:45:25 -0700182void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700183 return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); });
John Recke1628b72014-05-23 15:11:19 -0700184}
185
John Reck2de950d2017-01-25 10:58:30 -0800186void RenderProxy::destroyHardwareResources() {
John Reck1bcacfd2017-11-03 10:12:19 -0700187 return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); });
John Reckf47a5942014-06-30 16:20:04 -0700188}
189
190void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700191 // Avoid creating a RenderThread to do a trimMemory.
192 if (RenderThread::hasInstance()) {
193 RenderThread& thread = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700194 thread.queue().post([&thread, level]() { CanvasContext::trimMemory(thread, level); });
John Reckcd3a22c2014-08-06 13:33:59 -0700195 }
John Reckf47a5942014-06-30 16:20:04 -0700196}
197
John Reck39207682021-05-12 19:10:47 -0400198void RenderProxy::purgeCaches() {
199 if (RenderThread::hasInstance()) {
200 RenderThread& thread = RenderThread::getInstance();
201 thread.queue().post([&thread]() {
202 if (thread.getGrContext()) {
203 thread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
204 }
205 });
206 }
207}
208
Chris Craik2507c342015-05-04 14:36:49 -0700209void RenderProxy::overrideProperty(const char* name, const char* value) {
John Reckf8441e62017-10-23 13:10:41 -0700210 // expensive, but block here since name/value pointers owned by caller
John Reck1bcacfd2017-11-03 10:12:19 -0700211 RenderThread::getInstance().queue().runSync(
212 [&]() { Properties::overrideProperty(name, value); });
Chris Craik2507c342015-05-04 14:36:49 -0700213}
214
John Reck28ad7b52014-04-07 16:59:25 -0700215void RenderProxy::fence() {
John Reck1bcacfd2017-11-03 10:12:19 -0700216 mRenderThread.queue().runSync([]() {});
John Reck28ad7b52014-04-07 16:59:25 -0700217}
218
John Recke4c1e6c2018-05-24 16:27:35 -0700219int RenderProxy::maxTextureSize() {
John Reck5cca8f22018-12-10 17:06:22 -0800220 static int maxTextureSize = RenderThread::getInstance().queue().runSync(
221 []() { return DeviceInfo::get()->maxTextureSize(); });
John Recke4c1e6c2018-05-24 16:27:35 -0700222 return maxTextureSize;
John Reckf47a5942014-06-30 16:20:04 -0700223}
224
225void RenderProxy::stopDrawing() {
John Reck1bcacfd2017-11-03 10:12:19 -0700226 mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); });
John Recka5dda642014-05-22 15:43:54 -0700227}
228
229void RenderProxy::notifyFramePending() {
John Reck1bcacfd2017-11-03 10:12:19 -0700230 mRenderThread.queue().post([this]() { mContext->notifyFramePending(); });
John Reckfe5e7b72014-05-23 17:42:28 -0700231}
232
John Reckba6adf62015-02-19 14:36:50 -0800233void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckf8441e62017-10-23 13:10:41 -0700234 mRenderThread.queue().runSync([&]() {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100235 std::lock_guard lock(mRenderThread.getJankDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700236 mContext->profiler().dumpData(fd);
237 if (dumpFlags & DumpFlags::FrameStats) {
238 mContext->dumpFrames(fd);
239 }
240 if (dumpFlags & DumpFlags::JankStats) {
241 mRenderThread.globalProfileData()->dump(fd);
242 }
243 if (dumpFlags & DumpFlags::Reset) {
244 mContext->resetFrameStats();
245 }
246 });
John Reck7f2e5e32015-05-05 11:00:53 -0700247}
248
249void RenderProxy::resetProfileInfo() {
Jorim Jaggi33adb572021-02-22 14:27:53 +0100250 mRenderThread.queue().runSync([=]() {
251 std::lock_guard lock(mRenderThread.getJankDataMutex());
252 mContext->resetFrameStats();
253 });
John Reck7f2e5e32015-05-05 11:00:53 -0700254}
255
John Reckf8441e62017-10-23 13:10:41 -0700256uint32_t RenderProxy::frameTimePercentile(int percentile) {
257 return mRenderThread.queue().runSync([&]() -> auto {
Jorim Jaggi71db8892021-02-03 23:19:29 +0100258 std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex());
John Reckf8441e62017-10-23 13:10:41 -0700259 return mRenderThread.globalProfileData()->findPercentile(percentile);
260 });
John Reck0e89e2b2014-10-31 14:49:06 -0700261}
262
John Reck66e06d42021-05-11 17:04:54 -0400263void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData) {
John Reckba7e9652019-01-23 10:33:41 -0800264 if (RenderThread::hasInstance()) {
265 auto& thread = RenderThread::getInstance();
John Reck66e06d42021-05-11 17:04:54 -0400266 thread.queue().runSync([&]() { thread.dumpGraphicsMemory(fd, includeProfileData); });
John Reckba7e9652019-01-23 10:33:41 -0800267 }
John Reckedc524c2015-03-18 15:24:33 -0700268}
269
John Reck39207682021-05-12 19:10:47 -0400270void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) {
271 if (RenderThread::hasInstance()) {
272 auto& thread = RenderThread::getInstance();
273 thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); });
274 }
275}
276
John Reckedc524c2015-03-18 15:24:33 -0700277void RenderProxy::setProcessStatsBuffer(int fd) {
John Reckdf1742e2017-01-19 15:56:21 -0800278 auto& rt = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700279 rt.queue().post([&rt, fd = dup(fd)]() {
John Reckf8441e62017-10-23 13:10:41 -0700280 rt.globalProfileData().switchStorageToAshmem(fd);
281 close(fd);
282 });
John Reckdf1742e2017-01-19 15:56:21 -0800283}
284
285void RenderProxy::rotateProcessStatsBuffer() {
John Reckdf1742e2017-01-19 15:56:21 -0800286 auto& rt = RenderThread::getInstance();
John Reck1bcacfd2017-11-03 10:12:19 -0700287 rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); });
John Reckedc524c2015-03-18 15:24:33 -0700288}
289
Tim Murray33eb07f2016-06-10 10:03:20 -0700290int RenderProxy::getRenderThreadTid() {
291 return mRenderThread.getTid();
292}
293
Skuhneea7a7fb2015-08-28 07:10:31 -0700294void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
John Reck1bcacfd2017-11-03 10:12:19 -0700295 mRenderThread.queue().post([=]() { mContext->addRenderNode(node, placeFront); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700296}
297
298void RenderProxy::removeRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700299 mRenderThread.queue().post([=]() { mContext->removeRenderNode(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700300}
301
302void RenderProxy::drawRenderNode(RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -0700303 mRenderThread.queue().runSync([=]() { mContext->prepareAndDraw(node); });
Skuhneea7a7fb2015-08-28 07:10:31 -0700304}
305
Skuhneb8160872015-09-22 09:51:39 -0700306void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
John Reckf138b172017-09-08 11:00:42 -0700307 mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700308}
309
John Reck5cca8f22018-12-10 17:06:22 -0800310void RenderProxy::setPictureCapturedCallback(
311 const std::function<void(sk_sp<SkPicture>&&)>& callback) {
312 mRenderThread.queue().post(
John Reck0fa0cbc2019-04-05 16:57:46 -0700313 [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); });
John Reck5cca8f22018-12-10 17:06:22 -0800314}
315
Huihong Luo054b8d32021-02-24 18:48:12 -0800316void RenderProxy::setASurfaceTransactionCallback(
317 const std::function<void(int64_t, int64_t, int64_t)>& callback) {
318 mRenderThread.queue().post(
319 [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); });
320}
321
Mihai Popa95688002018-02-23 16:10:11 +0000322void RenderProxy::setFrameCallback(std::function<void(int64_t)>&& callback) {
323 mDrawFrameTask.setFrameCallback(std::move(callback));
324}
325
John Reckcc2eee82018-05-17 10:44:00 -0700326void RenderProxy::setFrameCompleteCallback(std::function<void(int64_t)>&& callback) {
327 mDrawFrameTask.setFrameCompleteCallback(std::move(callback));
328}
329
John Reckf8441e62017-10-23 13:10:41 -0700330void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700331 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700332 mContext->addFrameMetricsObserver(observer.get());
333 });
Andres Morales06f5bc72015-12-15 15:21:31 -0800334}
335
John Reckf8441e62017-10-23 13:10:41 -0700336void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700337 mRenderThread.queue().post([this, observer = sp{observerPtr}]() {
John Reckf8441e62017-10-23 13:10:41 -0700338 mContext->removeFrameMetricsObserver(observer.get());
339 });
John Reck10dd0582016-03-31 16:36:16 -0700340}
341
John Reckbb3a3582018-09-26 11:21:08 -0700342void RenderProxy::setForceDark(bool enable) {
John Reck5cca8f22018-12-10 17:06:22 -0800343 mRenderThread.queue().post([this, enable]() { mContext->setForceDark(enable); });
John Reckbb3a3582018-09-26 11:21:08 -0700344}
345
Alec Mouri43fe6fc2019-12-23 07:46:19 -0800346int RenderProxy::copySurfaceInto(ANativeWindow* window, int left, int top, int right, int bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700347 SkBitmap* bitmap) {
John Reckf8441e62017-10-23 13:10:41 -0700348 auto& thread = RenderThread::getInstance();
349 return static_cast<int>(thread.queue().runSync([&]() -> auto {
Alec Mouri8a82b142019-12-17 09:41:48 -0800350 return thread.readback().copySurfaceInto(window, Rect(left, top, right, bottom), bitmap);
John Reckf8441e62017-10-23 13:10:41 -0700351 }));
John Reck43871902016-08-01 14:39:24 -0700352}
353
sergeyvec4a4b12016-10-20 18:39:04 -0700354void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700355 // If we haven't spun up a hardware accelerated window yet, there's no
356 // point in precaching these bitmaps as it can't impact jank.
357 // We also don't know if we even will spin up a hardware-accelerated
358 // window or not.
359 if (!RenderThread::hasInstance()) return;
360 RenderThread* renderThread = &RenderThread::getInstance();
sergeyvec4a4b12016-10-20 18:39:04 -0700361 bitmap.ref();
John Reckf8441e62017-10-23 13:10:41 -0700362 auto task = [renderThread, &bitmap]() {
363 CanvasContext::prepareToDraw(*renderThread, &bitmap);
364 bitmap.unref();
365 };
John Reck43871902016-08-01 14:39:24 -0700366 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
367 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
Jerome Gaillarde218c692019-06-14 12:58:57 +0100368 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC);
John Reck43871902016-08-01 14:39:24 -0700369 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
370 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
371 // be idle
372 // TODO: Make this concept a first-class supported thing? RT could use
373 // knowledge of pending draws to better schedule this task
374 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
John Reckf8441e62017-10-23 13:10:41 -0700375 renderThread->queue().postAt(estimatedNextVsync + 8_ms, task);
John Reck43871902016-08-01 14:39:24 -0700376 } else {
John Reckf8441e62017-10-23 13:10:41 -0700377 renderThread->queue().post(task);
John Reck43871902016-08-01 14:39:24 -0700378 }
379}
380
Stan Iliev1a025a72018-09-05 16:35:11 -0400381int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) {
John Reckfbeac3c2019-03-29 11:24:56 -0700382 ATRACE_NAME("HardwareBitmap readback");
Stan Iliev6983bc42017-02-02 14:11:53 -0500383 RenderThread& thread = RenderThread::getInstance();
John Reck1072fff2018-04-12 15:20:09 -0700384 if (gettid() == thread.getTid()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700385 // TODO: fix everything that hits this. We should never be triggering a readback ourselves.
Stan Iliev1a025a72018-09-05 16:35:11 -0400386 return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap);
Stan Iliev6983bc42017-02-02 14:11:53 -0500387 } else {
John Reck5cca8f22018-12-10 17:06:22 -0800388 return thread.queue().runSync(
389 [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); });
Stan Iliev6983bc42017-02-02 14:11:53 -0500390 }
sergeyv59eecb522016-11-17 17:54:57 -0800391}
392
John Recka8963062017-06-14 10:47:50 -0700393void RenderProxy::disableVsync() {
394 Properties::disableVsync = true;
395}
396
Stan Iliev898123b2019-02-14 14:57:44 -0500397void RenderProxy::preload() {
398 // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver.
399 auto& thread = RenderThread::getInstance();
John Reck0fa0cbc2019-04-05 16:57:46 -0700400 thread.queue().post([&thread]() { thread.preload(); });
Stan Iliev898123b2019-02-14 14:57:44 -0500401}
402
John Reck4f02bf42014-01-03 18:09:17 -0800403} /* namespace renderthread */
404} /* namespace uirenderer */
405} /* namespace android */