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 Ref<RenderStyle> CompositeAnimation::animate(RenderElement& renderer, RenderStyle* currentStyle, RenderStyle& targetStyle)
302 RefPtr<RenderStyle> resultStyle;
304 // We don't do any transitions if we don't have a currentStyle (on startup).
305 updateTransitions(&renderer, currentStyle, &targetStyle);
306 updateKeyframeAnimations(&renderer, currentStyle, &targetStyle);
307 m_keyframeAnimations.checkConsistency();
310 // Now that we have transition objects ready, let them know about the new goal state. We want them
311 // to fill in a RenderStyle*& only if needed.
312 if (!m_transitions.isEmpty()) {
313 for (auto& transition : m_transitions.values())
314 transition->animate(this, &renderer, currentStyle, &targetStyle, resultStyle);
318 // Now that we have animation objects ready, let them know about the new goal state. We want them
319 // to fill in a RenderStyle*& only if needed.
320 for (auto& name : m_keyframeAnimationOrderMap) {
321 RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name);
323 keyframeAnim->animate(this, &renderer, currentStyle, &targetStyle, resultStyle);
327 return resultStyle.releaseNonNull();
332 PassRefPtr<RenderStyle> CompositeAnimation::getAnimatedStyle() const
334 RefPtr<RenderStyle> resultStyle;
335 for (auto& transition : m_transitions.values())
336 transition->getAnimatedStyle(resultStyle);
338 m_keyframeAnimations.checkConsistency();
340 for (auto& name : m_keyframeAnimationOrderMap) {
341 RefPtr<KeyframeAnimation> keyframeAnimation = m_keyframeAnimations.get(name);
342 if (keyframeAnimation)
343 keyframeAnimation->getAnimatedStyle(resultStyle);
349 double CompositeAnimation::timeToNextService() const
351 // Returns the time at which next service is required. -1 means no service is required. 0 means
352 // service is required now, and > 0 means service is required that many seconds in the future.
355 if (!m_transitions.isEmpty()) {
356 for (auto& transition : m_transitions.values()) {
357 double t = transition->timeToNextService();
358 if (t < minT || minT == -1)
364 if (!m_keyframeAnimations.isEmpty()) {
365 m_keyframeAnimations.checkConsistency();
366 for (auto& animation : m_keyframeAnimations.values()) {
367 double t = animation->timeToNextService();
368 if (t < minT || minT == -1)
378 PassRefPtr<KeyframeAnimation> CompositeAnimation::getAnimationForProperty(CSSPropertyID property) const
380 RefPtr<KeyframeAnimation> retval;
382 // We want to send back the last animation with the property if there are multiples.
383 // So we need to iterate through all animations
384 if (!m_keyframeAnimations.isEmpty()) {
385 m_keyframeAnimations.checkConsistency();
386 for (auto& animation : m_keyframeAnimations.values()) {
387 if (animation->hasAnimationForProperty(property))
395 bool CompositeAnimation::computeExtentOfTransformAnimation(LayoutRect& bounds) const
397 // If more than one transition and animation affect transform, give up.
398 bool seenTransformAnimation = false;
400 for (auto& animation : m_keyframeAnimations.values()) {
401 if (!animation->hasAnimationForProperty(CSSPropertyTransform))
404 if (seenTransformAnimation)
407 seenTransformAnimation = true;
409 if (!animation->computeExtentOfTransformAnimation(bounds))
413 for (auto& transition : m_transitions.values()) {
414 if (transition->animatingProperty() != CSSPropertyTransform || !transition->hasStyle())
417 if (seenTransformAnimation)
420 if (!transition->computeExtentOfTransformAnimation(bounds))
427 void CompositeAnimation::suspendAnimations()
434 if (!m_keyframeAnimations.isEmpty()) {
435 m_keyframeAnimations.checkConsistency();
436 for (auto& animation : m_keyframeAnimations.values())
437 animation->updatePlayState(AnimPlayStatePaused);
440 if (!m_transitions.isEmpty()) {
441 for (auto& transition : m_transitions.values()) {
442 if (transition->hasStyle())
443 transition->updatePlayState(AnimPlayStatePaused);
448 void CompositeAnimation::resumeAnimations()
455 if (!m_keyframeAnimations.isEmpty()) {
456 m_keyframeAnimations.checkConsistency();
457 for (auto& animation : m_keyframeAnimations.values()) {
458 if (animation->playStatePlaying())
459 animation->updatePlayState(AnimPlayStatePlaying);
463 if (!m_transitions.isEmpty()) {
464 for (auto& transition : m_transitions.values()) {
465 if (transition->hasStyle())
466 transition->updatePlayState(AnimPlayStatePlaying);
471 void CompositeAnimation::overrideImplicitAnimations(CSSPropertyID property)
473 if (!m_transitions.isEmpty()) {
474 for (auto& transition : m_transitions.values()) {
475 if (transition->animatingProperty() == property)
476 transition->setOverridden(true);
481 void CompositeAnimation::resumeOverriddenImplicitAnimations(CSSPropertyID property)
483 if (!m_transitions.isEmpty()) {
484 for (auto& transition : m_transitions.values()) {
485 if (transition->animatingProperty() == property)
486 transition->setOverridden(false);
491 bool CompositeAnimation::isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, AnimationBase::RunningState runningState) const
493 if (!m_keyframeAnimations.isEmpty()) {
494 m_keyframeAnimations.checkConsistency();
495 for (auto& animation : m_keyframeAnimations.values()) {
496 if (animation->isAnimatingProperty(property, acceleratedOnly, runningState))
501 if (!m_transitions.isEmpty()) {
502 for (auto& transition : m_transitions.values()) {
503 if (transition->isAnimatingProperty(property, acceleratedOnly, runningState))
510 bool CompositeAnimation::pauseAnimationAtTime(const AtomicString& name, double t)
512 m_keyframeAnimations.checkConsistency();
514 RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name.impl());
515 if (!keyframeAnim || !keyframeAnim->running())
518 double count = keyframeAnim->m_animation->iterationCount();
519 if ((t >= 0.0) && ((count == Animation::IterationCountInfinite) || (t <= count * keyframeAnim->duration()))) {
520 keyframeAnim->freezeAtTime(t);
527 bool CompositeAnimation::pauseTransitionAtTime(CSSPropertyID property, double t)
529 if ((property < firstCSSProperty) || (property >= firstCSSProperty + numCSSProperties))
532 ImplicitAnimation* implAnim = m_transitions.get(property);
534 // Check to see if this property is being animated via a shorthand.
535 // This code is only used for testing, so performance is not critical here.
536 HashSet<CSSPropertyID> shorthandProperties = CSSPropertyAnimation::animatableShorthandsAffectingProperty(property);
537 bool anyPaused = false;
538 for (auto propertyID : shorthandProperties) {
539 if (pauseTransitionAtTime(propertyID, t))
545 if (!implAnim->running())
548 if ((t >= 0.0) && (t <= implAnim->duration())) {
549 implAnim->freezeAtTime(t);
556 unsigned CompositeAnimation::numberOfActiveAnimations() const
560 m_keyframeAnimations.checkConsistency();
561 for (auto& animation : m_keyframeAnimations.values()) {
562 if (animation->running())
566 for (auto& transition : m_transitions.values()) {
567 if (transition->running())
574 } // namespace WebCore