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