-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ScrollingCoordinator.cpp
472 lines (398 loc) · 16.8 KB
/
ScrollingCoordinator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Copyright (C) 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ScrollingCoordinator.h"
#include "Document.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsLayer.h"
#include "Page.h"
#include "PlatformWheelEvent.h"
#include "PluginViewBase.h"
#include "Region.h"
#include "RenderLayerCompositor.h"
#include "RenderLayerScrollableArea.h"
#include "RenderView.h"
#include "ScrollAnimator.h"
#include "Settings.h"
#include <wtf/MainThread.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
#if PLATFORM(IOS_FAMILY) || !ENABLE(ASYNC_SCROLLING)
Ref<ScrollingCoordinator> ScrollingCoordinator::create(Page* page)
{
return adoptRef(*new ScrollingCoordinator(page));
}
#endif
ScrollingCoordinator::ScrollingCoordinator(Page* page)
: m_page(page)
{
}
ScrollingCoordinator::~ScrollingCoordinator()
{
ASSERT(!m_page);
}
void ScrollingCoordinator::pageDestroyed()
{
ASSERT(m_page);
m_page = nullptr;
}
bool ScrollingCoordinator::coordinatesScrollingForFrameView(const FrameView& frameView) const
{
ASSERT(isMainThread());
ASSERT(m_page);
if (!frameView.frame().isMainFrame() && !m_page->settings().scrollingTreeIncludesFrames()
#if PLATFORM(MAC) || USE(NICOSIA)
&& !m_page->settings().asyncFrameScrollingEnabled()
#endif
)
return false;
auto* renderView = frameView.frame().contentRenderer();
if (!renderView)
return false;
return renderView->usesCompositing();
}
bool ScrollingCoordinator::coordinatesScrollingForOverflowLayer(const RenderLayer& layer) const
{
ASSERT(isMainThread());
ASSERT(m_page);
return layer.hasCompositedScrollableOverflow();
}
ScrollingNodeID ScrollingCoordinator::scrollableContainerNodeID(const RenderObject&) const
{
return 0;
}
EventTrackingRegions ScrollingCoordinator::absoluteEventTrackingRegionsForFrame(const Frame& frame) const
{
auto* renderView = frame.contentRenderer();
if (!renderView || renderView->renderTreeBeingDestroyed())
return EventTrackingRegions();
#if ENABLE(IOS_TOUCH_EVENTS)
// On iOS, we use nonFastScrollableRegion to represent the region covered by elements with touch event handlers.
ASSERT(frame.isMainFrame());
auto* document = frame.document();
if (!document)
return { };
return document->eventTrackingRegions();
#else
auto* frameView = frame.view();
if (!frameView)
return EventTrackingRegions();
Region nonFastScrollableRegion;
// FIXME: should ASSERT(!frameView->needsLayout()) here, but need to fix DebugPageOverlays
// to not ask for regions at bad times.
if (auto* scrollableAreas = frameView->scrollableAreas()) {
for (auto& scrollableArea : *scrollableAreas) {
// Composited scrollable areas can be scrolled off the main thread.
if (!scrollableArea->isVisibleToHitTesting() || scrollableArea->usesAsyncScrolling())
continue;
bool isInsideFixed;
IntRect box = scrollableArea->scrollableAreaBoundingBox(&isInsideFixed);
if (isInsideFixed)
box = IntRect(frameView->fixedScrollableAreaBoundsInflatedForScrolling(LayoutRect(box)));
nonFastScrollableRegion.unite(box);
}
}
for (auto& widget : frameView->widgetsInRenderTree()) {
if (!is<PluginViewBase>(*widget))
continue;
if (!downcast<PluginViewBase>(*widget).wantsWheelEvents())
continue;
auto* renderWidget = RenderWidget::find(*widget);
if (!renderWidget)
continue;
nonFastScrollableRegion.unite(renderWidget->absoluteBoundingBoxRect());
}
EventTrackingRegions eventTrackingRegions;
// FIXME: if we've already accounted for this subframe as a scrollable area, we can avoid recursing into it here.
for (auto* subframe = frame.tree().firstChild(); subframe; subframe = subframe->tree().nextSibling()) {
auto* subframeView = subframe->view();
if (!subframeView)
continue;
EventTrackingRegions subframeRegion = absoluteEventTrackingRegionsForFrame(*subframe);
// Map from the frame document to our document.
IntPoint offset = subframeView->contentsToContainingViewContents(IntPoint());
// FIXME: this translation ignores non-trival transforms on the frame.
subframeRegion.translate(toIntSize(offset));
eventTrackingRegions.unite(subframeRegion);
}
#if !ENABLE(WHEEL_EVENT_REGIONS)
auto wheelHandlerRegion = frame.document()->absoluteRegionForEventTargets(frame.document()->wheelEventTargets());
bool wheelHandlerInFixedContent = wheelHandlerRegion.second;
if (wheelHandlerInFixedContent) {
// FIXME: need to handle position:sticky here too.
LayoutRect inflatedWheelHandlerBounds = frameView->fixedScrollableAreaBoundsInflatedForScrolling(LayoutRect(wheelHandlerRegion.first.bounds()));
wheelHandlerRegion.first.unite(enclosingIntRect(inflatedWheelHandlerBounds));
}
nonFastScrollableRegion.unite(wheelHandlerRegion.first);
#endif
// FIXME: If this is not the main frame, we could clip the region to the frame's bounds.
eventTrackingRegions.uniteSynchronousRegion(EventTrackingRegions::EventType::Wheel, nonFastScrollableRegion);
return eventTrackingRegions;
#endif
}
EventTrackingRegions ScrollingCoordinator::absoluteEventTrackingRegions() const
{
return absoluteEventTrackingRegionsForFrame(m_page->mainFrame());
}
void ScrollingCoordinator::frameViewFixedObjectsDidChange(FrameView& frameView)
{
ASSERT(isMainThread());
ASSERT(m_page);
if (!coordinatesScrollingForFrameView(frameView))
return;
updateSynchronousScrollingReasons(frameView);
}
GraphicsLayer* ScrollingCoordinator::scrollContainerLayerForFrameView(FrameView& frameView)
{
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().scrollContainerLayer();
return nullptr;
}
GraphicsLayer* ScrollingCoordinator::scrolledContentsLayerForFrameView(FrameView& frameView)
{
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().scrolledContentsLayer();
return nullptr;
}
GraphicsLayer* ScrollingCoordinator::headerLayerForFrameView(FrameView& frameView)
{
#if HAVE(RUBBER_BANDING)
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().headerLayer();
return nullptr;
#else
UNUSED_PARAM(frameView);
return nullptr;
#endif
}
GraphicsLayer* ScrollingCoordinator::footerLayerForFrameView(FrameView& frameView)
{
#if HAVE(RUBBER_BANDING)
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().footerLayer();
return nullptr;
#else
UNUSED_PARAM(frameView);
return nullptr;
#endif
}
GraphicsLayer* ScrollingCoordinator::counterScrollingLayerForFrameView(FrameView& frameView)
{
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().fixedRootBackgroundLayer();
return nullptr;
}
GraphicsLayer* ScrollingCoordinator::insetClipLayerForFrameView(FrameView& frameView)
{
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().clipLayer();
return nullptr;
}
GraphicsLayer* ScrollingCoordinator::contentShadowLayerForFrameView(FrameView& frameView)
{
#if HAVE(RUBBER_BANDING)
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().layerForContentShadow();
return nullptr;
#else
UNUSED_PARAM(frameView);
return nullptr;
#endif
}
GraphicsLayer* ScrollingCoordinator::rootContentsLayerForFrameView(FrameView& frameView)
{
if (auto* renderView = frameView.frame().contentRenderer())
return renderView->compositor().rootContentsLayer();
return nullptr;
}
void ScrollingCoordinator::frameViewRootLayerDidChange(FrameView& frameView)
{
ASSERT(isMainThread());
ASSERT(m_page);
if (!coordinatesScrollingForFrameView(frameView))
return;
frameViewLayoutUpdated(frameView);
updateSynchronousScrollingReasons(frameView);
}
bool ScrollingCoordinator::hasVisibleSlowRepaintViewportConstrainedObjects(const FrameView& frameView) const
{
auto* viewportConstrainedObjects = frameView.viewportConstrainedObjects();
if (!viewportConstrainedObjects)
return false;
for (auto& viewportConstrainedObject : *viewportConstrainedObjects) {
if (!is<RenderBoxModelObject>(viewportConstrainedObject) || !viewportConstrainedObject.hasLayer())
return true;
auto& layer = *downcast<RenderBoxModelObject>(viewportConstrainedObject).layer();
// Any explicit reason that a fixed position element is not composited shouldn't cause slow scrolling.
if (!layer.isComposited() && layer.viewportConstrainedNotCompositedReason() == RenderLayer::NoNotCompositedReason)
return true;
}
return false;
}
void ScrollingCoordinator::updateSynchronousScrollingReasons(FrameView& frameView)
{
ASSERT(coordinatesScrollingForFrameView(frameView));
OptionSet<SynchronousScrollingReason> newSynchronousScrollingReasons;
// RenderLayerCompositor::updateSynchronousScrollingReasons maintains this bit, so maintain its current value.
if (synchronousScrollingReasons(frameView.scrollingNodeID()).contains(SynchronousScrollingReason::HasSlowRepaintObjects))
newSynchronousScrollingReasons.add(SynchronousScrollingReason::HasSlowRepaintObjects);
if (m_forceSynchronousScrollLayerPositionUpdates)
newSynchronousScrollingReasons.add(SynchronousScrollingReason::ForcedOnMainThread);
if (hasVisibleSlowRepaintViewportConstrainedObjects(frameView))
newSynchronousScrollingReasons.add(SynchronousScrollingReason::HasNonLayerViewportConstrainedObjects);
if (frameView.frame().mainFrame().document() && frameView.frame().document()->isImageDocument())
newSynchronousScrollingReasons.add(SynchronousScrollingReason::IsImageDocument);
setSynchronousScrollingReasons(frameView.scrollingNodeID(), newSynchronousScrollingReasons);
}
void ScrollingCoordinator::updateSynchronousScrollingReasonsForAllFrames()
{
for (Frame* frame = &m_page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
if (auto* frameView = frame->view()) {
if (coordinatesScrollingForFrameView(*frameView))
updateSynchronousScrollingReasons(*frameView);
}
}
}
void ScrollingCoordinator::setForceSynchronousScrollLayerPositionUpdates(bool forceSynchronousScrollLayerPositionUpdates)
{
if (m_forceSynchronousScrollLayerPositionUpdates == forceSynchronousScrollLayerPositionUpdates)
return;
m_forceSynchronousScrollLayerPositionUpdates = forceSynchronousScrollLayerPositionUpdates;
updateSynchronousScrollingReasonsForAllFrames();
}
bool ScrollingCoordinator::shouldUpdateScrollLayerPositionSynchronously(const FrameView& frameView) const
{
if (&frameView == m_page->mainFrame().view())
return !synchronousScrollingReasons(frameView.scrollingNodeID()).isEmpty();
return true;
}
ScrollingNodeID ScrollingCoordinator::uniqueScrollingNodeID()
{
static ScrollingNodeID uniqueScrollingNodeID = 1;
return uniqueScrollingNodeID++;
}
String ScrollingCoordinator::scrollingStateTreeAsText(OptionSet<ScrollingStateTreeAsTextBehavior>) const
{
return emptyString();
}
String ScrollingCoordinator::scrollingTreeAsText(OptionSet<ScrollingStateTreeAsTextBehavior>) const
{
return emptyString();
}
String ScrollingCoordinator::synchronousScrollingReasonsAsText(OptionSet<SynchronousScrollingReason> reasons)
{
auto string = makeString(reasons.contains(SynchronousScrollingReason::ForcedOnMainThread) ? "Forced on main thread, " : "",
reasons.contains(SynchronousScrollingReason::HasSlowRepaintObjects) ? "Has slow repaint objects, " : "",
reasons.contains(SynchronousScrollingReason::HasViewportConstrainedObjectsWithoutSupportingFixedLayers) ? "Has viewport constrained objects without supporting fixed layers, " : "",
reasons.contains(SynchronousScrollingReason::HasNonLayerViewportConstrainedObjects) ? "Has non-layer viewport-constrained objects, " : "",
reasons.contains(SynchronousScrollingReason::IsImageDocument) ? "Is image document, " : "",
reasons.contains(SynchronousScrollingReason::DescendantScrollersHaveSynchronousScrolling) ? "Has slow repaint descendant scrollers, " : "");
return string.isEmpty() ? string : string.left(string.length() - 2);
}
String ScrollingCoordinator::synchronousScrollingReasonsAsText() const
{
if (auto* frameView = m_page->mainFrame().view())
return synchronousScrollingReasonsAsText(synchronousScrollingReasons(frameView->scrollingNodeID()));
return String();
}
TextStream& operator<<(TextStream& ts, ScrollableAreaParameters scrollableAreaParameters)
{
ts.dumpProperty("horizontal scroll elasticity", scrollableAreaParameters.horizontalScrollElasticity);
ts.dumpProperty("vertical scroll elasticity", scrollableAreaParameters.verticalScrollElasticity);
ts.dumpProperty("horizontal scrollbar mode", scrollableAreaParameters.horizontalScrollbarMode);
ts.dumpProperty("vertical scrollbar mode", scrollableAreaParameters.verticalScrollbarMode);
if (scrollableAreaParameters.allowsHorizontalScrolling)
ts.dumpProperty("allows horizontal scrolling", scrollableAreaParameters.allowsHorizontalScrolling);
if (scrollableAreaParameters.allowsVerticalScrolling)
ts.dumpProperty("allows vertical scrolling", scrollableAreaParameters.allowsVerticalScrolling);
if (scrollableAreaParameters.horizontalScrollbarHiddenByStyle)
ts.dumpProperty("horizontal scrollbar hidden by style", scrollableAreaParameters.horizontalScrollbarHiddenByStyle);
if (scrollableAreaParameters.verticalScrollbarHiddenByStyle)
ts.dumpProperty("vertical scrollbar hidden by style", scrollableAreaParameters.verticalScrollbarHiddenByStyle);
return ts;
}
TextStream& operator<<(TextStream& ts, ScrollingNodeType nodeType)
{
switch (nodeType) {
case ScrollingNodeType::MainFrame:
ts << "main-frame-scrolling";
break;
case ScrollingNodeType::Subframe:
ts << "subframe-scrolling";
break;
case ScrollingNodeType::FrameHosting:
ts << "frame-hosting";
break;
case ScrollingNodeType::Overflow:
ts << "overflow-scrolling";
break;
case ScrollingNodeType::OverflowProxy:
ts << "overflow-scroll-proxy";
break;
case ScrollingNodeType::Fixed:
ts << "fixed";
break;
case ScrollingNodeType::Sticky:
ts << "sticky";
break;
case ScrollingNodeType::Positioned:
ts << "positioned";
break;
}
return ts;
}
TextStream& operator<<(TextStream& ts, ScrollingLayerPositionAction action)
{
switch (action) {
case ScrollingLayerPositionAction::Set:
ts << "set";
break;
case ScrollingLayerPositionAction::SetApproximate:
ts << "set approximate";
break;
case ScrollingLayerPositionAction::Sync:
ts << "sync";
break;
}
return ts;
}
TextStream& operator<<(TextStream& ts, ViewportRectStability stability)
{
switch (stability) {
case ViewportRectStability::Stable:
ts << "stable";
break;
case ViewportRectStability::Unstable:
ts << "unstable";
break;
case ViewportRectStability::ChangingObscuredInsetsInteractively:
ts << "changing obscured insets interactively";
break;
}
return ts;
}
} // namespace WebCore