Bumping manifests a=b2g-bump
[gecko.git] / dom / animation / Animation.h
bloba99d7d6518b34a2909298ebc153275ba84d645ae
1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_Animation_h
7 #define mozilla_dom_Animation_h
9 #include "nsAutoPtr.h"
10 #include "nsCycleCollectionParticipant.h"
11 #include "nsCSSPseudoElements.h"
12 #include "nsIDocument.h"
13 #include "nsWrapperCache.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/StickyTimeDuration.h"
16 #include "mozilla/StyleAnimationValue.h"
17 #include "mozilla/TimeStamp.h"
18 #include "mozilla/dom/Element.h"
19 #include "mozilla/dom/Nullable.h"
20 #include "nsSMILKeySpline.h"
21 #include "nsStyleStruct.h" // for nsTimingFunction
23 struct JSContext;
24 class nsCSSPropertySet;
26 namespace mozilla {
27 namespace css {
28 class AnimValuesStyleRule;
29 } // namespace css
31 /**
32 * Input timing parameters.
34 * Eventually this will represent all the input timing parameters specified
35 * by content but for now it encapsulates just the subset of those
36 * parameters passed to GetPositionInIteration.
38 struct AnimationTiming
40 TimeDuration mIterationDuration;
41 TimeDuration mDelay;
42 float mIterationCount; // mozilla::PositiveInfinity<float>() means infinite
43 uint8_t mDirection;
44 uint8_t mFillMode;
46 bool FillsForwards() const {
47 return mFillMode == NS_STYLE_ANIMATION_FILL_MODE_BOTH ||
48 mFillMode == NS_STYLE_ANIMATION_FILL_MODE_FORWARDS;
50 bool FillsBackwards() const {
51 return mFillMode == NS_STYLE_ANIMATION_FILL_MODE_BOTH ||
52 mFillMode == NS_STYLE_ANIMATION_FILL_MODE_BACKWARDS;
56 /**
57 * Stores the results of calculating the timing properties of an animation
58 * at a given sample time.
60 struct ComputedTiming
62 ComputedTiming()
63 : mTimeFraction(kNullTimeFraction)
64 , mCurrentIteration(0)
65 , mPhase(AnimationPhase_Null)
66 { }
68 static const double kNullTimeFraction;
70 // The total duration of the animation including all iterations.
71 // Will equal StickyTimeDuration::Forever() if the animation repeats
72 // indefinitely.
73 StickyTimeDuration mActiveDuration;
75 // Will be kNullTimeFraction if the animation is neither animating nor
76 // filling at the sampled time.
77 double mTimeFraction;
79 // Zero-based iteration index (meaningless if mTimeFraction is
80 // kNullTimeFraction).
81 uint64_t mCurrentIteration;
83 enum {
84 // Not sampled (null sample time)
85 AnimationPhase_Null,
86 // Sampled prior to the start of the active interval
87 AnimationPhase_Before,
88 // Sampled within the active interval
89 AnimationPhase_Active,
90 // Sampled after (or at) the end of the active interval
91 AnimationPhase_After
92 } mPhase;
95 class ComputedTimingFunction
97 public:
98 typedef nsTimingFunction::Type Type;
99 void Init(const nsTimingFunction &aFunction);
100 double GetValue(double aPortion) const;
101 const nsSMILKeySpline* GetFunction() const {
102 NS_ASSERTION(mType == nsTimingFunction::Function, "Type mismatch");
103 return &mTimingFunction;
105 Type GetType() const { return mType; }
106 uint32_t GetSteps() const { return mSteps; }
108 private:
109 Type mType;
110 nsSMILKeySpline mTimingFunction;
111 uint32_t mSteps;
114 struct AnimationPropertySegment
116 float mFromKey, mToKey;
117 StyleAnimationValue mFromValue, mToValue;
118 ComputedTimingFunction mTimingFunction;
121 struct AnimationProperty
123 nsCSSProperty mProperty;
124 InfallibleTArray<AnimationPropertySegment> mSegments;
127 struct ElementPropertyTransition;
129 namespace dom {
131 class AnimationEffect;
133 class Animation : public nsWrapperCache
135 public:
136 Animation(nsIDocument* aDocument,
137 Element* aTarget,
138 nsCSSPseudoElements::Type aPseudoType,
139 const AnimationTiming &aTiming,
140 const nsSubstring& aName)
141 : mDocument(aDocument)
142 , mTarget(aTarget)
143 , mTiming(aTiming)
144 , mName(aName)
145 , mIsFinishedTransition(false)
146 , mPseudoType(aPseudoType)
148 MOZ_ASSERT(aTarget, "null animation target is not yet supported");
151 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Animation)
152 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(Animation)
154 nsIDocument* GetParentObject() const { return mDocument; }
155 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
157 // FIXME: If we succeed in moving transition-specific code to a type of
158 // AnimationEffect (as per the Web Animations API) we should remove these
159 // virtual methods.
160 virtual ElementPropertyTransition* AsTransition() { return nullptr; }
161 virtual const ElementPropertyTransition* AsTransition() const {
162 return nullptr;
165 // Animation interface
166 // This currently returns a new object each time when used from C++ but is
167 // cached when used from JS.
168 already_AddRefed<AnimationEffect> GetEffect();
169 Element* GetTarget() const {
170 // Currently we only implement Element.getAnimationPlayers() which only
171 // returns animations targetting Elements so this should never
172 // be called for an animation that targets a pseudo-element.
173 MOZ_ASSERT(mPseudoType == nsCSSPseudoElements::ePseudo_NotPseudoElement,
174 "Requesting the target of an Animation that targets a"
175 " pseudo-element is not yet supported.");
176 return mTarget;
179 // Temporary workaround to return both the target element and pseudo-type
180 // until we implement PseudoElement.
181 void GetTarget(Element*& aTarget,
182 nsCSSPseudoElements::Type& aPseudoType) const {
183 aTarget = mTarget;
184 aPseudoType = mPseudoType;
187 void SetParentTime(Nullable<TimeDuration> aParentTime);
189 const AnimationTiming& Timing() const {
190 return mTiming;
192 AnimationTiming& Timing() {
193 return mTiming;
196 const nsString& Name() const {
197 return mName;
200 // Return the duration from the start the active interval to the point where
201 // the animation begins playback. This is zero unless the animation has
202 // a negative delay in which case it is the absolute value of the delay.
203 // This is used for setting the elapsedTime member of CSS AnimationEvents.
204 TimeDuration InitialAdvance() const {
205 return std::max(TimeDuration(), mTiming.mDelay * -1);
208 Nullable<TimeDuration> GetLocalTime() const {
209 // Since the *animation* start time is currently always zero, the local
210 // time is equal to the parent time.
211 return mParentTime;
214 // This function takes as input the timing parameters of an animation and
215 // returns the computed timing at the specified local time.
217 // The local time may be null in which case only static parameters such as the
218 // active duration are calculated. All other members of the returned object
219 // are given a null/initial value.
221 // This function returns ComputedTiming::kNullTimeFraction for the
222 // mTimeFraction member of the return value if the animation should not be
223 // run (because it is not currently active and is not filling at this time).
224 static ComputedTiming
225 GetComputedTimingAt(const Nullable<TimeDuration>& aLocalTime,
226 const AnimationTiming& aTiming);
228 // Shortcut for that gets the computed timing using the current local time as
229 // calculated from the timeline time.
230 ComputedTiming GetComputedTiming(const AnimationTiming* aTiming
231 = nullptr) const {
232 return GetComputedTimingAt(GetLocalTime(), aTiming ? *aTiming : mTiming);
235 // Return the duration of the active interval for the given timing parameters.
236 static StickyTimeDuration
237 ActiveDuration(const AnimationTiming& aTiming);
239 // After transitions finish they need to be retained for one throttle-able
240 // cycle (for reasons see explanation in
241 // layout/style/nsTransitionManager.cpp).
242 // In the meantime, however, they should be ignored.
243 bool IsFinishedTransition() const {
244 return mIsFinishedTransition;
247 void SetIsFinishedTransition() {
248 MOZ_ASSERT(AsTransition(),
249 "Calling SetIsFinishedTransition but it's not a transition");
250 mIsFinishedTransition = true;
253 bool IsCurrent() const;
254 bool IsInEffect() const;
256 bool HasAnimationOfProperty(nsCSSProperty aProperty) const;
257 const InfallibleTArray<AnimationProperty>& Properties() const {
258 return mProperties;
260 InfallibleTArray<AnimationProperty>& Properties() {
261 return mProperties;
264 // Updates |aStyleRule| with the animation values produced by this
265 // Animation for the current time except any properties already contained
266 // in |aSetProperties|.
267 // Any updated properties are added to |aSetProperties|.
268 void ComposeStyle(nsRefPtr<css::AnimValuesStyleRule>& aStyleRule,
269 nsCSSPropertySet& aSetProperties);
271 protected:
272 virtual ~Animation() { }
274 // We use a document for a parent object since the other likely candidate,
275 // the target element, can be empty.
276 nsCOMPtr<nsIDocument> mDocument;
277 nsCOMPtr<Element> mTarget;
278 Nullable<TimeDuration> mParentTime;
280 AnimationTiming mTiming;
281 nsString mName;
282 // A flag to mark transitions that have finished and are due to
283 // be removed on the next throttle-able cycle.
284 bool mIsFinishedTransition;
285 nsCSSPseudoElements::Type mPseudoType;
287 InfallibleTArray<AnimationProperty> mProperties;
290 } // namespace dom
291 } // namespace mozilla
293 #endif // mozilla_dom_Animation_h