1 | /* |
2 | * Copyright (C) 2012-2015 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 | #pragma once |
27 | |
28 | #if ENABLE(ASYNC_SCROLLING) |
29 | |
30 | #include "PlatformWheelEvent.h" |
31 | #include "Region.h" |
32 | #include "ScrollingCoordinator.h" |
33 | #include "TouchAction.h" |
34 | #include "WheelEventTestTrigger.h" |
35 | #include <wtf/HashMap.h> |
36 | #include <wtf/Lock.h> |
37 | #include <wtf/ThreadSafeRefCounted.h> |
38 | #include <wtf/TypeCasts.h> |
39 | |
40 | namespace WebCore { |
41 | |
42 | class IntPoint; |
43 | class ScrollingStateTree; |
44 | class ScrollingStateNode; |
45 | class ScrollingTreeFrameScrollingNode; |
46 | class ScrollingTreeNode; |
47 | class ScrollingTreeScrollingNode; |
48 | |
49 | class ScrollingTree : public ThreadSafeRefCounted<ScrollingTree> { |
50 | public: |
51 | WEBCORE_EXPORT ScrollingTree(); |
52 | WEBCORE_EXPORT virtual ~ScrollingTree(); |
53 | |
54 | virtual bool isThreadedScrollingTree() const { return false; } |
55 | virtual bool isRemoteScrollingTree() const { return false; } |
56 | virtual bool isScrollingTreeIOS() const { return false; } |
57 | |
58 | // This implies that we'll do hit-testing in the scrolling tree. |
59 | bool asyncFrameOrOverflowScrollingEnabled() const { return m_asyncFrameOrOverflowScrollingEnabled; } |
60 | void setAsyncFrameOrOverflowScrollingEnabled(bool); |
61 | |
62 | virtual ScrollingEventResult tryToHandleWheelEvent(const PlatformWheelEvent&) = 0; |
63 | WEBCORE_EXPORT bool shouldHandleWheelEventSynchronously(const PlatformWheelEvent&); |
64 | |
65 | void setMainFrameIsRubberBanding(bool); |
66 | bool isRubberBandInProgress(); |
67 | void setMainFrameIsScrollSnapping(bool); |
68 | bool isScrollSnapInProgress(); |
69 | |
70 | virtual void invalidate() { } |
71 | WEBCORE_EXPORT virtual void commitTreeState(std::unique_ptr<ScrollingStateTree>); |
72 | |
73 | WEBCORE_EXPORT virtual void applyLayerPositions(); |
74 | WEBCORE_EXPORT void applyLayerPositionsAfterCommit(); |
75 | |
76 | virtual Ref<ScrollingTreeNode> createScrollingTreeNode(ScrollingNodeType, ScrollingNodeID) = 0; |
77 | |
78 | WEBCORE_EXPORT ScrollingTreeNode* nodeForID(ScrollingNodeID) const; |
79 | |
80 | // Called after a scrolling tree node has handled a scroll and updated its layers. |
81 | // Updates FrameView/RenderLayer scrolling state and GraphicsLayers. |
82 | virtual void scrollingTreeNodeDidScroll(ScrollingTreeScrollingNode&, ScrollingLayerPositionAction = ScrollingLayerPositionAction::Sync) = 0; |
83 | |
84 | // Called for requested scroll position updates. |
85 | virtual void scrollingTreeNodeRequestsScroll(ScrollingNodeID, const FloatPoint& /*scrollPosition*/, bool /*representsProgrammaticScroll*/) { } |
86 | |
87 | // Delegated scrolling/zooming has caused the viewport to change, so update viewport-constrained layers |
88 | WEBCORE_EXPORT void mainFrameViewportChangedViaDelegatedScrolling(const FloatPoint& scrollPosition, const WebCore::FloatRect& layoutViewport, double scale); |
89 | |
90 | void didScrollByDelegatedScrolling() { m_wasScrolledByDelegatedScrollingSincePreviousCommit = true; } |
91 | |
92 | void notifyRelatedNodesAfterScrollPositionChange(ScrollingTreeScrollingNode& changedNode); |
93 | |
94 | virtual void reportSynchronousScrollingReasonsChanged(MonotonicTime, SynchronousScrollingReasons) { } |
95 | virtual void reportExposedUnfilledArea(MonotonicTime, unsigned /* unfilledArea */) { } |
96 | |
97 | #if PLATFORM(IOS_FAMILY) |
98 | virtual void scrollingTreeNodeWillStartPanGesture() { } |
99 | virtual void scrollingTreeNodeWillStartScroll() { } |
100 | virtual void scrollingTreeNodeDidEndScroll() { } |
101 | #endif |
102 | |
103 | WEBCORE_EXPORT TrackingType eventTrackingTypeForPoint(const AtomString& eventName, IntPoint); |
104 | #if ENABLE(POINTER_EVENTS) |
105 | WEBCORE_EXPORT Optional<TouchActionData> touchActionDataAtPoint(IntPoint) const; |
106 | #endif |
107 | |
108 | #if PLATFORM(MAC) |
109 | virtual void handleWheelEventPhase(PlatformWheelEventPhase) = 0; |
110 | virtual void setActiveScrollSnapIndices(ScrollingNodeID, unsigned /*horizontalIndex*/, unsigned /*verticalIndex*/) { } |
111 | virtual void deferTestsForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) { } |
112 | virtual void removeTestDeferralForReason(WheelEventTestTrigger::ScrollableAreaIdentifier, WheelEventTestTrigger::DeferTestTriggerReason) { } |
113 | #endif |
114 | |
115 | #if PLATFORM(COCOA) |
116 | WEBCORE_EXPORT virtual void currentSnapPointIndicesDidChange(ScrollingNodeID, unsigned horizontal, unsigned vertical) = 0; |
117 | #endif |
118 | |
119 | void setMainFramePinState(bool pinnedToTheLeft, bool pinnedToTheRight, bool pinnedToTheTop, bool pinnedToTheBottom); |
120 | |
121 | // Can be called from any thread. Will update what edges allow rubber-banding. |
122 | WEBCORE_EXPORT void setCanRubberBandState(bool canRubberBandAtLeft, bool canRubberBandAtRight, bool canRubberBandAtTop, bool canRubberBandAtBottom); |
123 | |
124 | bool isHandlingProgrammaticScroll() const { return m_isHandlingProgrammaticScroll; } |
125 | void setIsHandlingProgrammaticScroll(bool isHandlingProgrammaticScroll) { m_isHandlingProgrammaticScroll = isHandlingProgrammaticScroll; } |
126 | |
127 | void setScrollPinningBehavior(ScrollPinningBehavior); |
128 | WEBCORE_EXPORT ScrollPinningBehavior scrollPinningBehavior(); |
129 | |
130 | WEBCORE_EXPORT bool willWheelEventStartSwipeGesture(const PlatformWheelEvent&); |
131 | |
132 | WEBCORE_EXPORT void setScrollingPerformanceLoggingEnabled(bool flag); |
133 | bool scrollingPerformanceLoggingEnabled(); |
134 | |
135 | ScrollingTreeFrameScrollingNode* rootNode() const { return m_rootNode.get(); } |
136 | |
137 | ScrollingNodeID latchedNode(); |
138 | void setLatchedNode(ScrollingNodeID); |
139 | void clearLatchedNode(); |
140 | |
141 | bool hasLatchedNode() const { return m_treeState.latchedNodeID; } |
142 | void setOrClearLatchedNode(const PlatformWheelEvent&, ScrollingNodeID); |
143 | |
144 | bool hasFixedOrSticky() const { return !!m_fixedOrStickyNodeCount; } |
145 | void fixedOrStickyNodeAdded() { ++m_fixedOrStickyNodeCount; } |
146 | void fixedOrStickyNodeRemoved() |
147 | { |
148 | ASSERT(m_fixedOrStickyNodeCount); |
149 | --m_fixedOrStickyNodeCount; |
150 | } |
151 | |
152 | // A map of overflow scrolling nodes to positioned nodes which need to be updated |
153 | // when the scroller changes, but are not descendants. |
154 | using RelatedNodesMap = HashMap<ScrollingNodeID, Vector<ScrollingNodeID>>; |
155 | RelatedNodesMap& overflowRelatedNodes() { return m_overflowRelatedNodesMap; } |
156 | |
157 | HashSet<ScrollingNodeID>& positionedNodesWithRelatedOverflow() { return m_positionedNodesWithRelatedOverflow; } |
158 | |
159 | WEBCORE_EXPORT String scrollingTreeAsText(ScrollingStateTreeAsTextBehavior = ScrollingStateTreeAsTextBehaviorNormal); |
160 | |
161 | protected: |
162 | void setMainFrameScrollPosition(FloatPoint); |
163 | |
164 | WEBCORE_EXPORT virtual ScrollingEventResult handleWheelEvent(const PlatformWheelEvent&); |
165 | |
166 | private: |
167 | using OrphanScrollingNodeMap = HashMap<ScrollingNodeID, RefPtr<ScrollingTreeNode>>; |
168 | void updateTreeFromStateNode(const ScrollingStateNode*, OrphanScrollingNodeMap&, HashSet<ScrollingNodeID>& unvisitedNodes); |
169 | |
170 | void applyLayerPositionsRecursive(ScrollingTreeNode&); |
171 | |
172 | void notifyRelatedNodesRecursive(ScrollingTreeNode&); |
173 | |
174 | Lock m_treeMutex; // Protects the scrolling tree. |
175 | |
176 | RefPtr<ScrollingTreeFrameScrollingNode> m_rootNode; |
177 | |
178 | using ScrollingTreeNodeMap = HashMap<ScrollingNodeID, ScrollingTreeNode*>; |
179 | ScrollingTreeNodeMap m_nodeMap; |
180 | |
181 | RelatedNodesMap m_overflowRelatedNodesMap; |
182 | HashSet<ScrollingNodeID> m_positionedNodesWithRelatedOverflow; |
183 | |
184 | struct TreeState { |
185 | ScrollingNodeID latchedNodeID { 0 }; |
186 | EventTrackingRegions eventTrackingRegions; |
187 | FloatPoint mainFrameScrollPosition; |
188 | bool mainFrameIsRubberBanding { false }; |
189 | bool mainFrameIsScrollSnapping { false }; |
190 | }; |
191 | |
192 | Lock m_treeStateMutex; |
193 | TreeState m_treeState; |
194 | |
195 | struct SwipeState { |
196 | ScrollPinningBehavior scrollPinningBehavior { DoNotPin }; |
197 | bool rubberBandsAtLeft { true }; |
198 | bool rubberBandsAtRight { true }; |
199 | bool rubberBandsAtTop { true }; |
200 | bool rubberBandsAtBottom { true }; |
201 | bool mainFramePinnedToTheLeft { true }; |
202 | bool mainFramePinnedToTheRight { true }; |
203 | bool mainFramePinnedToTheTop { true }; |
204 | bool mainFramePinnedToTheBottom { true }; |
205 | }; |
206 | |
207 | Lock m_swipeStateMutex; |
208 | SwipeState m_swipeState; |
209 | |
210 | unsigned m_fixedOrStickyNodeCount { 0 }; |
211 | bool m_isHandlingProgrammaticScroll { false }; |
212 | bool m_scrollingPerformanceLoggingEnabled { false }; |
213 | bool m_asyncFrameOrOverflowScrollingEnabled { false }; |
214 | bool m_wasScrolledByDelegatedScrollingSincePreviousCommit { false }; |
215 | }; |
216 | |
217 | } // namespace WebCore |
218 | |
219 | #define SPECIALIZE_TYPE_TRAITS_SCROLLING_TREE(ToValueTypeName, predicate) \ |
220 | SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \ |
221 | static bool isType(const WebCore::ScrollingTree& tree) { return tree.predicate; } \ |
222 | SPECIALIZE_TYPE_TRAITS_END() |
223 | #endif // ENABLE(ASYNC_SCROLLING) |
224 | |