1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
* Copyright (C) 2012 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ScrollingStateTree.h"
#if ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
#include "AsyncScrollingCoordinator.h"
#include "ScrollingStateFixedNode.h"
#include "ScrollingStateFrameScrollingNode.h"
#include "ScrollingStateOverflowScrollingNode.h"
#include "ScrollingStateStickyNode.h"
#include <wtf/text/CString.h>
#ifndef NDEBUG
#include <stdio.h>
#endif
namespace WebCore {
ScrollingStateTree::ScrollingStateTree(AsyncScrollingCoordinator* scrollingCoordinator)
: m_scrollingCoordinator(scrollingCoordinator)
, m_hasChangedProperties(false)
, m_hasNewRootStateNode(false)
, m_preferredLayerRepresentation(LayerRepresentation::GraphicsLayerRepresentation)
{
}
ScrollingStateTree::~ScrollingStateTree()
{
}
void ScrollingStateTree::setHasChangedProperties(bool changedProperties)
{
#if ENABLE(ASYNC_SCROLLING)
bool gainedChangedProperties = !m_hasChangedProperties && changedProperties;
#endif
m_hasChangedProperties = changedProperties;
#if ENABLE(ASYNC_SCROLLING)
if (gainedChangedProperties && m_scrollingCoordinator)
m_scrollingCoordinator->scrollingStateTreePropertiesChanged();
#endif
}
PassRefPtr<ScrollingStateNode> ScrollingStateTree::createNode(ScrollingNodeType nodeType, ScrollingNodeID nodeID)
{
switch (nodeType) {
case FixedNode:
return ScrollingStateFixedNode::create(*this, nodeID);
case StickyNode:
return ScrollingStateStickyNode::create(*this, nodeID);
case FrameScrollingNode:
return ScrollingStateFrameScrollingNode::create(*this, nodeID);
case OverflowScrollingNode:
return ScrollingStateOverflowScrollingNode::create(*this, nodeID);
}
ASSERT_NOT_REACHED();
return nullptr;
}
bool ScrollingStateTree::nodeTypeAndParentMatch(ScrollingStateNode& node, ScrollingNodeType nodeType, ScrollingNodeID parentID) const
{
if (node.nodeType() != nodeType)
return false;
ScrollingStateNode* parent = stateNodeForID(parentID);
if (!parent)
return true;
return node.parent() == parent;
}
ScrollingNodeID ScrollingStateTree::attachNode(ScrollingNodeType nodeType, ScrollingNodeID newNodeID, ScrollingNodeID parentID)
{
ASSERT(newNodeID);
if (ScrollingStateNode* node = stateNodeForID(newNodeID)) {
if (nodeTypeAndParentMatch(*node, nodeType, parentID))
return newNodeID;
#if ENABLE(ASYNC_SCROLLING)
// If the type has changed, we need to destroy and recreate the node with a new ID.
if (nodeType != node->nodeType())
newNodeID = m_scrollingCoordinator->uniqueScrollLayerID();
#endif
// The node is being re-parented. To do that, we'll remove it, and then create a new node.
removeNodeAndAllDescendants(node, SubframeNodeRemoval::Orphan);
}
ScrollingStateNode* newNode = nullptr;
if (!parentID) {
// If we're resetting the root node, we should clear the HashMap and destroy the current children.
clear();
setRootStateNode(ScrollingStateFrameScrollingNode::create(*this, newNodeID));
newNode = rootStateNode();
m_hasNewRootStateNode = true;
} else {
ScrollingStateNode* parent = stateNodeForID(parentID);
if (!parent)
return 0;
if (nodeType == FrameScrollingNode && parentID) {
if (RefPtr<ScrollingStateNode> orphanedNode = m_orphanedSubframeNodes.take(newNodeID)) {
newNode = orphanedNode.get();
parent->appendChild(orphanedNode.release());
}
}
if (!newNode) {
RefPtr<ScrollingStateNode> stateNode = createNode(nodeType, newNodeID);
newNode = stateNode.get();
parent->appendChild(stateNode.release());
}
}
m_stateNodeMap.set(newNodeID, newNode);
m_nodesRemovedSinceLastCommit.remove(newNodeID);
return newNodeID;
}
void ScrollingStateTree::detachNode(ScrollingNodeID nodeID)
{
if (!nodeID)
return;
// The node may not be found if clearStateTree() was recently called.
ScrollingStateNode* node = m_stateNodeMap.take(nodeID);
if (!node)
return;
removeNodeAndAllDescendants(node, SubframeNodeRemoval::Orphan);
}
void ScrollingStateTree::clear()
{
if (rootStateNode())
removeNodeAndAllDescendants(rootStateNode());
m_stateNodeMap.clear();
m_orphanedSubframeNodes.clear();
}
std::unique_ptr<ScrollingStateTree> ScrollingStateTree::commit(LayerRepresentation::Type preferredLayerRepresentation)
{
if (!m_orphanedSubframeNodes.isEmpty()) {
// If we still have orphaned subtrees, remove them from m_stateNodeMap since they will be deleted
// when clearing m_orphanedSubframeNodes.
for (auto& orphanNode : m_orphanedSubframeNodes.values())
recursiveNodeWillBeRemoved(orphanNode.get(), SubframeNodeRemoval::Delete);
m_orphanedSubframeNodes.clear();
}
// This function clones and resets the current state tree, but leaves the tree structure intact.
std::unique_ptr<ScrollingStateTree> treeStateClone = std::make_unique<ScrollingStateTree>();
treeStateClone->setPreferredLayerRepresentation(preferredLayerRepresentation);
if (m_rootStateNode)
treeStateClone->setRootStateNode(static_pointer_cast<ScrollingStateFrameScrollingNode>(m_rootStateNode->cloneAndReset(*treeStateClone)));
// Copy the IDs of the nodes that have been removed since the last commit into the clone.
treeStateClone->m_nodesRemovedSinceLastCommit.swap(m_nodesRemovedSinceLastCommit);
// Now the clone tree has changed properties, and the original tree does not.
treeStateClone->m_hasChangedProperties = m_hasChangedProperties;
m_hasChangedProperties = false;
treeStateClone->m_hasNewRootStateNode = m_hasNewRootStateNode;
m_hasNewRootStateNode = false;
return treeStateClone;
}
void ScrollingStateTree::addNode(ScrollingStateNode* node)
{
m_stateNodeMap.add(node->scrollingNodeID(), node);
}
void ScrollingStateTree::removeNodeAndAllDescendants(ScrollingStateNode* node, SubframeNodeRemoval subframeNodeRemoval)
{
ScrollingStateNode* parent = node->parent();
recursiveNodeWillBeRemoved(node, subframeNodeRemoval);
if (node == m_rootStateNode)
m_rootStateNode = nullptr;
else if (parent) {
ASSERT(parent->children() && parent->children()->find(node) != notFound);
if (auto children = parent->children()) {
size_t index = children->find(node);
if (index != notFound)
children->remove(index);
}
}
}
void ScrollingStateTree::recursiveNodeWillBeRemoved(ScrollingStateNode* currNode, SubframeNodeRemoval subframeNodeRemoval)
{
currNode->setParent(nullptr);
if (subframeNodeRemoval == SubframeNodeRemoval::Orphan && currNode != m_rootStateNode && currNode->isFrameScrollingNode()) {
m_orphanedSubframeNodes.add(currNode->scrollingNodeID(), currNode);
return;
}
willRemoveNode(currNode);
if (auto children = currNode->children()) {
for (auto& child : *children)
recursiveNodeWillBeRemoved(child.get(), subframeNodeRemoval);
}
}
void ScrollingStateTree::willRemoveNode(ScrollingStateNode* node)
{
m_nodesRemovedSinceLastCommit.add(node->scrollingNodeID());
m_stateNodeMap.remove(node->scrollingNodeID());
setHasChangedProperties();
}
void ScrollingStateTree::setRemovedNodes(HashSet<ScrollingNodeID> nodes)
{
m_nodesRemovedSinceLastCommit = WTFMove(nodes);
}
ScrollingStateNode* ScrollingStateTree::stateNodeForID(ScrollingNodeID scrollLayerID) const
{
if (!scrollLayerID)
return 0;
auto it = m_stateNodeMap.find(scrollLayerID);
if (it == m_stateNodeMap.end())
return 0;
ASSERT(it->value->scrollingNodeID() == scrollLayerID);
return it->value;
}
} // namespace WebCore
#ifndef NDEBUG
void showScrollingStateTree(const WebCore::ScrollingStateTree* tree)
{
if (!tree)
return;
auto rootNode = tree->rootStateNode();
if (!rootNode) {
fprintf(stderr, "Scrolling state tree %p with no root node\n", tree);
return;
}
String output = rootNode->scrollingStateTreeAsText();
fprintf(stderr, "%s\n", output.utf8().data());
}
void showScrollingStateTree(const WebCore::ScrollingStateNode* node)
{
if (!node)
return;
showScrollingStateTree(&node->scrollingStateTree());
}
#endif
#endif // ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
|