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