228b52edebf899d616dd21a4d5dd7b02063400fc
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / Animation.cpp
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 #include "config.h"
32 #include "core/animation/Animation.h"
33
34 #include "core/animation/ActiveAnimations.h"
35 #include "core/animation/CompositorAnimations.h"
36 #include "core/animation/Player.h"
37 #include "core/dom/Element.h"
38
39 namespace WebCore {
40
41 PassRefPtr<Animation> Animation::create(PassRefPtr<Element> target, PassRefPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtr<EventDelegate> eventDelegate)
42 {
43     return adoptRef(new Animation(target, effect, timing, priority, eventDelegate));
44 }
45
46 Animation::Animation(PassRefPtr<Element> target, PassRefPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtr<EventDelegate> eventDelegate)
47     : TimedItem(timing, eventDelegate)
48     , m_target(target)
49     , m_effect(effect)
50     , m_activeInAnimationStack(false)
51     , m_priority(priority)
52 {
53 }
54
55 void Animation::didAttach()
56 {
57     if (m_target)
58         m_target->ensureActiveAnimations()->players().add(player());
59 }
60
61 void Animation::willDetach()
62 {
63     if (m_target)
64         m_target->activeAnimations()->players().remove(player());
65     if (m_activeInAnimationStack)
66         clearEffects();
67 }
68
69 static AnimationStack& ensureAnimationStack(Element* element)
70 {
71     return element->ensureActiveAnimations()->defaultStack();
72 }
73
74 bool Animation::applyEffects(bool previouslyInEffect)
75 {
76     ASSERT(isInEffect());
77     if (!m_target || !m_effect)
78         return false;
79
80     if (player() && !previouslyInEffect) {
81         ensureAnimationStack(m_target.get()).add(this);
82         m_activeInAnimationStack = true;
83     }
84
85     double iteration = currentIteration();
86     ASSERT(iteration >= 0);
87     // FIXME: Handle iteration values which overflow int.
88     m_compositableValues = m_effect->sample(static_cast<int>(iteration), timeFraction());
89     if (player()) {
90         m_target->setNeedsAnimationStyleRecalc();
91         return true;
92     }
93     return false;
94 }
95
96 void Animation::clearEffects()
97 {
98     ASSERT(player());
99     ASSERT(m_activeInAnimationStack);
100     ensureAnimationStack(m_target.get()).remove(this);
101     cancelAnimationOnCompositor();
102     m_activeInAnimationStack = false;
103     m_compositableValues.clear();
104     m_target->setNeedsAnimationStyleRecalc();
105     invalidate();
106 }
107
108 bool Animation::updateChildrenAndEffects() const
109 {
110     if (!m_effect)
111         return false;
112
113     if (isInEffect())
114         return const_cast<Animation*>(this)->applyEffects(m_activeInAnimationStack);
115
116     if (m_activeInAnimationStack) {
117         const_cast<Animation*>(this)->clearEffects();
118         return true;
119     }
120     return false;
121 }
122
123 double Animation::calculateTimeToEffectChange(double localTime, double timeToNextIteration) const
124 {
125     const double activeStartTime = startTime() + specified().startDelay;
126     switch (phase()) {
127     case PhaseBefore:
128         return activeStartTime - localTime;
129     case PhaseActive:
130         if (hasActiveAnimationsOnCompositor()) {
131             // Need service to apply fill / fire events.
132             const double activeEndTime = activeStartTime + activeDuration();
133             return std::min(activeEndTime - localTime, timeToNextIteration);
134         }
135         return 0;
136     case PhaseAfter:
137         // If this Animation is still in effect then it will need to update
138         // when its parent goes out of effect. We have no way of knowing when
139         // that will be, however, so the parent will need to supply it.
140         return std::numeric_limits<double>::infinity();
141     case PhaseNone:
142     default:
143         ASSERT_NOT_REACHED();
144         return 0;
145     }
146 }
147
148 bool Animation::isCandidateForAnimationOnCompositor() const
149 {
150     if (!effect() || !m_target)
151         return false;
152     return CompositorAnimations::instance()->isCandidateForAnimationOnCompositor(specified(), *effect());
153 }
154
155 bool Animation::maybeStartAnimationOnCompositor()
156 {
157     ASSERT(!hasActiveAnimationsOnCompositor());
158     if (!isCandidateForAnimationOnCompositor())
159         return false;
160     if (!CompositorAnimations::instance()->canStartAnimationOnCompositor(*m_target.get()))
161         return false;
162     if (!CompositorAnimations::instance()->startAnimationOnCompositor(*m_target.get(), specified(), *effect(), m_compositorAnimationIds))
163         return false;
164     ASSERT(!m_compositorAnimationIds.isEmpty());
165     return true;
166 }
167
168 bool Animation::hasActiveAnimationsOnCompositor() const
169 {
170     return !m_compositorAnimationIds.isEmpty();
171 }
172
173 bool Animation::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
174 {
175     return hasActiveAnimationsOnCompositor() && affects(property);
176 }
177
178 bool Animation::affects(CSSPropertyID property) const
179 {
180     return m_effect && m_effect->affects(property);
181 }
182
183 void Animation::cancelAnimationOnCompositor()
184 {
185     if (!hasActiveAnimationsOnCompositor())
186         return;
187     if (!m_target || !m_target->renderer())
188         return;
189     for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
190         CompositorAnimations::instance()->cancelAnimationOnCompositor(*m_target.get(), m_compositorAnimationIds[i]);
191     m_compositorAnimationIds.clear();
192 }
193
194 void Animation::pauseAnimationForTestingOnCompositor(double pauseTime)
195 {
196     ASSERT(hasActiveAnimationsOnCompositor());
197     if (!m_target || !m_target->renderer())
198         return;
199     for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
200         CompositorAnimations::instance()->pauseAnimationForTestingOnCompositor(*m_target.get(), m_compositorAnimationIds[i], pauseTime);
201 }
202
203 } // namespace WebCore