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/Interpolation.h"
35 #include "core/animation/css/CSSAnimations.h"
36 #include "wtf/BitArray.h"
37 #include "wtf/NonCopyingSort.h"
44 void copyToActiveInterpolationMap(const WillBeHeapVector<RefPtrWillBeMember<WebCore::Interpolation> >& source, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<WebCore::Interpolation> >& target)
46 for (size_t i = 0; i < source.size(); ++i) {
47 Interpolation* interpolation = source[i].get();
48 target.set(toStyleInterpolation(interpolation)->id(), interpolation);
52 bool compareEffects(const OwnPtr<SampledEffect>& effect1, const OwnPtr<SampledEffect>& effect2)
54 ASSERT(effect1 && effect2);
55 return effect1->sortInfo() < effect2->sortInfo();
58 void copyNewAnimationsToActiveInterpolationMap(const Vector<InertAnimation*>& newAnimations, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& result)
60 for (size_t i = 0; i < newAnimations.size(); ++i) {
61 OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > sample = newAnimations[i]->sample(0);
63 copyToActiveInterpolationMap(*sample, result);
70 AnimationStack::AnimationStack()
74 bool AnimationStack::affects(CSSPropertyID property) const
76 for (size_t i = 0; i < m_effects.size(); ++i) {
77 if (m_effects[i]->animation() && m_effects[i]->animation()->affects(property))
83 bool AnimationStack::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
85 for (size_t i = 0; i < m_effects.size(); ++i) {
86 if (m_effects[i]->animation() && m_effects[i]->animation()->hasActiveAnimationsOnCompositor(property))
92 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > AnimationStack::activeInterpolations(AnimationStack* animationStack, const Vector<InertAnimation*>* newAnimations, const HashSet<const AnimationPlayer*>* cancelledAnimationPlayers, Animation::Priority priority, double timelineCurrentTime)
94 // We don't exactly know when new animations will start, but timelineCurrentTime is a good estimate.
96 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result;
99 Vector<OwnPtr<SampledEffect> >& effects = animationStack->m_effects;
100 // std::sort doesn't work with OwnPtrs
101 nonCopyingSort(effects.begin(), effects.end(), compareEffects);
102 animationStack->simplifyEffects();
103 for (size_t i = 0; i < effects.size(); ++i) {
104 const SampledEffect& effect = *effects[i];
105 if (effect.priority() != priority || (cancelledAnimationPlayers && effect.animation() && cancelledAnimationPlayers->contains(effect.animation()->player())))
107 if (newAnimations && effect.sortInfo().startTime() > timelineCurrentTime) {
108 copyNewAnimationsToActiveInterpolationMap(*newAnimations, result);
111 copyToActiveInterpolationMap(effect.interpolations(), result);
116 copyNewAnimationsToActiveInterpolationMap(*newAnimations, result);
121 void AnimationStack::simplifyEffects()
123 // FIXME: This will need to be updated when we have 'add' keyframes.
125 BitArray<numCSSProperties> replacedProperties;
126 for (size_t i = m_effects.size(); i--; ) {
127 SampledEffect& effect = *m_effects[i];
128 effect.removeReplacedInterpolationsIfNeeded(replacedProperties);
129 if (!effect.canChange()) {
130 for (size_t i = 0; i < effect.interpolations().size(); ++i)
131 replacedProperties.set(toStyleInterpolation(effect.interpolations()[i].get())->id());
136 for (size_t i = 0; i < m_effects.size(); ++i) {
137 if (!m_effects[i]->interpolations().isEmpty()) {
138 swap(m_effects[dest++], m_effects[i]);
141 if (m_effects[i]->animation())
142 m_effects[i]->animation()->notifySampledEffectRemovedFromAnimationStack();
144 m_effects.shrink(dest);
147 } // namespace WebCore