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/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 WebCore { 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 NewAnimation newAnimation; 60 newAnimation.name = animationName; 61 newAnimation.animation = animation; 62 m_newAnimations.append(newAnimation); 63 } 64 // Returns whether player has been cancelled and should be filtered during style application. isCancelledAnimation(const AnimationPlayer * player)65 bool isCancelledAnimation(const AnimationPlayer* player) const { return m_cancelledAnimationPlayers.contains(player); } cancelAnimation(const AtomicString & name,AnimationPlayer & player)66 void cancelAnimation(const AtomicString& name, AnimationPlayer& player) 67 { 68 m_cancelledAnimationNames.append(name); 69 m_cancelledAnimationPlayers.add(&player); 70 } toggleAnimationPaused(const AtomicString & name)71 void toggleAnimationPaused(const AtomicString& name) 72 { 73 m_animationsWithPauseToggled.append(name); 74 } 75 startTransition(CSSPropertyID id,CSSPropertyID eventId,const AnimatableValue * from,const AnimatableValue * to,PassRefPtrWillBeRawPtr<InertAnimation> animation)76 void startTransition(CSSPropertyID id, CSSPropertyID eventId, const AnimatableValue* from, const AnimatableValue* to, PassRefPtrWillBeRawPtr<InertAnimation> animation) 77 { 78 NewTransition newTransition; 79 newTransition.id = id; 80 newTransition.eventId = eventId; 81 newTransition.from = from; 82 newTransition.to = to; 83 newTransition.animation = animation; 84 m_newTransitions.set(id, newTransition); 85 } isCancelledTransition(CSSPropertyID id)86 bool isCancelledTransition(CSSPropertyID id) const { return m_cancelledTransitions.contains(id); } cancelTransition(CSSPropertyID id)87 void cancelTransition(CSSPropertyID id) { m_cancelledTransitions.add(id); } 88 89 struct NewAnimation { 90 ALLOW_ONLY_INLINE_ALLOCATION(); 91 public: traceNewAnimation92 void trace(Visitor* visitor) 93 { 94 visitor->trace(animation); 95 } 96 97 AtomicString name; 98 RefPtrWillBeMember<InertAnimation> animation; 99 }; newAnimations()100 const WillBeHeapVector<NewAnimation>& newAnimations() const { return m_newAnimations; } cancelledAnimationNames()101 const Vector<AtomicString>& cancelledAnimationNames() const { return m_cancelledAnimationNames; } cancelledAnimationAnimationPlayers()102 const WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> >& cancelledAnimationAnimationPlayers() const { return m_cancelledAnimationPlayers; } animationsWithPauseToggled()103 const Vector<AtomicString>& animationsWithPauseToggled() const { return m_animationsWithPauseToggled; } 104 105 struct NewTransition { 106 ALLOW_ONLY_INLINE_ALLOCATION(); 107 public: traceNewTransition108 void trace(Visitor* visitor) 109 { 110 visitor->trace(from); 111 visitor->trace(to); 112 visitor->trace(animation); 113 } 114 115 CSSPropertyID id; 116 CSSPropertyID eventId; 117 RawPtrWillBeMember<const AnimatableValue> from; 118 RawPtrWillBeMember<const AnimatableValue> to; 119 RefPtrWillBeMember<InertAnimation> animation; 120 }; 121 typedef WillBeHeapHashMap<CSSPropertyID, NewTransition> NewTransitionMap; newTransitions()122 const NewTransitionMap& newTransitions() const { return m_newTransitions; } cancelledTransitions()123 const HashSet<CSSPropertyID>& cancelledTransitions() const { return m_cancelledTransitions; } 124 adoptActiveInterpolationsForAnimations(WillBeHeapHashMap<CSSPropertyID,RefPtrWillBeMember<Interpolation>> & newMap)125 void adoptActiveInterpolationsForAnimations(WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& newMap) { newMap.swap(m_activeInterpolationsForAnimations); } adoptActiveInterpolationsForTransitions(WillBeHeapHashMap<CSSPropertyID,RefPtrWillBeMember<Interpolation>> & newMap)126 void adoptActiveInterpolationsForTransitions(WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& newMap) { newMap.swap(m_activeInterpolationsForTransitions); } activeInterpolationsForAnimations()127 const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForAnimations() const { return m_activeInterpolationsForAnimations; } activeInterpolationsForTransitions()128 const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForTransitions() const { return m_activeInterpolationsForTransitions; } activeInterpolationsForAnimations()129 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& activeInterpolationsForAnimations() { return m_activeInterpolationsForAnimations; } 130 isEmpty()131 bool isEmpty() const 132 { 133 return m_newAnimations.isEmpty() 134 && m_cancelledAnimationNames.isEmpty() 135 && m_cancelledAnimationPlayers.isEmpty() 136 && m_animationsWithPauseToggled.isEmpty() 137 && m_newTransitions.isEmpty() 138 && m_cancelledTransitions.isEmpty() 139 && m_activeInterpolationsForAnimations.isEmpty() 140 && m_activeInterpolationsForTransitions.isEmpty(); 141 } 142 143 void trace(Visitor*); 144 145 private: 146 // Order is significant since it defines the order in which new animations 147 // will be started. Note that there may be multiple animations present 148 // with the same name, due to the way in which we split up animations with 149 // incomplete keyframes. 150 WillBeHeapVector<NewAnimation> m_newAnimations; 151 Vector<AtomicString> m_cancelledAnimationNames; 152 WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> > m_cancelledAnimationPlayers; 153 Vector<AtomicString> m_animationsWithPauseToggled; 154 155 NewTransitionMap m_newTransitions; 156 HashSet<CSSPropertyID> m_cancelledTransitions; 157 158 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_activeInterpolationsForAnimations; 159 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > m_activeInterpolationsForTransitions; 160 }; 161 162 class CSSAnimations FINAL { 163 WTF_MAKE_NONCOPYABLE(CSSAnimations); 164 DISALLOW_ALLOCATION(); 165 public: 166 CSSAnimations(); 167 168 // FIXME: This method is only used here and in the legacy animations 169 // implementation. It should be made private or file-scope when the legacy 170 // engine is removed. 171 static const StyleRuleKeyframes* matchScopedKeyframesRule(StyleResolver*, const Element*, const StringImpl*); 172 173 static bool isAnimatableProperty(CSSPropertyID); 174 static const StylePropertyShorthand& animatableProperties(); 175 static bool isAllowedAnimation(CSSPropertyID); 176 // FIXME: This should take a const ScopedStyleTree instead of a StyleResolver. 177 // We should also change the Element* to a const Element* 178 static PassOwnPtrWillBeRawPtr<CSSAnimationUpdate> calculateUpdate(Element*, const Element& parentElement, 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*, Element*, const Element& parentElement, const RenderStyle&, RenderStyle* parentStyle, StyleResolver*); 214 static void calculateTransitionUpdate(CSSAnimationUpdate*, const Element*, 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*, double timelineCurrentTime); 218 static void calculateTransitionActiveInterpolations(CSSAnimationUpdate*, const Element*, 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 private: 231 void maybeDispatch(Document::ListenerType, const AtomicString& eventName, double elapsedTime); 232 Element* m_target; 233 const AtomicString m_name; 234 AnimationNode::Phase m_previousPhase; 235 double m_previousIteration; 236 }; 237 238 class TransitionEventDelegate FINAL : public AnimationNode::EventDelegate { 239 public: TransitionEventDelegate(Element * target,CSSPropertyID property)240 TransitionEventDelegate(Element* target, CSSPropertyID property) 241 : m_target(target) 242 , m_property(property) 243 , m_previousPhase(AnimationNode::PhaseNone) 244 { 245 } 246 virtual void onEventCondition(const AnimationNode*) OVERRIDE; 247 private: 248 Element* m_target; 249 const CSSPropertyID m_property; 250 AnimationNode::Phase m_previousPhase; 251 }; 252 }; 253 254 } // namespace WebCore 255 256 namespace WTF { 257 template<> struct VectorTraits<WebCore::CSSAnimationUpdate::NewAnimation> : VectorTraitsBase<WebCore::CSSAnimationUpdate::NewAnimation> { 258 static const bool canInitializeWithMemset = true; 259 }; 260 } 261 262 #endif 263