1 /*
2 * Copyright (C) 2007 Apple 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
6 * are met:
7 *
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.
16 *
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.
27 */
28
29 #include "config.h"
30
31 #include "AnimationControllerPrivate.h"
32 #include "CompositeAnimation.h"
33 #include "CSSPropertyNames.h"
34 #include "EventNames.h"
35 #include "ImplicitAnimation.h"
36 #include "KeyframeAnimation.h"
37 #include "RenderLayer.h"
38 #include "RenderLayerBacking.h"
39 #include <wtf/UnusedParam.h>
40
41 namespace WebCore {
42
ImplicitAnimation(const Animation * transition,int animatingProperty,RenderObject * renderer,CompositeAnimation * compAnim,RenderStyle * fromStyle)43 ImplicitAnimation::ImplicitAnimation(const Animation* transition, int animatingProperty, RenderObject* renderer, CompositeAnimation* compAnim, RenderStyle* fromStyle)
44 : AnimationBase(transition, renderer, compAnim)
45 , m_transitionProperty(transition->property())
46 , m_animatingProperty(animatingProperty)
47 , m_overridden(false)
48 , m_active(true)
49 , m_fromStyle(fromStyle)
50 {
51 ASSERT(animatingProperty != cAnimateAll);
52 }
53
~ImplicitAnimation()54 ImplicitAnimation::~ImplicitAnimation()
55 {
56 // // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed.
57 if (!postActive())
58 endAnimation(true);
59 }
60
shouldSendEventForListener(Document::ListenerType inListenerType) const61 bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const
62 {
63 return m_object->document()->hasListenerType(inListenerType);
64 }
65
animate(CompositeAnimation *,RenderObject *,const RenderStyle *,RenderStyle * targetStyle,RefPtr<RenderStyle> & animatedStyle)66 void ImplicitAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
67 {
68 // If we get this far and the animation is done, it means we are cleaning up a just finished animation.
69 // So just return. Everything is already all cleaned up.
70 if (postActive())
71 return;
72
73 // Reset to start the transition if we are new
74 if (isNew())
75 reset(targetStyle);
76
77 // Run a cycle of animation.
78 // We know we will need a new render style, so make one if needed
79 if (!animatedStyle)
80 animatedStyle = RenderStyle::clone(targetStyle);
81
82 bool needsAnim = blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
83 // FIXME: we also need to detect cases where we have to software animate for other reasons,
84 // such as a child using inheriting the transform. https://bugs.webkit.org/show_bug.cgi?id=23902
85 if (needsAnim)
86 setAnimating();
87 else {
88 #if USE(ACCELERATED_COMPOSITING)
89 // If we are running an accelerated animation, set a flag in the style which causes the style
90 // to compare as different to any other style. This ensures that changes to the property
91 // that is animating are correctly detected during the animation (e.g. when a transition
92 // gets interrupted).
93 animatedStyle->setIsRunningAcceleratedAnimation();
94 #endif
95 }
96
97 // Fire the start timeout if needed
98 fireAnimationEventsIfNeeded();
99 }
100
getAnimatedStyle(RefPtr<RenderStyle> & animatedStyle)101 void ImplicitAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle)
102 {
103 if (!animatedStyle)
104 animatedStyle = RenderStyle::clone(m_toStyle.get());
105
106 blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
107 }
108
startAnimation(double beginTime)109 bool ImplicitAnimation::startAnimation(double beginTime)
110 {
111 #if USE(ACCELERATED_COMPOSITING)
112 if (m_object && m_object->hasLayer()) {
113 RenderLayer* layer = toRenderBoxModelObject(m_object)->layer();
114 if (layer->isComposited())
115 return layer->backing()->startTransition(beginTime, m_animatingProperty, m_fromStyle.get(), m_toStyle.get());
116 }
117 #else
118 UNUSED_PARAM(beginTime);
119 #endif
120 return false;
121 }
122
endAnimation(bool)123 void ImplicitAnimation::endAnimation(bool /*reset*/)
124 {
125 #if USE(ACCELERATED_COMPOSITING)
126 if (m_object && m_object->hasLayer()) {
127 RenderLayer* layer = toRenderBoxModelObject(m_object)->layer();
128 if (layer->isComposited())
129 layer->backing()->transitionFinished(m_animatingProperty);
130 }
131 #endif
132 }
133
onAnimationEnd(double elapsedTime)134 void ImplicitAnimation::onAnimationEnd(double elapsedTime)
135 {
136 // If we have a keyframe animation on this property, this transition is being overridden. The keyframe
137 // animation keeps an unanimated style in case a transition starts while the keyframe animation is
138 // running. But now that the transition has completed, we need to update this style with its new
139 // destination. If we didn't, the next time through we would think a transition had started
140 // (comparing the old unanimated style with the new final style of the transition).
141 RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty);
142 if (keyframeAnim)
143 keyframeAnim->setUnanimatedStyle(m_toStyle);
144
145 if (!sendTransitionEvent(eventNames().webkitTransitionEndEvent, elapsedTime)) {
146 // We didn't dispatch an event, which would call endAnimation(), so we'll just call it here.
147 endAnimation(true);
148 }
149 }
150
sendTransitionEvent(const AtomicString & eventType,double elapsedTime)151 bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
152 {
153 if (eventType == eventNames().webkitTransitionEndEvent) {
154 Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
155
156 if (shouldSendEventForListener(listenerType)) {
157 String propertyName;
158 if (m_animatingProperty != cAnimateAll)
159 propertyName = getPropertyName(static_cast<CSSPropertyID>(m_animatingProperty));
160
161 // Dispatch the event
162 RefPtr<Element> element = 0;
163 if (m_object->node() && m_object->node()->isElementNode())
164 element = static_cast<Element*>(m_object->node());
165
166 ASSERT(!element || element->document() && !element->document()->inPageCache());
167 if (!element)
168 return false;
169
170 // Schedule event handling
171 m_compAnim->animationController()->addEventToDispatch(element, eventType, propertyName, elapsedTime);
172
173 // Restore the original (unanimated) style
174 if (eventType == eventNames().webkitTransitionEndEvent && element->renderer())
175 setNeedsStyleRecalc(element.get());
176
177 return true; // Did dispatch an event
178 }
179 }
180
181 return false; // Didn't dispatch an event
182 }
183
reset(RenderStyle * to)184 void ImplicitAnimation::reset(RenderStyle* to)
185 {
186 ASSERT(to);
187 ASSERT(m_fromStyle);
188
189 m_toStyle = to;
190
191 // Restart the transition
192 if (m_fromStyle && m_toStyle)
193 updateStateMachine(AnimationStateInputRestartAnimation, -1);
194
195 // set the transform animation list
196 validateTransformFunctionList();
197 }
198
setOverridden(bool b)199 void ImplicitAnimation::setOverridden(bool b)
200 {
201 if (b == m_overridden)
202 return;
203
204 m_overridden = b;
205 updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1);
206 }
207
affectsProperty(int property) const208 bool ImplicitAnimation::affectsProperty(int property) const
209 {
210 return (m_animatingProperty == property);
211 }
212
isTargetPropertyEqual(int prop,const RenderStyle * targetStyle)213 bool ImplicitAnimation::isTargetPropertyEqual(int prop, const RenderStyle* targetStyle)
214 {
215 // We can get here for a transition that has not started yet. This would make m_toStyle unset and null.
216 // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>)
217 if (!m_toStyle)
218 return false;
219 return propertiesEqual(prop, m_toStyle.get(), targetStyle);
220 }
221
blendPropertyValueInStyle(int prop,RenderStyle * currentStyle)222 void ImplicitAnimation::blendPropertyValueInStyle(int prop, RenderStyle* currentStyle)
223 {
224 // We should never add a transition with a 0 duration and delay. But if we ever did
225 // it would have a null toStyle. So just in case, let's check that here. (See
226 // <https://bugs.webkit.org/show_bug.cgi?id=24787>
227 if (!m_toStyle)
228 return;
229
230 blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0));
231 }
232
validateTransformFunctionList()233 void ImplicitAnimation::validateTransformFunctionList()
234 {
235 m_transformFunctionListValid = false;
236
237 if (!m_fromStyle || !m_toStyle)
238 return;
239
240 const TransformOperations* val = &m_fromStyle->transform();
241 const TransformOperations* toVal = &m_toStyle->transform();
242
243 if (val->operations().isEmpty())
244 val = toVal;
245
246 if (val->operations().isEmpty())
247 return;
248
249 // See if the keyframes are valid
250 if (val != toVal) {
251 // A list of length 0 matches anything
252 if (!toVal->operations().isEmpty()) {
253 // If the sizes of the function lists don't match, the lists don't match
254 if (val->operations().size() != toVal->operations().size())
255 return;
256
257 // If the types of each function are not the same, the lists don't match
258 for (size_t j = 0; j < val->operations().size(); ++j) {
259 if (!val->operations()[j]->isSameType(*toVal->operations()[j]))
260 return;
261 }
262 }
263 }
264
265 // Keyframes are valid
266 m_transformFunctionListValid = true;
267 }
268
timeToNextService()269 double ImplicitAnimation::timeToNextService()
270 {
271 double t = AnimationBase::timeToNextService();
272 #if USE(ACCELERATED_COMPOSITING)
273 if (t != 0 || preActive())
274 return t;
275
276 // A return value of 0 means we need service. But if this is an accelerated animation we
277 // only need service at the end of the transition.
278 if (animationOfPropertyIsAccelerated(m_animatingProperty) && !isFallbackAnimating()) {
279 bool isLooping;
280 getTimeToNextEvent(t, isLooping);
281 }
282 #endif
283 return t;
284 }
285
286 } // namespace WebCore
287