layout/style/nsAnimationManager.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #ifndef nsAnimationManager_h_
michael@0 6 #define nsAnimationManager_h_
michael@0 7
michael@0 8 #include "mozilla/Attributes.h"
michael@0 9 #include "mozilla/ContentEvents.h"
michael@0 10 #include "AnimationCommon.h"
michael@0 11 #include "nsCSSPseudoElements.h"
michael@0 12 #include "mozilla/MemoryReporting.h"
michael@0 13 #include "mozilla/TimeStamp.h"
michael@0 14
michael@0 15 class nsCSSKeyframesRule;
michael@0 16 class nsStyleContext;
michael@0 17
michael@0 18 namespace mozilla {
michael@0 19 namespace css {
michael@0 20 class Declaration;
michael@0 21 }
michael@0 22 }
michael@0 23
michael@0 24 struct AnimationEventInfo {
michael@0 25 nsRefPtr<mozilla::dom::Element> mElement;
michael@0 26 mozilla::InternalAnimationEvent mEvent;
michael@0 27
michael@0 28 AnimationEventInfo(mozilla::dom::Element *aElement,
michael@0 29 const nsString& aAnimationName,
michael@0 30 uint32_t aMessage, mozilla::TimeDuration aElapsedTime,
michael@0 31 const nsAString& aPseudoElement)
michael@0 32 : mElement(aElement), mEvent(true, aMessage)
michael@0 33 {
michael@0 34 // XXX Looks like nobody initialize WidgetEvent::time
michael@0 35 mEvent.animationName = aAnimationName;
michael@0 36 mEvent.elapsedTime = aElapsedTime.ToSeconds();
michael@0 37 mEvent.pseudoElement = aPseudoElement;
michael@0 38 }
michael@0 39
michael@0 40 // InternalAnimationEvent doesn't support copy-construction, so we need
michael@0 41 // to ourselves in order to work with nsTArray
michael@0 42 AnimationEventInfo(const AnimationEventInfo &aOther)
michael@0 43 : mElement(aOther.mElement), mEvent(true, aOther.mEvent.message)
michael@0 44 {
michael@0 45 mEvent.AssignAnimationEventData(aOther.mEvent, false);
michael@0 46 }
michael@0 47 };
michael@0 48
michael@0 49 typedef InfallibleTArray<AnimationEventInfo> EventArray;
michael@0 50
michael@0 51 /**
michael@0 52 * Data about all of the animations running on an element.
michael@0 53 */
michael@0 54 struct ElementAnimations MOZ_FINAL
michael@0 55 : public mozilla::css::CommonElementAnimationData
michael@0 56 {
michael@0 57 typedef mozilla::TimeStamp TimeStamp;
michael@0 58 typedef mozilla::TimeDuration TimeDuration;
michael@0 59
michael@0 60 ElementAnimations(mozilla::dom::Element *aElement, nsIAtom *aElementProperty,
michael@0 61 nsAnimationManager *aAnimationManager, TimeStamp aNow);
michael@0 62
michael@0 63 // This function takes as input the start time, duration, and direction of an
michael@0 64 // animation and returns the position in the current iteration. Note that
michael@0 65 // this only works when we know that the animation is currently running.
michael@0 66 // This way of calling the function can be used from the compositor. Note
michael@0 67 // that if the animation has not started yet, has already ended, or is paused,
michael@0 68 // it should not be run from the compositor. When this function is called
michael@0 69 // from the main thread, we need the actual StyleAnimation* in order to
michael@0 70 // get correct animation-fill behavior and to fire animation events.
michael@0 71 // This function returns -1 for the position if the animation should not be
michael@0 72 // run (because it is not currently active and has no fill behavior), but
michael@0 73 // only does so if aAnimation is non-null; with a null aAnimation it is an
michael@0 74 // error to give aElapsedDuration < 0, and fill-forwards is assumed.
michael@0 75 // After calling GetPositionInIteration with non-null aAnimation and aEa, be
michael@0 76 // sure to call CheckNeedsRefresh on the animation manager afterwards.
michael@0 77 static double GetPositionInIteration(TimeDuration aElapsedDuration,
michael@0 78 TimeDuration aIterationDuration,
michael@0 79 double aIterationCount,
michael@0 80 uint32_t aDirection,
michael@0 81 mozilla::StyleAnimation* aAnimation =
michael@0 82 nullptr,
michael@0 83 ElementAnimations* aEa = nullptr,
michael@0 84 EventArray* aEventsToDispatch = nullptr);
michael@0 85
michael@0 86 void EnsureStyleRuleFor(TimeStamp aRefreshTime,
michael@0 87 EventArray &aEventsToDispatch,
michael@0 88 bool aIsThrottled);
michael@0 89
michael@0 90 bool IsForElement() const { // rather than for a pseudo-element
michael@0 91 return mElementProperty == nsGkAtoms::animationsProperty;
michael@0 92 }
michael@0 93
michael@0 94 nsString PseudoElement()
michael@0 95 {
michael@0 96 return mElementProperty == nsGkAtoms::animationsProperty ?
michael@0 97 EmptyString() :
michael@0 98 mElementProperty == nsGkAtoms::animationsOfBeforeProperty ?
michael@0 99 NS_LITERAL_STRING("::before") :
michael@0 100 NS_LITERAL_STRING("::after");
michael@0 101 }
michael@0 102
michael@0 103 void PostRestyleForAnimation(nsPresContext *aPresContext) {
michael@0 104 nsRestyleHint styleHint = IsForElement() ? eRestyle_Self : eRestyle_Subtree;
michael@0 105 aPresContext->PresShell()->RestyleForAnimation(mElement, styleHint);
michael@0 106 }
michael@0 107
michael@0 108 // If aFlags contains CanAnimate_AllowPartial, returns whether the
michael@0 109 // state of this element's animations at the current refresh driver
michael@0 110 // time contains animation data that can be done on the compositor
michael@0 111 // thread. (This is useful for determining whether a layer should be
michael@0 112 // active, or whether to send data to the layer.)
michael@0 113 // If aFlags does not contain CanAnimate_AllowPartial, returns whether
michael@0 114 // the state of this element's animations at the current refresh driver
michael@0 115 // time can be fully represented by data sent to the compositor.
michael@0 116 // (This is useful for determining whether throttle the animation
michael@0 117 // (suppress main-thread style updates).)
michael@0 118 // Note that when CanPerformOnCompositorThread returns true, it also,
michael@0 119 // as a side-effect, notifies the ActiveLayerTracker. FIXME: This
michael@0 120 // should probably move to the relevant callers.
michael@0 121 virtual bool CanPerformOnCompositorThread(CanAnimateFlags aFlags) const MOZ_OVERRIDE;
michael@0 122
michael@0 123 virtual bool HasAnimationOfProperty(nsCSSProperty aProperty) const MOZ_OVERRIDE;
michael@0 124
michael@0 125 // False when we know that our current style rule is valid
michael@0 126 // indefinitely into the future (because all of our animations are
michael@0 127 // either completed or paused). May be invalidated by a style change.
michael@0 128 bool mNeedsRefreshes;
michael@0 129
michael@0 130 InfallibleTArray<mozilla::StyleAnimation> mAnimations;
michael@0 131 };
michael@0 132
michael@0 133 class nsAnimationManager MOZ_FINAL
michael@0 134 : public mozilla::css::CommonAnimationManager
michael@0 135 {
michael@0 136 public:
michael@0 137 nsAnimationManager(nsPresContext *aPresContext)
michael@0 138 : mozilla::css::CommonAnimationManager(aPresContext)
michael@0 139 , mObservingRefreshDriver(false)
michael@0 140 {
michael@0 141 }
michael@0 142
michael@0 143 static ElementAnimations* GetAnimationsForCompositor(nsIContent* aContent,
michael@0 144 nsCSSProperty aProperty)
michael@0 145 {
michael@0 146 if (!aContent->MayHaveAnimations())
michael@0 147 return nullptr;
michael@0 148 ElementAnimations* animations = static_cast<ElementAnimations*>(
michael@0 149 aContent->GetProperty(nsGkAtoms::animationsProperty));
michael@0 150 if (!animations)
michael@0 151 return nullptr;
michael@0 152 bool propertyMatches = animations->HasAnimationOfProperty(aProperty);
michael@0 153 return (propertyMatches &&
michael@0 154 animations->CanPerformOnCompositorThread(
michael@0 155 mozilla::css::CommonElementAnimationData::CanAnimate_AllowPartial))
michael@0 156 ? animations
michael@0 157 : nullptr;
michael@0 158 }
michael@0 159
michael@0 160 // Returns true if aContent or any of its ancestors has an animation.
michael@0 161 static bool ContentOrAncestorHasAnimation(nsIContent* aContent) {
michael@0 162 do {
michael@0 163 if (aContent->GetProperty(nsGkAtoms::animationsProperty)) {
michael@0 164 return true;
michael@0 165 }
michael@0 166 } while ((aContent = aContent->GetParent()));
michael@0 167
michael@0 168 return false;
michael@0 169 }
michael@0 170
michael@0 171 void EnsureStyleRuleFor(ElementAnimations* aET);
michael@0 172
michael@0 173 // nsIStyleRuleProcessor (parts)
michael@0 174 virtual void RulesMatching(ElementRuleProcessorData* aData) MOZ_OVERRIDE;
michael@0 175 virtual void RulesMatching(PseudoElementRuleProcessorData* aData) MOZ_OVERRIDE;
michael@0 176 virtual void RulesMatching(AnonBoxRuleProcessorData* aData) MOZ_OVERRIDE;
michael@0 177 #ifdef MOZ_XUL
michael@0 178 virtual void RulesMatching(XULTreeRuleProcessorData* aData) MOZ_OVERRIDE;
michael@0 179 #endif
michael@0 180 virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
michael@0 181 const MOZ_MUST_OVERRIDE MOZ_OVERRIDE;
michael@0 182 virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
michael@0 183 const MOZ_MUST_OVERRIDE MOZ_OVERRIDE;
michael@0 184
michael@0 185 // nsARefreshObserver
michael@0 186 virtual void WillRefresh(mozilla::TimeStamp aTime) MOZ_OVERRIDE;
michael@0 187
michael@0 188 void FlushAnimations(FlushFlags aFlags);
michael@0 189
michael@0 190 /**
michael@0 191 * Return the style rule that RulesMatching should add for
michael@0 192 * aStyleContext. This might be different from what RulesMatching
michael@0 193 * actually added during aStyleContext's construction because the
michael@0 194 * element's animation-name may have changed. (However, this does
michael@0 195 * return null during the non-animation restyling phase, as
michael@0 196 * RulesMatching does.)
michael@0 197 *
michael@0 198 * aStyleContext may be a style context for aElement or for its
michael@0 199 * :before or :after pseudo-element.
michael@0 200 */
michael@0 201 nsIStyleRule* CheckAnimationRule(nsStyleContext* aStyleContext,
michael@0 202 mozilla::dom::Element* aElement);
michael@0 203
michael@0 204 /**
michael@0 205 * Dispatch any pending events. We accumulate animationend and
michael@0 206 * animationiteration events only during refresh driver notifications
michael@0 207 * (and dispatch them at the end of such notifications), but we
michael@0 208 * accumulate animationstart events at other points when style
michael@0 209 * contexts are created.
michael@0 210 */
michael@0 211 void DispatchEvents() {
michael@0 212 // Fast-path the common case: no events
michael@0 213 if (!mPendingEvents.IsEmpty()) {
michael@0 214 DoDispatchEvents();
michael@0 215 }
michael@0 216 }
michael@0 217
michael@0 218 ElementAnimations* GetElementAnimations(mozilla::dom::Element *aElement,
michael@0 219 nsCSSPseudoElements::Type aPseudoType,
michael@0 220 bool aCreateIfNeeded);
michael@0 221
michael@0 222 // Updates styles on throttled animations. See note on nsTransitionManager
michael@0 223 void UpdateAllThrottledStyles();
michael@0 224
michael@0 225 protected:
michael@0 226 virtual void ElementDataRemoved() MOZ_OVERRIDE
michael@0 227 {
michael@0 228 CheckNeedsRefresh();
michael@0 229 }
michael@0 230 virtual void AddElementData(mozilla::css::CommonElementAnimationData* aData) MOZ_OVERRIDE;
michael@0 231
michael@0 232 /**
michael@0 233 * Check to see if we should stop or start observing the refresh driver
michael@0 234 */
michael@0 235 void CheckNeedsRefresh();
michael@0 236
michael@0 237 private:
michael@0 238 void BuildAnimations(nsStyleContext* aStyleContext,
michael@0 239 InfallibleTArray<mozilla::StyleAnimation>& aAnimations);
michael@0 240 bool BuildSegment(InfallibleTArray<mozilla::AnimationPropertySegment>&
michael@0 241 aSegments,
michael@0 242 nsCSSProperty aProperty, const nsAnimation& aAnimation,
michael@0 243 float aFromKey, nsStyleContext* aFromContext,
michael@0 244 mozilla::css::Declaration* aFromDeclaration,
michael@0 245 float aToKey, nsStyleContext* aToContext);
michael@0 246 nsIStyleRule* GetAnimationRule(mozilla::dom::Element* aElement,
michael@0 247 nsCSSPseudoElements::Type aPseudoType);
michael@0 248
michael@0 249 // Update the animated styles of an element and its descendants.
michael@0 250 // If the element has an animation, it is flushed back to its primary frame.
michael@0 251 // If the element does not have an animation, then its style is reparented.
michael@0 252 void UpdateThrottledStylesForSubtree(nsIContent* aContent,
michael@0 253 nsStyleContext* aParentStyle,
michael@0 254 nsStyleChangeList &aChangeList);
michael@0 255 void UpdateAllThrottledStylesInternal();
michael@0 256
michael@0 257 // The guts of DispatchEvents
michael@0 258 void DoDispatchEvents();
michael@0 259
michael@0 260 EventArray mPendingEvents;
michael@0 261
michael@0 262 bool mObservingRefreshDriver;
michael@0 263 };
michael@0 264
michael@0 265 #endif /* !defined(nsAnimationManager_h_) */

mercurial