Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimationStack.cpp
1 /*
2  * Copyright (C) 2013 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "core/animation/AnimationStack.h"
33
34 #include "core/animation/Interpolation.h"
35 #include "core/animation/css/CSSAnimations.h"
36 #include "wtf/BitArray.h"
37 #include "wtf/NonCopyingSort.h"
38 #include <algorithm>
39
40 namespace WebCore {
41
42 namespace {
43
44 void copyToActiveInterpolationMap(const WillBeHeapVector<RefPtrWillBeMember<WebCore::Interpolation> >& source, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<WebCore::Interpolation> >& target)
45 {
46     for (size_t i = 0; i < source.size(); ++i) {
47         Interpolation* interpolation = source[i].get();
48         target.set(toStyleInterpolation(interpolation)->id(), interpolation);
49     }
50 }
51
52 bool compareEffects(const OwnPtr<SampledEffect>& effect1, const OwnPtr<SampledEffect>& effect2)
53 {
54     ASSERT(effect1 && effect2);
55     return effect1->sortInfo() < effect2->sortInfo();
56 }
57
58 void copyNewAnimationsToActiveInterpolationMap(const Vector<InertAnimation*>& newAnimations, WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >& result)
59 {
60     for (size_t i = 0; i < newAnimations.size(); ++i) {
61         OwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > sample = newAnimations[i]->sample(0);
62         if (sample) {
63             copyToActiveInterpolationMap(*sample, result);
64         }
65     }
66 }
67
68 } // namespace
69
70 AnimationStack::AnimationStack()
71 {
72 }
73
74 bool AnimationStack::affects(CSSPropertyID property) const
75 {
76     for (size_t i = 0; i < m_effects.size(); ++i) {
77         if (m_effects[i]->animation() && m_effects[i]->animation()->affects(property))
78             return true;
79     }
80     return false;
81 }
82
83 bool AnimationStack::hasActiveAnimationsOnCompositor(CSSPropertyID property) const
84 {
85     for (size_t i = 0; i < m_effects.size(); ++i) {
86         if (m_effects[i]->animation() && m_effects[i]->animation()->hasActiveAnimationsOnCompositor(property))
87             return true;
88     }
89     return false;
90 }
91
92 WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > AnimationStack::activeInterpolations(AnimationStack* animationStack, const Vector<InertAnimation*>* newAnimations, const HashSet<const AnimationPlayer*>* cancelledAnimationPlayers, Animation::Priority priority, double timelineCurrentTime)
93 {
94     // We don't exactly know when new animations will start, but timelineCurrentTime is a good estimate.
95
96     WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> > result;
97
98     if (animationStack) {
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())))
106                 continue;
107             if (newAnimations && effect.sortInfo().startTime() > timelineCurrentTime) {
108                 copyNewAnimationsToActiveInterpolationMap(*newAnimations, result);
109                 newAnimations = 0;
110             }
111             copyToActiveInterpolationMap(effect.interpolations(), result);
112         }
113     }
114
115     if (newAnimations)
116         copyNewAnimationsToActiveInterpolationMap(*newAnimations, result);
117
118     return result;
119 }
120
121 void AnimationStack::simplifyEffects()
122 {
123     // FIXME: This will need to be updated when we have 'add' keyframes.
124
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());
132         }
133     }
134
135     size_t dest = 0;
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]);
139             continue;
140         }
141         if (m_effects[i]->animation())
142             m_effects[i]->animation()->notifySampledEffectRemovedFromAnimationStack();
143     }
144     m_effects.shrink(dest);
145 }
146
147 } // namespace WebCore