1/*
2 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
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 THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if USE(COORDINATED_GRAPHICS)
29
30#include "ScrollingCoordinatorCoordinatedGraphics.h"
31
32#include "CoordinatedGraphicsLayer.h"
33#include "FrameView.h"
34#include "Page.h"
35#include "RenderLayer.h"
36#include "RenderLayerBacking.h"
37#include "ScrollingConstraints.h"
38#include "ScrollingStateFixedNode.h"
39#include "ScrollingStateScrollingNode.h"
40#include "ScrollingStateStickyNode.h"
41#include "ScrollingStateTree.h"
42#include "Settings.h"
43
44namespace WebCore {
45
46ScrollingCoordinatorCoordinatedGraphics::ScrollingCoordinatorCoordinatedGraphics(Page* page)
47    : ScrollingCoordinator(page)
48    , m_scrollingStateTree(ScrollingStateTree::create())
49{
50}
51
52ScrollingCoordinatorCoordinatedGraphics::~ScrollingCoordinatorCoordinatedGraphics()
53{
54}
55
56ScrollingNodeID ScrollingCoordinatorCoordinatedGraphics::attachToStateTree(ScrollingNodeType nodeType, ScrollingNodeID newNodeID, ScrollingNodeID parentID)
57{
58    return m_scrollingStateTree->attachNode(nodeType, newNodeID, parentID);
59}
60
61void ScrollingCoordinatorCoordinatedGraphics::detachFromStateTree(ScrollingNodeID nodeID)
62{
63    ScrollingStateNode* node = m_scrollingStateTree->stateNodeForID(nodeID);
64    if (node && node->nodeType() == FixedNode)
65        toCoordinatedGraphicsLayer(node->layer())->setFixedToViewport(false);
66
67    m_scrollingStateTree->detachNode(nodeID);
68}
69
70void ScrollingCoordinatorCoordinatedGraphics::clearStateTree()
71{
72    m_scrollingStateTree->clear();
73}
74
75void ScrollingCoordinatorCoordinatedGraphics::updateViewportConstrainedNode(ScrollingNodeID nodeID, const ViewportConstraints& constraints, GraphicsLayer* graphicsLayer)
76{
77    ASSERT(supportsFixedPositionLayers());
78
79    ScrollingStateNode* node = m_scrollingStateTree->stateNodeForID(nodeID);
80    if (!node)
81        return;
82
83    switch (constraints.constraintType()) {
84    case ViewportConstraints::FixedPositionConstraint: {
85        toCoordinatedGraphicsLayer(graphicsLayer)->setFixedToViewport(true); // FIXME : Use constraints!
86        ScrollingStateFixedNode* fixedNode = toScrollingStateFixedNode(node);
87        fixedNode->setLayer(graphicsLayer);
88        break;
89    }
90    case ViewportConstraints::StickyPositionConstraint:
91        break; // FIXME : Support sticky elements.
92    default:
93        ASSERT_NOT_REACHED();
94    }
95}
96
97void ScrollingCoordinatorCoordinatedGraphics::scrollableAreaScrollLayerDidChange(ScrollableArea* scrollableArea)
98{
99    CoordinatedGraphicsLayer* layer = toCoordinatedGraphicsLayer(scrollLayerForScrollableArea(scrollableArea));
100    if (!layer)
101        return;
102
103    layer->setScrollableArea(scrollableArea);
104}
105
106void ScrollingCoordinatorCoordinatedGraphics::willDestroyScrollableArea(ScrollableArea* scrollableArea)
107{
108    CoordinatedGraphicsLayer* layer = toCoordinatedGraphicsLayer(scrollLayerForScrollableArea(scrollableArea));
109    if (!layer)
110        return;
111
112    layer->setScrollableArea(0);
113}
114
115} // namespace WebCore
116
117#endif // USE(COORDINATED_GRAPHICS)
118