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.
30 #include "CompositeAnimation.h"
32 #include "AnimationControllerPrivate.h"
33 #include "CSSPropertyAnimation.h"
34 #include "CSSPropertyNames.h"
35 #include "ImplicitAnimation.h"
36 #include "KeyframeAnimation.h"
38 #include "RenderElement.h"
39 #include "RenderStyle.h"
40 #include <wtf/text/CString.h>
44 CompositeAnimation::CompositeAnimation(AnimationControllerPrivate& animationController)
45 : m_animationController(animationController)
47 m_suspended = m_animationController.isSuspended() && !m_animationController.allowsNewAnimationsWhileSuspended();
50 CompositeAnimation::~CompositeAnimation()
52 // Toss the refs to all animations, but make sure we remove them from
53 // any waiting lists first.
56 m_transitions.clear();
57 m_keyframeAnimations.clear();
60 void CompositeAnimation::clearRenderer()
62 if (!m_transitions.isEmpty()) {
63 // Clear the renderers from all running animations, in case we are in the middle of
64 // an animation callback (see https://bugs.webkit.org/show_bug.cgi?id=22052)
65 for (auto& transition : m_transitions.values()) {
66 animationController().animationWillBeRemoved(transition.get());
70 if (!m_keyframeAnimations.isEmpty()) {
71 m_keyframeAnimations.checkConsistency();
72 for (auto& animation : m_keyframeAnimations.values()) {
73 animationController().animationWillBeRemoved(animation.get());
79 void CompositeAnimation::updateTransitions(RenderElement* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle)
81 // If currentStyle is null or there are no old or new transitions, just skip it
82 if (!currentStyle || (!targetStyle->transitions() && m_transitions.isEmpty()))
85 // Mark all existing transitions as no longer active. We will mark the still active ones
86 // in the next loop and then toss the ones that didn't get marked.
87 for (auto& transition : m_transitions.values())
88 transition->setActive(false);
90 RefPtr<RenderStyle> modifiedCurrentStyle;
92 // Check to see if we need to update the active transitions
93 if (targetStyle->transitions()) {
94 for (size_t i = 0; i < targetStyle->transitions()->size(); ++i) {
95 Animation& animation = targetStyle->transitions()->animation(i);
96 bool isActiveTransition = !m_suspended && (animation.duration() || animation.delay() > 0);
98 Animation::AnimationMode mode = animation.animationMode();
99 if (mode == Animation::AnimateNone)
102 CSSPropertyID prop = animation.property();
104 bool all = mode == Animation::AnimateAll;
106 // Handle both the 'all' and single property cases. For the single prop case, we make only one pass
108 for (int propertyIndex = 0; propertyIndex < CSSPropertyAnimation::getNumProperties(); ++propertyIndex) {
110 // Get the next property which is not a shorthand.
112 prop = CSSPropertyAnimation::getPropertyAtIndex(propertyIndex, isShorthand);
117 // ImplicitAnimations are always hashed by actual properties, never animateAll.
118 ASSERT(prop >= firstCSSProperty && prop < (firstCSSProperty + numCSSProperties));
120 // If there is a running animation for this property, the transition is overridden
121 // and we have to use the unanimatedStyle from the animation. We do the test
122 // against the unanimated style here, but we "override" the transition later.
123 RefPtr<KeyframeAnimation> keyframeAnim = getAnimationForProperty(prop);
124 RenderStyle* fromStyle = keyframeAnim ? keyframeAnim->unanimatedStyle() : currentStyle;
126 // See if there is a current transition for this prop
127 ImplicitAnimation* implAnim = m_transitions.get(prop);
131 // If we are post active don't bother setting the active flag. This will cause
132 // this animation to get removed at the end of this function.
133 if (!implAnim->postActive())
134 implAnim->setActive(true);
136 // This might be a transition that is just finishing. That would be the case
137 // if it were postActive. But we still need to check for equality because
138 // it could be just finishing AND changing to a new goal state.
140 // This implAnim might also not be an already running transition. It might be
141 // newly added to the list in a previous iteration. This would happen if
142 // you have both an explicit transition-property and 'all' in the same
143 // list. In this case, the latter one overrides the earlier one, so we
144 // behave as though this is a running animation being replaced.
145 if (!implAnim->isTargetPropertyEqual(prop, targetStyle)) {
146 // For accelerated animations we need to return a new RenderStyle with the _current_ value
147 // of the property, so that restarted transitions use the correct starting point.
148 if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(prop) && implAnim->isAccelerated()) {
149 if (!modifiedCurrentStyle)
150 modifiedCurrentStyle = RenderStyle::clone(currentStyle);
152 implAnim->blendPropertyValueInStyle(prop, modifiedCurrentStyle.get());
154 LOG(Animations, "Removing existing ImplicitAnimation %p for property %s", implAnim, getPropertyName(prop));
155 animationController().animationWillBeRemoved(implAnim);
156 m_transitions.remove(prop);
160 // We need to start a transition if it is active and the properties don't match
161 equal = !isActiveTransition || CSSPropertyAnimation::propertiesEqual(prop, fromStyle, targetStyle);
164 // We can be in this loop with an inactive transition (!isActiveTransition). We need
165 // to do that to check to see if we are canceling a transition. But we don't want to
166 // start one of the inactive transitions. So short circuit that here. (See
167 // <https://bugs.webkit.org/show_bug.cgi?id=24787>
168 if (!equal && isActiveTransition) {
169 // Add the new transition
170 RefPtr<ImplicitAnimation> implicitAnimation = ImplicitAnimation::create(animation, prop, renderer, this, modifiedCurrentStyle ? modifiedCurrentStyle.get() : fromStyle);
171 LOG(Animations, "Created ImplicitAnimation %p for property %s duration %.2f delay %.2f", implicitAnimation.get(), getPropertyName(prop), animation.duration(), animation.delay());
172 m_transitions.set(prop, implicitAnimation.release());
175 // We only need one pass for the single prop case
182 // Make a list of transitions to be removed
183 Vector<int> toBeRemoved;
184 for (auto& transition : m_transitions.values()) {
185 if (!transition->active()) {
186 animationController().animationWillBeRemoved(transition.get());
187 toBeRemoved.append(transition->animatingProperty());
188 LOG(Animations, "Removing ImplicitAnimation %p for property %s", transition.get(), getPropertyName(transition->animatingProperty()));
192 // Now remove the transitions from the list
193 for (auto propertyToRemove : toBeRemoved)
194 m_transitions.remove(propertyToRemove);
197 void CompositeAnimation::updateKeyframeAnimations(RenderElement* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle)
199 // Nothing to do if we don't have any animations, and didn't have any before
200 if (m_keyframeAnimations.isEmpty() && !targetStyle->hasAnimations())
203 m_keyframeAnimations.checkConsistency();
205 if (currentStyle && currentStyle->hasAnimations() && targetStyle->hasAnimations() && *(currentStyle->animations()) == *(targetStyle->animations())) {
206 // The current and target animations are the same so we just need to toss any
207 // animation which is finished (postActive).
208 for (auto& animation : m_keyframeAnimations.values()) {
209 if (animation->postActive())
210 animation->setIndex(-1);
213 // Mark all existing animations as no longer active.
214 for (auto& animation : m_keyframeAnimations.values())
215 animation->setIndex(-1);
217 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
218 m_hasScrollTriggeredAnimation = false;
221 // Toss the animation order map.
222 m_keyframeAnimationOrderMap.clear();
224 DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, none, ("none", AtomicString::ConstructFromLiteral));
226 // Now mark any still active animations as active and add any new animations.
227 if (targetStyle->animations()) {
228 int numAnims = targetStyle->animations()->size();
229 for (int i = 0; i < numAnims; ++i) {
230 Animation& animation = targetStyle->animations()->animation(i);
231 AtomicString animationName(animation.name());
233 if (!animation.isValidAnimation())
236 // See if there is a current animation for this name.
237 RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(animationName.impl());
240 // If this animation is postActive, skip it so it gets removed at the end of this function.
241 if (keyframeAnim->postActive())
244 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
245 if (animation.trigger()->isScrollAnimationTrigger())
246 m_hasScrollTriggeredAnimation = true;
249 // This one is still active.
251 // Animations match, but play states may differ. Update if needed.
252 keyframeAnim->updatePlayState(animation.playState());
254 // Set the saved animation to this new one, just in case the play state has changed.
255 keyframeAnim->setAnimation(animation);
256 keyframeAnim->setIndex(i);
257 } else if ((animation.duration() || animation.delay()) && animation.iterationCount() && animationName != none) {
258 keyframeAnim = KeyframeAnimation::create(animation, renderer, i, this, targetStyle);
259 LOG(Animations, "Creating KeyframeAnimation %p with keyframes %s, duration %.2f, delay %.2f, iterations %.2f", keyframeAnim.get(), animation.name().utf8().data(), animation.duration(), animation.delay(), animation.iterationCount());
261 keyframeAnim->updatePlayState(AnimPlayStatePaused);
262 LOG(Animations, " (created in suspended/paused state)");
265 for (auto propertyID : keyframeAnim->keyframes().properties())
266 LOG(Animations, " property %s", getPropertyName(propertyID));
269 #if ENABLE(CSS_ANIMATIONS_LEVEL_2)
270 if (animation.trigger()->isScrollAnimationTrigger())
271 m_hasScrollTriggeredAnimation = true;
274 m_keyframeAnimations.set(keyframeAnim->name().impl(), keyframeAnim);
277 // Add this to the animation order map.
279 m_keyframeAnimationOrderMap.append(keyframeAnim->name().impl());
284 // Make a list of animations to be removed.
285 Vector<AtomicStringImpl*> animsToBeRemoved;
286 for (auto& animation : m_keyframeAnimations.values()) {
287 if (animation->index() < 0) {
288 animsToBeRemoved.append(animation->name().impl());
289 animationController().animationWillBeRemoved(animation.get());
291 LOG(Animations, "Removing KeyframeAnimation %p", animation.get());
295 // Now remove the animations from the list.
296 for (auto nameForRemoval : animsToBeRemoved)
297 m_keyframeAnimations.remove(nameForRemoval);
300 bool CompositeAnimation::animate(RenderElement& renderer, RenderStyle* currentStyle, RenderStyle& targetStyle, Ref<RenderStyle>& blendedStyle)
302 // We don't do any transitions if we don't have a currentStyle (on startup).
303 updateTransitions(&renderer, currentStyle, &targetStyle);
304 updateKeyframeAnimations(&renderer, currentStyle, &targetStyle);
305 m_keyframeAnimations.checkConsistency();
307 RefPtr<RenderStyle> animatedStyle;
308 bool animationStateChanged = false;
311 // Now that we have transition objects ready, let them know about the new goal state. We want them
312 // to fill in a RenderStyle*& only if needed.
313 for (auto& transition : m_transitions.values()) {
314 if (transition->animate(this, &renderer, currentStyle, &targetStyle, animatedStyle))
315 animationStateChanged = true;
319 // Now that we have animation objects ready, let them know about the new goal state. We want them
320 // to fill in a RenderStyle*& only if needed.
321 for (auto& name : m_keyframeAnimationOrderMap) {
322 RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name);
323 if (keyframeAnim && keyframeAnim->animate(this, &renderer, currentStyle, &targetStyle, animatedStyle))
324 animationStateChanged = true;
328 blendedStyle = animatedStyle.releaseNonNull();
330 blendedStyle = targetStyle;
332 return animationStateChanged;
335 PassRefPtr<RenderStyle> CompositeAnimation::getAnimatedStyle() const
337 RefPtr<RenderStyle> resultStyle;
338 for (auto& transition : m_transitions.values())
339 transition->getAnimatedStyle(resultStyle);
341 m_keyframeAnimations.checkConsistency();
343 for (auto& name : m_keyframeAnimationOrderMap) {
344 RefPtr<KeyframeAnimation> keyframeAnimation = m_keyframeAnimations.get(name);
345 if (keyframeAnimation)
346 keyframeAnimation->getAnimatedStyle(resultStyle);
352 double CompositeAnimation::timeToNextService() const
354 // Returns the time at which next service is required. -1 means no service is required. 0 means
355 // service is required now, and > 0 means service is required that many seconds in the future.
358 if (!m_transitions.isEmpty()) {
359 for (auto& transition : m_transitions.values()) {
360 double t = transition->timeToNextService();
361 if (t < minT || minT == -1)
367 if (!m_keyframeAnimations.isEmpty()) {
368 m_keyframeAnimations.checkConsistency();
369 for (auto& animation : m_keyframeAnimations.values()) {
370 double t = animation->timeToNextService();
371 if (t < minT || minT == -1)
381 PassRefPtr<KeyframeAnimation> CompositeAnimation::getAnimationForProperty(CSSPropertyID property) const
383 RefPtr<KeyframeAnimation> retval;
385 // We want to send back the last animation with the property if there are multiples.
386 // So we need to iterate through all animations
387 if (!m_keyframeAnimations.isEmpty()) {
388 m_keyframeAnimations.checkConsistency();
389 for (auto& animation : m_keyframeAnimations.values()) {
390 if (animation->hasAnimationForProperty(property))
398 bool CompositeAnimation::computeExtentOfTransformAnimation(LayoutRect& bounds) const
400 // If more than one transition and animation affect transform, give up.
401 bool seenTransformAnimation = false;
403 for (auto& animation : m_keyframeAnimations.values()) {
404 if (!animation->hasAnimationForProperty(CSSPropertyTransform))
407 if (seenTransformAnimation)
410 seenTransformAnimation = true;
412 if (!animation->computeExtentOfTransformAnimation(bounds))
416 for (auto& transition : m_transitions.values()) {
417 if (transition->animatingProperty() != CSSPropertyTransform || !transition->hasStyle())
420 if (seenTransformAnimation)
423 if (!transition->computeExtentOfTransformAnimation(bounds))
430 void CompositeAnimation::suspendAnimations()
437 if (!m_keyframeAnimations.isEmpty()) {
438 m_keyframeAnimations.checkConsistency();
439 for (auto& animation : m_keyframeAnimations.values())
440 animation->updatePlayState(AnimPlayStatePaused);
443 if (!m_transitions.isEmpty()) {
444 for (auto& transition : m_transitions.values()) {
445 if (transition->hasStyle())
446 transition->updatePlayState(AnimPlayStatePaused);
451 void CompositeAnimation::resumeAnimations()
458 if (!m_keyframeAnimations.isEmpty()) {
459 m_keyframeAnimations.checkConsistency();
460 for (auto& animation : m_keyframeAnimations.values()) {
461 if (animation->playStatePlaying())
462 animation->updatePlayState(AnimPlayStatePlaying);
466 if (!m_transitions.isEmpty()) {
467 for (auto& transition : m_transitions.values()) {
468 if (transition->hasStyle())
469 transition->updatePlayState(AnimPlayStatePlaying);
474 void CompositeAnimation::overrideImplicitAnimations(CSSPropertyID property)
476 if (!m_transitions.isEmpty()) {
477 for (auto& transition : m_transitions.values()) {
478 if (transition->animatingProperty() == property)
479 transition->setOverridden(true);
484 void CompositeAnimation::resumeOverriddenImplicitAnimations(CSSPropertyID property)
486 if (!m_transitions.isEmpty()) {
487 for (auto& transition : m_transitions.values()) {
488 if (transition->animatingProperty() == property)
489 transition->setOverridden(false);
494 bool CompositeAnimation::isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, AnimationBase::RunningState runningState) const
496 if (!m_keyframeAnimations.isEmpty()) {
497 m_keyframeAnimations.checkConsistency();
498 for (auto& animation : m_keyframeAnimations.values()) {
499 if (animation->isAnimatingProperty(property, acceleratedOnly, runningState))
504 if (!m_transitions.isEmpty()) {
505 for (auto& transition : m_transitions.values()) {
506 if (transition->isAnimatingProperty(property, acceleratedOnly, runningState))
513 bool CompositeAnimation::pauseAnimationAtTime(const AtomicString& name, double t)
515 m_keyframeAnimations.checkConsistency();
517 RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name.impl());
518 if (!keyframeAnim || !keyframeAnim->running())
521 double count = keyframeAnim->m_animation->iterationCount();
522 if ((t >= 0.0) && ((count == Animation::IterationCountInfinite) || (t <= count * keyframeAnim->duration()))) {
523 keyframeAnim->freezeAtTime(t);
530 bool CompositeAnimation::pauseTransitionAtTime(CSSPropertyID property, double t)
532 if ((property < firstCSSProperty) || (property >= firstCSSProperty + numCSSProperties))
535 ImplicitAnimation* implAnim = m_transitions.get(property);
537 // Check to see if this property is being animated via a shorthand.
538 // This code is only used for testing, so performance is not critical here.
539 HashSet<CSSPropertyID> shorthandProperties = CSSPropertyAnimation::animatableShorthandsAffectingProperty(property);
540 bool anyPaused = false;
541 for (auto propertyID : shorthandProperties) {
542 if (pauseTransitionAtTime(propertyID, t))
548 if (!implAnim->running())
551 if ((t >= 0.0) && (t <= implAnim->duration())) {
552 implAnim->freezeAtTime(t);
559 unsigned CompositeAnimation::numberOfActiveAnimations() const
563 m_keyframeAnimations.checkConsistency();
564 for (auto& animation : m_keyframeAnimations.values()) {
565 if (animation->running())
569 for (auto& transition : m_transitions.values()) {
570 if (transition->running())
577 } // namespace WebCore