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#pragma once
27
28#if ENABLE(ASYNC_SCROLLING)
29
30#include "ScrollingCoordinator.h"
31#include "ScrollingStateNode.h"
32#include <wtf/RefPtr.h>
33
34namespace WebCore {
35
36class AsyncScrollingCoordinator;
37class ScrollingStateFrameScrollingNode;
38
39// The ScrollingStateTree is a tree that managed ScrollingStateNodes. The nodes keep track of the current
40// state of scrolling related properties. Whenever any properties change, the scrolling coordinator
41// will be informed and will schedule a timer that will clone the new state tree and send it over to
42// the scrolling thread, avoiding locking.
43
44class ScrollingStateTree {
45 WTF_MAKE_FAST_ALLOCATED;
46 friend class ScrollingStateNode;
47public:
48 WEBCORE_EXPORT ScrollingStateTree(AsyncScrollingCoordinator* = nullptr);
49 WEBCORE_EXPORT ~ScrollingStateTree();
50
51 ScrollingStateFrameScrollingNode* rootStateNode() const { return m_rootStateNode.get(); }
52 WEBCORE_EXPORT ScrollingStateNode* stateNodeForID(ScrollingNodeID) const;
53
54 ScrollingNodeID createUnparentedNode(ScrollingNodeType, ScrollingNodeID);
55 WEBCORE_EXPORT ScrollingNodeID insertNode(ScrollingNodeType, ScrollingNodeID, ScrollingNodeID parentID, size_t childIndex);
56 void unparentNode(ScrollingNodeID);
57 void unparentChildrenAndDestroyNode(ScrollingNodeID);
58 void detachAndDestroySubtree(ScrollingNodeID);
59 void clear();
60
61 // Copies the current tree state and clears the changed properties mask in the original.
62 WEBCORE_EXPORT std::unique_ptr<ScrollingStateTree> commit(LayerRepresentation::Type preferredLayerRepresentation);
63
64 WEBCORE_EXPORT void setHasChangedProperties(bool = true);
65 bool hasChangedProperties() const { return m_hasChangedProperties; }
66
67 bool hasNewRootStateNode() const { return m_hasNewRootStateNode; }
68 void setHasNewRootStateNode(bool hasNewRoot) { m_hasNewRootStateNode = hasNewRoot; }
69
70 unsigned nodeCount() const { return m_stateNodeMap.size(); }
71
72 typedef HashMap<ScrollingNodeID, ScrollingStateNode*> StateNodeMap;
73 const StateNodeMap& nodeMap() const { return m_stateNodeMap; }
74
75 LayerRepresentation::Type preferredLayerRepresentation() const { return m_preferredLayerRepresentation; }
76 void setPreferredLayerRepresentation(LayerRepresentation::Type representation) { m_preferredLayerRepresentation = representation; }
77
78 void reconcileViewportConstrainedLayerPositions(ScrollingNodeID, const LayoutRect& viewportRect, ScrollingLayerPositionAction);
79
80private:
81 void setRootStateNode(Ref<ScrollingStateFrameScrollingNode>&&);
82 void addNode(ScrollingStateNode&);
83
84 void nodeWasReattachedRecursive(ScrollingStateNode&);
85
86 Ref<ScrollingStateNode> createNode(ScrollingNodeType, ScrollingNodeID);
87
88 bool nodeTypeAndParentMatch(ScrollingStateNode&, ScrollingNodeType, ScrollingStateNode* parentNode) const;
89
90 void removeNodeAndAllDescendants(ScrollingStateNode*);
91
92 void recursiveNodeWillBeRemoved(ScrollingStateNode*);
93 void willRemoveNode(ScrollingStateNode*);
94
95 void reconcileLayerPositionsRecursive(ScrollingStateNode&, const LayoutRect& viewportRect, ScrollingLayerPositionAction);
96
97 AsyncScrollingCoordinator* m_scrollingCoordinator;
98 // Contains all the nodes we know about (those in the m_rootStateNode tree, and in m_unparentedNodes subtrees).
99 StateNodeMap m_stateNodeMap;
100 // Owns roots of unparented subtrees.
101 HashMap<ScrollingNodeID, RefPtr<ScrollingStateNode>> m_unparentedNodes;
102
103 RefPtr<ScrollingStateFrameScrollingNode> m_rootStateNode;
104 bool m_hasChangedProperties { false };
105 bool m_hasNewRootStateNode { false };
106 LayerRepresentation::Type m_preferredLayerRepresentation { LayerRepresentation::GraphicsLayerRepresentation };
107};
108
109} // namespace WebCore
110
111#ifndef NDEBUG
112void showScrollingStateTree(const WebCore::ScrollingStateTree*);
113void showScrollingStateTree(const WebCore::ScrollingStateNode*);
114#endif
115
116#endif // ENABLE(ASYNC_SCROLLING)
117