Upstream version 7.36.149.0
[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 "bindings/v8/Dictionary.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "core/animation/ActiveAnimations.h"
37 #include "core/animation/AnimationHelpers.h"
38 #include "core/animation/AnimationPlayer.h"
39 #include "core/animation/CompositorAnimations.h"
40 #include "core/animation/DocumentTimeline.h"
41 #include "core/animation/Interpolation.h"
42 #include "core/animation/KeyframeEffectModel.h"
43 #include "core/dom/Element.h"
44 #include "core/frame/UseCounter.h"
45 #include "core/rendering/RenderLayer.h"
46
47 namespace WebCore {
48
49 PassRefPtr<Animation> Animation::create(Element* target, PassRefPtrWillBeRawPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtr<EventDelegate> eventDelegate)
50 {
51     return adoptRef(new Animation(target, effect, timing, priority, eventDelegate));
52 }
53
54 PassRefPtr<Animation> Animation::create(Element* element, PassRefPtrWillBeRawPtr<AnimationEffect> effect, const Dictionary& timingInputDictionary)
55 {
56     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
57     return create(element, effect, TimingInput::convert(timingInputDictionary));
58 }
59 PassRefPtr<Animation> Animation::create(Element* element, PassRefPtrWillBeRawPtr<AnimationEffect> effect, double duration)
60 {
61     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
62     return create(element, effect, TimingInput::convert(duration));
63 }
64 PassRefPtr<Animation> Animation::create(Element* element, PassRefPtrWillBeRawPtr<AnimationEffect> effect)
65 {
66     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
67     return create(element, effect, Timing());
68 }
69 PassRefPtr<Animation> Animation::create(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, const Dictionary& timingInputDictionary, ExceptionState& exceptionState)
70 {
71     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
72     if (element)
73         UseCounter::count(element->document(), UseCounter::AnimationConstructorKeyframeListEffectObjectTiming);
74     return create(element, EffectInput::convert(element, keyframeDictionaryVector, exceptionState), TimingInput::convert(timingInputDictionary));
75 }
76 PassRefPtr<Animation> Animation::create(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, double duration, ExceptionState& exceptionState)
77 {
78     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
79     if (element)
80         UseCounter::count(element->document(), UseCounter::AnimationConstructorKeyframeListEffectDoubleTiming);
81     return create(element, EffectInput::convert(element, keyframeDictionaryVector, exceptionState), TimingInput::convert(duration));
82 }
83 PassRefPtr<Animation> Animation::create(Element* element, const Vector<Dictionary>& keyframeDictionaryVector, ExceptionState& exceptionState)
84 {
85     ASSERT(RuntimeEnabledFeatures::webAnimationsAPIEnabled());
86     if (element)
87         UseCounter::count(element->document(), UseCounter::AnimationConstructorKeyframeListEffectNoTiming);
88     return create(element, EffectInput::convert(element, keyframeDictionaryVector, exceptionState), Timing());
89 }
90
91 Animation::Animation(Element* target, PassRefPtrWillBeRawPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtr<EventDelegate> eventDelegate)
92     : TimedItem(timing, eventDelegate)
93     , m_target(target)
94     , m_effect(effect)
95     , m_sampledEffect(0)
96     , m_priority(priority)
97 {
98     if (m_target)
99         m_target->ensureActiveAnimations().addAnimation(this);
100 }
101
102 Animation::~Animation()
103 {
104     if (m_target)
105         m_target->activeAnimations()->notifyAnimationDestroyed(this);
106 }
107
108 void Animation::didAttach()
109 {
110     if (m_target) {
111         m_target->ensureActiveAnimations().addPlayer(player());
112         m_target->setNeedsAnimationStyleRecalc();
113     }
114 }
115
116 void Animation::willDetach()
117 {
118     if (m_target)
119         m_target->activeAnimations()->removePlayer(player());
120     if (m_sampledEffect)
121         clearEffects();
122 }
123
124 void Animation::specifiedTimingChanged()
125 {
126     cancelAnimationOnCompositor();
127     if (player()) {
128         // FIXME: Needs to consider groups when added.
129         ASSERT(player()->source() == this);
130         player()->schedulePendingAnimationOnCompositor();
131     }
132 }
133
134 static AnimationStack& ensureAnimationStack(Element* element)
135 {
136     return element->ensureActiveAnimations().defaultStack();
137 }
138
139 void Animation::applyEffects()
140 {
141     ASSERT(isInEffect());
142     ASSERT(player());
143     if (!m_target || !m_effect)
144         return;
145
146     double iteration = currentIteration();
147     ASSERT(iteration >= 0);
148     // FIXME: Handle iteration values which overflow int.
149     OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > interpolations = m_effect->sample(static_cast<int>(iteration), timeFraction(), iterationDuration());
150     if (m_sampledEffect) {
151         m_sampledEffect->setInterpolations(interpolations.release());
152     } else if (!interpolations->isEmpty()) {
153         OwnPtr<SampledEffect> sampledEffect = SampledEffect::create(this, interpolations.release());
154         m_sampledEffect = sampledEffect.get();
155         ensureAnimationStack(m_target).add(sampledEffect.release());
156     } else {
157         return;
158     }
159
160     m_target->setNeedsAnimationStyleRecalc();
161 }
162
163 void Animation::clearEffects()
164 {
165     ASSERT(player());
166     ASSERT(m_sampledEffect);
167
168     m_sampledEffect->clear();
169     m_sampledEffect = 0;
170     cancelAnimationOnCompositor();
171     m_target->setNeedsAnimationStyleRecalc();
172     invalidate();
173 }
174
175 void Animation::updateChildrenAndEffects() const
176 {
177     if (!m_effect)
178         return;
179     if (isInEffect())
180         const_cast<Animation*>(this)->applyEffects();
181     else if (m_sampledEffect)
182         const_cast<Animation*>(this)->clearEffects();
183 }
184
185 double Animation::calculateTimeToEffectChange(bool forwards, double localTime, double timeToNextIteration) const
186 {
187     const double start = startTimeInternal() + specifiedTiming().startDelay;
188     const double end = start + activeDurationInternal();
189
190     switch (phase()) {
191     case PhaseBefore:
192         ASSERT(start >= localTime);
193         return forwards
194             ? start - localTime
195             : std::numeric_limits<double>::infinity();
196     case PhaseActive:
197         if (forwards && hasActiveAnimationsOnCompositor()) {
198             ASSERT(specifiedTiming().playbackRate == 1);
199             // Need service to apply fill / fire events.
200             const double timeToEnd = end - localTime;
201             if (hasEvents()) {
202                 return std::min(timeToEnd, timeToNextIteration);
203             } else {
204                 return timeToEnd;
205             }
206         }
207         return 0;
208     case PhaseAfter:
209         ASSERT(localTime >= end);
210         // If this Animation is still in effect then it will need to update
211         // when its parent goes out of effect. We have no way of knowing when
212         // that will be, however, so the parent will need to supply it.
213         return forwards
214             ? std::numeric_limits<double>::infinity()
215             : localTime - end;
216     case PhaseNone:
217         ASSERT(player() && player()->timeline() && !player()->timeline()->hasStarted());
218         return std::numeric_limits<double>::infinity();
219     default:
220         ASSERT_NOT_REACHED();
221         return std::numeric_limits<double>::infinity();
222     }
223 }
224
225 void Animation::notifySampledEffectRemovedFromAnimationStack()
226 {
227     ASSERT(m_sampledEffect);
228     m_sampledEffect = 0;
229 }
230
231 void Animation::notifyElementDestroyed()
232 {
233     // If our player is kept alive just by the sampledEffect, we might get our
234     // destructor called when we call SampledEffect::clear(), so we need to
235     // clear m_sampledEffect first.
236     m_target = 0;
237     clearEventDelegate();
238     SampledEffect* sampledEffect = m_sampledEffect;
239     m_sampledEffect = 0;
240     if (sampledEffect)
241         sampledEffect->clear();
242 }
243
244 bool Animation::isCandidateForAnimationOnCompositor() const
245 {
246     if (!effect() || !m_target)
247         return false;
248     return CompositorAnimations::instance()->isCandidateForAnimationOnCompositor(specifiedTiming(), *effect());
249 }
250
251 bool Animation::maybeStartAnimationOnCompositor(double startTime)
252 {
253     ASSERT(!hasActiveAnimationsOnCompositor());
254     if (!isCandidateForAnimationOnCompositor())
255         return false;
256     if (!CompositorAnimations::instance()->canStartAnimationOnCompositor(*m_target))
257         return false;
258     if (!CompositorAnimations::instance()->startAnimationOnCompositor(*m_target, startTime, specifiedTiming(), *effect(), m_compositorAnimationIds))
259         return false;
260     ASSERT(!m_compositorAnimationIds.isEmpty());
261     return true;
262 }
263
264 bool Animation::hasActiveAnimationsOnCompositor() const
265 {
266     return !m_compositorAnimationIds.isEmpty();
267 }
268
269 bool Animation::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
270 {
271     return hasActiveAnimationsOnCompositor() && affects(property);
272 }
273
274 bool Animation::affects(CSSPropertyID property) const
275 {
276     return m_effect && m_effect->affects(property);
277 }
278
279 void Animation::cancelAnimationOnCompositor()
280 {
281     // FIXME: cancelAnimationOnCompositor is called from withins style recalc.
282     // This queries compositingState, which is not necessarily up to date.
283     // https://code.google.com/p/chromium/issues/detail?id=339847
284     DisableCompositingQueryAsserts disabler;
285     if (!hasActiveAnimationsOnCompositor())
286         return;
287     if (!m_target || !m_target->renderer())
288         return;
289     for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
290         CompositorAnimations::instance()->cancelAnimationOnCompositor(*m_target, m_compositorAnimationIds[i]);
291     m_compositorAnimationIds.clear();
292 }
293
294 void Animation::pauseAnimationForTestingOnCompositor(double pauseTime)
295 {
296     ASSERT(hasActiveAnimationsOnCompositor());
297     if (!m_target || !m_target->renderer())
298         return;
299     for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
300         CompositorAnimations::instance()->pauseAnimationForTestingOnCompositor(*m_target, m_compositorAnimationIds[i], pauseTime);
301 }
302
303 } // namespace WebCore