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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
/*
* 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)
#include "AsyncScrollingCoordinator.h"
#include "Logging.h"
#include "ScrollingStateFixedNode.h"
#include "ScrollingStateFrameHostingNode.h"
#include "ScrollingStateFrameScrollingNode.h"
#include "ScrollingStateOverflowScrollProxyNode.h"
#include "ScrollingStateOverflowScrollingNode.h"
#include "ScrollingStatePositionedNode.h"
#include "ScrollingStateStickyNode.h"
#include <wtf/text/CString.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
static bool nodeTypeAndParentMatch(ScrollingStateNode& node, ScrollingNodeType nodeType, ScrollingStateNode* parentNode)
{
return node.nodeType() == nodeType && node.parent() == parentNode;
}
static void nodeWasReattachedRecursive(ScrollingStateNode& node)
{
// When a node is re-attached, the ScrollingTree is recreating the ScrollingNode from scratch, so we need to set all the dirty bits.
node.setPropertyChangesAfterReattach();
if (auto* children = node.children()) {
for (auto& child : *children)
nodeWasReattachedRecursive(*child);
}
}
ScrollingStateTree::ScrollingStateTree(AsyncScrollingCoordinator* scrollingCoordinator)
: m_scrollingCoordinator(scrollingCoordinator)
{
}
ScrollingStateTree::~ScrollingStateTree() = default;
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
}
Ref<ScrollingStateNode> ScrollingStateTree::createNode(ScrollingNodeType nodeType, ScrollingNodeID nodeID)
{
switch (nodeType) {
case ScrollingNodeType::MainFrame:
case ScrollingNodeType::Subframe:
return ScrollingStateFrameScrollingNode::create(*this, nodeType, nodeID);
case ScrollingNodeType::FrameHosting:
return ScrollingStateFrameHostingNode::create(*this, nodeID);
case ScrollingNodeType::Overflow:
return ScrollingStateOverflowScrollingNode::create(*this, nodeID);
case ScrollingNodeType::OverflowProxy:
return ScrollingStateOverflowScrollProxyNode::create(*this, nodeID);
case ScrollingNodeType::Fixed:
return ScrollingStateFixedNode::create(*this, nodeID);
case ScrollingNodeType::Sticky:
return ScrollingStateStickyNode::create(*this, nodeID);
case ScrollingNodeType::Positioned:
return ScrollingStatePositionedNode::create(*this, nodeID);
}
ASSERT_NOT_REACHED();
return ScrollingStateFixedNode::create(*this, nodeID);
}
ScrollingNodeID ScrollingStateTree::createUnparentedNode(ScrollingNodeType nodeType, ScrollingNodeID newNodeID)
{
LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " createUnparentedNode " << newNodeID);
if (auto node = stateNodeForID(newNodeID)) {
if (node->nodeType() == nodeType) {
// If the node exists and is parented, unparent it. It may already be in m_unparentedNodes.
unparentNode(newNodeID);
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()) {
unparentChildrenAndDestroyNode(newNodeID);
newNodeID = m_scrollingCoordinator->uniqueScrollingNodeID();
}
#endif
}
auto stateNode = createNode(nodeType, newNodeID);
addNode(stateNode);
m_unparentedNodes.add(newNodeID, WTFMove(stateNode));
return newNodeID;
}
ScrollingNodeID ScrollingStateTree::insertNode(ScrollingNodeType nodeType, ScrollingNodeID newNodeID, ScrollingNodeID parentID, size_t childIndex)
{
LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " insertNode " << newNodeID << " in parent " << parentID << " at " << childIndex);
ASSERT(newNodeID);
if (auto node = stateNodeForID(newNodeID)) {
auto parent = stateNodeForID(parentID);
if (nodeTypeAndParentMatch(*node, nodeType, parent.get())) {
if (!parentID)
return newNodeID;
size_t currentIndex = parent->indexOfChild(*node);
if (currentIndex == childIndex)
return newNodeID;
ASSERT(currentIndex != notFound);
Ref protectedNode { *node };
parent->removeChildAtIndex(currentIndex);
if (childIndex == notFound)
parent->appendChild(WTFMove(protectedNode));
else
parent->insertChild(WTFMove(protectedNode), childIndex);
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->uniqueScrollingNodeID();
#endif
// The node is being re-parented. To do that, we'll remove it, and then create a new node.
unparentNode(newNodeID);
}
ScrollingStateNode* newNode = nullptr;
if (!parentID) {
RELEASE_ASSERT(nodeType == ScrollingNodeType::MainFrame);
ASSERT(!childIndex || childIndex == notFound);
// If we're resetting the root node, we should clear the HashMap and destroy the current children.
clear();
setRootStateNode(ScrollingStateFrameScrollingNode::create(*this, ScrollingNodeType::MainFrame, newNodeID));
newNode = rootStateNode();
m_hasNewRootStateNode = true;
} else {
auto parent = stateNodeForID(parentID);
if (!parent) {
ASSERT_NOT_REACHED();
return 0;
}
ASSERT(parentID);
if (auto unparentedNode = m_unparentedNodes.take(newNodeID)) {
LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " insertNode reattaching node " << newNodeID);
newNode = unparentedNode.get();
nodeWasReattachedRecursive(*unparentedNode);
if (childIndex == notFound)
parent->appendChild(unparentedNode.releaseNonNull());
else
parent->insertChild(unparentedNode.releaseNonNull(), childIndex);
}
if (!newNode) {
auto stateNode = createNode(nodeType, newNodeID);
newNode = stateNode.ptr();
if (childIndex == notFound)
parent->appendChild(WTFMove(stateNode));
else
parent->insertChild(WTFMove(stateNode), childIndex);
}
}
addNode(*newNode);
return newNodeID;
}
void ScrollingStateTree::unparentNode(ScrollingNodeID nodeID)
{
if (!nodeID)
return;
LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " unparentNode " << nodeID);
// The node may not be found if clear() was recently called.
RefPtr protectedNode = m_stateNodeMap.get(nodeID);
if (!protectedNode)
return;
if (protectedNode == m_rootStateNode)
m_rootStateNode = nullptr;
protectedNode->removeFromParent();
m_unparentedNodes.add(nodeID, WTFMove(protectedNode));
}
void ScrollingStateTree::unparentChildrenAndDestroyNode(ScrollingNodeID nodeID)
{
if (!nodeID)
return;
LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " unparentChildrenAndDestroyNode " << nodeID);
// The node may not be found if clear() was recently called.
RefPtr protectedNode = m_stateNodeMap.take(nodeID);
if (!protectedNode)
return;
if (protectedNode == m_rootStateNode)
m_rootStateNode = nullptr;
if (auto isolatedChildren = protectedNode->takeChildren()) {
for (auto child : *isolatedChildren) {
child->removeFromParent();
LOG_WITH_STREAM(ScrollingTree, stream << " moving " << child->scrollingNodeID() << " to unparented nodes");
m_unparentedNodes.add(child->scrollingNodeID(), WTFMove(child));
}
}
protectedNode->removeFromParent();
m_unparentedNodes.remove(nodeID);
willRemoveNode(*protectedNode);
}
void ScrollingStateTree::detachAndDestroySubtree(ScrollingNodeID nodeID)
{
if (!nodeID)
return;
LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " detachAndDestroySubtree " << nodeID);
// The node may not be found if clear() was recently called.
auto node = m_stateNodeMap.take(nodeID);
if (!node)
return;
// If the node was unparented, remove it from m_unparentedNodes (keeping it alive until this function returns).
auto unparentedNode = m_unparentedNodes.take(nodeID);
removeNodeAndAllDescendants(*node);
}
void ScrollingStateTree::clear()
{
if (auto* node = rootStateNode())
removeNodeAndAllDescendants(*node);
m_stateNodeMap.clear();
m_unparentedNodes.clear();
}
std::unique_ptr<ScrollingStateTree> ScrollingStateTree::commit(LayerRepresentation::Type preferredLayerRepresentation)
{
ASSERT(isValid());
if (!m_unparentedNodes.isEmpty()) {
// We expect temporarily to have unparented nodes when committing when connecting across iframe boundaries, but unparented nodes should not stick around for a long time.
LOG(ScrollingTree, "Committing with %u unparented nodes", m_unparentedNodes.size());
}
// This function clones and resets the current state tree, but leaves the tree structure intact.
auto treeStateClone = makeUnique<ScrollingStateTree>();
treeStateClone->setPreferredLayerRepresentation(preferredLayerRepresentation);
if (m_rootStateNode)
treeStateClone->setRootStateNode(static_reference_cast<ScrollingStateFrameScrollingNode>(m_rootStateNode->cloneAndReset(*treeStateClone)));
// Now the clone tree has changed properties, and the original tree does not.
treeStateClone->m_hasChangedProperties = std::exchange(m_hasChangedProperties, false);
treeStateClone->m_hasNewRootStateNode = std::exchange(m_hasNewRootStateNode, false);
return treeStateClone;
}
void ScrollingStateTree::traverse(const ScrollingStateNode& node, const Function<void(const ScrollingStateNode&)>& traversalFunc) const
{
traversalFunc(node);
if (auto* children = node.children()) {
for (auto childNode : *children) {
ASSERT(childNode);
traverse(*childNode, traversalFunc);
}
}
}
bool ScrollingStateTree::isValid() const
{
if (!m_rootStateNode)
return true;
bool isValid = true;
traverse(*m_rootStateNode, [&](const ScrollingStateNode& node) {
switch (node.nodeType()) {
case ScrollingNodeType::MainFrame:
break;
case ScrollingNodeType::Subframe:
break;
case ScrollingNodeType::FrameHosting:
break;
case ScrollingNodeType::Overflow:
break;
case ScrollingNodeType::OverflowProxy: {
auto& proxyNode = downcast<ScrollingStateOverflowScrollProxyNode>(node);
if (!proxyNode.overflowScrollingNode() || !nodeMap().contains(proxyNode.overflowScrollingNode())) {
ALWAYS_LOG_WITH_STREAM(stream << "ScrollingStateOverflowScrollProxyNode " << node.scrollingNodeID() << " refers to non-existant overflow node " << proxyNode.overflowScrollingNode());
isValid = false;
}
break;
}
case ScrollingNodeType::Fixed:
break;
case ScrollingNodeType::Sticky:
break;
case ScrollingNodeType::Positioned: {
auto& positionedNode = downcast<ScrollingStatePositionedNode>(node);
for (auto relatedNodeID : positionedNode.relatedOverflowScrollingNodes()) {
if (!relatedNodeID || !nodeMap().contains(relatedNodeID)) {
ALWAYS_LOG_WITH_STREAM(stream << "ScrollingStatePositionedNode " << node.scrollingNodeID() << " refers to non-existant overflow node " << relatedNodeID);
isValid = false;
}
}
break;
}
}
});
return isValid;
}
void ScrollingStateTree::setRootStateNode(Ref<ScrollingStateFrameScrollingNode>&& rootStateNode)
{
m_rootStateNode = WTFMove(rootStateNode);
}
void ScrollingStateTree::addNode(ScrollingStateNode& node)
{
m_stateNodeMap.add(node.scrollingNodeID(), &node);
}
void ScrollingStateTree::removeNodeAndAllDescendants(ScrollingStateNode& node)
{
auto parent = node.parent();
recursiveNodeWillBeRemoved(node);
if (&node == m_rootStateNode)
m_rootStateNode = nullptr;
else if (parent) {
ASSERT(parent->children());
ASSERT(parent->children()->contains(&node));
if (auto* children = parent->children()) {
if (size_t index = children->find(&node); index != notFound)
children->remove(index);
}
}
}
void ScrollingStateTree::recursiveNodeWillBeRemoved(ScrollingStateNode& currentNode)
{
currentNode.setParent(nullptr);
willRemoveNode(currentNode);
if (auto* children = currentNode.children()) {
for (auto& child : *children)
recursiveNodeWillBeRemoved(*child);
}
}
void ScrollingStateTree::willRemoveNode(ScrollingStateNode& node)
{
m_stateNodeMap.remove(node.scrollingNodeID());
setHasChangedProperties();
}
RefPtr<ScrollingStateNode> ScrollingStateTree::stateNodeForID(ScrollingNodeID nodeID) const
{
RefPtr node = nodeID ? m_stateNodeMap.get(nodeID) : nullptr;
RELEASE_ASSERT(!node || node->scrollingNodeID() == nodeID);
return node;
}
static void reconcileLayerPositionsRecursive(ScrollingStateNode& currentNode, const LayoutRect& layoutViewport, ScrollingLayerPositionAction action)
{
currentNode.reconcileLayerPositionForViewportRect(layoutViewport, action);
if (!currentNode.children())
return;
for (auto& child : *currentNode.children()) {
// Never need to cross frame boundaries, since viewport rect reconciliation is per frame.
if (is<ScrollingStateFrameScrollingNode>(child))
continue;
reconcileLayerPositionsRecursive(*child, layoutViewport, action);
}
}
void ScrollingStateTree::reconcileViewportConstrainedLayerPositions(ScrollingNodeID scrollingNodeID, const LayoutRect& layoutViewport, ScrollingLayerPositionAction action)
{
if (auto scrollingNode = stateNodeForID(scrollingNodeID))
reconcileLayerPositionsRecursive(*scrollingNode, layoutViewport, action);
}
String ScrollingStateTree::scrollingStateTreeAsText(OptionSet<ScrollingStateTreeAsTextBehavior> behavior) const
{
if (!rootStateNode())
return emptyString();
StringBuilder stateTreeAsString;
stateTreeAsString.append(rootStateNode()->scrollingStateTreeAsText(behavior));
if (!m_unparentedNodes.isEmpty())
stateTreeAsString.append("\nunparented node count: ", m_unparentedNodes.size());
return stateTreeAsString.toString();
}
} // namespace WebCore
#ifndef NDEBUG
void showScrollingStateTree(const WebCore::ScrollingStateTree& tree)
{
auto rootNode = tree.rootStateNode();
if (!rootNode) {
WTFLogAlways("Scrolling state tree %p with no root node\n", &tree);
return;
}
String output = rootNode->scrollingStateTreeAsText(WebCore::debugScrollingStateTreeAsTextBehaviors);
WTFLogAlways("%s\n", output.utf8().data());
}
void showScrollingStateTree(const WebCore::ScrollingStateNode& node)
{
showScrollingStateTree(node.scrollingStateTree());
}
#endif
#endif // ENABLE(ASYNC_SCROLLING)
|