AnimationCommon.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef mozilla_css_AnimationCommon_h
  5. #define mozilla_css_AnimationCommon_h
  6. #include <algorithm> // For <std::stable_sort>
  7. #include "mozilla/AnimationCollection.h"
  8. #include "mozilla/AnimationComparator.h"
  9. #include "mozilla/EventDispatcher.h"
  10. #include "mozilla/LinkedList.h"
  11. #include "mozilla/MemoryReporting.h"
  12. #include "mozilla/dom/Animation.h"
  13. #include "mozilla/Attributes.h" // For MOZ_NON_OWNING_REF
  14. #include "mozilla/Assertions.h"
  15. #include "nsContentUtils.h"
  16. #include "nsCSSPseudoElements.h"
  17. #include "nsCycleCollectionParticipant.h"
  18. class nsIFrame;
  19. class nsPresContext;
  20. namespace mozilla {
  21. namespace dom {
  22. class Element;
  23. }
  24. template <class AnimationType>
  25. class CommonAnimationManager {
  26. public:
  27. explicit CommonAnimationManager(nsPresContext *aPresContext)
  28. : mPresContext(aPresContext)
  29. {
  30. }
  31. // NOTE: This can return null after Disconnect().
  32. nsPresContext* PresContext() const { return mPresContext; }
  33. /**
  34. * Notify the manager that the pres context is going away.
  35. */
  36. void Disconnect()
  37. {
  38. // Content nodes might outlive the transition or animation manager.
  39. RemoveAllElementCollections();
  40. mPresContext = nullptr;
  41. }
  42. protected:
  43. virtual ~CommonAnimationManager()
  44. {
  45. MOZ_ASSERT(!mPresContext, "Disconnect should have been called");
  46. }
  47. void AddElementCollection(AnimationCollection<AnimationType>* aCollection)
  48. {
  49. mElementCollections.insertBack(aCollection);
  50. }
  51. void RemoveAllElementCollections()
  52. {
  53. while (AnimationCollection<AnimationType>* head =
  54. mElementCollections.getFirst()) {
  55. head->Destroy(); // Note: this removes 'head' from mElementCollections.
  56. }
  57. }
  58. LinkedList<AnimationCollection<AnimationType>> mElementCollections;
  59. nsPresContext *mPresContext; // weak (non-null from ctor to Disconnect)
  60. };
  61. /**
  62. * Utility class for referencing the element that created a CSS animation or
  63. * transition. It is non-owning (i.e. it uses a raw pointer) since it is only
  64. * expected to be set by the owned animation while it actually being managed
  65. * by the owning element.
  66. *
  67. * This class also abstracts the comparison of an element/pseudo-class pair
  68. * for the sake of composite ordering since this logic is common to both CSS
  69. * animations and transitions.
  70. *
  71. * (We call this OwningElementRef instead of just OwningElement so that we can
  72. * call the getter on CSSAnimation/CSSTransition OwningElement() without
  73. * clashing with this object's contructor.)
  74. */
  75. class OwningElementRef final
  76. {
  77. public:
  78. OwningElementRef()
  79. : mElement(nullptr)
  80. , mPseudoType(CSSPseudoElementType::NotPseudo)
  81. { }
  82. OwningElementRef(dom::Element& aElement,
  83. CSSPseudoElementType aPseudoType)
  84. : mElement(&aElement)
  85. , mPseudoType(aPseudoType)
  86. { }
  87. bool Equals(const OwningElementRef& aOther) const
  88. {
  89. return mElement == aOther.mElement &&
  90. mPseudoType == aOther.mPseudoType;
  91. }
  92. bool LessThan(const OwningElementRef& aOther) const
  93. {
  94. MOZ_ASSERT(mElement && aOther.mElement,
  95. "Elements to compare should not be null");
  96. if (mElement != aOther.mElement) {
  97. return nsContentUtils::PositionIsBefore(mElement, aOther.mElement);
  98. }
  99. return mPseudoType == CSSPseudoElementType::NotPseudo ||
  100. (mPseudoType == CSSPseudoElementType::before &&
  101. aOther.mPseudoType == CSSPseudoElementType::after);
  102. }
  103. bool IsSet() const { return !!mElement; }
  104. void GetElement(dom::Element*& aElement,
  105. CSSPseudoElementType& aPseudoType) const {
  106. aElement = mElement;
  107. aPseudoType = mPseudoType;
  108. }
  109. nsPresContext* GetRenderedPresContext() const;
  110. private:
  111. dom::Element* MOZ_NON_OWNING_REF mElement;
  112. CSSPseudoElementType mPseudoType;
  113. };
  114. template <class EventInfo>
  115. class DelayedEventDispatcher
  116. {
  117. public:
  118. DelayedEventDispatcher() : mIsSorted(true) { }
  119. void QueueEvent(EventInfo&& aEventInfo)
  120. {
  121. mPendingEvents.AppendElement(Forward<EventInfo>(aEventInfo));
  122. mIsSorted = false;
  123. }
  124. // This is exposed as a separate method so that when we are dispatching
  125. // *both* transition events and animation events we can sort both lists
  126. // once using the current state of the document before beginning any
  127. // dispatch.
  128. void SortEvents()
  129. {
  130. if (mIsSorted) {
  131. return;
  132. }
  133. // FIXME: Replace with mPendingEvents.StableSort when bug 1147091 is
  134. // fixed.
  135. std::stable_sort(mPendingEvents.begin(), mPendingEvents.end(),
  136. EventInfoLessThan());
  137. mIsSorted = true;
  138. }
  139. // Takes a reference to the owning manager's pres context so it can
  140. // detect if the pres context is destroyed while dispatching one of
  141. // the events.
  142. //
  143. // This will call SortEvents automatically if it has not already been
  144. // called.
  145. void DispatchEvents(nsPresContext* const & aPresContext)
  146. {
  147. if (!aPresContext || mPendingEvents.IsEmpty()) {
  148. return;
  149. }
  150. SortEvents();
  151. EventArray events;
  152. mPendingEvents.SwapElements(events);
  153. // mIsSorted will be set to true by SortEvents above, and we leave it
  154. // that way since mPendingEvents is now empty
  155. for (EventInfo& info : events) {
  156. EventDispatcher::Dispatch(info.mElement, aPresContext, &info.mEvent);
  157. if (!aPresContext) {
  158. break;
  159. }
  160. }
  161. }
  162. void ClearEventQueue()
  163. {
  164. mPendingEvents.Clear();
  165. mIsSorted = true;
  166. }
  167. bool HasQueuedEvents() const { return !mPendingEvents.IsEmpty(); }
  168. // Methods for supporting cycle-collection
  169. void Traverse(nsCycleCollectionTraversalCallback* aCallback,
  170. const char* aName)
  171. {
  172. for (EventInfo& info : mPendingEvents) {
  173. ImplCycleCollectionTraverse(*aCallback, info.mElement, aName);
  174. ImplCycleCollectionTraverse(*aCallback, info.mAnimation, aName);
  175. }
  176. }
  177. void Unlink() { ClearEventQueue(); }
  178. protected:
  179. class EventInfoLessThan
  180. {
  181. public:
  182. bool operator()(const EventInfo& a, const EventInfo& b) const
  183. {
  184. if (a.mTimeStamp != b.mTimeStamp) {
  185. // Null timestamps sort first
  186. if (a.mTimeStamp.IsNull() || b.mTimeStamp.IsNull()) {
  187. return a.mTimeStamp.IsNull();
  188. } else {
  189. return a.mTimeStamp < b.mTimeStamp;
  190. }
  191. }
  192. AnimationPtrComparator<RefPtr<dom::Animation>> comparator;
  193. return comparator.LessThan(a.mAnimation, b.mAnimation);
  194. }
  195. };
  196. typedef nsTArray<EventInfo> EventArray;
  197. EventArray mPendingEvents;
  198. bool mIsSorted;
  199. };
  200. template <class EventInfo>
  201. inline void
  202. ImplCycleCollectionUnlink(DelayedEventDispatcher<EventInfo>& aField)
  203. {
  204. aField.Unlink();
  205. }
  206. template <class EventInfo>
  207. inline void
  208. ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
  209. DelayedEventDispatcher<EventInfo>& aField,
  210. const char* aName,
  211. uint32_t aFlags = 0)
  212. {
  213. aField.Traverse(&aCallback, aName);
  214. }
  215. // Return the TransitionPhase or AnimationPhase to use when the animation
  216. // doesn't have a target effect.
  217. template <typename PhaseType>
  218. PhaseType GetAnimationPhaseWithoutEffect(const dom::Animation& aAnimation)
  219. {
  220. MOZ_ASSERT(!aAnimation.GetEffect(),
  221. "Should only be called when we do not have an effect");
  222. Nullable<TimeDuration> currentTime = aAnimation.GetCurrentTime();
  223. if (currentTime.IsNull()) {
  224. return PhaseType::Idle;
  225. }
  226. // If we don't have a target effect, the duration will be zero so the phase is
  227. // 'before' if the current time is less than zero.
  228. return currentTime.Value() < TimeDuration()
  229. ? PhaseType::Before
  230. : PhaseType::After;
  231. };
  232. } // namespace mozilla
  233. #endif /* !defined(mozilla_css_AnimationCommon_h) */