| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 17 | #include "RenderProxy.h" | 
|  | 18 |  | 
| John Reck | 2c9a2db | 2023-07-21 16:35:41 -0400 | [diff] [blame] | 19 | #include <SkBitmap.h> | 
|  | 20 | #include <SkImage.h> | 
|  | 21 | #include <SkPicture.h> | 
| rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 22 | #include <gui/TraceUtils.h> | 
| John Reck | 2c9a2db | 2023-07-21 16:35:41 -0400 | [diff] [blame] | 23 | #include <pthread.h> | 
|  | 24 | #include <ui/GraphicBufferAllocator.h> | 
|  | 25 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 26 | #include "DeferredLayerUpdater.h" | 
|  | 27 | #include "DisplayList.h" | 
| John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 28 | #include "Properties.h" | 
| John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 29 | #include "Readback.h" | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 30 | #include "Rect.h" | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 31 | #include "WebViewFunctorManager.h" | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 32 | #include "renderthread/CanvasContext.h" | 
|  | 33 | #include "renderthread/RenderTask.h" | 
|  | 34 | #include "renderthread/RenderThread.h" | 
|  | 35 | #include "utils/Macros.h" | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 36 | #include "utils/TimeUtils.h" | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | namespace android { | 
|  | 39 | namespace uirenderer { | 
|  | 40 | namespace renderthread { | 
|  | 41 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 42 | RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, | 
|  | 43 | IContextFactory* contextFactory) | 
|  | 44 | : mRenderThread(RenderThread::getInstance()), mContext(nullptr) { | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 45 | pid_t uiThreadId = pthread_gettid_np(pthread_self()); | 
|  | 46 | pid_t renderThreadId = getRenderThreadTid(); | 
|  | 47 | mContext = mRenderThread.queue().runSync([=, this]() -> CanvasContext* { | 
| Matt Buckley | f5f90f1 | 2023-01-28 04:09:14 +0000 | [diff] [blame] | 48 | CanvasContext* context = CanvasContext::create(mRenderThread, translucent, rootRenderNode, | 
|  | 49 | contextFactory, uiThreadId, renderThreadId); | 
|  | 50 | if (context != nullptr) { | 
|  | 51 | mRenderThread.queue().post([=] { context->startHintSession(); }); | 
|  | 52 | } | 
|  | 53 | return context; | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 54 | }); | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 55 | mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
|  | 58 | RenderProxy::~RenderProxy() { | 
|  | 59 | destroyContext(); | 
|  | 60 | } | 
|  | 61 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 62 | void RenderProxy::destroyContext() { | 
|  | 63 | if (mContext) { | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 64 | mDrawFrameTask.setContext(nullptr, nullptr, nullptr); | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 65 | // This is also a fence as we need to be certain that there are no | 
|  | 66 | // outstanding mDrawFrame tasks posted before it is destroyed | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 67 | mRenderThread.queue().runSync([this]() { delete mContext; }); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 68 | mContext = nullptr; | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 69 | } | 
|  | 70 | } | 
|  | 71 |  | 
| John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 72 | void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | mRenderThread.queue().post([this, swapBehavior]() { mContext->setSwapBehavior(swapBehavior); }); | 
| John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
|  | 76 | bool RenderProxy::loadSystemProperties() { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 77 | return mRenderThread.queue().runSync([this]() -> bool { | 
| John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 78 | bool needsRedraw = Properties::load(); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 79 | if (mContext->profiler().consumeProperties()) { | 
|  | 80 | needsRedraw = true; | 
|  | 81 | } | 
|  | 82 | return needsRedraw; | 
|  | 83 | }); | 
| John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | void RenderProxy::setName(const char* name) { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 87 | // block since name/value pointers owned by caller | 
|  | 88 | // TODO: Support move arguments | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 | mRenderThread.queue().runSync([this, name]() { mContext->setName(std::string(name)); }); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 92 | void RenderProxy::setHardwareBuffer(AHardwareBuffer* buffer) { | 
|  | 93 | if (buffer) { | 
|  | 94 | AHardwareBuffer_acquire(buffer); | 
|  | 95 | } | 
|  | 96 | mRenderThread.queue().post([this, hardwareBuffer = buffer]() mutable { | 
|  | 97 | mContext->setHardwareBuffer(hardwareBuffer); | 
|  | 98 | if (hardwareBuffer) { | 
|  | 99 | AHardwareBuffer_release(hardwareBuffer); | 
|  | 100 | } | 
|  | 101 | }); | 
|  | 102 | } | 
|  | 103 |  | 
| Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 104 | void RenderProxy::setSurface(ANativeWindow* window, bool enableTimeout) { | 
| John Reck | e95c62d | 2020-08-18 12:37:43 -0700 | [diff] [blame] | 105 | if (window) { ANativeWindow_acquire(window); } | 
| Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 106 | mRenderThread.queue().post([this, win = window, enableTimeout]() mutable { | 
|  | 107 | mContext->setSurface(win, enableTimeout); | 
| John Reck | e95c62d | 2020-08-18 12:37:43 -0700 | [diff] [blame] | 108 | if (win) { ANativeWindow_release(win); } | 
| John Reck | cd18c22 | 2019-11-21 14:40:53 -0800 | [diff] [blame] | 109 | }); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 112 | void RenderProxy::setSurfaceControl(ASurfaceControl* surfaceControl) { | 
|  | 113 | auto funcs = mRenderThread.getASurfaceControlFunctions(); | 
|  | 114 | if (surfaceControl) { | 
|  | 115 | funcs.acquireFunc(surfaceControl); | 
|  | 116 | } | 
|  | 117 | mRenderThread.queue().post([this, control = surfaceControl, funcs]() mutable { | 
|  | 118 | mContext->setSurfaceControl(control); | 
|  | 119 | if (control) { | 
|  | 120 | funcs.releaseFunc(control); | 
|  | 121 | } | 
|  | 122 | }); | 
|  | 123 | } | 
|  | 124 |  | 
| John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 125 | void RenderProxy::allocateBuffers() { | 
|  | 126 | mRenderThread.queue().post([=]() { mContext->allocateBuffers(); }); | 
| Jorim Jaggi | 7823ee7 | 2018-07-17 15:24:16 +0200 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
| John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 129 | bool RenderProxy::pause() { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 130 | return mRenderThread.queue().runSync([this]() -> bool { return mContext->pauseSurface(); }); | 
| John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
|  | 133 | void RenderProxy::setStopped(bool stopped) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | mRenderThread.queue().runSync([this, stopped]() { mContext->setStopped(stopped); }); | 
| John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 137 | void RenderProxy::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 138 | mRenderThread.queue().post( | 
| John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 139 | [=]() { mContext->setLightAlpha(ambientShadowAlpha, spotShadowAlpha); }); | 
| Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
| John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 142 | void RenderProxy::setLightGeometry(const Vector3& lightCenter, float lightRadius) { | 
|  | 143 | mRenderThread.queue().post([=]() { mContext->setLightGeometry(lightCenter, lightRadius); }); | 
| John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
|  | 146 | void RenderProxy::setOpaque(bool opaque) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 147 | mRenderThread.queue().post([=]() { mContext->setOpaque(opaque); }); | 
| Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 150 | float RenderProxy::setColorMode(ColorMode mode) { | 
|  | 151 | // We only need to figure out what the renderer supports for HDR, otherwise this can stay | 
|  | 152 | // an async call since we already know the return value | 
| John Reck | 0b3f331 | 2023-01-31 16:21:28 -0500 | [diff] [blame] | 153 | if (mode == ColorMode::Hdr || mode == ColorMode::Hdr10) { | 
| John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 154 | return mRenderThread.queue().runSync( | 
|  | 155 | [=]() -> float { return mContext->setColorMode(mode); }); | 
|  | 156 | } else { | 
|  | 157 | mRenderThread.queue().post([=]() { mContext->setColorMode(mode); }); | 
|  | 158 | return 1.f; | 
|  | 159 | } | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | void RenderProxy::setRenderSdrHdrRatio(float ratio) { | 
|  | 163 | mDrawFrameTask.setRenderSdrHdrRatio(ratio); | 
| Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 166 | int64_t* RenderProxy::frameInfo() { | 
|  | 167 | return mDrawFrameTask.frameInfo(); | 
|  | 168 | } | 
|  | 169 |  | 
| chaviw | adba0b1 | 2022-03-18 17:42:15 -0500 | [diff] [blame] | 170 | void RenderProxy::forceDrawNextFrame() { | 
|  | 171 | mDrawFrameTask.forceDrawNextFrame(); | 
|  | 172 | } | 
|  | 173 |  | 
| John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 174 | int RenderProxy::syncAndDrawFrame() { | 
|  | 175 | return mDrawFrameTask.drawFrame(); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 178 | void RenderProxy::destroy() { | 
| John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 179 | // destroyCanvasAndSurface() needs a fence as when it returns the | 
|  | 180 | // underlying BufferQueue is going to be released from under | 
|  | 181 | // the render thread. | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 182 | mRenderThread.queue().runSync([=]() { mContext->destroy(); }); | 
| John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 185 | void RenderProxy::destroyFunctor(int functor) { | 
|  | 186 | ATRACE_CALL(); | 
|  | 187 | RenderThread& thread = RenderThread::getInstance(); | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 188 | thread.queue().post([=]() { WebViewFunctorManager::instance().destroyFunctor(functor); }); | 
| John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 191 | DeferredLayerUpdater* RenderProxy::createTextureLayer() { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 192 | return mRenderThread.queue().runSync([this]() -> auto { | 
|  | 193 | return mContext->createTextureLayer(); | 
|  | 194 | }); | 
| John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 197 | void RenderProxy::buildLayer(RenderNode* node) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 198 | mRenderThread.queue().runSync([&]() { mContext->buildLayer(node); }); | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 201 | bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) { | 
| John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 202 | ATRACE_NAME("TextureView#getBitmap"); | 
| Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 203 | auto& thread = RenderThread::getInstance(); | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 204 | return thread.queue().runSync([&]() -> bool { | 
|  | 205 | return thread.readback().copyLayerInto(layer, &bitmap) == CopyResult::Success; | 
|  | 206 | }); | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 209 | void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) { | 
|  | 210 | mDrawFrameTask.pushLayerUpdate(layer); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) { | 
|  | 214 | mDrawFrameTask.removeLayerUpdate(layer); | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
| John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 217 | void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 218 | return mRenderThread.queue().runSync([&]() { layer->detachSurfaceTexture(); }); | 
| John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
| John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 221 | void RenderProxy::destroyHardwareResources() { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 222 | return mRenderThread.queue().runSync([&]() { mContext->destroyHardwareResources(); }); | 
| John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
|  | 225 | void RenderProxy::trimMemory(int level) { | 
| John Reck | cd3a22c | 2014-08-06 13:33:59 -0700 | [diff] [blame] | 226 | // Avoid creating a RenderThread to do a trimMemory. | 
|  | 227 | if (RenderThread::hasInstance()) { | 
|  | 228 | RenderThread& thread = RenderThread::getInstance(); | 
| John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 229 | const auto trimLevel = static_cast<TrimLevel>(level); | 
|  | 230 | thread.queue().post([&thread, trimLevel]() { thread.trimMemory(trimLevel); }); | 
| John Reck | cd3a22c | 2014-08-06 13:33:59 -0700 | [diff] [blame] | 231 | } | 
| John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 232 | } | 
|  | 233 |  | 
| Jernej Virag | 44db040 | 2023-05-09 20:24:48 +0200 | [diff] [blame] | 234 | void RenderProxy::trimCaches(int level) { | 
|  | 235 | // Avoid creating a RenderThread to do a trimMemory. | 
|  | 236 | if (RenderThread::hasInstance()) { | 
|  | 237 | RenderThread& thread = RenderThread::getInstance(); | 
|  | 238 | const auto trimLevel = static_cast<CacheTrimLevel>(level); | 
|  | 239 | thread.queue().post([&thread, trimLevel]() { thread.trimCaches(trimLevel); }); | 
|  | 240 | } | 
|  | 241 | } | 
|  | 242 |  | 
| John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 243 | void RenderProxy::purgeCaches() { | 
|  | 244 | if (RenderThread::hasInstance()) { | 
|  | 245 | RenderThread& thread = RenderThread::getInstance(); | 
|  | 246 | thread.queue().post([&thread]() { | 
|  | 247 | if (thread.getGrContext()) { | 
| John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 248 | thread.cacheManager().trimMemory(TrimLevel::COMPLETE); | 
| John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 249 | } | 
|  | 250 | }); | 
|  | 251 | } | 
|  | 252 | } | 
|  | 253 |  | 
| Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 254 | void RenderProxy::overrideProperty(const char* name, const char* value) { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 255 | // expensive, but block here since name/value pointers owned by caller | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 256 | RenderThread::getInstance().queue().runSync( | 
|  | 257 | [&]() { Properties::overrideProperty(name, value); }); | 
| Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
| John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 260 | void RenderProxy::fence() { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 261 | mRenderThread.queue().runSync([]() {}); | 
| John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| John Reck | e4c1e6c | 2018-05-24 16:27:35 -0700 | [diff] [blame] | 264 | int RenderProxy::maxTextureSize() { | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 265 | static int maxTextureSize = RenderThread::getInstance().queue().runSync( | 
|  | 266 | []() { return DeviceInfo::get()->maxTextureSize(); }); | 
| John Reck | e4c1e6c | 2018-05-24 16:27:35 -0700 | [diff] [blame] | 267 | return maxTextureSize; | 
| John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
|  | 270 | void RenderProxy::stopDrawing() { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 271 | mRenderThread.queue().runSync([this]() { mContext->stopDrawing(); }); | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
|  | 274 | void RenderProxy::notifyFramePending() { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 275 | mRenderThread.queue().post([this]() { mContext->notifyFramePending(); }); | 
| John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Matt Buckley | d98e805 | 2022-10-21 22:13:23 +0000 | [diff] [blame] | 278 | void RenderProxy::notifyCallbackPending() { | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 279 | mRenderThread.queue().post([this]() { mContext->sendLoadResetHint(); }); | 
| Matt Buckley | d98e805 | 2022-10-21 22:13:23 +0000 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
| Matt Buckley | ac5f755 | 2022-12-19 22:03:27 +0000 | [diff] [blame] | 282 | void RenderProxy::notifyExpensiveFrame() { | 
|  | 283 | mRenderThread.queue().post([this]() { mContext->sendLoadIncreaseHint(); }); | 
|  | 284 | } | 
|  | 285 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 286 | void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 287 | mRenderThread.queue().runSync([&]() { | 
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 288 | std::lock_guard lock(mRenderThread.getJankDataMutex()); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 289 | mContext->profiler().dumpData(fd); | 
|  | 290 | if (dumpFlags & DumpFlags::FrameStats) { | 
|  | 291 | mContext->dumpFrames(fd); | 
|  | 292 | } | 
|  | 293 | if (dumpFlags & DumpFlags::JankStats) { | 
|  | 294 | mRenderThread.globalProfileData()->dump(fd); | 
|  | 295 | } | 
|  | 296 | if (dumpFlags & DumpFlags::Reset) { | 
|  | 297 | mContext->resetFrameStats(); | 
|  | 298 | } | 
|  | 299 | }); | 
| John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 300 | } | 
|  | 301 |  | 
|  | 302 | void RenderProxy::resetProfileInfo() { | 
| Jorim Jaggi | 33adb57 | 2021-02-22 14:27:53 +0100 | [diff] [blame] | 303 | mRenderThread.queue().runSync([=]() { | 
|  | 304 | std::lock_guard lock(mRenderThread.getJankDataMutex()); | 
|  | 305 | mContext->resetFrameStats(); | 
|  | 306 | }); | 
| John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 309 | uint32_t RenderProxy::frameTimePercentile(int percentile) { | 
|  | 310 | return mRenderThread.queue().runSync([&]() -> auto { | 
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 311 | std::lock_guard lock(mRenderThread.globalProfileData().getDataMutex()); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 312 | return mRenderThread.globalProfileData()->findPercentile(percentile); | 
|  | 313 | }); | 
| John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 314 | } | 
|  | 315 |  | 
| John Reck | 712eae0 | 2021-10-01 15:24:27 -0400 | [diff] [blame] | 316 | void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData, bool resetProfile) { | 
| John Reck | ba7e965 | 2019-01-23 10:33:41 -0800 | [diff] [blame] | 317 | if (RenderThread::hasInstance()) { | 
|  | 318 | auto& thread = RenderThread::getInstance(); | 
| John Reck | 712eae0 | 2021-10-01 15:24:27 -0400 | [diff] [blame] | 319 | thread.queue().runSync([&]() { | 
|  | 320 | thread.dumpGraphicsMemory(fd, includeProfileData); | 
|  | 321 | if (resetProfile) { | 
|  | 322 | thread.globalProfileData()->reset(); | 
|  | 323 | } | 
|  | 324 | }); | 
| John Reck | ba7e965 | 2019-01-23 10:33:41 -0800 | [diff] [blame] | 325 | } | 
| John Reck | 0341ba5 | 2023-08-24 11:37:36 -0400 | [diff] [blame] | 326 | if (!Properties::isolatedProcess) { | 
|  | 327 | std::string grallocInfo; | 
|  | 328 | GraphicBufferAllocator::getInstance().dump(grallocInfo); | 
|  | 329 | dprintf(fd, "%s\n", grallocInfo.c_str()); | 
|  | 330 | } | 
| John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
| John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 333 | void RenderProxy::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) { | 
|  | 334 | if (RenderThread::hasInstance()) { | 
|  | 335 | auto& thread = RenderThread::getInstance(); | 
|  | 336 | thread.queue().runSync([&]() { thread.getMemoryUsage(cpuUsage, gpuUsage); }); | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 |  | 
| John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 340 | void RenderProxy::setProcessStatsBuffer(int fd) { | 
| John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 341 | auto& rt = RenderThread::getInstance(); | 
| John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 342 | rt.queue().post([&rt, fd = dup(fd)]() { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 343 | rt.globalProfileData().switchStorageToAshmem(fd); | 
|  | 344 | close(fd); | 
|  | 345 | }); | 
| John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
|  | 348 | void RenderProxy::rotateProcessStatsBuffer() { | 
| John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 349 | auto& rt = RenderThread::getInstance(); | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 350 | rt.queue().post([&rt]() { rt.globalProfileData().rotateStorage(); }); | 
| John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 351 | } | 
|  | 352 |  | 
| Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 353 | int RenderProxy::getRenderThreadTid() { | 
|  | 354 | return mRenderThread.getTid(); | 
|  | 355 | } | 
|  | 356 |  | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 357 | void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 358 | mRenderThread.queue().post([=]() { mContext->addRenderNode(node, placeFront); }); | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | void RenderProxy::removeRenderNode(RenderNode* node) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 362 | mRenderThread.queue().post([=]() { mContext->removeRenderNode(node); }); | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 363 | } | 
|  | 364 |  | 
|  | 365 | void RenderProxy::drawRenderNode(RenderNode* node) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 366 | mRenderThread.queue().runSync([=]() { mContext->prepareAndDraw(node); }); | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 369 | void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) { | 
| John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 370 | mDrawFrameTask.setContentDrawBounds(left, top, right, bottom); | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 371 | } | 
|  | 372 |  | 
| Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 373 | void RenderProxy::setHardwareBufferRenderParams(const HardwareBufferRenderParams& params) { | 
|  | 374 | mDrawFrameTask.setHardwareBufferRenderParams(params); | 
|  | 375 | } | 
|  | 376 |  | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 377 | void RenderProxy::setPictureCapturedCallback( | 
|  | 378 | const std::function<void(sk_sp<SkPicture>&&)>& callback) { | 
|  | 379 | mRenderThread.queue().post( | 
| John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 380 | [this, cb = callback]() { mContext->setPictureCapturedCallback(cb); }); | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 381 | } | 
|  | 382 |  | 
| Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 383 | void RenderProxy::setASurfaceTransactionCallback( | 
| Huihong Luo | 4df4151 | 2021-06-24 10:04:32 -0700 | [diff] [blame] | 384 | const std::function<bool(int64_t, int64_t, int64_t)>& callback) { | 
| Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 385 | mRenderThread.queue().post( | 
|  | 386 | [this, cb = callback]() { mContext->setASurfaceTransactionCallback(cb); }); | 
|  | 387 | } | 
|  | 388 |  | 
| Huihong Luo | 34f42fd | 2021-05-03 14:47:36 -0700 | [diff] [blame] | 389 | void RenderProxy::setPrepareSurfaceControlForWebviewCallback( | 
|  | 390 | const std::function<void()>& callback) { | 
|  | 391 | mRenderThread.queue().post( | 
|  | 392 | [this, cb = callback]() { mContext->setPrepareSurfaceControlForWebviewCallback(cb); }); | 
|  | 393 | } | 
|  | 394 |  | 
| chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 395 | void RenderProxy::setFrameCallback( | 
|  | 396 | std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback) { | 
| Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 397 | mDrawFrameTask.setFrameCallback(std::move(callback)); | 
|  | 398 | } | 
|  | 399 |  | 
| chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 400 | void RenderProxy::setFrameCommitCallback(std::function<void(bool)>&& callback) { | 
|  | 401 | mDrawFrameTask.setFrameCommitCallback(std::move(callback)); | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | void RenderProxy::setFrameCompleteCallback(std::function<void()>&& callback) { | 
| John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 405 | mDrawFrameTask.setFrameCompleteCallback(std::move(callback)); | 
|  | 406 | } | 
|  | 407 |  | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 408 | void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) { | 
| John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 409 | mRenderThread.queue().post([this, observer = sp{observerPtr}]() { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 410 | mContext->addFrameMetricsObserver(observer.get()); | 
|  | 411 | }); | 
| Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 414 | void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) { | 
| John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 415 | mRenderThread.queue().post([this, observer = sp{observerPtr}]() { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 416 | mContext->removeFrameMetricsObserver(observer.get()); | 
|  | 417 | }); | 
| John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 418 | } | 
|  | 419 |  | 
| Tyler Freeman | 417accd | 2023-10-31 02:01:06 +0000 | [diff] [blame] | 420 | void RenderProxy::setForceDark(ForceDarkType type) { | 
|  | 421 | mRenderThread.queue().post([this, type]() { mContext->setForceDark(type); }); | 
| John Reck | bb3a358 | 2018-09-26 11:21:08 -0700 | [diff] [blame] | 422 | } | 
|  | 423 |  | 
| John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 424 | void RenderProxy::copySurfaceInto(ANativeWindow* window, std::shared_ptr<CopyRequest>&& request) { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 425 | auto& thread = RenderThread::getInstance(); | 
| John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 426 | ANativeWindow_acquire(window); | 
|  | 427 | thread.queue().post([&thread, window, request = std::move(request)] { | 
|  | 428 | thread.readback().copySurfaceInto(window, request); | 
|  | 429 | ANativeWindow_release(window); | 
|  | 430 | }); | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 431 | } | 
|  | 432 |  | 
| sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 433 | void RenderProxy::prepareToDraw(Bitmap& bitmap) { | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 434 | // If we haven't spun up a hardware accelerated window yet, there's no | 
|  | 435 | // point in precaching these bitmaps as it can't impact jank. | 
|  | 436 | // We also don't know if we even will spin up a hardware-accelerated | 
|  | 437 | // window or not. | 
|  | 438 | if (!RenderThread::hasInstance()) return; | 
|  | 439 | RenderThread* renderThread = &RenderThread::getInstance(); | 
| sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 440 | bitmap.ref(); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 441 | auto task = [renderThread, &bitmap]() { | 
|  | 442 | CanvasContext::prepareToDraw(*renderThread, &bitmap); | 
|  | 443 | bitmap.unref(); | 
|  | 444 | }; | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 445 | nsecs_t lastVsync = renderThread->timeLord().latestVsync(); | 
|  | 446 | nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos(); | 
| Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 447 | nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(SYSTEM_TIME_MONOTONIC); | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 448 | // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to | 
|  | 449 | // VSYNC+12ms or so, so aim for the gap during which RT is expected to | 
|  | 450 | // be idle | 
|  | 451 | // TODO: Make this concept a first-class supported thing? RT could use | 
|  | 452 | // knowledge of pending draws to better schedule this task | 
|  | 453 | if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 454 | renderThread->queue().postAt(estimatedNextVsync + 8_ms, task); | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 455 | } else { | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 456 | renderThread->queue().post(task); | 
| John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 457 | } | 
|  | 458 | } | 
|  | 459 |  | 
| Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 460 | int RenderProxy::copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap) { | 
| John Reck | fbeac3c | 2019-03-29 11:24:56 -0700 | [diff] [blame] | 461 | ATRACE_NAME("HardwareBitmap readback"); | 
| Stan Iliev | 6983bc4 | 2017-02-02 14:11:53 -0500 | [diff] [blame] | 462 | RenderThread& thread = RenderThread::getInstance(); | 
| John Reck | 1072fff | 2018-04-12 15:20:09 -0700 | [diff] [blame] | 463 | if (gettid() == thread.getTid()) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 464 | // TODO: fix everything that hits this. We should never be triggering a readback ourselves. | 
| Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 465 | return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); | 
| Stan Iliev | 6983bc4 | 2017-02-02 14:11:53 -0500 | [diff] [blame] | 466 | } else { | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 467 | return thread.queue().runSync( | 
|  | 468 | [&]() -> int { return (int)thread.readback().copyHWBitmapInto(hwBitmap, bitmap); }); | 
| Stan Iliev | 6983bc4 | 2017-02-02 14:11:53 -0500 | [diff] [blame] | 469 | } | 
| sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 470 | } | 
|  | 471 |  | 
| John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 472 | int RenderProxy::copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap) { | 
|  | 473 | RenderThread& thread = RenderThread::getInstance(); | 
|  | 474 | if (gettid() == thread.getTid()) { | 
|  | 475 | // TODO: fix everything that hits this. We should never be triggering a readback ourselves. | 
|  | 476 | return (int)thread.readback().copyImageInto(image, bitmap); | 
|  | 477 | } else { | 
|  | 478 | return thread.queue().runSync( | 
|  | 479 | [&]() -> int { return (int)thread.readback().copyImageInto(image, bitmap); }); | 
|  | 480 | } | 
|  | 481 | } | 
|  | 482 |  | 
| John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 483 | void RenderProxy::disableVsync() { | 
|  | 484 | Properties::disableVsync = true; | 
|  | 485 | } | 
|  | 486 |  | 
| Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 487 | void RenderProxy::preload() { | 
|  | 488 | // Create RenderThread object and start the thread. Then preload Vulkan/EGL driver. | 
|  | 489 | auto& thread = RenderThread::getInstance(); | 
| John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 490 | thread.queue().post([&thread]() { thread.preload(); }); | 
| Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 491 | } | 
|  | 492 |  | 
| chaviw | 01053d43 | 2022-03-18 17:54:00 -0500 | [diff] [blame] | 493 | void RenderProxy::setRtAnimationsEnabled(bool enabled) { | 
|  | 494 | if (RenderThread::hasInstance()) { | 
|  | 495 | RenderThread::getInstance().queue().post( | 
|  | 496 | [enabled]() { Properties::enableRTAnimations = enabled; }); | 
|  | 497 | } else { | 
|  | 498 | Properties::enableRTAnimations = enabled; | 
|  | 499 | } | 
|  | 500 | } | 
|  | 501 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 502 | } /* namespace renderthread */ | 
|  | 503 | } /* namespace uirenderer */ | 
|  | 504 | } /* namespace android */ |