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#include "config.h"
27#include "ScrollingStateNode.h"
28
29#if ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
30
31#include "ScrollingStateFixedNode.h"
32#include "ScrollingStateTree.h"
33#include "TextStream.h"
34
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39ScrollingStateNode::ScrollingStateNode(ScrollingNodeType nodeType, ScrollingStateTree& scrollingStateTree, ScrollingNodeID nodeID)
40    : m_nodeType(nodeType)
41    , m_nodeID(nodeID)
42    , m_changedProperties(0)
43    , m_scrollingStateTree(scrollingStateTree)
44    , m_parent(nullptr)
45{
46}
47
48// This copy constructor is used for cloning nodes in the tree, and it doesn't make sense
49// to clone the relationship pointers, so don't copy that information from the original node.
50ScrollingStateNode::ScrollingStateNode(const ScrollingStateNode& stateNode, ScrollingStateTree& adoptiveTree)
51    : m_nodeType(stateNode.nodeType())
52    , m_nodeID(stateNode.scrollingNodeID())
53    , m_changedProperties(stateNode.changedProperties())
54    , m_scrollingStateTree(adoptiveTree)
55    , m_parent(nullptr)
56{
57    if (hasChangedProperty(ScrollLayer))
58        setLayer(stateNode.layer().toRepresentation(adoptiveTree.preferredLayerRepresentation()));
59    scrollingStateTree().addNode(this);
60}
61
62ScrollingStateNode::~ScrollingStateNode()
63{
64}
65
66void ScrollingStateNode::setPropertyChanged(unsigned propertyBit)
67{
68    if (m_changedProperties & (1 << propertyBit))
69        return;
70
71    m_changedProperties |= (1 << propertyBit);
72    m_scrollingStateTree.setHasChangedProperties();
73}
74
75PassRefPtr<ScrollingStateNode> ScrollingStateNode::cloneAndReset(ScrollingStateTree& adoptiveTree)
76{
77    RefPtr<ScrollingStateNode> clone = this->clone(adoptiveTree);
78
79    // Now that this node is cloned, reset our change properties.
80    resetChangedProperties();
81
82    cloneAndResetChildren(*clone, adoptiveTree);
83    return clone.release();
84}
85
86void ScrollingStateNode::cloneAndResetChildren(ScrollingStateNode& clone, ScrollingStateTree& adoptiveTree)
87{
88    if (!m_children)
89        return;
90
91    for (auto& child : *m_children)
92        clone.appendChild(child->cloneAndReset(adoptiveTree));
93}
94
95void ScrollingStateNode::appendChild(PassRefPtr<ScrollingStateNode> childNode)
96{
97    childNode->setParent(this);
98
99    if (!m_children)
100        m_children = adoptPtr(new Vector<RefPtr<ScrollingStateNode>>);
101
102    m_children->append(childNode);
103}
104
105void ScrollingStateNode::setLayer(const LayerRepresentation& layerRepresentation)
106{
107    if (layerRepresentation == m_layer)
108        return;
109
110    m_layer = layerRepresentation;
111
112    setPropertyChanged(ScrollLayer);
113}
114
115void ScrollingStateNode::dump(TextStream& ts, int indent) const
116{
117    writeIndent(ts, indent);
118    dumpProperties(ts, indent);
119
120    if (m_children) {
121        writeIndent(ts, indent + 1);
122        ts << "(children " << children()->size() << "\n";
123
124        for (auto& child : *m_children)
125            child->dump(ts, indent + 2);
126        writeIndent(ts, indent + 1);
127        ts << ")\n";
128    }
129
130    writeIndent(ts, indent);
131    ts << ")\n";
132}
133
134String ScrollingStateNode::scrollingStateTreeAsText() const
135{
136    TextStream ts;
137
138    dump(ts, 0);
139    return ts.release();
140}
141
142} // namespace WebCore
143
144#endif // ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
145