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 | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 19 | #include "DeferredLayerUpdater.h" |
| 20 | #include "DisplayList.h" |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 22 | #include "Readback.h" |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 23 | #include "Rect.h" |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 24 | #include "pipeline/skia/VectorDrawableAtlas.h" |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 25 | #include "renderthread/CanvasContext.h" |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 26 | #include "renderthread/EglManager.h" |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 27 | #include "renderthread/RenderTask.h" |
| 28 | #include "renderthread/RenderThread.h" |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 29 | #include "renderstate/RenderState.h" |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 30 | #include "utils/Macros.h" |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 31 | #include "utils/TimeUtils.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 32 | |
sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 33 | #include <ui/GraphicBuffer.h> |
| 34 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace uirenderer { |
| 37 | namespace renderthread { |
| 38 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 39 | RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory) |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 40 | : mRenderThread(RenderThread::getInstance()) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 41 | , mContext(nullptr) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 42 | mContext = mRenderThread.queue().runSync([&]() -> CanvasContext* { |
| 43 | return CanvasContext::create(mRenderThread, translucent, rootRenderNode, contextFactory); |
| 44 | }); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 45 | mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode); |
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) { |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 54 | mDrawFrameTask.setContext(nullptr, nullptr, nullptr); |
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 | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 57 | mRenderThread.queue().runSync([this]() { |
| 58 | delete mContext; |
| 59 | }); |
| 60 | mContext = nullptr; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 64 | void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 65 | mRenderThread.queue().post([this, swapBehavior]() { |
| 66 | mContext->setSwapBehavior(swapBehavior); |
| 67 | }); |
John Reck | e4280ba | 2014-05-05 16:39:37 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool RenderProxy::loadSystemProperties() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 71 | return mRenderThread.queue().runSync([this]() -> bool { |
| 72 | bool needsRedraw = false; |
| 73 | if (Caches::hasInstance()) { |
| 74 | needsRedraw = Properties::load(); |
| 75 | } |
| 76 | if (mContext->profiler().consumeProperties()) { |
| 77 | needsRedraw = true; |
| 78 | } |
| 79 | return needsRedraw; |
| 80 | }); |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void RenderProxy::setName(const char* name) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 84 | // block since name/value pointers owned by caller |
| 85 | // TODO: Support move arguments |
| 86 | mRenderThread.queue().runSync([this, name]() { |
| 87 | mContext->setName(std::string(name)); |
| 88 | }); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 89 | } |
| 90 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 91 | void RenderProxy::initialize(const sp<Surface>& surface) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 92 | mRenderThread.queue().post([this, surf = surface]() mutable { |
| 93 | mContext->setSurface(std::move(surf)); |
| 94 | }); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 95 | } |
| 96 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 97 | void RenderProxy::updateSurface(const sp<Surface>& surface) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 98 | mRenderThread.queue().post([this, surf = surface]() mutable { |
| 99 | mContext->setSurface(std::move(surf)); |
| 100 | }); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 103 | bool RenderProxy::pauseSurface(const sp<Surface>& surface) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 104 | return mRenderThread.queue().runSync([this]() -> bool { |
| 105 | return mContext->pauseSurface(); |
| 106 | }); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void RenderProxy::setStopped(bool stopped) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 110 | mRenderThread.queue().runSync([this, stopped]() { |
| 111 | mContext->setStopped(stopped); |
| 112 | }); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 113 | } |
| 114 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 115 | void RenderProxy::setup(float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { |
| 116 | mRenderThread.queue().post([=]() { |
| 117 | mContext->setup(lightRadius, ambientShadowAlpha, spotShadowAlpha); |
| 118 | }); |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void RenderProxy::setLightCenter(const Vector3& lightCenter) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 122 | mRenderThread.queue().post([=]() { |
| 123 | mContext->setLightCenter(lightCenter); |
| 124 | }); |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void RenderProxy::setOpaque(bool opaque) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 128 | mRenderThread.queue().post([=]() { |
| 129 | mContext->setOpaque(opaque); |
| 130 | }); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void RenderProxy::setWideGamut(bool wideGamut) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 134 | mRenderThread.queue().post([=]() { |
| 135 | mContext->setWideGamut(wideGamut); |
| 136 | }); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 137 | } |
| 138 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 139 | int64_t* RenderProxy::frameInfo() { |
| 140 | return mDrawFrameTask.frameInfo(); |
| 141 | } |
| 142 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 143 | int RenderProxy::syncAndDrawFrame() { |
| 144 | return mDrawFrameTask.drawFrame(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 145 | } |
| 146 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 147 | void RenderProxy::destroy() { |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 148 | // destroyCanvasAndSurface() needs a fence as when it returns the |
| 149 | // underlying BufferQueue is going to be released from under |
| 150 | // the render thread. |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 151 | mRenderThread.queue().runSync([=]() { |
| 152 | mContext->destroy(); |
| 153 | }); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 157 | ATRACE_CALL(); |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 158 | RenderThread& thread = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 159 | auto invoke = [&thread, functor]() { CanvasContext::invokeFunctor(thread, functor); }; |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 160 | if (waitForCompletion) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 161 | // waitForCompletion = true is expected to be fairly rare and only |
| 162 | // happen in destruction. Thus it should be fine to temporarily |
| 163 | // create a Mutex |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 164 | thread.queue().runSync(std::move(invoke)); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 165 | } else { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 166 | thread.queue().post(std::move(invoke)); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 170 | DeferredLayerUpdater* RenderProxy::createTextureLayer() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 171 | return mRenderThread.queue().runSync([this]() -> auto { |
| 172 | return mContext->createTextureLayer(); |
| 173 | }); |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 174 | } |
| 175 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 176 | void RenderProxy::buildLayer(RenderNode* node) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 177 | mRenderThread.queue().runSync([&]() { |
| 178 | mContext->buildLayer(node); |
| 179 | }); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 180 | } |
| 181 | |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 182 | bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 183 | return mRenderThread.queue().runSync([&]() -> bool { |
| 184 | return mContext->copyLayerInto(layer, &bitmap); |
| 185 | }); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 186 | } |
| 187 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 188 | void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) { |
| 189 | mDrawFrameTask.pushLayerUpdate(layer); |
| 190 | } |
| 191 | |
| 192 | void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) { |
| 193 | mDrawFrameTask.removeLayerUpdate(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 194 | } |
| 195 | |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 196 | void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 197 | return mRenderThread.queue().runSync([&]() { |
| 198 | layer->detachSurfaceTexture(); |
| 199 | }); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 200 | } |
| 201 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 202 | void RenderProxy::destroyHardwareResources() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 203 | return mRenderThread.queue().runSync([&]() { |
| 204 | mContext->destroyHardwareResources(); |
| 205 | }); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void RenderProxy::trimMemory(int level) { |
John Reck | cd3a22c | 2014-08-06 13:33:59 -0700 | [diff] [blame] | 209 | // Avoid creating a RenderThread to do a trimMemory. |
| 210 | if (RenderThread::hasInstance()) { |
| 211 | RenderThread& thread = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 212 | thread.queue().post([&thread, level]() { |
| 213 | CanvasContext::trimMemory(thread, level); |
| 214 | }); |
John Reck | cd3a22c | 2014-08-06 13:33:59 -0700 | [diff] [blame] | 215 | } |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 218 | void RenderProxy::overrideProperty(const char* name, const char* value) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 219 | // expensive, but block here since name/value pointers owned by caller |
| 220 | RenderThread::getInstance().queue().runSync([&]() { |
| 221 | Properties::overrideProperty(name, value); |
| 222 | }); |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 223 | } |
| 224 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 225 | void RenderProxy::fence() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 226 | mRenderThread.queue().runSync([](){}); |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Thomas Buhot | c0a0e1a | 2016-01-18 10:31:58 +0100 | [diff] [blame] | 229 | void RenderProxy::staticFence() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 230 | RenderThread::getInstance().queue().runSync([](){}); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void RenderProxy::stopDrawing() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 234 | mRenderThread.queue().runSync([this]() { |
| 235 | mContext->stopDrawing(); |
| 236 | }); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void RenderProxy::notifyFramePending() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 240 | mRenderThread.queue().post([this]() { |
| 241 | mContext->notifyFramePending(); |
| 242 | }); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 243 | } |
| 244 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 245 | void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 246 | mRenderThread.queue().runSync([&]() { |
| 247 | mContext->profiler().dumpData(fd); |
| 248 | if (dumpFlags & DumpFlags::FrameStats) { |
| 249 | mContext->dumpFrames(fd); |
| 250 | } |
| 251 | if (dumpFlags & DumpFlags::JankStats) { |
| 252 | mRenderThread.globalProfileData()->dump(fd); |
| 253 | } |
| 254 | if (dumpFlags & DumpFlags::Reset) { |
| 255 | mContext->resetFrameStats(); |
| 256 | } |
| 257 | }); |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void RenderProxy::resetProfileInfo() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 261 | mRenderThread.queue().runSync([=]() { |
| 262 | mContext->resetFrameStats(); |
| 263 | }); |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 264 | } |
| 265 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 266 | uint32_t RenderProxy::frameTimePercentile(int percentile) { |
| 267 | return mRenderThread.queue().runSync([&]() -> auto { |
| 268 | return mRenderThread.globalProfileData()->findPercentile(percentile); |
| 269 | }); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Chris Craik | 2ae0733 | 2015-01-21 14:22:39 -0800 | [diff] [blame] | 272 | void RenderProxy::dumpGraphicsMemory(int fd) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 273 | auto& thread = RenderThread::getInstance(); |
| 274 | thread.queue().runSync([&]() { |
| 275 | thread.dumpGraphicsMemory(fd); |
| 276 | }); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void RenderProxy::setProcessStatsBuffer(int fd) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 280 | auto& rt = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 281 | rt.queue().post([&rt, fd = dup(fd)]() { |
| 282 | rt.globalProfileData().switchStorageToAshmem(fd); |
| 283 | close(fd); |
| 284 | }); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void RenderProxy::rotateProcessStatsBuffer() { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 288 | auto& rt = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 289 | rt.queue().post([&rt]() { |
| 290 | rt.globalProfileData().rotateStorage(); |
| 291 | }); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Tim Murray | 33eb07f | 2016-06-10 10:03:20 -0700 | [diff] [blame] | 294 | int RenderProxy::getRenderThreadTid() { |
| 295 | return mRenderThread.getTid(); |
| 296 | } |
| 297 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 298 | void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 299 | mRenderThread.queue().post([=]() { |
| 300 | mContext->addRenderNode(node, placeFront); |
| 301 | }); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void RenderProxy::removeRenderNode(RenderNode* node) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 305 | mRenderThread.queue().post([=]() { |
| 306 | mContext->removeRenderNode(node); |
| 307 | }); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void RenderProxy::drawRenderNode(RenderNode* node) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 311 | mRenderThread.queue().runSync([=]() { |
| 312 | mContext->prepareAndDraw(node); |
| 313 | }); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Skuhne | b816087 | 2015-09-22 09:51:39 -0700 | [diff] [blame] | 316 | void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) { |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 317 | mDrawFrameTask.setContentDrawBounds(left, top, right, bottom); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 318 | } |
| 319 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 320 | void RenderProxy::serializeDisplayListTree() { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 321 | mRenderThread.queue().post([=]() { |
| 322 | mContext->serializeDisplayListTree(); |
| 323 | }); |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 324 | } |
| 325 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 326 | void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observerPtr) { |
| 327 | mRenderThread.queue().post([this, observer = sp{observerPtr}]() { |
| 328 | mContext->addFrameMetricsObserver(observer.get()); |
| 329 | }); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 330 | } |
| 331 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 332 | void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observerPtr) { |
| 333 | mRenderThread.queue().post([this, observer = sp{observerPtr}]() { |
| 334 | mContext->removeFrameMetricsObserver(observer.get()); |
| 335 | }); |
John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 336 | } |
| 337 | |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 338 | int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top, |
| 339 | int right, int bottom, SkBitmap* bitmap) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 340 | auto& thread = RenderThread::getInstance(); |
| 341 | return static_cast<int>(thread.queue().runSync([&]() -> auto { |
| 342 | return thread.readback().copySurfaceInto(*surface, Rect(left, top, right, bottom), bitmap); |
| 343 | })); |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 344 | } |
| 345 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 346 | void RenderProxy::prepareToDraw(Bitmap& bitmap) { |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 347 | // If we haven't spun up a hardware accelerated window yet, there's no |
| 348 | // point in precaching these bitmaps as it can't impact jank. |
| 349 | // We also don't know if we even will spin up a hardware-accelerated |
| 350 | // window or not. |
| 351 | if (!RenderThread::hasInstance()) return; |
| 352 | RenderThread* renderThread = &RenderThread::getInstance(); |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 353 | bitmap.ref(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 354 | auto task = [renderThread, &bitmap]() { |
| 355 | CanvasContext::prepareToDraw(*renderThread, &bitmap); |
| 356 | bitmap.unref(); |
| 357 | }; |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 358 | nsecs_t lastVsync = renderThread->timeLord().latestVsync(); |
| 359 | nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos(); |
| 360 | nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC); |
| 361 | // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to |
| 362 | // VSYNC+12ms or so, so aim for the gap during which RT is expected to |
| 363 | // be idle |
| 364 | // TODO: Make this concept a first-class supported thing? RT could use |
| 365 | // knowledge of pending draws to better schedule this task |
| 366 | if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 367 | renderThread->queue().postAt(estimatedNextVsync + 8_ms, task); |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 368 | } else { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 369 | renderThread->queue().post(task); |
John Reck | 4387190 | 2016-08-01 14:39:24 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
sergeyv | 694d499 | 2016-10-27 10:23:13 -0700 | [diff] [blame] | 373 | sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 374 | auto& thread = RenderThread::getInstance(); |
| 375 | return thread.queue().runSync([&]() -> auto { |
| 376 | return thread.allocateHardwareBitmap(bitmap); |
| 377 | }); |
sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) { |
Stan Iliev | 6983bc4 | 2017-02-02 14:11:53 -0500 | [diff] [blame] | 381 | RenderThread& thread = RenderThread::getInstance(); |
| 382 | if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) { |
| 383 | //TODO: fix everything that hits this. We should never be triggering a readback ourselves. |
| 384 | return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap); |
| 385 | } else { |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 386 | return thread.queue().runSync([&]() -> int { |
| 387 | return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap); |
| 388 | }); |
Stan Iliev | 6983bc4 | 2017-02-02 14:11:53 -0500 | [diff] [blame] | 389 | } |
sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 390 | } |
| 391 | |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 392 | void RenderProxy::onBitmapDestroyed(uint32_t pixelRefId) { |
| 393 | if (!RenderThread::hasInstance()) return; |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 394 | RenderThread& thread = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 395 | thread.queue().post([&thread, pixelRefId]() { |
| 396 | thread.renderState().onBitmapDestroyed(pixelRefId); |
| 397 | }); |
John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 398 | } |
| 399 | |
John Reck | a896306 | 2017-06-14 10:47:50 -0700 | [diff] [blame] | 400 | void RenderProxy::disableVsync() { |
| 401 | Properties::disableVsync = true; |
| 402 | } |
| 403 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 404 | void RenderProxy::repackVectorDrawableAtlas() { |
| 405 | RenderThread& thread = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 406 | thread.queue().post([&thread]() { |
| 407 | thread.cacheManager().acquireVectorDrawableAtlas()->repackIfNeeded(thread.getGrContext()); |
| 408 | }); |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void RenderProxy::releaseVDAtlasEntries() { |
| 412 | RenderThread& thread = RenderThread::getInstance(); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame^] | 413 | thread.queue().post([&thread]() { |
| 414 | thread.cacheManager().acquireVectorDrawableAtlas()->delayedReleaseEntries(); |
| 415 | }); |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 416 | } |
| 417 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 418 | } /* namespace renderthread */ |
| 419 | } /* namespace uirenderer */ |
| 420 | } /* namespace android */ |