Refactor ScrollingCoordinator::setSynchronousScrollingReasons to accept a FrameView
[WebKit-https.git] / Source / WebCore / page / scrolling / ScrollingCoordinator.h
1 /*
2  * Copyright (C) 2011, 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 #include "EventTrackingRegions.h"
29 #include "IntRect.h"
30 #include "LayoutRect.h"
31 #include "PlatformWheelEvent.h"
32 #include "ScrollSnapOffsetsInfo.h"
33 #include "ScrollTypes.h"
34 #include <wtf/Forward.h>
35 #include <wtf/ThreadSafeRefCounted.h>
36 #include <wtf/TypeCasts.h>
37 #include <wtf/Variant.h>
38
39 #if ENABLE(ASYNC_SCROLLING)
40 #include <wtf/HashMap.h>
41 #include <wtf/ThreadSafeRefCounted.h>
42 #include <wtf/Threading.h>
43 #endif
44
45 #if ENABLE(CSS_SCROLL_SNAP)
46 #include "AxisScrollSnapOffsets.h"
47 #endif
48
49 namespace WebCore {
50
51 typedef unsigned SynchronousScrollingReasons;
52 typedef uint64_t ScrollingNodeID;
53
54 enum ScrollingNodeType { FrameScrollingNode, OverflowScrollingNode, FixedNode, StickyNode };
55
56 enum ScrollingStateTreeAsTextBehaviorFlags {
57     ScrollingStateTreeAsTextBehaviorNormal                  = 0,
58     ScrollingStateTreeAsTextBehaviorIncludeLayerIDs         = 1 << 0,
59     ScrollingStateTreeAsTextBehaviorIncludeNodeIDs          = 1 << 1,
60     ScrollingStateTreeAsTextBehaviorIncludeLayerPositions   = 1 << 2,
61     ScrollingStateTreeAsTextBehaviorDebug                   = ScrollingStateTreeAsTextBehaviorIncludeLayerIDs | ScrollingStateTreeAsTextBehaviorIncludeNodeIDs | ScrollingStateTreeAsTextBehaviorIncludeLayerPositions
62 };
63 typedef unsigned ScrollingStateTreeAsTextBehavior;
64
65 class Document;
66 class Frame;
67 class FrameView;
68 class GraphicsLayer;
69 class Page;
70 class Region;
71 class ScrollableArea;
72 class TextStream;
73 class ViewportConstraints;
74
75 #if ENABLE(ASYNC_SCROLLING)
76 class ScrollingTree;
77 #endif
78
79 enum class ScrollingLayerPositionAction {
80     Set,
81     SetApproximate,
82     Sync
83 };
84
85 struct ScrollableAreaParameters {
86     ScrollElasticity horizontalScrollElasticity;
87     ScrollElasticity verticalScrollElasticity;
88
89     ScrollbarMode horizontalScrollbarMode;
90     ScrollbarMode verticalScrollbarMode;
91
92     bool hasEnabledHorizontalScrollbar;
93     bool hasEnabledVerticalScrollbar;
94     
95     ScrollableAreaParameters()
96         : horizontalScrollElasticity(ScrollElasticityNone)
97         , verticalScrollElasticity(ScrollElasticityNone)
98         , horizontalScrollbarMode(ScrollbarAuto)
99         , verticalScrollbarMode(ScrollbarAuto)
100         , hasEnabledHorizontalScrollbar(false)
101         , hasEnabledVerticalScrollbar(false)
102     {
103     }
104
105     bool operator==(const ScrollableAreaParameters& other) const
106     {
107         return horizontalScrollElasticity == other.horizontalScrollElasticity
108             && verticalScrollElasticity == other.verticalScrollElasticity
109             && horizontalScrollbarMode == other.horizontalScrollbarMode
110             && verticalScrollbarMode == other.verticalScrollbarMode
111             && hasEnabledHorizontalScrollbar == other.hasEnabledHorizontalScrollbar
112             && hasEnabledVerticalScrollbar == other.hasEnabledVerticalScrollbar;
113     }
114 };
115
116 class ScrollingCoordinator : public ThreadSafeRefCounted<ScrollingCoordinator> {
117 public:
118     static Ref<ScrollingCoordinator> create(Page*);
119     virtual ~ScrollingCoordinator();
120
121     WEBCORE_EXPORT virtual void pageDestroyed();
122     
123     virtual bool isAsyncScrollingCoordinator() const { return false; }
124     virtual bool isRemoteScrollingCoordinator() const { return false; }
125
126     // Return whether this scrolling coordinator handles scrolling for the given frame view.
127     virtual bool coordinatesScrollingForFrameView(const FrameView&) const;
128
129     // Should be called whenever the given frame view has been laid out.
130     virtual void frameViewLayoutUpdated(FrameView&) { }
131
132     using LayoutViewportOriginOrOverrideRect = WTF::Variant<std::optional<FloatPoint>, std::optional<FloatRect>>;
133     virtual void reconcileScrollingState(FrameView&, const FloatPoint&, const LayoutViewportOriginOrOverrideRect&, bool /* programmaticScroll */, bool /* inStableState*/, ScrollingLayerPositionAction) { }
134
135     // Should be called whenever the slow repaint objects counter changes between zero and one.
136     void frameViewHasSlowRepaintObjectsDidChange(FrameView&);
137
138     // Should be called whenever the set of fixed objects changes.
139     void frameViewFixedObjectsDidChange(FrameView&);
140
141     // Called whenever the non-fast scrollable region changes for reasons other than layout.
142     virtual void frameViewEventTrackingRegionsChanged(FrameView&) { }
143
144     // Should be called whenever the root layer for the given frame view changes.
145     virtual void frameViewRootLayerDidChange(FrameView&);
146
147 #if PLATFORM(COCOA)
148     // Dispatched by the scrolling tree during handleWheelEvent. This is required as long as scrollbars are painted on the main thread.
149     void handleWheelEventPhase(PlatformWheelEventPhase);
150 #endif
151
152 #if ENABLE(WEB_REPLAY)
153     // Called when the page transitions between executing normally and deterministically.
154     void replaySessionStateDidChange();
155 #endif
156
157     // Force all scroll layer position updates to happen on the main thread.
158     WEBCORE_EXPORT void setForceSynchronousScrollLayerPositionUpdates(bool);
159
160     // These virtual functions are currently unique to the threaded scrolling architecture. 
161     // Their meaningful implementations are in ScrollingCoordinatorMac.
162     virtual void commitTreeStateIfNeeded() { }
163     virtual bool requestScrollPositionUpdate(FrameView&, const IntPoint&) { return false; }
164     virtual bool handleWheelEvent(FrameView&, const PlatformWheelEvent&) { return true; }
165     virtual ScrollingNodeID attachToStateTree(ScrollingNodeType, ScrollingNodeID newNodeID, ScrollingNodeID /*parentID*/) { return newNodeID; }
166     virtual void detachFromStateTree(ScrollingNodeID) { }
167     virtual void clearStateTree() { }
168     virtual void updateViewportConstrainedNode(ScrollingNodeID, const ViewportConstraints&, GraphicsLayer*) { }
169
170     struct ScrollingGeometry {
171         FloatSize scrollableAreaSize;
172         FloatSize contentSize;
173         FloatSize reachableContentSize; // Smaller than contentSize when overflow is hidden on one axis.
174         FloatPoint scrollPosition;
175         IntPoint scrollOrigin;
176 #if ENABLE(CSS_SCROLL_SNAP)
177         Vector<LayoutUnit> horizontalSnapOffsets;
178         Vector<LayoutUnit> verticalSnapOffsets;
179         Vector<ScrollOffsetRange<LayoutUnit>> horizontalSnapOffsetRanges;
180         Vector<ScrollOffsetRange<LayoutUnit>> verticalSnapOffsetRanges;
181         unsigned currentHorizontalSnapPointIndex;
182         unsigned currentVerticalSnapPointIndex;
183 #endif
184     };
185
186     virtual void updateFrameScrollingNode(ScrollingNodeID, GraphicsLayer* /*scrollLayer*/, GraphicsLayer* /*scrolledContentsLayer*/, GraphicsLayer* /*counterScrollingLayer*/, GraphicsLayer* /*insetClipLayer*/, const ScrollingGeometry* = nullptr) { }
187     virtual void updateOverflowScrollingNode(ScrollingNodeID, GraphicsLayer* /*scrollLayer*/, GraphicsLayer* /*scrolledContentsLayer*/, const ScrollingGeometry* = nullptr) { }
188     virtual void reconcileViewportConstrainedLayerPositions(const LayoutRect&, ScrollingLayerPositionAction) { }
189     virtual String scrollingStateTreeAsText(ScrollingStateTreeAsTextBehavior = ScrollingStateTreeAsTextBehaviorNormal) const;
190     virtual bool isRubberBandInProgress() const { return false; }
191     virtual bool isScrollSnapInProgress() const { return false; }
192     virtual void updateScrollSnapPropertiesWithFrameView(const FrameView&) { }
193     virtual void setScrollPinningBehavior(ScrollPinningBehavior) { }
194
195     // Generated a unique id for scroll layers.
196     ScrollingNodeID uniqueScrollLayerID();
197
198     enum MainThreadScrollingReasonFlags {
199         ForcedOnMainThread                                          = 1 << 0,
200         HasSlowRepaintObjects                                       = 1 << 1,
201         HasViewportConstrainedObjectsWithoutSupportingFixedLayers   = 1 << 2,
202         HasNonLayerViewportConstrainedObjects                       = 1 << 3,
203         IsImageDocument                                             = 1 << 4
204     };
205
206     SynchronousScrollingReasons synchronousScrollingReasons(const FrameView&) const;
207     bool shouldUpdateScrollLayerPositionSynchronously(const FrameView&) const;
208
209     virtual void willDestroyScrollableArea(ScrollableArea&) { }
210     virtual void scrollableAreaScrollLayerDidChange(ScrollableArea&) { }
211     virtual void scrollableAreaScrollbarLayerDidChange(ScrollableArea&, ScrollbarOrientation) { }
212
213     static String synchronousScrollingReasonsAsText(SynchronousScrollingReasons);
214     String synchronousScrollingReasonsAsText() const;
215
216     EventTrackingRegions absoluteEventTrackingRegions() const;
217     virtual void updateExpectsWheelEventTestTriggerWithFrameView(const FrameView&) { }
218
219 protected:
220     explicit ScrollingCoordinator(Page*);
221
222     static GraphicsLayer* scrollLayerForScrollableArea(ScrollableArea&);
223
224     GraphicsLayer* scrollLayerForFrameView(FrameView&);
225     GraphicsLayer* counterScrollingLayerForFrameView(FrameView&);
226     GraphicsLayer* insetClipLayerForFrameView(FrameView&);
227     GraphicsLayer* rootContentLayerForFrameView(FrameView&);
228     GraphicsLayer* contentShadowLayerForFrameView(FrameView&);
229     GraphicsLayer* headerLayerForFrameView(FrameView&);
230     GraphicsLayer* footerLayerForFrameView(FrameView&);
231
232     virtual void willCommitTree() { }
233
234     Page* m_page; // FIXME: ideally this would be a reference but it gets nulled on async teardown.
235
236 private:
237     virtual void setSynchronousScrollingReasons(FrameView&, SynchronousScrollingReasons) { }
238
239     virtual bool hasVisibleSlowRepaintViewportConstrainedObjects(const FrameView&) const;
240     void updateSynchronousScrollingReasons(FrameView&);
241
242     EventTrackingRegions absoluteEventTrackingRegionsForFrame(const Frame&) const;
243     
244     bool m_forceSynchronousScrollLayerPositionUpdates { false };
245 };
246
247 WEBCORE_EXPORT TextStream& operator<<(TextStream&, ScrollableAreaParameters);
248 WEBCORE_EXPORT TextStream& operator<<(TextStream&, ScrollingNodeType);
249 WEBCORE_EXPORT TextStream& operator<<(TextStream&, ScrollingLayerPositionAction);
250
251 } // namespace WebCore
252
253 #define SPECIALIZE_TYPE_TRAITS_SCROLLING_COORDINATOR(ToValueTypeName, predicate) \
254 SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \
255     static bool isType(const WebCore::ScrollingCoordinator& value) { return value.predicate; } \
256 SPECIALIZE_TYPE_TRAITS_END()