nsAnimationManager.h (3748B)
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 #ifndef nsAnimationManager_h_ 7 #define nsAnimationManager_h_ 8 9 #include "AnimationCommon.h" 10 #include "mozilla/Keyframe.h" 11 #include "mozilla/dom/CSSAnimation.h" 12 #include "nsISupportsImpl.h" 13 #include "nsTHashSet.h" 14 15 class ServoCSSAnimationBuilder; 16 17 struct nsStyleUIReset; 18 19 namespace mozilla { 20 class ComputedStyle; 21 22 struct NonOwningAnimationTarget; 23 struct PseudoStyleRequest; 24 25 } /* namespace mozilla */ 26 27 class nsAnimationManager final 28 : public mozilla::CommonAnimationManager<mozilla::dom::CSSAnimation> { 29 public: 30 explicit nsAnimationManager(nsPresContext* aPresContext) 31 : mozilla::CommonAnimationManager<mozilla::dom::CSSAnimation>( 32 aPresContext) {} 33 34 typedef mozilla::AnimationCollection<mozilla::dom::CSSAnimation> 35 CSSAnimationCollection; 36 typedef nsTArray<RefPtr<mozilla::dom::CSSAnimation>> 37 OwningCSSAnimationPtrArray; 38 39 ~nsAnimationManager() override = default; 40 41 /** 42 * This function does the same thing as the above UpdateAnimations() 43 * but with servo's computed values. 44 */ 45 void UpdateAnimations(mozilla::dom::Element* aElement, 46 const mozilla::PseudoStyleRequest& aPseudoRequest, 47 const mozilla::ComputedStyle* aComputedValues); 48 49 // Utility function to walk through |aIter| to find the Keyframe with 50 // matching offset and timing function but stopping as soon as the offset 51 // differs from |aOffset| (i.e. it assumes a sorted iterator). 52 // 53 // If a matching Keyframe is found, 54 // Returns true and sets |aIndex| to the index of the matching Keyframe 55 // within |aIter|. 56 // 57 // If no matching Keyframe is found, 58 // Returns false and sets |aIndex| to the index in the iterator of the 59 // first Keyframe with an offset differing to |aOffset| or, if the end 60 // of the iterator is reached, sets |aIndex| to the index after the last 61 // Keyframe. 62 template <class IterType> 63 static bool FindMatchingKeyframe( 64 IterType&& aIter, double aOffset, 65 const mozilla::StyleComputedTimingFunction& aTimingFunctionToMatch, 66 mozilla::dom::CompositeOperationOrAuto aCompositionToMatch, 67 size_t& aIndex) { 68 aIndex = 0; 69 for (mozilla::Keyframe& keyframe : aIter) { 70 if (keyframe.mOffset.value() != aOffset) { 71 break; 72 } 73 const bool matches = [&] { 74 if (keyframe.mComposite != aCompositionToMatch) { 75 return false; 76 } 77 return keyframe.mTimingFunction 78 ? *keyframe.mTimingFunction == aTimingFunctionToMatch 79 : aTimingFunctionToMatch.IsLinearKeyword(); 80 }(); 81 if (matches) { 82 return true; 83 } 84 ++aIndex; 85 } 86 return false; 87 } 88 89 bool AnimationMayBeReferenced(nsAtom* aName) const { 90 return mMaybeReferencedAnimations.Contains(aName); 91 } 92 93 private: 94 // This includes all animation names referenced regardless of whether a 95 // corresponding `@keyframes` rule is available. 96 // 97 // It may contain names which are no longer referenced, but it should always 98 // contain names which are currently referenced, so that it is usable for 99 // style invalidation. 100 nsTHashSet<RefPtr<nsAtom>> mMaybeReferencedAnimations; 101 102 void DoUpdateAnimations(const mozilla::NonOwningAnimationTarget& aTarget, 103 const nsStyleUIReset& aStyle, 104 ServoCSSAnimationBuilder& aBuilder); 105 }; 106 107 #endif /* !defined(nsAnimationManager_h_) */