Upstream version 7.36.149.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/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"
47
48 namespace WebCore {
49
50 class Element;
51 class KeyframeEffectModelTest;
52
53 class KeyframeEffectModelBase : public AnimationEffect {
54 public:
55     // FIXME: Implement accumulation.
56
57     typedef WillBeHeapVector<OwnPtrWillBeMember<Keyframe::PropertySpecificKeyframe> > PropertySpecificKeyframeVector;
58     class PropertySpecificKeyframeGroup : public NoBaseWillBeGarbageCollected<PropertySpecificKeyframeGroup> {
59     public:
60         void appendKeyframe(PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe>);
61         const PropertySpecificKeyframeVector& keyframes() const { return m_keyframes; }
62
63         void trace(Visitor*);
64
65     private:
66         void removeRedundantKeyframes();
67         void addSyntheticKeyframeIfRequired(const KeyframeEffectModelBase* context);
68
69         PropertySpecificKeyframeVector m_keyframes;
70
71         friend class KeyframeEffectModelBase;
72     };
73
74     bool isReplaceOnly();
75
76     PropertySet properties() const;
77
78     typedef WillBeHeapVector<RefPtrWillBeMember<Keyframe> > KeyframeVector;
79     const KeyframeVector& getFrames() const { return m_keyframes; }
80     // FIXME: Implement setFrames()
81
82     const PropertySpecificKeyframeVector& getPropertySpecificKeyframes(CSSPropertyID id) const
83     {
84         ensureKeyframeGroups();
85         return m_keyframeGroups->get(id)->keyframes();
86     }
87
88     // AnimationEffect implementation.
89     virtual PassOwnPtrWillBeRawPtr<WillBeHeapVector<RefPtrWillBeMember<Interpolation> > > sample(int iteration, double fraction, double iterationDuration) const OVERRIDE;
90
91     virtual bool isKeyframeEffectModel() const OVERRIDE { return true; }
92
93     virtual bool isAnimatableValueKeyframeEffectModel() const { return false; }
94     virtual bool isStringKeyframeEffectModel() const { return false; }
95
96     virtual void trace(Visitor*) OVERRIDE;
97
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)
101     {
102         ensureKeyframeGroups();
103         ensureInterpolationEffect(element);
104     }
105
106 protected:
107     static KeyframeVector normalizedKeyframes(const KeyframeVector& keyframes);
108
109     // Lazily computes the groups of property-specific keyframes.
110     void ensureKeyframeGroups() const;
111     void ensureInterpolationEffect(Element* = 0) const;
112
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;
120
121     friend class KeyframeEffectModelTest;
122
123     bool affects(CSSPropertyID property)
124     {
125         ensureKeyframeGroups();
126         return m_keyframeGroups->contains(property);
127     }
128 };
129
130 template <class Keyframe>
131 class KeyframeEffectModel FINAL : public KeyframeEffectModelBase {
132 public:
133     typedef WillBeHeapVector<RefPtrWillBeMember<Keyframe> > KeyframeVector;
134     static PassRefPtrWillBeRawPtr<KeyframeEffectModel<Keyframe> > create(const KeyframeVector& keyframes) { return adoptRefWillBeNoop(new KeyframeEffectModel(keyframes)); }
135
136 private:
137     KeyframeEffectModel(const KeyframeVector& keyframes)
138     {
139         m_keyframes.appendVector(keyframes);
140     }
141
142     virtual bool isAnimatableValueKeyframeEffectModel() const { return false; }
143     virtual bool isStringKeyframeEffectModel() const { return false; }
144
145 };
146
147 typedef KeyframeEffectModelBase::KeyframeVector KeyframeVector;
148 typedef KeyframeEffectModelBase::PropertySpecificKeyframeVector PropertySpecificKeyframeVector;
149
150 typedef KeyframeEffectModel<AnimatableValueKeyframe> AnimatableValueKeyframeEffectModel;
151 typedef AnimatableValueKeyframeEffectModel::KeyframeVector AnimatableValueKeyframeVector;
152 typedef AnimatableValueKeyframeEffectModel::PropertySpecificKeyframeVector AnimatableValuePropertySpecificKeyframeVector;
153
154 typedef KeyframeEffectModel<StringKeyframe> StringKeyframeEffectModel;
155 typedef StringKeyframeEffectModel::KeyframeVector StringKeyframeVector;
156 typedef StringKeyframeEffectModel::PropertySpecificKeyframeVector StringPropertySpecificKeyframeVector;
157
158 DEFINE_TYPE_CASTS(KeyframeEffectModelBase, AnimationEffect, value, value->isKeyframeEffectModel(), value.isKeyframeEffectModel());
159 DEFINE_TYPE_CASTS(AnimatableValueKeyframeEffectModel, KeyframeEffectModelBase, value, value->isAnimatableValueKeyframeEffectModel(), value.isAnimatableValueKeyframeEffectModel());
160
161 inline const AnimatableValueKeyframeEffectModel* toAnimatableValueKeyframeEffectModel(const AnimationEffect* base)
162 {
163     return toAnimatableValueKeyframeEffectModel(toKeyframeEffectModelBase(base));
164 }
165
166 inline AnimatableValueKeyframeEffectModel* toAnimatableValueKeyframeEffectModel(AnimationEffect* base)
167 {
168     return toAnimatableValueKeyframeEffectModel(toKeyframeEffectModelBase(base));
169 }
170
171 template <>
172 inline bool KeyframeEffectModel<AnimatableValueKeyframe>::isAnimatableValueKeyframeEffectModel() const { return true; }
173
174 template <>
175 inline bool KeyframeEffectModel<StringKeyframe>::isStringKeyframeEffectModel() const { return true; }
176
177 } // namespace WebCore
178
179 #endif // KeyframeEffectModel_h