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 using NewTransitionMap = WillBeHeapHashMap<CSSPropertyID, NewTransition>;
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 const AtomicString getAnimationNameForInspector(const AnimationPlayer&);
172 // FIXME: This method is only used here and in the legacy animations
173 // implementation. It should be made private or file-scope when the legacy
174 // engine is removed.
175 static const StyleRuleKeyframes* matchScopedKeyframesRule(StyleResolver*, const Element*, const StringImpl*);
177 static const StylePropertyShorthand& animatableProperties();
178 static bool isAllowedAnimation(CSSPropertyID);
179 // FIXME: We should change the Element* to a const Element*
180 static PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> calculateUpdate(const Element* animatingElement, Element&, const RenderStyle&, RenderStyle* parentStyle, StyleResolver*);
182 void setPendingUpdate(PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> update) { m_pendingUpdate = update; }
183 void maybeApplyPendingUpdate(Element*);
184 bool isEmpty() const { return m_animations.isEmpty() && m_transitions.isEmpty() && !m_pendingUpdate; }
187 void trace(Visitor*);
190 struct RunningTransition {
191 ALLOW_ONLY_INLINE_ALLOCATION();
193 void trace(Visitor* visitor)
195 visitor->trace(from);
197 visitor->trace(player);
200 RefPtrWillBeMember<AnimationPlayer> player;
201 RawPtrWillBeMember<const AnimatableValue> from;
202 RawPtrWillBeMember<const AnimatableValue> to;
205 using AnimationMap = WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<AnimationPlayer>>;
206 AnimationMap m_animations;
208 using TransitionMap = WillBeHeapHashMap<CSSPropertyID, RunningTransition>;
209 TransitionMap m_transitions;
211 OwnPtrWillBeMember<CSSAnimationUpdate> m_pendingUpdate;
213 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation>> m_previousActiveInterpolationsForAnimations;
215 static void calculateAnimationUpdate(CSSAnimationUpdate*, const Element* animatingElement, Element&, const RenderStyle&, RenderStyle* parentStyle, StyleResolver*);
216 static void calculateTransitionUpdate(CSSAnimationUpdate*, const Element* animatingElement, const RenderStyle&);
217 static void calculateTransitionUpdateForProperty(CSSPropertyID, CSSPropertyID eventId, const CSSTransitionData&, size_t transitionIndex, const RenderStyle& oldStyle, const RenderStyle&, const TransitionMap* activeTransitions, CSSAnimationUpdate*, const Element*);
219 static void calculateAnimationActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime);
220 static void calculateTransitionActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime);
222 class AnimationEventDelegate final : public AnimationNode::EventDelegate {
224 AnimationEventDelegate(Element* target, const AtomicString& name)
227 , m_previousPhase(AnimationNode::PhaseNone)
228 , m_previousIteration(nullValue())
231 virtual void onEventCondition(const AnimationNode*) override;
232 virtual void trace(Visitor*) override;
235 void maybeDispatch(Document::ListenerType, const AtomicString& eventName, double elapsedTime);
236 RawPtrWillBeMember<Element> m_target;
237 const AtomicString m_name;
238 AnimationNode::Phase m_previousPhase;
239 double m_previousIteration;
242 class TransitionEventDelegate final : public AnimationNode::EventDelegate {
244 TransitionEventDelegate(Element* target, CSSPropertyID property)
246 , m_property(property)
247 , m_previousPhase(AnimationNode::PhaseNone)
250 virtual void onEventCondition(const AnimationNode*) override;
251 virtual void trace(Visitor*) override;
254 RawPtrWillBeMember<Element> m_target;
255 const CSSPropertyID m_property;
256 AnimationNode::Phase m_previousPhase;
262 WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(blink::CSSAnimationUpdate::NewAnimation);