2 * Copyright (C) 2007 Apple 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "core/events/ThreadLocalEventNames.h"
32 #include "core/frame/UseCounter.h"
33 #include "core/frame/animation/AnimationControllerPrivate.h"
34 #include "core/frame/animation/CSSPropertyAnimation.h"
35 #include "core/frame/animation/CompositeAnimation.h"
36 #include "core/frame/animation/ImplicitAnimation.h"
37 #include "core/frame/animation/KeyframeAnimation.h"
38 #include "core/rendering/RenderBoxModelObject.h"
39 #include "public/platform/Platform.h"
43 ImplicitAnimation::ImplicitAnimation(const CSSAnimationData* transition, CSSPropertyID animatingProperty, RenderObject& renderer, CompositeAnimation* compAnim, RenderStyle* fromStyle)
44 : AnimationBase(transition, renderer, compAnim)
45 , m_transitionProperty(transition->property())
46 , m_animatingProperty(animatingProperty)
49 , m_fromStyle(fromStyle)
51 ASSERT(animatingProperty != CSSPropertyInvalid);
52 blink::Platform::current()->histogramSparse("WebCore.Animation.CSSProperties", UseCounter::mapCSSPropertyIdToCSSSampleIdForHistogram(m_animatingProperty));
55 ImplicitAnimation::~ImplicitAnimation()
57 // // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed.
62 bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const
64 return m_object->document().hasListenerType(inListenerType);
67 void ImplicitAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
69 // If we get this far and the animation is done, it means we are cleaning up a just finished animation.
70 // So just return. Everything is already all cleaned up.
74 // Reset to start the transition if we are new
78 // Run a cycle of animation.
79 // We know we will need a new render style, so make one if needed
81 animatedStyle = RenderStyle::clone(targetStyle);
83 bool needsAnim = CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
84 // FIXME: we also need to detect cases where we have to software animate for other reasons,
85 // such as a child using inheriting the transform. https://bugs.webkit.org/show_bug.cgi?id=23902
87 // If we are running an accelerated animation, set a flag in the style which causes the style
88 // to compare as different to any other style. This ensures that changes to the property
89 // that is animating are correctly detected during the animation (e.g. when a transition
91 animatedStyle->setIsRunningAcceleratedAnimation();
93 // Fire the start timeout if needed
94 fireAnimationEventsIfNeeded();
97 void ImplicitAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle)
100 animatedStyle = RenderStyle::clone(m_toStyle.get());
102 CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
105 void ImplicitAnimation::startAnimation(double timeOffset)
107 if (m_object && m_object->compositingState() == PaintsIntoOwnBacking)
108 m_isAccelerated = toRenderBoxModelObject(m_object)->startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get());
111 void ImplicitAnimation::pauseAnimation(double timeOffset)
116 if (m_object && m_object->compositingState() == PaintsIntoOwnBacking && isAccelerated())
117 toRenderBoxModelObject(m_object)->transitionPaused(timeOffset, m_animatingProperty);
119 // Restore the original (unanimated) style
121 setNeedsStyleRecalc(m_object->node());
124 void ImplicitAnimation::endAnimation()
126 if (m_object && m_object->compositingState() == PaintsIntoOwnBacking && isAccelerated())
127 toRenderBoxModelObject(m_object)->transitionFinished(m_animatingProperty);
128 m_isAccelerated = false;
131 void ImplicitAnimation::onAnimationEnd(double elapsedTime)
133 // If we have a keyframe animation on this property, this transition is being overridden. The keyframe
134 // animation keeps an unanimated style in case a transition starts while the keyframe animation is
135 // running. But now that the transition has completed, we need to update this style with its new
136 // destination. If we didn't, the next time through we would think a transition had started
137 // (comparing the old unanimated style with the new final style of the transition).
138 RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty);
140 keyframeAnim->setUnanimatedStyle(m_toStyle);
142 sendTransitionEvent(EventTypeNames::transitionend, elapsedTime);
146 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
148 if (eventType == EventTypeNames::transitionend) {
149 Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
151 if (shouldSendEventForListener(listenerType)) {
152 String propertyName = getPropertyNameString(m_animatingProperty);
154 // Dispatch the event
155 RefPtr<Element> element = 0;
156 if (m_object->node() && m_object->node()->isElementNode())
157 element = toElement(m_object->node());
162 // Schedule event handling
163 m_compAnim->animationController()->addEventToDispatch(element, eventType, propertyName, elapsedTime);
165 // Restore the original (unanimated) style
166 if (eventType == EventTypeNames::transitionend && element->renderer())
167 setNeedsStyleRecalc(element.get());
169 return true; // Did dispatch an event
173 return false; // Didn't dispatch an event
176 void ImplicitAnimation::reset(RenderStyle* to)
183 // Restart the transition
184 if (m_fromStyle && m_toStyle)
185 updateStateMachine(AnimationStateInputRestartAnimation, -1);
187 // set the transform animation list
188 validateTransformFunctionList();
189 checkForMatchingFilterFunctionLists();
192 void ImplicitAnimation::setOverridden(bool b)
194 if (b == m_overridden)
198 updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1);
201 bool ImplicitAnimation::affectsProperty(CSSPropertyID property) const
203 return (m_animatingProperty == property);
206 bool ImplicitAnimation::isTargetPropertyEqual(CSSPropertyID prop, const RenderStyle* targetStyle)
208 // We can get here for a transition that has not started yet. This would make m_toStyle unset and null.
209 // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>)
212 return CSSPropertyAnimation::propertiesEqual(prop, m_toStyle.get(), targetStyle);
215 void ImplicitAnimation::blendPropertyValueInStyle(CSSPropertyID prop, RenderStyle* currentStyle)
217 // We should never add a transition with a 0 duration and delay. But if we ever did
218 // it would have a null toStyle. So just in case, let's check that here. (See
219 // <https://bugs.webkit.org/show_bug.cgi?id=24787>
223 CSSPropertyAnimation::blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
226 void ImplicitAnimation::validateTransformFunctionList()
228 m_transformFunctionListValid = false;
230 if (!m_fromStyle || !m_toStyle)
233 const TransformOperations* val = &m_fromStyle->transform();
234 const TransformOperations* toVal = &m_toStyle->transform();
236 if (val->operations().isEmpty())
239 if (val->operations().isEmpty())
242 // An emtpy transform list matches anything.
243 if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
246 // Transform lists match.
247 m_transformFunctionListValid = true;
250 void ImplicitAnimation::checkForMatchingFilterFunctionLists()
252 m_filterFunctionListsMatch = false;
254 if (!m_fromStyle || !m_toStyle)
257 const FilterOperations* val = &m_fromStyle->filter();
258 const FilterOperations* toVal = &m_toStyle->filter();
260 if (!val->canInterpolateWith(*toVal))
263 // Filter lists match.
264 m_filterFunctionListsMatch = true;
267 double ImplicitAnimation::timeToNextService()
269 double t = AnimationBase::timeToNextService();
270 if (t != 0 || preActive())
273 // A return value of 0 means we need service. But if this is an accelerated animation we
274 // only need service at the end of the transition.
275 if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) {
277 getTimeToNextEvent(t, isLooping);
283 } // namespace WebCore