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.
31 #ifndef KeyframeEffectModel_h
32 #define KeyframeEffectModel_h
34 #include "core/animation/AnimatableValueKeyframe.h"
35 #include "core/animation/AnimationEffect.h"
36 #include "core/animation/InterpolationEffect.h"
37 #include "core/animation/StringKeyframe.h"
38 #include "core/animation/TimedItem.h"
39 #include "platform/animation/TimingFunction.h"
40 #include "platform/heap/Handle.h"
41 #include "wtf/HashMap.h"
42 #include "wtf/HashSet.h"
43 #include "wtf/PassOwnPtr.h"
44 #include "wtf/PassRefPtr.h"
45 #include "wtf/RefCounted.h"
46 #include "wtf/Vector.h"
51 class KeyframeEffectModelTest;
53 class KeyframeEffectModelBase : public AnimationEffect {
55 // FIXME: Implement accumulation.
57 typedef WillBeHeapVector<OwnPtrWillBeMember<Keyframe::PropertySpecificKeyframe> > PropertySpecificKeyframeVector;
58 class PropertySpecificKeyframeGroup : public NoBaseWillBeGarbageCollected<PropertySpecificKeyframeGroup> {
60 void appendKeyframe(PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe>);
61 const PropertySpecificKeyframeVector& keyframes() const { return m_keyframes; }
66 void removeRedundantKeyframes();
67 void addSyntheticKeyframeIfRequired(const KeyframeEffectModelBase* context);
69 PropertySpecificKeyframeVector m_keyframes;
71 friend class KeyframeEffectModelBase;
76 PropertySet properties() const;
78 typedef WillBeHeapVector<RefPtrWillBeMember<Keyframe> > KeyframeVector;
79 const KeyframeVector& getFrames() const { return m_keyframes; }
80 // FIXME: Implement setFrames()
82 const PropertySpecificKeyframeVector& getPropertySpecificKeyframes(CSSPropertyID id) const
84 ensureKeyframeGroups();
85 return m_keyframeGroups->get(id)->keyframes();
88 // AnimationEffect implementation.
89 virtual PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > sample(int iteration, double fraction, double iterationDuration) const OVERRIDE;
91 virtual bool isKeyframeEffectModel() const OVERRIDE { return true; }
93 virtual bool isAnimatableValueKeyframeEffectModel() const { return false; }
94 virtual bool isStringKeyframeEffectModel() const { return false; }
96 virtual void trace(Visitor*) OVERRIDE;
98 // FIXME: This is a hack used to resolve CSSValues to AnimatableValues while we have a valid handle on an element.
99 // This should be removed once StringKeyframes no longer uses InterpolableAnimatableValues.
100 void forceConversionsToAnimatableValues(Element* element)
102 ensureKeyframeGroups();
103 ensureInterpolationEffect(element);
107 static KeyframeVector normalizedKeyframes(const KeyframeVector& keyframes);
109 // Lazily computes the groups of property-specific keyframes.
110 void ensureKeyframeGroups() const;
111 void ensureInterpolationEffect(Element* = 0) const;
113 KeyframeVector m_keyframes;
114 // The spec describes filtering the normalized keyframes at sampling time
115 // to get the 'property-specific keyframes'. For efficiency, we cache the
116 // property-specific lists.
117 typedef WillBeHeapHashMap<CSSPropertyID, OwnPtrWillBeMember<PropertySpecificKeyframeGroup> > KeyframeGroupMap;
118 mutable OwnPtrWillBeMember<KeyframeGroupMap> m_keyframeGroups;
119 mutable RefPtrWillBeMember<InterpolationEffect> m_interpolationEffect;
121 friend class KeyframeEffectModelTest;
123 bool affects(CSSPropertyID property)
125 ensureKeyframeGroups();
126 return m_keyframeGroups->contains(property);
130 template <class Keyframe>
131 class KeyframeEffectModel FINAL : public KeyframeEffectModelBase {
133 typedef WillBeHeapVector<RefPtrWillBeMember<Keyframe> > KeyframeVector;
134 static PassRefPtrWillBeRawPtr<KeyframeEffectModel<Keyframe> > create(const KeyframeVector& keyframes) { return adoptRefWillBeNoop(new KeyframeEffectModel(keyframes)); }
137 KeyframeEffectModel(const KeyframeVector& keyframes)
139 m_keyframes.appendVector(keyframes);
142 virtual bool isAnimatableValueKeyframeEffectModel() const { return false; }
143 virtual bool isStringKeyframeEffectModel() const { return false; }
147 typedef KeyframeEffectModelBase::KeyframeVector KeyframeVector;
148 typedef KeyframeEffectModelBase::PropertySpecificKeyframeVector PropertySpecificKeyframeVector;
150 typedef KeyframeEffectModel<AnimatableValueKeyframe> AnimatableValueKeyframeEffectModel;
151 typedef AnimatableValueKeyframeEffectModel::KeyframeVector AnimatableValueKeyframeVector;
152 typedef AnimatableValueKeyframeEffectModel::PropertySpecificKeyframeVector AnimatableValuePropertySpecificKeyframeVector;
154 typedef KeyframeEffectModel<StringKeyframe> StringKeyframeEffectModel;
155 typedef StringKeyframeEffectModel::KeyframeVector StringKeyframeVector;
156 typedef StringKeyframeEffectModel::PropertySpecificKeyframeVector StringPropertySpecificKeyframeVector;
158 DEFINE_TYPE_CASTS(KeyframeEffectModelBase, AnimationEffect, value, value->isKeyframeEffectModel(), value.isKeyframeEffectModel());
159 DEFINE_TYPE_CASTS(AnimatableValueKeyframeEffectModel, KeyframeEffectModelBase, value, value->isAnimatableValueKeyframeEffectModel(), value.isAnimatableValueKeyframeEffectModel());
161 inline const AnimatableValueKeyframeEffectModel* toAnimatableValueKeyframeEffectModel(const AnimationEffect* base)
163 return toAnimatableValueKeyframeEffectModel(toKeyframeEffectModelBase(base));
166 inline AnimatableValueKeyframeEffectModel* toAnimatableValueKeyframeEffectModel(AnimationEffect* base)
168 return toAnimatableValueKeyframeEffectModel(toKeyframeEffectModelBase(base));
172 inline bool KeyframeEffectModel<AnimatableValueKeyframe>::isAnimatableValueKeyframeEffectModel() const { return true; }
175 inline bool KeyframeEffectModel<StringKeyframe>::isStringKeyframeEffectModel() const { return true; }
177 } // namespace WebCore
179 #endif // KeyframeEffectModel_h