John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 17 | #include "RenderNode.h" |
| 18 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 19 | #include "DamageAccumulator.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 20 | #include "Debug.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 21 | #include "TreeInfo.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include "VectorDrawable.h" |
Derek Sollenberger | 5d0ca63 | 2019-07-19 16:17:12 -0400 | [diff] [blame^] | 23 | #include "private/hwui/WebViewFunctor.h" |
Fedor Kudasov | 86bd214 | 2019-06-18 15:51:57 +0100 | [diff] [blame] | 24 | #ifdef __ANDROID__ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | #include "renderthread/CanvasContext.h" |
Fedor Kudasov | 86bd214 | 2019-06-18 15:51:57 +0100 | [diff] [blame] | 26 | #else |
| 27 | #include "DamageAccumulator.h" |
| 28 | #include "pipeline/skia/SkiaDisplayList.h" |
| 29 | #endif |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 30 | #include "utils/FatVector.h" |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 31 | #include "utils/MathUtils.h" |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 32 | #include "utils/StringUtils.h" |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 33 | #include "utils/TraceUtils.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 34 | |
Derek Sollenberger | 579317d4 | 2017-08-29 16:33:49 -0400 | [diff] [blame] | 35 | #include <SkPathOps.h> |
John Reck | 77c4010 | 2015-10-26 15:49:47 -0700 | [diff] [blame] | 36 | #include <algorithm> |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 37 | #include <atomic> |
John Reck | 77c4010 | 2015-10-26 15:49:47 -0700 | [diff] [blame] | 38 | #include <sstream> |
| 39 | #include <string> |
| 40 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace uirenderer { |
| 43 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 44 | // Used for tree mutations that are purely destructive. |
| 45 | // Generic tree mutations should use MarkAndSweepObserver instead |
| 46 | class ImmediateRemoved : public TreeObserver { |
| 47 | public: |
| 48 | explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {} |
| 49 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); } |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | TreeInfo* mTreeInfo; |
| 54 | }; |
| 55 | |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 56 | static int64_t generateId() { |
| 57 | static std::atomic<int64_t> sNextId{1}; |
| 58 | return sNextId++; |
| 59 | } |
| 60 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 61 | RenderNode::RenderNode() |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 62 | : mUniqueId(generateId()) |
| 63 | , mDirtyPropertyFields(0) |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 64 | , mNeedsDisplayListSync(false) |
| 65 | , mDisplayList(nullptr) |
| 66 | , mStagingDisplayList(nullptr) |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 67 | , mAnimatorManager(*this) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 68 | , mParentCount(0) {} |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 69 | |
| 70 | RenderNode::~RenderNode() { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 71 | ImmediateRemoved observer(nullptr); |
| 72 | deleteDisplayList(observer); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 73 | delete mStagingDisplayList; |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 74 | LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!"); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 75 | } |
| 76 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 77 | void RenderNode::setStagingDisplayList(DisplayList* displayList) { |
| 78 | mValid = (displayList != nullptr); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 79 | mNeedsDisplayListSync = true; |
| 80 | delete mStagingDisplayList; |
| 81 | mStagingDisplayList = displayList; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * This function is a simplified version of replay(), where we simply retrieve and log the |
| 86 | * display list. This function should remain in sync with the replay() function. |
| 87 | */ |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 88 | void RenderNode::output() { |
| 89 | LogcatStream strout; |
| 90 | strout << "Root"; |
| 91 | output(strout, 0); |
| 92 | } |
| 93 | |
| 94 | void RenderNode::output(std::ostream& output, uint32_t level) { |
| 95 | output << " (" << getName() << " " << this |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 96 | << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "") |
| 97 | << (properties().hasShadow() ? ", casting shadow" : "") |
| 98 | << (isRenderable() ? "" : ", empty") |
| 99 | << (properties().getProjectBackwards() ? ", projected" : "") |
| 100 | << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl; |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 101 | |
| 102 | properties().debugOutputProperties(output, level + 1); |
Chris Craik | 91eff22 | 2016-02-22 13:39:33 -0800 | [diff] [blame] | 103 | |
| 104 | if (mDisplayList) { |
Stan Iliev | d217237 | 2017-02-09 16:59:27 -0500 | [diff] [blame] | 105 | mDisplayList->output(output, level); |
Chris Craik | 91eff22 | 2016-02-22 13:39:33 -0800 | [diff] [blame] | 106 | } |
sergeyv | c3849aa | 2016-08-08 13:22:06 -0700 | [diff] [blame] | 107 | output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")"; |
| 108 | output << std::endl; |
Chris Craik | 91eff22 | 2016-02-22 13:39:33 -0800 | [diff] [blame] | 109 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 110 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 111 | int RenderNode::getDebugSize() { |
| 112 | int size = sizeof(RenderNode); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 113 | if (mStagingDisplayList) { |
| 114 | size += mStagingDisplayList->getUsedSize(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 115 | } |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 116 | if (mDisplayList && mDisplayList != mStagingDisplayList) { |
| 117 | size += mDisplayList->getUsedSize(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 118 | } |
| 119 | return size; |
| 120 | } |
| 121 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 122 | void RenderNode::prepareTree(TreeInfo& info) { |
| 123 | ATRACE_CALL(); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 124 | LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing"); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 125 | MarkAndSweepRemoved observer(&info); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 126 | |
John Reck | 1423e13 | 2018-09-21 14:30:19 -0700 | [diff] [blame] | 127 | const int before = info.disableForceDark; |
John Reck | 1072fff | 2018-04-12 15:20:09 -0700 | [diff] [blame] | 128 | prepareTreeImpl(observer, info, false); |
John Reck | 1423e13 | 2018-09-21 14:30:19 -0700 | [diff] [blame] | 129 | LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark"); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 130 | } |
| 131 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 132 | void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
| 133 | mAnimatorManager.addAnimator(animator); |
| 134 | } |
| 135 | |
Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 136 | void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
| 137 | mAnimatorManager.removeAnimator(animator); |
| 138 | } |
| 139 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 140 | void RenderNode::damageSelf(TreeInfo& info) { |
John Reck | ce9f308 | 2014-06-17 16:18:09 -0700 | [diff] [blame] | 141 | if (isRenderable()) { |
John Reck | f1aa7909e | 2019-03-07 17:01:08 -0800 | [diff] [blame] | 142 | mDamageGenerationId = info.damageGenerationId; |
John Reck | 293e868 | 2014-06-17 10:34:02 -0700 | [diff] [blame] | 143 | if (properties().getClipDamageToBounds()) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 144 | info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight()); |
| 145 | } else { |
| 146 | // Hope this is big enough? |
| 147 | // TODO: Get this from the display list ops or something |
John Reck | c128823 | 2015-08-12 13:39:11 -0700 | [diff] [blame] | 148 | info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 149 | } |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 153 | void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) { |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 154 | LayerType layerType = properties().effectiveLayerType(); |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 155 | if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) { |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 156 | // Damage applied so far needs to affect our parent, but does not require |
| 157 | // the layer to be updated. So we pop/push here to clear out the current |
| 158 | // damage and get a clean state for display list or children updates to |
| 159 | // affect, which will require the layer to be updated |
| 160 | info.damageAccumulator->popTransform(); |
| 161 | info.damageAccumulator->pushTransform(this); |
| 162 | if (dirtyMask & DISPLAY_LIST) { |
| 163 | damageSelf(info); |
| 164 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | void RenderNode::pushLayerUpdate(TreeInfo& info) { |
Fedor Kudasov | 86bd214 | 2019-06-18 15:51:57 +0100 | [diff] [blame] | 169 | #ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 170 | LayerType layerType = properties().effectiveLayerType(); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 171 | // If we are not a layer OR we cannot be rendered (eg, view was detached) |
| 172 | // we need to destroy any Layers we may have had previously |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 173 | if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) || |
| 174 | CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) || |
| 175 | CC_UNLIKELY(!properties().fitsOnLayer())) { |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 176 | if (CC_UNLIKELY(hasLayer())) { |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 177 | this->setLayerSurface(nullptr); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 178 | } |
| 179 | return; |
| 180 | } |
| 181 | |
Stan Iliev | 216b157 | 2018-03-26 14:29:50 -0400 | [diff] [blame] | 182 | if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 183 | damageSelf(info); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 186 | if (!hasLayer()) { |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Derek Sollenberger | 6a21ca5 | 2016-09-28 13:39:55 -0400 | [diff] [blame] | 190 | SkRect dirty; |
| 191 | info.damageAccumulator->peekAtDirty(&dirty); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 192 | info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 193 | |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 194 | // There might be prefetched layers that need to be accounted for. |
| 195 | // That might be us, so tell CanvasContext that this layer is in the |
| 196 | // tree and should not be destroyed. |
| 197 | info.canvasContext.markLayerInUse(this); |
Fedor Kudasov | 86bd214 | 2019-06-18 15:51:57 +0100 | [diff] [blame] | 198 | #endif |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Traverse down the the draw tree to prepare for a frame. |
| 203 | * |
| 204 | * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven |
| 205 | * |
| 206 | * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the |
| 207 | * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer. |
| 208 | */ |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 209 | void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) { |
John Reck | f1aa7909e | 2019-03-07 17:01:08 -0800 | [diff] [blame] | 210 | if (mDamageGenerationId == info.damageGenerationId) { |
| 211 | // We hit the same node a second time in the same tree. We don't know the minimal |
| 212 | // damage rect anymore, so just push the biggest we can onto our parent's transform |
| 213 | // We push directly onto parent in case we are clipped to bounds but have moved position. |
| 214 | info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX); |
| 215 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 216 | info.damageAccumulator->pushTransform(this); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 217 | |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 218 | if (info.mode == TreeInfo::MODE_FULL) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 219 | pushStagingPropertiesChanges(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 220 | } |
John Reck | 1423e13 | 2018-09-21 14:30:19 -0700 | [diff] [blame] | 221 | |
| 222 | if (!mProperties.getAllowForceDark()) { |
| 223 | info.disableForceDark++; |
| 224 | } |
| 225 | |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 226 | uint32_t animatorDirtyMask = 0; |
| 227 | if (CC_LIKELY(info.runAnimations)) { |
| 228 | animatorDirtyMask = mAnimatorManager.animate(info); |
| 229 | } |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 230 | |
John Reck | 3f725f0 | 2015-06-16 10:29:31 -0700 | [diff] [blame] | 231 | bool willHaveFunctor = false; |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 232 | if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) { |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 233 | willHaveFunctor = mStagingDisplayList->hasFunctor(); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 234 | } else if (mDisplayList) { |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 235 | willHaveFunctor = mDisplayList->hasFunctor(); |
John Reck | 3f725f0 | 2015-06-16 10:29:31 -0700 | [diff] [blame] | 236 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 237 | bool childFunctorsNeedLayer = |
| 238 | mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer); |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 239 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 240 | if (CC_UNLIKELY(mPositionListener.get())) { |
| 241 | mPositionListener->onPositionUpdated(*this, info); |
| 242 | } |
| 243 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 244 | prepareLayer(info, animatorDirtyMask); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 245 | if (info.mode == TreeInfo::MODE_FULL) { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 246 | pushStagingDisplayListChanges(observer, info); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 247 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 248 | |
Doris Liu | 07c056d | 2016-06-13 12:52:44 -0700 | [diff] [blame] | 249 | if (mDisplayList) { |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 250 | info.out.hasFunctors |= mDisplayList->hasFunctor(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 251 | bool isDirty = mDisplayList->prepareListAndChildren( |
| 252 | observer, info, childFunctorsNeedLayer, |
| 253 | [](RenderNode* child, TreeObserver& observer, TreeInfo& info, |
| 254 | bool functorsNeedLayer) { |
| 255 | child->prepareTreeImpl(observer, info, functorsNeedLayer); |
| 256 | }); |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 257 | if (isDirty) { |
| 258 | damageSelf(info); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 259 | } |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 260 | } |
Doris Liu | b51b286 | 2016-08-01 19:56:47 -0700 | [diff] [blame] | 261 | pushLayerUpdate(info); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 262 | |
John Reck | 1423e13 | 2018-09-21 14:30:19 -0700 | [diff] [blame] | 263 | if (!mProperties.getAllowForceDark()) { |
| 264 | info.disableForceDark--; |
| 265 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 266 | info.damageAccumulator->popTransform(); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 269 | void RenderNode::syncProperties() { |
| 270 | mProperties = mStagingProperties; |
| 271 | } |
| 272 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 273 | void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) { |
John Reck | 097e1d3 | 2019-06-12 15:01:51 -0700 | [diff] [blame] | 274 | if (mPositionListenerDirty) { |
| 275 | mPositionListener = std::move(mStagingPositionListener); |
| 276 | mStagingPositionListener = nullptr; |
| 277 | mPositionListenerDirty = false; |
| 278 | } |
| 279 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 280 | // Push the animators first so that setupStartValueIfNecessary() is called |
| 281 | // before properties() is trampled by stagingProperties(), as they are |
| 282 | // required by some animators. |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 283 | if (CC_LIKELY(info.runAnimations)) { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 284 | mAnimatorManager.pushStaging(); |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 285 | } |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 286 | if (mDirtyPropertyFields) { |
| 287 | mDirtyPropertyFields = 0; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 288 | damageSelf(info); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 289 | info.damageAccumulator->popTransform(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 290 | syncProperties(); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 291 | // We could try to be clever and only re-damage if the matrix changed. |
| 292 | // However, we don't need to worry about that. The cost of over-damaging |
| 293 | // here is only going to be a single additional map rect of this node |
| 294 | // plus a rect join(). The parent's transform (and up) will only be |
| 295 | // performed once. |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 296 | info.damageAccumulator->pushTransform(this); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 297 | damageSelf(info); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 298 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 299 | } |
| 300 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 301 | void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 302 | // Make sure we inc first so that we don't fluctuate between 0 and 1, |
| 303 | // which would thrash the layer cache |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 304 | if (mStagingDisplayList) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 305 | mStagingDisplayList->updateChildren([](RenderNode* child) { child->incParentRefCount(); }); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 306 | } |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 307 | deleteDisplayList(observer, info); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 308 | mDisplayList = mStagingDisplayList; |
| 309 | mStagingDisplayList = nullptr; |
| 310 | if (mDisplayList) { |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 311 | WebViewSyncData syncData { |
| 312 | .applyForceDark = info && !info->disableForceDark |
| 313 | }; |
| 314 | mDisplayList->syncContents(syncData); |
John Reck | 3b4510cd | 2018-09-27 17:39:45 -0700 | [diff] [blame] | 315 | handleForceDark(info); |
| 316 | } |
| 317 | } |
John Reck | f3c724f | 2018-09-20 13:00:04 -0700 | [diff] [blame] | 318 | |
John Reck | 3b4510cd | 2018-09-27 17:39:45 -0700 | [diff] [blame] | 319 | void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) { |
| 320 | if (CC_LIKELY(!info || info->disableForceDark)) { |
| 321 | return; |
| 322 | } |
| 323 | auto usage = usageHint(); |
| 324 | const auto& children = mDisplayList->mChildNodes; |
| 325 | if (mDisplayList->hasText()) { |
| 326 | usage = UsageHint::Foreground; |
| 327 | } |
| 328 | if (usage == UsageHint::Unknown) { |
| 329 | if (children.size() > 1) { |
| 330 | usage = UsageHint::Background; |
| 331 | } else if (children.size() == 1 && |
| 332 | children.front().getRenderNode()->usageHint() != |
| 333 | UsageHint::Background) { |
| 334 | usage = UsageHint::Background; |
John Reck | 8f45d4a | 2018-08-15 10:17:12 -0700 | [diff] [blame] | 335 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 336 | } |
John Reck | 3b4510cd | 2018-09-27 17:39:45 -0700 | [diff] [blame] | 337 | if (children.size() > 1) { |
| 338 | // Crude overlap check |
| 339 | SkRect drawn = SkRect::MakeEmpty(); |
| 340 | for (auto iter = children.rbegin(); iter != children.rend(); ++iter) { |
| 341 | const auto& child = iter->getRenderNode(); |
| 342 | // We use stagingProperties here because we haven't yet sync'd the children |
| 343 | SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(), |
| 344 | child->stagingProperties().getWidth(), child->stagingProperties().getHeight()); |
| 345 | if (bounds.contains(drawn)) { |
| 346 | // This contains everything drawn after it, so make it a background |
| 347 | child->setUsageHint(UsageHint::Background); |
| 348 | } |
| 349 | drawn.join(bounds); |
| 350 | } |
| 351 | } |
| 352 | mDisplayList->mDisplayList.applyColorTransform( |
| 353 | usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 354 | } |
| 355 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 356 | void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) { |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 357 | if (mNeedsDisplayListSync) { |
| 358 | mNeedsDisplayListSync = false; |
John Reck | 5c9d717 | 2014-10-22 11:32:27 -0700 | [diff] [blame] | 359 | // Damage with the old display list first then the new one to catch any |
| 360 | // changes in isRenderable or, in the future, bounds |
| 361 | damageSelf(info); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 362 | syncDisplayList(observer, &info); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 363 | damageSelf(info); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 364 | } |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 365 | } |
| 366 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 367 | void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) { |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 368 | if (mDisplayList) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 369 | mDisplayList->updateChildren( |
| 370 | [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); }); |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 371 | if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) { |
| 372 | delete mDisplayList; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 375 | mDisplayList = nullptr; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 376 | } |
| 377 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 378 | void RenderNode::destroyHardwareResources(TreeInfo* info) { |
Derek Sollenberger | 0df6209 | 2016-09-27 16:04:42 -0400 | [diff] [blame] | 379 | if (hasLayer()) { |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 380 | this->setLayerSurface(nullptr); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 381 | } |
John Reck | 3afd637 | 2017-01-30 10:15:48 -0800 | [diff] [blame] | 382 | setStagingDisplayList(nullptr); |
| 383 | |
| 384 | ImmediateRemoved observer(info); |
| 385 | deleteDisplayList(observer, info); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void RenderNode::destroyLayers() { |
| 389 | if (hasLayer()) { |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 390 | this->setLayerSurface(nullptr); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 391 | } |
| 392 | if (mDisplayList) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 393 | mDisplayList->updateChildren([](RenderNode* child) { child->destroyLayers(); }); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) { |
| 398 | LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!"); |
| 399 | mParentCount--; |
| 400 | if (!mParentCount) { |
| 401 | observer.onMaybeRemovedFromTree(this); |
| 402 | if (CC_UNLIKELY(mPositionListener.get())) { |
| 403 | mPositionListener->onPositionLost(*this, info); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 408 | void RenderNode::onRemovedFromTree(TreeInfo* info) { |
| 409 | destroyHardwareResources(info); |
| 410 | } |
| 411 | |
| 412 | void RenderNode::clearRoot() { |
| 413 | ImmediateRemoved observer(nullptr); |
| 414 | decParentRefCount(observer); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 415 | } |
| 416 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 417 | /** |
| 418 | * Apply property-based transformations to input matrix |
| 419 | * |
| 420 | * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 |
| 421 | * matrix computation instead of the Skia 3x3 matrix + camera hackery. |
| 422 | */ |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 423 | void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 424 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 425 | matrix.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 426 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 427 | if (properties().getStaticMatrix()) { |
| 428 | mat4 stat(*properties().getStaticMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 429 | matrix.multiply(stat); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 430 | } else if (properties().getAnimationMatrix()) { |
| 431 | mat4 anim(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 432 | matrix.multiply(anim); |
| 433 | } |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 434 | |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 435 | bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ()); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 436 | if (properties().hasTransformMatrix() || applyTranslationZ) { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 437 | if (properties().isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 438 | matrix.translate(properties().getTranslationX(), properties().getTranslationY(), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 439 | true3dTransform ? properties().getZ() : 0.0f); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 440 | } else { |
| 441 | if (!true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 442 | matrix.multiply(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 443 | } else { |
| 444 | mat4 true3dMat; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 445 | true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(), |
| 446 | properties().getPivotY() + properties().getTranslationY(), |
| 447 | properties().getZ()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 448 | true3dMat.rotate(properties().getRotationX(), 1, 0, 0); |
| 449 | true3dMat.rotate(properties().getRotationY(), 0, 1, 0); |
| 450 | true3dMat.rotate(properties().getRotation(), 0, 0, 1); |
| 451 | true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); |
| 452 | true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 453 | |
| 454 | matrix.multiply(true3dMat); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Derek Sollenberger | 579317d4 | 2017-08-29 16:33:49 -0400 | [diff] [blame] | 460 | const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const { |
| 461 | const SkPath* outlinePath = properties().getOutline().getPath(); |
| 462 | const uint32_t outlineID = outlinePath->getGenerationID(); |
| 463 | |
| 464 | if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) { |
| 465 | // update the cache keys |
| 466 | mClippedOutlineCache.outlineID = outlineID; |
| 467 | mClippedOutlineCache.clipRect = clipRect; |
| 468 | |
| 469 | // update the cache value by recomputing a new path |
| 470 | SkPath clipPath; |
| 471 | clipPath.addRect(clipRect); |
| 472 | Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline); |
Derek Sollenberger | 579317d4 | 2017-08-29 16:33:49 -0400 | [diff] [blame] | 473 | } |
| 474 | return &mClippedOutlineCache.clippedOutline; |
| 475 | } |
| 476 | |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 477 | using StringBuffer = FatVector<char, 128>; |
| 478 | |
| 479 | template <typename... T> |
John Reck | 7af5b2c | 2018-12-05 13:36:20 -0800 | [diff] [blame] | 480 | // TODO:__printflike(2, 3) |
| 481 | // Doesn't work because the warning doesn't understand string_view and doesn't like that |
| 482 | // it's not a C-style variadic function. |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 483 | static void format(StringBuffer& buffer, const std::string_view& format, T... args) { |
| 484 | buffer.resize(buffer.capacity()); |
| 485 | while (1) { |
| 486 | int needed = snprintf(buffer.data(), buffer.size(), |
| 487 | format.data(), std::forward<T>(args)...); |
| 488 | if (needed < 0) { |
| 489 | buffer[0] = '\0'; |
| 490 | buffer.resize(1); |
| 491 | return; |
| 492 | } |
| 493 | if (needed < buffer.size()) { |
| 494 | buffer.resize(needed + 1); |
| 495 | return; |
| 496 | } |
John Reck | 7af5b2c | 2018-12-05 13:36:20 -0800 | [diff] [blame] | 497 | // If we're doing a heap alloc anyway might as well give it some slop |
| 498 | buffer.resize(needed + 100); |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | void RenderNode::markDrawStart(SkCanvas& canvas) { |
| 503 | StringBuffer buffer; |
John Reck | 7af5b2c | 2018-12-05 13:36:20 -0800 | [diff] [blame] | 504 | format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName()); |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 505 | canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr); |
| 506 | } |
| 507 | |
| 508 | void RenderNode::markDrawEnd(SkCanvas& canvas) { |
| 509 | StringBuffer buffer; |
John Reck | 7af5b2c | 2018-12-05 13:36:20 -0800 | [diff] [blame] | 510 | format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName()); |
John Reck | f96b284 | 2018-11-29 09:44:10 -0800 | [diff] [blame] | 511 | canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr); |
| 512 | } |
| 513 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 514 | } /* namespace uirenderer */ |
| 515 | } /* namespace android */ |