1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "AsyncPanZoomController.h"  // for AsyncPanZoomController, etc
8 
9 #include <math.h>       // for fabsf, fabs, atan2
10 #include <stdint.h>     // for uint32_t, uint64_t
11 #include <sys/types.h>  // for int32_t
12 #include <algorithm>    // for max, min
13 
14 #include "APZCTreeManager.h"          // for APZCTreeManager
15 #include "AsyncPanZoomAnimation.h"    // for AsyncPanZoomAnimation
16 #include "AutoscrollAnimation.h"      // for AutoscrollAnimation
17 #include "Axis.h"                     // for AxisX, AxisY, Axis, etc
18 #include "CheckerboardEvent.h"        // for CheckerboardEvent
19 #include "Compositor.h"               // for Compositor
20 #include "FrameMetrics.h"             // for FrameMetrics, etc
21 #include "GenericFlingAnimation.h"    // for GenericFlingAnimation
22 #include "GestureEventListener.h"     // for GestureEventListener
23 #include "HitTestingTreeNode.h"       // for HitTestingTreeNode
24 #include "InputData.h"                // for MultiTouchInput, etc
25 #include "InputBlockState.h"          // for InputBlockState, TouchBlockState
26 #include "InputQueue.h"               // for InputQueue
27 #include "Overscroll.h"               // for OverscrollAnimation
28 #include "OverscrollHandoffState.h"   // for OverscrollHandoffState
29 #include "Units.h"                    // for CSSRect, CSSPoint, etc
30 #include "UnitTransforms.h"           // for TransformTo
31 #include "base/message_loop.h"        // for MessageLoop
32 #include "base/task.h"                // for NewRunnableMethod, etc
33 #include "gfxPrefs.h"                 // for gfxPrefs
34 #include "gfxTypes.h"                 // for gfxFloat
35 #include "LayersLogging.h"            // for print_stderr
36 #include "mozilla/Assertions.h"       // for MOZ_ASSERT, etc
37 #include "mozilla/BasicEvents.h"      // for Modifiers, MODIFIER_*
38 #include "mozilla/ClearOnShutdown.h"  // for ClearOnShutdown
39 #include "mozilla/ComputedTimingFunction.h"  // for ComputedTimingFunction
40 #include "mozilla/EventForwards.h"           // for nsEventStatus_*
41 #include "mozilla/EventStateManager.h"       // for EventStateManager
42 #include "mozilla/MouseEvents.h"             // for WidgetWheelEvent
43 #include "mozilla/Preferences.h"             // for Preferences
44 #include "mozilla/RecursiveMutex.h"          // for RecursiveMutexAutoLock, etc
45 #include "mozilla/RefPtr.h"                  // for RefPtr
46 #include "mozilla/StaticPtr.h"               // for StaticAutoPtr
47 #include "mozilla/Telemetry.h"               // for Telemetry
48 #include "mozilla/TimeStamp.h"               // for TimeDuration, TimeStamp
49 #include "mozilla/dom/CheckerboardReportService.h"  // for CheckerboardEventStorage
50 // note: CheckerboardReportService.h actually lives in gfx/layers/apz/util/
51 #include "mozilla/dom/Touch.h"              // for Touch
52 #include "mozilla/gfx/BasePoint.h"          // for BasePoint
53 #include "mozilla/gfx/BaseRect.h"           // for BaseRect
54 #include "mozilla/gfx/Point.h"              // for Point, RoundedToInt, etc
55 #include "mozilla/gfx/Rect.h"               // for RoundedIn
56 #include "mozilla/gfx/ScaleFactor.h"        // for ScaleFactor
57 #include "mozilla/layers/APZThreadUtils.h"  // for AssertOnControllerThread, etc
58 #include "mozilla/layers/AsyncCompositionManager.h"  // for ViewTransform
59 #include "mozilla/layers/AxisPhysicsModel.h"         // for AxisPhysicsModel
60 #include "mozilla/layers/AxisPhysicsMSDModel.h"      // for AxisPhysicsMSDModel
61 #include "mozilla/layers/CompositorController.h"     // for CompositorController
62 #include "mozilla/layers/DirectionUtils.h"  // for GetAxis{Start,End,Length,Scale}
63 #include "mozilla/layers/LayerTransactionParent.h"  // for LayerTransactionParent
64 #include "mozilla/layers/MetricsSharingController.h"  // for MetricsSharingController
65 #include "mozilla/mozalloc.h"                         // for operator new, etc
66 #include "mozilla/Unused.h"                           // for unused
67 #include "mozilla/FloatingPoint.h"                    // for FuzzyEquals*
68 #include "nsAlgorithm.h"                              // for clamped
69 #include "nsCOMPtr.h"                                 // for already_AddRefed
70 #include "nsDebug.h"                                  // for NS_WARNING
71 #include "nsIDOMWindowUtils.h"                        // for nsIDOMWindowUtils
72 #include "nsMathUtils.h"                              // for NS_hypot
73 #include "nsPoint.h"                                  // for nsIntPoint
74 #include "nsStyleConsts.h"
75 #include "nsTimingFunction.h"
76 #include "nsTArray.h"                // for nsTArray, nsTArray_Impl, etc
77 #include "nsThreadUtils.h"           // for NS_IsMainThread
78 #include "nsViewportInfo.h"          // for kViewportMinScale, kViewportMaxScale
79 #include "prsystem.h"                // for PR_GetPhysicalMemorySize
80 #include "SharedMemoryBasic.h"       // for SharedMemoryBasic
81 #include "ScrollSnap.h"              // for ScrollSnapUtils
82 #include "ScrollAnimationPhysics.h"  // for ComputeAcceleratedWheelDelta
83 #include "WheelScrollAnimation.h"
84 #include "KeyboardScrollAnimation.h"
85 #if defined(MOZ_WIDGET_ANDROID)
86 #include "AndroidAPZ.h"
87 #include "mozilla/layers/AndroidDynamicToolbarAnimator.h"
88 #endif  // defined(MOZ_WIDGET_ANDROID)
89 
90 #define ENABLE_APZC_LOGGING 0
91 // #define ENABLE_APZC_LOGGING 1
92 
93 #if ENABLE_APZC_LOGGING
94 #define APZC_LOG(...) printf_stderr("APZC: " __VA_ARGS__)
95 #define APZC_LOG_FM(fm, prefix, ...)                  \
96   {                                                   \
97     std::stringstream ss;                             \
98     ss << nsPrintfCString(prefix, __VA_ARGS__).get(); \
99     AppendToString(ss, fm, ":", "", true);            \
100     APZC_LOG("%s\n", ss.str().c_str());               \
101   }
102 #else
103 #define APZC_LOG(...)
104 #define APZC_LOG_FM(fm, prefix, ...)
105 #endif
106 
107 namespace mozilla {
108 namespace layers {
109 
110 typedef mozilla::layers::AllowedTouchBehavior AllowedTouchBehavior;
111 typedef GeckoContentController::APZStateChange APZStateChange;
112 typedef GeckoContentController::TapType TapType;
113 typedef mozilla::gfx::Point Point;
114 typedef mozilla::gfx::Matrix4x4 Matrix4x4;
115 using mozilla::gfx::PointTyped;
116 
117 // Choose between platform-specific implementations.
118 #ifdef MOZ_WIDGET_ANDROID
119 typedef WidgetOverscrollEffect OverscrollEffect;
120 typedef AndroidSpecificState PlatformSpecificState;
121 typedef AndroidFlingAnimation FlingAnimation;
122 #else
123 typedef GenericOverscrollEffect OverscrollEffect;
124 typedef PlatformSpecificStateBase
125     PlatformSpecificState;  // no extra state, just use the base class
126 typedef GenericFlingAnimation FlingAnimation;
127 #endif
128 
129 /**
130  * \page APZCPrefs APZ preferences
131  *
132  * The following prefs are used to control the behaviour of the APZC.
133  * The default values are provided in gfxPrefs.h.
134  *
135  * \li\b apz.allow_checkerboarding
136  * Pref that allows or disallows checkerboarding
137  *
138  * \li\b apz.allow_immediate_handoff
139  * If set to true, scroll can be handed off from one APZC to another within
140  * a single input block. If set to false, a single input block can only
141  * scroll one APZC.
142  *
143  * \li\b apz.autoscroll.enabled
144  * If set to true, autoscrolling is driven by APZ rather than the content
145  * process main thread.
146  *
147  * \li\b apz.axis_lock.mode
148  * The preferred axis locking style. See AxisLockMode for possible values.
149  *
150  * \li\b apz.axis_lock.lock_angle
151  * Angle from axis within which we stay axis-locked.\n
152  * Units: radians
153  *
154  * \li\b apz.axis_lock.breakout_threshold
155  * Distance in inches the user must pan before axis lock can be broken.\n
156  * Units: (real-world, i.e. screen) inches
157  *
158  * \li\b apz.axis_lock.breakout_angle
159  * Angle at which axis lock can be broken.\n
160  * Units: radians
161  *
162  * \li\b apz.axis_lock.direct_pan_angle
163  * If the angle from an axis to the line drawn by a pan move is less than
164  * this value, we can assume that panning can be done in the allowed direction
165  * (horizontal or vertical).\n
166  * Currently used only for touch-action css property stuff and was addded to
167  * keep behaviour consistent with IE.\n
168  * Units: radians
169  *
170  * \li\b apz.content_response_timeout
171  * Amount of time before we timeout response from content. For example, if
172  * content is being unruly/slow and we don't get a response back within this
173  * time, we will just pretend that content did not preventDefault any touch
174  * events we dispatched to it.\n
175  * Units: milliseconds
176  *
177  * \li\b apz.danger_zone_x
178  * \li\b apz.danger_zone_y
179  * When drawing high-res tiles, we drop down to drawing low-res tiles
180  * when we know we can't keep up with the scrolling. The way we determine
181  * this is by checking if we are entering the "danger zone", which is the
182  * boundary of the painted content. For example, if the painted content
183  * goes from y=0...1000 and the visible portion is y=250...750 then
184  * we're far from checkerboarding. If we get to y=490...990 though then we're
185  * only 10 pixels away from showing checkerboarding so we are probably in
186  * a state where we can't keep up with scrolling. The danger zone prefs specify
187  * how wide this margin is; in the above example a y-axis danger zone of 10
188  * pixels would make us drop to low-res at y=490...990.\n
189  * This value is in layer pixels.
190  *
191  * \li\b apz.disable_for_scroll_linked_effects
192  * Setting this pref to true will disable APZ scrolling on documents where
193  * scroll-linked effects are detected. A scroll linked effect is detected if
194  * positioning or transform properties are updated inside a scroll event
195  * dispatch; we assume that such an update is in response to the scroll event
196  * and is therefore a scroll-linked effect which will be laggy with APZ
197  * scrolling.
198  *
199  * \li\b apz.displayport_expiry_ms
200  * While a scrollable frame is scrolling async, we set a displayport on it
201  * to make sure it is layerized. However this takes up memory, so once the
202  * scrolling stops we want to remove the displayport. This pref controls how
203  * long after scrolling stops the displayport is removed. A value of 0 will
204  * disable the expiry behavior entirely.
205  * Units: milliseconds
206  *
207  * \li\b apz.drag.enabled
208  * Setting this pref to true will cause APZ to handle mouse-dragging of
209  * scrollbar thumbs.
210  *
211  * \li\b apz.drag.initial.enabled
212  * Setting this pref to true will cause APZ to try to handle mouse-dragging
213  * of scrollbar thumbs without an initial round-trip to content to start it
214  * if possible. Only has an effect if apz.drag.enabled is also true.
215  *
216  * \li\b apz.drag.touch.enabled
217  * Setting this pref to true will cause APZ to handle touch-dragging of
218  * scrollbar thumbs. Only has an effect if apz.drag.enabled is also true.
219  *
220  * \li\b apz.enlarge_displayport_when_clipped
221  * Pref that enables enlarging of the displayport along one axis when the
222  * generated displayport's size is beyond that of the scrollable rect on the
223  * opposite axis.
224  *
225  * \li\b apz.fling_accel_interval_ms
226  * The time that determines whether a second fling will be treated as
227  * accelerated. If two flings are started within this interval, the second one
228  * will be accelerated. Setting an interval of 0 means that acceleration will
229  * be disabled.\n
230  * Units: milliseconds
231  *
232  * \li\b apz.fling_accel_min_velocity
233  * The minimum velocity of the second fling for it to be considered for fling
234  * acceleration.
235  * Units: screen pixels per milliseconds
236  *
237  * \li\b apz.fling_accel_base_mult
238  * \li\b apz.fling_accel_supplemental_mult
239  * When applying an acceleration on a fling, the new computed velocity is
240  * (new_fling_velocity * base_mult) + (old_velocity * supplemental_mult).
241  * The base_mult and supplemental_mult multiplier values are controlled by
242  * these prefs. Note that "old_velocity" here is the initial velocity of the
243  * previous fling _after_ acceleration was applied to it (if applicable).
244  *
245  * \li\b apz.fling_curve_function_x1
246  * \li\b apz.fling_curve_function_y1
247  * \li\b apz.fling_curve_function_x2
248  * \li\b apz.fling_curve_function_y2
249  * \li\b apz.fling_curve_threshold_inches_per_ms
250  * These five parameters define a Bezier curve function and threshold used to
251  * increase the actual velocity relative to the user's finger velocity. When the
252  * finger velocity is below the threshold (or if the threshold is not positive),
253  * the velocity is used as-is. If the finger velocity exceeds the threshold
254  * velocity, then the function defined by the curve is applied on the part of
255  * the velocity that exceeds the threshold. Note that the upper bound of the
256  * velocity is still specified by the \b apz.max_velocity_inches_per_ms pref,
257  * and the function will smoothly curve the velocity from the threshold to the
258  * max. In general the function parameters chosen should define an ease-out
259  * curve in order to increase the velocity in this range, or an ease-in curve to
260  * decrease the velocity. A straight-line curve is equivalent to disabling the
261  * curve entirely by setting the threshold to -1. The max velocity pref must
262  * also be set in order for the curving to take effect, as it defines the upper
263  * bound of the velocity curve.\n
264  * The points (x1, y1) and (x2, y2) used as the two intermediate control points
265  * in the cubic bezier curve; the first and last points are (0,0) and (1,1).\n
266  * Some example values for these prefs can be found at\n
267  * https://dxr.mozilla.org/mozilla-central/rev/70e05c6832e831374604ac3ce7433971368dffe0/layout/style/nsStyleStruct.cpp#2729
268  *
269  * \li\b apz.fling_friction
270  * Amount of friction applied during flings. This is used in the following
271  * formula: v(t1) = v(t0) * (1 - f)^(t1 - t0), where v(t1) is the velocity
272  * for a new sample, v(t0) is the velocity at the previous sample, f is the
273  * value of this pref, and (t1 - t0) is the amount of time, in milliseconds,
274  * that has elapsed between the two samples.\n
275  * NOTE: Not currently used in Android fling calculations.
276  *
277  * \li\b apz.fling_min_velocity_threshold
278  * Minimum velocity for a fling to actually kick off. If the user pans and lifts
279  * their finger such that the velocity is smaller than this amount, no fling
280  * is initiated.\n
281  * Units: screen pixels per millisecond
282  *
283  * \li\b apz.fling_stop_on_tap_threshold
284  * When flinging, if the velocity is above this number, then a tap on the
285  * screen will stop the fling without dispatching a tap to content. If the
286  * velocity is below this threshold a tap will also be dispatched.
287  * Note: when modifying this pref be sure to run the APZC gtests as some of
288  * them depend on the value of this pref.\n
289  * Units: screen pixels per millisecond
290  *
291  * \li\b apz.fling_stopped_threshold
292  * When flinging, if the velocity goes below this number, we just stop the
293  * animation completely. This is to prevent asymptotically approaching 0
294  * velocity and rerendering unnecessarily.\n
295  * Units: screen pixels per millisecond.\n
296  * NOTE: Should not be set to anything
297  * other than 0.0 for Android except for tests to disable flings.
298  *
299  * \li\b apz.frame_delay.enabled
300  * If this is set to true, changes to the async scroll offset and async zoom
301  * will not be immediately reflected in GetCurrentAsyncTransform() when called
302  * with |AsyncTransformConsumer::eForCompositing|. Rather, the transform will
303  * reflect the value of the async scroll offset and async zoom at the last time
304  * SampleCompositedAsyncTransform() was called.
305  *
306  * \li\b apz.keyboard.enabled
307  * Determines whether scrolling with the keyboard will be allowed to be handled
308  * by APZ.
309  *
310  * \li\b apz.keyboard.passive-listeners
311  * When enabled, APZ will interpret the passive event listener flag to mean
312  * that the event listener won't change the focused element or selection of
313  * the page. With this, web content can use passive key listeners and not have
314  * keyboard APZ disabled.
315  *
316  * \li\b apz.max_velocity_inches_per_ms
317  * Maximum velocity.  Velocity will be capped at this value if a faster fling
318  * occurs.  Negative values indicate unlimited velocity.\n
319  * Units: (real-world, i.e. screen) inches per millisecond
320  *
321  * \li\b apz.max_velocity_queue_size
322  * Maximum size of velocity queue. The queue contains last N velocity records.
323  * On touch end we calculate the average velocity in order to compensate
324  * touch/mouse drivers misbehaviour.
325  *
326  * \li\b apz.min_skate_speed
327  * Minimum amount of speed along an axis before we switch to "skate" multipliers
328  * rather than using the "stationary" multipliers.\n
329  * Units: CSS pixels per millisecond
330  *
331  * \li\b apz.one_touch_pinch.enabled
332  * Whether or not the "one-touch-pinch" gesture (for zooming with one finger)
333  * is enabled or not.
334  *
335  * \li\b apz.overscroll.enabled
336  * Pref that enables overscrolling. If this is disabled, excess scroll that
337  * cannot be handed off is discarded.
338  *
339  * \li\b apz.overscroll.min_pan_distance_ratio
340  * The minimum ratio of the pan distance along one axis to the pan distance
341  * along the other axis needed to initiate overscroll along the first axis
342  * during panning.
343  *
344  * \li\b apz.overscroll.stretch_factor
345  * How much overscrolling can stretch content along an axis.
346  * The maximum stretch along an axis is a factor of (1 + kStretchFactor).
347  * (So if kStretchFactor is 0, you can't stretch at all; if kStretchFactor
348  * is 1, you can stretch at most by a factor of 2).
349  *
350  * \li\b apz.overscroll.spring_stiffness
351  * The stiffness of the spring used in the physics model for the overscroll
352  * animation.
353  *
354  * \li\b apz.overscroll.spring_friction
355  * The friction of the spring used in the physics model for the overscroll
356  * animation.
357  * Even though a realistic physics model would dictate that this be the same
358  * as \b apz.fling_friction, we allow it to be set to be something different,
359  * because in practice we want flings to skate smoothly (low friction), while
360  * we want the overscroll bounce-back to oscillate few times (high friction).
361  *
362  * \li\b apz.overscroll.stop_distance_threshold
363  * \li\b apz.overscroll.stop_velocity_threshold
364  * Thresholds for stopping the overscroll animation. When both the distance
365  * and the velocity fall below their thresholds, we stop oscillating.\n
366  * Units: screen pixels (for distance)
367  *        screen pixels per millisecond (for velocity)
368  *
369  * \li\b apz.paint_skipping.enabled
370  * When APZ is scrolling and sending repaint requests to the main thread, often
371  * the main thread doesn't actually need to do a repaint. This pref allows the
372  * main thread to skip doing those repaints in cases where it doesn't need to.
373  *
374  * \li\b apz.pinch_lock.mode
375  * The preferred pinch locking style. See PinchLockMode for possible values.
376  *
377  * \li\b apz.pinch_lock.scroll_lock_threshold
378  * Pinch locking is triggered if the user scrolls more than this distance
379  * and pinches less than apz.pinch_lock.span_lock_threshold.\n
380  * Units: (real-world, i.e. screen) inches
381  *
382  * \li\b apz.pinch_lock.span_breakout_threshold
383  * Distance in inches the user must pinch before lock can be broken.\n
384  * Units: (real-world, i.e. screen) inches measured between two touch points
385  *
386  * \li\b apz.pinch_lock.span_lock_threshold
387  * Pinch locking is triggered if the user pinches less than this distance
388  * and scrolls more than apz.pinch_lock.scroll_lock_threshold.\n
389  * Units: (real-world, i.e. screen) inches measured between two touch points
390  *
391  * \li\b apz.popups.enabled
392  * Determines whether APZ is used for XUL popup widgets with remote content.
393  * Ideally, this should always be true, but it is currently not well tested, and
394  * has known issues, so needs to be prefable.
395  *
396  * \li\b apz.record_checkerboarding
397  * Whether or not to record detailed info on checkerboarding events.
398  *
399  * \li\b apz.second_tap_tolerance
400  * Constant describing the tolerance in distance we use, multiplied by the
401  * device DPI, within which a second tap is counted as part of a gesture
402  * continuing from the first tap. Making this larger allows the user more
403  * distance between the first and second taps in a "double tap" or "one touch
404  * pinch" gesture.\n
405  * Units: (real-world, i.e. screen) inches
406  *
407  * \li\b apz.test.logging_enabled
408  * Enable logging of APZ test data (see bug 961289).
409  *
410  * \li\b apz.touch_move_tolerance
411  * See the description for apz.touch_start_tolerance below. This is a similar
412  * threshold, except it is used to suppress touchmove events from being
413  * delivered to content for NON-scrollable frames (or more precisely, for APZCs
414  * where ArePointerEventsConsumable returns false).\n Units: (real-world, i.e.
415  * screen) inches
416  *
417  * \li\b apz.touch_start_tolerance
418  * Constant describing the tolerance in distance we use, multiplied by the
419  * device DPI, before we start panning the screen. This is to prevent us from
420  * accidentally processing taps as touch moves, and from very short/accidental
421  * touches moving the screen. touchmove events are also not delivered to content
422  * within this distance on scrollable frames.\n
423  * Units: (real-world, i.e. screen) inches
424  *
425  * \li\b apz.velocity_bias
426  * How much to adjust the displayport in the direction of scrolling. This value
427  * is multiplied by the velocity and added to the displayport offset.
428  *
429  * \li\b apz.velocity_relevance_time_ms
430  * When computing a fling velocity from the most recently stored velocity
431  * information, only velocities within the most X milliseconds are used.
432  * This pref controls the value of X.\n
433  * Units: ms
434  *
435  * \li\b apz.x_skate_size_multiplier
436  * \li\b apz.y_skate_size_multiplier
437  * The multiplier we apply to the displayport size if it is skating (current
438  * velocity is above \b apz.min_skate_speed). We prefer to increase the size of
439  * the Y axis because it is more natural in the case that a user is reading a
440  * page page that scrolls up/down. Note that one, both or neither of these may
441  * be used at any instant.\n In general we want \b
442  * apz.[xy]_skate_size_multiplier to be smaller than the corresponding
443  * stationary size multiplier because when panning fast we would like to paint
444  * less and get faster, more predictable paint times. When panning slowly we
445  * can afford to paint more even though it's slower.
446  *
447  * \li\b apz.x_stationary_size_multiplier
448  * \li\b apz.y_stationary_size_multiplier
449  * The multiplier we apply to the displayport size if it is not skating (see
450  * documentation for the skate size multipliers above).
451  *
452  * \li\b apz.x_skate_highmem_adjust
453  * \li\b apz.y_skate_highmem_adjust
454  * On high memory systems, we adjust the displayport during skating
455  * to be larger so we can reduce checkerboarding.
456  *
457  * \li\b apz.zoom_animation_duration_ms
458  * This controls how long the zoom-to-rect animation takes.\n
459  * Units: ms
460  *
461  * \li\b apz.scale_repaint_delay_ms
462  * How long to delay between repaint requests during a scale.
463  * A negative number prevents repaint requests during a scale.\n
464  * Units: ms
465  *
466  */
467 
468 /**
469  * Computed time function used for sampling frames of a zoom to animation.
470  */
471 StaticAutoPtr<ComputedTimingFunction> gZoomAnimationFunction;
472 
473 /**
474  * Computed time function used for curving up velocity when it gets high.
475  */
476 StaticAutoPtr<ComputedTimingFunction> gVelocityCurveFunction;
477 
478 /**
479  * The estimated duration of a paint for the purposes of calculating a new
480  * displayport, in milliseconds.
481  */
482 static const double kDefaultEstimatedPaintDurationMs = 50;
483 
484 /**
485  * Returns true if this is a high memory system and we can use
486  * extra memory for a larger displayport to reduce checkerboarding.
487  */
488 static bool gIsHighMemSystem = false;
IsHighMemSystem()489 static bool IsHighMemSystem() { return gIsHighMemSystem; }
490 
491 /**
492  * Is aAngle within the given threshold of the horizontal axis?
493  * @param aAngle an angle in radians in the range [0, pi]
494  * @param aThreshold an angle in radians in the range [0, pi/2]
495  */
IsCloseToHorizontal(float aAngle,float aThreshold)496 static bool IsCloseToHorizontal(float aAngle, float aThreshold) {
497   return (aAngle < aThreshold || aAngle > (M_PI - aThreshold));
498 }
499 
500 // As above, but for the vertical axis.
IsCloseToVertical(float aAngle,float aThreshold)501 static bool IsCloseToVertical(float aAngle, float aThreshold) {
502   return (fabs(aAngle - (M_PI / 2)) < aThreshold);
503 }
504 
505 // Counter used to give each APZC a unique id
506 static uint32_t sAsyncPanZoomControllerCount = 0;
507 
GetFrameTime() const508 TimeStamp AsyncPanZoomController::GetFrameTime() const {
509   APZCTreeManager* treeManagerLocal = GetApzcTreeManager();
510   return treeManagerLocal ? treeManagerLocal->GetFrameTime() : TimeStamp::Now();
511 }
512 
513 class MOZ_STACK_CLASS StateChangeNotificationBlocker {
514  public:
StateChangeNotificationBlocker(AsyncPanZoomController * aApzc)515   explicit StateChangeNotificationBlocker(AsyncPanZoomController* aApzc)
516       : mApzc(aApzc) {
517     RecursiveMutexAutoLock lock(mApzc->mRecursiveMutex);
518     mInitialState = mApzc->mState;
519     mApzc->mNotificationBlockers++;
520   }
521 
~StateChangeNotificationBlocker()522   ~StateChangeNotificationBlocker() {
523     AsyncPanZoomController::PanZoomState newState;
524     {
525       RecursiveMutexAutoLock lock(mApzc->mRecursiveMutex);
526       mApzc->mNotificationBlockers--;
527       newState = mApzc->mState;
528     }
529     mApzc->DispatchStateChangeNotification(mInitialState, newState);
530   }
531 
532  private:
533   AsyncPanZoomController* mApzc;
534   AsyncPanZoomController::PanZoomState mInitialState;
535 };
536 
537 class ZoomAnimation : public AsyncPanZoomAnimation {
538  public:
ZoomAnimation(CSSPoint aStartOffset,CSSToParentLayerScale2D aStartZoom,CSSPoint aEndOffset,CSSToParentLayerScale2D aEndZoom)539   ZoomAnimation(CSSPoint aStartOffset, CSSToParentLayerScale2D aStartZoom,
540                 CSSPoint aEndOffset, CSSToParentLayerScale2D aEndZoom)
541       : mTotalDuration(TimeDuration::FromMilliseconds(
542             gfxPrefs::APZZoomAnimationDuration())),
543         mStartOffset(aStartOffset),
544         mStartZoom(aStartZoom),
545         mEndOffset(aEndOffset),
546         mEndZoom(aEndZoom) {}
547 
DoSample(FrameMetrics & aFrameMetrics,const TimeDuration & aDelta)548   virtual bool DoSample(FrameMetrics& aFrameMetrics,
549                         const TimeDuration& aDelta) override {
550     mDuration += aDelta;
551     double animPosition = mDuration / mTotalDuration;
552 
553     if (animPosition >= 1.0) {
554       aFrameMetrics.SetZoom(mEndZoom);
555       aFrameMetrics.SetScrollOffset(mEndOffset);
556       return false;
557     }
558 
559     // Sample the zoom at the current time point.  The sampled zoom
560     // will affect the final computed resolution.
561     float sampledPosition = gZoomAnimationFunction->GetValue(
562         animPosition, ComputedTimingFunction::BeforeFlag::Unset);
563 
564     // We scale the scrollOffset linearly with sampledPosition, so the zoom
565     // needs to scale inversely to match.
566     aFrameMetrics.SetZoom(CSSToParentLayerScale2D(
567         1 / (sampledPosition / mEndZoom.xScale +
568              (1 - sampledPosition) / mStartZoom.xScale),
569         1 / (sampledPosition / mEndZoom.yScale +
570              (1 - sampledPosition) / mStartZoom.yScale)));
571 
572     aFrameMetrics.SetScrollOffset(CSSPoint::FromUnknownPoint(gfx::Point(
573         mEndOffset.x * sampledPosition + mStartOffset.x * (1 - sampledPosition),
574         mEndOffset.y * sampledPosition +
575             mStartOffset.y * (1 - sampledPosition))));
576 
577     return true;
578   }
579 
WantsRepaints()580   virtual bool WantsRepaints() override { return false; }
581 
582  private:
583   TimeDuration mDuration;
584   const TimeDuration mTotalDuration;
585 
586   // Old metrics from before we started a zoom animation. This is only valid
587   // when we are in the "ANIMATED_ZOOM" state. This is used so that we can
588   // interpolate between the start and end frames. We only use the
589   // |mViewportScrollOffset| and |mResolution| fields on this.
590   CSSPoint mStartOffset;
591   CSSToParentLayerScale2D mStartZoom;
592 
593   // Target metrics for a zoom to animation. This is only valid when we are in
594   // the "ANIMATED_ZOOM" state. We only use the |mViewportScrollOffset| and
595   // |mResolution| fields on this.
596   CSSPoint mEndOffset;
597   CSSToParentLayerScale2D mEndZoom;
598 };
599 
600 class SmoothScrollAnimation : public AsyncPanZoomAnimation {
601  public:
SmoothScrollAnimation(AsyncPanZoomController & aApzc,const nsPoint & aInitialPosition,const nsPoint & aInitialVelocity,const nsPoint & aDestination,double aSpringConstant,double aDampingRatio)602   SmoothScrollAnimation(AsyncPanZoomController& aApzc,
603                         const nsPoint& aInitialPosition,
604                         const nsPoint& aInitialVelocity,
605                         const nsPoint& aDestination, double aSpringConstant,
606                         double aDampingRatio)
607       : mApzc(aApzc),
608         mXAxisModel(aInitialPosition.x, aDestination.x, aInitialVelocity.x,
609                     aSpringConstant, aDampingRatio),
610         mYAxisModel(aInitialPosition.y, aDestination.y, aInitialVelocity.y,
611                     aSpringConstant, aDampingRatio) {}
612 
613   /**
614    * Advances a smooth scroll simulation based on the time passed in |aDelta|.
615    * This should be called whenever sampling the content transform for this
616    * frame. Returns true if the smooth scroll should be advanced by one frame,
617    * or false if the smooth scroll has ended.
618    */
DoSample(FrameMetrics & aFrameMetrics,const TimeDuration & aDelta)619   bool DoSample(FrameMetrics& aFrameMetrics,
620                 const TimeDuration& aDelta) override {
621     nsPoint oneParentLayerPixel =
622         CSSPoint::ToAppUnits(ParentLayerPoint(1, 1) / aFrameMetrics.GetZoom());
623     if (mXAxisModel.IsFinished(oneParentLayerPixel.x) &&
624         mYAxisModel.IsFinished(oneParentLayerPixel.y)) {
625       // Set the scroll offset to the exact destination. If we allow the scroll
626       // offset to end up being a bit off from the destination, we can get
627       // artefacts like "scroll to the next snap point in this direction"
628       // scrolling to the snap point we're already supposed to be at.
629       aFrameMetrics.ClampAndSetScrollOffset(CSSPoint::FromAppUnits(
630           nsPoint(mXAxisModel.GetDestination(), mYAxisModel.GetDestination())));
631       return false;
632     }
633 
634     mXAxisModel.Simulate(aDelta);
635     mYAxisModel.Simulate(aDelta);
636 
637     CSSPoint position = CSSPoint::FromAppUnits(
638         nsPoint(mXAxisModel.GetPosition(), mYAxisModel.GetPosition()));
639     CSSPoint css_velocity = CSSPoint::FromAppUnits(
640         nsPoint(mXAxisModel.GetVelocity(), mYAxisModel.GetVelocity()));
641 
642     // Convert from points/second to points/ms
643     ParentLayerPoint velocity =
644         ParentLayerPoint(css_velocity.x, css_velocity.y) / 1000.0f;
645 
646     // Keep the velocity updated for the Axis class so that any animations
647     // chained off of the smooth scroll will inherit it.
648     if (mXAxisModel.IsFinished(oneParentLayerPixel.x)) {
649       mApzc.mX.SetVelocity(0);
650     } else {
651       mApzc.mX.SetVelocity(velocity.x);
652     }
653     if (mYAxisModel.IsFinished(oneParentLayerPixel.y)) {
654       mApzc.mY.SetVelocity(0);
655     } else {
656       mApzc.mY.SetVelocity(velocity.y);
657     }
658     // If we overscroll, hand off to a fling animation that will complete the
659     // spring back.
660     CSSToParentLayerScale2D zoom = aFrameMetrics.GetZoom();
661     ParentLayerPoint displacement =
662         (position - aFrameMetrics.GetScrollOffset()) * zoom;
663 
664     ParentLayerPoint overscroll;
665     ParentLayerPoint adjustedOffset;
666     mApzc.mX.AdjustDisplacement(displacement.x, adjustedOffset.x, overscroll.x);
667     mApzc.mY.AdjustDisplacement(displacement.y, adjustedOffset.y, overscroll.y);
668 
669     aFrameMetrics.ScrollBy(adjustedOffset / zoom);
670 
671     // The smooth scroll may have caused us to reach the end of our scroll
672     // range. This can happen if either the
673     // layout.css.scroll-behavior.damping-ratio preference is set to less than 1
674     // (underdamped) or if a smooth scroll inherits velocity from a fling
675     // gesture.
676     if (!IsZero(overscroll)) {
677       // Hand off a fling with the remaining momentum to the next APZC in the
678       // overscroll handoff chain.
679 
680       // We may have reached the end of the scroll range along one axis but
681       // not the other. In such a case we only want to hand off the relevant
682       // component of the fling.
683       if (FuzzyEqualsAdditive(overscroll.x, 0.0f, COORDINATE_EPSILON)) {
684         velocity.x = 0;
685       } else if (FuzzyEqualsAdditive(overscroll.y, 0.0f, COORDINATE_EPSILON)) {
686         velocity.y = 0;
687       }
688 
689       // To hand off the fling, we attempt to find a target APZC and start a new
690       // fling with the same velocity on that APZC. For simplicity, the actual
691       // overscroll of the current sample is discarded rather than being handed
692       // off. The compositor should sample animations sufficiently frequently
693       // that this is not noticeable. The target APZC is chosen by seeing if
694       // there is an APZC further in the handoff chain which is pannable; if
695       // there isn't, we take the new fling ourselves, entering an overscrolled
696       // state.
697       // Note: APZC is holding mRecursiveMutex, so directly calling
698       // HandleSmoothScrollOverscroll() (which acquires the tree lock) would
699       // violate the lock ordering. Instead we schedule
700       // HandleSmoothScrollOverscroll() to be called after mRecursiveMutex is
701       // released.
702       mDeferredTasks.AppendElement(NewRunnableMethod<ParentLayerPoint>(
703           "layers::AsyncPanZoomController::HandleSmoothScrollOverscroll",
704           &mApzc, &AsyncPanZoomController::HandleSmoothScrollOverscroll,
705           velocity));
706       return false;
707     }
708 
709     return true;
710   }
711 
SetDestination(const nsPoint & aNewDestination)712   void SetDestination(const nsPoint& aNewDestination) {
713     mXAxisModel.SetDestination(static_cast<int32_t>(aNewDestination.x));
714     mYAxisModel.SetDestination(static_cast<int32_t>(aNewDestination.y));
715   }
716 
GetDestination() const717   CSSPoint GetDestination() const {
718     return CSSPoint::FromAppUnits(
719         nsPoint(mXAxisModel.GetDestination(), mYAxisModel.GetDestination()));
720   }
721 
AsSmoothScrollAnimation()722   SmoothScrollAnimation* AsSmoothScrollAnimation() override { return this; }
723 
724  private:
725   AsyncPanZoomController& mApzc;
726   AxisPhysicsMSDModel mXAxisModel, mYAxisModel;
727 };
728 
InitializeGlobalState()729 /*static*/ void AsyncPanZoomController::InitializeGlobalState() {
730   static bool sInitialized = false;
731   if (sInitialized) return;
732   sInitialized = true;
733 
734   MOZ_ASSERT(NS_IsMainThread());
735 
736   gZoomAnimationFunction = new ComputedTimingFunction(
737       nsTimingFunction(NS_STYLE_TRANSITION_TIMING_FUNCTION_EASE));
738   ClearOnShutdown(&gZoomAnimationFunction);
739   gVelocityCurveFunction = new ComputedTimingFunction(nsTimingFunction(
740       gfxPrefs::APZCurveFunctionX1(), gfxPrefs::APZCurveFunctionY1(),
741       gfxPrefs::APZCurveFunctionX2(), gfxPrefs::APZCurveFunctionY2()));
742   ClearOnShutdown(&gVelocityCurveFunction);
743 
744   uint64_t sysmem = PR_GetPhysicalMemorySize();
745   uint64_t threshold = 1LL << 32;  // 4 GB in bytes
746   gIsHighMemSystem = sysmem >= threshold;
747 }
748 
AsyncPanZoomController(uint64_t aLayersId,APZCTreeManager * aTreeManager,const RefPtr<InputQueue> & aInputQueue,GeckoContentController * aGeckoContentController,GestureBehavior aGestures)749 AsyncPanZoomController::AsyncPanZoomController(
750     uint64_t aLayersId, APZCTreeManager* aTreeManager,
751     const RefPtr<InputQueue>& aInputQueue,
752     GeckoContentController* aGeckoContentController, GestureBehavior aGestures)
753     : mLayersId(aLayersId),
754       mGeckoContentController(aGeckoContentController),
755       mRefPtrMonitor("RefPtrMonitor"),
756       // mTreeManager must be initialized before GetFrameTime() is called
757       mTreeManager(aTreeManager),
758       mFrameMetrics(mScrollMetadata.GetMetrics()),
759       mRecursiveMutex("AsyncPanZoomController"),
760       mLastContentPaintMetrics(mLastContentPaintMetadata.GetMetrics()),
761       mX(this),
762       mY(this),
763       mPanDirRestricted(false),
764       mPinchLocked(false),
765       mZoomConstraints(false, false,
766                        mFrameMetrics.GetDevPixelsPerCSSPixel() *
767                            kViewportMinScale / ParentLayerToScreenScale(1),
768                        mFrameMetrics.GetDevPixelsPerCSSPixel() *
769                            kViewportMaxScale / ParentLayerToScreenScale(1)),
770       mLastSampleTime(GetFrameTime()),
771       mLastCheckerboardReport(GetFrameTime()),
772       mOverscrollEffect(MakeUnique<OverscrollEffect>(*this)),
773       mState(NOTHING),
774       mNotificationBlockers(0),
775       mInputQueue(aInputQueue),
776       mPinchPaintTimerSet(false),
777       mAPZCId(sAsyncPanZoomControllerCount++),
778       mSharedLock(nullptr),
779       mAsyncTransformAppliedToContent(false),
780       mTestHasAsyncKeyScrolled(false),
781       mCheckerboardEventLock("APZCBELock") {
782   if (aGestures == USE_GESTURE_DETECTOR) {
783     mGestureEventListener = new GestureEventListener(this);
784   }
785 }
786 
~AsyncPanZoomController()787 AsyncPanZoomController::~AsyncPanZoomController() { MOZ_ASSERT(IsDestroyed()); }
788 
GetPlatformSpecificState()789 PlatformSpecificStateBase* AsyncPanZoomController::GetPlatformSpecificState() {
790   if (!mPlatformSpecificState) {
791     mPlatformSpecificState = MakeUnique<PlatformSpecificState>();
792   }
793   return mPlatformSpecificState.get();
794 }
795 
796 already_AddRefed<GeckoContentController>
GetGeckoContentController() const797 AsyncPanZoomController::GetGeckoContentController() const {
798   MonitorAutoLock lock(mRefPtrMonitor);
799   RefPtr<GeckoContentController> controller = mGeckoContentController;
800   return controller.forget();
801 }
802 
803 already_AddRefed<GestureEventListener>
GetGestureEventListener() const804 AsyncPanZoomController::GetGestureEventListener() const {
805   MonitorAutoLock lock(mRefPtrMonitor);
806   RefPtr<GestureEventListener> listener = mGestureEventListener;
807   return listener.forget();
808 }
809 
GetInputQueue() const810 const RefPtr<InputQueue>& AsyncPanZoomController::GetInputQueue() const {
811   return mInputQueue;
812 }
813 
Destroy()814 void AsyncPanZoomController::Destroy() {
815   APZThreadUtils::AssertOnSamplerThread();
816 
817   CancelAnimation(CancelAnimationFlags::ScrollSnap);
818 
819   {  // scope the lock
820     MonitorAutoLock lock(mRefPtrMonitor);
821     mGeckoContentController = nullptr;
822     mGestureEventListener = nullptr;
823   }
824   mParent = nullptr;
825   mTreeManager = nullptr;
826 
827   // Only send the release message if the SharedFrameMetrics has been created.
828   if (mMetricsSharingController && mSharedFrameMetricsBuffer) {
829     Unused << mMetricsSharingController->StopSharingMetrics(
830         mFrameMetrics.GetScrollId(), mAPZCId);
831   }
832 
833   {  // scope the lock
834     RecursiveMutexAutoLock lock(mRecursiveMutex);
835     mSharedFrameMetricsBuffer = nullptr;
836     delete mSharedLock;
837     mSharedLock = nullptr;
838   }
839 }
840 
IsDestroyed() const841 bool AsyncPanZoomController::IsDestroyed() const {
842   return mTreeManager == nullptr;
843 }
844 
GetTouchStartTolerance()845 /* static */ ScreenCoord AsyncPanZoomController::GetTouchStartTolerance() {
846   return (gfxPrefs::APZTouchStartTolerance() * APZCTreeManager::GetDPI());
847 }
848 
GetSecondTapTolerance()849 /* static */ ScreenCoord AsyncPanZoomController::GetSecondTapTolerance() {
850   return (gfxPrefs::APZSecondTapTolerance() * APZCTreeManager::GetDPI());
851 }
852 
853 /* static */ AsyncPanZoomController::AxisLockMode
GetAxisLockMode()854 AsyncPanZoomController::GetAxisLockMode() {
855   return static_cast<AxisLockMode>(gfxPrefs::APZAxisLockMode());
856 }
857 
858 /* static */ AsyncPanZoomController::PinchLockMode
GetPinchLockMode()859 AsyncPanZoomController::GetPinchLockMode() {
860   return static_cast<PinchLockMode>(gfxPrefs::APZPinchLockMode());
861 }
862 
ArePointerEventsConsumable(TouchBlockState * aBlock,uint32_t aTouchPoints)863 bool AsyncPanZoomController::ArePointerEventsConsumable(TouchBlockState* aBlock,
864                                                         uint32_t aTouchPoints) {
865   if (aTouchPoints == 0) {
866     // Cant' do anything with zero touch points
867     return false;
868   }
869 
870   // This logic is simplified, erring on the side of returning true
871   // if we're not sure. It's safer to pretend that we can consume the
872   // event and then not be able to than vice-versa.
873   // We could probably enhance this logic to determine things like "we're
874   // not pannable, so we can only zoom in, and the zoom is already maxed
875   // out, so we're not zoomable either" but no need for that at this point.
876 
877   bool pannable = aBlock->GetOverscrollHandoffChain()->CanBePanned(this);
878   bool zoomable = mZoomConstraints.mAllowZoom;
879 
880   pannable &= (aBlock->TouchActionAllowsPanningX() ||
881                aBlock->TouchActionAllowsPanningY());
882   zoomable &= (aBlock->TouchActionAllowsPinchZoom());
883 
884   // XXX once we fix bug 1031443, consumable should be assigned
885   // pannable || zoomable if aTouchPoints > 1.
886   bool consumable = (aTouchPoints == 1 ? pannable : zoomable);
887   if (!consumable) {
888     return false;
889   }
890 
891   return true;
892 }
893 
HandleDragEvent(const MouseInput & aEvent,const AsyncDragMetrics & aDragMetrics,CSSCoord aInitialThumbPos)894 nsEventStatus AsyncPanZoomController::HandleDragEvent(
895     const MouseInput& aEvent, const AsyncDragMetrics& aDragMetrics,
896     CSSCoord aInitialThumbPos) {
897   if (!gfxPrefs::APZDragEnabled()) {
898     return nsEventStatus_eIgnore;
899   }
900 
901   if (!GetApzcTreeManager()) {
902     return nsEventStatus_eConsumeNoDefault;
903   }
904 
905   if (aEvent.mType == MouseInput::MouseType::MOUSE_DOWN) {
906     SetState(SCROLLBAR_DRAG);
907   }
908 
909   if (aEvent.mType == MouseInput::MouseType::MOUSE_UP) {
910     SetState(NOTHING);
911     ScrollSnap();
912   }
913 
914   if (aEvent.mType != MouseInput::MouseType::MOUSE_MOVE) {
915     return nsEventStatus_eConsumeNoDefault;
916   }
917 
918   RefPtr<HitTestingTreeNode> node =
919       GetApzcTreeManager()->FindScrollThumbNode(aDragMetrics);
920   if (!node) {
921     return nsEventStatus_eConsumeNoDefault;
922   }
923 
924   const ScrollThumbData& thumbData = node->GetScrollThumbData();
925   MOZ_ASSERT(thumbData.mDirection.isSome());
926   ScrollDirection direction = *thumbData.mDirection;
927 
928   bool isMouseAwayFromThumb = false;
929   if (int snapMultiplier = gfxPrefs::SliderSnapMultiplier()) {
930     // It's fine to ignore the async component of the thumb's transform,
931     // because any async transform of the thumb will be in the direction of
932     // scrolling, but here we're interested in the other direction.
933     ParentLayerRect thumbRect =
934         (node->GetTransform() * AsyncTransformMatrix())
935             .TransformBounds(LayerRect(node->GetVisibleRegion().GetBounds()));
936     ScrollDirection otherDirection = GetPerpendicularDirection(direction);
937     ParentLayerCoord distance =
938         GetAxisStart(otherDirection, thumbRect.DistanceTo(aEvent.mLocalOrigin));
939     ParentLayerCoord thumbWidth = GetAxisLength(otherDirection, thumbRect);
940     // Avoid triggering this condition spuriously when the thumb is
941     // offscreen and its visible region is therefore empty.
942     if (thumbWidth > 0 && thumbWidth * snapMultiplier < distance) {
943       isMouseAwayFromThumb = true;
944     }
945   }
946 
947   RecursiveMutexAutoLock lock(mRecursiveMutex);
948   CSSCoord thumbPosition;
949   if (isMouseAwayFromThumb) {
950     thumbPosition = aInitialThumbPos;
951   } else {
952     thumbPosition = ConvertScrollbarPoint(aEvent.mLocalOrigin, thumbData) -
953                     aDragMetrics.mScrollbarDragOffset;
954   }
955 
956   CSSCoord maxThumbPos = thumbData.mScrollTrackLength;
957   maxThumbPos -= thumbData.mThumbLength;
958 
959   float scrollPercent = thumbPosition / maxThumbPos;
960 
961   CSSCoord minScrollPosition =
962       GetAxisStart(direction, mFrameMetrics.GetScrollableRect().TopLeft());
963   CSSCoord maxScrollPosition =
964       GetAxisStart(direction, mFrameMetrics.GetScrollableRect().BottomRight()) -
965       GetAxisLength(direction,
966                     mFrameMetrics.CalculateCompositedRectInCssPixels());
967   CSSCoord scrollPosition =
968       minScrollPosition +
969       (scrollPercent * (maxScrollPosition - minScrollPosition));
970 
971   scrollPosition = std::max(scrollPosition, minScrollPosition);
972   scrollPosition = std::min(scrollPosition, maxScrollPosition);
973 
974   CSSPoint scrollOffset = mFrameMetrics.GetScrollOffset();
975   if (direction == ScrollDirection::eHorizontal) {
976     scrollOffset.x = scrollPosition;
977   } else {
978     scrollOffset.y = scrollPosition;
979   }
980   mFrameMetrics.SetScrollOffset(scrollOffset);
981   ScheduleCompositeAndMaybeRepaint();
982   UpdateSharedCompositorFrameMetrics();
983 
984   return nsEventStatus_eConsumeNoDefault;
985 }
986 
HandleInputEvent(const InputData & aEvent,const ScreenToParentLayerMatrix4x4 & aTransformToApzc)987 nsEventStatus AsyncPanZoomController::HandleInputEvent(
988     const InputData& aEvent,
989     const ScreenToParentLayerMatrix4x4& aTransformToApzc) {
990   APZThreadUtils::AssertOnControllerThread();
991 
992   nsEventStatus rv = nsEventStatus_eIgnore;
993 
994   switch (aEvent.mInputType) {
995     case MULTITOUCH_INPUT: {
996       MultiTouchInput multiTouchInput = aEvent.AsMultiTouchInput();
997       if (!multiTouchInput.TransformToLocal(aTransformToApzc)) {
998         return rv;
999       }
1000 
1001       RefPtr<GestureEventListener> listener = GetGestureEventListener();
1002       if (listener) {
1003         rv = listener->HandleInputEvent(multiTouchInput);
1004         if (rv == nsEventStatus_eConsumeNoDefault) {
1005           return rv;
1006         }
1007       }
1008 
1009       switch (multiTouchInput.mType) {
1010         case MultiTouchInput::MULTITOUCH_START:
1011           rv = OnTouchStart(multiTouchInput);
1012           break;
1013         case MultiTouchInput::MULTITOUCH_MOVE:
1014           rv = OnTouchMove(multiTouchInput);
1015           break;
1016         case MultiTouchInput::MULTITOUCH_END:
1017           rv = OnTouchEnd(multiTouchInput);
1018           break;
1019         case MultiTouchInput::MULTITOUCH_CANCEL:
1020           rv = OnTouchCancel(multiTouchInput);
1021           break;
1022       }
1023       break;
1024     }
1025     case PANGESTURE_INPUT: {
1026       PanGestureInput panGestureInput = aEvent.AsPanGestureInput();
1027       if (!panGestureInput.TransformToLocal(aTransformToApzc)) {
1028         return rv;
1029       }
1030 
1031       switch (panGestureInput.mType) {
1032         case PanGestureInput::PANGESTURE_MAYSTART:
1033           rv = OnPanMayBegin(panGestureInput);
1034           break;
1035         case PanGestureInput::PANGESTURE_CANCELLED:
1036           rv = OnPanCancelled(panGestureInput);
1037           break;
1038         case PanGestureInput::PANGESTURE_START:
1039           rv = OnPanBegin(panGestureInput);
1040           break;
1041         case PanGestureInput::PANGESTURE_PAN:
1042           rv = OnPan(panGestureInput, true);
1043           break;
1044         case PanGestureInput::PANGESTURE_END:
1045           rv = OnPanEnd(panGestureInput);
1046           break;
1047         case PanGestureInput::PANGESTURE_MOMENTUMSTART:
1048           rv = OnPanMomentumStart(panGestureInput);
1049           break;
1050         case PanGestureInput::PANGESTURE_MOMENTUMPAN:
1051           rv = OnPan(panGestureInput, false);
1052           break;
1053         case PanGestureInput::PANGESTURE_MOMENTUMEND:
1054           rv = OnPanMomentumEnd(panGestureInput);
1055           break;
1056       }
1057       break;
1058     }
1059     case MOUSE_INPUT: {
1060       MouseInput mouseInput = aEvent.AsMouseInput();
1061       if (!mouseInput.TransformToLocal(aTransformToApzc)) {
1062         return rv;
1063       }
1064       break;
1065     }
1066     case SCROLLWHEEL_INPUT: {
1067       ScrollWheelInput scrollInput = aEvent.AsScrollWheelInput();
1068       if (!scrollInput.TransformToLocal(aTransformToApzc)) {
1069         return rv;
1070       }
1071 
1072       rv = OnScrollWheel(scrollInput);
1073       break;
1074     }
1075     case PINCHGESTURE_INPUT: {
1076       PinchGestureInput pinchInput = aEvent.AsPinchGestureInput();
1077       if (!pinchInput.TransformToLocal(aTransformToApzc)) {
1078         return rv;
1079       }
1080 
1081       rv = HandleGestureEvent(pinchInput);
1082       break;
1083     }
1084     case TAPGESTURE_INPUT: {
1085       TapGestureInput tapInput = aEvent.AsTapGestureInput();
1086       if (!tapInput.TransformToLocal(aTransformToApzc)) {
1087         return rv;
1088       }
1089 
1090       rv = HandleGestureEvent(tapInput);
1091       break;
1092     }
1093     case KEYBOARD_INPUT: {
1094       KeyboardInput keyInput = aEvent.AsKeyboardInput();
1095       rv = OnKeyboard(keyInput);
1096       break;
1097     }
1098   }
1099 
1100   return rv;
1101 }
1102 
HandleGestureEvent(const InputData & aEvent)1103 nsEventStatus AsyncPanZoomController::HandleGestureEvent(
1104     const InputData& aEvent) {
1105   APZThreadUtils::AssertOnControllerThread();
1106 
1107   nsEventStatus rv = nsEventStatus_eIgnore;
1108 
1109   switch (aEvent.mInputType) {
1110     case PINCHGESTURE_INPUT: {
1111       const PinchGestureInput& pinchGestureInput = aEvent.AsPinchGestureInput();
1112       switch (pinchGestureInput.mType) {
1113         case PinchGestureInput::PINCHGESTURE_START:
1114           rv = OnScaleBegin(pinchGestureInput);
1115           break;
1116         case PinchGestureInput::PINCHGESTURE_SCALE:
1117           rv = OnScale(pinchGestureInput);
1118           break;
1119         case PinchGestureInput::PINCHGESTURE_END:
1120           rv = OnScaleEnd(pinchGestureInput);
1121           break;
1122       }
1123       break;
1124     }
1125     case TAPGESTURE_INPUT: {
1126       const TapGestureInput& tapGestureInput = aEvent.AsTapGestureInput();
1127       switch (tapGestureInput.mType) {
1128         case TapGestureInput::TAPGESTURE_LONG:
1129           rv = OnLongPress(tapGestureInput);
1130           break;
1131         case TapGestureInput::TAPGESTURE_LONG_UP:
1132           rv = OnLongPressUp(tapGestureInput);
1133           break;
1134         case TapGestureInput::TAPGESTURE_UP:
1135           rv = OnSingleTapUp(tapGestureInput);
1136           break;
1137         case TapGestureInput::TAPGESTURE_CONFIRMED:
1138           rv = OnSingleTapConfirmed(tapGestureInput);
1139           break;
1140         case TapGestureInput::TAPGESTURE_DOUBLE:
1141           rv = OnDoubleTap(tapGestureInput);
1142           break;
1143         case TapGestureInput::TAPGESTURE_SECOND:
1144           rv = OnSecondTap(tapGestureInput);
1145           break;
1146         case TapGestureInput::TAPGESTURE_CANCEL:
1147           rv = OnCancelTap(tapGestureInput);
1148           break;
1149       }
1150       break;
1151     }
1152     default:
1153       MOZ_ASSERT_UNREACHABLE("Unhandled input event");
1154       break;
1155   }
1156 
1157   return rv;
1158 }
1159 
HandleTouchVelocity(uint32_t aTimesampMs,float aSpeedY)1160 void AsyncPanZoomController::HandleTouchVelocity(uint32_t aTimesampMs,
1161                                                  float aSpeedY) {
1162   mY.HandleTouchVelocity(aTimesampMs, aSpeedY);
1163 }
1164 
StartAutoscroll(const ScreenPoint & aPoint)1165 void AsyncPanZoomController::StartAutoscroll(const ScreenPoint& aPoint) {
1166   // Cancel any existing animation.
1167   CancelAnimation();
1168 
1169   SetState(AUTOSCROLL);
1170   StartAnimation(new AutoscrollAnimation(*this, aPoint));
1171 }
1172 
StopAutoscroll()1173 void AsyncPanZoomController::StopAutoscroll() {
1174   if (mState == AUTOSCROLL) {
1175     CancelAnimation(TriggeredExternally);
1176   }
1177 }
1178 
OnTouchStart(const MultiTouchInput & aEvent)1179 nsEventStatus AsyncPanZoomController::OnTouchStart(
1180     const MultiTouchInput& aEvent) {
1181   APZC_LOG("%p got a touch-start in state %d\n", this, mState);
1182   mPanDirRestricted = false;
1183   ParentLayerPoint point = GetFirstTouchPoint(aEvent);
1184 
1185   switch (mState) {
1186     case FLING:
1187     case ANIMATING_ZOOM:
1188     case SMOOTH_SCROLL:
1189     case OVERSCROLL_ANIMATION:
1190     case WHEEL_SCROLL:
1191     case KEYBOARD_SCROLL:
1192     case PAN_MOMENTUM:
1193     case AUTOSCROLL:
1194       MOZ_ASSERT(GetCurrentTouchBlock());
1195       GetCurrentTouchBlock()->GetOverscrollHandoffChain()->CancelAnimations(
1196           ExcludeOverscroll);
1197       MOZ_FALLTHROUGH;
1198     case SCROLLBAR_DRAG:
1199     case NOTHING: {
1200       mX.StartTouch(point.x, aEvent.mTime);
1201       mY.StartTouch(point.y, aEvent.mTime);
1202       if (RefPtr<GeckoContentController> controller =
1203               GetGeckoContentController()) {
1204         MOZ_ASSERT(GetCurrentTouchBlock());
1205         controller->NotifyAPZStateChange(
1206             GetGuid(), APZStateChange::eStartTouch,
1207             GetCurrentTouchBlock()->GetOverscrollHandoffChain()->CanBePanned(
1208                 this));
1209       }
1210       SetState(TOUCHING);
1211       break;
1212     }
1213     case TOUCHING:
1214     case PANNING:
1215     case PANNING_LOCKED_X:
1216     case PANNING_LOCKED_Y:
1217     case PINCHING:
1218       NS_WARNING("Received impossible touch in OnTouchStart");
1219       break;
1220   }
1221 
1222   return nsEventStatus_eConsumeNoDefault;
1223 }
1224 
OnTouchMove(const MultiTouchInput & aEvent)1225 nsEventStatus AsyncPanZoomController::OnTouchMove(
1226     const MultiTouchInput& aEvent) {
1227   APZC_LOG("%p got a touch-move in state %d\n", this, mState);
1228   switch (mState) {
1229     case FLING:
1230     case SMOOTH_SCROLL:
1231     case NOTHING:
1232     case ANIMATING_ZOOM:
1233       // May happen if the user double-taps and drags without lifting after the
1234       // second tap. Ignore the move if this happens.
1235       return nsEventStatus_eIgnore;
1236 
1237     case TOUCHING: {
1238       ScreenCoord panThreshold = GetTouchStartTolerance();
1239 
1240       // We intentionally skip the UpdateWithTouchAtDevicePoint call when the
1241       // panThreshold is zero. This ensures more deterministic behaviour during
1242       // testing. If we call that, Axis::mPos gets updated to the point of this
1243       // touchmove event, but we "consume" the move to overcome the
1244       // panThreshold, so it's hard to pan a specific amount reliably from a
1245       // mochitest.
1246       if (panThreshold > 0.0f) {
1247         UpdateWithTouchAtDevicePoint(aEvent);
1248         if (PanDistance() < panThreshold) {
1249           return nsEventStatus_eIgnore;
1250         }
1251       }
1252 
1253       ParentLayerPoint touchPoint = GetFirstTouchPoint(aEvent);
1254 
1255       MOZ_ASSERT(GetCurrentTouchBlock());
1256       if (gfxPrefs::TouchActionEnabled() &&
1257           GetCurrentTouchBlock()->TouchActionAllowsPanningXY()) {
1258         // User tries to trigger a touch behavior. If allowed touch behavior is
1259         // vertical pan
1260         // + horizontal pan (touch-action value is equal to AUTO) we can return
1261         // ConsumeNoDefault status immediately to trigger cancel event further.
1262         // It should happen independent of the parent type (whether it is
1263         // scrolling or not).
1264         StartPanning(touchPoint);
1265         return nsEventStatus_eConsumeNoDefault;
1266       }
1267 
1268       return StartPanning(touchPoint);
1269     }
1270 
1271     case PANNING:
1272     case PANNING_LOCKED_X:
1273     case PANNING_LOCKED_Y:
1274     case PAN_MOMENTUM:
1275       TrackTouch(aEvent);
1276       return nsEventStatus_eConsumeNoDefault;
1277 
1278     case PINCHING:
1279       // The scale gesture listener should have handled this.
1280       NS_WARNING(
1281           "Gesture listener should have handled pinching in OnTouchMove.");
1282       return nsEventStatus_eIgnore;
1283 
1284     case WHEEL_SCROLL:
1285     case KEYBOARD_SCROLL:
1286     case OVERSCROLL_ANIMATION:
1287     case AUTOSCROLL:
1288     case SCROLLBAR_DRAG:
1289       // Should not receive a touch-move in the OVERSCROLL_ANIMATION state
1290       // as touch blocks that begin in an overscrolled state cancel the
1291       // animation. The same is true for wheel scroll animations.
1292       NS_WARNING("Received impossible touch in OnTouchMove");
1293       break;
1294   }
1295 
1296   return nsEventStatus_eConsumeNoDefault;
1297 }
1298 
OnTouchEnd(const MultiTouchInput & aEvent)1299 nsEventStatus AsyncPanZoomController::OnTouchEnd(
1300     const MultiTouchInput& aEvent) {
1301   APZC_LOG("%p got a touch-end in state %d\n", this, mState);
1302   OnTouchEndOrCancel();
1303 
1304   // In case no touch behavior triggered previously we can avoid sending
1305   // scroll events or requesting content repaint. This condition is added
1306   // to make tests consistent - in case touch-action is NONE (and therefore
1307   // no pans/zooms can be performed) we expected neither scroll or repaint
1308   // events.
1309   if (mState != NOTHING) {
1310     RecursiveMutexAutoLock lock(mRecursiveMutex);
1311   }
1312 
1313   switch (mState) {
1314     case FLING:
1315       // Should never happen.
1316       NS_WARNING("Received impossible touch end in OnTouchEnd.");
1317       MOZ_FALLTHROUGH;
1318     case ANIMATING_ZOOM:
1319     case SMOOTH_SCROLL:
1320     case NOTHING:
1321       // May happen if the user double-taps and drags without lifting after the
1322       // second tap. Ignore if this happens.
1323       return nsEventStatus_eIgnore;
1324 
1325     case TOUCHING:
1326       // We may have some velocity stored on the axis from move events
1327       // that were not big enough to trigger scrolling. Clear that out.
1328       mX.SetVelocity(0);
1329       mY.SetVelocity(0);
1330       MOZ_ASSERT(GetCurrentTouchBlock());
1331       APZC_LOG("%p still has %u touch points active\n", this,
1332                GetCurrentTouchBlock()->GetActiveTouchCount());
1333       // In cases where the user is panning, then taps the second finger without
1334       // entering a pinch, we will arrive here when the second finger is lifted.
1335       // However the first finger is still down so we want to remain in state
1336       // TOUCHING.
1337       if (GetCurrentTouchBlock()->GetActiveTouchCount() == 0) {
1338         // It's possible we may be overscrolled if the user tapped during a
1339         // previous overscroll pan. Make sure to snap back in this situation.
1340         // An ancestor APZC could be overscrolled instead of this APZC, so
1341         // walk the handoff chain as well.
1342         GetCurrentTouchBlock()
1343             ->GetOverscrollHandoffChain()
1344             ->SnapBackOverscrolledApzc(this);
1345         // SnapBackOverscrolledApzc() will put any APZC it causes to snap back
1346         // into the OVERSCROLL_ANIMATION state. If that's not us, since we're
1347         // done TOUCHING enter the NOTHING state.
1348         if (mState != OVERSCROLL_ANIMATION) {
1349           SetState(NOTHING);
1350         }
1351       }
1352       return nsEventStatus_eIgnore;
1353 
1354     case PANNING:
1355     case PANNING_LOCKED_X:
1356     case PANNING_LOCKED_Y:
1357     case PAN_MOMENTUM: {
1358       MOZ_ASSERT(GetCurrentTouchBlock());
1359       mX.EndTouch(aEvent.mTime);
1360       mY.EndTouch(aEvent.mTime);
1361       return HandleEndOfPan();
1362     }
1363     case PINCHING:
1364       SetState(NOTHING);
1365       // Scale gesture listener should have handled this.
1366       NS_WARNING(
1367           "Gesture listener should have handled pinching in OnTouchEnd.");
1368       return nsEventStatus_eIgnore;
1369 
1370     case WHEEL_SCROLL:
1371     case KEYBOARD_SCROLL:
1372     case OVERSCROLL_ANIMATION:
1373     case AUTOSCROLL:
1374     case SCROLLBAR_DRAG:
1375       // Should not receive a touch-end in the OVERSCROLL_ANIMATION state
1376       // as touch blocks that begin in an overscrolled state cancel the
1377       // animation. The same is true for WHEEL_SCROLL.
1378       NS_WARNING("Received impossible touch in OnTouchEnd");
1379       break;
1380   }
1381 
1382   return nsEventStatus_eConsumeNoDefault;
1383 }
1384 
OnTouchCancel(const MultiTouchInput & aEvent)1385 nsEventStatus AsyncPanZoomController::OnTouchCancel(
1386     const MultiTouchInput& aEvent) {
1387   APZC_LOG("%p got a touch-cancel in state %d\n", this, mState);
1388   OnTouchEndOrCancel();
1389   CancelAnimationAndGestureState();
1390   return nsEventStatus_eConsumeNoDefault;
1391 }
1392 
OnScaleBegin(const PinchGestureInput & aEvent)1393 nsEventStatus AsyncPanZoomController::OnScaleBegin(
1394     const PinchGestureInput& aEvent) {
1395   APZC_LOG("%p got a scale-begin in state %d\n", this, mState);
1396 
1397   mPinchLocked = false;
1398   mPinchPaintTimerSet = false;
1399   // Note that there may not be a touch block at this point, if we received the
1400   // PinchGestureEvent directly from widget code without any touch events.
1401   if (HasReadyTouchBlock() &&
1402       !GetCurrentTouchBlock()->TouchActionAllowsPinchZoom()) {
1403     return nsEventStatus_eIgnore;
1404   }
1405 
1406   // If zooming is not allowed, this is a two-finger pan.
1407   // Start tracking panning distance and velocity.
1408   if (!mZoomConstraints.mAllowZoom) {
1409     mX.StartTouch(aEvent.mLocalFocusPoint.x, aEvent.mTime);
1410     mY.StartTouch(aEvent.mLocalFocusPoint.y, aEvent.mTime);
1411   }
1412 
1413   // For platforms that don't support APZ zooming, dispatch a message to the
1414   // content controller, it may want to do something else with this gesture.
1415   if (!gfxPrefs::APZAllowZooming()) {
1416     if (RefPtr<GeckoContentController> controller =
1417             GetGeckoContentController()) {
1418       controller->NotifyPinchGesture(aEvent.mType, GetGuid(), 0,
1419                                      aEvent.modifiers);
1420     }
1421   }
1422 
1423   SetState(PINCHING);
1424   mX.SetVelocity(0);
1425   mY.SetVelocity(0);
1426   mLastZoomFocus =
1427       aEvent.mLocalFocusPoint - mFrameMetrics.GetCompositionBounds().TopLeft();
1428 
1429   return nsEventStatus_eConsumeNoDefault;
1430 }
1431 
OnScale(const PinchGestureInput & aEvent)1432 nsEventStatus AsyncPanZoomController::OnScale(const PinchGestureInput& aEvent) {
1433   APZC_LOG("%p got a scale in state %d\n", this, mState);
1434 
1435   if (HasReadyTouchBlock() &&
1436       !GetCurrentTouchBlock()->TouchActionAllowsPinchZoom()) {
1437     return nsEventStatus_eIgnore;
1438   }
1439 
1440   if (mState != PINCHING) {
1441     return nsEventStatus_eConsumeNoDefault;
1442   }
1443 
1444   ParentLayerCoord spanDistance =
1445       fabsf(aEvent.mPreviousSpan - aEvent.mCurrentSpan);
1446   ParentLayerPoint focusPoint, focusChange;
1447   {
1448     RecursiveMutexAutoLock lock(mRecursiveMutex);
1449 
1450     focusPoint = aEvent.mLocalFocusPoint -
1451                  mFrameMetrics.GetCompositionBounds().TopLeft();
1452     focusChange = mLastZoomFocus - focusPoint;
1453     mLastZoomFocus = focusPoint;
1454   }
1455 
1456   HandlePinchLocking(
1457       ToScreenCoordinates(ParentLayerPoint(0, spanDistance), focusPoint)
1458           .Length(),
1459       ToScreenCoordinates(focusChange, focusPoint));
1460   bool allowZoom = mZoomConstraints.mAllowZoom && !mPinchLocked;
1461 
1462   // If zooming is not allowed, this is a two-finger pan.
1463   // Tracking panning distance and velocity.
1464   // UpdateWithTouchAtDevicePoint() acquires the tree lock, so
1465   // it cannot be called while the mRecursiveMutex lock is held.
1466   if (!allowZoom) {
1467     mX.UpdateWithTouchAtDevicePoint(aEvent.mLocalFocusPoint.x, 0, aEvent.mTime);
1468     mY.UpdateWithTouchAtDevicePoint(aEvent.mLocalFocusPoint.y, 0, aEvent.mTime);
1469   }
1470 
1471   if (!gfxPrefs::APZAllowZooming()) {
1472     if (RefPtr<GeckoContentController> controller =
1473             GetGeckoContentController()) {
1474       controller->NotifyPinchGesture(
1475           aEvent.mType, GetGuid(),
1476           ViewAs<LayoutDevicePixel>(
1477               aEvent.mCurrentSpan - aEvent.mPreviousSpan,
1478               PixelCastJustification::LayoutDeviceIsParentLayerForRCDRSF),
1479           aEvent.modifiers);
1480     }
1481   }
1482 
1483   // Only the root APZC is zoomable, and the root APZC is not allowed to have
1484   // different x and y scales. If it did, the calculations in this function
1485   // would have to be adjusted (as e.g. it would no longer be valid to take
1486   // the minimum or maximum of the ratios of the widths and heights of the
1487   // page rect and the composition bounds).
1488   MOZ_ASSERT(mFrameMetrics.IsRootContent());
1489   MOZ_ASSERT(mFrameMetrics.GetZoom().AreScalesSame());
1490 
1491   {
1492     RecursiveMutexAutoLock lock(mRecursiveMutex);
1493 
1494     CSSToParentLayerScale userZoom = mFrameMetrics.GetZoom().ToScaleFactor();
1495     CSSPoint cssFocusPoint = focusPoint / mFrameMetrics.GetZoom();
1496 
1497     // If displacing by the change in focus point will take us off page bounds,
1498     // then reduce the displacement such that it doesn't.
1499     focusChange.x -= mX.DisplacementWillOverscrollAmount(focusChange.x);
1500     focusChange.y -= mY.DisplacementWillOverscrollAmount(focusChange.y);
1501     ScrollBy(focusChange / userZoom);
1502 
1503     // If the span is zero or close to it, we don't want to process this zoom
1504     // change because we're going to get wonky numbers for the spanRatio. So
1505     // let's bail out here. Note that we do this after the focus-change-scroll
1506     // above, so that if we have a pinch with zero span but changing focus,
1507     // such as generated by some Synaptics touchpads on Windows, we still
1508     // scroll properly.
1509     float prevSpan = aEvent.mPreviousSpan;
1510     if (fabsf(prevSpan) <= EPSILON || fabsf(aEvent.mCurrentSpan) <= EPSILON) {
1511       // We might have done a nonzero ScrollBy above, so update metrics and
1512       // repaint/recomposite
1513       ScheduleCompositeAndMaybeRepaint();
1514       UpdateSharedCompositorFrameMetrics();
1515       return nsEventStatus_eConsumeNoDefault;
1516     }
1517     float spanRatio = aEvent.mCurrentSpan / aEvent.mPreviousSpan;
1518 
1519     // When we zoom in with focus, we can zoom too much towards the boundaries
1520     // that we actually go over them. These are the needed displacements along
1521     // either axis such that we don't overscroll the boundaries when zooming.
1522     CSSPoint neededDisplacement;
1523 
1524     CSSToParentLayerScale realMinZoom = mZoomConstraints.mMinZoom;
1525     CSSToParentLayerScale realMaxZoom = mZoomConstraints.mMaxZoom;
1526     realMinZoom.scale = std::max(realMinZoom.scale,
1527                                  mFrameMetrics.GetCompositionBounds().Width() /
1528                                      mFrameMetrics.GetScrollableRect().Width());
1529     realMinZoom.scale = std::max(
1530         realMinZoom.scale, mFrameMetrics.GetCompositionBounds().Height() /
1531                                mFrameMetrics.GetScrollableRect().Height());
1532     if (realMaxZoom < realMinZoom) {
1533       realMaxZoom = realMinZoom;
1534     }
1535 
1536     bool doScale = allowZoom && ((spanRatio > 1.0 && userZoom < realMaxZoom) ||
1537                                  (spanRatio < 1.0 && userZoom > realMinZoom));
1538 
1539     if (doScale) {
1540       spanRatio = clamped(spanRatio, realMinZoom.scale / userZoom.scale,
1541                           realMaxZoom.scale / userZoom.scale);
1542 
1543       // Note that the spanRatio here should never put us into OVERSCROLL_BOTH
1544       // because up above we clamped it.
1545       neededDisplacement.x =
1546           -mX.ScaleWillOverscrollAmount(spanRatio, cssFocusPoint.x);
1547       neededDisplacement.y =
1548           -mY.ScaleWillOverscrollAmount(spanRatio, cssFocusPoint.y);
1549 
1550       ScaleWithFocus(spanRatio, cssFocusPoint);
1551 
1552       if (neededDisplacement != CSSPoint()) {
1553         ScrollBy(neededDisplacement);
1554       }
1555 
1556       // We don't want to redraw on every scale, so throttle it.
1557       if (!mPinchPaintTimerSet) {
1558         const int delay = gfxPrefs::APZScaleRepaintDelay();
1559         if (delay >= 0) {
1560           if (RefPtr<GeckoContentController> controller =
1561                   GetGeckoContentController()) {
1562             mPinchPaintTimerSet = true;
1563             controller->PostDelayedTask(
1564                 NewRunnableMethod(
1565                     "layers::AsyncPanZoomController::"
1566                     "DoDelayedRequestContentRepaint",
1567                     this,
1568                     &AsyncPanZoomController::DoDelayedRequestContentRepaint),
1569                 delay);
1570           }
1571         }
1572       }
1573 
1574       UpdateSharedCompositorFrameMetrics();
1575 
1576     } else {
1577       // Trigger a repaint request after scrolling.
1578       RequestContentRepaint();
1579     }
1580 
1581     // We did a ScrollBy call above even if we didn't do a scale, so we
1582     // should composite for that.
1583     ScheduleComposite();
1584   }
1585 
1586   return nsEventStatus_eConsumeNoDefault;
1587 }
1588 
OnScaleEnd(const PinchGestureInput & aEvent)1589 nsEventStatus AsyncPanZoomController::OnScaleEnd(
1590     const PinchGestureInput& aEvent) {
1591   APZC_LOG("%p got a scale-end in state %d\n", this, mState);
1592 
1593   mPinchPaintTimerSet = false;
1594 
1595   if (HasReadyTouchBlock() &&
1596       !GetCurrentTouchBlock()->TouchActionAllowsPinchZoom()) {
1597     return nsEventStatus_eIgnore;
1598   }
1599 
1600   if (!gfxPrefs::APZAllowZooming()) {
1601     if (RefPtr<GeckoContentController> controller =
1602             GetGeckoContentController()) {
1603       controller->NotifyPinchGesture(aEvent.mType, GetGuid(), 0,
1604                                      aEvent.modifiers);
1605     }
1606   }
1607 
1608   {
1609     RecursiveMutexAutoLock lock(mRecursiveMutex);
1610     ScheduleComposite();
1611     RequestContentRepaint();
1612     UpdateSharedCompositorFrameMetrics();
1613   }
1614 
1615   // Non-negative focus point would indicate that one finger is still down
1616   if (aEvent.mLocalFocusPoint.x != -1 && aEvent.mLocalFocusPoint.y != -1) {
1617     if (mZoomConstraints.mAllowZoom) {
1618       mPanDirRestricted = false;
1619       mX.StartTouch(aEvent.mLocalFocusPoint.x, aEvent.mTime);
1620       mY.StartTouch(aEvent.mLocalFocusPoint.y, aEvent.mTime);
1621       SetState(TOUCHING);
1622     } else {
1623       // If zooming isn't allowed, StartTouch() was already called
1624       // in OnScaleBegin().
1625       StartPanning(aEvent.mLocalFocusPoint);
1626     }
1627   } else {
1628     // Otherwise, handle the fingers being lifted.
1629 
1630     // Some of the code paths below, like ScrollSnap() or HandleEndOfPan(),
1631     // may start an animation, but otherwise we want to end up in the NOTHING
1632     // state. To avoid state change notification churn, we use a
1633     // notification blocker.
1634     bool stateWasPinching = (mState == PINCHING);
1635     StateChangeNotificationBlocker blocker(this);
1636     SetState(NOTHING);
1637 
1638     if (mZoomConstraints.mAllowZoom) {
1639       RecursiveMutexAutoLock lock(mRecursiveMutex);
1640 
1641       // We can get into a situation where we are overscrolled at the end of a
1642       // pinch if we go into overscroll with a two-finger pan, and then turn
1643       // that into a pinch by increasing the span sufficiently. In such a case,
1644       // there is no snap-back animation to get us out of overscroll, so we need
1645       // to get out of it somehow.
1646       // Moreover, in cases of scroll handoff, the overscroll can be on an APZC
1647       // further up in the handoff chain rather than on the current APZC, so
1648       // we need to clear overscroll along the entire handoff chain.
1649       if (HasReadyTouchBlock()) {
1650         GetCurrentTouchBlock()->GetOverscrollHandoffChain()->ClearOverscroll();
1651       } else {
1652         ClearOverscroll();
1653       }
1654       // Along with clearing the overscroll, we also want to snap to the nearest
1655       // snap point as appropriate.
1656       ScrollSnap();
1657     } else {
1658       // when zoom is not allowed
1659       mX.EndTouch(aEvent.mTime);
1660       mY.EndTouch(aEvent.mTime);
1661       if (stateWasPinching) {
1662         // still pinching
1663         if (HasReadyTouchBlock()) {
1664           return HandleEndOfPan();
1665         }
1666       }
1667     }
1668   }
1669   return nsEventStatus_eConsumeNoDefault;
1670 }
1671 
HandleEndOfPan()1672 nsEventStatus AsyncPanZoomController::HandleEndOfPan() {
1673   MOZ_ASSERT(GetCurrentTouchBlock());
1674   GetCurrentTouchBlock()->GetOverscrollHandoffChain()->FlushRepaints();
1675   ParentLayerPoint flingVelocity = GetVelocityVector();
1676 
1677   // Clear our velocities; if DispatchFling() gives the fling to us,
1678   // the fling velocity gets *added* to our existing velocity in
1679   // AcceptFling().
1680   mX.SetVelocity(0);
1681   mY.SetVelocity(0);
1682   // Clear our state so that we don't stay in the PANNING state
1683   // if DispatchFling() gives the fling to somone else. However,
1684   // don't send the state change notification until we've determined
1685   // what our final state is to avoid notification churn.
1686   StateChangeNotificationBlocker blocker(this);
1687   SetState(NOTHING);
1688 
1689   APZC_LOG("%p starting a fling animation if %f >= %f\n", this,
1690            flingVelocity.Length().value,
1691            gfxPrefs::APZFlingMinVelocityThreshold());
1692 
1693   if (flingVelocity.Length() < gfxPrefs::APZFlingMinVelocityThreshold()) {
1694     // Relieve overscroll now if needed, since we will not transition to a fling
1695     // animation and then an overscroll animation, and relieve it then.
1696     GetCurrentTouchBlock()
1697         ->GetOverscrollHandoffChain()
1698         ->SnapBackOverscrolledApzc(this);
1699     return nsEventStatus_eConsumeNoDefault;
1700   }
1701 
1702   // Make a local copy of the tree manager pointer and check that it's not
1703   // null before calling DispatchFling(). This is necessary because Destroy(),
1704   // which nulls out mTreeManager, could be called concurrently.
1705   if (APZCTreeManager* treeManagerLocal = GetApzcTreeManager()) {
1706     const FlingHandoffState handoffState{
1707         flingVelocity, GetCurrentTouchBlock()->GetOverscrollHandoffChain(),
1708         false /* not handoff */, GetCurrentTouchBlock()->GetScrolledApzc()};
1709     treeManagerLocal->DispatchFling(this, handoffState);
1710   }
1711   return nsEventStatus_eConsumeNoDefault;
1712 }
1713 
ConvertToGecko(const ScreenIntPoint & aPoint,LayoutDevicePoint * aOut)1714 bool AsyncPanZoomController::ConvertToGecko(const ScreenIntPoint& aPoint,
1715                                             LayoutDevicePoint* aOut) {
1716   if (APZCTreeManager* treeManagerLocal = GetApzcTreeManager()) {
1717     ScreenToScreenMatrix4x4 transformScreenToGecko =
1718         treeManagerLocal->GetScreenToApzcTransform(this) *
1719         treeManagerLocal->GetApzcToGeckoTransform(this);
1720 
1721     Maybe<ScreenIntPoint> layoutPoint =
1722         UntransformBy(transformScreenToGecko, aPoint);
1723     if (!layoutPoint) {
1724       return false;
1725     }
1726 
1727     *aOut = LayoutDevicePoint(ViewAs<LayoutDevicePixel>(
1728         *layoutPoint,
1729         PixelCastJustification::LayoutDeviceIsScreenForUntransformedEvent));
1730     return true;
1731   }
1732   return false;
1733 }
1734 
ConvertScrollbarPoint(const ParentLayerPoint & aScrollbarPoint,const ScrollThumbData & aThumbData) const1735 CSSCoord AsyncPanZoomController::ConvertScrollbarPoint(
1736     const ParentLayerPoint& aScrollbarPoint,
1737     const ScrollThumbData& aThumbData) const {
1738   RecursiveMutexAutoLock lock(mRecursiveMutex);
1739 
1740   // First, get it into the right coordinate space.
1741   CSSPoint scrollbarPoint = aScrollbarPoint / mFrameMetrics.GetZoom();
1742   // The scrollbar can be transformed with the frame but the pres shell
1743   // resolution is only applied to the scroll frame.
1744   scrollbarPoint = scrollbarPoint * mFrameMetrics.GetPresShellResolution();
1745 
1746   // Now, get it to be relative to the beginning of the scroll track.
1747   CSSRect cssCompositionBound =
1748       mFrameMetrics.CalculateCompositedRectInCssPixels();
1749   return GetAxisStart(*aThumbData.mDirection, scrollbarPoint) -
1750          GetAxisStart(*aThumbData.mDirection, cssCompositionBound) -
1751          aThumbData.mScrollTrackStart;
1752 }
1753 
AllowsScrollingMoreThanOnePage(double aMultiplier)1754 static bool AllowsScrollingMoreThanOnePage(double aMultiplier) {
1755   const int32_t kMinAllowPageScroll =
1756       EventStateManager::MIN_MULTIPLIER_VALUE_ALLOWING_OVER_ONE_PAGE_SCROLL;
1757   return Abs(aMultiplier) >= kMinAllowPageScroll;
1758 }
1759 
GetScrollWheelDelta(const ScrollWheelInput & aEvent) const1760 ParentLayerPoint AsyncPanZoomController::GetScrollWheelDelta(
1761     const ScrollWheelInput& aEvent) const {
1762   ParentLayerSize scrollAmount;
1763   ParentLayerSize pageScrollSize;
1764 
1765   {
1766     // Grab the lock to access the frame metrics.
1767     RecursiveMutexAutoLock lock(mRecursiveMutex);
1768     LayoutDeviceIntSize scrollAmountLD = mScrollMetadata.GetLineScrollAmount();
1769     LayoutDeviceIntSize pageScrollSizeLD =
1770         mScrollMetadata.GetPageScrollAmount();
1771     scrollAmount = scrollAmountLD / mFrameMetrics.GetDevPixelsPerCSSPixel() *
1772                    mFrameMetrics.GetZoom();
1773     pageScrollSize = pageScrollSizeLD /
1774                      mFrameMetrics.GetDevPixelsPerCSSPixel() *
1775                      mFrameMetrics.GetZoom();
1776   }
1777 
1778   ParentLayerPoint delta;
1779   switch (aEvent.mDeltaType) {
1780     case ScrollWheelInput::SCROLLDELTA_LINE: {
1781       delta.x = aEvent.mDeltaX * scrollAmount.width;
1782       delta.y = aEvent.mDeltaY * scrollAmount.height;
1783       break;
1784     }
1785     case ScrollWheelInput::SCROLLDELTA_PAGE: {
1786       delta.x = aEvent.mDeltaX * pageScrollSize.width;
1787       delta.y = aEvent.mDeltaY * pageScrollSize.height;
1788       break;
1789     }
1790     case ScrollWheelInput::SCROLLDELTA_PIXEL: {
1791       delta = ToParentLayerCoordinates(
1792           ScreenPoint(aEvent.mDeltaX, aEvent.mDeltaY), aEvent.mOrigin);
1793       break;
1794     }
1795   }
1796 
1797   // Apply user-set multipliers.
1798   delta.x *= aEvent.mUserDeltaMultiplierX;
1799   delta.y *= aEvent.mUserDeltaMultiplierY;
1800 
1801   // For the conditions under which we allow system scroll overrides, see
1802   // EventStateManager::DeltaAccumulator::ComputeScrollAmountForDefaultAction
1803   // and WheelTransaction::OverrideSystemScrollSpeed. Note that we do *not*
1804   // restrict this to the root content, see bug 1217715 for discussion on this.
1805   if (gfxPrefs::MouseWheelHasRootScrollDeltaOverride() &&
1806       !aEvent.IsCustomizedByUserPrefs() &&
1807       aEvent.mDeltaType == ScrollWheelInput::SCROLLDELTA_LINE &&
1808       aEvent.mAllowToOverrideSystemScrollSpeed) {
1809     delta.x = WidgetWheelEvent::ComputeOverriddenDelta(delta.x, false);
1810     delta.y = WidgetWheelEvent::ComputeOverriddenDelta(delta.y, true);
1811   }
1812 
1813   // If this is a line scroll, and this event was part of a scroll series, then
1814   // it might need extra acceleration. See WheelHandlingHelper.cpp.
1815   if (aEvent.mDeltaType == ScrollWheelInput::SCROLLDELTA_LINE &&
1816       aEvent.mScrollSeriesNumber > 0) {
1817     int32_t start = gfxPrefs::MouseWheelAccelerationStart();
1818     if (start >= 0 && aEvent.mScrollSeriesNumber >= uint32_t(start)) {
1819       int32_t factor = gfxPrefs::MouseWheelAccelerationFactor();
1820       if (factor > 0) {
1821         delta.x = ComputeAcceleratedWheelDelta(
1822             delta.x, aEvent.mScrollSeriesNumber, factor);
1823         delta.y = ComputeAcceleratedWheelDelta(
1824             delta.y, aEvent.mScrollSeriesNumber, factor);
1825       }
1826     }
1827   }
1828 
1829   // We shouldn't scroll more than one page at once except when the
1830   // user preference is large.
1831   if (!AllowsScrollingMoreThanOnePage(aEvent.mUserDeltaMultiplierX) &&
1832       Abs(delta.x) > pageScrollSize.width) {
1833     delta.x = (delta.x >= 0) ? pageScrollSize.width : -pageScrollSize.width;
1834   }
1835   if (!AllowsScrollingMoreThanOnePage(aEvent.mUserDeltaMultiplierY) &&
1836       Abs(delta.y) > pageScrollSize.height) {
1837     delta.y = (delta.y >= 0) ? pageScrollSize.height : -pageScrollSize.height;
1838   }
1839 
1840   return delta;
1841 }
1842 
OnKeyboard(const KeyboardInput & aEvent)1843 nsEventStatus AsyncPanZoomController::OnKeyboard(const KeyboardInput& aEvent) {
1844   // Mark that this APZC has async key scrolled
1845   mTestHasAsyncKeyScrolled = true;
1846 
1847   // Calculate the destination for this keyboard scroll action
1848   CSSPoint destination = GetKeyboardDestination(aEvent.mAction);
1849   bool scrollSnapped =
1850       MaybeAdjustDestinationForScrollSnapping(aEvent, destination);
1851 
1852   // If smooth scrolling is disabled, then scroll immediately to the destination
1853   if (!gfxPrefs::SmoothScrollEnabled()) {
1854     CancelAnimation();
1855 
1856     // CallDispatchScroll interprets the start and end points as the start and
1857     // end of a touch scroll so they need to be reversed.
1858     ParentLayerPoint startPoint = destination * mFrameMetrics.GetZoom();
1859     ParentLayerPoint endPoint =
1860         mFrameMetrics.GetScrollOffset() * mFrameMetrics.GetZoom();
1861     ParentLayerPoint delta = endPoint - startPoint;
1862 
1863     ScreenPoint distance = ToScreenCoordinates(
1864         ParentLayerPoint(fabs(delta.x), fabs(delta.y)), startPoint);
1865 
1866     OverscrollHandoffState handoffState(
1867         *mInputQueue->GetCurrentKeyboardBlock()->GetOverscrollHandoffChain(),
1868         distance, ScrollSource::Keyboard);
1869 
1870     CallDispatchScroll(startPoint, endPoint, handoffState);
1871 
1872     SetState(NOTHING);
1873 
1874     return nsEventStatus_eConsumeDoDefault;
1875   }
1876 
1877   // The lock must be held across the entire update operation, so the
1878   // compositor doesn't end the animation before we get a chance to
1879   // update it.
1880   RecursiveMutexAutoLock lock(mRecursiveMutex);
1881 
1882   if (scrollSnapped) {
1883     // If we're scroll snapping, use a smooth scroll animation to get
1884     // the desired physics. Note that SmoothScrollTo() will re-use an
1885     // existing smooth scroll animation if there is one.
1886     APZC_LOG("%p keyboard scrolling to snap point %s\n", this,
1887              Stringify(destination).c_str());
1888     SmoothScrollTo(destination);
1889     return nsEventStatus_eConsumeDoDefault;
1890   }
1891 
1892   // Use a keyboard scroll animation to scroll, reusing an existing one if it
1893   // exists
1894   if (mState != KEYBOARD_SCROLL) {
1895     CancelAnimation();
1896     SetState(KEYBOARD_SCROLL);
1897 
1898     nsPoint initialPosition =
1899         CSSPoint::ToAppUnits(mFrameMetrics.GetScrollOffset());
1900     StartAnimation(new KeyboardScrollAnimation(*this, initialPosition,
1901                                                aEvent.mAction.mType));
1902   }
1903 
1904   // Convert velocity from ParentLayerPoints/ms to ParentLayerPoints/s and then
1905   // to appunits/second.
1906   nsPoint velocity = CSSPoint::ToAppUnits(
1907       ParentLayerPoint(mX.GetVelocity() * 1000.0f, mY.GetVelocity() * 1000.0f) /
1908       mFrameMetrics.GetZoom());
1909 
1910   KeyboardScrollAnimation* animation = mAnimation->AsKeyboardScrollAnimation();
1911   MOZ_ASSERT(animation);
1912 
1913   animation->UpdateDestination(aEvent.mTimeStamp,
1914                                CSSPixel::ToAppUnits(destination),
1915                                nsSize(velocity.x, velocity.y));
1916 
1917   return nsEventStatus_eConsumeDoDefault;
1918 }
1919 
GetKeyboardDestination(const KeyboardScrollAction & aAction) const1920 CSSPoint AsyncPanZoomController::GetKeyboardDestination(
1921     const KeyboardScrollAction& aAction) const {
1922   CSSSize lineScrollSize;
1923   CSSSize pageScrollSize;
1924   CSSPoint scrollOffset;
1925   CSSRect scrollRect;
1926 
1927   {
1928     // Grab the lock to access the frame metrics.
1929     RecursiveMutexAutoLock lock(mRecursiveMutex);
1930 
1931     lineScrollSize = mScrollMetadata.GetLineScrollAmount() /
1932                      mFrameMetrics.GetDevPixelsPerCSSPixel();
1933     pageScrollSize = mScrollMetadata.GetPageScrollAmount() /
1934                      mFrameMetrics.GetDevPixelsPerCSSPixel();
1935 
1936     if (mState == WHEEL_SCROLL) {
1937       scrollOffset = mAnimation->AsWheelScrollAnimation()->GetDestination();
1938     } else if (mState == SMOOTH_SCROLL) {
1939       scrollOffset = mAnimation->AsSmoothScrollAnimation()->GetDestination();
1940     } else if (mState == KEYBOARD_SCROLL) {
1941       scrollOffset = mAnimation->AsKeyboardScrollAnimation()->GetDestination();
1942     } else {
1943       scrollOffset = mFrameMetrics.GetScrollOffset();
1944     }
1945 
1946     scrollRect = mFrameMetrics.GetScrollableRect();
1947   }
1948 
1949   // Calculate the scroll destination based off of the scroll type and direction
1950   CSSPoint scrollDestination = scrollOffset;
1951 
1952   switch (aAction.mType) {
1953     case KeyboardScrollAction::eScrollCharacter: {
1954       int32_t scrollDistance = gfxPrefs::ToolkitHorizontalScrollDistance();
1955 
1956       if (aAction.mForward) {
1957         scrollDestination.x += scrollDistance * lineScrollSize.width;
1958       } else {
1959         scrollDestination.x -= scrollDistance * lineScrollSize.width;
1960       }
1961       break;
1962     }
1963     case KeyboardScrollAction::eScrollLine: {
1964       int32_t scrollDistance = gfxPrefs::ToolkitVerticalScrollDistance();
1965 
1966       if (aAction.mForward) {
1967         scrollDestination.y += scrollDistance * lineScrollSize.height;
1968       } else {
1969         scrollDestination.y -= scrollDistance * lineScrollSize.height;
1970       }
1971       break;
1972     }
1973     case KeyboardScrollAction::eScrollPage: {
1974       if (aAction.mForward) {
1975         scrollDestination.y += pageScrollSize.height;
1976       } else {
1977         scrollDestination.y -= pageScrollSize.height;
1978       }
1979       break;
1980     }
1981     case KeyboardScrollAction::eScrollComplete: {
1982       if (aAction.mForward) {
1983         scrollDestination.y = scrollRect.YMost();
1984       } else {
1985         scrollDestination.y = scrollRect.Y();
1986       }
1987       break;
1988     }
1989   }
1990 
1991   return scrollDestination;
1992 }
1993 
GetDeltaForEvent(const InputData & aEvent) const1994 ParentLayerPoint AsyncPanZoomController::GetDeltaForEvent(
1995     const InputData& aEvent) const {
1996   ParentLayerPoint delta;
1997   if (aEvent.mInputType == SCROLLWHEEL_INPUT) {
1998     delta = GetScrollWheelDelta(aEvent.AsScrollWheelInput());
1999   } else if (aEvent.mInputType == PANGESTURE_INPUT) {
2000     const PanGestureInput& panInput = aEvent.AsPanGestureInput();
2001     delta = ToParentLayerCoordinates(panInput.UserMultipliedPanDisplacement(),
2002                                      panInput.mPanStartPoint);
2003   }
2004   return delta;
2005 }
2006 
2007 // Return whether or not the underlying layer can be scrolled on either axis.
CanScroll(const InputData & aEvent) const2008 bool AsyncPanZoomController::CanScroll(const InputData& aEvent) const {
2009   ParentLayerPoint delta = GetDeltaForEvent(aEvent);
2010   if (!delta.x && !delta.y) {
2011     return false;
2012   }
2013 
2014   return CanScrollWithWheel(delta);
2015 }
2016 
GetAllowedHandoffDirections() const2017 ScrollDirections AsyncPanZoomController::GetAllowedHandoffDirections() const {
2018   ScrollDirections result;
2019   RecursiveMutexAutoLock lock(mRecursiveMutex);
2020   if (mX.OverscrollBehaviorAllowsHandoff()) {
2021     result += ScrollDirection::eHorizontal;
2022   }
2023   if (mY.OverscrollBehaviorAllowsHandoff()) {
2024     result += ScrollDirection::eVertical;
2025   }
2026   return result;
2027 }
2028 
CanScrollWithWheel(const ParentLayerPoint & aDelta) const2029 bool AsyncPanZoomController::CanScrollWithWheel(
2030     const ParentLayerPoint& aDelta) const {
2031   RecursiveMutexAutoLock lock(mRecursiveMutex);
2032 
2033   // For more details about the concept of a disregarded direction, refer to the
2034   // code in struct ScrollMetadata which defines mDisregardedDirection.
2035   Maybe<ScrollDirection> disregardedDirection =
2036       mScrollMetadata.GetDisregardedDirection();
2037   if (mX.CanScroll(aDelta.x) &&
2038       disregardedDirection != Some(ScrollDirection::eHorizontal)) {
2039     return true;
2040   }
2041   if (mY.CanScroll(aDelta.y) &&
2042       disregardedDirection != Some(ScrollDirection::eVertical)) {
2043     return true;
2044   }
2045   return false;
2046 }
2047 
CanScroll(ScrollDirection aDirection) const2048 bool AsyncPanZoomController::CanScroll(ScrollDirection aDirection) const {
2049   RecursiveMutexAutoLock lock(mRecursiveMutex);
2050   switch (aDirection) {
2051     case ScrollDirection::eHorizontal:
2052       return mX.CanScroll();
2053     case ScrollDirection::eVertical:
2054       return mY.CanScroll();
2055   }
2056   MOZ_ASSERT_UNREACHABLE("Invalid value");
2057   return false;
2058 }
2059 
AllowScrollHandoffInCurrentBlock() const2060 bool AsyncPanZoomController::AllowScrollHandoffInCurrentBlock() const {
2061   bool result = mInputQueue->AllowScrollHandoff();
2062   if (!gfxPrefs::APZAllowImmediateHandoff()) {
2063     if (InputBlockState* currentBlock = GetCurrentInputBlock()) {
2064       // Do not allow handoff beyond the first APZC to scroll.
2065       if (currentBlock->GetScrolledApzc() == this) {
2066         result = false;
2067       }
2068     }
2069   }
2070   return result;
2071 }
2072 
DoDelayedRequestContentRepaint()2073 void AsyncPanZoomController::DoDelayedRequestContentRepaint() {
2074   if (!IsDestroyed() && mPinchPaintTimerSet) {
2075     RecursiveMutexAutoLock lock(mRecursiveMutex);
2076     RequestContentRepaint();
2077   }
2078   mPinchPaintTimerSet = false;
2079 }
2080 
AdjustDeltaForAllowedScrollDirections(ParentLayerPoint & aDelta,const ScrollDirections & aAllowedScrollDirections)2081 static void AdjustDeltaForAllowedScrollDirections(
2082     ParentLayerPoint& aDelta,
2083     const ScrollDirections& aAllowedScrollDirections) {
2084   if (!aAllowedScrollDirections.contains(ScrollDirection::eHorizontal)) {
2085     aDelta.x = 0;
2086   }
2087   if (!aAllowedScrollDirections.contains(ScrollDirection::eVertical)) {
2088     aDelta.y = 0;
2089   }
2090 }
2091 
OnScrollWheel(const ScrollWheelInput & aEvent)2092 nsEventStatus AsyncPanZoomController::OnScrollWheel(
2093     const ScrollWheelInput& aEvent) {
2094   ParentLayerPoint delta = GetScrollWheelDelta(aEvent);
2095   APZC_LOG("%p got a scroll-wheel with delta %s\n", this,
2096            Stringify(delta).c_str());
2097 
2098   if ((delta.x || delta.y) && !CanScrollWithWheel(delta)) {
2099     // We can't scroll this apz anymore, so we simply drop the event.
2100     if (mInputQueue->GetActiveWheelTransaction() &&
2101         gfxPrefs::MouseScrollTestingEnabled()) {
2102       if (RefPtr<GeckoContentController> controller =
2103               GetGeckoContentController()) {
2104         controller->NotifyMozMouseScrollEvent(
2105             mFrameMetrics.GetScrollId(),
2106             NS_LITERAL_STRING("MozMouseScrollFailed"));
2107       }
2108     }
2109     return nsEventStatus_eConsumeNoDefault;
2110   }
2111 
2112   MOZ_ASSERT(mInputQueue->GetCurrentWheelBlock());
2113   AdjustDeltaForAllowedScrollDirections(
2114       delta, mInputQueue->GetCurrentWheelBlock()->GetAllowedScrollDirections());
2115 
2116   if (delta.x == 0 && delta.y == 0) {
2117     // Avoid spurious state changes and unnecessary work
2118     return nsEventStatus_eIgnore;
2119   }
2120 
2121   switch (aEvent.mScrollMode) {
2122     case ScrollWheelInput::SCROLLMODE_INSTANT: {
2123       // Wheel events from "clicky" mouse wheels trigger scroll snapping to the
2124       // next snap point. Check for this, and adjust the delta to take into
2125       // account the snap point.
2126       CSSPoint startPosition = mFrameMetrics.GetScrollOffset();
2127       MaybeAdjustDeltaForScrollSnapping(aEvent, delta, startPosition);
2128 
2129       ScreenPoint distance = ToScreenCoordinates(
2130           ParentLayerPoint(fabs(delta.x), fabs(delta.y)), aEvent.mLocalOrigin);
2131 
2132       CancelAnimation();
2133 
2134       OverscrollHandoffState handoffState(
2135           *mInputQueue->GetCurrentWheelBlock()->GetOverscrollHandoffChain(),
2136           distance, ScrollSource::Wheel);
2137       ParentLayerPoint startPoint = aEvent.mLocalOrigin;
2138       ParentLayerPoint endPoint = aEvent.mLocalOrigin - delta;
2139       CallDispatchScroll(startPoint, endPoint, handoffState);
2140 
2141       SetState(NOTHING);
2142 
2143       // The calls above handle their own locking; moreover,
2144       // ToScreenCoordinates() and CallDispatchScroll() can grab the tree lock.
2145       RecursiveMutexAutoLock lock(mRecursiveMutex);
2146       RequestContentRepaint();
2147 
2148       break;
2149     }
2150 
2151     case ScrollWheelInput::SCROLLMODE_SMOOTH: {
2152       // The lock must be held across the entire update operation, so the
2153       // compositor doesn't end the animation before we get a chance to
2154       // update it.
2155       RecursiveMutexAutoLock lock(mRecursiveMutex);
2156 
2157       // Perform scroll snapping if appropriate.
2158       CSSPoint startPosition = mFrameMetrics.GetScrollOffset();
2159       // If we're already in a wheel scroll or smooth scroll animation,
2160       // the delta is applied to its destination, not to the current
2161       // scroll position. Take this into account when finding a snap point.
2162       if (mState == WHEEL_SCROLL) {
2163         startPosition = mAnimation->AsWheelScrollAnimation()->GetDestination();
2164       } else if (mState == SMOOTH_SCROLL) {
2165         startPosition = mAnimation->AsSmoothScrollAnimation()->GetDestination();
2166       } else if (mState == KEYBOARD_SCROLL) {
2167         startPosition =
2168             mAnimation->AsKeyboardScrollAnimation()->GetDestination();
2169       }
2170       if (MaybeAdjustDeltaForScrollSnapping(aEvent, delta, startPosition)) {
2171         // If we're scroll snapping, use a smooth scroll animation to get
2172         // the desired physics. Note that SmoothScrollTo() will re-use an
2173         // existing smooth scroll animation if there is one.
2174         APZC_LOG("%p wheel scrolling to snap point %s\n", this,
2175                  Stringify(startPosition).c_str());
2176         SmoothScrollTo(startPosition);
2177         break;
2178       }
2179 
2180       // Otherwise, use a wheel scroll animation, also reusing one if possible.
2181       if (mState != WHEEL_SCROLL) {
2182         CancelAnimation();
2183         SetState(WHEEL_SCROLL);
2184 
2185         nsPoint initialPosition =
2186             CSSPoint::ToAppUnits(mFrameMetrics.GetScrollOffset());
2187         StartAnimation(new WheelScrollAnimation(*this, initialPosition,
2188                                                 aEvent.mDeltaType));
2189       }
2190 
2191       nsPoint deltaInAppUnits =
2192           CSSPoint::ToAppUnits(delta / mFrameMetrics.GetZoom());
2193 
2194       // Convert velocity from ParentLayerPoints/ms to ParentLayerPoints/s and
2195       // then to appunits/second.
2196       nsPoint velocity =
2197           CSSPoint::ToAppUnits(ParentLayerPoint(mX.GetVelocity() * 1000.0f,
2198                                                 mY.GetVelocity() * 1000.0f) /
2199                                mFrameMetrics.GetZoom());
2200 
2201       WheelScrollAnimation* animation = mAnimation->AsWheelScrollAnimation();
2202       animation->UpdateDelta(aEvent.mTimeStamp, deltaInAppUnits,
2203                              nsSize(velocity.x, velocity.y));
2204       break;
2205     }
2206   }
2207 
2208   return nsEventStatus_eConsumeNoDefault;
2209 }
2210 
NotifyMozMouseScrollEvent(const nsString & aString) const2211 void AsyncPanZoomController::NotifyMozMouseScrollEvent(
2212     const nsString& aString) const {
2213   RefPtr<GeckoContentController> controller = GetGeckoContentController();
2214   if (!controller) {
2215     return;
2216   }
2217 
2218   controller->NotifyMozMouseScrollEvent(mFrameMetrics.GetScrollId(), aString);
2219 }
2220 
OnPanMayBegin(const PanGestureInput & aEvent)2221 nsEventStatus AsyncPanZoomController::OnPanMayBegin(
2222     const PanGestureInput& aEvent) {
2223   APZC_LOG("%p got a pan-maybegin in state %d\n", this, mState);
2224 
2225   mX.StartTouch(aEvent.mLocalPanStartPoint.x, aEvent.mTime);
2226   mY.StartTouch(aEvent.mLocalPanStartPoint.y, aEvent.mTime);
2227   MOZ_ASSERT(GetCurrentPanGestureBlock());
2228   GetCurrentPanGestureBlock()->GetOverscrollHandoffChain()->CancelAnimations();
2229 
2230   return nsEventStatus_eConsumeNoDefault;
2231 }
2232 
OnPanCancelled(const PanGestureInput & aEvent)2233 nsEventStatus AsyncPanZoomController::OnPanCancelled(
2234     const PanGestureInput& aEvent) {
2235   APZC_LOG("%p got a pan-cancelled in state %d\n", this, mState);
2236 
2237   mX.CancelGesture();
2238   mY.CancelGesture();
2239 
2240   return nsEventStatus_eConsumeNoDefault;
2241 }
2242 
OnPanBegin(const PanGestureInput & aEvent)2243 nsEventStatus AsyncPanZoomController::OnPanBegin(
2244     const PanGestureInput& aEvent) {
2245   APZC_LOG("%p got a pan-begin in state %d\n", this, mState);
2246 
2247   if (mState == SMOOTH_SCROLL) {
2248     // SMOOTH_SCROLL scrolls are cancelled by pan gestures.
2249     CancelAnimation();
2250   }
2251 
2252   mX.StartTouch(aEvent.mLocalPanStartPoint.x, aEvent.mTime);
2253   mY.StartTouch(aEvent.mLocalPanStartPoint.y, aEvent.mTime);
2254 
2255   if (GetAxisLockMode() == FREE) {
2256     SetState(PANNING);
2257     return nsEventStatus_eConsumeNoDefault;
2258   }
2259 
2260   float dx = aEvent.mPanDisplacement.x, dy = aEvent.mPanDisplacement.y;
2261 
2262   if (dx || dy) {
2263     double angle = atan2(dy, dx);  // range [-pi, pi]
2264     angle = fabs(angle);           // range [0, pi]
2265     HandlePanning(angle);
2266   } else {
2267     SetState(PANNING);
2268   }
2269 
2270   // Call into OnPan in order to process any delta included in this event.
2271   OnPan(aEvent, true);
2272 
2273   return nsEventStatus_eConsumeNoDefault;
2274 }
2275 
OnPan(const PanGestureInput & aEvent,bool aFingersOnTouchpad)2276 nsEventStatus AsyncPanZoomController::OnPan(const PanGestureInput& aEvent,
2277                                             bool aFingersOnTouchpad) {
2278   APZC_LOG("%p got a pan-pan in state %d\n", this, mState);
2279 
2280   if (mState == SMOOTH_SCROLL) {
2281     if (!aFingersOnTouchpad) {
2282       // When a SMOOTH_SCROLL scroll is being processed on a frame, mouse
2283       // wheel and trackpad momentum scroll position updates will not cancel the
2284       // SMOOTH_SCROLL scroll animations, enabling scripts that depend on
2285       // them to be responsive without forcing the user to wait for the momentum
2286       // scrolling to completely stop.
2287       return nsEventStatus_eConsumeNoDefault;
2288     }
2289 
2290     // SMOOTH_SCROLL scrolls are cancelled by pan gestures.
2291     CancelAnimation();
2292   }
2293 
2294   if (mState == NOTHING) {
2295     // This event block was interrupted by something else. If the user's fingers
2296     // are still on on the touchpad we want to resume scrolling, otherwise we
2297     // ignore the rest of the scroll gesture.
2298     if (!aFingersOnTouchpad) {
2299       return nsEventStatus_eConsumeNoDefault;
2300     }
2301     // Resume / restart the pan.
2302     // PanBegin will call back into this function with mState == PANNING.
2303     return OnPanBegin(aEvent);
2304   }
2305 
2306   // Note that there is a multiplier that applies onto the "physical" pan
2307   // displacement (how much the user's fingers moved) that produces the
2308   // "logical" pan displacement (how much the page should move). For some of the
2309   // code below it makes more sense to use the physical displacement rather than
2310   // the logical displacement, and vice-versa.
2311   ScreenPoint physicalPanDisplacement = aEvent.mPanDisplacement;
2312   ParentLayerPoint logicalPanDisplacement =
2313       aEvent.UserMultipliedLocalPanDisplacement();
2314 
2315   MOZ_ASSERT(GetCurrentPanGestureBlock());
2316   AdjustDeltaForAllowedScrollDirections(
2317       logicalPanDisplacement,
2318       GetCurrentPanGestureBlock()->GetAllowedScrollDirections());
2319 
2320   // We need to update the axis velocity in order to get a useful display port
2321   // size and position. We need to do so even if this is a momentum pan (i.e.
2322   // aFingersOnTouchpad == false); in that case the "with touch" part is not
2323   // really appropriate, so we may want to rethink this at some point.
2324   mX.UpdateWithTouchAtDevicePoint(aEvent.mLocalPanStartPoint.x,
2325                                   logicalPanDisplacement.x, aEvent.mTime);
2326   mY.UpdateWithTouchAtDevicePoint(aEvent.mLocalPanStartPoint.y,
2327                                   logicalPanDisplacement.y, aEvent.mTime);
2328 
2329   HandlePanningUpdate(physicalPanDisplacement);
2330 
2331   ScreenPoint panDistance(fabs(physicalPanDisplacement.x),
2332                           fabs(physicalPanDisplacement.y));
2333   OverscrollHandoffState handoffState(
2334       *GetCurrentPanGestureBlock()->GetOverscrollHandoffChain(), panDistance,
2335       ScrollSource::Wheel);
2336 
2337   // Create fake "touch" positions that will result in the desired scroll
2338   // motion. Note that the pan displacement describes the change in scroll
2339   // position: positive displacement values mean that the scroll position
2340   // increases. However, an increase in scroll position means that the scrolled
2341   // contents are moved to the left / upwards. Since our simulated "touches"
2342   // determine the motion of the scrolled contents, not of the scroll position,
2343   // they need to move in the opposite direction of the pan displacement.
2344   ParentLayerPoint startPoint = aEvent.mLocalPanStartPoint;
2345   ParentLayerPoint endPoint =
2346       aEvent.mLocalPanStartPoint - logicalPanDisplacement;
2347   CallDispatchScroll(startPoint, endPoint, handoffState);
2348 
2349   return nsEventStatus_eConsumeNoDefault;
2350 }
2351 
OnPanEnd(const PanGestureInput & aEvent)2352 nsEventStatus AsyncPanZoomController::OnPanEnd(const PanGestureInput& aEvent) {
2353   APZC_LOG("%p got a pan-end in state %d\n", this, mState);
2354 
2355   // Call into OnPan in order to process any delta included in this event.
2356   OnPan(aEvent, true);
2357 
2358   mX.EndTouch(aEvent.mTime);
2359   mY.EndTouch(aEvent.mTime);
2360 
2361   // Drop any velocity on axes where we don't have room to scroll anyways
2362   // (in this APZC, or an APZC further in the handoff chain).
2363   // This ensures that we don't enlarge the display port unnecessarily.
2364   MOZ_ASSERT(GetCurrentPanGestureBlock());
2365   RefPtr<const OverscrollHandoffChain> overscrollHandoffChain =
2366       GetCurrentPanGestureBlock()->GetOverscrollHandoffChain();
2367   if (!overscrollHandoffChain->CanScrollInDirection(
2368           this, ScrollDirection::eHorizontal)) {
2369     mX.SetVelocity(0);
2370   }
2371   if (!overscrollHandoffChain->CanScrollInDirection(
2372           this, ScrollDirection::eVertical)) {
2373     mY.SetVelocity(0);
2374   }
2375 
2376   SetState(NOTHING);
2377   RequestContentRepaint();
2378 
2379   if (!aEvent.mFollowedByMomentum) {
2380     ScrollSnap();
2381   }
2382 
2383   return nsEventStatus_eConsumeNoDefault;
2384 }
2385 
OnPanMomentumStart(const PanGestureInput & aEvent)2386 nsEventStatus AsyncPanZoomController::OnPanMomentumStart(
2387     const PanGestureInput& aEvent) {
2388   APZC_LOG("%p got a pan-momentumstart in state %d\n", this, mState);
2389 
2390   if (mState == SMOOTH_SCROLL) {
2391     // SMOOTH_SCROLL scrolls are cancelled by pan gestures.
2392     CancelAnimation();
2393   }
2394 
2395   SetState(PAN_MOMENTUM);
2396   ScrollSnapToDestination();
2397 
2398   // Call into OnPan in order to process any delta included in this event.
2399   OnPan(aEvent, false);
2400 
2401   return nsEventStatus_eConsumeNoDefault;
2402 }
2403 
OnPanMomentumEnd(const PanGestureInput & aEvent)2404 nsEventStatus AsyncPanZoomController::OnPanMomentumEnd(
2405     const PanGestureInput& aEvent) {
2406   APZC_LOG("%p got a pan-momentumend in state %d\n", this, mState);
2407 
2408   // Call into OnPan in order to process any delta included in this event.
2409   OnPan(aEvent, false);
2410 
2411   // We need to reset the velocity to zero. We don't really have a "touch"
2412   // here because the touch has already ended long before the momentum
2413   // animation started, but I guess it doesn't really matter for now.
2414   mX.CancelGesture();
2415   mY.CancelGesture();
2416   SetState(NOTHING);
2417 
2418   RequestContentRepaint();
2419 
2420   return nsEventStatus_eConsumeNoDefault;
2421 }
2422 
OnLongPress(const TapGestureInput & aEvent)2423 nsEventStatus AsyncPanZoomController::OnLongPress(
2424     const TapGestureInput& aEvent) {
2425   APZC_LOG("%p got a long-press in state %d\n", this, mState);
2426   RefPtr<GeckoContentController> controller = GetGeckoContentController();
2427   if (controller) {
2428     LayoutDevicePoint geckoScreenPoint;
2429     if (ConvertToGecko(aEvent.mPoint, &geckoScreenPoint)) {
2430       TouchBlockState* touch = GetCurrentTouchBlock();
2431       if (!touch) {
2432         APZC_LOG(
2433             "%p dropping long-press because some non-touch block interrupted "
2434             "it\n",
2435             this);
2436         return nsEventStatus_eIgnore;
2437       }
2438       if (touch->IsDuringFastFling()) {
2439         APZC_LOG("%p dropping long-press because of fast fling\n", this);
2440         return nsEventStatus_eIgnore;
2441       }
2442       uint64_t blockId = GetInputQueue()->InjectNewTouchBlock(this);
2443       controller->HandleTap(TapType::eLongTap, geckoScreenPoint,
2444                             aEvent.modifiers, GetGuid(), blockId);
2445       return nsEventStatus_eConsumeNoDefault;
2446     }
2447   }
2448   return nsEventStatus_eIgnore;
2449 }
2450 
OnLongPressUp(const TapGestureInput & aEvent)2451 nsEventStatus AsyncPanZoomController::OnLongPressUp(
2452     const TapGestureInput& aEvent) {
2453   APZC_LOG("%p got a long-tap-up in state %d\n", this, mState);
2454   return GenerateSingleTap(TapType::eLongTapUp, aEvent.mPoint,
2455                            aEvent.modifiers);
2456 }
2457 
GenerateSingleTap(TapType aType,const ScreenIntPoint & aPoint,mozilla::Modifiers aModifiers)2458 nsEventStatus AsyncPanZoomController::GenerateSingleTap(
2459     TapType aType, const ScreenIntPoint& aPoint,
2460     mozilla::Modifiers aModifiers) {
2461   RefPtr<GeckoContentController> controller = GetGeckoContentController();
2462   if (controller) {
2463     LayoutDevicePoint geckoScreenPoint;
2464     if (ConvertToGecko(aPoint, &geckoScreenPoint)) {
2465       TouchBlockState* touch = GetCurrentTouchBlock();
2466       // |touch| may be null in the case where this function is
2467       // invoked by GestureEventListener on a timeout. In that case we already
2468       // verified that the single tap is allowed so we let it through.
2469       // XXX there is a bug here that in such a case the touch block that
2470       // generated this tap will not get its mSingleTapOccurred flag set.
2471       // See https://bugzilla.mozilla.org/show_bug.cgi?id=1256344#c6
2472       if (touch) {
2473         if (touch->IsDuringFastFling()) {
2474           APZC_LOG(
2475               "%p dropping single-tap because it was during a fast-fling\n",
2476               this);
2477           return nsEventStatus_eIgnore;
2478         }
2479         touch->SetSingleTapOccurred();
2480       }
2481       // Because this may be being running as part of
2482       // APZCTreeManager::ReceiveInputEvent, calling controller->HandleTap
2483       // directly might mean that content receives the single tap message before
2484       // the corresponding touch-up. To avoid that we schedule the singletap
2485       // message to run on the next spin of the event loop. See bug 965381 for
2486       // the issue this was causing.
2487       RefPtr<Runnable> runnable =
2488           NewRunnableMethod<TapType, LayoutDevicePoint, mozilla::Modifiers,
2489                             ScrollableLayerGuid, uint64_t>(
2490               "layers::GeckoContentController::HandleTap", controller,
2491               &GeckoContentController::HandleTap, aType, geckoScreenPoint,
2492               aModifiers, GetGuid(), touch ? touch->GetBlockId() : 0);
2493 
2494       controller->PostDelayedTask(runnable.forget(), 0);
2495       return nsEventStatus_eConsumeNoDefault;
2496     }
2497   }
2498   return nsEventStatus_eIgnore;
2499 }
2500 
OnTouchEndOrCancel()2501 void AsyncPanZoomController::OnTouchEndOrCancel() {
2502   if (RefPtr<GeckoContentController> controller = GetGeckoContentController()) {
2503     MOZ_ASSERT(GetCurrentTouchBlock());
2504     controller->NotifyAPZStateChange(
2505         GetGuid(), APZStateChange::eEndTouch,
2506         GetCurrentTouchBlock()->SingleTapOccurred());
2507   }
2508 }
2509 
OnSingleTapUp(const TapGestureInput & aEvent)2510 nsEventStatus AsyncPanZoomController::OnSingleTapUp(
2511     const TapGestureInput& aEvent) {
2512   APZC_LOG("%p got a single-tap-up in state %d\n", this, mState);
2513   // If mZoomConstraints.mAllowDoubleTapZoom is true we wait for a call to
2514   // OnSingleTapConfirmed before sending event to content
2515   MOZ_ASSERT(GetCurrentTouchBlock());
2516   if (!(mZoomConstraints.mAllowDoubleTapZoom &&
2517         GetCurrentTouchBlock()->TouchActionAllowsDoubleTapZoom())) {
2518     return GenerateSingleTap(TapType::eSingleTap, aEvent.mPoint,
2519                              aEvent.modifiers);
2520   }
2521   return nsEventStatus_eIgnore;
2522 }
2523 
OnSingleTapConfirmed(const TapGestureInput & aEvent)2524 nsEventStatus AsyncPanZoomController::OnSingleTapConfirmed(
2525     const TapGestureInput& aEvent) {
2526   APZC_LOG("%p got a single-tap-confirmed in state %d\n", this, mState);
2527   return GenerateSingleTap(TapType::eSingleTap, aEvent.mPoint,
2528                            aEvent.modifiers);
2529 }
2530 
OnDoubleTap(const TapGestureInput & aEvent)2531 nsEventStatus AsyncPanZoomController::OnDoubleTap(
2532     const TapGestureInput& aEvent) {
2533   APZC_LOG("%p got a double-tap in state %d\n", this, mState);
2534   RefPtr<GeckoContentController> controller = GetGeckoContentController();
2535   if (controller) {
2536     MOZ_ASSERT(GetCurrentTouchBlock());
2537     if (mZoomConstraints.mAllowDoubleTapZoom &&
2538         GetCurrentTouchBlock()->TouchActionAllowsDoubleTapZoom()) {
2539       LayoutDevicePoint geckoScreenPoint;
2540       if (ConvertToGecko(aEvent.mPoint, &geckoScreenPoint)) {
2541         controller->HandleTap(TapType::eDoubleTap, geckoScreenPoint,
2542                               aEvent.modifiers, GetGuid(),
2543                               GetCurrentTouchBlock()->GetBlockId());
2544       }
2545     }
2546     return nsEventStatus_eConsumeNoDefault;
2547   }
2548   return nsEventStatus_eIgnore;
2549 }
2550 
OnSecondTap(const TapGestureInput & aEvent)2551 nsEventStatus AsyncPanZoomController::OnSecondTap(
2552     const TapGestureInput& aEvent) {
2553   APZC_LOG("%p got a second-tap in state %d\n", this, mState);
2554   return GenerateSingleTap(TapType::eSecondTap, aEvent.mPoint,
2555                            aEvent.modifiers);
2556 }
2557 
OnCancelTap(const TapGestureInput & aEvent)2558 nsEventStatus AsyncPanZoomController::OnCancelTap(
2559     const TapGestureInput& aEvent) {
2560   APZC_LOG("%p got a cancel-tap in state %d\n", this, mState);
2561   // XXX: Implement this.
2562   return nsEventStatus_eIgnore;
2563 }
2564 
GetTransformToThis() const2565 ScreenToParentLayerMatrix4x4 AsyncPanZoomController::GetTransformToThis()
2566     const {
2567   if (APZCTreeManager* treeManagerLocal = GetApzcTreeManager()) {
2568     return treeManagerLocal->GetScreenToApzcTransform(this);
2569   }
2570   return ScreenToParentLayerMatrix4x4();
2571 }
2572 
ToScreenCoordinates(const ParentLayerPoint & aVector,const ParentLayerPoint & aAnchor) const2573 ScreenPoint AsyncPanZoomController::ToScreenCoordinates(
2574     const ParentLayerPoint& aVector, const ParentLayerPoint& aAnchor) const {
2575   return TransformVector(GetTransformToThis().Inverse(), aVector, aAnchor);
2576 }
2577 
2578 // TODO: figure out a good way to check the w-coordinate is positive and return
2579 // the result
ToParentLayerCoordinates(const ScreenPoint & aVector,const ScreenPoint & aAnchor) const2580 ParentLayerPoint AsyncPanZoomController::ToParentLayerCoordinates(
2581     const ScreenPoint& aVector, const ScreenPoint& aAnchor) const {
2582   return TransformVector(GetTransformToThis(), aVector, aAnchor);
2583 }
2584 
Contains(const ScreenIntPoint & aPoint) const2585 bool AsyncPanZoomController::Contains(const ScreenIntPoint& aPoint) const {
2586   ScreenToParentLayerMatrix4x4 transformToThis = GetTransformToThis();
2587   Maybe<ParentLayerIntPoint> point = UntransformBy(transformToThis, aPoint);
2588   if (!point) {
2589     return false;
2590   }
2591 
2592   ParentLayerIntRect cb;
2593   {
2594     RecursiveMutexAutoLock lock(mRecursiveMutex);
2595     GetFrameMetrics().GetCompositionBounds().ToIntRect(&cb);
2596   }
2597   return cb.Contains(*point);
2598 }
2599 
PanDistance() const2600 ScreenCoord AsyncPanZoomController::PanDistance() const {
2601   ParentLayerPoint panVector;
2602   ParentLayerPoint panStart;
2603   {
2604     RecursiveMutexAutoLock lock(mRecursiveMutex);
2605     panVector = ParentLayerPoint(mX.PanDistance(), mY.PanDistance());
2606     panStart = PanStart();
2607   }
2608   return ToScreenCoordinates(panVector, panStart).Length();
2609 }
2610 
PanStart() const2611 ParentLayerPoint AsyncPanZoomController::PanStart() const {
2612   return ParentLayerPoint(mX.PanStart(), mY.PanStart());
2613 }
2614 
GetVelocityVector() const2615 const ParentLayerPoint AsyncPanZoomController::GetVelocityVector() const {
2616   return ParentLayerPoint(mX.GetVelocity(), mY.GetVelocity());
2617 }
2618 
SetVelocityVector(const ParentLayerPoint & aVelocityVector)2619 void AsyncPanZoomController::SetVelocityVector(
2620     const ParentLayerPoint& aVelocityVector) {
2621   mX.SetVelocity(aVelocityVector.x);
2622   mY.SetVelocity(aVelocityVector.y);
2623 }
2624 
HandlePanningWithTouchAction(double aAngle)2625 void AsyncPanZoomController::HandlePanningWithTouchAction(double aAngle) {
2626   // Handling of cross sliding will need to be added in this method after
2627   // touch-action released enabled by default.
2628   MOZ_ASSERT(GetCurrentTouchBlock());
2629   RefPtr<const OverscrollHandoffChain> overscrollHandoffChain =
2630       GetCurrentInputBlock()->GetOverscrollHandoffChain();
2631   bool canScrollHorizontal =
2632       !mX.IsAxisLocked() && overscrollHandoffChain->CanScrollInDirection(
2633                                 this, ScrollDirection::eHorizontal);
2634   bool canScrollVertical =
2635       !mY.IsAxisLocked() && overscrollHandoffChain->CanScrollInDirection(
2636                                 this, ScrollDirection::eVertical);
2637   if (GetCurrentTouchBlock()->TouchActionAllowsPanningXY()) {
2638     if (canScrollHorizontal && canScrollVertical) {
2639       if (IsCloseToHorizontal(aAngle, gfxPrefs::APZAxisLockAngle())) {
2640         mY.SetAxisLocked(true);
2641         SetState(PANNING_LOCKED_X);
2642       } else if (IsCloseToVertical(aAngle, gfxPrefs::APZAxisLockAngle())) {
2643         mX.SetAxisLocked(true);
2644         SetState(PANNING_LOCKED_Y);
2645       } else {
2646         SetState(PANNING);
2647       }
2648     } else if (canScrollHorizontal || canScrollVertical) {
2649       SetState(PANNING);
2650     } else {
2651       SetState(NOTHING);
2652     }
2653   } else if (GetCurrentTouchBlock()->TouchActionAllowsPanningX()) {
2654     // Using bigger angle for panning to keep behavior consistent
2655     // with IE.
2656     if (IsCloseToHorizontal(aAngle, gfxPrefs::APZAllowedDirectPanAngle())) {
2657       mY.SetAxisLocked(true);
2658       SetState(PANNING_LOCKED_X);
2659       mPanDirRestricted = true;
2660     } else {
2661       // Don't treat these touches as pan/zoom movements since 'touch-action'
2662       // value requires it.
2663       SetState(NOTHING);
2664     }
2665   } else if (GetCurrentTouchBlock()->TouchActionAllowsPanningY()) {
2666     if (IsCloseToVertical(aAngle, gfxPrefs::APZAllowedDirectPanAngle())) {
2667       mX.SetAxisLocked(true);
2668       SetState(PANNING_LOCKED_Y);
2669       mPanDirRestricted = true;
2670     } else {
2671       SetState(NOTHING);
2672     }
2673   } else {
2674     SetState(NOTHING);
2675   }
2676   if (!IsInPanningState()) {
2677     // If we didn't enter a panning state because touch-action disallowed it,
2678     // make sure to clear any leftover velocity from the pre-threshold
2679     // touchmoves.
2680     mX.SetVelocity(0);
2681     mY.SetVelocity(0);
2682   }
2683 }
2684 
HandlePanning(double aAngle)2685 void AsyncPanZoomController::HandlePanning(double aAngle) {
2686   RecursiveMutexAutoLock lock(mRecursiveMutex);
2687   MOZ_ASSERT(GetCurrentInputBlock());
2688   RefPtr<const OverscrollHandoffChain> overscrollHandoffChain =
2689       GetCurrentInputBlock()->GetOverscrollHandoffChain();
2690   bool canScrollHorizontal =
2691       !mX.IsAxisLocked() && overscrollHandoffChain->CanScrollInDirection(
2692                                 this, ScrollDirection::eHorizontal);
2693   bool canScrollVertical =
2694       !mY.IsAxisLocked() && overscrollHandoffChain->CanScrollInDirection(
2695                                 this, ScrollDirection::eVertical);
2696 
2697   if (!canScrollHorizontal || !canScrollVertical) {
2698     SetState(PANNING);
2699   } else if (IsCloseToHorizontal(aAngle, gfxPrefs::APZAxisLockAngle())) {
2700     mY.SetAxisLocked(true);
2701     if (canScrollHorizontal) {
2702       SetState(PANNING_LOCKED_X);
2703     }
2704   } else if (IsCloseToVertical(aAngle, gfxPrefs::APZAxisLockAngle())) {
2705     mX.SetAxisLocked(true);
2706     if (canScrollVertical) {
2707       SetState(PANNING_LOCKED_Y);
2708     }
2709   } else {
2710     SetState(PANNING);
2711   }
2712 }
2713 
HandlePanningUpdate(const ScreenPoint & aPanDistance)2714 void AsyncPanZoomController::HandlePanningUpdate(
2715     const ScreenPoint& aPanDistance) {
2716   // If we're axis-locked, check if the user is trying to break the lock
2717   if (GetAxisLockMode() == STICKY && !mPanDirRestricted) {
2718     double angle = atan2(aPanDistance.y, aPanDistance.x);  // range [-pi, pi]
2719     angle = fabs(angle);                                   // range [0, pi]
2720 
2721     float breakThreshold =
2722         gfxPrefs::APZAxisBreakoutThreshold() * APZCTreeManager::GetDPI();
2723 
2724     if (fabs(aPanDistance.x) > breakThreshold ||
2725         fabs(aPanDistance.y) > breakThreshold) {
2726       if (mState == PANNING_LOCKED_X) {
2727         if (!IsCloseToHorizontal(angle, gfxPrefs::APZAxisBreakoutAngle())) {
2728           mY.SetAxisLocked(false);
2729           SetState(PANNING);
2730         }
2731       } else if (mState == PANNING_LOCKED_Y) {
2732         if (!IsCloseToVertical(angle, gfxPrefs::APZAxisBreakoutAngle())) {
2733           mX.SetAxisLocked(false);
2734           SetState(PANNING);
2735         }
2736       }
2737     }
2738   }
2739 }
2740 
HandlePinchLocking(ScreenCoord spanDistance,ScreenPoint focusChange)2741 void AsyncPanZoomController::HandlePinchLocking(ScreenCoord spanDistance,
2742                                                 ScreenPoint focusChange) {
2743   if (mPinchLocked) {
2744     if (GetPinchLockMode() == PINCH_STICKY) {
2745       ScreenCoord spanBreakoutThreshold =
2746           gfxPrefs::APZPinchLockSpanBreakoutThreshold() *
2747           APZCTreeManager::GetDPI();
2748       mPinchLocked = !(spanDistance > spanBreakoutThreshold);
2749     }
2750   } else {
2751     if (GetPinchLockMode() != PINCH_FREE) {
2752       ScreenCoord spanLockThreshold =
2753           gfxPrefs::APZPinchLockSpanLockThreshold() * APZCTreeManager::GetDPI();
2754       ScreenCoord scrollLockThreshold =
2755           gfxPrefs::APZPinchLockScrollLockThreshold() *
2756           APZCTreeManager::GetDPI();
2757 
2758       if (spanDistance < spanLockThreshold &&
2759           focusChange.Length() > scrollLockThreshold) {
2760         mPinchLocked = true;
2761       }
2762     }
2763   }
2764 }
2765 
StartPanning(const ParentLayerPoint & aStartPoint)2766 nsEventStatus AsyncPanZoomController::StartPanning(
2767     const ParentLayerPoint& aStartPoint) {
2768   RecursiveMutexAutoLock lock(mRecursiveMutex);
2769 
2770   float dx = mX.PanDistance(aStartPoint.x);
2771   float dy = mY.PanDistance(aStartPoint.y);
2772 
2773   double angle = atan2(dy, dx);  // range [-pi, pi]
2774   angle = fabs(angle);           // range [0, pi]
2775 
2776   if (gfxPrefs::TouchActionEnabled()) {
2777     HandlePanningWithTouchAction(angle);
2778   } else {
2779     if (GetAxisLockMode() == FREE) {
2780       SetState(PANNING);
2781     } else {
2782       HandlePanning(angle);
2783     }
2784   }
2785 
2786   if (IsInPanningState()) {
2787     if (RefPtr<GeckoContentController> controller =
2788             GetGeckoContentController()) {
2789       controller->NotifyAPZStateChange(GetGuid(),
2790                                        APZStateChange::eStartPanning);
2791     }
2792     return nsEventStatus_eConsumeNoDefault;
2793   }
2794   // Don't consume an event that didn't trigger a panning.
2795   return nsEventStatus_eIgnore;
2796 }
2797 
UpdateWithTouchAtDevicePoint(const MultiTouchInput & aEvent)2798 void AsyncPanZoomController::UpdateWithTouchAtDevicePoint(
2799     const MultiTouchInput& aEvent) {
2800   ParentLayerPoint point = GetFirstTouchPoint(aEvent);
2801   mX.UpdateWithTouchAtDevicePoint(point.x, 0, aEvent.mTime);
2802   mY.UpdateWithTouchAtDevicePoint(point.y, 0, aEvent.mTime);
2803 }
2804 
AttemptScroll(ParentLayerPoint & aStartPoint,ParentLayerPoint & aEndPoint,OverscrollHandoffState & aOverscrollHandoffState)2805 bool AsyncPanZoomController::AttemptScroll(
2806     ParentLayerPoint& aStartPoint, ParentLayerPoint& aEndPoint,
2807     OverscrollHandoffState& aOverscrollHandoffState) {
2808   // "start - end" rather than "end - start" because e.g. moving your finger
2809   // down (*positive* direction along y axis) causes the vertical scroll offset
2810   // to *decrease* as the page follows your finger.
2811   ParentLayerPoint displacement = aStartPoint - aEndPoint;
2812 
2813   ParentLayerPoint overscroll;  // will be used outside monitor block
2814 
2815   // If the direction of panning is reversed within the same input block,
2816   // a later event in the block could potentially scroll an APZC earlier
2817   // in the handoff chain, than an earlier event in the block (because
2818   // the earlier APZC was scrolled to its extent in the original direction).
2819   // We want to disallow this.
2820   bool scrollThisApzc = false;
2821   if (InputBlockState* block = GetCurrentInputBlock()) {
2822     scrollThisApzc =
2823         !block->GetScrolledApzc() || block->IsDownchainOfScrolledApzc(this);
2824   }
2825 
2826   if (scrollThisApzc) {
2827     RecursiveMutexAutoLock lock(mRecursiveMutex);
2828     bool forcesVerticalOverscroll =
2829         ScrollSource::Wheel == aOverscrollHandoffState.mScrollSource &&
2830         mScrollMetadata.GetDisregardedDirection() ==
2831             Some(ScrollDirection::eVertical);
2832     bool forcesHorizontalOverscroll =
2833         ScrollSource::Wheel == aOverscrollHandoffState.mScrollSource &&
2834         mScrollMetadata.GetDisregardedDirection() ==
2835             Some(ScrollDirection::eHorizontal);
2836 
2837     ParentLayerPoint adjustedDisplacement;
2838     bool yChanged =
2839         mY.AdjustDisplacement(displacement.y, adjustedDisplacement.y,
2840                               overscroll.y, forcesVerticalOverscroll);
2841     bool xChanged =
2842         mX.AdjustDisplacement(displacement.x, adjustedDisplacement.x,
2843                               overscroll.x, forcesHorizontalOverscroll);
2844     if (xChanged || yChanged) {
2845       ScheduleComposite();
2846     }
2847 
2848     if (!IsZero(adjustedDisplacement)) {
2849       ScrollBy(adjustedDisplacement / mFrameMetrics.GetZoom());
2850       if (InputBlockState* block = GetCurrentInputBlock()) {
2851 #if defined(MOZ_WIDGET_ANDROID)
2852         if (block->AsTouchBlock() && (block->GetScrolledApzc() != this) &&
2853             IsRootContent()) {
2854           if (APZCTreeManager* manager = GetApzcTreeManager()) {
2855             AndroidDynamicToolbarAnimator* animator =
2856                 manager->GetAndroidDynamicToolbarAnimator();
2857             MOZ_ASSERT(animator);
2858             animator->SetScrollingRootContent();
2859           }
2860         }
2861 #endif
2862         block->SetScrolledApzc(this);
2863       }
2864       ScheduleCompositeAndMaybeRepaint();
2865       UpdateSharedCompositorFrameMetrics();
2866     }
2867 
2868     // Adjust the start point to reflect the consumed portion of the scroll.
2869     aStartPoint = aEndPoint + overscroll;
2870   } else {
2871     overscroll = displacement;
2872   }
2873 
2874   // If we consumed the entire displacement as a normal scroll, great.
2875   if (IsZero(overscroll)) {
2876     return true;
2877   }
2878 
2879   if (AllowScrollHandoffInCurrentBlock()) {
2880     // If there is overscroll, first try to hand it off to an APZC later
2881     // in the handoff chain to consume (either as a normal scroll or as
2882     // overscroll).
2883     // Note: "+ overscroll" rather than "- overscroll" because "overscroll"
2884     // is what's left of "displacement", and "displacement" is "start - end".
2885     ++aOverscrollHandoffState.mChainIndex;
2886     CallDispatchScroll(aStartPoint, aEndPoint, aOverscrollHandoffState);
2887 
2888     overscroll = aStartPoint - aEndPoint;
2889     if (IsZero(overscroll)) {
2890       return true;
2891     }
2892   }
2893 
2894   // If there is no APZC later in the handoff chain that accepted the
2895   // overscroll, try to accept it ourselves. We only accept it if we
2896   // are pannable.
2897   APZC_LOG("%p taking overscroll during panning\n", this);
2898   OverscrollForPanning(overscroll, aOverscrollHandoffState.mPanDistance);
2899   aStartPoint = aEndPoint + overscroll;
2900 
2901   return IsZero(overscroll);
2902 }
2903 
OverscrollForPanning(ParentLayerPoint & aOverscroll,const ScreenPoint & aPanDistance)2904 void AsyncPanZoomController::OverscrollForPanning(
2905     ParentLayerPoint& aOverscroll, const ScreenPoint& aPanDistance) {
2906   // Only allow entering overscroll along an axis if the pan distance along
2907   // that axis is greater than the pan distance along the other axis by a
2908   // configurable factor. If we are already overscrolled, don't check this.
2909   if (!IsOverscrolled()) {
2910     if (aPanDistance.x < gfxPrefs::APZMinPanDistanceRatio() * aPanDistance.y) {
2911       aOverscroll.x = 0;
2912     }
2913     if (aPanDistance.y < gfxPrefs::APZMinPanDistanceRatio() * aPanDistance.x) {
2914       aOverscroll.y = 0;
2915     }
2916   }
2917 
2918   OverscrollBy(aOverscroll);
2919 }
2920 
OverscrollBy(ParentLayerPoint & aOverscroll)2921 void AsyncPanZoomController::OverscrollBy(ParentLayerPoint& aOverscroll) {
2922   if (!gfxPrefs::APZOverscrollEnabled()) {
2923     return;
2924   }
2925 
2926   RecursiveMutexAutoLock lock(mRecursiveMutex);
2927   // Do not go into overscroll in a direction in which we have no room to
2928   // scroll to begin with.
2929   bool xCanScroll = mX.CanScroll();
2930   bool yCanScroll = mY.CanScroll();
2931   bool xConsumed = FuzzyEqualsAdditive(aOverscroll.x, 0.0f, COORDINATE_EPSILON);
2932   bool yConsumed = FuzzyEqualsAdditive(aOverscroll.y, 0.0f, COORDINATE_EPSILON);
2933 
2934   bool shouldOverscrollX =
2935       xCanScroll && !xConsumed && mX.OverscrollBehaviorAllowsOverscrollEffect();
2936   bool shouldOverscrollY =
2937       yCanScroll && !yConsumed && mY.OverscrollBehaviorAllowsOverscrollEffect();
2938 
2939   mOverscrollEffect->ConsumeOverscroll(aOverscroll, shouldOverscrollX,
2940                                        shouldOverscrollY);
2941 }
2942 
2943 RefPtr<const OverscrollHandoffChain>
BuildOverscrollHandoffChain()2944 AsyncPanZoomController::BuildOverscrollHandoffChain() {
2945   if (APZCTreeManager* treeManagerLocal = GetApzcTreeManager()) {
2946     return treeManagerLocal->BuildOverscrollHandoffChain(this);
2947   }
2948 
2949   // This APZC IsDestroyed(). To avoid callers having to special-case this
2950   // scenario, just build a 1-element chain containing ourselves.
2951   OverscrollHandoffChain* result = new OverscrollHandoffChain;
2952   result->Add(this);
2953   return result;
2954 }
2955 
AttemptFling(const FlingHandoffState & aHandoffState)2956 ParentLayerPoint AsyncPanZoomController::AttemptFling(
2957     const FlingHandoffState& aHandoffState) {
2958   RecursiveMutexAutoLock lock(mRecursiveMutex);
2959 
2960   if (!IsPannable()) {
2961     return aHandoffState.mVelocity;
2962   }
2963 
2964   // We may have a pre-existing velocity for whatever reason (for example,
2965   // a previously handed off fling). We don't want to clobber that.
2966   APZC_LOG("%p accepting fling with velocity %s\n", this,
2967            Stringify(aHandoffState.mVelocity).c_str());
2968   ParentLayerPoint residualVelocity = aHandoffState.mVelocity;
2969   if (mX.CanScroll()) {
2970     mX.SetVelocity(mX.GetVelocity() + aHandoffState.mVelocity.x);
2971     residualVelocity.x = 0;
2972   }
2973   if (mY.CanScroll()) {
2974     mY.SetVelocity(mY.GetVelocity() + aHandoffState.mVelocity.y);
2975     residualVelocity.y = 0;
2976   }
2977 
2978   // If there's a scroll snap point near the predicted fling destination,
2979   // scroll there using a smooth scroll animation. Otherwise, start a
2980   // fling animation.
2981   ScrollSnapToDestination();
2982   if (mState != SMOOTH_SCROLL) {
2983     SetState(FLING);
2984     FlingAnimation* fling = new FlingAnimation(
2985         *this, GetPlatformSpecificState(), aHandoffState.mChain,
2986         aHandoffState.mIsHandoff, aHandoffState.mScrolledApzc);
2987     StartAnimation(fling);
2988   }
2989 
2990   return residualVelocity;
2991 }
2992 
2993 ParentLayerPoint
AdjustHandoffVelocityForOverscrollBehavior(ParentLayerPoint & aHandoffVelocity) const2994 AsyncPanZoomController::AdjustHandoffVelocityForOverscrollBehavior(
2995     ParentLayerPoint& aHandoffVelocity) const {
2996   RecursiveMutexAutoLock lock(mRecursiveMutex);
2997   ParentLayerPoint residualVelocity;
2998   if (!mX.OverscrollBehaviorAllowsHandoff()) {
2999     residualVelocity.x = aHandoffVelocity.x;
3000     aHandoffVelocity.x = 0;
3001   }
3002   if (!mY.OverscrollBehaviorAllowsHandoff()) {
3003     residualVelocity.y = aHandoffVelocity.y;
3004     aHandoffVelocity.y = 0;
3005   }
3006   return residualVelocity;
3007 }
3008 
OverscrollBehaviorAllowsSwipe() const3009 bool AsyncPanZoomController::OverscrollBehaviorAllowsSwipe() const {
3010   RecursiveMutexAutoLock lock(mRecursiveMutex);
3011   // Swipe navigation is a "non-local" overscroll behavior like handoff.
3012   return mX.OverscrollBehaviorAllowsHandoff();
3013 }
3014 
HandleFlingOverscroll(const ParentLayerPoint & aVelocity,const RefPtr<const OverscrollHandoffChain> & aOverscrollHandoffChain,const RefPtr<const AsyncPanZoomController> & aScrolledApzc)3015 void AsyncPanZoomController::HandleFlingOverscroll(
3016     const ParentLayerPoint& aVelocity,
3017     const RefPtr<const OverscrollHandoffChain>& aOverscrollHandoffChain,
3018     const RefPtr<const AsyncPanZoomController>& aScrolledApzc) {
3019   APZCTreeManager* treeManagerLocal = GetApzcTreeManager();
3020   if (treeManagerLocal) {
3021     const FlingHandoffState handoffState{aVelocity, aOverscrollHandoffChain,
3022                                          true /* handoff */, aScrolledApzc};
3023     ParentLayerPoint residualVelocity =
3024         treeManagerLocal->DispatchFling(this, handoffState);
3025     if (!IsZero(residualVelocity) && IsPannable() &&
3026         gfxPrefs::APZOverscrollEnabled()) {
3027       // Obey overscroll-behavior.
3028       RecursiveMutexAutoLock lock(mRecursiveMutex);
3029       if (!mX.OverscrollBehaviorAllowsOverscrollEffect()) {
3030         residualVelocity.x = 0;
3031       }
3032       if (!mY.OverscrollBehaviorAllowsOverscrollEffect()) {
3033         residualVelocity.y = 0;
3034       }
3035 
3036       if (!IsZero(residualVelocity)) {
3037         mOverscrollEffect->HandleFlingOverscroll(residualVelocity);
3038       }
3039     }
3040   }
3041 }
3042 
HandleSmoothScrollOverscroll(const ParentLayerPoint & aVelocity)3043 void AsyncPanZoomController::HandleSmoothScrollOverscroll(
3044     const ParentLayerPoint& aVelocity) {
3045   // We must call BuildOverscrollHandoffChain from this deferred callback
3046   // function in order to avoid a deadlock when acquiring the tree lock.
3047   HandleFlingOverscroll(aVelocity, BuildOverscrollHandoffChain(), nullptr);
3048 }
3049 
SmoothScrollTo(const CSSPoint & aDestination)3050 void AsyncPanZoomController::SmoothScrollTo(const CSSPoint& aDestination) {
3051   if (mState == SMOOTH_SCROLL && mAnimation) {
3052     APZC_LOG("%p updating destination on existing animation\n", this);
3053     RefPtr<SmoothScrollAnimation> animation(
3054         static_cast<SmoothScrollAnimation*>(mAnimation.get()));
3055     animation->SetDestination(CSSPoint::ToAppUnits(aDestination));
3056   } else {
3057     CancelAnimation();
3058     SetState(SMOOTH_SCROLL);
3059     nsPoint initialPosition =
3060         CSSPoint::ToAppUnits(mFrameMetrics.GetScrollOffset());
3061     // Convert velocity from ParentLayerPoints/ms to ParentLayerPoints/s and
3062     // then to appunits/second.
3063     nsPoint initialVelocity =
3064         CSSPoint::ToAppUnits(ParentLayerPoint(mX.GetVelocity() * 1000.0f,
3065                                               mY.GetVelocity() * 1000.0f) /
3066                              mFrameMetrics.GetZoom());
3067 
3068     nsPoint destination = CSSPoint::ToAppUnits(aDestination);
3069 
3070     StartAnimation(new SmoothScrollAnimation(
3071         *this, initialPosition, initialVelocity, destination,
3072         gfxPrefs::ScrollBehaviorSpringConstant(),
3073         gfxPrefs::ScrollBehaviorDampingRatio()));
3074   }
3075 }
3076 
StartOverscrollAnimation(const ParentLayerPoint & aVelocity)3077 void AsyncPanZoomController::StartOverscrollAnimation(
3078     const ParentLayerPoint& aVelocity) {
3079   SetState(OVERSCROLL_ANIMATION);
3080   StartAnimation(new OverscrollAnimation(*this, aVelocity));
3081 }
3082 
CallDispatchScroll(ParentLayerPoint & aStartPoint,ParentLayerPoint & aEndPoint,OverscrollHandoffState & aOverscrollHandoffState)3083 void AsyncPanZoomController::CallDispatchScroll(
3084     ParentLayerPoint& aStartPoint, ParentLayerPoint& aEndPoint,
3085     OverscrollHandoffState& aOverscrollHandoffState) {
3086   // Make a local copy of the tree manager pointer and check if it's not
3087   // null before calling DispatchScroll(). This is necessary because
3088   // Destroy(), which nulls out mTreeManager, could be called concurrently.
3089   APZCTreeManager* treeManagerLocal = GetApzcTreeManager();
3090   if (!treeManagerLocal) {
3091     return;
3092   }
3093 
3094   // Obey overscroll-behavior.
3095   ParentLayerPoint endPoint = aEndPoint;
3096   if (aOverscrollHandoffState.mChainIndex > 0) {
3097     RecursiveMutexAutoLock lock(mRecursiveMutex);
3098     if (!mX.OverscrollBehaviorAllowsHandoff()) {
3099       endPoint.x = aStartPoint.x;
3100     }
3101     if (!mY.OverscrollBehaviorAllowsHandoff()) {
3102       endPoint.y = aStartPoint.y;
3103     }
3104     if (aStartPoint == endPoint) {
3105       // Handoff not allowed in either direction - don't even bother.
3106       return;
3107     }
3108   }
3109 
3110   treeManagerLocal->DispatchScroll(this, aStartPoint, endPoint,
3111                                    aOverscrollHandoffState);
3112 }
3113 
TrackTouch(const MultiTouchInput & aEvent)3114 void AsyncPanZoomController::TrackTouch(const MultiTouchInput& aEvent) {
3115   ParentLayerPoint prevTouchPoint(mX.GetPos(), mY.GetPos());
3116   ParentLayerPoint touchPoint = GetFirstTouchPoint(aEvent);
3117 
3118   ScreenPoint panDistance =
3119       ToScreenCoordinates(ParentLayerPoint(mX.PanDistance(touchPoint.x),
3120                                            mY.PanDistance(touchPoint.y)),
3121                           PanStart());
3122   HandlePanningUpdate(panDistance);
3123 
3124   UpdateWithTouchAtDevicePoint(aEvent);
3125 
3126   if (prevTouchPoint != touchPoint) {
3127     MOZ_ASSERT(GetCurrentTouchBlock());
3128     OverscrollHandoffState handoffState(
3129         *GetCurrentTouchBlock()->GetOverscrollHandoffChain(), panDistance,
3130         ScrollSource::Touch);
3131     CallDispatchScroll(prevTouchPoint, touchPoint, handoffState);
3132   }
3133 }
3134 
GetFirstTouchPoint(const MultiTouchInput & aEvent)3135 ParentLayerPoint AsyncPanZoomController::GetFirstTouchPoint(
3136     const MultiTouchInput& aEvent) {
3137   return ((SingleTouchData&)aEvent.mTouches[0]).mLocalScreenPoint;
3138 }
3139 
StartAnimation(AsyncPanZoomAnimation * aAnimation)3140 void AsyncPanZoomController::StartAnimation(AsyncPanZoomAnimation* aAnimation) {
3141   RecursiveMutexAutoLock lock(mRecursiveMutex);
3142   mAnimation = aAnimation;
3143   mLastSampleTime = GetFrameTime();
3144   ScheduleComposite();
3145 }
3146 
CancelAnimation(CancelAnimationFlags aFlags)3147 void AsyncPanZoomController::CancelAnimation(CancelAnimationFlags aFlags) {
3148   RecursiveMutexAutoLock lock(mRecursiveMutex);
3149   APZC_LOG("%p running CancelAnimation(0x%x) in state %d\n", this, aFlags,
3150            mState);
3151 
3152   if ((aFlags & ExcludeWheel) && mState == WHEEL_SCROLL) {
3153     return;
3154   }
3155 
3156   if (mAnimation) {
3157     mAnimation->Cancel(aFlags);
3158   }
3159 
3160   SetState(NOTHING);
3161   mAnimation = nullptr;
3162   // Since there is no animation in progress now the axes should
3163   // have no velocity either. If we are dropping the velocity from a non-zero
3164   // value we should trigger a repaint as the displayport margins are dependent
3165   // on the velocity and the last repaint request might not have good margins
3166   // any more.
3167   bool repaint = !IsZero(GetVelocityVector());
3168   mX.SetVelocity(0);
3169   mY.SetVelocity(0);
3170   mX.SetAxisLocked(false);
3171   mY.SetAxisLocked(false);
3172   // Setting the state to nothing and cancelling the animation can
3173   // preempt normal mechanisms for relieving overscroll, so we need to clear
3174   // overscroll here.
3175   if (!(aFlags & ExcludeOverscroll) && IsOverscrolled()) {
3176     ClearOverscroll();
3177     repaint = true;
3178   }
3179   // Similar to relieving overscroll, we also need to snap to any snap points
3180   // if appropriate.
3181   if (aFlags & CancelAnimationFlags::ScrollSnap) {
3182     ScrollSnap();
3183   }
3184   if (repaint) {
3185     RequestContentRepaint();
3186     ScheduleComposite();
3187     UpdateSharedCompositorFrameMetrics();
3188   }
3189 }
3190 
ClearOverscroll()3191 void AsyncPanZoomController::ClearOverscroll() {
3192   RecursiveMutexAutoLock lock(mRecursiveMutex);
3193   mX.ClearOverscroll();
3194   mY.ClearOverscroll();
3195 }
3196 
SetCompositorController(CompositorController * aCompositorController)3197 void AsyncPanZoomController::SetCompositorController(
3198     CompositorController* aCompositorController) {
3199   mCompositorController = aCompositorController;
3200 }
3201 
SetMetricsSharingController(MetricsSharingController * aMetricsSharingController)3202 void AsyncPanZoomController::SetMetricsSharingController(
3203     MetricsSharingController* aMetricsSharingController) {
3204   mMetricsSharingController = aMetricsSharingController;
3205 }
3206 
AdjustScrollForSurfaceShift(const ScreenPoint & aShift)3207 void AsyncPanZoomController::AdjustScrollForSurfaceShift(
3208     const ScreenPoint& aShift) {
3209   RecursiveMutexAutoLock lock(mRecursiveMutex);
3210   CSSPoint adjustment =
3211       ViewAs<ParentLayerPixel>(
3212           aShift, PixelCastJustification::ScreenIsParentLayerForRoot) /
3213       mFrameMetrics.GetZoom();
3214   APZC_LOG("%p adjusting scroll position by %s for surface shift\n", this,
3215            Stringify(adjustment).c_str());
3216   CSSRect scrollRange = mFrameMetrics.CalculateScrollRange();
3217   // Apply shift to mFrameMetrics.mScrollOffset.
3218   mFrameMetrics.SetScrollOffset(
3219       scrollRange.ClampPoint(mFrameMetrics.GetScrollOffset() + adjustment));
3220   // Apply shift to mCompositedScrollOffset, since the dynamic toolbar expects
3221   // the shift to take effect right away, without the usual frame delay.
3222   mCompositedScrollOffset =
3223       scrollRange.ClampPoint(mCompositedScrollOffset + adjustment);
3224   RequestContentRepaint();
3225   UpdateSharedCompositorFrameMetrics();
3226 }
3227 
ScrollBy(const CSSPoint & aOffset)3228 void AsyncPanZoomController::ScrollBy(const CSSPoint& aOffset) {
3229   mFrameMetrics.ScrollBy(aOffset);
3230 }
3231 
ScrollByAndClamp(const CSSPoint & aOffset)3232 void AsyncPanZoomController::ScrollByAndClamp(const CSSPoint& aOffset) {
3233   mFrameMetrics.ClampAndSetScrollOffset(mFrameMetrics.GetScrollOffset() +
3234                                         aOffset);
3235 }
3236 
ScaleWithFocus(float aScale,const CSSPoint & aFocus)3237 void AsyncPanZoomController::ScaleWithFocus(float aScale,
3238                                             const CSSPoint& aFocus) {
3239   mFrameMetrics.ZoomBy(aScale);
3240   // We want to adjust the scroll offset such that the CSS point represented by
3241   // aFocus remains at the same position on the screen before and after the
3242   // change in zoom. The below code accomplishes this; see
3243   // https://bugzilla.mozilla.org/show_bug.cgi?id=923431#c6 for an in-depth
3244   // explanation of how.
3245   mFrameMetrics.SetScrollOffset((mFrameMetrics.GetScrollOffset() + aFocus) -
3246                                 (aFocus / aScale));
3247 }
3248 
3249 /**
3250  * Enlarges the displayport along both axes based on the velocity.
3251  */
CalculateDisplayPortSize(const CSSSize & aCompositionSize,const CSSPoint & aVelocity)3252 static CSSSize CalculateDisplayPortSize(const CSSSize& aCompositionSize,
3253                                         const CSSPoint& aVelocity) {
3254   bool xIsStationarySpeed = fabsf(aVelocity.x) < gfxPrefs::APZMinSkateSpeed();
3255   bool yIsStationarySpeed = fabsf(aVelocity.y) < gfxPrefs::APZMinSkateSpeed();
3256   float xMultiplier = xIsStationarySpeed
3257                           ? gfxPrefs::APZXStationarySizeMultiplier()
3258                           : gfxPrefs::APZXSkateSizeMultiplier();
3259   float yMultiplier = yIsStationarySpeed
3260                           ? gfxPrefs::APZYStationarySizeMultiplier()
3261                           : gfxPrefs::APZYSkateSizeMultiplier();
3262 
3263   if (IsHighMemSystem() && !xIsStationarySpeed) {
3264     xMultiplier += gfxPrefs::APZXSkateHighMemAdjust();
3265   }
3266 
3267   if (IsHighMemSystem() && !yIsStationarySpeed) {
3268     yMultiplier += gfxPrefs::APZYSkateHighMemAdjust();
3269   }
3270 
3271   return aCompositionSize * CSSSize(xMultiplier, yMultiplier);
3272 }
3273 
3274 /**
3275  * Ensures that the displayport is at least as large as the visible area
3276  * inflated by the danger zone. If this is not the case then the
3277  * "AboutToCheckerboard" function in TiledContentClient.cpp will return true
3278  * even in the stable state.
3279  */
ExpandDisplayPortToDangerZone(const CSSSize & aDisplayPortSize,const FrameMetrics & aFrameMetrics)3280 static CSSSize ExpandDisplayPortToDangerZone(
3281     const CSSSize& aDisplayPortSize, const FrameMetrics& aFrameMetrics) {
3282   CSSSize dangerZone(0.0f, 0.0f);
3283   if (aFrameMetrics.LayersPixelsPerCSSPixel().xScale != 0 &&
3284       aFrameMetrics.LayersPixelsPerCSSPixel().yScale != 0) {
3285     dangerZone =
3286         LayerSize(gfxPrefs::APZDangerZoneX(), gfxPrefs::APZDangerZoneY()) /
3287         aFrameMetrics.LayersPixelsPerCSSPixel();
3288   }
3289   const CSSSize compositionSize =
3290       aFrameMetrics.CalculateBoundedCompositedSizeInCssPixels();
3291 
3292   const float xSize = std::max(aDisplayPortSize.width,
3293                                compositionSize.width + (2 * dangerZone.width));
3294 
3295   const float ySize =
3296       std::max(aDisplayPortSize.height,
3297                compositionSize.height + (2 * dangerZone.height));
3298 
3299   return CSSSize(xSize, ySize);
3300 }
3301 
3302 /**
3303  * Attempts to redistribute any area in the displayport that would get clipped
3304  * by the scrollable rect, or be inaccessible due to disabled scrolling, to the
3305  * other axis, while maintaining total displayport area.
3306  */
RedistributeDisplayPortExcess(CSSSize & aDisplayPortSize,const CSSRect & aScrollableRect)3307 static void RedistributeDisplayPortExcess(CSSSize& aDisplayPortSize,
3308                                           const CSSRect& aScrollableRect) {
3309   // As aDisplayPortSize.height * aDisplayPortSize.width does not change,
3310   // we are just scaling by the ratio and its inverse.
3311   if (aDisplayPortSize.height > aScrollableRect.Height()) {
3312     aDisplayPortSize.width *=
3313         (aDisplayPortSize.height / aScrollableRect.Height());
3314     aDisplayPortSize.height = aScrollableRect.Height();
3315   } else if (aDisplayPortSize.width > aScrollableRect.Width()) {
3316     aDisplayPortSize.height *=
3317         (aDisplayPortSize.width / aScrollableRect.Width());
3318     aDisplayPortSize.width = aScrollableRect.Width();
3319   }
3320 }
3321 
3322 /* static */
CalculatePendingDisplayPort(const FrameMetrics & aFrameMetrics,const ParentLayerPoint & aVelocity)3323 const ScreenMargin AsyncPanZoomController::CalculatePendingDisplayPort(
3324     const FrameMetrics& aFrameMetrics, const ParentLayerPoint& aVelocity) {
3325   if (aFrameMetrics.IsScrollInfoLayer()) {
3326     // Don't compute margins. Since we can't asynchronously scroll this frame,
3327     // we don't want to paint anything more than the composition bounds.
3328     return ScreenMargin();
3329   }
3330 
3331   CSSSize compositionSize =
3332       aFrameMetrics.CalculateBoundedCompositedSizeInCssPixels();
3333   CSSPoint velocity;
3334   if (aFrameMetrics.GetZoom() != CSSToParentLayerScale2D(0, 0)) {
3335     velocity = aVelocity / aFrameMetrics.GetZoom();  // avoid division by zero
3336   }
3337   CSSRect scrollableRect = aFrameMetrics.GetExpandedScrollableRect();
3338 
3339   // Calculate the displayport size based on how fast we're moving along each
3340   // axis.
3341   CSSSize displayPortSize = CalculateDisplayPortSize(compositionSize, velocity);
3342 
3343   displayPortSize =
3344       ExpandDisplayPortToDangerZone(displayPortSize, aFrameMetrics);
3345 
3346   if (gfxPrefs::APZEnlargeDisplayPortWhenClipped()) {
3347     RedistributeDisplayPortExcess(displayPortSize, scrollableRect);
3348   }
3349 
3350   // We calculate a "displayport" here which is relative to the scroll offset.
3351   // Note that the scroll offset we have here in the APZ code may not be the
3352   // same as the base rect that gets used on the layout side when the
3353   // displayport margins are actually applied, so it is important to only
3354   // consider the displayport as margins relative to a scroll offset rather than
3355   // relative to something more unchanging like the scrollable rect origin.
3356 
3357   // Center the displayport based on its expansion over the composition size.
3358   CSSRect displayPort((compositionSize.width - displayPortSize.width) / 2.0f,
3359                       (compositionSize.height - displayPortSize.height) / 2.0f,
3360                       displayPortSize.width, displayPortSize.height);
3361 
3362   // Offset the displayport, depending on how fast we're moving and the
3363   // estimated time it takes to paint, to try to minimise checkerboarding.
3364   float paintFactor = kDefaultEstimatedPaintDurationMs;
3365   displayPort.MoveBy(velocity * paintFactor * gfxPrefs::APZVelocityBias());
3366 
3367   APZC_LOG_FM(aFrameMetrics,
3368               "Calculated displayport as (%f %f %f %f) from velocity %s paint "
3369               "time %f metrics",
3370               displayPort.x, displayPort.y, displayPort.Width(),
3371               displayPort.Height(), ToString(aVelocity).c_str(), paintFactor);
3372 
3373   CSSMargin cssMargins;
3374   cssMargins.left = -displayPort.X();
3375   cssMargins.top = -displayPort.Y();
3376   cssMargins.right =
3377       displayPort.Width() - compositionSize.width - cssMargins.left;
3378   cssMargins.bottom =
3379       displayPort.Height() - compositionSize.height - cssMargins.top;
3380 
3381   return cssMargins * aFrameMetrics.DisplayportPixelsPerCSSPixel();
3382 }
3383 
ScheduleComposite()3384 void AsyncPanZoomController::ScheduleComposite() {
3385   if (mCompositorController) {
3386     mCompositorController->ScheduleRenderOnCompositorThread();
3387   }
3388 }
3389 
ScheduleCompositeAndMaybeRepaint()3390 void AsyncPanZoomController::ScheduleCompositeAndMaybeRepaint() {
3391   ScheduleComposite();
3392   RequestContentRepaint();
3393 }
3394 
FlushRepaintForOverscrollHandoff()3395 void AsyncPanZoomController::FlushRepaintForOverscrollHandoff() {
3396   RecursiveMutexAutoLock lock(mRecursiveMutex);
3397   RequestContentRepaint();
3398   UpdateSharedCompositorFrameMetrics();
3399 }
3400 
FlushRepaintForNewInputBlock()3401 void AsyncPanZoomController::FlushRepaintForNewInputBlock() {
3402   APZC_LOG("%p flushing repaint for new input block\n", this);
3403 
3404   RecursiveMutexAutoLock lock(mRecursiveMutex);
3405   RequestContentRepaint();
3406   UpdateSharedCompositorFrameMetrics();
3407 }
3408 
SnapBackIfOverscrolled()3409 bool AsyncPanZoomController::SnapBackIfOverscrolled() {
3410   RecursiveMutexAutoLock lock(mRecursiveMutex);
3411   // It's possible that we're already in the middle of an overscroll
3412   // animation - if so, don't start a new one.
3413   if (IsOverscrolled() && mState != OVERSCROLL_ANIMATION) {
3414     APZC_LOG("%p is overscrolled, starting snap-back\n", this);
3415     StartOverscrollAnimation(ParentLayerPoint(0, 0));
3416     return true;
3417   }
3418   // If we don't kick off an overscroll animation, we still need to ask the
3419   // main thread to snap to any nearby snap points, assuming we haven't already
3420   // done so when we started this fling
3421   if (mState != FLING) {
3422     ScrollSnap();
3423   }
3424   return false;
3425 }
3426 
IsFlingingFast() const3427 bool AsyncPanZoomController::IsFlingingFast() const {
3428   RecursiveMutexAutoLock lock(mRecursiveMutex);
3429   if (mState == FLING &&
3430       GetVelocityVector().Length() > gfxPrefs::APZFlingStopOnTapThreshold()) {
3431     APZC_LOG("%p is moving fast\n", this);
3432     return true;
3433   }
3434   return false;
3435 }
3436 
IsPannable() const3437 bool AsyncPanZoomController::IsPannable() const {
3438   RecursiveMutexAutoLock lock(mRecursiveMutex);
3439   return mX.CanScroll() || mY.CanScroll();
3440 }
3441 
IsScrollInfoLayer() const3442 bool AsyncPanZoomController::IsScrollInfoLayer() const {
3443   RecursiveMutexAutoLock lock(mRecursiveMutex);
3444   return mFrameMetrics.IsScrollInfoLayer();
3445 }
3446 
GetLastTouchIdentifier() const3447 int32_t AsyncPanZoomController::GetLastTouchIdentifier() const {
3448   RefPtr<GestureEventListener> listener = GetGestureEventListener();
3449   return listener ? listener->GetLastTouchIdentifier() : -1;
3450 }
3451 
RequestContentRepaint(bool aUserAction)3452 void AsyncPanZoomController::RequestContentRepaint(bool aUserAction) {
3453   // Reinvoke this method on the repaint thread if it's not there already. It's
3454   // important to do this before the call to CalculatePendingDisplayPort, so
3455   // that CalculatePendingDisplayPort uses the most recent available version of
3456   // mFrameMetrics, just before the paint request is dispatched to content.
3457   RefPtr<GeckoContentController> controller = GetGeckoContentController();
3458   if (!controller) {
3459     return;
3460   }
3461   if (!controller->IsRepaintThread()) {
3462     // use the local variable to resolve the function overload.
3463     auto func = static_cast<void (AsyncPanZoomController::*)(bool)>(
3464         &AsyncPanZoomController::RequestContentRepaint);
3465     controller->DispatchToRepaintThread(NewRunnableMethod<bool>(
3466         "layers::AsyncPanZoomController::RequestContentRepaint", this, func,
3467         aUserAction));
3468     return;
3469   }
3470 
3471   MOZ_ASSERT(controller->IsRepaintThread());
3472 
3473   RecursiveMutexAutoLock lock(mRecursiveMutex);
3474   ParentLayerPoint velocity = GetVelocityVector();
3475   mFrameMetrics.SetDisplayPortMargins(
3476       CalculatePendingDisplayPort(mFrameMetrics, velocity));
3477   mFrameMetrics.SetUseDisplayPortMargins(true);
3478   mFrameMetrics.SetPaintRequestTime(TimeStamp::Now());
3479   mFrameMetrics.SetRepaintDrivenByUserAction(aUserAction);
3480   RequestContentRepaint(mFrameMetrics, velocity);
3481 }
3482 
GetDisplayPortRect(const FrameMetrics & aFrameMetrics)3483 /*static*/ CSSRect GetDisplayPortRect(const FrameMetrics& aFrameMetrics) {
3484   // This computation is based on what happens in CalculatePendingDisplayPort.
3485   // If that changes then this might need to change too
3486   CSSRect baseRect(aFrameMetrics.GetScrollOffset(),
3487                    aFrameMetrics.CalculateBoundedCompositedSizeInCssPixels());
3488   baseRect.Inflate(aFrameMetrics.GetDisplayPortMargins() /
3489                    aFrameMetrics.DisplayportPixelsPerCSSPixel());
3490   return baseRect;
3491 }
3492 
RequestContentRepaint(const FrameMetrics & aFrameMetrics,const ParentLayerPoint & aVelocity)3493 void AsyncPanZoomController::RequestContentRepaint(
3494     const FrameMetrics& aFrameMetrics, const ParentLayerPoint& aVelocity) {
3495   RefPtr<GeckoContentController> controller = GetGeckoContentController();
3496   if (!controller) {
3497     return;
3498   }
3499   MOZ_ASSERT(controller->IsRepaintThread());
3500 
3501   // If we're trying to paint what we already think is painted, discard this
3502   // request since it's a pointless paint.
3503   ScreenMargin marginDelta = (mLastPaintRequestMetrics.GetDisplayPortMargins() -
3504                               aFrameMetrics.GetDisplayPortMargins());
3505   if (fabsf(marginDelta.left) < EPSILON && fabsf(marginDelta.top) < EPSILON &&
3506       fabsf(marginDelta.right) < EPSILON &&
3507       fabsf(marginDelta.bottom) < EPSILON &&
3508       fabsf(mLastPaintRequestMetrics.GetScrollOffset().x -
3509             aFrameMetrics.GetScrollOffset().x) < EPSILON &&
3510       fabsf(mLastPaintRequestMetrics.GetScrollOffset().y -
3511             aFrameMetrics.GetScrollOffset().y) < EPSILON &&
3512       aFrameMetrics.GetPresShellResolution() ==
3513           mLastPaintRequestMetrics.GetPresShellResolution() &&
3514       aFrameMetrics.GetZoom() == mLastPaintRequestMetrics.GetZoom() &&
3515       fabsf(aFrameMetrics.GetViewport().Width() -
3516             mLastPaintRequestMetrics.GetViewport().Width()) < EPSILON &&
3517       fabsf(aFrameMetrics.GetViewport().Height() -
3518             mLastPaintRequestMetrics.GetViewport().Height()) < EPSILON &&
3519       aFrameMetrics.GetScrollGeneration() ==
3520           mLastPaintRequestMetrics.GetScrollGeneration() &&
3521       aFrameMetrics.GetScrollUpdateType() ==
3522           mLastPaintRequestMetrics.GetScrollUpdateType()) {
3523     return;
3524   }
3525 
3526   APZC_LOG_FM(aFrameMetrics, "%p requesting content repaint", this);
3527   {  // scope lock
3528     MutexAutoLock lock(mCheckerboardEventLock);
3529     if (mCheckerboardEvent && mCheckerboardEvent->IsRecordingTrace()) {
3530       std::stringstream info;
3531       info << " velocity " << aVelocity;
3532       std::string str = info.str();
3533       mCheckerboardEvent->UpdateRendertraceProperty(
3534           CheckerboardEvent::RequestedDisplayPort,
3535           GetDisplayPortRect(aFrameMetrics), str);
3536     }
3537   }
3538 
3539   MOZ_ASSERT(aFrameMetrics.GetScrollUpdateType() == FrameMetrics::eNone ||
3540              aFrameMetrics.GetScrollUpdateType() == FrameMetrics::eUserAction);
3541   controller->RequestContentRepaint(aFrameMetrics);
3542   mExpectedGeckoMetrics = aFrameMetrics;
3543   mLastPaintRequestMetrics = aFrameMetrics;
3544 }
3545 
UpdateAnimation(const TimeStamp & aSampleTime,nsTArray<RefPtr<Runnable>> * aOutDeferredTasks)3546 bool AsyncPanZoomController::UpdateAnimation(
3547     const TimeStamp& aSampleTime,
3548     nsTArray<RefPtr<Runnable>>* aOutDeferredTasks) {
3549   APZThreadUtils::AssertOnSamplerThread();
3550 
3551   // This function may get called multiple with the same sample time, because
3552   // there may be multiple layers with this APZC, and each layer invokes this
3553   // function during composition. However we only want to do one animation step
3554   // per composition so we need to deduplicate these calls first.
3555   if (mLastSampleTime == aSampleTime) {
3556     return false;
3557   }
3558 
3559   // Sample the composited async transform once per composite. Note that we
3560   // call this after the |mLastSampleTime == aSampleTime| check, to ensure
3561   // it's only called once per APZC on each composite.
3562   bool needComposite = SampleCompositedAsyncTransform();
3563 
3564   TimeDuration sampleTimeDelta = aSampleTime - mLastSampleTime;
3565   mLastSampleTime = aSampleTime;
3566 
3567   if (mAnimation) {
3568     bool continueAnimation = mAnimation->Sample(mFrameMetrics, sampleTimeDelta);
3569     bool wantsRepaints = mAnimation->WantsRepaints();
3570     *aOutDeferredTasks = mAnimation->TakeDeferredTasks();
3571     if (!continueAnimation) {
3572       mAnimation = nullptr;
3573       SetState(NOTHING);
3574     }
3575     // Request a repaint at the end of the animation in case something such as a
3576     // call to NotifyLayersUpdated was invoked during the animation and Gecko's
3577     // current state is some intermediate point of the animation.
3578     if (!continueAnimation || wantsRepaints) {
3579       RequestContentRepaint();
3580     }
3581     UpdateSharedCompositorFrameMetrics();
3582     needComposite = true;
3583   }
3584   return needComposite;
3585 }
3586 
GetOverscrollTransform(AsyncTransformConsumer aMode) const3587 AsyncTransformComponentMatrix AsyncPanZoomController::GetOverscrollTransform(
3588     AsyncTransformConsumer aMode) const {
3589   RecursiveMutexAutoLock lock(mRecursiveMutex);
3590 
3591   if (aMode == eForCompositing && mScrollMetadata.IsApzForceDisabled()) {
3592     return AsyncTransformComponentMatrix();
3593   }
3594 
3595   if (!IsOverscrolled()) {
3596     return AsyncTransformComponentMatrix();
3597   }
3598 
3599   // The overscroll effect is a simple translation by the overscroll offset.
3600   ParentLayerPoint overscrollOffset(-mX.GetOverscroll(), -mY.GetOverscroll());
3601   return AsyncTransformComponentMatrix().PostTranslate(overscrollOffset.x,
3602                                                        overscrollOffset.y, 0);
3603 }
3604 
AdvanceAnimations(const TimeStamp & aSampleTime)3605 bool AsyncPanZoomController::AdvanceAnimations(const TimeStamp& aSampleTime) {
3606   APZThreadUtils::AssertOnSamplerThread();
3607 
3608   // Don't send any state-change notifications until the end of the function,
3609   // because we may go through some intermediate states while we finish
3610   // animations and start new ones.
3611   StateChangeNotificationBlocker blocker(this);
3612 
3613   // The eventual return value of this function. The compositor needs to know
3614   // whether or not to advance by a frame as soon as it can. For example, if a
3615   // fling is happening, it has to keep compositing so that the animation is
3616   // smooth. If an animation frame is requested, it is the compositor's
3617   // responsibility to schedule a composite.
3618   mAsyncTransformAppliedToContent = false;
3619   bool requestAnimationFrame = false;
3620   nsTArray<RefPtr<Runnable>> deferredTasks;
3621 
3622   {
3623     RecursiveMutexAutoLock lock(mRecursiveMutex);
3624 
3625     requestAnimationFrame = UpdateAnimation(aSampleTime, &deferredTasks);
3626 
3627     {  // scope lock
3628       MutexAutoLock lock(mCheckerboardEventLock);
3629       if (mCheckerboardEvent) {
3630         mCheckerboardEvent->UpdateRendertraceProperty(
3631             CheckerboardEvent::UserVisible,
3632             CSSRect(mFrameMetrics.GetScrollOffset(),
3633                     mFrameMetrics.CalculateCompositedSizeInCssPixels()));
3634       }
3635     }
3636   }
3637 
3638   // Execute any deferred tasks queued up by mAnimation's Sample() (called by
3639   // UpdateAnimation()). This needs to be done after the monitor is released
3640   // since the tasks are allowed to call APZCTreeManager methods which can grab
3641   // the tree lock.
3642   for (uint32_t i = 0; i < deferredTasks.Length(); ++i) {
3643     deferredTasks[i]->Run();
3644     deferredTasks[i] = nullptr;
3645   }
3646 
3647   // One of the deferred tasks may have started a new animation. In this case,
3648   // we want to ask the compositor to schedule a new composite.
3649   requestAnimationFrame |= (mAnimation != nullptr);
3650 
3651   return requestAnimationFrame;
3652 }
3653 
GetCurrentAsyncScrollOffset(AsyncTransformConsumer aMode) const3654 ParentLayerPoint AsyncPanZoomController::GetCurrentAsyncScrollOffset(
3655     AsyncTransformConsumer aMode) const {
3656   RecursiveMutexAutoLock lock(mRecursiveMutex);
3657 
3658   if (aMode == eForCompositing && mScrollMetadata.IsApzForceDisabled()) {
3659     return mLastContentPaintMetrics.GetScrollOffset() *
3660            mLastContentPaintMetrics.GetZoom();
3661   }
3662 
3663   return (GetEffectiveScrollOffset(aMode) + mTestAsyncScrollOffset) *
3664          GetEffectiveZoom(aMode) * mTestAsyncZoom.scale;
3665 }
3666 
GetCurrentAsyncScrollOffsetInCssPixels(AsyncTransformConsumer aMode) const3667 CSSPoint AsyncPanZoomController::GetCurrentAsyncScrollOffsetInCssPixels(
3668     AsyncTransformConsumer aMode) const {
3669   RecursiveMutexAutoLock lock(mRecursiveMutex);
3670 
3671   if (aMode == eForCompositing && mScrollMetadata.IsApzForceDisabled()) {
3672     return mLastContentPaintMetrics.GetScrollOffset();
3673   }
3674 
3675   return GetEffectiveScrollOffset(aMode) + mTestAsyncScrollOffset;
3676 }
3677 
GetCurrentAsyncTransform(AsyncTransformConsumer aMode) const3678 AsyncTransform AsyncPanZoomController::GetCurrentAsyncTransform(
3679     AsyncTransformConsumer aMode) const {
3680   RecursiveMutexAutoLock lock(mRecursiveMutex);
3681 
3682   if (aMode == eForCompositing && mScrollMetadata.IsApzForceDisabled()) {
3683     return AsyncTransform();
3684   }
3685 
3686   CSSPoint lastPaintScrollOffset;
3687   if (mLastContentPaintMetrics.IsScrollable()) {
3688     lastPaintScrollOffset = mLastContentPaintMetrics.GetScrollOffset();
3689   }
3690 
3691   CSSPoint currentScrollOffset =
3692       GetEffectiveScrollOffset(aMode) + mTestAsyncScrollOffset;
3693 
3694   // If checkerboarding has been disallowed, clamp the scroll position to stay
3695   // within rendered content.
3696   if (!gfxPrefs::APZAllowCheckerboarding() &&
3697       !mLastContentPaintMetrics.GetDisplayPort().IsEmpty()) {
3698     CSSSize compositedSize =
3699         mLastContentPaintMetrics.CalculateCompositedSizeInCssPixels();
3700     CSSPoint maxScrollOffset =
3701         lastPaintScrollOffset +
3702         CSSPoint(mLastContentPaintMetrics.GetDisplayPort().XMost() -
3703                      compositedSize.width,
3704                  mLastContentPaintMetrics.GetDisplayPort().YMost() -
3705                      compositedSize.height);
3706     CSSPoint minScrollOffset =
3707         lastPaintScrollOffset +
3708         mLastContentPaintMetrics.GetDisplayPort().TopLeft();
3709 
3710     if (minScrollOffset.x < maxScrollOffset.x) {
3711       currentScrollOffset.x =
3712           clamped(currentScrollOffset.x, minScrollOffset.x, maxScrollOffset.x);
3713     }
3714     if (minScrollOffset.y < maxScrollOffset.y) {
3715       currentScrollOffset.y =
3716           clamped(currentScrollOffset.y, minScrollOffset.y, maxScrollOffset.y);
3717     }
3718   }
3719 
3720   CSSToParentLayerScale2D effectiveZoom = GetEffectiveZoom(aMode);
3721 
3722   ParentLayerPoint translation = (currentScrollOffset - lastPaintScrollOffset) *
3723                                  effectiveZoom * mTestAsyncZoom.scale;
3724 
3725   LayerToParentLayerScale compositedAsyncZoom =
3726       (effectiveZoom / mFrameMetrics.LayersPixelsPerCSSPixel()).ToScaleFactor();
3727   return AsyncTransform(
3728       LayerToParentLayerScale(compositedAsyncZoom.scale * mTestAsyncZoom.scale),
3729       -translation);
3730 }
3731 
GetEffectiveScrollOffset(AsyncTransformConsumer aMode) const3732 CSSPoint AsyncPanZoomController::GetEffectiveScrollOffset(
3733     AsyncTransformConsumer aMode) const {
3734   if (gfxPrefs::APZFrameDelayEnabled() && aMode == eForCompositing) {
3735     return mCompositedScrollOffset;
3736   }
3737   return mFrameMetrics.GetScrollOffset();
3738 }
3739 
GetEffectiveZoom(AsyncTransformConsumer aMode) const3740 CSSToParentLayerScale2D AsyncPanZoomController::GetEffectiveZoom(
3741     AsyncTransformConsumer aMode) const {
3742   if (gfxPrefs::APZFrameDelayEnabled() && aMode == eForCompositing) {
3743     return mCompositedZoom;
3744   }
3745   return mFrameMetrics.GetZoom();
3746 }
3747 
SampleCompositedAsyncTransform()3748 bool AsyncPanZoomController::SampleCompositedAsyncTransform() {
3749   RecursiveMutexAutoLock lock(mRecursiveMutex);
3750   if (mCompositedScrollOffset != mFrameMetrics.GetScrollOffset() ||
3751       mCompositedZoom != mFrameMetrics.GetZoom()) {
3752     mCompositedScrollOffset = mFrameMetrics.GetScrollOffset();
3753     mCompositedZoom = mFrameMetrics.GetZoom();
3754     return true;
3755   }
3756   return false;
3757 }
3758 
3759 AsyncTransformComponentMatrix
GetCurrentAsyncTransformWithOverscroll(AsyncTransformConsumer aMode) const3760 AsyncPanZoomController::GetCurrentAsyncTransformWithOverscroll(
3761     AsyncTransformConsumer aMode) const {
3762   return AsyncTransformComponentMatrix(GetCurrentAsyncTransform(aMode)) *
3763          GetOverscrollTransform(aMode);
3764 }
3765 
GetTransformToLastDispatchedPaint() const3766 Matrix4x4 AsyncPanZoomController::GetTransformToLastDispatchedPaint() const {
3767   RecursiveMutexAutoLock lock(mRecursiveMutex);
3768 
3769   LayerPoint scrollChange = (mLastContentPaintMetrics.GetScrollOffset() -
3770                              mExpectedGeckoMetrics.GetScrollOffset()) *
3771                             mLastContentPaintMetrics.GetDevPixelsPerCSSPixel() *
3772                             mLastContentPaintMetrics.GetCumulativeResolution();
3773 
3774   // We're interested in the async zoom change. Factor out the content scale
3775   // that may change when dragging the window to a monitor with a different
3776   // content scale.
3777   LayoutDeviceToParentLayerScale2D lastContentZoom =
3778       mLastContentPaintMetrics.GetZoom() /
3779       mLastContentPaintMetrics.GetDevPixelsPerCSSPixel();
3780   LayoutDeviceToParentLayerScale2D lastDispatchedZoom =
3781       mExpectedGeckoMetrics.GetZoom() /
3782       mExpectedGeckoMetrics.GetDevPixelsPerCSSPixel();
3783   gfxSize zoomChange = lastContentZoom / lastDispatchedZoom;
3784 
3785   return Matrix4x4::Translation(scrollChange.x, scrollChange.y, 0)
3786       .PostScale(zoomChange.width, zoomChange.height, 1);
3787 }
3788 
GetCheckerboardMagnitude() const3789 uint32_t AsyncPanZoomController::GetCheckerboardMagnitude() const {
3790   RecursiveMutexAutoLock lock(mRecursiveMutex);
3791 
3792   CSSPoint currentScrollOffset =
3793       mFrameMetrics.GetScrollOffset() + mTestAsyncScrollOffset;
3794   CSSRect painted = mLastContentPaintMetrics.GetDisplayPort() +
3795                     mLastContentPaintMetrics.GetScrollOffset();
3796   CSSRect visible = CSSRect(currentScrollOffset,
3797                             mFrameMetrics.CalculateCompositedSizeInCssPixels());
3798 
3799   CSSIntRegion checkerboard;
3800   // Round so as to minimize checkerboarding; if we're only showing fractional
3801   // pixels of checkerboarding it's not really worth counting
3802   checkerboard.Sub(RoundedIn(visible), RoundedOut(painted));
3803   return checkerboard.Area();
3804 }
3805 
ReportCheckerboard(const TimeStamp & aSampleTime)3806 void AsyncPanZoomController::ReportCheckerboard(const TimeStamp& aSampleTime) {
3807   if (mLastCheckerboardReport == aSampleTime) {
3808     // This function will get called multiple times for each APZC on a single
3809     // composite (once for each layer it is attached to). Only report the
3810     // checkerboard once per composite though.
3811     return;
3812   }
3813   mLastCheckerboardReport = aSampleTime;
3814 
3815   bool recordTrace = gfxPrefs::APZRecordCheckerboarding();
3816   bool forTelemetry = Telemetry::CanRecordExtended();
3817   uint32_t magnitude = GetCheckerboardMagnitude();
3818 
3819   MutexAutoLock lock(mCheckerboardEventLock);
3820   if (!mCheckerboardEvent && (recordTrace || forTelemetry)) {
3821     mCheckerboardEvent = MakeUnique<CheckerboardEvent>(recordTrace);
3822   }
3823   mPotentialCheckerboardTracker.InTransform(IsTransformingState(mState));
3824   if (magnitude) {
3825     mPotentialCheckerboardTracker.CheckerboardSeen();
3826   }
3827   UpdateCheckerboardEvent(lock, magnitude);
3828 }
3829 
UpdateCheckerboardEvent(const MutexAutoLock & aProofOfLock,uint32_t aMagnitude)3830 void AsyncPanZoomController::UpdateCheckerboardEvent(
3831     const MutexAutoLock& aProofOfLock, uint32_t aMagnitude) {
3832   if (mCheckerboardEvent && mCheckerboardEvent->RecordFrameInfo(aMagnitude)) {
3833     // This checkerboard event is done. Report some metrics to telemetry.
3834     mozilla::Telemetry::Accumulate(mozilla::Telemetry::CHECKERBOARD_SEVERITY,
3835                                    mCheckerboardEvent->GetSeverity());
3836     mozilla::Telemetry::Accumulate(mozilla::Telemetry::CHECKERBOARD_PEAK,
3837                                    mCheckerboardEvent->GetPeak());
3838     mozilla::Telemetry::Accumulate(
3839         mozilla::Telemetry::CHECKERBOARD_DURATION,
3840         (uint32_t)mCheckerboardEvent->GetDuration().ToMilliseconds());
3841 
3842     mPotentialCheckerboardTracker.CheckerboardDone();
3843 
3844     if (gfxPrefs::APZRecordCheckerboarding()) {
3845       // if the pref is enabled, also send it to the storage class. it may be
3846       // chosen for public display on about:checkerboard, the hall of fame for
3847       // checkerboard events.
3848       uint32_t severity = mCheckerboardEvent->GetSeverity();
3849       std::string log = mCheckerboardEvent->GetLog();
3850       CheckerboardEventStorage::Report(severity, log);
3851     }
3852     mCheckerboardEvent = nullptr;
3853   }
3854 }
3855 
FlushActiveCheckerboardReport()3856 void AsyncPanZoomController::FlushActiveCheckerboardReport() {
3857   MutexAutoLock lock(mCheckerboardEventLock);
3858   // Pretend like we got a frame with 0 pixels checkerboarded. This will
3859   // terminate the checkerboard event and flush it out
3860   UpdateCheckerboardEvent(lock, 0);
3861 }
3862 
IsCurrentlyCheckerboarding() const3863 bool AsyncPanZoomController::IsCurrentlyCheckerboarding() const {
3864   RecursiveMutexAutoLock lock(mRecursiveMutex);
3865 
3866   if (!gfxPrefs::APZAllowCheckerboarding() ||
3867       mScrollMetadata.IsApzForceDisabled()) {
3868     return false;
3869   }
3870 
3871   CSSPoint currentScrollOffset =
3872       mFrameMetrics.GetScrollOffset() + mTestAsyncScrollOffset;
3873   CSSRect painted = mLastContentPaintMetrics.GetDisplayPort() +
3874                     mLastContentPaintMetrics.GetScrollOffset();
3875   painted.Inflate(CSSMargin::FromAppUnits(
3876       nsMargin(1, 1, 1, 1)));  // fuzz for rounding error
3877   CSSRect visible = CSSRect(currentScrollOffset,
3878                             mFrameMetrics.CalculateCompositedSizeInCssPixels());
3879   if (painted.Contains(visible)) {
3880     return false;
3881   }
3882   APZC_LOG_FM(mFrameMetrics,
3883               "%p is currently checkerboarding (painted %s visble %s)", this,
3884               Stringify(painted).c_str(), Stringify(visible).c_str());
3885   return true;
3886 }
3887 
NotifyLayersUpdated(const ScrollMetadata & aScrollMetadata,bool aIsFirstPaint,bool aThisLayerTreeUpdated)3888 void AsyncPanZoomController::NotifyLayersUpdated(
3889     const ScrollMetadata& aScrollMetadata, bool aIsFirstPaint,
3890     bool aThisLayerTreeUpdated) {
3891   APZThreadUtils::AssertOnSamplerThread();
3892 
3893   RecursiveMutexAutoLock lock(mRecursiveMutex);
3894   bool isDefault = mScrollMetadata.IsDefault();
3895 
3896   const FrameMetrics& aLayerMetrics = aScrollMetadata.GetMetrics();
3897 
3898   if ((aScrollMetadata == mLastContentPaintMetadata) && !isDefault) {
3899     // No new information here, skip it.
3900     APZC_LOG("%p NotifyLayersUpdated short-circuit\n", this);
3901     return;
3902   }
3903 
3904   // If the mFrameMetrics scroll offset is different from the last scroll offset
3905   // that the main-thread sent us, then we know that the user has been doing
3906   // something that triggers a scroll. This check is the APZ equivalent of the
3907   // check on the main-thread at
3908   // https://hg.mozilla.org/mozilla-central/file/97a52326b06a/layout/generic/nsGfxScrollFrame.cpp#l4050
3909   // There is code below (the use site of userScrolled) that prevents a
3910   // restored- scroll-position update from overwriting a user scroll, again
3911   // equivalent to how the main thread code does the same thing.
3912   CSSPoint lastScrollOffset =
3913       mLastContentPaintMetadata.GetMetrics().GetScrollOffset();
3914   bool userScrolled = !FuzzyEqualsAdditive(mFrameMetrics.GetScrollOffset().x,
3915                                            lastScrollOffset.x) ||
3916                       !FuzzyEqualsAdditive(mFrameMetrics.GetScrollOffset().y,
3917                                            lastScrollOffset.y);
3918 
3919   if (aLayerMetrics.GetScrollUpdateType() !=
3920       FrameMetrics::ScrollOffsetUpdateType::ePending) {
3921     mLastContentPaintMetadata = aScrollMetadata;
3922   }
3923 
3924   mScrollMetadata.SetScrollParentId(aScrollMetadata.GetScrollParentId());
3925   APZC_LOG_FM(aLayerMetrics,
3926               "%p got a NotifyLayersUpdated with aIsFirstPaint=%d, "
3927               "aThisLayerTreeUpdated=%d",
3928               this, aIsFirstPaint, aThisLayerTreeUpdated);
3929 
3930   {  // scope lock
3931     MutexAutoLock lock(mCheckerboardEventLock);
3932     if (mCheckerboardEvent && mCheckerboardEvent->IsRecordingTrace()) {
3933       std::string str;
3934       if (aThisLayerTreeUpdated) {
3935         if (!aLayerMetrics.GetPaintRequestTime().IsNull()) {
3936           // Note that we might get the paint request time as non-null, but with
3937           // aThisLayerTreeUpdated false. That can happen if we get a layer
3938           // transaction from a different process right after we get the layer
3939           // transaction with aThisLayerTreeUpdated == true. In this case we
3940           // want to ignore the paint request time because it was already dumped
3941           // in the previous layer transaction.
3942           TimeDuration paintTime =
3943               TimeStamp::Now() - aLayerMetrics.GetPaintRequestTime();
3944           std::stringstream info;
3945           info << " painttime " << paintTime.ToMilliseconds();
3946           str = info.str();
3947         } else {
3948           // This might be indicative of a wasted paint particularly if it
3949           // happens during a checkerboard event.
3950           str = " (this layertree updated)";
3951         }
3952       }
3953       mCheckerboardEvent->UpdateRendertraceProperty(
3954           CheckerboardEvent::Page, aLayerMetrics.GetScrollableRect());
3955       mCheckerboardEvent->UpdateRendertraceProperty(
3956           CheckerboardEvent::PaintedDisplayPort,
3957           aLayerMetrics.GetDisplayPort() + aLayerMetrics.GetScrollOffset(),
3958           str);
3959       if (!aLayerMetrics.GetCriticalDisplayPort().IsEmpty()) {
3960         mCheckerboardEvent->UpdateRendertraceProperty(
3961             CheckerboardEvent::PaintedCriticalDisplayPort,
3962             aLayerMetrics.GetCriticalDisplayPort() +
3963                 aLayerMetrics.GetScrollOffset());
3964       }
3965     }
3966   }
3967 
3968   bool needContentRepaint = false;
3969   bool viewportUpdated = false;
3970   if (FuzzyEqualsAdditive(aLayerMetrics.GetCompositionBounds().Width(),
3971                           mFrameMetrics.GetCompositionBounds().Width()) &&
3972       FuzzyEqualsAdditive(aLayerMetrics.GetCompositionBounds().Height(),
3973                           mFrameMetrics.GetCompositionBounds().Height())) {
3974     // Remote content has sync'd up to the composition geometry
3975     // change, so we can accept the viewport it's calculated.
3976     if (mFrameMetrics.GetViewport().Width() !=
3977             aLayerMetrics.GetViewport().Width() ||
3978         mFrameMetrics.GetViewport().Height() !=
3979             aLayerMetrics.GetViewport().Height()) {
3980       needContentRepaint = true;
3981       viewportUpdated = true;
3982     }
3983     mFrameMetrics.SetViewport(aLayerMetrics.GetViewport());
3984   }
3985 
3986   // If the layers update was not triggered by our own repaint request, then
3987   // we want to take the new scroll offset. Check the scroll generation as well
3988   // to filter duplicate calls to NotifyLayersUpdated with the same scroll
3989   // offset update message.
3990   bool scrollOffsetUpdated = aLayerMetrics.GetScrollOffsetUpdated() &&
3991                              (aLayerMetrics.GetScrollGeneration() !=
3992                               mFrameMetrics.GetScrollGeneration());
3993 
3994   if (scrollOffsetUpdated && userScrolled &&
3995       aLayerMetrics.GetScrollUpdateType() ==
3996           FrameMetrics::ScrollOffsetUpdateType::eRestore) {
3997     APZC_LOG(
3998         "%p dropping scroll update of type eRestore because of user scroll\n",
3999         this);
4000     scrollOffsetUpdated = false;
4001   }
4002 
4003   bool smoothScrollRequested = aLayerMetrics.GetDoSmoothScroll() &&
4004                                (aLayerMetrics.GetScrollGeneration() !=
4005                                 mFrameMetrics.GetScrollGeneration());
4006 
4007   // TODO if we're in a drag and scrollOffsetUpdated is set then we want to
4008   // ignore it
4009 
4010 #if defined(MOZ_WIDGET_ANDROID)
4011   if (aLayerMetrics.IsRootContent()) {
4012     if (APZCTreeManager* manager = GetApzcTreeManager()) {
4013       AndroidDynamicToolbarAnimator* animator =
4014           manager->GetAndroidDynamicToolbarAnimator();
4015       MOZ_ASSERT(animator);
4016       animator->MaybeUpdateCompositionSizeAndRootFrameMetrics(aLayerMetrics);
4017     }
4018   }
4019 #endif
4020 
4021   if ((aIsFirstPaint && aThisLayerTreeUpdated) || isDefault) {
4022     // Initialize our internal state to something sane when the content
4023     // that was just painted is something we knew nothing about previously
4024     CancelAnimation();
4025 
4026     mScrollMetadata = aScrollMetadata;
4027     mExpectedGeckoMetrics = aLayerMetrics;
4028     ShareCompositorFrameMetrics();
4029 
4030     mCompositedScrollOffset = mFrameMetrics.GetScrollOffset();
4031     mCompositedZoom = mFrameMetrics.GetZoom();
4032 
4033     if (mFrameMetrics.GetDisplayPortMargins() != ScreenMargin()) {
4034       // A non-zero display port margin here indicates a displayport has
4035       // been set by a previous APZC for the content at this guid. The
4036       // scrollable rect may have changed since then, making the margins
4037       // wrong, so we need to calculate a new display port.
4038       APZC_LOG("%p detected non-empty margins which probably need updating\n",
4039                this);
4040       needContentRepaint = true;
4041     }
4042   } else {
4043     // If we're not taking the aLayerMetrics wholesale we still need to pull
4044     // in some things into our local mFrameMetrics because these things are
4045     // determined by Gecko and our copy in mFrameMetrics may be stale.
4046 
4047     if (FuzzyEqualsAdditive(mFrameMetrics.GetCompositionBounds().Width(),
4048                             aLayerMetrics.GetCompositionBounds().Width()) &&
4049         mFrameMetrics.GetDevPixelsPerCSSPixel() ==
4050             aLayerMetrics.GetDevPixelsPerCSSPixel() &&
4051         !viewportUpdated) {
4052       // Any change to the pres shell resolution was requested by APZ and is
4053       // already included in our zoom; however, other components of the
4054       // cumulative resolution (a parent document's pres-shell resolution, or
4055       // the css-driven resolution) may have changed, and we need to update
4056       // our zoom to reflect that. Note that we can't just take
4057       // aLayerMetrics.mZoom because the APZ may have additional async zoom
4058       // since the repaint request.
4059       gfxSize totalResolutionChange = aLayerMetrics.GetCumulativeResolution() /
4060                                       mFrameMetrics.GetCumulativeResolution();
4061       float presShellResolutionChange = aLayerMetrics.GetPresShellResolution() /
4062                                         mFrameMetrics.GetPresShellResolution();
4063       if (presShellResolutionChange != 1.0f) {
4064         needContentRepaint = true;
4065       }
4066       mFrameMetrics.ZoomBy(totalResolutionChange / presShellResolutionChange);
4067       mCompositedZoom.xScale *=
4068           (totalResolutionChange / presShellResolutionChange).width;
4069       mCompositedZoom.yScale *=
4070           (totalResolutionChange / presShellResolutionChange).height;
4071     } else {
4072       // Take the new zoom as either device scale or composition width or
4073       // viewport size got changed (e.g. due to orientation change, or content
4074       // changing the meta-viewport tag).
4075       mFrameMetrics.SetZoom(aLayerMetrics.GetZoom());
4076       mCompositedZoom = aLayerMetrics.GetZoom();
4077       mFrameMetrics.SetDevPixelsPerCSSPixel(
4078           aLayerMetrics.GetDevPixelsPerCSSPixel());
4079     }
4080     bool scrollableRectChanged = false;
4081     if (!mFrameMetrics.GetScrollableRect().IsEqualEdges(
4082             aLayerMetrics.GetScrollableRect())) {
4083       mFrameMetrics.SetScrollableRect(aLayerMetrics.GetScrollableRect());
4084       needContentRepaint = true;
4085       scrollableRectChanged = true;
4086     }
4087     mFrameMetrics.SetCompositionBounds(aLayerMetrics.GetCompositionBounds());
4088     mFrameMetrics.SetRootCompositionSize(
4089         aLayerMetrics.GetRootCompositionSize());
4090     mFrameMetrics.SetPresShellResolution(
4091         aLayerMetrics.GetPresShellResolution());
4092     mFrameMetrics.SetCumulativeResolution(
4093         aLayerMetrics.GetCumulativeResolution());
4094     mScrollMetadata.SetHasScrollgrab(aScrollMetadata.GetHasScrollgrab());
4095     mScrollMetadata.SetLineScrollAmount(aScrollMetadata.GetLineScrollAmount());
4096     mScrollMetadata.SetPageScrollAmount(aScrollMetadata.GetPageScrollAmount());
4097     mScrollMetadata.SetSnapInfo(ScrollSnapInfo(aScrollMetadata.GetSnapInfo()));
4098     // The scroll clip can differ between layers associated a given scroll
4099     // frame, so APZC (which keeps a single copy of ScrollMetadata per scroll
4100     // frame) has no business using it.
4101     mScrollMetadata.SetScrollClip(Nothing());
4102     mScrollMetadata.SetIsLayersIdRoot(aScrollMetadata.IsLayersIdRoot());
4103     mScrollMetadata.SetUsesContainerScrolling(
4104         aScrollMetadata.UsesContainerScrolling());
4105     mFrameMetrics.SetIsScrollInfoLayer(aLayerMetrics.IsScrollInfoLayer());
4106     mScrollMetadata.SetForceDisableApz(aScrollMetadata.IsApzForceDisabled());
4107     mScrollMetadata.SetDisregardedDirection(
4108         aScrollMetadata.GetDisregardedDirection());
4109     mScrollMetadata.SetOverscrollBehavior(
4110         aScrollMetadata.GetOverscrollBehavior());
4111 
4112     if (scrollOffsetUpdated) {
4113       APZC_LOG("%p updating scroll offset from %s to %s\n", this,
4114                ToString(mFrameMetrics.GetScrollOffset()).c_str(),
4115                ToString(aLayerMetrics.GetScrollOffset()).c_str());
4116 
4117       // Send an acknowledgement with the new scroll generation so that any
4118       // repaint requests later in this function go through.
4119       // Because of the scroll generation update, any inflight paint requests
4120       // are going to be ignored by layout, and so mExpectedGeckoMetrics becomes
4121       // incorrect for the purposes of calculating the LD transform. To correct
4122       // this we need to update mExpectedGeckoMetrics to be the last thing we
4123       // know was painted by Gecko.
4124       mFrameMetrics.CopyScrollInfoFrom(aLayerMetrics);
4125       mCompositedScrollOffset = mFrameMetrics.GetScrollOffset();
4126       mExpectedGeckoMetrics = aLayerMetrics;
4127 
4128       // Cancel the animation (which might also trigger a repaint request)
4129       // after we update the scroll offset above. Otherwise we can be left
4130       // in a state where things are out of sync.
4131       CancelAnimation();
4132 
4133       // Since the scroll offset has changed, we need to recompute the
4134       // displayport margins and send them to layout. Otherwise there might be
4135       // scenarios where for example we scroll from the top of a page (where the
4136       // top displayport margin is zero) to the bottom of a page, which will
4137       // result in a displayport that doesn't extend upwards at all.
4138       // Note that even if the CancelAnimation call above requested a repaint
4139       // this is fine because we already have repaint request deduplication.
4140       needContentRepaint = true;
4141     } else if (scrollableRectChanged) {
4142       // Even if we didn't accept a new scroll offset from content, the
4143       // scrollable rect may have changed in a way that makes our local
4144       // scroll offset out of bounds, so re-clamp it.
4145       mFrameMetrics.ClampAndSetScrollOffset(mFrameMetrics.GetScrollOffset());
4146     }
4147   }
4148 
4149   if (smoothScrollRequested) {
4150     // A smooth scroll has been requested for animation on the compositor
4151     // thread.  This flag will be reset by the main thread when it receives
4152     // the scroll update acknowledgement.
4153 
4154     APZC_LOG("%p smooth scrolling from %s to %s in state %d\n", this,
4155              Stringify(mFrameMetrics.GetScrollOffset()).c_str(),
4156              Stringify(aLayerMetrics.GetSmoothScrollOffset()).c_str(), mState);
4157 
4158     // See comment on the similar code in the |if (scrollOffsetUpdated)| block
4159     // above.
4160     mFrameMetrics.CopySmoothScrollInfoFrom(aLayerMetrics);
4161     needContentRepaint = true;
4162     mExpectedGeckoMetrics = aLayerMetrics;
4163 
4164     SmoothScrollTo(mFrameMetrics.GetSmoothScrollOffset());
4165   }
4166 
4167   if (needContentRepaint) {
4168     // This repaint request is not driven by a user action on the APZ side
4169     RequestContentRepaint(false);
4170   }
4171   UpdateSharedCompositorFrameMetrics();
4172 }
4173 
GetFrameMetrics() const4174 const FrameMetrics& AsyncPanZoomController::GetFrameMetrics() const {
4175   mRecursiveMutex.AssertCurrentThreadIn();
4176   return mFrameMetrics;
4177 }
4178 
GetScrollMetadata() const4179 const ScrollMetadata& AsyncPanZoomController::GetScrollMetadata() const {
4180   mRecursiveMutex.AssertCurrentThreadIn();
4181   return mScrollMetadata;
4182 }
4183 
GetApzcTreeManager() const4184 APZCTreeManager* AsyncPanZoomController::GetApzcTreeManager() const {
4185   mRecursiveMutex.AssertNotCurrentThreadIn();
4186   return mTreeManager;
4187 }
4188 
ZoomToRect(CSSRect aRect,const uint32_t aFlags)4189 void AsyncPanZoomController::ZoomToRect(CSSRect aRect, const uint32_t aFlags) {
4190   if (!aRect.IsFinite()) {
4191     NS_WARNING("ZoomToRect got called with a non-finite rect; ignoring...");
4192     return;
4193   } else if (aRect.IsEmpty() && (aFlags & DISABLE_ZOOM_OUT)) {
4194     // Double-tap-to-zooming uses an empty rect to mean "zoom out".
4195     // If zooming out is disabled, an empty rect is nonsensical
4196     // and will produce undesirable scrolling.
4197     NS_WARNING(
4198         "ZoomToRect got called with an empty rect and zoom out disabled; "
4199         "ignoring...");
4200     return;
4201   }
4202 
4203   // Only the root APZC is zoomable, and the root APZC is not allowed to have
4204   // different x and y scales. If it did, the calculations in this function
4205   // would have to be adjusted (as e.g. it would no longer be valid to take
4206   // the minimum or maximum of the ratios of the widths and heights of the
4207   // page rect and the composition bounds).
4208   MOZ_ASSERT(mFrameMetrics.IsRootContent());
4209   MOZ_ASSERT(mFrameMetrics.GetZoom().AreScalesSame());
4210 
4211   SetState(ANIMATING_ZOOM);
4212 
4213   {
4214     RecursiveMutexAutoLock lock(mRecursiveMutex);
4215 
4216     ParentLayerRect compositionBounds = mFrameMetrics.GetCompositionBounds();
4217     CSSRect cssPageRect = mFrameMetrics.GetScrollableRect();
4218     CSSPoint scrollOffset = mFrameMetrics.GetScrollOffset();
4219     CSSToParentLayerScale currentZoom = mFrameMetrics.GetZoom().ToScaleFactor();
4220     CSSToParentLayerScale targetZoom;
4221 
4222     // The minimum zoom to prevent over-zoom-out.
4223     // If the zoom factor is lower than this (i.e. we are zoomed more into the
4224     // page), then the CSS content rect, in layers pixels, will be smaller than
4225     // the composition bounds. If this happens, we can't fill the target
4226     // composited area with this frame.
4227     CSSToParentLayerScale localMinZoom(
4228         std::max(mZoomConstraints.mMinZoom.scale,
4229                  std::max(compositionBounds.Width() / cssPageRect.Width(),
4230                           compositionBounds.Height() / cssPageRect.Height())));
4231     CSSToParentLayerScale localMaxZoom = mZoomConstraints.mMaxZoom;
4232 
4233     if (!aRect.IsEmpty()) {
4234       // Intersect the zoom-to-rect to the CSS rect to make sure it fits.
4235       aRect = aRect.Intersect(cssPageRect);
4236       targetZoom = CSSToParentLayerScale(
4237           std::min(compositionBounds.Width() / aRect.Width(),
4238                    compositionBounds.Height() / aRect.Height()));
4239     }
4240 
4241     // 1. If the rect is empty, the content-side logic for handling a double-tap
4242     //    requested that we zoom out.
4243     // 2. currentZoom is equal to mZoomConstraints.mMaxZoom and user still
4244     // double-tapping it
4245     // 3. currentZoom is equal to localMinZoom and user still double-tapping it
4246     // Treat these three cases as a request to zoom out as much as possible.
4247     bool zoomOut;
4248     if (aFlags & DISABLE_ZOOM_OUT) {
4249       zoomOut = false;
4250     } else {
4251       zoomOut = aRect.IsEmpty() ||
4252                 (currentZoom == localMaxZoom && targetZoom >= localMaxZoom) ||
4253                 (currentZoom == localMinZoom && targetZoom <= localMinZoom);
4254     }
4255 
4256     if (zoomOut) {
4257       CSSSize compositedSize =
4258           mFrameMetrics.CalculateCompositedSizeInCssPixels();
4259       float y = scrollOffset.y;
4260       float newHeight =
4261           cssPageRect.Width() * (compositedSize.height / compositedSize.width);
4262       float dh = compositedSize.height - newHeight;
4263 
4264       aRect = CSSRect(0.0f, y + dh / 2, cssPageRect.Width(), newHeight);
4265       aRect = aRect.Intersect(cssPageRect);
4266       targetZoom = CSSToParentLayerScale(
4267           std::min(compositionBounds.Width() / aRect.Width(),
4268                    compositionBounds.Height() / aRect.Height()));
4269     }
4270 
4271     targetZoom.scale =
4272         clamped(targetZoom.scale, localMinZoom.scale, localMaxZoom.scale);
4273     FrameMetrics endZoomToMetrics = mFrameMetrics;
4274     if (aFlags & PAN_INTO_VIEW_ONLY) {
4275       targetZoom = currentZoom;
4276     } else if (aFlags & ONLY_ZOOM_TO_DEFAULT_SCALE) {
4277       CSSToParentLayerScale zoomAtDefaultScale =
4278           mFrameMetrics.GetDevPixelsPerCSSPixel() *
4279           LayoutDeviceToParentLayerScale(1.0);
4280       if (targetZoom.scale > zoomAtDefaultScale.scale) {
4281         // Only change the zoom if we are less than the default zoom
4282         if (currentZoom.scale < zoomAtDefaultScale.scale) {
4283           targetZoom = zoomAtDefaultScale;
4284         } else {
4285           targetZoom = currentZoom;
4286         }
4287       }
4288     }
4289     endZoomToMetrics.SetZoom(CSSToParentLayerScale2D(targetZoom));
4290 
4291     // Adjust the zoomToRect to a sensible position to prevent overscrolling.
4292     CSSSize sizeAfterZoom =
4293         endZoomToMetrics.CalculateCompositedSizeInCssPixels();
4294 
4295     // Vertically center the zoomed element in the screen.
4296     if (!zoomOut && (sizeAfterZoom.height > aRect.Height())) {
4297       aRect.MoveByY(-(sizeAfterZoom.height - aRect.Height()) * 0.5f);
4298       if (aRect.Y() < 0.0f) {
4299         aRect.MoveToY(0.0f);
4300       }
4301     }
4302 
4303     // If either of these conditions are met, the page will be
4304     // overscrolled after zoomed
4305     if (aRect.Y() + sizeAfterZoom.height > cssPageRect.Height()) {
4306       aRect.MoveToY(std::max(0.f, cssPageRect.Height() - sizeAfterZoom.height));
4307     }
4308     if (aRect.X() + sizeAfterZoom.width > cssPageRect.Width()) {
4309       aRect.MoveToX(std::max(0.f, cssPageRect.Width() - sizeAfterZoom.width));
4310     }
4311 
4312     endZoomToMetrics.SetScrollOffset(aRect.TopLeft());
4313 
4314     StartAnimation(new ZoomAnimation(
4315         mFrameMetrics.GetScrollOffset(), mFrameMetrics.GetZoom(),
4316         endZoomToMetrics.GetScrollOffset(), endZoomToMetrics.GetZoom()));
4317 
4318     // Schedule a repaint now, so the new displayport will be painted before the
4319     // animation finishes.
4320     ParentLayerPoint velocity(0, 0);
4321     endZoomToMetrics.SetDisplayPortMargins(
4322         CalculatePendingDisplayPort(endZoomToMetrics, velocity));
4323     endZoomToMetrics.SetUseDisplayPortMargins(true);
4324     endZoomToMetrics.SetPaintRequestTime(TimeStamp::Now());
4325     endZoomToMetrics.SetRepaintDrivenByUserAction(true);
4326 
4327     RefPtr<GeckoContentController> controller = GetGeckoContentController();
4328     if (!controller) {
4329       return;
4330     }
4331     if (controller->IsRepaintThread()) {
4332       RequestContentRepaint(endZoomToMetrics, velocity);
4333     } else {
4334       // use a local var to resolve the function overload
4335       auto func = static_cast<void (AsyncPanZoomController::*)(
4336           const FrameMetrics&, const ParentLayerPoint&)>(
4337           &AsyncPanZoomController::RequestContentRepaint);
4338       controller->DispatchToRepaintThread(
4339           NewRunnableMethod<FrameMetrics, ParentLayerPoint>(
4340               "layers::AsyncPanZoomController::ZoomToRect", this, func,
4341               endZoomToMetrics, velocity));
4342     }
4343   }
4344 }
4345 
GetCurrentInputBlock() const4346 InputBlockState* AsyncPanZoomController::GetCurrentInputBlock() const {
4347   return GetInputQueue()->GetCurrentBlock();
4348 }
4349 
GetCurrentTouchBlock() const4350 TouchBlockState* AsyncPanZoomController::GetCurrentTouchBlock() const {
4351   return GetInputQueue()->GetCurrentTouchBlock();
4352 }
4353 
GetCurrentPanGestureBlock() const4354 PanGestureBlockState* AsyncPanZoomController::GetCurrentPanGestureBlock()
4355     const {
4356   return GetInputQueue()->GetCurrentPanGestureBlock();
4357 }
4358 
ResetTouchInputState()4359 void AsyncPanZoomController::ResetTouchInputState() {
4360   MultiTouchInput cancel(MultiTouchInput::MULTITOUCH_CANCEL, 0,
4361                          TimeStamp::Now(), 0);
4362   RefPtr<GestureEventListener> listener = GetGestureEventListener();
4363   if (listener) {
4364     listener->HandleInputEvent(cancel);
4365   }
4366   CancelAnimationAndGestureState();
4367   // Clear overscroll along the entire handoff chain, in case an APZC
4368   // later in the chain is overscrolled.
4369   if (TouchBlockState* block = GetCurrentTouchBlock()) {
4370     block->GetOverscrollHandoffChain()->ClearOverscroll();
4371   }
4372 }
4373 
CancelAnimationAndGestureState()4374 void AsyncPanZoomController::CancelAnimationAndGestureState() {
4375   mX.CancelGesture();
4376   mY.CancelGesture();
4377   CancelAnimation(CancelAnimationFlags::ScrollSnap);
4378 }
4379 
HasReadyTouchBlock() const4380 bool AsyncPanZoomController::HasReadyTouchBlock() const {
4381   return GetInputQueue()->HasReadyTouchBlock();
4382 }
4383 
SetState(PanZoomState aNewState)4384 void AsyncPanZoomController::SetState(PanZoomState aNewState) {
4385   PanZoomState oldState;
4386 
4387   // Intentional scoping for mutex
4388   {
4389     RecursiveMutexAutoLock lock(mRecursiveMutex);
4390     APZC_LOG("%p changing from state %d to %d\n", this, mState, aNewState);
4391     oldState = mState;
4392     mState = aNewState;
4393   }
4394 
4395   DispatchStateChangeNotification(oldState, aNewState);
4396 }
4397 
DispatchStateChangeNotification(PanZoomState aOldState,PanZoomState aNewState)4398 void AsyncPanZoomController::DispatchStateChangeNotification(
4399     PanZoomState aOldState, PanZoomState aNewState) {
4400   {  // scope the lock
4401     RecursiveMutexAutoLock lock(mRecursiveMutex);
4402     if (mNotificationBlockers > 0) {
4403       return;
4404     }
4405   }
4406 
4407   if (RefPtr<GeckoContentController> controller = GetGeckoContentController()) {
4408     if (!IsTransformingState(aOldState) && IsTransformingState(aNewState)) {
4409       controller->NotifyAPZStateChange(GetGuid(),
4410                                        APZStateChange::eTransformBegin);
4411 #if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
4412       // Let the compositor know about scroll state changes so it can manage
4413       // windowed plugins.
4414       if (gfxPrefs::HidePluginsForScroll() && mCompositorController) {
4415         mCompositorController->ScheduleHideAllPluginWindows();
4416       }
4417 #endif
4418     } else if (IsTransformingState(aOldState) &&
4419                !IsTransformingState(aNewState)) {
4420 #if defined(MOZ_WIDGET_ANDROID)
4421       // The Android UI thread only shows overlay UI elements when the content
4422       // is not being panned or zoomed and it is in a steady state. So the
4423       // FrameMetrics only need to be updated when the transform ends.
4424       if (APZCTreeManager* manager = GetApzcTreeManager()) {
4425         AndroidDynamicToolbarAnimator* animator =
4426             manager->GetAndroidDynamicToolbarAnimator();
4427         MOZ_ASSERT(animator);
4428         animator->UpdateRootFrameMetrics(mFrameMetrics);
4429       }
4430 #endif
4431 
4432       controller->NotifyAPZStateChange(GetGuid(),
4433                                        APZStateChange::eTransformEnd);
4434 #if defined(XP_WIN) || defined(MOZ_WIDGET_GTK)
4435       if (gfxPrefs::HidePluginsForScroll() && mCompositorController) {
4436         mCompositorController->ScheduleShowAllPluginWindows();
4437       }
4438 #endif
4439     }
4440   }
4441 }
4442 
IsTransformingState(PanZoomState aState)4443 bool AsyncPanZoomController::IsTransformingState(PanZoomState aState) {
4444   return !(aState == NOTHING || aState == TOUCHING);
4445 }
4446 
IsInPanningState() const4447 bool AsyncPanZoomController::IsInPanningState() const {
4448   return (mState == PANNING || mState == PANNING_LOCKED_X ||
4449           mState == PANNING_LOCKED_Y);
4450 }
4451 
UpdateZoomConstraints(const ZoomConstraints & aConstraints)4452 void AsyncPanZoomController::UpdateZoomConstraints(
4453     const ZoomConstraints& aConstraints) {
4454   APZC_LOG("%p updating zoom constraints to %d %d %f %f\n", this,
4455            aConstraints.mAllowZoom, aConstraints.mAllowDoubleTapZoom,
4456            aConstraints.mMinZoom.scale, aConstraints.mMaxZoom.scale);
4457   if (IsNaN(aConstraints.mMinZoom.scale) ||
4458       IsNaN(aConstraints.mMaxZoom.scale)) {
4459     NS_WARNING("APZC received zoom constraints with NaN values; dropping...");
4460     return;
4461   }
4462 
4463   CSSToParentLayerScale min = mFrameMetrics.GetDevPixelsPerCSSPixel() *
4464                               kViewportMinScale / ParentLayerToScreenScale(1);
4465   CSSToParentLayerScale max = mFrameMetrics.GetDevPixelsPerCSSPixel() *
4466                               kViewportMaxScale / ParentLayerToScreenScale(1);
4467 
4468   // inf float values and other bad cases should be sanitized by the code below.
4469   mZoomConstraints.mAllowZoom = aConstraints.mAllowZoom;
4470   mZoomConstraints.mAllowDoubleTapZoom = aConstraints.mAllowDoubleTapZoom;
4471   mZoomConstraints.mMinZoom =
4472       (min > aConstraints.mMinZoom ? min : aConstraints.mMinZoom);
4473   mZoomConstraints.mMaxZoom =
4474       (max > aConstraints.mMaxZoom ? aConstraints.mMaxZoom : max);
4475   if (mZoomConstraints.mMaxZoom < mZoomConstraints.mMinZoom) {
4476     mZoomConstraints.mMaxZoom = mZoomConstraints.mMinZoom;
4477   }
4478 }
4479 
GetZoomConstraints() const4480 ZoomConstraints AsyncPanZoomController::GetZoomConstraints() const {
4481   return mZoomConstraints;
4482 }
4483 
PostDelayedTask(already_AddRefed<Runnable> aTask,int aDelayMs)4484 void AsyncPanZoomController::PostDelayedTask(already_AddRefed<Runnable> aTask,
4485                                              int aDelayMs) {
4486   APZThreadUtils::AssertOnControllerThread();
4487   RefPtr<Runnable> task = aTask;
4488   RefPtr<GeckoContentController> controller = GetGeckoContentController();
4489   if (controller) {
4490     controller->PostDelayedTask(task.forget(), aDelayMs);
4491   }
4492   // If there is no controller, that means this APZC has been destroyed, and
4493   // we probably don't need to run the task. It will get destroyed when the
4494   // RefPtr goes out of scope.
4495 }
4496 
Matches(const ScrollableLayerGuid & aGuid)4497 bool AsyncPanZoomController::Matches(const ScrollableLayerGuid& aGuid) {
4498   return aGuid == GetGuid();
4499 }
4500 
HasTreeManager(const APZCTreeManager * aTreeManager) const4501 bool AsyncPanZoomController::HasTreeManager(
4502     const APZCTreeManager* aTreeManager) const {
4503   return GetApzcTreeManager() == aTreeManager;
4504 }
4505 
GetGuid(ScrollableLayerGuid * aGuidOut) const4506 void AsyncPanZoomController::GetGuid(ScrollableLayerGuid* aGuidOut) const {
4507   if (aGuidOut) {
4508     *aGuidOut = GetGuid();
4509   }
4510 }
4511 
GetGuid() const4512 ScrollableLayerGuid AsyncPanZoomController::GetGuid() const {
4513   return ScrollableLayerGuid(mLayersId, mFrameMetrics);
4514 }
4515 
UpdateSharedCompositorFrameMetrics()4516 void AsyncPanZoomController::UpdateSharedCompositorFrameMetrics() {
4517   mRecursiveMutex.AssertCurrentThreadIn();
4518 
4519   FrameMetrics* frame =
4520       mSharedFrameMetricsBuffer
4521           ? static_cast<FrameMetrics*>(mSharedFrameMetricsBuffer->memory())
4522           : nullptr;
4523 
4524   if (frame && mSharedLock && gfxPrefs::ProgressivePaint()) {
4525     mSharedLock->Lock();
4526     *frame = mFrameMetrics;
4527     mSharedLock->Unlock();
4528   }
4529 }
4530 
ShareCompositorFrameMetrics()4531 void AsyncPanZoomController::ShareCompositorFrameMetrics() {
4532   APZThreadUtils::AssertOnSamplerThread();
4533 
4534   // Only create the shared memory buffer if it hasn't already been created,
4535   // we are using progressive tile painting, and we have a
4536   // controller to pass the shared memory back to the content process/thread.
4537   if (!mSharedFrameMetricsBuffer && mMetricsSharingController &&
4538       gfxPrefs::ProgressivePaint()) {
4539     // Create shared memory and initialize it with the current FrameMetrics
4540     // value
4541     mSharedFrameMetricsBuffer = new ipc::SharedMemoryBasic;
4542     FrameMetrics* frame = nullptr;
4543     mSharedFrameMetricsBuffer->Create(sizeof(FrameMetrics));
4544     mSharedFrameMetricsBuffer->Map(sizeof(FrameMetrics));
4545     frame = static_cast<FrameMetrics*>(mSharedFrameMetricsBuffer->memory());
4546 
4547     if (frame) {
4548       {  // scope the monitor, only needed to copy the FrameMetrics.
4549         RecursiveMutexAutoLock lock(mRecursiveMutex);
4550         *frame = mFrameMetrics;
4551       }
4552 
4553       // Get the process id of the content process
4554       base::ProcessId otherPid = mMetricsSharingController->RemotePid();
4555       ipc::SharedMemoryBasic::Handle mem = ipc::SharedMemoryBasic::NULLHandle();
4556 
4557       // Get the shared memory handle to share with the content process
4558       mSharedFrameMetricsBuffer->ShareToProcess(otherPid, &mem);
4559 
4560       // Get the cross process mutex handle to share with the content process
4561       mSharedLock = new CrossProcessMutex("AsyncPanZoomControlLock");
4562       CrossProcessMutexHandle handle = mSharedLock->ShareToProcess(otherPid);
4563 
4564       // Send the shared memory handle and cross process handle to the content
4565       // process by an asynchronous ipc call. Include the APZC unique ID
4566       // so the content process know which APZC sent this shared FrameMetrics.
4567       if (!mMetricsSharingController->StartSharingMetrics(mem, handle,
4568                                                           mLayersId, mAPZCId)) {
4569         APZC_LOG("%p failed to share FrameMetrics with content process.", this);
4570       }
4571     }
4572   }
4573 }
4574 
SetTestAsyncScrollOffset(const CSSPoint & aPoint)4575 void AsyncPanZoomController::SetTestAsyncScrollOffset(const CSSPoint& aPoint) {
4576   mTestAsyncScrollOffset = aPoint;
4577   ScheduleComposite();
4578 }
4579 
SetTestAsyncZoom(const LayerToParentLayerScale & aZoom)4580 void AsyncPanZoomController::SetTestAsyncZoom(
4581     const LayerToParentLayerScale& aZoom) {
4582   mTestAsyncZoom = aZoom;
4583   ScheduleComposite();
4584 }
4585 
FindSnapPointNear(const CSSPoint & aDestination,nsIScrollableFrame::ScrollUnit aUnit)4586 Maybe<CSSPoint> AsyncPanZoomController::FindSnapPointNear(
4587     const CSSPoint& aDestination, nsIScrollableFrame::ScrollUnit aUnit) {
4588   mRecursiveMutex.AssertCurrentThreadIn();
4589   APZC_LOG("%p scroll snapping near %s\n", this,
4590            Stringify(aDestination).c_str());
4591   CSSRect scrollRange = mFrameMetrics.CalculateScrollRange();
4592   if (Maybe<nsPoint> snapPoint = ScrollSnapUtils::GetSnapPointForDestination(
4593           mScrollMetadata.GetSnapInfo(), aUnit,
4594           CSSSize::ToAppUnits(
4595               mFrameMetrics.CalculateCompositedSizeInCssPixels()),
4596           CSSRect::ToAppUnits(scrollRange),
4597           CSSPoint::ToAppUnits(mFrameMetrics.GetScrollOffset()),
4598           CSSPoint::ToAppUnits(aDestination))) {
4599     CSSPoint cssSnapPoint = CSSPoint::FromAppUnits(snapPoint.ref());
4600     // GetSnapPointForDestination() can produce a destination that's outside
4601     // of the scroll frame's scroll range. Clamp it here (this matches the
4602     // behaviour of the main-thread code path, which clamps it in
4603     // nsGfxScrollFrame::ScrollTo()).
4604     return Some(scrollRange.ClampPoint(cssSnapPoint));
4605   }
4606   return Nothing();
4607 }
4608 
ScrollSnapNear(const CSSPoint & aDestination)4609 void AsyncPanZoomController::ScrollSnapNear(const CSSPoint& aDestination) {
4610   if (Maybe<CSSPoint> snapPoint =
4611           FindSnapPointNear(aDestination, nsIScrollableFrame::DEVICE_PIXELS)) {
4612     if (*snapPoint != mFrameMetrics.GetScrollOffset()) {
4613       APZC_LOG("%p smooth scrolling to snap point %s\n", this,
4614                Stringify(*snapPoint).c_str());
4615       SmoothScrollTo(*snapPoint);
4616     }
4617   }
4618 }
4619 
ScrollSnap()4620 void AsyncPanZoomController::ScrollSnap() {
4621   RecursiveMutexAutoLock lock(mRecursiveMutex);
4622   ScrollSnapNear(mFrameMetrics.GetScrollOffset());
4623 }
4624 
ScrollSnapToDestination()4625 void AsyncPanZoomController::ScrollSnapToDestination() {
4626   RecursiveMutexAutoLock lock(mRecursiveMutex);
4627 
4628   float friction = gfxPrefs::APZFlingFriction();
4629   ParentLayerPoint velocity(mX.GetVelocity(), mY.GetVelocity());
4630   ParentLayerPoint predictedDelta;
4631   // "-velocity / log(1.0 - friction)" is the integral of the deceleration
4632   // curve modeled for flings in the "Axis" class.
4633   if (velocity.x != 0.0f) {
4634     predictedDelta.x = -velocity.x / log(1.0 - friction);
4635   }
4636   if (velocity.y != 0.0f) {
4637     predictedDelta.y = -velocity.y / log(1.0 - friction);
4638   }
4639   CSSPoint predictedDestination = mFrameMetrics.GetScrollOffset() +
4640                                   predictedDelta / mFrameMetrics.GetZoom();
4641 
4642   // If the fling will overscroll, don't scroll snap, because then the user
4643   // user would not see any overscroll animation.
4644   bool flingWillOverscroll =
4645       IsOverscrolled() && ((velocity.x * mX.GetOverscroll() >= 0) ||
4646                            (velocity.y * mY.GetOverscroll() >= 0));
4647   if (!flingWillOverscroll) {
4648     APZC_LOG(
4649         "%p fling snapping.  friction: %f velocity: %f, %f "
4650         "predictedDelta: %f, %f position: %f, %f "
4651         "predictedDestination: %f, %f\n",
4652         this, friction, velocity.x, velocity.y, (float)predictedDelta.x,
4653         (float)predictedDelta.y, (float)mFrameMetrics.GetScrollOffset().x,
4654         (float)mFrameMetrics.GetScrollOffset().y, (float)predictedDestination.x,
4655         (float)predictedDestination.y);
4656 
4657     ScrollSnapNear(predictedDestination);
4658   }
4659 }
4660 
MaybeAdjustDeltaForScrollSnapping(const ScrollWheelInput & aEvent,ParentLayerPoint & aDelta,CSSPoint & aStartPosition)4661 bool AsyncPanZoomController::MaybeAdjustDeltaForScrollSnapping(
4662     const ScrollWheelInput& aEvent, ParentLayerPoint& aDelta,
4663     CSSPoint& aStartPosition) {
4664   // Don't scroll snap for pixel scrolls. This matches the main thread
4665   // behaviour in EventStateManager::DoScrollText().
4666   if (aEvent.mDeltaType == ScrollWheelInput::SCROLLDELTA_PIXEL) {
4667     return false;
4668   }
4669 
4670   RecursiveMutexAutoLock lock(mRecursiveMutex);
4671   CSSToParentLayerScale2D zoom = mFrameMetrics.GetZoom();
4672   CSSPoint destination = mFrameMetrics.CalculateScrollRange().ClampPoint(
4673       aStartPosition + (aDelta / zoom));
4674   nsIScrollableFrame::ScrollUnit unit =
4675       ScrollWheelInput::ScrollUnitForDeltaType(aEvent.mDeltaType);
4676 
4677   if (Maybe<CSSPoint> snapPoint = FindSnapPointNear(destination, unit)) {
4678     aDelta = (*snapPoint - aStartPosition) * zoom;
4679     aStartPosition = *snapPoint;
4680     return true;
4681   }
4682   return false;
4683 }
4684 
MaybeAdjustDestinationForScrollSnapping(const KeyboardInput & aEvent,CSSPoint & aDestination)4685 bool AsyncPanZoomController::MaybeAdjustDestinationForScrollSnapping(
4686     const KeyboardInput& aEvent, CSSPoint& aDestination) {
4687   RecursiveMutexAutoLock lock(mRecursiveMutex);
4688   nsIScrollableFrame::ScrollUnit unit =
4689       KeyboardScrollAction::GetScrollUnit(aEvent.mAction.mType);
4690 
4691   if (Maybe<CSSPoint> snapPoint = FindSnapPointNear(aDestination, unit)) {
4692     aDestination = *snapPoint;
4693     return true;
4694   }
4695   return false;
4696 }
4697 
4698 }  // namespace layers
4699 }  // namespace mozilla
4700