Work towards having frames in the scrolling tree
[WebKit-https.git] / Source / WebCore / page / scrolling / AsyncScrollingCoordinator.cpp
1 /*
2  * Copyright (C) 2014 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 "Frame.h"
32 #include "FrameView.h"
33 #include "GraphicsLayer.h"
34 #include "MainFrame.h"
35 #include "Page.h"
36 #include "ScrollingConstraints.h"
37 #include "ScrollingStateFixedNode.h"
38 #include "ScrollingStateFrameScrollingNode.h"
39 #include "ScrollingStateOverflowScrollingNode.h"
40 #include "ScrollingStateStickyNode.h"
41 #include "ScrollingStateTree.h"
42
43 namespace WebCore {
44
45 AsyncScrollingCoordinator::AsyncScrollingCoordinator(Page* page)
46     : ScrollingCoordinator(page)
47     , m_updateNodeScrollPositionTimer(this, &AsyncScrollingCoordinator::updateScrollPositionAfterAsyncScrollTimerFired)
48     , m_scrollingStateTree(ScrollingStateTree::create(this))
49 {
50 }
51
52 AsyncScrollingCoordinator::~AsyncScrollingCoordinator()
53 {
54 }
55
56 void AsyncScrollingCoordinator::scrollingStateTreePropertiesChanged()
57 {
58     scheduleTreeStateCommit();
59 }
60
61 void AsyncScrollingCoordinator::frameViewLayoutUpdated(FrameView* frameView)
62 {
63     ASSERT(isMainThread());
64     ASSERT(m_page);
65
66     // If there isn't a root node yet, don't do anything. We'll be called again after creating one.
67     if (!m_scrollingStateTree->rootStateNode())
68         return;
69
70     // Compute the region of the page that we can't do fast scrolling for. This currently includes
71     // all scrollable areas, such as subframes, overflow divs and list boxes. We need to do this even if the
72     // frame view whose layout was updated is not the main frame.
73     // In the future, we may want to have the ability to set non-fast scrolling regions for more than
74     // just the root node. But right now, this concept only applies to the root.
75     if (frameView->frame().isMainFrame())
76         m_scrollingStateTree->rootStateNode()->setNonFastScrollableRegion(computeNonFastScrollableRegion(&frameView->frame(), IntPoint()));
77
78     if (!coordinatesScrollingForFrameView(frameView))
79         return;
80
81     ScrollingStateFrameScrollingNode* node = toScrollingStateFrameScrollingNode(m_scrollingStateTree->stateNodeForID(frameView->scrollLayerID()));
82     if (!node)
83         return;
84
85     Scrollbar* verticalScrollbar = frameView->verticalScrollbar();
86     Scrollbar* horizontalScrollbar = frameView->horizontalScrollbar();
87     node->setScrollbarPaintersFromScrollbars(verticalScrollbar, horizontalScrollbar);
88     
89     node->setFrameScaleFactor(frameView->frame().frameScaleFactor());
90     node->setHeaderHeight(frameView->headerHeight());
91     node->setFooterHeight(frameView->footerHeight());
92     node->setTopContentInset(frameView->topContentInset());
93
94     node->setScrollOrigin(frameView->scrollOrigin());
95     node->setScrollableAreaSize(frameView->visibleContentRect().size());
96     node->setTotalContentsSize(frameView->totalContentsSize());
97
98     ScrollableAreaParameters scrollParameters;
99     scrollParameters.horizontalScrollElasticity = frameView->horizontalScrollElasticity();
100     scrollParameters.verticalScrollElasticity = frameView->verticalScrollElasticity();
101     scrollParameters.hasEnabledHorizontalScrollbar = horizontalScrollbar && horizontalScrollbar->enabled();
102     scrollParameters.hasEnabledVerticalScrollbar = verticalScrollbar && verticalScrollbar->enabled();
103     scrollParameters.horizontalScrollbarMode = frameView->horizontalScrollbarMode();
104     scrollParameters.verticalScrollbarMode = frameView->verticalScrollbarMode();
105
106     node->setScrollableAreaParameters(scrollParameters);
107 }
108
109 void AsyncScrollingCoordinator::frameViewNonFastScrollableRegionChanged(FrameView* frameView)
110 {
111     if (!m_scrollingStateTree->rootStateNode())
112         return;
113
114     if (frameView->frame().isMainFrame())
115         m_scrollingStateTree->rootStateNode()->setNonFastScrollableRegion(computeNonFastScrollableRegion(&frameView->frame(), IntPoint()));
116 }
117
118 void AsyncScrollingCoordinator::frameViewRootLayerDidChange(FrameView* frameView)
119 {
120     ASSERT(isMainThread());
121     ASSERT(m_page);
122
123     if (!coordinatesScrollingForFrameView(frameView))
124         return;
125     
126     // FIXME: In some navigation scenarios, the FrameView has no RenderView or that RenderView has not been composited.
127     // This needs cleaning up: https://bugs.webkit.org/show_bug.cgi?id=132724
128     if (!frameView->scrollLayerID())
129         return;
130     
131     // If the root layer does not have a ScrollingStateNode, then we should create one.
132     ensureRootStateNodeForFrameView(frameView);
133     ASSERT(m_scrollingStateTree->rootStateNode());
134
135     ScrollingCoordinator::frameViewRootLayerDidChange(frameView);
136
137     ScrollingStateFrameScrollingNode* node = toScrollingStateFrameScrollingNode(m_scrollingStateTree->stateNodeForID(frameView->scrollLayerID()));
138     node->setLayer(scrollLayerForFrameView(frameView));
139     node->setScrolledContentsLayer(rootContentLayerForFrameView(frameView));
140     node->setCounterScrollingLayer(counterScrollingLayerForFrameView(frameView));
141     node->setInsetClipLayer(insetClipLayerForFrameView(frameView));
142     node->setContentShadowLayer(contentShadowLayerForFrameView(frameView));
143     node->setHeaderLayer(headerLayerForFrameView(frameView));
144     node->setFooterLayer(footerLayerForFrameView(frameView));
145     node->setScrollBehaviorForFixedElements(frameView->scrollBehaviorForFixedElements());
146 }
147
148 bool AsyncScrollingCoordinator::requestScrollPositionUpdate(FrameView* frameView, const IntPoint& scrollPosition)
149 {
150     ASSERT(isMainThread());
151     ASSERT(m_page);
152
153     if (!coordinatesScrollingForFrameView(frameView))
154         return false;
155
156     if (frameView->inProgrammaticScroll() || frameView->frame().document()->inPageCache())
157         updateScrollPositionAfterAsyncScroll(frameView->scrollLayerID(), scrollPosition, frameView->inProgrammaticScroll(), SetScrollingLayerPosition);
158
159     // If this frame view's document is being put into the page cache, we don't want to update our
160     // main frame scroll position. Just let the FrameView think that we did.
161     if (frameView->frame().document()->inPageCache())
162         return true;
163
164     ScrollingStateScrollingNode* stateNode = toScrollingStateScrollingNode(m_scrollingStateTree->stateNodeForID(frameView->scrollLayerID()));
165     if (!stateNode)
166         return false;
167
168     stateNode->setRequestedScrollPosition(scrollPosition, frameView->inProgrammaticScroll());
169     return true;
170 }
171
172 void AsyncScrollingCoordinator::scheduleUpdateScrollPositionAfterAsyncScroll(ScrollingNodeID nodeID, const FloatPoint& scrollPosition, bool programmaticScroll, SetOrSyncScrollingLayerPosition scrollingLayerPositionAction)
173 {
174     ScheduledScrollUpdate scrollUpdate(nodeID, scrollPosition, programmaticScroll, scrollingLayerPositionAction);
175     
176     if (m_updateNodeScrollPositionTimer.isActive()) {
177         if (m_scheduledScrollUpdate.matchesUpdateType(scrollUpdate)) {
178             m_scheduledScrollUpdate.scrollPosition = scrollPosition;
179             return;
180         }
181     
182         // If the parameters don't match what was previously scheduled, dispatch immediately.
183         m_updateNodeScrollPositionTimer.stop();
184         updateScrollPositionAfterAsyncScroll(m_scheduledScrollUpdate.nodeID, m_scheduledScrollUpdate.scrollPosition, m_scheduledScrollUpdate.isProgrammaticScroll, m_scheduledScrollUpdate.updateLayerPositionAction);
185         updateScrollPositionAfterAsyncScroll(nodeID, scrollPosition, programmaticScroll, scrollingLayerPositionAction);
186         return;
187     }
188
189     m_scheduledScrollUpdate = scrollUpdate;
190     m_updateNodeScrollPositionTimer.startOneShot(0);
191 }
192
193 void AsyncScrollingCoordinator::updateScrollPositionAfterAsyncScrollTimerFired(Timer<AsyncScrollingCoordinator>*)
194 {
195     updateScrollPositionAfterAsyncScroll(m_scheduledScrollUpdate.nodeID, m_scheduledScrollUpdate.scrollPosition, m_scheduledScrollUpdate.isProgrammaticScroll, m_scheduledScrollUpdate.updateLayerPositionAction);
196 }
197
198 FrameView* AsyncScrollingCoordinator::frameViewForScrollingNode(ScrollingNodeID scrollingNodeID) const
199 {
200     if (scrollingNodeID == m_scrollingStateTree->rootStateNode()->scrollingNodeID())
201         return m_page->mainFrame().view();
202
203     ScrollingStateNode* stateNode = m_scrollingStateTree->stateNodeForID(scrollingNodeID);
204     if (!stateNode)
205         return nullptr;
206
207     // Find the enclosing frame scrolling node.
208     ScrollingStateNode* parentNode = stateNode;
209     while (parentNode && parentNode->nodeType() != FrameScrollingNode)
210         parentNode = parentNode->parent();
211     
212     if (!parentNode)
213         return nullptr;
214     
215     // Walk the frame tree to find the matching FrameView. This is not ideal, but avoids back pointers to FrameViews
216     // from ScrollingTreeStateNodes.
217     for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
218         if (FrameView* view = frame->view()) {
219             if (view->scrollLayerID() == parentNode->scrollingNodeID())
220                 return view;
221         }
222     }
223
224     return nullptr;
225 }
226
227 void AsyncScrollingCoordinator::updateScrollPositionAfterAsyncScroll(ScrollingNodeID scrollingNodeID, const FloatPoint& scrollPosition, bool programmaticScroll, SetOrSyncScrollingLayerPosition scrollingLayerPositionAction)
228 {
229     ASSERT(isMainThread());
230
231     if (!m_page)
232         return;
233
234     FrameView* frameView = frameViewForScrollingNode(scrollingNodeID);
235     if (!frameView)
236         return;
237
238     if (scrollingNodeID == frameView->scrollLayerID()) {
239         bool oldProgrammaticScroll = frameView->inProgrammaticScroll();
240         frameView->setInProgrammaticScroll(programmaticScroll);
241
242         frameView->setConstrainsScrollingToContentEdge(false);
243         frameView->notifyScrollPositionChanged(roundedIntPoint(scrollPosition));
244         frameView->setConstrainsScrollingToContentEdge(true);
245
246         frameView->setInProgrammaticScroll(oldProgrammaticScroll);
247
248         if (GraphicsLayer* scrollLayer = scrollLayerForFrameView(frameView)) {
249             GraphicsLayer* counterScrollingLayer = counterScrollingLayerForFrameView(frameView);
250             GraphicsLayer* insetClipLayer = insetClipLayerForFrameView(frameView);
251             GraphicsLayer* contentShadowLayer = contentShadowLayerForFrameView(frameView);
252             GraphicsLayer* scrolledContentsLayer = rootContentLayerForFrameView(frameView);
253             GraphicsLayer* headerLayer = headerLayerForFrameView(frameView);
254             GraphicsLayer* footerLayer = footerLayerForFrameView(frameView);
255             LayoutSize scrollOffsetForFixed = frameView->scrollOffsetForFixedPosition();
256
257             float topContentInset = frameView->topContentInset();
258             FloatPoint positionForInsetClipLayer = FloatPoint(0, FrameView::yPositionForInsetClipLayer(scrollPosition, topContentInset));
259             FloatPoint positionForContentsLayer = FloatPoint(scrolledContentsLayer->position().x(),
260                 FrameView::yPositionForRootContentLayer(scrollPosition, topContentInset, frameView->headerHeight()));
261             FloatPoint positionForHeaderLayer = FloatPoint(scrollOffsetForFixed.width(), FrameView::yPositionForHeaderLayer(scrollPosition, topContentInset));
262             FloatPoint positionForFooterLayer = FloatPoint(scrollOffsetForFixed.width(),
263                 FrameView::yPositionForFooterLayer(scrollPosition, topContentInset, frameView->totalContentsSize().height(), frameView->footerHeight()));
264
265             if (programmaticScroll || scrollingLayerPositionAction == SetScrollingLayerPosition) {
266                 scrollLayer->setPosition(-frameView->scrollPosition());
267                 if (counterScrollingLayer)
268                     counterScrollingLayer->setPosition(toLayoutPoint(scrollOffsetForFixed));
269                 if (insetClipLayer)
270                     insetClipLayer->setPosition(positionForInsetClipLayer);
271                 if (contentShadowLayer)
272                     contentShadowLayer->setPosition(positionForContentsLayer);
273                 if (scrolledContentsLayer)
274                     scrolledContentsLayer->setPosition(positionForContentsLayer);
275                 if (headerLayer)
276                     headerLayer->setPosition(positionForHeaderLayer);
277                 if (footerLayer)
278                     footerLayer->setPosition(positionForFooterLayer);
279             } else {
280                 scrollLayer->syncPosition(-frameView->scrollPosition());
281                 if (counterScrollingLayer)
282                     counterScrollingLayer->syncPosition(toLayoutPoint(scrollOffsetForFixed));
283                 if (insetClipLayer)
284                     insetClipLayer->syncPosition(positionForInsetClipLayer);
285                 if (contentShadowLayer)
286                     contentShadowLayer->syncPosition(positionForContentsLayer);
287                 if (scrolledContentsLayer)
288                     scrolledContentsLayer->syncPosition(positionForContentsLayer);
289                 if (headerLayer)
290                     headerLayer->syncPosition(positionForHeaderLayer);
291                 if (footerLayer)
292                     footerLayer->syncPosition(positionForFooterLayer);
293
294                 LayoutRect viewportRect = frameView->viewportConstrainedVisibleContentRect();
295                 syncChildPositions(viewportRect);
296             }
297         }
298
299         return;
300     }
301
302     // Overflow-scroll area.
303     if (ScrollableArea* scrollableArea = frameView->scrollableAreaForScrollLayerID(scrollingNodeID)) {
304         scrollableArea->setIsUserScroll(scrollingLayerPositionAction == SyncScrollingLayerPosition);
305         scrollableArea->scrollToOffsetWithoutAnimation(scrollPosition);
306         scrollableArea->setIsUserScroll(false);
307     }
308 }
309
310 void AsyncScrollingCoordinator::scrollableAreaScrollbarLayerDidChange(ScrollableArea* scrollableArea, ScrollbarOrientation orientation)
311 {
312     ASSERT(isMainThread());
313     ASSERT(m_page);
314
315     if (scrollableArea != static_cast<ScrollableArea*>(m_page->mainFrame().view()))
316         return;
317
318     if (orientation == VerticalScrollbar)
319         scrollableArea->verticalScrollbarLayerDidChange();
320     else
321         scrollableArea->horizontalScrollbarLayerDidChange();
322 }
323
324 ScrollingNodeID AsyncScrollingCoordinator::attachToStateTree(ScrollingNodeType nodeType, ScrollingNodeID newNodeID, ScrollingNodeID parentID)
325 {
326     return m_scrollingStateTree->attachNode(nodeType, newNodeID, parentID);
327 }
328
329 void AsyncScrollingCoordinator::detachFromStateTree(ScrollingNodeID nodeID)
330 {
331     m_scrollingStateTree->detachNode(nodeID);
332 }
333
334 void AsyncScrollingCoordinator::clearStateTree()
335 {
336     m_scrollingStateTree->clear();
337 }
338
339 void AsyncScrollingCoordinator::syncChildPositions(const LayoutRect& viewportRect)
340 {
341     if (!m_scrollingStateTree->rootStateNode())
342         return;
343
344     Vector<OwnPtr<ScrollingStateNode>>* children = m_scrollingStateTree->rootStateNode()->children();
345     if (!children)
346         return;
347
348     // FIXME: We'll have to traverse deeper into the tree at some point.
349     for (auto& child : *children)
350         child->syncLayerPositionForViewportRect(viewportRect);
351 }
352
353 void AsyncScrollingCoordinator::ensureRootStateNodeForFrameView(FrameView* frameView)
354 {
355     ASSERT(frameView->scrollLayerID());
356     attachToStateTree(FrameScrollingNode, frameView->scrollLayerID(), 0);
357 }
358
359 void AsyncScrollingCoordinator::updateFrameScrollingNode(ScrollingNodeID nodeID, GraphicsLayer* layer, GraphicsLayer* scrolledContentsLayer, GraphicsLayer* counterScrollingLayer, GraphicsLayer* insetClipLayer, const ScrollingGeometry* scrollingGeometry)
360 {
361     ScrollingStateFrameScrollingNode* node = toScrollingStateFrameScrollingNode(m_scrollingStateTree->stateNodeForID(nodeID));
362     ASSERT(node);
363     if (!node)
364         return;
365
366     node->setLayer(layer);
367     node->setInsetClipLayer(insetClipLayer);
368     node->setScrolledContentsLayer(scrolledContentsLayer);
369     node->setCounterScrollingLayer(counterScrollingLayer);
370
371     if (scrollingGeometry) {
372         node->setScrollOrigin(scrollingGeometry->scrollOrigin);
373         node->setScrollPosition(scrollingGeometry->scrollPosition);
374         node->setTotalContentsSize(scrollingGeometry->contentSize);
375         node->setScrollableAreaSize(scrollingGeometry->scrollableAreaSize);
376     }
377 }
378     
379 void AsyncScrollingCoordinator::updateOverflowScrollingNode(ScrollingNodeID nodeID, GraphicsLayer* layer, GraphicsLayer* scrolledContentsLayer, const ScrollingGeometry* scrollingGeometry)
380 {
381     ScrollingStateOverflowScrollingNode* node = toScrollingStateOverflowScrollingNode(m_scrollingStateTree->stateNodeForID(nodeID));
382     ASSERT(node);
383     if (!node)
384         return;
385
386     node->setLayer(layer);
387     node->setScrolledContentsLayer(scrolledContentsLayer);
388     
389     if (scrollingGeometry) {
390         node->setScrollOrigin(scrollingGeometry->scrollOrigin);
391         node->setScrollPosition(scrollingGeometry->scrollPosition);
392         node->setTotalContentsSize(scrollingGeometry->contentSize);
393         node->setScrollableAreaSize(scrollingGeometry->scrollableAreaSize);
394     }
395 }
396
397 void AsyncScrollingCoordinator::updateViewportConstrainedNode(ScrollingNodeID nodeID, const ViewportConstraints& constraints, GraphicsLayer* graphicsLayer)
398 {
399     ASSERT(supportsFixedPositionLayers());
400
401     ScrollingStateNode* node = m_scrollingStateTree->stateNodeForID(nodeID);
402     if (!node)
403         return;
404
405     switch (constraints.constraintType()) {
406     case ViewportConstraints::FixedPositionConstraint: {
407         ScrollingStateFixedNode* fixedNode = toScrollingStateFixedNode(node);
408         fixedNode->setLayer(graphicsLayer);
409         fixedNode->updateConstraints((const FixedPositionViewportConstraints&)constraints);
410         break;
411     }
412     case ViewportConstraints::StickyPositionConstraint: {
413         ScrollingStateStickyNode* stickyNode = toScrollingStateStickyNode(node);
414         stickyNode->setLayer(graphicsLayer);
415         stickyNode->updateConstraints((const StickyPositionViewportConstraints&)constraints);
416         break;
417     }
418     }
419 }
420
421 void AsyncScrollingCoordinator::setSynchronousScrollingReasons(SynchronousScrollingReasons reasons)
422 {
423     if (!m_scrollingStateTree->rootStateNode())
424         return;
425
426     // The FrameView's GraphicsLayer is likely to be out-of-synch with the PlatformLayer
427     // at this point. So we'll update it before we switch back to main thread scrolling
428     // in order to avoid layer positioning bugs.
429     if (reasons)
430         updateMainFrameScrollLayerPosition();
431     m_scrollingStateTree->rootStateNode()->setSynchronousScrollingReasons(reasons);
432 }
433
434 void AsyncScrollingCoordinator::updateMainFrameScrollLayerPosition()
435 {
436     ASSERT(isMainThread());
437
438     if (!m_page)
439         return;
440
441     FrameView* frameView = m_page->mainFrame().view();
442     if (!frameView)
443         return;
444
445     if (GraphicsLayer* scrollLayer = scrollLayerForFrameView(frameView))
446         scrollLayer->setPosition(-frameView->scrollPosition());
447 }
448
449 void AsyncScrollingCoordinator::recomputeWheelEventHandlerCountForFrameView(FrameView* frameView)
450 {
451     ScrollingStateFrameScrollingNode* node = toScrollingStateFrameScrollingNode(m_scrollingStateTree->stateNodeForID(frameView->scrollLayerID()));
452     if (!node)
453         return;
454     node->setWheelEventHandlerCount(computeCurrentWheelEventHandlerCount());
455 }
456
457 bool AsyncScrollingCoordinator::isRubberBandInProgress() const
458 {
459     return scrollingTree()->isRubberBandInProgress();
460 }
461
462 void AsyncScrollingCoordinator::setScrollPinningBehavior(ScrollPinningBehavior pinning)
463 {
464     scrollingTree()->setScrollPinningBehavior(pinning);
465 }
466
467 String AsyncScrollingCoordinator::scrollingStateTreeAsText() const
468 {
469     if (m_scrollingStateTree->rootStateNode())
470         return m_scrollingStateTree->rootStateNode()->scrollingStateTreeAsText();
471
472     return String();
473 }
474
475 } // namespace WebCore
476
477 #endif // ENABLE(ASYNC_SCROLLING)