Title: [290411] trunk/Source/WebCore
Revision
290411
Author
cdu...@apple.com
Date
2022-02-23 19:18:27 -0800 (Wed, 23 Feb 2022)

Log Message

Modernize / simplify ScrollingStateTree a bit
https://bugs.webkit.org/show_bug.cgi?id=237111

Reviewed by Simon Fraser.

Modernize / simplify ScrollingStateTree a bit.

* page/scrolling/ScrollingStateTree.cpp:
(WebCore::nodeTypeAndParentMatch):
Simplify function body to be on one line and make function static since it doesn't
need an instance.

(WebCore::nodeWasReattachedRecursive):
Make function static since it doesn't need an instance.

(WebCore::ScrollingStateTree::createUnparentedNode):
- Pass a reference instead of pointer

(WebCore::ScrollingStateTree::insertNode):
- Use template deduction for Ref<>
- Drop unnecessarily 0-check for parentID since stateNodeForID(parentID) would have
  returned null and we would have returned early a few lines above.

(WebCore::ScrollingStateTree::unparentNode):
(WebCore::ScrollingStateTree::unparentChildrenAndDestroyNode):
- Use RefPtr type for protectedNode based on the variable naming. Previously, auto
  would have deduced to a raw pointer.

(WebCore::ScrollingStateTree::detachAndDestroySubtree):
(WebCore::ScrollingStateTree::clear):
Pass more references instead of pointers.

(WebCore::ScrollingStateTree::commit):
use auto and std::exchange() to make the code a bit more concise.

(WebCore::ScrollingStateTree::removeNodeAndAllDescendants):
Take a reference in parameter instead of a raw pointer since the pointer couldn't
be null.

(WebCore::ScrollingStateTree::recursiveNodeWillBeRemoved):
- Take a reference in parameter instead of a raw pointer since the pointer couldn't
be null.
- Rename currNode to currentNode per WebKit coding style.

(WebCore::ScrollingStateTree::willRemoveNode):
Take a reference in parameter instead of a raw pointer since the pointer couldn't
be null.

(WebCore::ScrollingStateTree::stateNodeForID const):
Rewrite function in a more concise way by calling HashMap::get() instead of HashMap::find().

(WebCore::reconcileLayerPositionsRecursive):
- Make function static since it doesn't need an instance
- Rename currNode to currentNode per WebKit coding style

(WebCore::ScrollingStateTree::reconcileViewportConstrainedLayerPositions):
Make function a bit more concise by not doing an early return.

(showScrollingStateTree):
Take in a reference instead of a raw pointer.

(WebCore::ScrollingStateTree::nodeTypeAndParentMatch const): Deleted.
(WebCore::ScrollingStateTree::nodeWasReattachedRecursive): Deleted.
(WebCore::ScrollingStateTree::reconcileLayerPositionsRecursive): Deleted.
* page/scrolling/ScrollingStateTree.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (290410 => 290411)


