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/KeyframeEffectModel.h"
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"
42 PropertySet KeyframeEffectModelBase::properties() const
45 for (const auto& keyframe : m_keyframes) {
46 for (const auto& property : keyframe->properties())
52 PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation>>> KeyframeEffectModelBase::sample(int iteration, double fraction, double iterationDuration) const
54 ASSERT(iteration >= 0);
55 ASSERT(!isNull(fraction));
56 ensureKeyframeGroups();
57 ensureInterpolationEffect();
59 return m_interpolationEffect->getActiveInterpolations(fraction, iterationDuration);
62 KeyframeEffectModelBase::KeyframeVector KeyframeEffectModelBase::normalizedKeyframes(const KeyframeVector& keyframes)
64 double lastOffset = 0;
65 KeyframeVector result;
66 result.reserveCapacity(keyframes.size());
68 for (const auto& keyframe : keyframes) {
69 double offset = keyframe->offset();
70 if (!isNull(offset)) {
73 ASSERT(offset >= lastOffset);
76 result.append(keyframe->clone());
82 if (isNull(result.last()->offset()))
83 result.last()->setOffset(1);
85 if (result.size() > 1 && isNull(result[0]->offset()))
86 result.first()->setOffset(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));
104 void KeyframeEffectModelBase::ensureKeyframeGroups() const
106 if (m_keyframeGroups)
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();
118 group = groupIter->value.get();
120 group->appendKeyframe(keyframe->createPropertySpecificKeyframe(property));
124 // Add synthetic keyframes.
125 for (const auto& entry : *m_keyframeGroups) {
126 entry.value->addSyntheticKeyframeIfRequired(this);
127 entry.value->removeRedundantKeyframes();
131 void KeyframeEffectModelBase::ensureInterpolationEffect(Element* element) const
133 if (m_interpolationEffect)
135 m_interpolationEffect = InterpolationEffect::create();
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();
145 applyTo = std::numeric_limits<double>::infinity();
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);
153 bool KeyframeEffectModelBase::isReplaceOnly()
155 ensureKeyframeGroups();
156 for (const auto& entry : *m_keyframeGroups) {
157 for (const auto& keyframe : entry.value->keyframes()) {
158 if (keyframe->composite() != AnimationEffect::CompositeReplace)
165 void KeyframeEffectModelBase::trace(Visitor* visitor)
167 visitor->trace(m_keyframes);
168 visitor->trace(m_interpolationEffect);
170 visitor->trace(m_keyframeGroups);
172 AnimationEffect::trace(visitor);
175 Keyframe::PropertySpecificKeyframe::PropertySpecificKeyframe(double offset, PassRefPtr<TimingFunction> easing, AnimationEffect::CompositeOperation composite)
178 , m_composite(composite)
182 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::appendKeyframe(PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe> keyframe)
184 ASSERT(m_keyframes.isEmpty() || m_keyframes.last()->offset() <= keyframe->offset());
185 m_keyframes.append(keyframe);
188 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::removeRedundantKeyframes()
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
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);
204 ASSERT(m_keyframes.size() >= 2);
207 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::addSyntheticKeyframeIfRequired(const KeyframeEffectModelBase* context)
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));
216 void KeyframeEffectModelBase::PropertySpecificKeyframeGroup::trace(Visitor* visitor)
219 visitor->trace(m_keyframes);