SMILCompositor.h (5287B)
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 DOM_SMIL_SMILCOMPOSITOR_H_ 8 #define DOM_SMIL_SMILCOMPOSITOR_H_ 9 10 #include <utility> 11 12 #include "NonCustomCSSPropertyId.h" 13 #include "PLDHashTable.h" 14 #include "SMILTargetIdentifier.h" 15 #include "mozilla/SMILAnimationFunction.h" 16 #include "mozilla/SMILCompositorTable.h" 17 #include "mozilla/UniquePtr.h" 18 #include "nsString.h" 19 #include "nsTHashtable.h" 20 21 namespace mozilla { 22 23 class ComputedStyle; 24 25 //---------------------------------------------------------------------- 26 // SMILCompositor 27 // 28 // Performs the composition of the animation sandwich by combining the results 29 // of a series animation functions according to the rules of SMIL composition 30 // including prioritising animations. 31 32 class SMILCompositor : public PLDHashEntryHdr { 33 public: 34 using KeyType = SMILTargetIdentifier; 35 using KeyTypeRef = const KeyType&; 36 using KeyTypePointer = const KeyType*; 37 38 explicit SMILCompositor(KeyTypePointer aKey) 39 : mKey(*aKey), mForceCompositing(false) {} 40 SMILCompositor(SMILCompositor&& toMove) noexcept 41 : PLDHashEntryHdr(std::move(toMove)), 42 mKey(std::move(toMove.mKey)), 43 mAnimationFunctions(std::move(toMove.mAnimationFunctions)), 44 mForceCompositing(false) {} 45 46 // PLDHashEntryHdr methods 47 KeyTypeRef GetKey() const { return mKey; } 48 bool KeyEquals(KeyTypePointer aKey) const; 49 static KeyTypePointer KeyToPointer(KeyTypeRef aKey) { return &aKey; } 50 static PLDHashNumber HashKey(KeyTypePointer aKey); 51 enum { ALLOW_MEMMOVE = false }; 52 53 // Adds the given animation function to this Compositor's list of functions 54 void AddAnimationFunction(SMILAnimationFunction* aFunc); 55 56 // Composes the attribute's current value with the list of animation 57 // functions, and assigns the resulting value to this compositor's target 58 // attribute. If a change is made that might produce style updates, 59 // aMightHavePendingStyleUpdates is set to true. Otherwise it is not modified. 60 void ComposeAttribute(bool& aMightHavePendingStyleUpdates); 61 62 // Clears animation effects on my target attribute 63 void ClearAnimationEffects(); 64 65 // Cycle-collection support 66 void Traverse(nsCycleCollectionTraversalCallback* aCallback); 67 68 // Toggles a bit that will force us to composite (bypassing early-return 69 // optimizations) when we hit ComposeAttribute. 70 void ToggleForceCompositing() { mForceCompositing = true; } 71 72 // Transfers |aOther|'s mCachedBaseValue to |this| 73 void StealCachedBaseValue(SMILCompositor* aOther) { 74 mCachedBaseValue = std::move(aOther->mCachedBaseValue); 75 } 76 77 bool HasSameNumberOfAnimationFunctionsAs(const SMILCompositor& aOther) const { 78 return mAnimationFunctions.Length() == aOther.mAnimationFunctions.Length(); 79 } 80 81 private: 82 // Create a SMILAttr for my target, on the heap. 83 // 84 // @param aBaseComputedStyle An optional ComputedStyle which, if set, will be 85 // used when fetching the base style. 86 UniquePtr<SMILAttr> CreateSMILAttr(const ComputedStyle* aBaseComputedStyle); 87 88 // Returns the CSS property this compositor should animate, or 89 // eCSSProperty_UNKNOWN if this compositor does not animate a CSS property. 90 NonCustomCSSPropertyId GetCSSPropertyToAnimate() const; 91 92 // Returns true if we might need to refer to base styles (i.e. we are 93 // targeting a CSS property and have one or more animation functions that 94 // don't just replace the underlying value). 95 // 96 // This might return true in some cases where we don't actually need the base 97 // style since it doesn't build up the animation sandwich to check if the 98 // functions that appear to need the base style are actually replaced by 99 // a function further up the stack. 100 bool MightNeedBaseStyle() const; 101 102 // Finds the index of the first function that will affect our animation 103 // sandwich. Also toggles the 'mForceCompositing' flag if it finds that any 104 // (used) functions have changed. 105 uint32_t GetFirstFuncToAffectSandwich(); 106 107 // If the passed-in base value differs from our cached base value, this 108 // method updates the cached value (and toggles the 'mForceCompositing' flag) 109 void UpdateCachedBaseValue(const SMILValue& aBaseValue); 110 111 // The hash key (tuple of element and attributeName) 112 KeyType mKey; 113 114 // Hash Value: List of animation functions that animate the specified attr 115 nsTArray<SMILAnimationFunction*> mAnimationFunctions; 116 117 // Member data for detecting when we need to force-recompose 118 // --------------------------------------------------------- 119 // Flag for tracking whether we need to compose. Initialized to false, but 120 // gets flipped to true if we detect that something has changed. 121 bool mForceCompositing; 122 123 // Cached base value, so we can detect & force-recompose when it changes 124 // from one sample to the next. (SMILAnimationController moves this 125 // forward from the previous sample's compositor by calling 126 // StealCachedBaseValue.) 127 SMILValue mCachedBaseValue; 128 }; 129 130 } // namespace mozilla 131 132 #endif // DOM_SMIL_SMILCOMPOSITOR_H_