Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / KeyframeEffectModel.h
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 #ifndef KeyframeEffectModel_h
32 #define KeyframeEffectModel_h
33
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"
42
43 namespace WebCore {
44
45 typedef HashSet<CSSPropertyID> PropertySet;
46
47 class KeyframeEffectModelTest;
48
49 // Represents the keyframes set through the API.
50 class Keyframe : public RefCounted<Keyframe> {
51 public:
52     static PassRefPtr<Keyframe> create()
53     {
54         return adoptRef(new Keyframe);
55     }
56     static bool compareOffsets(const RefPtr<Keyframe>& a, const RefPtr<Keyframe>& b)
57     {
58         return a->offset() < b->offset();
59     }
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;
70 private:
71     Keyframe();
72     Keyframe(const Keyframe&);
73     double m_offset;
74     AnimationEffect::CompositeOperation m_composite;
75     typedef HashMap<CSSPropertyID, RefPtr<AnimatableValue> > PropertyValueMap;
76     PropertyValueMap m_propertyValues;
77 };
78
79 class KeyframeEffectModel FINAL : public AnimationEffect {
80 public:
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)
86     {
87         return adoptRef(new KeyframeEffectModel(keyframes));
88     }
89
90     virtual bool affects(CSSPropertyID property) OVERRIDE
91     {
92         ensureKeyframeGroups();
93         return m_keyframeGroups->contains(property);
94     }
95
96     // AnimationEffect implementation.
97     virtual PassOwnPtr<CompositableValueList> sample(int iteration, double fraction) const OVERRIDE;
98
99     // FIXME: Implement setFrames()
100     const KeyframeVector& getFrames() const { return m_keyframes; }
101
102     virtual bool isKeyframeEffectModel() const OVERRIDE { return true; }
103
104     PropertySet properties() const;
105
106     class PropertySpecificKeyframe {
107     public:
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;
112     private:
113         // Used by cloneWithOffset().
114         PropertySpecificKeyframe(double offset, PassRefPtr<CompositableValue>);
115         double m_offset;
116         RefPtr<CompositableValue> m_value;
117     };
118
119     class PropertySpecificKeyframeGroup {
120     public:
121         void appendKeyframe(PassOwnPtr<PropertySpecificKeyframe>);
122         PassRefPtr<CompositableValue> sample(int iteration, double offset) const;
123         const PropertySpecificKeyframeVector& keyframes() const { return m_keyframes; }
124     private:
125         PropertySpecificKeyframeVector m_keyframes;
126         void removeRedundantKeyframes();
127         void addSyntheticKeyframeIfRequired();
128
129         friend class KeyframeEffectModel;
130     };
131
132     const PropertySpecificKeyframeVector& getPropertySpecificKeyframes(CSSPropertyID id) const
133     {
134         ensureKeyframeGroups();
135         return m_keyframeGroups->get(id)->keyframes();
136     }
137
138 private:
139     KeyframeEffectModel(const KeyframeVector& keyframes);
140
141     static KeyframeVector normalizedKeyframes(const KeyframeVector& keyframes);
142
143     // Lazily computes the groups of property-specific keyframes.
144     void ensureKeyframeGroups() const;
145
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;
152
153     friend class KeyframeEffectModelTest;
154 };
155
156 DEFINE_TYPE_CASTS(KeyframeEffectModel, AnimationEffect, value, value->isKeyframeEffectModel(), value.isKeyframeEffectModel());
157
158 } // namespace WebCore
159
160 #endif // KeyframeEffectModel_h