Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / css / CSSAnimations.h
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:
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.
66     bool isCancelledAnimation(const AnimationPlayer* player) const { return m_cancelledAnimationPlayers.contains(player); }
67     void cancelAnimation(const AtomicString& name, AnimationPlayer& player)
68     {
69         m_cancelledAnimationNames.append(name);
70         m_cancelledAnimationPlayers.add(&player);
71     }
72     void toggleAnimationPaused(const AtomicString& name)
73     {
74         m_animationsWithPauseToggled.append(name);
75     }
76
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     }
88     bool isCancelledTransition(CSSPropertyID id) const { return m_cancelledTransitions.contains(id); }
89     void cancelTransition(CSSPropertyID id) { m_cancelledTransitions.add(id); }
90
91     struct NewAnimation {
92         ALLOW_ONLY_INLINE_ALLOCATION();
93     public:
94         void trace(Visitor* visitor)
95         {
96             visitor->trace(animation);
97         }
98
99         AtomicString name;
100         RefPtrWillBeMember<InertAnimation> animation;
101     };
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; }
106
107     struct NewTransition {
108         ALLOW_ONLY_INLINE_ALLOCATION();
109     public:
110         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     using NewTransitionMap = WillBeHeapHashMap<CSSPropertyID, NewTransition>;
124     const NewTransitionMap& newTransitions() const { return m_newTransitions; }
125     const HashSet<CSSPropertyID>& cancelledTransitions() const { return m_cancelledTransitions; }
126
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; }
132
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     const AtomicString getAnimationNameForInspector(const AnimationPlayer&);
171
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*);
176
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*);
181
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; }
185     void cancel();
186
187     void trace(Visitor*);
188
189 private:
190     struct RunningTransition {
191         ALLOW_ONLY_INLINE_ALLOCATION();
192     public:
193         void trace(Visitor* visitor)
194         {
195             visitor->trace(from);
196             visitor->trace(to);
197             visitor->trace(player);
198         }
199
200         RefPtrWillBeMember<AnimationPlayer> player;
201         RawPtrWillBeMember<const AnimatableValue> from;
202         RawPtrWillBeMember<const AnimatableValue> to;
203     };
204
205     using AnimationMap = WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<AnimationPlayer>>;
206     AnimationMap m_animations;
207
208     using TransitionMap = WillBeHeapHashMap<CSSPropertyID, RunningTransition>;
209     TransitionMap m_transitions;
210
211     OwnPtrWillBeMember<CSSAnimationUpdate> m_pendingUpdate;
212
213     WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation>> m_previousActiveInterpolationsForAnimations;
214
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*);
218
219     static void calculateAnimationActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime);
220     static void calculateTransitionActiveInterpolations(CSSAnimationUpdate*, const Element* animatingElement, double timelineCurrentTime);
221
222     class AnimationEventDelegate final : public AnimationNode::EventDelegate {
223     public:
224         AnimationEventDelegate(Element* target, const AtomicString& name)
225             : m_target(target)
226             , m_name(name)
227             , m_previousPhase(AnimationNode::PhaseNone)
228             , m_previousIteration(nullValue())
229         {
230         }
231         virtual void onEventCondition(const AnimationNode*) override;
232         virtual void trace(Visitor*) override;
233
234     private:
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;
240     };
241
242     class TransitionEventDelegate final : public AnimationNode::EventDelegate {
243     public:
244         TransitionEventDelegate(Element* target, CSSPropertyID property)
245             : m_target(target)
246             , m_property(property)
247             , m_previousPhase(AnimationNode::PhaseNone)
248         {
249         }
250         virtual void onEventCondition(const AnimationNode*) override;
251         virtual void trace(Visitor*) override;
252
253     private:
254         RawPtrWillBeMember<Element> m_target;
255         const CSSPropertyID m_property;
256         AnimationNode::Phase m_previousPhase;
257     };
258 };
259
260 } // namespace blink
261
262 WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(blink::CSSAnimationUpdate::NewAnimation);
263
264 #endif