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