--- trunk/Source/WebCore/ChangeLog	2022-02-24 02:25:14 UTC (rev 290410)
+++ trunk/Source/WebCore/ChangeLog	2022-02-24 03:18:27 UTC (rev 290411)
@@ -1,3 +1,71 @@
+2022-02-23  Chris Dumez  <cdu...@apple.com>
+
+        Modernize / simplify ScrollingStateTree a bit
+        https://bugs.webkit.org/show_bug.cgi?id=237111
+
+        Reviewed by Simon Fraser.
+
+        Modernize / simplify ScrollingStateTree a bit.
+
+        * page/scrolling/ScrollingStateTree.cpp:
+        (WebCore::nodeTypeAndParentMatch):
+        Simplify function body to be on one line and make function static since it doesn't
+        need an instance.
+
+        (WebCore::nodeWasReattachedRecursive):
+        Make function static since it doesn't need an instance.
+
+        (WebCore::ScrollingStateTree::createUnparentedNode):
+        - Pass a reference instead of pointer
+
+        (WebCore::ScrollingStateTree::insertNode):
+        - Use template deduction for Ref<>
+        - Drop unnecessarily 0-check for parentID since stateNodeForID(parentID) would have
+          returned null and we would have returned early a few lines above.
+
+        (WebCore::ScrollingStateTree::unparentNode):
+        (WebCore::ScrollingStateTree::unparentChildrenAndDestroyNode):
+        - Use RefPtr type for protectedNode based on the variable naming. Previously, auto
+          would have deduced to a raw pointer.
+
+        (WebCore::ScrollingStateTree::detachAndDestroySubtree):
+        (WebCore::ScrollingStateTree::clear):
+        Pass more references instead of pointers.
+
+        (WebCore::ScrollingStateTree::commit):
+        use auto and std::exchange() to make the code a bit more concise.
+
+        (WebCore::ScrollingStateTree::removeNodeAndAllDescendants):
+        Take a reference in parameter instead of a raw pointer since the pointer couldn't
+        be null.
+
+        (WebCore::ScrollingStateTree::recursiveNodeWillBeRemoved):
+        - Take a reference in parameter instead of a raw pointer since the pointer couldn't
+        be null.
+        - Rename currNode to currentNode per WebKit coding style.
+
+        (WebCore::ScrollingStateTree::willRemoveNode):
+        Take a reference in parameter instead of a raw pointer since the pointer couldn't
+        be null.
+
+        (WebCore::ScrollingStateTree::stateNodeForID const):
+        Rewrite function in a more concise way by calling HashMap::get() instead of HashMap::find().
+
+        (WebCore::reconcileLayerPositionsRecursive):
+        - Make function static since it doesn't need an instance
+        - Rename currNode to currentNode per WebKit coding style
+
+        (WebCore::ScrollingStateTree::reconcileViewportConstrainedLayerPositions):
+        Make function a bit more concise by not doing an early return.
+
+        (showScrollingStateTree):
+        Take in a reference instead of a raw pointer.
+
+        (WebCore::ScrollingStateTree::nodeTypeAndParentMatch const): Deleted.
+        (WebCore::ScrollingStateTree::nodeWasReattachedRecursive): Deleted.
+        (WebCore::ScrollingStateTree::reconcileLayerPositionsRecursive): Deleted.
+        * page/scrolling/ScrollingStateTree.h:
+
 2022-02-23  Robert Jenner  <jen...@apple.com>
 
         Unreviewed, reverting r290351 and r290404.

Modified: trunk/Source/WebCore/page/scrolling/ScrollingStateTree.cpp (290410 => 290411)


--- trunk/Source/WebCore/page/scrolling/ScrollingStateTree.cpp	2022-02-24 02:25:14 UTC (rev 290410)
+++ trunk/Source/WebCore/page/scrolling/ScrollingStateTree.cpp	2022-02-24 03:18:27 UTC (rev 290411)
@@ -42,6 +42,22 @@
 
 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)
 {
@@ -86,14 +102,6 @@
     return ScrollingStateFixedNode::create(*this, nodeID);
 }
 
-bool ScrollingStateTree::nodeTypeAndParentMatch(ScrollingStateNode& node, ScrollingNodeType nodeType, ScrollingStateNode* parentNode) const
-{
-    if (node.nodeType() != nodeType)
-        return false;
-
-    return node.parent() == parentNode;
-}
-
 ScrollingNodeID ScrollingStateTree::createUnparentedNode(ScrollingNodeType nodeType, ScrollingNodeID newNodeID)
 {
     LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " createUnparentedNode " << newNodeID);
@@ -115,7 +123,7 @@
     }
 
     auto stateNode = createNode(nodeType, newNodeID);
-    addNode(stateNode.get());
+    addNode(stateNode);
     m_unparentedNodes.add(newNodeID, WTFMove(stateNode));
     return newNodeID;
 }
@@ -136,7 +144,7 @@
                 return newNodeID;
 
             ASSERT(currentIndex != notFound);
-            Ref<ScrollingStateNode> protectedNode(*node);
+            Ref protectedNode { *node };
             parent->removeChildAtIndex(currentIndex);
 
             if (childIndex == notFound)
@@ -174,17 +182,16 @@
             return 0;
         }
 
