1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #ifndef CSSAnimations_h 32 #define CSSAnimations_h 33 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" 44 45 namespace blink { 46 47 class CSSTransitionData; 48 class Element; 49 class StylePropertyShorthand; 50 class StyleResolver; 51 class StyleRuleKeyframes; 52 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> { 56 public: startAnimation(AtomicString & animationName,PassRefPtrWillBeRawPtr<InertAnimation> animation)57 void startAnimation(AtomicString& animationName, PassRefPtrWillBeRawPtr<InertAnimation> animation) 58 { 59 animation->setName(animationName); 60 NewAnimation newAnimation; 61 newAnimation.name = animationName; 62 newAnimation.animation = animation; 63 m_newAnimations.append(newAnimation); 64 } 65 // Returns whether player has been cancelled and should be filtered during style application. isCancelledAnimation(const AnimationPlayer * player)66 bool isCancelledAnimation(const AnimationPlayer* player) const { return m_cancelledAnimationPlayers.contains(player); } cancelAnimation(const AtomicString & name,AnimationPlayer & player)67 void cancelAnimation(const AtomicString& name, AnimationPlayer& player) 68 { 69 m_cancelledAnimationNames.append(name); 70 m_cancelledAnimationPlayers.add(&player); 71 } toggleAnimationPaused(const AtomicString & name)72 void toggleAnimationPaused(const AtomicString& name) 73 { 74 m_animationsWithPauseToggled.append(name); 75 } 76 startTransition(CSSPropertyID id,CSSPropertyID eventId,const AnimatableValue * from,const AnimatableValue * to,PassRefPtrWillBeRawPtr<InertAnimation> animation)77 void startTransition(CSSPropertyID id, CSSPropertyID eventId, const AnimatableValue* from, const AnimatableValue* to, PassRefPtrWillBeRawPtr<InertAnimation> animation) 78 { 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); 87 } isCancelledTransition(CSSPropertyID id)88 bool isCancelledTransition(CSSPropertyID id) const { return m_cancelledTransitions.contains(id); } cancelTransition(CSSPropertyID id)89 void cancelTransition(CSSPropertyID id) { m_cancelledTransitions.add(id); } 90 91 struct NewAnimation { 92 ALLOW_ONLY_INLINE_ALLOCATION(); 93 public: traceNewAnimation94 void trace(Visitor* visitor) 95 { 96 visitor->trace(animation); 97 } 98 99 AtomicString name; 100 RefPtrWillBeMember<InertAnimation> animation; 101 }; newAnimations()102 const WillBeHeapVector<NewAnimation>& newAnimations() const { return m_newAnimations; } cancelledAnimationNames()103 const Vector<AtomicString>& cancelledAnimationNames() const { return m_cancelledAnimationNames; } cancelledAnimationAnimationPlayers()104 const WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> >& cancelledAnimationAnimationPlayers() const { return m_cancelledAnimationPlayers; } animationsWithPauseToggled()105 const Vector<AtomicString>& animationsWithPauseToggled() const { return m_animationsWithPauseToggled; } 106 107 struct NewTransition { 108 ALLOW_ONLY_INLINE_ALLOCATION(); 109 public: traceNewTransition110 void trace(Visitor* visitor) 111 { 112 visitor->trace(from); 113 visitor->trace(to); 114 visitor->trace(animation); 115 } 116 117 CSSPropertyID id; 118 CSSPropertyID eventId; 119 RawPtrWillBeMember<const AnimatableValue> from; 120 RawPtrWillBeMember<const AnimatableValue> to; 121 RefPtrWillBeMember<InertAnimation> animation; 122 }; 123 typedef WillBeHeapHashMap<CSSPropertyID, NewTransition> NewTransitionMap; newTransitions()124 const NewTransitionMap& newTransitions() const { return m_newTransitions; } cancelledTransitions()125 const HashSet<CSSPropertyID>& cancelledTransitions() const { return m_cancelledTransitions; } 126 adoptActiveInterpolationsForAnimations(WillBeHeapHashMap<CSSPropertyID,RefPtrWillBeMember<Interpolation>> & newMap)127 void adoptActiveInterpolationsForAnimations(WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& newMap) { newMap.swap(m_activeInterpolationsForAnimations); } adoptActiveInterpolationsForTransitions(WillBeHeapHashMap<CSSPropertyID,RefPtrWillBeMember<Interpolation>> & newMap)128 void adoptActiveInterpolationsForTransitions(WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& newMap) { newMap.swap(m_activeInterpolationsForTransitions); } activeInterpolationsForAnimations()129 const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForAnimations() const { return m_activeInterpolationsForAnimations; } activeInterpolationsForTransitions()130 const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForTransitions() const { return m_activeInterpolationsForTransitions; } activeInterpolationsForAnimations()131 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForAnimations() { return m_activeInterpolationsForAnimations; } 132 isEmpty()133 bool isEmpty() const 134 { 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(); 143 } 144 145 void trace(Visitor*); 146 147 private: 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; 156 157 NewTransitionMap m_newTransitions; 158 HashSet<CSSPropertyID> m_cancelledTransitions; 159 160 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_activeInterpolationsForAnimations; 161 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_activeInterpolationsForTransitions; 162 }; 163 164 class CSSAnimations FINAL { 165 WTF_MAKE_NONCOPYABLE(CSSAnimations); 166 DISALLOW_ALLOCATION(); 167 public: 168 CSSAnimations(); 169 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*); 174 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*); 179 setPendingUpdate(PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> update)180 void setPendingUpdate(PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> update) { m_pendingUpdate = update; } 181 void maybeApplyPendingUpdate(Element*); isEmpty()182 bool isEmpty() const { return m_animations.isEmpty() && m_transitions.isEmpty() && !m_pendingUpdate; } 183 void cancel(); 184 185 void trace(Visitor*); 186 187 private: 188 struct RunningTransition { 189 ALLOW_ONLY_INLINE_ALLOCATION(); 190 public: traceRunningTransition191 void trace(Visitor* visitor) 192 { 193 visitor->trace(from); 194 visitor->trace(to); 195 visitor->trace(player); 196 } 197 198 RefPtrWillBeMember<AnimationPlayer> player; 199 RawPtrWillBeMember<const AnimatableValue> from; 200 RawPtrWillBeMember<const AnimatableValue> to; 201 }; 202 203 typedef WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<AnimationPlayer> > AnimationMap; 204 AnimationMap m_animations; 205 206 typedef WillBeHeapHashMap<CSSPropertyID, RunningTransition> TransitionMap; 207 TransitionMap m_transitions; 208 209 OwnPtrWillBeMember<CSSAnimationUpdate> m_pendingUpdate; 210 211 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_previousActiveInterpolationsForAnimations; 212 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*); 216 217 static void calculateAnimationActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime); 218 static void calculateTransitionActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime); 219 220 class AnimationEventDelegate FINAL : public AnimationNode::EventDelegate { 221 public: AnimationEventDelegate(Element * target,const AtomicString & name)222 AnimationEventDelegate(Element* target, const AtomicString& name) 223 : m_target(target) 224 , m_name(name) 225 , m_previousPhase(AnimationNode::PhaseNone) 226 , m_previousIteration(nullValue()) 227 { 228 } 229 virtual void onEventCondition(const AnimationNode*) OVERRIDE; 230 virtual void trace(Visitor*) OVERRIDE; 231 232 private: 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; 238 }; 239 240 class TransitionEventDelegate FINAL : public AnimationNode::EventDelegate { 241 public: TransitionEventDelegate(Element * target,CSSPropertyID property)242 TransitionEventDelegate(Element* target, CSSPropertyID property) 243 : m_target(target) 244 , m_property(property) 245 , m_previousPhase(AnimationNode::PhaseNone) 246 { 247 } 248 virtual void onEventCondition(const AnimationNode*) OVERRIDE; 249 virtual void trace(Visitor*) OVERRIDE; 250 251 private: 252 RawPtrWillBeMember<Element> m_target; 253 const CSSPropertyID m_property; 254 AnimationNode::Phase m_previousPhase; 255 }; 256 }; 257 258 } // namespace blink 259 260 WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(blink::CSSAnimationUpdate::NewAnimation); 261 262 #endif 263