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 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 "AnimationControllerPrivate.h"
32 #include "CSSPropertyAnimation.h"
33 #include "CompositeAnimation.h"
34 #include "EventNames.h"
35 #include "GeometryUtilities.h"
36 #include "ImplicitAnimation.h"
37 #include "KeyframeAnimation.h"
38 #include "RenderBox.h"
42 ImplicitAnimation::ImplicitAnimation(Animation& transition, CSSPropertyID animatingProperty, RenderElement* renderer, CompositeAnimation* compAnim, RenderStyle* fromStyle)
43 : AnimationBase(transition, renderer, compAnim)
44 , m_fromStyle(fromStyle)
45 , m_transitionProperty(transition.property())
46 , m_animatingProperty(animatingProperty)
48 ASSERT(animatingProperty != CSSPropertyInvalid);
51 ImplicitAnimation::~ImplicitAnimation()
53 // // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed.
58 bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const
60 return m_object->document().hasListenerType(inListenerType);
63 bool ImplicitAnimation::animate(CompositeAnimation*, RenderElement*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
65 // If we get this far and the animation is done, it means we are cleaning up a just finished animation.
66 // So just return. Everything is already all cleaned up.
70 AnimationState oldState = state();
72 // Reset to start the transition if we are new
76 // Run a cycle of animation.
77 // We know we will need a new render style, so make one if needed
79 animatedStyle = RenderStyle::clone(targetStyle);
81 bool needsAnim = CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
82 // FIXME: we also need to detect cases where we have to software animate for other reasons,
83 // such as a child using inheriting the transform. https://bugs.webkit.org/show_bug.cgi?id=23902
85 // If we are running an accelerated animation, set a flag in the style which causes the style
86 // to compare as different to any other style. This ensures that changes to the property
87 // that is animating are correctly detected during the animation (e.g. when a transition
89 // FIXME: still need this hack?
90 animatedStyle->setIsRunningAcceleratedAnimation();
93 // Fire the start timeout if needed
94 fireAnimationEventsIfNeeded();
95 return state() != oldState;
98 void ImplicitAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle)
101 animatedStyle = RenderStyle::clone(m_toStyle.get());
103 CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
106 bool ImplicitAnimation::computeExtentOfTransformAnimation(LayoutRect& bounds) const
110 if (!is<RenderBox>(m_object))
111 return true; // Non-boxes don't get transformed;
113 ASSERT(m_animatingProperty == CSSPropertyTransform);
115 RenderBox& box = downcast<RenderBox>(*m_object);
116 FloatRect rendererBox = snapRectToDevicePixels(box.borderBoxRect(), box.document().deviceScaleFactor());
118 LayoutRect startBounds = bounds;
119 LayoutRect endBounds = bounds;
121 if (isTransformFunctionListValid()) {
122 if (!computeTransformedExtentViaTransformList(rendererBox, *m_fromStyle, startBounds))
125 if (!computeTransformedExtentViaTransformList(rendererBox, *m_toStyle, endBounds))
128 if (!computeTransformedExtentViaMatrix(rendererBox, *m_fromStyle, startBounds))
131 if (!computeTransformedExtentViaMatrix(rendererBox, *m_toStyle, endBounds))
135 bounds = unionRect(startBounds, endBounds);
139 bool ImplicitAnimation::startAnimation(double timeOffset)
141 if (m_object && m_object->isComposited())
142 return downcast<RenderBoxModelObject>(*m_object).startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get());
146 void ImplicitAnimation::pauseAnimation(double timeOffset)
151 if (m_object->isComposited())
152 downcast<RenderBoxModelObject>(*m_object).transitionPaused(timeOffset, m_animatingProperty);
153 // Restore the original (unanimated) style
155 setNeedsStyleRecalc(m_object->element());
158 void ImplicitAnimation::endAnimation()
160 if (m_object && m_object->isComposited())
161 downcast<RenderBoxModelObject>(*m_object).transitionFinished(m_animatingProperty);
164 void ImplicitAnimation::onAnimationEnd(double elapsedTime)
166 // If we have a keyframe animation on this property, this transition is being overridden. The keyframe
167 // animation keeps an unanimated style in case a transition starts while the keyframe animation is
168 // running. But now that the transition has completed, we need to update this style with its new
169 // destination. If we didn't, the next time through we would think a transition had started
170 // (comparing the old unanimated style with the new final style of the transition).
171 RefPtr<KeyframeAnimation> keyframeAnim = m_compositeAnimation->getAnimationForProperty(m_animatingProperty);
173 keyframeAnim->setUnanimatedStyle(m_toStyle);
175 sendTransitionEvent(eventNames().transitionendEvent, elapsedTime);
179 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
181 if (eventType == eventNames().transitionendEvent) {
182 Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
184 if (shouldSendEventForListener(listenerType)) {
185 String propertyName = getPropertyNameString(m_animatingProperty);
187 // Dispatch the event
188 RefPtr<Element> element = m_object->element();
190 ASSERT(!element || !element->document().inPageCache());
194 // Schedule event handling
195 m_compositeAnimation->animationController().addEventToDispatch(element, eventType, propertyName, elapsedTime);
197 // Restore the original (unanimated) style
198 if (eventType == eventNames().transitionendEvent && element->renderer())
199 setNeedsStyleRecalc(element.get());
201 return true; // Did dispatch an event
205 return false; // Didn't dispatch an event
208 void ImplicitAnimation::reset(RenderStyle* to)
215 // Restart the transition
216 if (m_fromStyle && m_toStyle)
217 updateStateMachine(AnimationStateInput::RestartAnimation, -1);
219 // set the transform animation list
220 validateTransformFunctionList();
221 checkForMatchingFilterFunctionLists();
224 void ImplicitAnimation::setOverridden(bool b)
226 if (b == m_overridden)
230 updateStateMachine(m_overridden ? AnimationStateInput::PauseOverride : AnimationStateInput::ResumeOverride, -1);
233 bool ImplicitAnimation::affectsProperty(CSSPropertyID property) const
235 return (m_animatingProperty == property);
238 bool ImplicitAnimation::isTargetPropertyEqual(CSSPropertyID prop, const RenderStyle* targetStyle)
240 // We can get here for a transition that has not started yet. This would make m_toStyle unset and null.
241 // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>)
244 return CSSPropertyAnimation::propertiesEqual(prop, m_toStyle.get(), targetStyle);
247 void ImplicitAnimation::blendPropertyValueInStyle(CSSPropertyID prop, RenderStyle* currentStyle)
249 // We should never add a transition with a 0 duration and delay. But if we ever did
250 // it would have a null toStyle. So just in case, let's check that here. (See
251 // <https://bugs.webkit.org/show_bug.cgi?id=24787>
255 CSSPropertyAnimation::blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
258 void ImplicitAnimation::validateTransformFunctionList()
260 m_transformFunctionListValid = false;
262 if (!m_fromStyle || !m_toStyle)
265 const TransformOperations* val = &m_fromStyle->transform();
266 const TransformOperations* toVal = &m_toStyle->transform();
268 if (val->operations().isEmpty())
271 if (val->operations().isEmpty())
274 // An emtpy transform list matches anything.
275 if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
278 // Transform lists match.
279 m_transformFunctionListValid = true;
282 void ImplicitAnimation::checkForMatchingFilterFunctionLists()
284 m_filterFunctionListsMatch = false;
286 if (!m_fromStyle || !m_toStyle)
289 const FilterOperations* val = &m_fromStyle->filter();
290 const FilterOperations* toVal = &m_toStyle->filter();
292 if (val->operations().isEmpty())
295 if (val->operations().isEmpty())
298 // An emtpy filter list matches anything.
299 if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
302 // Filter lists match.
303 m_filterFunctionListsMatch = true;
306 double ImplicitAnimation::timeToNextService()
308 double t = AnimationBase::timeToNextService();
309 if (t != 0 || preActive())
312 // A return value of 0 means we need service. But if this is an accelerated animation we
313 // only need service at the end of the transition.
314 if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) {
316 getTimeToNextEvent(t, isLooping);
321 } // namespace WebCore