-        if (parentID) {
-            if (auto unparentedNode = m_unparentedNodes.take(newNodeID)) {
-                LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " insertNode reattaching node " << newNodeID);
-                newNode = unparentedNode.get();
-                nodeWasReattachedRecursive(*unparentedNode);
+        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 (childIndex == notFound)
+                parent->appendChild(unparentedNode.releaseNonNull());
+            else
+                parent->insertChild(unparentedNode.releaseNonNull(), childIndex);
         }
 
         if (!newNode) {
@@ -209,7 +216,7 @@
     LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " unparentNode " << nodeID);
 
     // The node may not be found if clear() was recently called.
-    auto protectedNode = m_stateNodeMap.get(nodeID);
+    RefPtr protectedNode = m_stateNodeMap.get(nodeID);
     if (!protectedNode)
         return;
 
@@ -228,7 +235,7 @@
     LOG_WITH_STREAM(ScrollingTree, stream << "ScrollingStateTree " << this << " unparentChildrenAndDestroyNode " << nodeID);
 
     // The node may not be found if clear() was recently called.
-    auto protectedNode = m_stateNodeMap.take(nodeID);
+    RefPtr protectedNode = m_stateNodeMap.take(nodeID);
     if (!protectedNode)
         return;
 
@@ -235,8 +242,7 @@
     if (protectedNode == m_rootStateNode)
         m_rootStateNode = nullptr;
 
-    if (protectedNode->children()) {
-        auto isolatedChildren = protectedNode->takeChildren();
+    if (auto isolatedChildren = protectedNode->takeChildren()) {
         for (auto child : *isolatedChildren) {
             child->removeFromParent();
             LOG_WITH_STREAM(ScrollingTree, stream << " moving " << child->scrollingNodeID() << " to unparented nodes");
@@ -245,7 +251,7 @@
     }
     
     protectedNode->removeFromParent();
-    willRemoveNode(protectedNode.get());
+    willRemoveNode(*protectedNode);
 }
 
 void ScrollingStateTree::detachAndDestroySubtree(ScrollingNodeID nodeID)
@@ -262,29 +268,18 @@
 
     // 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.get());
+    removeNodeAndAllDescendants(*node);
 }
 
 void ScrollingStateTree::clear()
 {
-    if (rootStateNode())
-        removeNodeAndAllDescendants(rootStateNode());
+    if (auto* node = rootStateNode())
+        removeNodeAndAllDescendants(*node);
 
     m_stateNodeMap.clear();
     m_unparentedNodes.clear();
 }
 
-void ScrollingStateTree::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);
-    }
-}
-
 std::unique_ptr<ScrollingStateTree> ScrollingStateTree::commit(LayerRepresentation::Type preferredLayerRepresentation)
 {
     if (!m_unparentedNodes.isEmpty()) {
@@ -293,7 +288,7 @@
     }
 
     // This function clones and resets the current state tree, but leaves the tree structure intact.
-    std::unique_ptr<ScrollingStateTree> treeStateClone = makeUnique<ScrollingStateTree>();
+    auto treeStateClone = makeUnique<ScrollingStateTree>();
     treeStateClone->setPreferredLayerRepresentation(preferredLayerRepresentation);
 
     if (m_rootStateNode)
@@ -300,12 +295,9 @@
         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 = m_hasChangedProperties;
-    m_hasChangedProperties = false;
+    treeStateClone->m_hasChangedProperties = std::exchange(m_hasChangedProperties, false);
+    treeStateClone->m_hasNewRootStateNode = std::exchange(m_hasNewRootStateNode, false);
 
-    treeStateClone->m_hasNewRootStateNode = m_hasNewRootStateNode;
-    m_hasNewRootStateNode = false;
-
     return treeStateClone;
 }
 
@@ -319,63 +311,56 @@
     m_stateNodeMap.add(node.scrollingNodeID(), &node);
 }
 
