Bug 867089 - Validate the playbackRate before using it. r=ehsan
[gecko.git] / layout / style / AnimationCommon.h
blobf0f7c99d81b19777ddcc834a354f697ba0f44cdc
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
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_css_AnimationCommon_h
7 #define mozilla_css_AnimationCommon_h
9 #include "nsIStyleRuleProcessor.h"
10 #include "nsIStyleRule.h"
11 #include "nsRefreshDriver.h"
12 #include "prclist.h"
13 #include "nsStyleAnimation.h"
14 #include "nsCSSProperty.h"
15 #include "mozilla/dom/Element.h"
16 #include "nsSMILKeySpline.h"
17 #include "nsStyleStruct.h"
18 #include "mozilla/Attributes.h"
20 class nsPresContext;
23 namespace mozilla {
24 namespace css {
26 bool IsGeometricProperty(nsCSSProperty aProperty);
28 struct CommonElementAnimationData;
30 class CommonAnimationManager : public nsIStyleRuleProcessor,
31 public nsARefreshObserver {
32 public:
33 CommonAnimationManager(nsPresContext *aPresContext);
34 virtual ~CommonAnimationManager();
36 // nsISupports
37 NS_DECL_ISUPPORTS
39 // nsIStyleRuleProcessor (parts)
40 virtual nsRestyleHint HasStateDependentStyle(StateRuleProcessorData* aData);
41 virtual bool HasDocumentStateDependentStyle(StateRuleProcessorData* aData) MOZ_OVERRIDE;
42 virtual nsRestyleHint
43 HasAttributeDependentStyle(AttributeRuleProcessorData* aData) MOZ_OVERRIDE;
44 virtual bool MediumFeaturesChanged(nsPresContext* aPresContext) MOZ_OVERRIDE;
45 virtual size_t SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf)
46 const MOZ_MUST_OVERRIDE MOZ_OVERRIDE;
47 virtual size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
48 const MOZ_MUST_OVERRIDE MOZ_OVERRIDE;
50 /**
51 * Notify the manager that the pres context is going away.
53 void Disconnect();
55 enum FlushFlags {
56 Can_Throttle,
57 Cannot_Throttle
60 static bool ExtractComputedValueForTransition(
61 nsCSSProperty aProperty,
62 nsStyleContext* aStyleContext,
63 nsStyleAnimation::Value& aComputedValue);
64 static bool ThrottlingEnabled();
65 protected:
66 friend struct CommonElementAnimationData; // for ElementDataRemoved
68 void AddElementData(CommonElementAnimationData* aData);
69 void ElementDataRemoved();
70 void RemoveAllElementData();
72 PRCList mElementData;
73 nsPresContext *mPresContext; // weak (non-null from ctor to Disconnect)
76 /**
77 * A style rule that maps property-nsStyleAnimation::Value pairs.
79 class AnimValuesStyleRule MOZ_FINAL : public nsIStyleRule
81 public:
82 // nsISupports implementation
83 NS_DECL_ISUPPORTS
85 // nsIStyleRule implementation
86 virtual void MapRuleInfoInto(nsRuleData* aRuleData);
87 #ifdef DEBUG
88 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE;
89 #endif
91 void AddValue(nsCSSProperty aProperty, nsStyleAnimation::Value &aStartValue)
93 PropertyValuePair v = { aProperty, aStartValue };
94 mPropertyValuePairs.AppendElement(v);
97 // Caller must fill in returned value.
98 nsStyleAnimation::Value* AddEmptyValue(nsCSSProperty aProperty)
100 PropertyValuePair *p = mPropertyValuePairs.AppendElement();
101 p->mProperty = aProperty;
102 return &p->mValue;
105 struct PropertyValuePair {
106 nsCSSProperty mProperty;
107 nsStyleAnimation::Value mValue;
110 private:
111 InfallibleTArray<PropertyValuePair> mPropertyValuePairs;
114 class ComputedTimingFunction {
115 public:
116 typedef nsTimingFunction::Type Type;
117 void Init(const nsTimingFunction &aFunction);
118 double GetValue(double aPortion) const;
119 const nsSMILKeySpline* GetFunction() const {
120 NS_ASSERTION(mType == nsTimingFunction::Function, "Type mismatch");
121 return &mTimingFunction;
123 Type GetType() const { return mType; }
124 uint32_t GetSteps() const { return mSteps; }
125 private:
126 Type mType;
127 nsSMILKeySpline mTimingFunction;
128 uint32_t mSteps;
131 struct CommonElementAnimationData : public PRCList
133 CommonElementAnimationData(dom::Element *aElement, nsIAtom *aElementProperty,
134 CommonAnimationManager *aManager)
135 : mElement(aElement)
136 , mElementProperty(aElementProperty)
137 , mManager(aManager)
138 , mAnimationGeneration(0)
139 #ifdef DEBUG
140 , mCalledPropertyDtor(false)
141 #endif
143 MOZ_COUNT_CTOR(CommonElementAnimationData);
144 PR_INIT_CLIST(this);
146 ~CommonElementAnimationData()
148 NS_ABORT_IF_FALSE(mCalledPropertyDtor,
149 "must call destructor through element property dtor");
150 MOZ_COUNT_DTOR(CommonElementAnimationData);
151 PR_REMOVE_LINK(this);
152 mManager->ElementDataRemoved();
155 void Destroy()
157 // This will call our destructor.
158 mElement->DeleteProperty(mElementProperty);
161 bool CanThrottleTransformChanges(mozilla::TimeStamp aTime);
163 bool CanThrottleAnimation(mozilla::TimeStamp aTime);
165 enum CanAnimateFlags {
166 // Testing for width, height, top, right, bottom, or left.
167 CanAnimate_HasGeometricProperty = 1,
168 // Allow the case where OMTA is allowed in general, but not for the
169 // specified property.
170 CanAnimate_AllowPartial = 2
173 static bool
174 CanAnimatePropertyOnCompositor(const dom::Element *aElement,
175 nsCSSProperty aProperty,
176 CanAnimateFlags aFlags);
178 // True if this animation can be performed on the compositor thread.
179 // Do not pass CanAnimate_AllowPartial to make sure that all properties of this
180 // animation are supported by the compositor.
181 virtual bool CanPerformOnCompositorThread(CanAnimateFlags aFlags) const = 0;
182 virtual bool HasAnimationOfProperty(nsCSSProperty aProperty) const = 0;
184 static void LogAsyncAnimationFailure(nsCString& aMessage,
185 const nsIContent* aContent = nullptr);
187 dom::Element *mElement;
189 // the atom we use in mElement's prop table (must be a static atom,
190 // i.e., in an atom list)
191 nsIAtom *mElementProperty;
193 CommonAnimationManager *mManager;
195 // This style rule contains the style data for currently animating
196 // values. It only matches when styling with animation. When we
197 // style without animation, we need to not use it so that we can
198 // detect any new changes; if necessary we restyle immediately
199 // afterwards with animation.
200 // NOTE: If we don't need to apply any styles, mStyleRule will be
201 // null, but mStyleRuleRefreshTime will still be valid.
202 nsRefPtr<mozilla::css::AnimValuesStyleRule> mStyleRule;
204 // nsCSSFrameConstructor keeps track of the number of animation 'mini-flushes'
205 // (see nsTransitionManager::UpdateAllThrottledStyles()). mFlushCount is
206 // the last flush where a transition/animation changed. We keep a similar
207 // count on the corresponding layer so we can check that the layer is up to
208 // date with the animation manager.
209 uint64_t mAnimationGeneration;
210 // Update mFlushCount to nsCSSFrameConstructor's count
211 void UpdateAnimationGeneration(nsPresContext* aPresContext);
213 // The refresh time associated with mStyleRule.
214 TimeStamp mStyleRuleRefreshTime;
216 #ifdef DEBUG
217 bool mCalledPropertyDtor;
218 #endif
224 #endif /* !defined(mozilla_css_AnimationCommon_h) */