Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Side by Side Diff: Source/core/animation/css/CSSAnimations.cpp

Issue 21012002: Web Animations: Trigger and update CSS Animations backed by the Web Animations model (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Recompute and apply updates to active animations after style resolution. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #include "config.h"
32 #include "core/animation/css/CSSAnimations.h"
33
34 #include "core/animation/DocumentTimeline.h"
35 #include "core/animation/KeyframeAnimationEffect.h"
36 #include "core/css/CSSKeyframeRule.h"
37 #include "core/css/resolver/StyleResolver.h"
38 #include "core/dom/AnimationEvent.h"
39 #include "core/dom/Element.h"
40 #include "core/dom/EventNames.h"
41 #include "core/platform/animation/CSSAnimationDataList.h"
42 #include "core/platform/animation/TimingFunction.h"
43 #include "wtf/HashSet.h"
44
45 namespace WebCore {
46
47 void timingFromAnimationData(const CSSAnimationData* animationData, Timing& timi ng)
48 {
49 if (animationData->isDelaySet())
50 timing.startDelay = animationData->delay();
51 if (animationData->isDurationSet()) {
52 timing.iterationDuration = animationData->duration();
53 timing.hasIterationDuration = true;
54 }
55 if (animationData->isIterationCountSet()) {
56 if (animationData->iterationCount() == CSSAnimationData::IterationCountI nfinite)
57 timing.iterationCount = std::numeric_limits<double>::infinity();
58 else
59 timing.iterationCount = animationData->iterationCount();
60 }
61 if (animationData->isTimingFunctionSet()) {
62 if (!animationData->timingFunction()->isLinearTimingFunction())
63 timing.timingFunction = animationData->timingFunction();
64 } else {
65 // CSS default is ease, default in model is linear.
66 timing.timingFunction = CubicBezierTimingFunction::preset(CubicBezierTim ingFunction::Ease);
67 }
68 if (animationData->isFillModeSet()) {
69 switch (animationData->fillMode()) {
70 case AnimationFillModeForwards:
71 timing.fillMode = Timing::FillModeForwards;
72 break;
73 case AnimationFillModeBackwards:
74 timing.fillMode = Timing::FillModeBackwards;
75 break;
76 case AnimationFillModeBoth:
77 timing.fillMode = Timing::FillModeBoth;
78 break;
79 case AnimationFillModeNone:
80 timing.fillMode = Timing::FillModeNone;
81 break;
82 default:
83 ASSERT_NOT_REACHED();
84 }
85 }
86 if (animationData->isDirectionSet()) {
87 switch (animationData->direction()) {
88 case CSSAnimationData::AnimationDirectionNormal:
89 timing.direction = Timing::PlaybackDirectionNormal;
90 break;
91 case CSSAnimationData::AnimationDirectionAlternate:
92 timing.direction = Timing::PlaybackDirectionAlternate;
93 break;
94 case CSSAnimationData::AnimationDirectionReverse:
95 timing.direction = Timing::PlaybackDirectionReverse;
96 break;
97 case CSSAnimationData::AnimationDirectionAlternateReverse:
98 timing.direction = Timing::PlaybackDirectionAlternateReverse;
99 break;
100 default:
101 ASSERT_NOT_REACHED();
102 }
103 }
104 }
105
106 void resolveKeyframes(Element* element, const StringImpl* name, StyleResolver* r esolver, KeyframeAnimationEffect::KeyframeVector& keyframes)
107 {
108 const StyleRuleKeyframes* keyframesRule = resolver->matchScopedKeyframesRule (element, name);
109 if (!keyframesRule)
110 return;
111
112 // Construct and populate the style for each keyframe
113 const Vector<RefPtr<StyleKeyframe> >& styleKeyframes = keyframesRule->keyfra mes();
114 for (unsigned i = 0; i < styleKeyframes.size(); ++i) {
115 const StyleKeyframe* styleKeyframe = styleKeyframes[i].get();
116
117 Vector<float> offsets;
118 styleKeyframe->getKeys(offsets);
119 for (size_t j = 0; j < offsets.size(); ++j) {
120 RefPtr<Keyframe> keyframe = Keyframe::create();
121 keyframe->setOffset(offsets[j]);
122 const StylePropertySet* properties = styleKeyframe->properties();
123 // FIXME: AnimatableValues should be shared between the keyframes at different offsets.
124 for (unsigned k = 0; k < properties->propertyCount(); k++) {
125 CSSPropertyID property = properties->propertyAt(k).id();
126 // FIXME: CSSValue needs to be resolved.
127 keyframe->setPropertyValue(property, AnimatableValue::create(pro perties->getPropertyCSSValue(property).get()).get());
128 }
129 keyframes.append(keyframe);
130 }
131 }
132
133 // FIXME: If the 0% keyframe is missing, create it (but only if there is at least one other keyframe)
134 // FIXME: If the 100% keyframe is missing, create it (but only if there is a t least one other keyframe)
135 }
136
137 const StylePropertySet* firstKeyframeStyles(const Element* element, const String Impl* animationName, StyleResolver* resolver)
dglazkov 2013/08/01 16:12:46 We should probably move this to StyleResolver, sin
dstockwell 2013/08/02 00:11:01 Done.
138 {
139 const StyleRuleKeyframes* keyframesRule = resolver->matchScopedKeyframesRule (element, animationName);
140 if (!keyframesRule)
141 return 0;
142
143 // Find the last keyframe at offset 0
144 const StyleKeyframe* firstKeyframe = 0;
145 const Vector<RefPtr<StyleKeyframe> >& styleKeyframes = keyframesRule->keyfra mes();
146 for (unsigned i = 0; i < styleKeyframes.size(); ++i) {
147 const StyleKeyframe* styleKeyframe = styleKeyframes[i].get();
148
149 Vector<float> offsets;
150 styleKeyframe->getKeys(offsets);
151 for (size_t j = 0; j < offsets.size(); ++j) {
152 if (!offsets[j]) {
153 firstKeyframe = styleKeyframe;
154 break;
155 }
156 }
157 }
158
159 return firstKeyframe ? firstKeyframe->properties() : 0;
160 }
161
162 bool CSSAnimations::needsUpdate(const Element* element, const RenderStyle* style )
163 {
164 ActiveAnimations* activeAnimations = element->activeAnimations();
165 const CSSAnimationDataList* animations = style->animations();
166 const CSSAnimations* cssAnimations = activeAnimations ? activeAnimations->cs sAnimations() : 0;
167 EDisplay display = style->display();
168 return (display != NONE && animations && animations->size()) || (cssAnimatio ns && !cssAnimations->isEmpty());
169 }
170
171 PassOwnPtr<CSSAnimationUpdate> CSSAnimations::calculateUpdate(const Element* ele ment, EDisplay display, const CSSAnimations* cssAnimations, const CSSAnimationDa taList* animationDataList, StyleResolver* resolver)
172 {
173 OwnPtr<CSSAnimationUpdate> update;
174 HashSet<StringImpl*> inactive;
175 if (cssAnimations)
176 for (AnimationMap::const_iterator iter = cssAnimations->m_animations.beg in(); iter != cssAnimations->m_animations.end(); ++iter)
177 inactive.add(iter->key);
178
179 RefPtr<MutableStylePropertySet> newStyles;
180 if (display != NONE) {
181 for (int i = 0; animationDataList && i < animationDataList->size(); ++i) {
182 const CSSAnimationData* animationData = animationDataList->animation (i);
183 ASSERT(animationData->isValidAnimation());
184 AtomicString animationName(animationData->name());
185
186 if (cssAnimations) {
187 AnimationMap::const_iterator existing(cssAnimations->m_animation s.find(animationName.impl()));
188 if (existing != cssAnimations->m_animations.end()) {
189 inactive.remove(animationName.impl());
190 continue;
191 }
192 }
193
194 // If there's a delay, no styles will apply yet.
195 if (animationData->isDelaySet() && animationData->delay())
196 continue;
197
198 const StylePropertySet* keyframeStyles = firstKeyframeStyles(element , animationName.impl(), resolver);
199 if (keyframeStyles) {
200 if (!update)
201 update = adoptPtr(new CSSAnimationUpdate());
202 update->addStyles(keyframeStyles);
203 }
204 }
205 }
206
207 if (!inactive.isEmpty() && !update)
208 update = adoptPtr(new CSSAnimationUpdate());
209 for (HashSet<StringImpl*>::const_iterator iter = inactive.begin(); iter != i nactive.end(); ++iter)
210 update->cancel(cssAnimations->m_animations.get(*iter));
211
212 return update.release();
213 }
214
215 void CSSAnimations::update(Element* element, const RenderStyle* style)
216 {
217 const CSSAnimationDataList* animationDataList = style->animations();
218 HashSet<StringImpl*> inactive;
219 for (AnimationMap::const_iterator iter = m_animations.begin(); iter != m_ani mations.end(); ++iter)
220 inactive.add(iter->key);
221
222 if (style->display() != NONE) {
223 for (int i = 0; animationDataList && i < animationDataList->size(); ++i) {
224 const CSSAnimationData* animationData = animationDataList->animation (i);
225 ASSERT(animationData->isValidAnimation());
226 AtomicString animationName(animationData->name());
227
228 AnimationMap::const_iterator existing(m_animations.find(animationNam e.impl()));
229 if (existing != m_animations.end()) {
230 bool paused = animationData->playState() == AnimPlayStatePaused;
231 existing->value->setPaused(paused);
232 inactive.remove(animationName.impl());
233 continue;
234 }
235
236 KeyframeAnimationEffect::KeyframeVector keyframes;
237 resolveKeyframes(element, animationName.impl(), element->document()- >styleResolver(), keyframes);
238 if (!keyframes.isEmpty()) {
239 Timing timing;
240 timingFromAnimationData(animationData, timing);
241 m_animations.set(animationName.impl(), element->document()->time line()->play(
242 Animation::create(element, KeyframeAnimationEffect::create(k eyframes), timing).get()).get());
243 }
244 }
245 }
246
247 for (HashSet<StringImpl*>::const_iterator iter = inactive.begin(); iter != i nactive.end(); ++iter)
248 m_animations.take(*iter)->cancel();
249 }
250
251 void CSSAnimations::cancel()
252 {
253 for (AnimationMap::iterator iter = m_animations.begin(); iter != m_animation s.end(); ++iter)
254 iter->value->cancel();
255
256 m_animations.clear();
257 }
258
259 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698