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