Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / page / scrolling / ScrollingStateNode.cpp
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_changedProperties(0)
43     , m_scrollingStateTree(scrollingStateTree)
44     , m_parent(nullptr)
45 {
46 }
47
48 // This copy constructor is used for cloning nodes in the tree, and it doesn't make sense
49 // to clone the relationship pointers, so don't copy that information from the original node.
50 ScrollingStateNode::ScrollingStateNode(const ScrollingStateNode& stateNode, ScrollingStateTree& adoptiveTree)
51     : m_nodeType(stateNode.nodeType())
52     , m_nodeID(stateNode.scrollingNodeID())
53     , m_changedProperties(stateNode.changedProperties())
54     , m_scrollingStateTree(adoptiveTree)
55     , m_parent(nullptr)
56 {
57     if (hasChangedProperty(ScrollLayer))
58         setLayer(stateNode.layer().toRepresentation(adoptiveTree.preferredLayerRepresentation()));
59     scrollingStateTree().addNode(this);
60 }
61
62 ScrollingStateNode::~ScrollingStateNode() = default;
63
64 void ScrollingStateNode::setPropertyChanged(unsigned propertyBit)
65 {
66     if (hasChangedProperty(propertyBit))
67         return;
68
69     m_changedProperties |= (static_cast<ChangedProperties>(1) << propertyBit);
70     m_scrollingStateTree.setHasChangedProperties();
71 }
72
73 Ref<ScrollingStateNode> ScrollingStateNode::cloneAndReset(ScrollingStateTree& adoptiveTree)
74 {
75     auto clone = this->clone(adoptiveTree);
76
77     // Now that this node is cloned, reset our change properties.
78     resetChangedProperties();
79
80     cloneAndResetChildren(clone.get(), adoptiveTree);
81
82     return clone;
83 }
84
85 void ScrollingStateNode::cloneAndResetChildren(ScrollingStateNode& clone, ScrollingStateTree& adoptiveTree)
86 {
87     if (!m_children)
88         return;
89
90     for (auto& child : *m_children)
91         clone.appendChild(child->cloneAndReset(adoptiveTree));
92 }
93
94 void ScrollingStateNode::appendChild(Ref<ScrollingStateNode>&& childNode)
95 {
96     childNode->setParent(this);
97
98     if (!m_children)
99         m_children = std::make_unique<Vector<RefPtr<ScrollingStateNode>>>();
100     m_children->append(WTFMove(childNode));
101 }
102
103 void ScrollingStateNode::setLayer(const LayerRepresentation& layerRepresentation)
104 {
105     if (layerRepresentation == m_layer)
106         return;
107     
108     m_layer = layerRepresentation;
109
110     setPropertyChanged(ScrollLayer);
111 }
112
113 void ScrollingStateNode::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
114 {
115     if (behavior & ScrollingStateTreeAsTextBehaviorIncludeNodeIDs)
116         ts.dumpProperty("nodeID", scrollingNodeID());
117     
118     if (behavior & ScrollingStateTreeAsTextBehaviorIncludeLayerIDs)
119         ts.dumpProperty("layerID", layer().layerID());
120 }
121
122 void ScrollingStateNode::dump(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
123 {
124     ts << "\n";
125     ts.writeIndent();
126     ts << "(";
127     ts.increaseIndent();
128     dumpProperties(ts, behavior);
129
130     if (m_children) {
131         ts << "\n";
132         ts.writeIndent();
133         ts << "(";
134         ts.increaseIndent();
135         ts << "children " << children()->size();
136         for (auto& child : *m_children)
137             child->dump(ts, behavior);
138         ts << "\n";
139         ts.decreaseIndent();
140         ts.writeIndent();
141         ts << ")";
142     }
143     ts << "\n";
144     ts.decreaseIndent();
145     ts.writeIndent();
146     ts << ")";
147 }
148
149 String ScrollingStateNode::scrollingStateTreeAsText(ScrollingStateTreeAsTextBehavior behavior) const
150 {
151     TextStream ts(TextStream::LineMode::MultipleLine, TextStream::Formatting::SVGStyleRect);
152
153     dump(ts, behavior);
154     ts << "\n";
155     return ts.release();
156 }
157
158 } // namespace WebCore
159
160 #endif // ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)