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 "ScrollingCoordinatorTypes.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
49namespace WTF {
50class TextStream;
51}
52
53namespace WebCore {
54
55class Document;
56class Frame;
57class FrameView;
58class GraphicsLayer;
59class LayoutConstraints;
60class Page;
61class Region;
62class RenderLayer;
63class ScrollableArea;
64class ViewportConstraints;
65
66#if ENABLE(ASYNC_SCROLLING)
67class ScrollingTree;
68#endif
69
70class ScrollingCoordinator : public ThreadSafeRefCounted<ScrollingCoordinator> {
71public:
72 static Ref<ScrollingCoordinator> create(Page*);
73 virtual ~ScrollingCoordinator();
74
75 WEBCORE_EXPORT virtual void pageDestroyed();
76
77 virtual bool isAsyncScrollingCoordinator() const { return false; }
78 virtual bool isRemoteScrollingCoordinator() const { return false; }
79
80 // Return whether this scrolling coordinator handles scrolling for the given frame view.
81 WEBCORE_EXPORT virtual bool coordinatesScrollingForFrameView(const FrameView&) const;
82
83 // Return whether this scrolling coordinator handles scrolling for the given overflow scroll layer.
84 WEBCORE_EXPORT virtual bool coordinatesScrollingForOverflowLayer(const RenderLayer&) const;
85
86 // Should be called whenever the given frame view has been laid out.
87 virtual void frameViewLayoutUpdated(FrameView&) { }
88
89 using LayoutViewportOriginOrOverrideRect = WTF::Variant<Optional<FloatPoint>, Optional<FloatRect>>;
90 virtual void reconcileScrollingState(FrameView&, const FloatPoint&, const LayoutViewportOriginOrOverrideRect&, ScrollType, ViewportRectStability, ScrollingLayerPositionAction) { }
91
92 // Should be called whenever the slow repaint objects counter changes between zero and one.
93 void frameViewHasSlowRepaintObjectsDidChange(FrameView&);
94
95 // Should be called whenever the set of fixed objects changes.
96 void frameViewFixedObjectsDidChange(FrameView&);
97
98 // Called whenever the non-fast scrollable region changes for reasons other than layout.
99 virtual void frameViewEventTrackingRegionsChanged(FrameView&) { }
100
101 // Should be called whenever the root layer for the given frame view changes.
102 virtual void frameViewRootLayerDidChange(FrameView&);
103
104 // Traverses the scrolling tree, setting layer positions to represent the current scrolled state.
105 virtual void applyScrollingTreeLayerPositions() { }
106
107#if PLATFORM(COCOA)
108 // Dispatched by the scrolling tree during handleWheelEvent. This is required as long as scrollbars are painted on the main thread.
109 void handleWheelEventPhase(PlatformWheelEventPhase);
110#endif
111
112 // Force all scroll layer position updates to happen on the main thread.
113 WEBCORE_EXPORT void setForceSynchronousScrollLayerPositionUpdates(bool);
114
115 // These virtual functions are currently unique to the threaded scrolling architecture.
116 virtual void commitTreeStateIfNeeded() { }
117 virtual bool requestScrollPositionUpdate(ScrollableArea&, const IntPoint&) { return false; }
118 virtual ScrollingEventResult handleWheelEvent(FrameView&, const PlatformWheelEvent&) { return ScrollingEventResult::DidNotHandleEvent; }
119
120 // Create an unparented node.
121 virtual ScrollingNodeID createNode(ScrollingNodeType, ScrollingNodeID newNodeID) { return newNodeID; }
122 // Parent a node in the scrolling tree. This may return a new nodeID if the node type changed. parentID = 0 sets the root node.
123 virtual ScrollingNodeID insertNode(ScrollingNodeType, ScrollingNodeID newNodeID, ScrollingNodeID /*parentID*/, size_t /*childIndex*/ = notFound) { return newNodeID; }
124 // Node will be unparented, but not destroyed. It's the client's responsibility to either re-parent or destroy this node.
125 virtual void unparentNode(ScrollingNodeID) { }
126 // Node will be destroyed, and its children left unparented.
127 virtual void unparentChildrenAndDestroyNode(ScrollingNodeID) { }
128 // Node will be unparented, and it and its children destroyed.
129 virtual void detachAndDestroySubtree(ScrollingNodeID) { }
130 // Destroy the tree, including both parented and unparented nodes.
131 virtual void clearAllNodes() { }
132
133 virtual ScrollingNodeID parentOfNode(ScrollingNodeID) const { return 0; }
134 virtual Vector<ScrollingNodeID> childrenOfNode(ScrollingNodeID) const { return { }; }
135
136 struct NodeLayers {
137 GraphicsLayer* layer { nullptr };
138 GraphicsLayer* scrollContainerLayer { nullptr };
139 GraphicsLayer* scrolledContentsLayer { nullptr };
140 GraphicsLayer* counterScrollingLayer { nullptr };
141 GraphicsLayer* insetClipLayer { nullptr };
142 GraphicsLayer* rootContentsLayer { nullptr };
143 GraphicsLayer* horizontalScrollbarLayer { nullptr };
144 GraphicsLayer* verticalScrollbarLayer { nullptr };
145 };
146 virtual void setNodeLayers(ScrollingNodeID, const NodeLayers&) { }
147
148 virtual void setRectRelativeToParentNode(ScrollingNodeID, const LayoutRect&) { }
149 virtual void setScrollingNodeScrollableAreaGeometry(ScrollingNodeID, ScrollableArea&) { }
150 virtual void setFrameScrollingNodeState(ScrollingNodeID, const FrameView&) { }
151 virtual void setViewportConstraintedNodeConstraints(ScrollingNodeID, const ViewportConstraints&) { }
152 virtual void setPositionedNodeGeometry(ScrollingNodeID, const LayoutConstraints&) { }
153 virtual void setRelatedOverflowScrollingNodes(ScrollingNodeID, Vector<ScrollingNodeID>&&) { }
154
155 virtual void reconcileViewportConstrainedLayerPositions(ScrollingNodeID, const LayoutRect&, ScrollingLayerPositionAction) { }
156 virtual String scrollingStateTreeAsText(ScrollingStateTreeAsTextBehavior = ScrollingStateTreeAsTextBehaviorNormal) const;
157 virtual bool isRubberBandInProgress() const { return false; }
158 virtual bool isScrollSnapInProgress() const { return false; }
159 virtual void updateScrollSnapPropertiesWithFrameView(const FrameView&) { }
160 virtual void setScrollPinningBehavior(ScrollPinningBehavior) { }
161
162 // Generated a unique id for scrolling nodes.
163 ScrollingNodeID uniqueScrollingNodeID();
164
165 enum MainThreadScrollingReasonFlags {
166 ForcedOnMainThread = 1 << 0,
167 HasSlowRepaintObjects = 1 << 1,
168 HasViewportConstrainedObjectsWithoutSupportingFixedLayers = 1 << 2,
169 HasNonLayerViewportConstrainedObjects = 1 << 3,
170 IsImageDocument = 1 << 4
171 };
172
173 SynchronousScrollingReasons synchronousScrollingReasons(const FrameView&) const;
174 bool shouldUpdateScrollLayerPositionSynchronously(const FrameView&) const;
175
176 virtual void willDestroyScrollableArea(ScrollableArea&) { }
177 virtual void scrollableAreaScrollbarLayerDidChange(ScrollableArea&, ScrollbarOrientation) { }
178
179 static String synchronousScrollingReasonsAsText(SynchronousScrollingReasons);
180 String synchronousScrollingReasonsAsText() const;
181
182 EventTrackingRegions absoluteEventTrackingRegions() const;
183 virtual void updateExpectsWheelEventTestTriggerWithFrameView(const FrameView&) { }
184
185protected:
186 explicit ScrollingCoordinator(Page*);
187
188 GraphicsLayer* scrollContainerLayerForFrameView(FrameView&);
189 GraphicsLayer* scrolledContentsLayerForFrameView(FrameView&);
190 GraphicsLayer* counterScrollingLayerForFrameView(FrameView&);
191 GraphicsLayer* insetClipLayerForFrameView(FrameView&);
192 GraphicsLayer* rootContentsLayerForFrameView(FrameView&);
193 GraphicsLayer* contentShadowLayerForFrameView(FrameView&);
194 GraphicsLayer* headerLayerForFrameView(FrameView&);
195 GraphicsLayer* footerLayerForFrameView(FrameView&);
196
197 virtual void willCommitTree() { }
198
199 Page* m_page; // FIXME: ideally this would be a reference but it gets nulled on async teardown.
200
201private:
202 virtual void setSynchronousScrollingReasons(FrameView&, SynchronousScrollingReasons) { }
203
204 virtual bool hasVisibleSlowRepaintViewportConstrainedObjects(const FrameView&) const;
205 void updateSynchronousScrollingReasons(FrameView&);
206 void updateSynchronousScrollingReasonsForAllFrames();
207
208 EventTrackingRegions absoluteEventTrackingRegionsForFrame(const Frame&) const;
209
210 bool m_forceSynchronousScrollLayerPositionUpdates { false };
211};
212
213WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ScrollableAreaParameters);
214WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ScrollingNodeType);
215WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ScrollingLayerPositionAction);
216WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ViewportRectStability);
217WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ScrollType);
218
219} // namespace WebCore
220
221#define SPECIALIZE_TYPE_TRAITS_SCROLLING_COORDINATOR(ToValueTypeName, predicate) \
222SPECIALIZE_TYPE_TRAITS_BEGIN(ToValueTypeName) \
223 static bool isType(const WebCore::ScrollingCoordinator& value) { return value.predicate; } \
224SPECIALIZE_TYPE_TRAITS_END()
225