2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/Animation.h"
34 #include "core/animation/ActiveAnimations.h"
35 #include "core/animation/CompositorAnimations.h"
36 #include "core/animation/Player.h"
37 #include "core/dom/Element.h"
41 PassRefPtr<Animation> Animation::create(PassRefPtr<Element> target, PassRefPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtr<EventDelegate> eventDelegate)
43 return adoptRef(new Animation(target, effect, timing, priority, eventDelegate));
46 Animation::Animation(PassRefPtr<Element> target, PassRefPtr<AnimationEffect> effect, const Timing& timing, Priority priority, PassOwnPtr<EventDelegate> eventDelegate)
47 : TimedItem(timing, eventDelegate)
50 , m_activeInAnimationStack(false)
51 , m_priority(priority)
55 void Animation::didAttach()
58 m_target->ensureActiveAnimations()->players().add(player());
61 void Animation::willDetach()
64 m_target->activeAnimations()->players().remove(player());
65 if (m_activeInAnimationStack)
69 static AnimationStack& ensureAnimationStack(Element* element)
71 return element->ensureActiveAnimations()->defaultStack();
74 bool Animation::applyEffects(bool previouslyInEffect)
77 if (!m_target || !m_effect)
80 if (player() && !previouslyInEffect) {
81 ensureAnimationStack(m_target.get()).add(this);
82 m_activeInAnimationStack = true;
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());
90 m_target->setNeedsAnimationStyleRecalc();
96 void Animation::clearEffects()
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();
108 bool Animation::updateChildrenAndEffects() const
114 return const_cast<Animation*>(this)->applyEffects(m_activeInAnimationStack);
116 if (m_activeInAnimationStack) {
117 const_cast<Animation*>(this)->clearEffects();
123 double Animation::calculateTimeToEffectChange(double localTime, double timeToNextIteration) const
125 const double activeStartTime = startTime() + specified().startDelay;
128 return activeStartTime - localTime;
130 if (hasActiveAnimationsOnCompositor()) {
131 // Need service to apply fill / fire events.
132 const double activeEndTime = activeStartTime + activeDuration();
133 return std::min(activeEndTime - localTime, timeToNextIteration);
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();
143 ASSERT_NOT_REACHED();
148 bool Animation::isCandidateForAnimationOnCompositor() const
150 if (!effect() || !m_target)
152 return CompositorAnimations::instance()->isCandidateForAnimationOnCompositor(specified(), *effect());
155 bool Animation::maybeStartAnimationOnCompositor()
157 ASSERT(!hasActiveAnimationsOnCompositor());
158 if (!isCandidateForAnimationOnCompositor())
160 if (!CompositorAnimations::instance()->canStartAnimationOnCompositor(*m_target.get()))
162 if (!CompositorAnimations::instance()->startAnimationOnCompositor(*m_target.get(), specified(), *effect(), m_compositorAnimationIds))
164 ASSERT(!m_compositorAnimationIds.isEmpty());
168 bool Animation::hasActiveAnimationsOnCompositor() const
170 return !m_compositorAnimationIds.isEmpty();
173 bool Animation::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
175 return hasActiveAnimationsOnCompositor() && affects(property);
178 bool Animation::affects(CSSPropertyID property) const
180 return m_effect && m_effect->affects(property);
183 void Animation::cancelAnimationOnCompositor()
185 if (!hasActiveAnimationsOnCompositor())
187 if (!m_target || !m_target->renderer())
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();
194 void Animation::pauseAnimationForTestingOnCompositor(double pauseTime)
196 ASSERT(hasActiveAnimationsOnCompositor());
197 if (!m_target || !m_target->renderer())
199 for (size_t i = 0; i < m_compositorAnimationIds.size(); ++i)
200 CompositorAnimations::instance()->pauseAnimationForTestingOnCompositor(*m_target.get(), m_compositorAnimationIds[i], pauseTime);
203 } // namespace WebCore