| 1 | /* |
| 2 | * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "ScrollingStateNode.h" |
| 28 | |
| 29 | #if ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS) |
| 30 | |
| 31 | #include "ScrollingStateFixedNode.h" |
| 32 | #include "ScrollingStateTree.h" |
| 33 | #include <wtf/text/TextStream.h> |
| 34 | |
| 35 | #include <wtf/text/WTFString.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | ScrollingStateNode::ScrollingStateNode(ScrollingNodeType nodeType, ScrollingStateTree& scrollingStateTree, ScrollingNodeID nodeID) |
| 40 | : m_nodeType(nodeType) |
| 41 | , m_nodeID(nodeID) |
| 42 | , m_scrollingStateTree(scrollingStateTree) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | // This copy constructor is used for cloning nodes in the tree, and it doesn't make sense |
| 47 | // to clone the relationship pointers, so don't copy that information from the original node. |
| 48 | ScrollingStateNode::ScrollingStateNode(const ScrollingStateNode& stateNode, ScrollingStateTree& adoptiveTree) |
| 49 | : m_nodeType(stateNode.nodeType()) |
| 50 | , m_nodeID(stateNode.scrollingNodeID()) |
| 51 | , m_changedProperties(stateNode.changedProperties()) |
| 52 | , m_scrollingStateTree(adoptiveTree) |
| 53 | { |
| 54 | if (hasChangedProperty(Layer)) |
| 55 | setLayer(stateNode.layer().toRepresentation(adoptiveTree.preferredLayerRepresentation())); |
| 56 | scrollingStateTree().addNode(*this); |
| 57 | } |
| 58 | |
| 59 | ScrollingStateNode::~ScrollingStateNode() = default; |
| 60 | |
| 61 | void ScrollingStateNode::setPropertyChanged(unsigned propertyBit) |
| 62 | { |
| 63 | if (hasChangedProperty(propertyBit)) |
| 64 | return; |
| 65 | |
| 66 | setPropertyChangedBit(propertyBit); |
| 67 | m_scrollingStateTree.setHasChangedProperties(); |
| 68 | } |
| 69 | |
| 70 | void ScrollingStateNode::setAllPropertiesChanged() |
| 71 | { |
| 72 | setPropertyChangedBit(Layer); |
| 73 | setPropertyChangedBit(ChildNodes); |
| 74 | m_scrollingStateTree.setHasChangedProperties(); |
| 75 | } |
| 76 | |
| 77 | Ref<ScrollingStateNode> ScrollingStateNode::cloneAndReset(ScrollingStateTree& adoptiveTree) |
| 78 | { |
| 79 | auto clone = this->clone(adoptiveTree); |
| 80 | |
| 81 | // Now that this node is cloned, reset our change properties. |
| 82 | resetChangedProperties(); |
| 83 | |
| 84 | cloneAndResetChildren(clone.get(), adoptiveTree); |
| 85 | |
| 86 | return clone; |
| 87 | } |
| 88 | |
| 89 | void ScrollingStateNode::cloneAndResetChildren(ScrollingStateNode& clone, ScrollingStateTree& adoptiveTree) |
| 90 | { |
| 91 | if (!m_children) |
| 92 | return; |
| 93 | |
| 94 | for (auto& child : *m_children) |
| 95 | clone.appendChild(child->cloneAndReset(adoptiveTree)); |
| 96 | } |
| 97 | |
| 98 | void ScrollingStateNode::appendChild(Ref<ScrollingStateNode>&& childNode) |
| 99 | { |
| 100 | childNode->setParent(this); |
| 101 | |
| 102 | if (!m_children) |
| 103 | m_children = std::make_unique<Vector<RefPtr<ScrollingStateNode>>>(); |
| 104 | m_children->append(WTFMove(childNode)); |
| 105 | setPropertyChanged(ChildNodes); |
| 106 | } |
| 107 | |
| 108 | void ScrollingStateNode::insertChild(Ref<ScrollingStateNode>&& childNode, size_t index) |
| 109 | { |
| 110 | childNode->setParent(this); |
| 111 | |
| 112 | if (!m_children) { |
| 113 | ASSERT(!index); |
| 114 | m_children = std::make_unique<Vector<RefPtr<ScrollingStateNode>>>(); |
| 115 | } |
| 116 | |
| 117 | m_children->insert(index, WTFMove(childNode)); |
| 118 | setPropertyChanged(ChildNodes); |
| 119 | } |
| 120 | |
| 121 | void ScrollingStateNode::removeFromParent() |
| 122 | { |
| 123 | if (!m_parent) |
| 124 | return; |
| 125 | |
| 126 | m_parent->removeChild(*this); |
| 127 | m_parent = nullptr; |
| 128 | } |
| 129 | |
| 130 | void ScrollingStateNode::removeChild(ScrollingStateNode& childNode) |
| 131 | { |
| 132 | auto childIndex = indexOfChild(childNode); |
| 133 | if (childIndex != notFound) |
| 134 | removeChildAtIndex(childIndex); |
| 135 | } |
| 136 | |
| 137 | void ScrollingStateNode::removeChildAtIndex(size_t index) |
| 138 | { |
| 139 | ASSERT(m_children && index < m_children->size()); |
| 140 | if (m_children && index < m_children->size()) { |
| 141 | m_children->remove(index); |
| 142 | setPropertyChanged(ChildNodes); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | size_t ScrollingStateNode::indexOfChild(ScrollingStateNode& childNode) const |
| 147 | { |
| 148 | if (!m_children) |
| 149 | return notFound; |
| 150 | |
| 151 | return m_children->find(&childNode); |
| 152 | } |
| 153 | |
| 154 | void ScrollingStateNode::setLayer(const LayerRepresentation& layerRepresentation) |
| 155 | { |
| 156 | if (layerRepresentation == m_layer) |
| 157 | return; |
| 158 | |
| 159 | m_layer = layerRepresentation; |
| 160 | |
| 161 | setPropertyChanged(Layer); |
| 162 | } |
| 163 | |
| 164 | void ScrollingStateNode::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const |
| 165 | { |
| 166 | if (behavior & ScrollingStateTreeAsTextBehaviorIncludeNodeIDs) |
| 167 | ts.dumpProperty("nodeID" , scrollingNodeID()); |
| 168 | |
| 169 | if (behavior & ScrollingStateTreeAsTextBehaviorIncludeLayerIDs) |
| 170 | ts.dumpProperty("layerID" , layer().layerID()); |
| 171 | } |
| 172 | |
| 173 | void ScrollingStateNode::dump(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const |
| 174 | { |
| 175 | ts << "\n" ; |
| 176 | ts << indent << "(" ; |
| 177 | ts.increaseIndent(); |
| 178 | dumpProperties(ts, behavior); |
| 179 | |
| 180 | if (m_children) { |
| 181 | ts << "\n" ; |
| 182 | ts << indent <<"(" ; |
| 183 | { |
| 184 | TextStream::IndentScope indentScope(ts); |
| 185 | ts << "children " << children()->size(); |
| 186 | for (auto& child : *m_children) |
| 187 | child->dump(ts, behavior); |
| 188 | ts << "\n" ; |
| 189 | } |
| 190 | ts << indent << ")" ; |
| 191 | } |
| 192 | ts << "\n" ; |
| 193 | ts.decreaseIndent(); |
| 194 | ts << indent << ")" ; |
| 195 | } |
| 196 | |
| 197 | String ScrollingStateNode::scrollingStateTreeAsText(ScrollingStateTreeAsTextBehavior behavior) const |
| 198 | { |
| 199 | TextStream ts(TextStream::LineMode::MultipleLine, TextStream::Formatting::SVGStyleRect); |
| 200 | |
| 201 | dump(ts, behavior); |
| 202 | ts << "\n" ; |
| 203 | return ts.release(); |
| 204 | } |
| 205 | |
| 206 | } // namespace WebCore |
| 207 | |
| 208 | #endif // ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS) |
| 209 | |