2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef ScrollingCoordinator_h
27 #define ScrollingCoordinator_h
30 #include "LayoutRect.h"
31 #include "PlatformWheelEvent.h"
32 #include "RenderObject.h"
33 #include "ScrollTypes.h"
35 #include <wtf/Forward.h>
37 #if ENABLE(THREADED_SCROLLING)
38 #include <wtf/HashMap.h>
39 #include <wtf/ThreadSafeRefCounted.h>
40 #include <wtf/Threading.h>
44 #include <wtf/RetainPtr.h>
49 typedef unsigned MainThreadScrollingReasons;
50 typedef uint64_t ScrollingNodeID;
52 enum ScrollingNodeType { ScrollingNode, FixedNode, StickyNode };
61 class ViewportConstraints;
63 #if ENABLE(THREADED_SCROLLING)
67 enum SetOrSyncScrollingLayerPosition {
68 SetScrollingLayerPosition,
69 SyncScrollingLayerPosition
72 class ScrollingCoordinator : public ThreadSafeRefCounted<ScrollingCoordinator> {
74 static PassRefPtr<ScrollingCoordinator> create(Page*);
75 virtual ~ScrollingCoordinator();
77 virtual void pageDestroyed();
79 #if ENABLE(THREADED_SCROLLING)
80 virtual ScrollingTree* scrollingTree() const { return 0; }
83 // Return whether this scrolling coordinator handles scrolling for the given frame view.
84 bool coordinatesScrollingForFrameView(FrameView*) const;
86 // Should be called whenever the given frame view has been laid out.
87 virtual void frameViewLayoutUpdated(FrameView*) { }
89 // Should be called whenever a wheel event handler is added or removed in the
90 // frame view's underlying document.
91 void frameViewWheelEventHandlerCountChanged(FrameView*);
93 // Should be called whenever the slow repaint objects counter changes between zero and one.
94 void frameViewHasSlowRepaintObjectsDidChange(FrameView*);
96 // Should be called whenever the set of fixed objects changes.
97 void frameViewFixedObjectsDidChange(FrameView*);
99 // Should be called whenever the root layer for the given frame view changes.
100 virtual void frameViewRootLayerDidChange(FrameView*);
102 // Return whether this scrolling coordinator can keep fixed position layers fixed to their
103 // containers while scrolling.
104 virtual bool supportsFixedPositionLayers() const { return false; }
106 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
107 // Dispatched by the scrolling tree during handleWheelEvent. This is required as long as scrollbars are painted on the main thread.
108 void handleWheelEventPhase(PlatformWheelEventPhase);
111 // Force all scroll layer position updates to happen on the main thread.
112 void setForceMainThreadScrollLayerPositionUpdates(bool);
114 // These virtual functions are currently unique to the threaded scrolling architecture.
115 // Their meaningful implementations are in ScrollingCoordinatorMac.
116 virtual void commitTreeStateIfNeeded() { }
117 virtual bool requestScrollPositionUpdate(FrameView*, const IntPoint&) { return false; }
118 virtual bool handleWheelEvent(FrameView*, const PlatformWheelEvent&) { return true; }
119 virtual ScrollingNodeID attachToStateTree(ScrollingNodeType, ScrollingNodeID newNodeID, ScrollingNodeID /*parentID*/) { return newNodeID; }
120 virtual void detachFromStateTree(ScrollingNodeID) { }
121 virtual void clearStateTree() { }
122 virtual void updateViewportConstrainedNode(ScrollingNodeID, const ViewportConstraints&, GraphicsLayer*) { }
123 virtual void updateScrollingNode(ScrollingNodeID, GraphicsLayer* /*scrollLayer*/, GraphicsLayer* /*counterScrollingLayer*/) { }
124 virtual void syncChildPositions(const LayoutRect&) { }
125 virtual String scrollingStateTreeAsText() const;
126 virtual bool isRubberBandInProgress() const { return false; }
128 // Generated a unique id for scroll layers.
129 ScrollingNodeID uniqueScrollLayerID();
131 // Dispatched by the scrolling tree whenever the main frame scroll position changes.
132 void scheduleUpdateMainFrameScrollPosition(const IntPoint&, bool programmaticScroll, SetOrSyncScrollingLayerPosition);
133 void updateMainFrameScrollPosition(const IntPoint&, bool programmaticScroll, SetOrSyncScrollingLayerPosition);
135 enum MainThreadScrollingReasonFlags {
136 ForcedOnMainThread = 1 << 0,
137 HasSlowRepaintObjects = 1 << 1,
138 HasViewportConstrainedObjectsWithoutSupportingFixedLayers = 1 << 2,
139 HasNonLayerViewportConstrainedObjects = 1 << 3,
140 IsImageDocument = 1 << 4
143 MainThreadScrollingReasons mainThreadScrollingReasons() const;
144 bool shouldUpdateScrollLayerPositionOnMainThread() const { return mainThreadScrollingReasons() != 0; }
146 // These virtual functions are currently unique to Chromium's WebLayer approach. Their meaningful
147 // implementations are in ScrollingCoordinatorChromium.
148 virtual void frameViewHorizontalScrollbarLayerDidChange(FrameView*, GraphicsLayer*) { }
149 virtual void frameViewVerticalScrollbarLayerDidChange(FrameView*, GraphicsLayer*) { }
150 virtual void scrollableAreaScrollLayerDidChange(ScrollableArea*, GraphicsLayer*) { }
151 virtual void setLayerIsContainerForFixedPositionLayers(GraphicsLayer*, bool) { }
152 virtual void setLayerIsFixedToContainerLayer(GraphicsLayer*, bool) { }
153 virtual void touchEventTargetRectsDidChange(const Document*) { }
155 #if ENABLE(TOUCH_EVENT_TRACKING)
156 void computeAbsoluteTouchEventTargetRects(const Document*, Vector<IntRect>&);
159 static String mainThreadScrollingReasonsAsText(MainThreadScrollingReasons);
160 String mainThreadScrollingReasonsAsText() const;
162 Region computeNonFastScrollableRegion(const Frame*, const IntPoint& frameLocation) const;
165 explicit ScrollingCoordinator(Page*);
167 unsigned computeCurrentWheelEventHandlerCount();
168 GraphicsLayer* scrollLayerForFrameView(FrameView*);
169 GraphicsLayer* counterScrollingLayerForFrameView(FrameView*);
174 virtual void recomputeWheelEventHandlerCountForFrameView(FrameView*) { }
175 virtual void setShouldUpdateScrollLayerPositionOnMainThread(MainThreadScrollingReasons) { }
177 virtual bool hasVisibleSlowRepaintViewportConstrainedObjects(FrameView*) const;
178 void updateShouldUpdateScrollLayerPositionOnMainThread();
180 void updateMainFrameScrollPositionTimerFired(Timer<ScrollingCoordinator>*);
182 Timer<ScrollingCoordinator> m_updateMainFrameScrollPositionTimer;
183 IntPoint m_scheduledUpdateScrollPosition;
184 bool m_scheduledUpdateIsProgrammaticScroll;
185 SetOrSyncScrollingLayerPosition m_scheduledScrollingLayerPositionAction;
187 bool m_forceMainThreadScrollLayerPositionUpdates;
190 } // namespace WebCore
192 #endif // ScrollingCoordinator_h