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#ifndef ScrollingStateTree_h
27#define ScrollingStateTree_h
28
29#if ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
30
31#include "ScrollingStateFrameScrollingNode.h"
32#include <wtf/OwnPtr.h>
33#include <wtf/PassOwnPtr.h>
34#include <wtf/RefPtr.h>
35
36namespace WebCore {
37
38class AsyncScrollingCoordinator;
39
40// The ScrollingStateTree is a tree that managed ScrollingStateNodes. The nodes keep track of the current
41// state of scrolling related properties. Whenever any properties change, the scrolling coordinator
42// will be informed and will schedule a timer that will clone the new state tree and send it over to
43// the scrolling thread, avoiding locking.
44
45class ScrollingStateTree {
46    friend class ScrollingStateNode;
47public:
48
49    static PassOwnPtr<ScrollingStateTree> create(AsyncScrollingCoordinator* = 0);
50    ~ScrollingStateTree();
51
52    ScrollingStateFrameScrollingNode* rootStateNode() const { return m_rootStateNode.get(); }
53    ScrollingStateNode* stateNodeForID(ScrollingNodeID);
54
55    ScrollingNodeID attachNode(ScrollingNodeType, ScrollingNodeID, ScrollingNodeID parentID);
56    void detachNode(ScrollingNodeID);
57    void clear();
58
59    const HashSet<ScrollingNodeID>& removedNodes() const { return m_nodesRemovedSinceLastCommit; }
60    void setRemovedNodes(HashSet<ScrollingNodeID>);
61
62    // Copies the current tree state and clears the changed properties mask in the original.
63    PassOwnPtr<ScrollingStateTree> commit(LayerRepresentation::Type preferredLayerRepresentation);
64
65    void setHasChangedProperties(bool = true);
66    bool hasChangedProperties() const { return m_hasChangedProperties; }
67
68    bool hasNewRootStateNode() const { return m_hasNewRootStateNode; }
69    void setHasNewRootStateNode(bool hasNewRoot) { m_hasNewRootStateNode = hasNewRoot; }
70
71    int nodeCount() const { return m_stateNodeMap.size(); }
72
73    typedef HashMap<ScrollingNodeID, ScrollingStateNode*> StateNodeMap;
74    const StateNodeMap& nodeMap() const { return m_stateNodeMap; }
75
76    LayerRepresentation::Type preferredLayerRepresentation() const { return m_preferredLayerRepresentation; }
77    void setPreferredLayerRepresentation(LayerRepresentation::Type representation) { m_preferredLayerRepresentation = representation; }
78
79private:
80    ScrollingStateTree(AsyncScrollingCoordinator*);
81
82    void setRootStateNode(PassRefPtr<ScrollingStateFrameScrollingNode> rootStateNode) { m_rootStateNode = rootStateNode; }
83    void addNode(ScrollingStateNode*);
84
85    PassRefPtr<ScrollingStateNode> createNode(ScrollingNodeType, ScrollingNodeID);
86
87    enum class SubframeNodeRemoval {
88        Delete,
89        Orphan
90    };
91    void removeNodeAndAllDescendants(ScrollingStateNode*, SubframeNodeRemoval = SubframeNodeRemoval::Delete);
92
93    void recursiveNodeWillBeRemoved(ScrollingStateNode* currNode, SubframeNodeRemoval);
94    void willRemoveNode(ScrollingStateNode*);
95
96    AsyncScrollingCoordinator* m_scrollingCoordinator;
97    StateNodeMap m_stateNodeMap;
98    RefPtr<ScrollingStateFrameScrollingNode> m_rootStateNode;
99    HashSet<ScrollingNodeID> m_nodesRemovedSinceLastCommit;
100    HashMap<ScrollingNodeID, RefPtr<ScrollingStateNode>> m_orphanedSubframeNodes;
101    bool m_hasChangedProperties;
102    bool m_hasNewRootStateNode;
103    LayerRepresentation::Type m_preferredLayerRepresentation;
104};
105
106} // namespace WebCore
107
108#endif // ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
109
110#endif // ScrollingStateTree_h
111