Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / KeyframeEffectModel.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/KeyframeEffectModel.h"
33
34 #include "core/StylePropertyShorthand.h"
35 #include "core/animation/AnimationNode.h"
36 #include "platform/geometry/FloatBox.h"
37 #include "platform/transforms/TransformationMatrix.h"
38 #include "wtf/text/StringHash.h"
39
40 namespace blink {
41
42 PropertySet KeyframeEffectModelBase::properties() const
43 {
44     PropertySet result;
45     for (const auto& keyframe : m_keyframes) {
46         for (const auto& property : keyframe->properties())
47             result.add(property);
48     }
49     return result;
50 }
51
52 PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation>>> KeyframeEffectModelBase::sample(int iteration, double fraction, double iterationDuration) const
53 {
54     ASSERT(iteration >= 0);
55     ASSERT(!isNull(fraction));
56     ensureKeyframeGroups();
57     ensureInterpolationEffect();
58
59     return m_interpolationEffect->getActiveInterpolations(fraction, iterationDuration);
60 }
61
62 KeyframeEffectModelBase::KeyframeVector KeyframeEffectModelBase::normalizedKeyframes(const KeyframeVector& keyframes)
63 {
64     double lastOffset = 0;
65     KeyframeVector result;
66     result.reserveCapacity(keyframes.size());
67
68     for (const auto& keyframe : keyframes) {
69         double offset = keyframe->offset();
70         if (!isNull(offset)) {
71             ASSERT(offset >= 0);
72             ASSERT(offset <= 1);
73             ASSERT(offset >= lastOffset);
74             lastOffset = offset;
75         }
76         result.append(keyframe->clone());
77     }
78
79     if (result.isEmpty())
80         return result;
81
82     if (isNull(result.last()->offset()))
83         result.last()->setOffset(1);
84
85     if (result.size() > 1 && isNull(result[0]->offset()))
86         result.first()->setOffset(0);
87
88     size_t lastIndex = 0;
89     lastOffset = result.first()->offset();
90     for (size_t i = 1; i < result.size(); ++i) {
91         double offset = result[i]->offset();
92         if (!isNull(offset)) {
93             for (size_t j = 1; j < i - lastIndex; ++j)
94                 result[lastIndex + j]->setOffset(lastOffset + (offset - lastOffset) * j / (i - lastIndex));
95             lastIndex = i;
96             lastOffset = offset;
97         }
98     }
99
100     return result;
101 }
102
103
104 void KeyframeEffectModelBase::ensureKeyframeGroups() const
105 {
106     if (m_keyframeGroups)
107         return;
108
109     m_keyframeGroups = adoptPtrWillBeNoop(new KeyframeGroupMap);
110     for (const auto& keyframe : normalizedKeyframes(getFrames())) {
111         for (CSSPropertyID property : keyframe->properties()) {
112             ASSERT_WITH_MESSAGE(!isShorthandProperty(property), "Web Animations: Encountered shorthand CSS property (%d) in normalized keyframes.", property);
113             KeyframeGroupMap::iterator groupIter = m_keyframeGroups->find(property);
114             PropertySpecificKeyframeGroup* group;
115             if (groupIter == m_keyframeGroups->end())
116                 group = m_keyframeGroups->add(property, adoptPtrWillBeNoop(new PropertySpecificKeyframeGroup)).storedValue->value.get();
117             else
118                 group = groupIter->value.get();
119
120             group->appendKeyframe(keyframe->createPropertySpecificKeyframe(property));
121         }
122     }
123
124     // Add synthetic keyframes.
125     for (const auto& entry : *m_keyframeGroups) {
126         entry.value->addSyntheticKeyframeIfRequired(this);
127         entry.value->removeRedundantKeyframes();
128     }
129 }
130
131 void KeyframeEffectModelBase::ensureInterpolationEffect(Element* element) const
132 {
133     if (m_interpolationEffect)
134         return;
135     m_interpolationEffect = InterpolationEffect::create();
136
137     for (const auto& entry : *m_keyframeGroups) {
138         const PropertySpecificKeyframeVector& keyframes = entry.value->keyframes();
139         ASSERT(keyframes[0]->composite() == AnimationEffect::CompositeReplace);
140         for (size_t i = 0; i < keyframes.size() - 1; i++) {
141             ASSERT(keyframes[i + 1]->composite() == AnimationEffect::CompositeReplace);
142             double applyFrom = i ? keyframes[i]->offset() : (-std::numeric_limits<double>::infinity());
143             double applyTo = i == keyframes.size() - 2 ? std::numeric_limits<double>::infinity() : keyframes[i + 1]->offset();
144             if (applyTo == 1)
145                 applyTo = std::numeric_limits<double>::infinity();
146
147             m_interpolationEffect->addInterpolation(keyframes[i]->createInterpolation(entry.key, keyframes[i + 1].get(), element),
148                 &keyframes[i]->easing(), keyframes[i]->offset(), keyframes[i + 1]->offset(), applyFrom, applyTo);
149         }
150     }
151 }
152
153 bool KeyframeEffectModelBase::isReplaceOnly()
154 {
155     ensureKeyframeGroups();
156     for (const auto& entry : *m_keyframeGroups) {
157         for (const auto& keyframe : entry.value->keyframes()) {
158             if (keyframe->composite() != AnimationEffect::CompositeReplace)
159                 return false;
160         }
161     }
162     return true;
163 }
164
165 void KeyframeEffectModelBase::trace(Visitor* visitor)
166 {
167     visitor->trace(m_keyframes);
168     visitor->trace(m_interpolationEffect);
169 #if ENABLE_OILPAN
170     visitor->trace(m_keyframeGroups);
171 #endif
172     AnimationEffect::trace(visitor);
173 }
174
175 Keyframe::PropertySpecificKeyframe::PropertySpecificKeyframe(double offset, PassRefPtr<TimingFunction> easing, AnimationEffect::CompositeOperation composite)
176     : m_offset(offset)
177     , m_easing(easing)
178     , m_composite(composite)
179 {
180 }
181
182 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::appendKeyframe(PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe> keyframe)
183 {
184     ASSERT(m_keyframes.isEmpty() || m_keyframes.last()->offset() <= keyframe->offset());
185     m_keyframes.append(keyframe);
186 }
187
188 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::removeRedundantKeyframes()
189 {
190     // As an optimization, removes keyframes in the following categories, as
191     // they will never be used by sample().
192     // - End keyframes with the same offset as their neighbor
193     // - Interior keyframes with the same offset as both their neighbors
194     // Note that synthetic keyframes must be added before this method is
195     // called.
196     ASSERT(m_keyframes.size() >= 2);
197     for (int i = m_keyframes.size() - 1; i >= 0; --i) {
198         double offset = m_keyframes[i]->offset();
199         bool hasSameOffsetAsPreviousNeighbor = !i || m_keyframes[i - 1]->offset() == offset;
200         bool hasSameOffsetAsNextNeighbor = i == static_cast<int>(m_keyframes.size() - 1) || m_keyframes[i + 1]->offset() == offset;
201         if (hasSameOffsetAsPreviousNeighbor && hasSameOffsetAsNextNeighbor)
202             m_keyframes.remove(i);
203     }
204     ASSERT(m_keyframes.size() >= 2);
205 }
206
207 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::addSyntheticKeyframeIfRequired(const KeyframeEffectModelBase* context)
208 {
209     ASSERT(!m_keyframes.isEmpty());
210     if (m_keyframes.first()->offset() != 0.0)
211         m_keyframes.insert(0, m_keyframes.first()->neutralKeyframe(0, nullptr));
212     if (m_keyframes.last()->offset() != 1.0)
213         appendKeyframe(m_keyframes.last()->neutralKeyframe(1, nullptr));
214 }
215
216 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::trace(Visitor* visitor)
217 {
218 #if ENABLE(OILPAN)
219     visitor->trace(m_keyframes);
220 #endif
221 }
222
223 } // namespace