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 IntSize scrollOffsetForFixedPosition(const IntRect& visibleContentRect, const IntSize& contentsSize, const IntPoint& scrollPosition,
68 const IntPoint& scrollOrigin, float frameScaleFactor, bool fixedElementsLayoutRelativeToFrame);
70 enum SetOrSyncScrollingLayerPosition {
71 SetScrollingLayerPosition,
72 SyncScrollingLayerPosition
75 class ScrollingCoordinator : public ThreadSafeRefCounted<ScrollingCoordinator> {
77 static PassRefPtr<ScrollingCoordinator> create(Page*);
78 virtual ~ScrollingCoordinator();
80 virtual void pageDestroyed();
82 #if ENABLE(THREADED_SCROLLING)
83 virtual ScrollingTree* scrollingTree() const { return 0; }
86 // Return whether this scrolling coordinator handles scrolling for the given frame view.
87 bool coordinatesScrollingForFrameView(FrameView*) const;
89 // Should be called whenever the given frame view has been laid out.
90 virtual void frameViewLayoutUpdated(FrameView*) { }
92 // Should be called whenever a wheel event handler is added or removed in the
93 // frame view's underlying document.
94 void frameViewWheelEventHandlerCountChanged(FrameView*);
96 // Should be called whenever the slow repaint objects counter changes between zero and one.
97 void frameViewHasSlowRepaintObjectsDidChange(FrameView*);
99 // Should be called whenever the set of fixed objects changes.
100 void frameViewFixedObjectsDidChange(FrameView*);
102 // Should be called whenever the root layer for the given frame view changes.
103 virtual void frameViewRootLayerDidChange(FrameView*);
105 // Return whether this scrolling coordinator can keep fixed position layers fixed to their
106 // containers while scrolling.
107 virtual bool supportsFixedPositionLayers() const { return false; }
109 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
110 // Dispatched by the scrolling tree during handleWheelEvent. This is required as long as scrollbars are painted on the main thread.
111 void handleWheelEventPhase(PlatformWheelEventPhase);
114 // Force all scroll layer position updates to happen on the main thread.
115 void setForceMainThreadScrollLayerPositionUpdates(bool);
117 // These virtual functions are currently unique to the threaded scrolling architecture.
118 // Their meaningful implementations are in ScrollingCoordinatorMac.
119 virtual void commitTreeStateIfNeeded() { }
120 virtual bool requestScrollPositionUpdate(FrameView*, const IntPoint&) { return false; }
121 virtual bool handleWheelEvent(FrameView*, const PlatformWheelEvent&) { return true; }
122 virtual ScrollingNodeID attachToStateTree(ScrollingNodeType, ScrollingNodeID newNodeID, ScrollingNodeID /*parentID*/) { return newNodeID; }
123 virtual void detachFromStateTree(ScrollingNodeID) { }
124 virtual void clearStateTree() { }
125 virtual void updateViewportConstrainedNode(ScrollingNodeID, const ViewportConstraints&, GraphicsLayer*) { }
126 virtual void updateScrollingNode(ScrollingNodeID, GraphicsLayer* /*scrollLayer*/, GraphicsLayer* /*counterScrollingLayer*/) { }
127 virtual void syncChildPositions(const LayoutRect&) { }
128 virtual String scrollingStateTreeAsText() const;
129 virtual bool isRubberBandInProgress() const { return false; }
131 // Generated a unique id for scroll layers.
132 ScrollingNodeID uniqueScrollLayerID();
134 // Dispatched by the scrolling tree whenever the main frame scroll position changes.
135 void scheduleUpdateMainFrameScrollPosition(const IntPoint&, bool programmaticScroll, SetOrSyncScrollingLayerPosition);
136 void updateMainFrameScrollPosition(const IntPoint&, bool programmaticScroll, SetOrSyncScrollingLayerPosition);
138 enum MainThreadScrollingReasonFlags {
139 ForcedOnMainThread = 1 << 0,
140 HasSlowRepaintObjects = 1 << 1,
141 HasViewportConstrainedObjectsWithoutSupportingFixedLayers = 1 << 2,
142 HasNonLayerViewportConstrainedObjects = 1 << 3,
143 IsImageDocument = 1 << 4
146 MainThreadScrollingReasons mainThreadScrollingReasons() const;
147 bool shouldUpdateScrollLayerPositionOnMainThread() const { return mainThreadScrollingReasons() != 0; }
149 // These virtual functions are currently unique to Chromium's WebLayer approach. Their meaningful
150 // implementations are in ScrollingCoordinatorChromium.
151 virtual void frameViewHorizontalScrollbarLayerDidChange(FrameView*, GraphicsLayer*) { }
152 virtual void frameViewVerticalScrollbarLayerDidChange(FrameView*, GraphicsLayer*) { }
153 virtual void scrollableAreaScrollLayerDidChange(ScrollableArea*, GraphicsLayer*) { }
154 virtual void setLayerIsContainerForFixedPositionLayers(GraphicsLayer*, bool) { }
155 virtual void setLayerIsFixedToContainerLayer(GraphicsLayer*, bool) { }
156 virtual void touchEventTargetRectsDidChange(const Document*) { }
158 #if ENABLE(TOUCH_EVENT_TRACKING)
159 void computeAbsoluteTouchEventTargetRects(const Document*, Vector<IntRect>&);
162 static String mainThreadScrollingReasonsAsText(MainThreadScrollingReasons);
163 String mainThreadScrollingReasonsAsText() const;
165 Region computeNonFastScrollableRegion(const Frame*, const IntPoint& frameLocation) const;
168 explicit ScrollingCoordinator(Page*);
170 unsigned computeCurrentWheelEventHandlerCount();
171 GraphicsLayer* scrollLayerForFrameView(FrameView*);
172 GraphicsLayer* counterScrollingLayerForFrameView(FrameView*);
177 virtual void recomputeWheelEventHandlerCountForFrameView(FrameView*) { }
178 virtual void setShouldUpdateScrollLayerPositionOnMainThread(MainThreadScrollingReasons) { }
180 virtual bool hasVisibleSlowRepaintViewportConstrainedObjects(FrameView*) const;
181 void updateShouldUpdateScrollLayerPositionOnMainThread();
183 void updateMainFrameScrollPositionTimerFired(Timer<ScrollingCoordinator>*);
185 Timer<ScrollingCoordinator> m_updateMainFrameScrollPositionTimer;
186 IntPoint m_scheduledUpdateScrollPosition;
187 bool m_scheduledUpdateIsProgrammaticScroll;
188 SetOrSyncScrollingLayerPosition m_scheduledScrollingLayerPositionAction;
190 bool m_forceMainThreadScrollLayerPositionUpdates;
193 } // namespace WebCore
195 #endif // ScrollingCoordinator_h