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