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