2 * Copyright (C) 2013 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/animation/AnimationStack.h"
34 #include "core/animation/CompositorAnimations.h"
35 #include "core/animation/StyleInterpolation.h"
36 #include "core/animation/css/CSSAnimations.h"
37 #include "wtf/BitArray.h"
38 #include "wtf/NonCopyingSort.h"
45 void copyToActiveInterpolationMap(const WillBeHeapVector<RefPtrWillBeMember<blink::Interpolation> >& source, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<blink::Interpolation> >& target)
47 for (size_t i = 0; i < source.size(); ++i) {
48 Interpolation* interpolation = source[i].get();
49 target.set(toStyleInterpolation(interpolation)->id(), interpolation);
53 bool compareEffects(const OwnPtrWillBeMember<SampledEffect>& effect1, const OwnPtrWillBeMember<SampledEffect>& effect2)
55 ASSERT(effect1 && effect2);
56 return effect1->sequenceNumber() < effect2->sequenceNumber();
59 void copyNewAnimationsToActiveInterpolationMap(const WillBeHeapVector<RawPtrWillBeMember<InertAnimation> >& newAnimations, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& result)
61 for (size_t i = 0; i < newAnimations.size(); ++i) {
62 OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > sample = newAnimations[i]->sample(0);
64 copyToActiveInterpolationMap(*sample, result);
71 AnimationStack::AnimationStack()
75 bool AnimationStack::affects(CSSPropertyID property) const
77 for (size_t i = 0; i < m_effects.size(); ++i) {
78 if (m_effects[i]->animation() && m_effects[i]->animation()->affects(property))
84 bool AnimationStack::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
86 for (size_t i = 0; i < m_effects.size(); ++i) {
87 if (m_effects[i]->animation() && m_effects[i]->animation()->hasActiveAnimationsOnCompositor(property))
93 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > AnimationStack::activeInterpolations(AnimationStack* animationStack, const WillBeHeapVector<RawPtrWillBeMember<InertAnimation> >* newAnimations, const WillBeHeapHashSet<RawPtrWillBeMember<const AnimationPlayer> >* cancelledAnimationPlayers, Animation::Priority priority, double timelineCurrentTime)
95 // We don't exactly know when new animations will start, but timelineCurrentTime is a good estimate.
97 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result;
100 WillBeHeapVector<OwnPtrWillBeMember<SampledEffect> >& effects = animationStack->m_effects;
101 // std::sort doesn't work with OwnPtrs
102 nonCopyingSort(effects.begin(), effects.end(), compareEffects);
103 animationStack->simplifyEffects();
104 for (size_t i = 0; i < effects.size(); ++i) {
105 const SampledEffect& effect = *effects[i];
106 if (effect.priority() != priority || (cancelledAnimationPlayers && effect.animation() && cancelledAnimationPlayers->contains(effect.animation()->player())))
108 copyToActiveInterpolationMap(effect.interpolations(), result);
113 copyNewAnimationsToActiveInterpolationMap(*newAnimations, result);
118 void AnimationStack::simplifyEffects()
120 // FIXME: This will need to be updated when we have 'add' keyframes.
122 BitArray<numCSSProperties> replacedProperties;
123 for (size_t i = m_effects.size(); i--; ) {
124 SampledEffect& effect = *m_effects[i];
125 effect.removeReplacedInterpolationsIfNeeded(replacedProperties);
126 if (!effect.canChange()) {
127 for (size_t i = 0; i < effect.interpolations().size(); ++i)
128 replacedProperties.set(toStyleInterpolation(effect.interpolations()[i].get())->id());
133 for (size_t i = 0; i < m_effects.size(); ++i) {
134 if (!m_effects[i]->interpolations().isEmpty()) {
135 m_effects[dest++].swap(m_effects[i]);
138 if (m_effects[i]->animation())
139 m_effects[i]->animation()->notifySampledEffectRemovedFromAnimationStack();
141 m_effects.shrink(dest);
144 void AnimationStack::trace(Visitor* visitor)
146 visitor->trace(m_effects);
149 bool AnimationStack::getAnimatedBoundingBox(FloatBox& box, CSSPropertyID property) const
151 FloatBox originalBox(box);
152 for (size_t i = 0; i < m_effects.size(); ++i) {
153 if (m_effects[i]->animation() && m_effects[i]->animation()->affects(property)) {
154 Animation* anim = m_effects[i]->animation();
157 const Timing& timing = anim->specifiedTiming();
158 double startRange = 0;
160 timing.timingFunction->range(&startRange, &endRange);
161 FloatBox expandingBox(originalBox);
162 if (!CompositorAnimations::instance()->getAnimatedBoundingBox(expandingBox, *anim->effect(), startRange, endRange))
164 box.expandTo(expandingBox);