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 #ifndef mozilla_layers_AsyncPanZoomAnimation_h_
8 #define mozilla_layers_AsyncPanZoomAnimation_h_
9 
10 #include "APZUtils.h"
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/TimeStamp.h"
13 #include "nsISupportsImpl.h"
14 #include "nsTArray.h"
15 #include "nsThreadUtils.h"
16 
17 namespace mozilla {
18 namespace layers {
19 
20 struct FrameMetrics;
21 
22 class WheelScrollAnimation;
23 class KeyboardScrollAnimation;
24 class OverscrollAnimation;
25 class SmoothMsdScrollAnimation;
26 class SmoothScrollAnimation;
27 
28 class AsyncPanZoomAnimation {
29   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AsyncPanZoomAnimation)
30 
31  public:
32   explicit AsyncPanZoomAnimation() = default;
33 
34   virtual bool DoSample(FrameMetrics& aFrameMetrics,
35                         const TimeDuration& aDelta) = 0;
36 
37   /**
38    * Attempt to handle a main-thread scroll offset update without cancelling
39    * the animation. This may or may not make sense depending on the type of
40    * the animation and whether the scroll update is relative or absolute.
41    *
42    * If the scroll update is relative, |aRelativeDelta| will contain the
43    * delta of the relative update. If the scroll update is absolute,
44    * |aRelativeDelta| will be Nothing() (the animation can check the APZC's
45    * FrameMetrics for the new absolute scroll offset if it wants to handle
46    * and absolute update).
47    *
48    * Returns whether the animation could handle the scroll update. If the
49    * return value is false, the animation will be cancelled.
50    */
HandleScrollOffsetUpdate(const Maybe<CSSPoint> & aRelativeDelta)51   virtual bool HandleScrollOffsetUpdate(const Maybe<CSSPoint>& aRelativeDelta) {
52     return false;
53   }
54 
Sample(FrameMetrics & aFrameMetrics,const TimeDuration & aDelta)55   bool Sample(FrameMetrics& aFrameMetrics, const TimeDuration& aDelta) {
56     // In some situations, particularly when handoff is involved, it's possible
57     // for |aDelta| to be negative on the first call to sample. Ignore such a
58     // sample here, to avoid each derived class having to deal with this case.
59     if (aDelta.ToMilliseconds() <= 0) {
60       return true;
61     }
62 
63     return DoSample(aFrameMetrics, aDelta);
64   }
65 
66   /**
67    * Get the deferred tasks in |mDeferredTasks| and place them in |aTasks|. See
68    * |mDeferredTasks| for more information.  Clears |mDeferredTasks|.
69    */
TakeDeferredTasks()70   nsTArray<RefPtr<Runnable>> TakeDeferredTasks() {
71     return std::move(mDeferredTasks);
72   }
73 
AsKeyboardScrollAnimation()74   virtual KeyboardScrollAnimation* AsKeyboardScrollAnimation() {
75     return nullptr;
76   }
AsWheelScrollAnimation()77   virtual WheelScrollAnimation* AsWheelScrollAnimation() { return nullptr; }
AsSmoothMsdScrollAnimation()78   virtual SmoothMsdScrollAnimation* AsSmoothMsdScrollAnimation() {
79     return nullptr;
80   }
AsSmoothScrollAnimation()81   virtual SmoothScrollAnimation* AsSmoothScrollAnimation() { return nullptr; }
AsOverscrollAnimation()82   virtual OverscrollAnimation* AsOverscrollAnimation() { return nullptr; }
83 
WantsRepaints()84   virtual bool WantsRepaints() { return true; }
85 
Cancel(CancelAnimationFlags aFlags)86   virtual void Cancel(CancelAnimationFlags aFlags) {}
87 
88  protected:
89   // Protected destructor, to discourage deletion outside of Release():
90   virtual ~AsyncPanZoomAnimation() = default;
91 
92   /**
93    * Tasks scheduled for execution after the APZC's mMonitor is released.
94    * Derived classes can add tasks here in Sample(), and the APZC can call
95    * ExecuteDeferredTasks() to execute them.
96    */
97   nsTArray<RefPtr<Runnable>> mDeferredTasks;
98 };
99 
100 }  // namespace layers
101 }  // namespace mozilla
102 
103 #endif  // mozilla_layers_AsyncPanZoomAnimation_h_
104