ScrollingStateTree.cpp [plain text]
#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"
namespace WebCore {
PassOwnPtr<ScrollingStateTree> ScrollingStateTree::create(AsyncScrollingCoordinator* scrollingCoordinator)
{
return adoptPtr(new ScrollingStateTree(scrollingCoordinator));
}
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;
}
ScrollingNodeID ScrollingStateTree::attachNode(ScrollingNodeType nodeType, ScrollingNodeID newNodeID, ScrollingNodeID parentID)
{
ASSERT(newNodeID);
if (ScrollingStateNode* node = stateNodeForID(newNodeID)) {
if (!parentID)
return newNodeID;
ScrollingStateNode* parent = stateNodeForID(parentID);
if (!parent)
return newNodeID;
if (node->parent() == parent)
return newNodeID;
removeNodeAndAllDescendants(node, SubframeNodeRemoval::Orphan);
}
ScrollingStateNode* newNode = nullptr;
if (!parentID) {
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;
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();
}
PassOwnPtr<ScrollingStateTree> ScrollingStateTree::commit(LayerRepresentation::Type preferredLayerRepresentation)
{
if (!m_orphanedSubframeNodes.isEmpty()) {
for (auto& orphanNode : m_orphanedSubframeNodes.values())
recursiveNodeWillBeRemoved(orphanNode.get(), SubframeNodeRemoval::Delete);
m_orphanedSubframeNodes.clear();
}
OwnPtr<ScrollingStateTree> treeStateClone = ScrollingStateTree::create();
treeStateClone->setPreferredLayerRepresentation(preferredLayerRepresentation);
if (m_rootStateNode)
treeStateClone->setRootStateNode(static_pointer_cast<ScrollingStateFrameScrollingNode>(m_rootStateNode->cloneAndReset(*treeStateClone)));
treeStateClone->m_nodesRemovedSinceLastCommit.swap(m_nodesRemovedSinceLastCommit);
treeStateClone->m_hasChangedProperties = m_hasChangedProperties;
m_hasChangedProperties = false;
treeStateClone->m_hasNewRootStateNode = m_hasNewRootStateNode;
m_hasNewRootStateNode = false;
return treeStateClone.release();
}
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 = WTF::move(nodes);
}
ScrollingStateNode* ScrollingStateTree::stateNodeForID(ScrollingNodeID scrollLayerID)
{
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;
}
}
#endif // ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)