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/AnimatableValue.h"
35 #include "core/animation/AnimationEffect.h"
36 #include "wtf/HashMap.h"
37 #include "wtf/HashSet.h"
38 #include "wtf/PassOwnPtr.h"
39 #include "wtf/PassRefPtr.h"
40 #include "wtf/RefCounted.h"
41 #include "wtf/Vector.h"
45 typedef HashSet<CSSPropertyID> PropertySet;
47 class KeyframeEffectModelTest;
49 // Represents the keyframes set through the API.
50 class Keyframe : public RefCounted<Keyframe> {
52 static PassRefPtr<Keyframe> create()
54 return adoptRef(new Keyframe);
56 static bool compareOffsets(const RefPtr<Keyframe>& a, const RefPtr<Keyframe>& b)
58 return a->offset() < b->offset();
60 void setOffset(double offset) { m_offset = offset; }
61 double offset() const { return m_offset; }
62 void setComposite(AnimationEffect::CompositeOperation composite) { m_composite = composite; }
63 AnimationEffect::CompositeOperation composite() const { return m_composite; }
64 void setPropertyValue(CSSPropertyID, const AnimatableValue*);
65 void clearPropertyValue(CSSPropertyID);
66 const AnimatableValue* propertyValue(CSSPropertyID) const;
67 PropertySet properties() const;
68 PassRefPtr<Keyframe> clone() const { return adoptRef(new Keyframe(*this)); }
69 PassRefPtr<Keyframe> cloneWithOffset(double offset) const;
72 Keyframe(const Keyframe&);
74 AnimationEffect::CompositeOperation m_composite;
75 typedef HashMap<CSSPropertyID, RefPtr<AnimatableValue> > PropertyValueMap;
76 PropertyValueMap m_propertyValues;
79 class KeyframeEffectModel FINAL : public AnimationEffect {
81 class PropertySpecificKeyframe;
82 typedef Vector<RefPtr<Keyframe> > KeyframeVector;
83 typedef Vector<OwnPtr<PropertySpecificKeyframe> > PropertySpecificKeyframeVector;
84 // FIXME: Implement accumulation.
85 static PassRefPtr<KeyframeEffectModel> create(const KeyframeVector& keyframes)
87 return adoptRef(new KeyframeEffectModel(keyframes));
90 virtual bool affects(CSSPropertyID property) OVERRIDE
92 ensureKeyframeGroups();
93 return m_keyframeGroups->contains(property);
96 // AnimationEffect implementation.
97 virtual PassOwnPtr<CompositableValueList> sample(int iteration, double fraction) const OVERRIDE;
99 // FIXME: Implement setFrames()
100 const KeyframeVector& getFrames() const { return m_keyframes; }
102 virtual bool isKeyframeEffectModel() const OVERRIDE { return true; }
104 PropertySet properties() const;
106 class PropertySpecificKeyframe {
108 PropertySpecificKeyframe(double offset, const AnimatableValue*, CompositeOperation);
109 double offset() const { return m_offset; }
110 const CompositableValue* value() const { return m_value.get(); }
111 PassOwnPtr<PropertySpecificKeyframe> cloneWithOffset(double offset) const;
113 // Used by cloneWithOffset().
114 PropertySpecificKeyframe(double offset, PassRefPtr<CompositableValue>);
116 RefPtr<CompositableValue> m_value;
119 class PropertySpecificKeyframeGroup {
121 void appendKeyframe(PassOwnPtr<PropertySpecificKeyframe>);
122 PassRefPtr<CompositableValue> sample(int iteration, double offset) const;
123 const PropertySpecificKeyframeVector& keyframes() const { return m_keyframes; }
125 PropertySpecificKeyframeVector m_keyframes;
126 void removeRedundantKeyframes();
127 void addSyntheticKeyframeIfRequired();
129 friend class KeyframeEffectModel;
132 const PropertySpecificKeyframeVector& getPropertySpecificKeyframes(CSSPropertyID id) const
134 ensureKeyframeGroups();
135 return m_keyframeGroups->get(id)->keyframes();
139 KeyframeEffectModel(const KeyframeVector& keyframes);
141 static KeyframeVector normalizedKeyframes(const KeyframeVector& keyframes);
143 // Lazily computes the groups of property-specific keyframes.
144 void ensureKeyframeGroups() const;
146 KeyframeVector m_keyframes;
147 // The spec describes filtering the normalized keyframes at sampling time
148 // to get the 'property-specific keyframes'. For efficiency, we cache the
149 // property-specific lists.
150 typedef HashMap<CSSPropertyID, OwnPtr<PropertySpecificKeyframeGroup> > KeyframeGroupMap;
151 mutable OwnPtr<KeyframeGroupMap> m_keyframeGroups;
153 friend class KeyframeEffectModelTest;
156 DEFINE_TYPE_CASTS(KeyframeEffectModel, AnimationEffect, value, value->isKeyframeEffectModel(), value.isKeyframeEffectModel());
158 } // namespace WebCore
160 #endif // KeyframeEffectModel_h