Implement visual-viewport based position:fixed handling for Mac async scrolling
[WebKit-https.git] / Source / WebCore / page / scrolling / AsyncScrollingCoordinator.cpp
1 /*
2  * Copyright (C) 2014-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 #include "config.h"
27
28 #if ENABLE(ASYNC_SCROLLING)
29 #include "AsyncScrollingCoordinator.h"
30
31 #include "Document.h"
32 #include "EditorClient.h"
33 #include "Frame.h"
34 #include "FrameView.h"
35 #include "GraphicsLayer.h"
36 #include "Logging.h"
37 #include "MainFrame.h"
38 #include "Page.h"
39 #include "ScrollAnimator.h"
40 #include "ScrollingConstraints.h"
41 #include "ScrollingStateFixedNode.h"
42 #include "ScrollingStateFrameScrollingNode.h"
43 #include "ScrollingStateOverflowScrollingNode.h"
44 #include "ScrollingStateStickyNode.h"
45 #include "ScrollingStateTree.h"
46 #include "Settings.h"
47 #include "WheelEventTestTrigger.h"
48
49 namespace WebCore {
50
51 AsyncScrollingCoordinator::AsyncScrollingCoordinator(Page* page)
52     : ScrollingCoordinator(page)
53     , m_updateNodeScrollPositionTimer(*this, &AsyncScrollingCoordinator::updateScrollPositionAfterAsyncScrollTimerFired)
54     , m_scrollingStateTree(std::make_unique<ScrollingStateTree>(this))
55 {
56 }
57
58 AsyncScrollingCoordinator::~AsyncScrollingCoordinator()
59 {
60 }
61
62 void AsyncScrollingCoordinator::scrollingStateTreePropertiesChanged()
63 {
64     scheduleTreeStateCommit();
65 }
66
67 static inline void setStateScrollingNodeSnapOffsetsAsFloat(ScrollingStateScrollingNode& node, ScrollEventAxis axis, const Vector<LayoutUnit>* snapOffsets, float deviceScaleFactor)
68 {
69     // FIXME: Incorporate current page scale factor in snapping to device pixel. Perhaps we should just convert to float here and let UI process do the pixel snapping?
70     Vector<float> snapOffsetsAsFloat;
71     if (snapOffsets) {
72         snapOffsetsAsFloat.reserveInitialCapacity(snapOffsets->size());
73         for (auto& offset : *snapOffsets)
74             snapOffsetsAsFloat.uncheckedAppend(roundToDevicePixel(offset, deviceScaleFactor, false));
75     }
76     if (axis == ScrollEventAxis::Horizontal)
77         node.setHorizontalSnapOffsets(snapOffsetsAsFloat);
78     else
79         node.setVerticalSnapOffsets(snapOffsetsAsFloat);
80 }
81
82 void AsyncScrollingCoordinator::setEventTrackingRegionsDirty()
83 {
84     m_eventTrackingRegionsDirty = true;
85     // We have to schedule a commit, but the computed non-fast region may not have actually changed.
86     scheduleTreeStateCommit();
87 }
88
89 void AsyncScrollingCoordinator::willCommitTree()
90 {
91     updateEventTrackingRegions();
92 }
93
94 void AsyncScrollingCoordinator::updateEventTrackingRegions()
95 {
96     if (!m_eventTrackingRegionsDirty)
97         return;
98
99     if (!m_scrollingStateTree->rootStateNode())
100         return;
101
102     m_scrollingStateTree->rootStateNode()->setEventTrackingRegions(absoluteEventTrackingRegions());
103     m_eventTrackingRegionsDirty = false;
104 }
105
106 void AsyncScrollingCoordinator::frameViewLayoutUpdated(FrameView& frameView)
107 {
108     ASSERT(isMainThread());
109     ASSERT(m_page);
110
111     // If there isn't a root node yet, don't do anything. We'll be called again after creating one.
112     if (!m_scrollingStateTree->rootStateNode())
113         return;
114
115     // Compute the region of the page that we can't do fast scrolling for. This currently includes
116     // all scrollable areas, such as subframes, overflow divs and list boxes. We need to do this even if the
117     // frame view whose layout was updated is not the main frame.
118     // In the future, we may want to have the ability to set non-fast scrolling regions for more than
119     // just the root node. But right now, this concept only applies to the root.
120     m_scrollingStateTree->rootStateNode()->setEventTrackingRegions(absoluteEventTrackingRegions());
121     m_eventTrackingRegionsDirty = false;
122
123     if (!coordinatesScrollingForFrameView(frameView))
124         return;
125
126     ScrollingStateFrameScrollingNode* node = downcast<ScrollingStateFrameScrollingNode>(m_scrollingStateTree->stateNodeForID(frameView.scrollLayerID()));
127     if (!node)
128         return;
129
130     Scrollbar* verticalScrollbar = frameView.verticalScrollbar();
131     Scrollbar* horizontalScrollbar = frameView.horizontalScrollbar();
132     node->setScrollerImpsFromScrollbars(verticalScrollbar, horizontalScrollbar);
133     
134     node->setFrameScaleFactor(frameView.frame().frameScaleFactor());
135     node->setHeaderHeight(frameView.headerHeight());
136     node->setFooterHeight(frameView.footerHeight());
137     node->setTopContentInset(frameView.topContentInset());
138
139     node->setVisualViewportEnabled(visualViewportEnabled());
140     node->setLayoutViewport(frameView.layoutViewportRect());
141     node->setMinLayoutViewportOrigin(frameView.minStableLayoutViewportOrigin());
142     node->setMaxLayoutViewportOrigin(frameView.maxStableLayoutViewportOrigin());
143
144     node->setScrollOrigin(frameView.scrollOrigin());
145     node->setScrollableAreaSize(frameView.visibleContentRect().size());
146     node->setTotalContentsSize(frameView.totalContentsSize());
147     node->setReachableContentsSize(frameView.totalContentsSize());
148     node->setFixedElementsLayoutRelativeToFrame(frameView.fixedElementsLayoutRelativeToFrame());
149
150 #if ENABLE(CSS_SCROLL_SNAP)
151     frameView.updateSnapOffsets();
152     updateScrollSnapPropertiesWithFrameView(frameView);
153 #endif
154
155 #if PLATFORM(COCOA)
156     Page* page = frameView.frame().page();
157     if (page && page->expectsWheelEventTriggers()) {
158         LOG(WheelEventTestTriggers, "    AsyncScrollingCoordinator::frameViewLayoutUpdated: Expects wheel event test trigger=%d", page->expectsWheelEventTriggers());
159         node->setExpectsWheelEventTestTrigger(page->expectsWheelEventTriggers());
160     }
161 #endif
162
163     ScrollableAreaParameters scrollParameters;
164     scrollParameters.horizontalScrollElasticity = frameView.horizontalScrollElasticity();
165     scrollParameters.verticalScrollElasticity = frameView.verticalScrollElasticity();
166     scrollParameters.hasEnabledHorizontalScrollbar = horizontalScrollbar && horizontalScrollbar->enabled();
167     scrollParameters.hasEnabledVerticalScrollbar = verticalScrollbar && verticalScrollbar->enabled();
168     scrollParameters.horizontalScrollbarMode = frameView.horizontalScrollbarMode();
169     scrollParameters.verticalScrollbarMode = frameView.verticalScrollbarMode();
170
171     node->setScrollableAreaParameters(scrollParameters);
172 }
173
174 void AsyncScrollingCoordinator::frameViewEventTrackingRegionsChanged(FrameView&)
175 {
176     if (!m_scrollingStateTree->rootStateNode())
177         return;
178
179     setEventTrackingRegionsDirty();
180 }
181
182 void AsyncScrollingCoordinator::frameViewRootLayerDidChange(FrameView& frameView)
183 {
184     ASSERT(isMainThread());
185     ASSERT(m_page);
186
187     if (!coordinatesScrollingForFrameView(frameView))
188         return;
189     
190     // FIXME: In some navigation scenarios, the FrameView has no RenderView or that RenderView has not been composited.
191     // This needs cleaning up: https://bugs.webkit.org/show_bug.cgi?id=132724
192     if (!frameView.scrollLayerID())
193         return;
194     
195     // If the root layer does not have a ScrollingStateNode, then we should create one.
196     ensureRootStateNodeForFrameView(frameView);
197     ASSERT(m_scrollingStateTree->rootStateNode());
198
199     ScrollingCoordinator::frameViewRootLayerDidChange(frameView);
200
201     ScrollingStateFrameScrollingNode* node = downcast<ScrollingStateFrameScrollingNode>(m_scrollingStateTree->stateNodeForID(frameView.scrollLayerID()));
202     node->setLayer(scrollLayerForFrameView(frameView));
203     node->setScrolledContentsLayer(rootContentLayerForFrameView(frameView));
204     node->setCounterScrollingLayer(counterScrollingLayerForFrameView(frameView));
205     node->setInsetClipLayer(insetClipLayerForFrameView(frameView));
206     node->setContentShadowLayer(contentShadowLayerForFrameView(frameView));
207     node->setHeaderLayer(headerLayerForFrameView(frameView));
208     node->setFooterLayer(footerLayerForFrameView(frameView));
209     node->setScrollBehaviorForFixedElements(frameView.scrollBehaviorForFixedElements());
210 }
211
212 bool AsyncScrollingCoordinator::requestScrollPositionUpdate(FrameView& frameView, const IntPoint& scrollPosition)
213 {
214     ASSERT(isMainThread());
215     ASSERT(m_page);
216
217     if (!coordinatesScrollingForFrameView(frameView))
218         return false;
219
220     bool isProgrammaticScroll = frameView.inProgrammaticScroll();
221     if (isProgrammaticScroll || frameView.frame().document()->pageCacheState() != Document::NotInPageCache)
222         updateScrollPositionAfterAsyncScroll(frameView.scrollLayerID(), scrollPosition, Nullopt, isProgrammaticScroll, SetScrollingLayerPosition);
223
224     // If this frame view's document is being put into the page cache, we don't want to update our
225     // main frame scroll position. Just let the FrameView think that we did.
226     if (frameView.frame().document()->pageCacheState() != Document::NotInPageCache)
227         return true;
228
229     ScrollingStateScrollingNode* stateNode = downcast<ScrollingStateScrollingNode>(m_scrollingStateTree->stateNodeForID(frameView.scrollLayerID()));
230     if (!stateNode)
231         return false;
232
233     stateNode->setRequestedScrollPosition(scrollPosition, isProgrammaticScroll);
234     return true;
235 }
236
237 void AsyncScrollingCoordinator::scheduleUpdateScrollPositionAfterAsyncScroll(ScrollingNodeID nodeID, const FloatPoint& scrollPosition, const Optional<FloatPoint>& layoutViewportOrigin, bool programmaticScroll, SetOrSyncScrollingLayerPosition scrollingLayerPositionAction)
238 {
239     ScheduledScrollUpdate scrollUpdate(nodeID, scrollPosition, layoutViewportOrigin, programmaticScroll, scrollingLayerPositionAction);
240     
241     // For programmatic scrolls, requestScrollPositionUpdate() has already called updateScrollPositionAfterAsyncScroll().
242     if (programmaticScroll)
243         return;
244
245     if (m_updateNodeScrollPositionTimer.isActive()) {
246         if (m_scheduledScrollUpdate.matchesUpdateType(scrollUpdate)) {
247             m_scheduledScrollUpdate.scrollPosition = scrollPosition;
248             return;
249         }
250     
251         // If the parameters don't match what was previously scheduled, dispatch immediately.
252         m_updateNodeScrollPositionTimer.stop();
253         updateScrollPositionAfterAsyncScroll(m_scheduledScrollUpdate.nodeID, m_scheduledScrollUpdate.scrollPosition, m_scheduledScrollUpdate.layoutViewportOrigin, m_scheduledScrollUpdate.isProgrammaticScroll, m_scheduledScrollUpdate.updateLayerPositionAction);
254         updateScrollPositionAfterAsyncScroll(nodeID, scrollPosition, layoutViewportOrigin, programmaticScroll, scrollingLayerPositionAction);
255         return;
256     }
257
258     m_scheduledScrollUpdate = scrollUpdate;
259     m_updateNodeScrollPositionTimer.startOneShot(0);
260 }
261
262 void AsyncScrollingCoordinator::updateScrollPositionAfterAsyncScrollTimerFired()
263 {
264     updateScrollPositionAfterAsyncScroll(m_scheduledScrollUpdate.nodeID, m_scheduledScrollUpdate.scrollPosition, m_scheduledScrollUpdate.layoutViewportOrigin, m_scheduledScrollUpdate.isProgrammaticScroll, m_scheduledScrollUpdate.updateLayerPositionAction);
265 }
266
267 FrameView* AsyncScrollingCoordinator::frameViewForScrollingNode(ScrollingNodeID scrollingNodeID) const
268 {
269     if (!m_scrollingStateTree->rootStateNode())
270         return nullptr;
271     
272     if (scrollingNodeID == m_scrollingStateTree->rootStateNode()->scrollingNodeID())
273         return m_page->mainFrame().view();
274
275     ScrollingStateNode* stateNode = m_scrollingStateTree->stateNodeForID(scrollingNodeID);
276     if (!stateNode)
277         return nullptr;
278
279     // Find the enclosing frame scrolling node.
280     ScrollingStateNode* parentNode = stateNode;
281     while (parentNode && parentNode->nodeType() != FrameScrollingNode)
282         parentNode = parentNode->parent();
283     
284     if (!parentNode)
285         return nullptr;
286     
287     // Walk the frame tree to find the matching FrameView. This is not ideal, but avoids back pointers to FrameViews
288     // from ScrollingTreeStateNodes.
289     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
290         if (FrameView* view = frame->view()) {
291             if (view->scrollLayerID() == parentNode->scrollingNodeID())
292                 return view;
293         }
294     }
295
296     return nullptr;
297 }
298
299 void AsyncScrollingCoordinator::updateScrollPositionAfterAsyncScroll(ScrollingNodeID scrollingNodeID, const FloatPoint& scrollPosition, Optional<FloatPoint> layoutViewportOrigin, bool programmaticScroll, SetOrSyncScrollingLayerPosition scrollingLayerPositionAction)
300 {
301     ASSERT(isMainThread());
302
303     if (!m_page)
304         return;
305
306     FrameView* frameViewPtr = frameViewForScrollingNode(scrollingNodeID);
307     if (!frameViewPtr)
308         return;
309
310     FrameView& frameView = *frameViewPtr;
311
312     if (scrollingNodeID == frameView.scrollLayerID()) {
313         bool oldProgrammaticScroll = frameView.inProgrammaticScroll();
314         frameView.setInProgrammaticScroll(programmaticScroll);
315
316         if (layoutViewportOrigin)
317             frameView.setLayoutViewportOrigin(LayoutPoint(layoutViewportOrigin.value()), FrameView::TriggerLayoutOrNot::No);
318
319         frameView.setConstrainsScrollingToContentEdge(false);
320         frameView.notifyScrollPositionChanged(roundedIntPoint(scrollPosition));
321         frameView.setConstrainsScrollingToContentEdge(true);
322         frameView.setInProgrammaticScroll(oldProgrammaticScroll);
323
324         if (GraphicsLayer* scrollLayer = scrollLayerForFrameView(frameView)) {
325             GraphicsLayer* counterScrollingLayer = counterScrollingLayerForFrameView(frameView);
326             GraphicsLayer* insetClipLayer = insetClipLayerForFrameView(frameView);
327             GraphicsLayer* contentShadowLayer = contentShadowLayerForFrameView(frameView);
328             GraphicsLayer* scrolledContentsLayer = rootContentLayerForFrameView(frameView);
329             GraphicsLayer* headerLayer = headerLayerForFrameView(frameView);
330             GraphicsLayer* footerLayer = footerLayerForFrameView(frameView);
331
332             ASSERT(frameView.scrollPosition() == roundedIntPoint(scrollPosition));
333             LayoutPoint scrollPositionForFixed = visualViewportEnabled() ? frameView.layoutViewportOrigin() : frameView.scrollPositionForFixedPosition();
334             float topContentInset = frameView.topContentInset();
335
336             FloatPoint positionForInsetClipLayer;
337             if (insetClipLayer)
338                 positionForInsetClipLayer = FloatPoint(insetClipLayer->position().x(), FrameView::yPositionForInsetClipLayer(scrollPosition, topContentInset));
339             FloatPoint positionForContentsLayer = frameView.positionForRootContentLayer();
340             
341             FloatPoint positionForHeaderLayer = FloatPoint(scrollPositionForFixed.x(), FrameView::yPositionForHeaderLayer(scrollPosition, topContentInset));
342             FloatPoint positionForFooterLayer = FloatPoint(scrollPositionForFixed.x(),
343                 FrameView::yPositionForFooterLayer(scrollPosition, topContentInset, frameView.totalContentsSize().height(), frameView.footerHeight()));
344
345             if (programmaticScroll || scrollingLayerPositionAction == SetScrollingLayerPosition) {
346                 scrollLayer->setPosition(-frameView.scrollPosition());
347                 if (counterScrollingLayer)
348                     counterScrollingLayer->setPosition(scrollPositionForFixed);
349                 if (insetClipLayer)
350                     insetClipLayer->setPosition(positionForInsetClipLayer);
351                 if (contentShadowLayer)
352                     contentShadowLayer->setPosition(positionForContentsLayer);
353                 if (scrolledContentsLayer)
354                     scrolledContentsLayer->setPosition(positionForContentsLayer);
355                 if (headerLayer)
356                     headerLayer->setPosition(positionForHeaderLayer);
357                 if (footerLayer)
358                     footerLayer->setPosition(positionForFooterLayer);
359             } else {
360                 scrollLayer->syncPosition(-frameView.scrollPosition());
361                 if (counterScrollingLayer)
362                     counterScrollingLayer->syncPosition(scrollPositionForFixed);
363                 if (insetClipLayer)
364                     insetClipLayer->syncPosition(positionForInsetClipLayer);
365                 if (contentShadowLayer)
366                     contentShadowLayer->syncPosition(positionForContentsLayer);
367                 if (scrolledContentsLayer)
368                     scrolledContentsLayer->syncPosition(positionForContentsLayer);
369                 if (headerLayer)
370                     headerLayer->syncPosition(positionForHeaderLayer);
371                 if (footerLayer)
372                     footerLayer->syncPosition(positionForFooterLayer);
373
374                 LayoutRect viewportRect = frameView.rectForFixedPositionLayout();
375                 syncChildPositions(viewportRect);
376             }
377         }
378
379 #if PLATFORM(COCOA)
380         if (m_page->expectsWheelEventTriggers()) {
381             frameView.scrollAnimator().setWheelEventTestTrigger(m_page->testTrigger());
382             if (const auto& trigger = m_page->testTrigger())
383                 trigger->removeTestDeferralForReason(reinterpret_cast<WheelEventTestTrigger::ScrollableAreaIdentifier>(scrollingNodeID), WheelEventTestTrigger::ScrollingThreadSyncNeeded);
384         }
385 #endif
386         
387         return;
388     }
389
390     // Overflow-scroll area.
391     if (ScrollableArea* scrollableArea = frameView.scrollableAreaForScrollLayerID(scrollingNodeID)) {
392         scrollableArea->setIsUserScroll(scrollingLayerPositionAction == SyncScrollingLayerPosition);
393         scrollableArea->scrollToOffsetWithoutAnimation(scrollPosition);
394         scrollableArea->setIsUserScroll(false);
395         if (scrollingLayerPositionAction == SetScrollingLayerPosition)
396             m_page->editorClient().overflowScrollPositionChanged();
397
398 #if PLATFORM(COCOA)
399         if (m_page->expectsWheelEventTriggers()) {
400             frameView.scrollAnimator().setWheelEventTestTrigger(m_page->testTrigger());
401             if (const auto& trigger = m_page->testTrigger())
402                 trigger->removeTestDeferralForReason(reinterpret_cast<WheelEventTestTrigger::ScrollableAreaIdentifier>(scrollingNodeID), WheelEventTestTrigger::ScrollingThreadSyncNeeded);
403         }
404 #endif
405     }
406 }
407
408 void AsyncScrollingCoordinator::scrollableAreaScrollbarLayerDidChange(ScrollableArea& scrollableArea, ScrollbarOrientation orientation)
409 {
410     ASSERT(isMainThread());
411     ASSERT(m_page);
412
413     if (&scrollableArea != static_cast<ScrollableArea*>(m_page->mainFrame().view()))
414         return;
415
416     if (orientation == VerticalScrollbar)
417         scrollableArea.verticalScrollbarLayerDidChange();
418     else
419         scrollableArea.horizontalScrollbarLayerDidChange();
420 }
421
422 ScrollingNodeID AsyncScrollingCoordinator::attachToStateTree(ScrollingNodeType nodeType, ScrollingNodeID newNodeID, ScrollingNodeID parentID)
423 {
424     return m_scrollingStateTree->attachNode(nodeType, newNodeID, parentID);
425 }
426
427 void AsyncScrollingCoordinator::detachFromStateTree(ScrollingNodeID nodeID)
428 {
429     m_scrollingStateTree->detachNode(nodeID);
430 }
431
432 void AsyncScrollingCoordinator::clearStateTree()
433 {
434     m_scrollingStateTree->clear();
435 }
436
437 void AsyncScrollingCoordinator::syncChildPositions(const LayoutRect& viewportRect)
438 {
439     if (!m_scrollingStateTree->rootStateNode())
440         return;
441
442     auto children = m_scrollingStateTree->rootStateNode()->children();
443     if (!children)
444         return;
445
446     // FIXME: We'll have to traverse deeper into the tree at some point.
447     for (auto& child : *children)
448         child->syncLayerPositionForViewportRect(viewportRect);
449 }
450
451 void AsyncScrollingCoordinator::ensureRootStateNodeForFrameView(FrameView& frameView)
452 {
453     ASSERT(frameView.scrollLayerID());
454     attachToStateTree(FrameScrollingNode, frameView.scrollLayerID(), 0);
455 }
456
457 void AsyncScrollingCoordinator::updateFrameScrollingNode(ScrollingNodeID nodeID, GraphicsLayer* layer, GraphicsLayer* scrolledContentsLayer, GraphicsLayer* counterScrollingLayer, GraphicsLayer* insetClipLayer, const ScrollingGeometry* scrollingGeometry)
458 {
459     ScrollingStateFrameScrollingNode* node = downcast<ScrollingStateFrameScrollingNode>(m_scrollingStateTree->stateNodeForID(nodeID));
460     ASSERT(node);
461     if (!node)
462         return;
463
464     node->setLayer(layer);
465     node->setInsetClipLayer(insetClipLayer);
466     node->setScrolledContentsLayer(scrolledContentsLayer);
467     node->setCounterScrollingLayer(counterScrollingLayer);
468
469     if (scrollingGeometry) {
470         node->setScrollOrigin(scrollingGeometry->scrollOrigin);
471         node->setScrollPosition(scrollingGeometry->scrollPosition);
472         node->setTotalContentsSize(scrollingGeometry->contentSize);
473         node->setReachableContentsSize(scrollingGeometry->reachableContentSize);
474         node->setScrollableAreaSize(scrollingGeometry->scrollableAreaSize);
475     }
476 }
477     
478 void AsyncScrollingCoordinator::updateOverflowScrollingNode(ScrollingNodeID nodeID, GraphicsLayer* layer, GraphicsLayer* scrolledContentsLayer, const ScrollingGeometry* scrollingGeometry)
479 {
480     ScrollingStateOverflowScrollingNode* node = downcast<ScrollingStateOverflowScrollingNode>(m_scrollingStateTree->stateNodeForID(nodeID));
481     ASSERT(node);
482     if (!node)
483         return;
484
485     node->setLayer(layer);
486     node->setScrolledContentsLayer(scrolledContentsLayer);
487     
488     if (scrollingGeometry) {
489         node->setScrollOrigin(scrollingGeometry->scrollOrigin);
490         node->setScrollPosition(scrollingGeometry->scrollPosition);
491         node->setTotalContentsSize(scrollingGeometry->contentSize);
492         node->setReachableContentsSize(scrollingGeometry->reachableContentSize);
493         node->setScrollableAreaSize(scrollingGeometry->scrollableAreaSize);
494 #if ENABLE(CSS_SCROLL_SNAP)
495         setStateScrollingNodeSnapOffsetsAsFloat(*node, ScrollEventAxis::Horizontal, &scrollingGeometry->horizontalSnapOffsets, m_page->deviceScaleFactor());
496         setStateScrollingNodeSnapOffsetsAsFloat(*node, ScrollEventAxis::Vertical, &scrollingGeometry->verticalSnapOffsets, m_page->deviceScaleFactor());
497         node->setCurrentHorizontalSnapPointIndex(scrollingGeometry->currentHorizontalSnapPointIndex);
498         node->setCurrentVerticalSnapPointIndex(scrollingGeometry->currentVerticalSnapPointIndex);
499 #endif
500     }
501 }
502
503 void AsyncScrollingCoordinator::updateViewportConstrainedNode(ScrollingNodeID nodeID, const ViewportConstraints& constraints, GraphicsLayer* graphicsLayer)
504 {
505     ASSERT(supportsFixedPositionLayers());
506
507     ScrollingStateNode* node = m_scrollingStateTree->stateNodeForID(nodeID);
508     if (!node)
509         return;
510
511     switch (constraints.constraintType()) {
512     case ViewportConstraints::FixedPositionConstraint: {
513         ScrollingStateFixedNode& fixedNode = downcast<ScrollingStateFixedNode>(*node);
514         fixedNode.setLayer(graphicsLayer);
515         fixedNode.updateConstraints((const FixedPositionViewportConstraints&)constraints);
516         break;
517     }
518     case ViewportConstraints::StickyPositionConstraint: {
519         ScrollingStateStickyNode& stickyNode = downcast<ScrollingStateStickyNode>(*node);
520         stickyNode.setLayer(graphicsLayer);
521         stickyNode.updateConstraints((const StickyPositionViewportConstraints&)constraints);
522         break;
523     }
524     }
525 }
526
527 void AsyncScrollingCoordinator::setSynchronousScrollingReasons(SynchronousScrollingReasons reasons)
528 {
529     if (!m_scrollingStateTree->rootStateNode())
530         return;
531
532     // The FrameView's GraphicsLayer is likely to be out-of-synch with the PlatformLayer
533     // at this point. So we'll update it before we switch back to main thread scrolling
534     // in order to avoid layer positioning bugs.
535     if (reasons)
536         updateMainFrameScrollLayerPosition();
537     m_scrollingStateTree->rootStateNode()->setSynchronousScrollingReasons(reasons);
538 }
539
540 void AsyncScrollingCoordinator::updateMainFrameScrollLayerPosition()
541 {
542     ASSERT(isMainThread());
543
544     if (!m_page)
545         return;
546
547     FrameView* frameView = m_page->mainFrame().view();
548     if (!frameView)
549         return;
550
551     if (GraphicsLayer* scrollLayer = scrollLayerForFrameView(*frameView))
552         scrollLayer->setPosition(-frameView->scrollPosition());
553 }
554
555 bool AsyncScrollingCoordinator::isRubberBandInProgress() const
556 {
557     return scrollingTree()->isRubberBandInProgress();
558 }
559
560 void AsyncScrollingCoordinator::setScrollPinningBehavior(ScrollPinningBehavior pinning)
561 {
562     scrollingTree()->setScrollPinningBehavior(pinning);
563 }
564
565 bool AsyncScrollingCoordinator::visualViewportEnabled() const
566 {
567     return m_page->mainFrame().settings().visualViewportEnabled();
568 }
569
570 String AsyncScrollingCoordinator::scrollingStateTreeAsText() const
571 {
572     if (m_scrollingStateTree->rootStateNode()) {
573         if (m_eventTrackingRegionsDirty)
574             m_scrollingStateTree->rootStateNode()->setEventTrackingRegions(absoluteEventTrackingRegions());
575         return m_scrollingStateTree->rootStateNode()->scrollingStateTreeAsText();
576     }
577
578     return String();
579 }
580
581 #if PLATFORM(COCOA)
582 void AsyncScrollingCoordinator::setActiveScrollSnapIndices(ScrollingNodeID scrollingNodeID, unsigned horizontalIndex, unsigned verticalIndex)
583 {
584     ASSERT(isMainThread());
585     
586     if (!m_page)
587         return;
588     
589     FrameView* frameView = frameViewForScrollingNode(scrollingNodeID);
590     if (!frameView)
591         return;
592     
593     if (scrollingNodeID == frameView->scrollLayerID()) {
594         frameView->setCurrentHorizontalSnapPointIndex(horizontalIndex);
595         frameView->setCurrentVerticalSnapPointIndex(verticalIndex);
596         return;
597     }
598     
599     // Overflow-scroll area.
600     if (ScrollableArea* scrollableArea = frameView->scrollableAreaForScrollLayerID(scrollingNodeID)) {
601         scrollableArea->setCurrentHorizontalSnapPointIndex(horizontalIndex);
602         scrollableArea->setCurrentVerticalSnapPointIndex(verticalIndex);
603     }
604 }
605
606 void AsyncScrollingCoordinator::deferTestsForReason(WheelEventTestTrigger::ScrollableAreaIdentifier identifier, WheelEventTestTrigger::DeferTestTriggerReason reason) const
607 {
608     ASSERT(isMainThread());
609     if (!m_page || !m_page->expectsWheelEventTriggers())
610         return;
611
612     if (const auto& trigger = m_page->testTrigger()) {
613         LOG(WheelEventTestTriggers, "    (!) AsyncScrollingCoordinator::deferTestsForReason: Deferring %p for reason %d.", identifier, reason);
614         trigger->deferTestsForReason(identifier, reason);
615     }
616 }
617
618 void AsyncScrollingCoordinator::removeTestDeferralForReason(WheelEventTestTrigger::ScrollableAreaIdentifier identifier, WheelEventTestTrigger::DeferTestTriggerReason reason) const
619 {
620     ASSERT(isMainThread());
621     if (!m_page || !m_page->expectsWheelEventTriggers())
622         return;
623
624     if (const auto& trigger = m_page->testTrigger()) {
625         LOG(WheelEventTestTriggers, "    (!) AsyncScrollingCoordinator::removeTestDeferralForReason: Deferring %p for reason %d.", identifier, reason);
626         trigger->removeTestDeferralForReason(identifier, reason);
627     }
628 }
629 #endif
630
631 #if ENABLE(CSS_SCROLL_SNAP)
632 bool AsyncScrollingCoordinator::isScrollSnapInProgress() const
633 {
634     return scrollingTree()->isScrollSnapInProgress();
635 }
636
637 void AsyncScrollingCoordinator::updateScrollSnapPropertiesWithFrameView(const FrameView& frameView)
638 {
639     if (auto node = downcast<ScrollingStateFrameScrollingNode>(m_scrollingStateTree->stateNodeForID(frameView.scrollLayerID()))) {
640         setStateScrollingNodeSnapOffsetsAsFloat(*node, ScrollEventAxis::Horizontal, frameView.horizontalSnapOffsets(), m_page->deviceScaleFactor());
641         setStateScrollingNodeSnapOffsetsAsFloat(*node, ScrollEventAxis::Vertical, frameView.verticalSnapOffsets(), m_page->deviceScaleFactor());
642         node->setCurrentHorizontalSnapPointIndex(frameView.currentHorizontalSnapPointIndex());
643         node->setCurrentVerticalSnapPointIndex(frameView.currentVerticalSnapPointIndex());
644     }
645 }
646 #endif
647     
648 } // namespace WebCore
649
650 #endif // ENABLE(ASYNC_SCROLLING)