-void ScrollingStateTree::removeNodeAndAllDescendants(ScrollingStateNode* node)
+void ScrollingStateTree::removeNodeAndAllDescendants(ScrollingStateNode& node)
 {
-    auto* parent = node->parent();
+    auto* parent = node.parent();
 
     recursiveNodeWillBeRemoved(node);
 
-    if (node == m_rootStateNode)
+    if (&node == m_rootStateNode)
         m_rootStateNode = nullptr;
     else if (parent) {
         ASSERT(parent->children());
-        ASSERT(parent->children()->find(node) != notFound);
+        ASSERT(parent->children()->contains(&node));
         if (auto* children = parent->children()) {
-            size_t index = children->find(node);
-            if (index != notFound)
+            if (size_t index = children->find(&node); index != notFound)
                 children->remove(index);
         }
     }
 }
 
-void ScrollingStateTree::recursiveNodeWillBeRemoved(ScrollingStateNode* currNode)
+void ScrollingStateTree::recursiveNodeWillBeRemoved(ScrollingStateNode& currentNode)
 {
-    currNode->setParent(nullptr);
-    willRemoveNode(currNode);
+    currentNode.setParent(nullptr);
+    willRemoveNode(currentNode);
 
-    if (auto* children = currNode->children()) {
+    if (auto* children = currentNode.children()) {
         for (auto& child : *children)
-            recursiveNodeWillBeRemoved(child.get());
+            recursiveNodeWillBeRemoved(*child);
     }
 }
 
-void ScrollingStateTree::willRemoveNode(ScrollingStateNode* node)
+void ScrollingStateTree::willRemoveNode(ScrollingStateNode& node)
 {
-    m_stateNodeMap.remove(node->scrollingNodeID());
+    m_stateNodeMap.remove(node.scrollingNodeID());
     setHasChangedProperties();
 }
 
 ScrollingStateNode* ScrollingStateTree::stateNodeForID(ScrollingNodeID scrollLayerID) const
 {
-    if (!scrollLayerID)
-        return nullptr;
-
-    auto it = m_stateNodeMap.find(scrollLayerID);
-    if (it == m_stateNodeMap.end())
-        return nullptr;
-
-    ASSERT(it->value->scrollingNodeID() == scrollLayerID);
-    return it->value.get();
+    auto* node = scrollLayerID ? m_stateNodeMap.get(scrollLayerID) : nullptr;
+    ASSERT(!node || node->scrollingNodeID() == scrollLayerID);
+    return node;
 }
 
-void ScrollingStateTree::reconcileLayerPositionsRecursive(ScrollingStateNode& currNode, const LayoutRect& layoutViewport, ScrollingLayerPositionAction action)
+static void reconcileLayerPositionsRecursive(ScrollingStateNode& currentNode, const LayoutRect& layoutViewport, ScrollingLayerPositionAction action)
 {
-    currNode.reconcileLayerPositionForViewportRect(layoutViewport, action);
+    currentNode.reconcileLayerPositionForViewportRect(layoutViewport, action);
 
-    if (!currNode.children())
+    if (!currentNode.children())
         return;
     
-    for (auto& child : *currNode.children()) {
+    for (auto& child : *currentNode.children()) {
         // Never need to cross frame boundaries, since viewport rect reconciliation is per frame.
         if (is<ScrollingStateFrameScrollingNode>(child))
             continue;
@@ -386,24 +371,18 @@
 
 void ScrollingStateTree::reconcileViewportConstrainedLayerPositions(ScrollingNodeID scrollingNodeID, const LayoutRect& layoutViewport, ScrollingLayerPositionAction action)
 {
-    auto* scrollingNode = stateNodeForID(scrollingNodeID);
-    if (!scrollingNode)
-        return;
-    
-    reconcileLayerPositionsRecursive(*scrollingNode, layoutViewport, action);
+    if (auto* scrollingNode = stateNodeForID(scrollingNodeID))
+        reconcileLayerPositionsRecursive(*scrollingNode, layoutViewport, action);
 }
 
 } // namespace WebCore
 
 #ifndef NDEBUG
-void showScrollingStateTree(const WebCore::ScrollingStateTree* tree)
+void showScrollingStateTree(const WebCore::ScrollingStateTree& tree)
 {
-    if (!tree)
-        return;
-
-    auto rootNode = tree->rootStateNode();
+    auto rootNode = tree.rootStateNode();
     if (!rootNode) {
-        WTFLogAlways("Scrolling state tree %p with no root node\n", tree);
+        WTFLogAlways("Scrolling state tree %p with no root node\n", &tree);
         return;
     }
 
@@ -411,12 +390,9 @@
     WTFLogAlways("%s\n", output.utf8().data());
 }
 
-void showScrollingStateTree(const WebCore::ScrollingStateNode* node)
+void showScrollingStateTree(const WebCore::ScrollingStateNode& node)
 {
-    if (!node)
-        return;
-
-    showScrollingStateTree(&node->scrollingStateTree());
+    showScrollingStateTree(node.scrollingStateTree());
 }
 
 #endif

Modified: trunk/Source/WebCore/page/scrolling/ScrollingStateTree.h (290410 => 290411)


--- trunk/Source/WebCore/page/scrolling/ScrollingStateTree.h	2022-02-24 02:25:14 UTC (rev 290410)
+++ trunk/Source/WebCore/page/scrolling/ScrollingStateTree.h	2022-02-24 03:18:27 UTC (rev 290411)
@@ -70,7 +70,7 @@
     unsigned nodeCount() const { return m_stateNodeMap.size(); }
     unsigned scrollingNodeCount() const { return m_scrollingNodeCount; }
 
-    typedef HashMap<ScrollingNodeID, RefPtr<ScrollingStateNode>> StateNodeMap;
+    using StateNodeMap = HashMap<ScrollingNodeID, RefPtr<ScrollingStateNode>>;
     const StateNodeMap& nodeMap() const { return m_stateNodeMap; }
 
     LayerRepresentation::Type preferredLayerRepresentation() const { return m_preferredLayerRepresentation; }
@@ -85,24 +85,17 @@
         --m_scrollingNodeCount;
     }
 
-
 private:
     void setRootStateNode(Ref<ScrollingStateFrameScrollingNode>&&);
     void addNode(ScrollingStateNode&);
 
-    void nodeWasReattachedRecursive(ScrollingStateNode&);
-
     Ref<ScrollingStateNode> createNode(ScrollingNodeType, ScrollingNodeID);
 
-    bool nodeTypeAndParentMatch(ScrollingStateNode&, ScrollingNodeType, ScrollingStateNode* parentNode) const;
+    void removeNodeAndAllDescendants(ScrollingStateNode&);
 
-    void removeNodeAndAllDescendants(ScrollingStateNode*);
+    void recursiveNodeWillBeRemoved(ScrollingStateNode&);
+    void willRemoveNode(ScrollingStateNode&);
 
-    void recursiveNodeWillBeRemoved(ScrollingStateNode*);
-    void willRemoveNode(ScrollingStateNode*);
-
-    void reconcileLayerPositionsRecursive(ScrollingStateNode&, const LayoutRect& viewportRect, ScrollingLayerPositionAction);
-
     AsyncScrollingCoordinator* m_scrollingCoordinator;
     // Contains all the nodes we know about (those in the m_rootStateNode tree, and in m_unparentedNodes subtrees).
     StateNodeMap m_stateNodeMap;
@@ -110,17 +103,17 @@
     HashMap<ScrollingNodeID, RefPtr<ScrollingStateNode>> m_unparentedNodes;
 
     RefPtr<ScrollingStateFrameScrollingNode> m_rootStateNode;
+    unsigned m_scrollingNodeCount { 0 };
+    LayerRepresentation::Type m_preferredLayerRepresentation { LayerRepresentation::GraphicsLayerRepresentation };
     bool m_hasChangedProperties { false };
     bool m_hasNewRootStateNode { false };
-    unsigned m_scrollingNodeCount { 0 };
-    LayerRepresentation::Type m_preferredLayerRepresentation { LayerRepresentation::GraphicsLayerRepresentation };
 };
 
 } // namespace WebCore
 
 #ifndef NDEBUG
-void showScrollingStateTree(const WebCore::ScrollingStateTree*);
-void showScrollingStateTree(const WebCore::ScrollingStateNode*);
+void showScrollingStateTree(const WebCore::ScrollingStateTree&);
+void showScrollingStateTree(const WebCore::ScrollingStateNode&);
 #endif
 
 #endif // ENABLE(ASYNC_SCROLLING)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to