2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CSSAnimations_h
32 #define CSSAnimations_h
34 #include "core/animation/Animation.h"
35 #include "core/animation/AnimationPlayer.h"
36 #include "core/animation/InertAnimation.h"
37 #include "core/animation/Interpolation.h"
38 #include "core/css/StylePropertySet.h"
39 #include "core/dom/Document.h"
40 #include "core/rendering/style/RenderStyleConstants.h"
41 #include "wtf/HashMap.h"
42 #include "wtf/Vector.h"
43 #include "wtf/text/AtomicString.h"
47 class CSSTransitionData;
49 class StylePropertyShorthand;
51 class StyleRuleKeyframes;
53 // This class stores the CSS Animations/Transitions information we use during a style recalc.
54 // This includes updates to animations/transitions as well as the Interpolations to be applied.
55 class CSSAnimationUpdate FINAL : public NoBaseWillBeGarbageCollectedFinalized<CSSAnimationUpdate> {
57 void startAnimation(AtomicString& animationName, PassRefPtrWillBeRawPtr<InertAnimation> animation)
59 animation->setName(animationName);
60 NewAnimation newAnimation;
61 newAnimation.name = animationName;
62 newAnimation.animation = animation;
63 m_newAnimations.append(newAnimation);
65 // Returns whether player has been cancelled and should be filtered during style application.
66 bool isCancelledAnimation(const AnimationPlayer* player) const { return m_cancelledAnimationPlayers.contains(player); }
67 void cancelAnimation(const AtomicString& name, AnimationPlayer& player)
69 m_cancelledAnimationNames.append(name);
70 m_cancelledAnimationPlayers.add(&player);
72 void toggleAnimationPaused(const AtomicString& name)
74 m_animationsWithPauseToggled.append(name);
77 void startTransition(CSSPropertyID id, CSSPropertyID eventId, const AnimatableValue* from, const AnimatableValue* to, PassRefPtrWillBeRawPtr<InertAnimation> animation)
79 animation->setName(getPropertyName(id));
80 NewTransition newTransition;
81 newTransition.id = id;
82 newTransition.eventId = eventId;
83 newTransition.from = from;
84 newTransition.to = to;
85 newTransition.animation = animation;
86 m_newTransitions.set(id, newTransition);
88 bool isCancelledTransition(CSSPropertyID id) const { return m_cancelledTransitions.contains(id); }
89 void cancelTransition(CSSPropertyID id) { m_cancelledTransitions.add(id); }
92 ALLOW_ONLY_INLINE_ALLOCATION();
94 void trace(Visitor* visitor)
96 visitor->trace(animation);
100 RefPtrWillBeMember<InertAnimation> animation;
102 const WillBeHeapVector<NewAnimation>& newAnimations() const { return m_newAnimations; }
103 const Vector<AtomicString>& cancelledAnimationNames() const { return m_cancelledAnimationNames; }
104 const WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> >& cancelledAnimationAnimationPlayers() const { return m_cancelledAnimationPlayers; }
105 const Vector<AtomicString>& animationsWithPauseToggled() const { return m_animationsWithPauseToggled; }
107 struct NewTransition {
108 ALLOW_ONLY_INLINE_ALLOCATION();
110 void trace(Visitor* visitor)
112 visitor->trace(from);
114 visitor->trace(animation);
118 CSSPropertyID eventId;
119 RawPtrWillBeMember<const AnimatableValue> from;
120 RawPtrWillBeMember<const AnimatableValue> to;
121 RefPtrWillBeMember<InertAnimation> animation;
123 typedef WillBeHeapHashMap<CSSPropertyID, NewTransition> NewTransitionMap;
124 const NewTransitionMap& newTransitions() const { return m_newTransitions; }
125 const HashSet<CSSPropertyID>& cancelledTransitions() const { return m_cancelledTransitions; }
127 void adoptActiveInterpolationsForAnimations(WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& newMap) { newMap.swap(m_activeInterpolationsForAnimations); }
128 void adoptActiveInterpolationsForTransitions(WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& newMap) { newMap.swap(m_activeInterpolationsForTransitions); }
129 const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForAnimations() const { return m_activeInterpolationsForAnimations; }
130 const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForTransitions() const { return m_activeInterpolationsForTransitions; }
131 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForAnimations() { return m_activeInterpolationsForAnimations; }
135 return m_newAnimations.isEmpty()
136 && m_cancelledAnimationNames.isEmpty()
137 && m_cancelledAnimationPlayers.isEmpty()
138 && m_animationsWithPauseToggled.isEmpty()
139 && m_newTransitions.isEmpty()
140 && m_cancelledTransitions.isEmpty()
141 && m_activeInterpolationsForAnimations.isEmpty()
142 && m_activeInterpolationsForTransitions.isEmpty();
145 void trace(Visitor*);
148 // Order is significant since it defines the order in which new animations
149 // will be started. Note that there may be multiple animations present
150 // with the same name, due to the way in which we split up animations with
151 // incomplete keyframes.
152 WillBeHeapVector<NewAnimation> m_newAnimations;
153 Vector<AtomicString> m_cancelledAnimationNames;
154 WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> > m_cancelledAnimationPlayers;
155 Vector<AtomicString> m_animationsWithPauseToggled;
157 NewTransitionMap m_newTransitions;
158 HashSet<CSSPropertyID> m_cancelledTransitions;
160 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_activeInterpolationsForAnimations;
161 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_activeInterpolationsForTransitions;
164 class CSSAnimations FINAL {
165 WTF_MAKE_NONCOPYABLE(CSSAnimations);
166 DISALLOW_ALLOCATION();
170 // FIXME: This method is only used here and in the legacy animations
171 // implementation. It should be made private or file-scope when the legacy
172 // engine is removed.
173 static const StyleRuleKeyframes* matchScopedKeyframesRule(StyleResolver*, const Element*, const StringImpl*);
175 static const StylePropertyShorthand& animatableProperties();
176 static bool isAllowedAnimation(CSSPropertyID);
177 // FIXME: We should change the Element* to a const Element*
178 static PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> calculateUpdate(const Element* animatingElement, Element&, const RenderStyle&, RenderStyle* parentStyle, StyleResolver*);
180 void setPendingUpdate(PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> update) { m_pendingUpdate = update; }
181 void maybeApplyPendingUpdate(Element*);
182 bool isEmpty() const { return m_animations.isEmpty() && m_transitions.isEmpty() && !m_pendingUpdate; }
185 void trace(Visitor*);
188 struct RunningTransition {
189 ALLOW_ONLY_INLINE_ALLOCATION();
191 void trace(Visitor* visitor)
193 visitor->trace(from);
195 visitor->trace(player);
198 RefPtrWillBeMember<AnimationPlayer> player;
199 RawPtrWillBeMember<const AnimatableValue> from;
200 RawPtrWillBeMember<const AnimatableValue> to;
203 typedef WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<AnimationPlayer> > AnimationMap;
204 AnimationMap m_animations;
206 typedef WillBeHeapHashMap<CSSPropertyID, RunningTransition> TransitionMap;
207 TransitionMap m_transitions;
209 OwnPtrWillBeMember<CSSAnimationUpdate> m_pendingUpdate;
211 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_previousActiveInterpolationsForAnimations;
213 static void calculateAnimationUpdate(CSSAnimationUpdate*, const Element* animatingElement, Element&, const RenderStyle&, RenderStyle* parentStyle, StyleResolver*);
214 static void calculateTransitionUpdate(CSSAnimationUpdate*, const Element* animatingElement, const RenderStyle&);
215 static void calculateTransitionUpdateForProperty(CSSPropertyID, CSSPropertyID eventId, const CSSTransitionData&, size_t transitionIndex, const RenderStyle& oldStyle, const RenderStyle&, const TransitionMap* activeTransitions, CSSAnimationUpdate*, const Element*);
217 static void calculateAnimationActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime);
218 static void calculateTransitionActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime);
220 class AnimationEventDelegate FINAL : public AnimationNode::EventDelegate {
222 AnimationEventDelegate(Element* target, const AtomicString& name)
225 , m_previousPhase(AnimationNode::PhaseNone)
226 , m_previousIteration(nullValue())
229 virtual void onEventCondition(const AnimationNode*) OVERRIDE;
230 virtual void trace(Visitor*) OVERRIDE;
233 void maybeDispatch(Document::ListenerType, const AtomicString& eventName, double elapsedTime);
234 RawPtrWillBeMember<Element> m_target;
235 const AtomicString m_name;
236 AnimationNode::Phase m_previousPhase;
237 double m_previousIteration;
240 class TransitionEventDelegate FINAL : public AnimationNode::EventDelegate {
242 TransitionEventDelegate(Element* target, CSSPropertyID property)
244 , m_property(property)
245 , m_previousPhase(AnimationNode::PhaseNone)
248 virtual void onEventCondition(const AnimationNode*) OVERRIDE;
249 virtual void trace(Visitor*) OVERRIDE;
252 RawPtrWillBeMember<Element> m_target;
253 const CSSPropertyID m_property;
254 AnimationNode::Phase m_previousPhase;
260 WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(blink::CSSAnimationUpdate::NewAnimation);