• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Apple 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 #include "AnimationBase.h"
31 
32 #include "AnimationController.h"
33 #include "CSSMutableStyleDeclaration.h"
34 #include "CSSPropertyLonghand.h"
35 #include "CSSPropertyNames.h"
36 #include "CString.h"
37 #include "CompositeAnimation.h"
38 #include "Document.h"
39 #include "EventNames.h"
40 #include "FloatConversion.h"
41 #include "Frame.h"
42 #include "IdentityTransformOperation.h"
43 #include "ImplicitAnimation.h"
44 #include "KeyframeAnimation.h"
45 #include "MatrixTransformOperation.h"
46 #include "RenderObject.h"
47 #include "RenderStyle.h"
48 #include "UnitBezier.h"
49 
50 namespace WebCore {
51 
52 // The epsilon value we pass to UnitBezier::solve given that the animation is going to run over |dur| seconds. The longer the
53 // animation, the more precision we need in the timing function result to avoid ugly discontinuities.
solveEpsilon(double duration)54 static inline double solveEpsilon(double duration)
55 {
56     return 1.0 / (200.0 * duration);
57 }
58 
solveCubicBezierFunction(double p1x,double p1y,double p2x,double p2y,double t,double duration)59 static inline double solveCubicBezierFunction(double p1x, double p1y, double p2x, double p2y, double t, double duration)
60 {
61     // Convert from input time to parametric value in curve, then from
62     // that to output time.
63     UnitBezier bezier(p1x, p1y, p2x, p2y);
64     return bezier.solve(t, solveEpsilon(duration));
65 }
66 
blendFunc(const AnimationBase *,int from,int to,double progress)67 static inline int blendFunc(const AnimationBase*, int from, int to, double progress)
68 {
69     return int(from + (to - from) * progress);
70 }
71 
blendFunc(const AnimationBase *,double from,double to,double progress)72 static inline double blendFunc(const AnimationBase*, double from, double to, double progress)
73 {
74     return from + (to - from) * progress;
75 }
76 
blendFunc(const AnimationBase *,float from,float to,double progress)77 static inline float blendFunc(const AnimationBase*, float from, float to, double progress)
78 {
79     return narrowPrecisionToFloat(from + (to - from) * progress);
80 }
81 
blendFunc(const AnimationBase * anim,const Color & from,const Color & to,double progress)82 static inline Color blendFunc(const AnimationBase* anim, const Color& from, const Color& to, double progress)
83 {
84     // We need to preserve the state of the valid flag at the end of the animation
85     if (progress == 1 && !to.isValid())
86         return Color();
87 
88     return Color(blendFunc(anim, from.red(), to.red(), progress),
89                  blendFunc(anim, from.green(), to.green(), progress),
90                  blendFunc(anim, from.blue(), to.blue(), progress),
91                  blendFunc(anim, from.alpha(), to.alpha(), progress));
92 }
93 
blendFunc(const AnimationBase *,const Length & from,const Length & to,double progress)94 static inline Length blendFunc(const AnimationBase*, const Length& from, const Length& to, double progress)
95 {
96     return to.blend(from, progress);
97 }
98 
blendFunc(const AnimationBase * anim,const IntSize & from,const IntSize & to,double progress)99 static inline IntSize blendFunc(const AnimationBase* anim, const IntSize& from, const IntSize& to, double progress)
100 {
101     return IntSize(blendFunc(anim, from.width(), to.width(), progress),
102                    blendFunc(anim, from.height(), to.height(), progress));
103 }
104 
blendFunc(const AnimationBase * anim,const ShadowData * from,const ShadowData * to,double progress)105 static inline ShadowData* blendFunc(const AnimationBase* anim, const ShadowData* from, const ShadowData* to, double progress)
106 {
107     ASSERT(from && to);
108     return new ShadowData(blendFunc(anim, from->x, to->x, progress), blendFunc(anim, from->y, to->y, progress),
109                           blendFunc(anim, from->blur, to->blur, progress), blendFunc(anim, from->color, to->color, progress));
110 }
111 
blendFunc(const AnimationBase * anim,const TransformOperations & from,const TransformOperations & to,double progress)112 static inline TransformOperations blendFunc(const AnimationBase* anim, const TransformOperations& from, const TransformOperations& to, double progress)
113 {
114     TransformOperations result;
115 
116     // If we have a transform function list, use that to do a per-function animation. Otherwise do a Matrix animation
117     if (anim->isTransformFunctionListValid()) {
118         unsigned fromSize = from.operations().size();
119         unsigned toSize = to.operations().size();
120         unsigned size = max(fromSize, toSize);
121         for (unsigned i = 0; i < size; i++) {
122             RefPtr<TransformOperation> fromOp = (i < fromSize) ? from.operations()[i].get() : 0;
123             RefPtr<TransformOperation> toOp = (i < toSize) ? to.operations()[i].get() : 0;
124             RefPtr<TransformOperation> blendedOp = toOp ? toOp->blend(fromOp.get(), progress) : (fromOp ? fromOp->blend(0, progress, true) : 0);
125             if (blendedOp)
126                 result.operations().append(blendedOp);
127             else {
128                 RefPtr<TransformOperation> identityOp = IdentityTransformOperation::create();
129                 if (progress > 0.5)
130                     result.operations().append(toOp ? toOp : identityOp);
131                 else
132                     result.operations().append(fromOp ? fromOp : identityOp);
133             }
134         }
135     } else {
136         // Convert the TransformOperations into matrices
137         IntSize size = anim->renderer()->isBox() ? toRenderBox(anim->renderer())->borderBoxRect().size() : IntSize();
138         TransformationMatrix fromT;
139         TransformationMatrix toT;
140         from.apply(size, fromT);
141         to.apply(size, toT);
142 
143         toT.blend(fromT, progress);
144 
145         // Append the result
146         result.operations().append(MatrixTransformOperation::create(toT.a(), toT.b(), toT.c(), toT.d(), toT.e(), toT.f()));
147     }
148     return result;
149 }
150 
blendFunc(const AnimationBase * anim,EVisibility from,EVisibility to,double progress)151 static inline EVisibility blendFunc(const AnimationBase* anim, EVisibility from, EVisibility to, double progress)
152 {
153     // Any non-zero result means we consider the object to be visible.  Only at 0 do we consider the object to be
154     // invisible.   The invisible value we use (HIDDEN vs. COLLAPSE) depends on the specified from/to values.
155     double fromVal = from == VISIBLE ? 1. : 0.;
156     double toVal = to == VISIBLE ? 1. : 0.;
157     if (fromVal == toVal)
158         return to;
159     double result = blendFunc(anim, fromVal, toVal, progress);
160     return result > 0. ? VISIBLE : (to != VISIBLE ? to : from);
161 }
162 
163 class PropertyWrapperBase;
164 
165 static void addShorthandProperties();
166 static PropertyWrapperBase* wrapperForProperty(int propertyID);
167 
168 class PropertyWrapperBase {
169 public:
PropertyWrapperBase(int prop)170     PropertyWrapperBase(int prop)
171         : m_prop(prop)
172     {
173     }
174 
~PropertyWrapperBase()175     virtual ~PropertyWrapperBase() { }
176 
isShorthandWrapper() const177     virtual bool isShorthandWrapper() const { return false; }
178     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const = 0;
179     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const = 0;
180 
property() const181     int property() const { return m_prop; }
182 
183 private:
184     int m_prop;
185 };
186 
187 template <typename T>
188 class PropertyWrapperGetter : public PropertyWrapperBase {
189 public:
PropertyWrapperGetter(int prop,T (RenderStyle::* getter)()const)190     PropertyWrapperGetter(int prop, T (RenderStyle::*getter)() const)
191         : PropertyWrapperBase(prop)
192         , m_getter(getter)
193     {
194     }
195 
equals(const RenderStyle * a,const RenderStyle * b) const196     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
197     {
198        // If the style pointers are the same, don't bother doing the test.
199        // If either is null, return false. If both are null, return true.
200        if (!a && !b || a == b)
201            return true;
202        if (!a || !b)
203             return false;
204         return (a->*m_getter)() == (b->*m_getter)();
205     }
206 
207 protected:
208     T (RenderStyle::*m_getter)() const;
209 };
210 
211 template <typename T>
212 class PropertyWrapper : public PropertyWrapperGetter<T> {
213 public:
PropertyWrapper(int prop,T (RenderStyle::* getter)()const,void (RenderStyle::* setter)(T))214     PropertyWrapper(int prop, T (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T))
215         : PropertyWrapperGetter<T>(prop, getter)
216         , m_setter(setter)
217     {
218     }
219 
blend(const AnimationBase * anim,RenderStyle * dst,const RenderStyle * a,const RenderStyle * b,double progress) const220     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
221     {
222         (dst->*m_setter)(blendFunc(anim, (a->*PropertyWrapperGetter<T>::m_getter)(), (b->*PropertyWrapperGetter<T>::m_getter)(), progress));
223     }
224 
225 protected:
226     void (RenderStyle::*m_setter)(T);
227 };
228 
229 class PropertyWrapperShadow : public PropertyWrapperGetter<ShadowData*> {
230 public:
PropertyWrapperShadow(int prop,ShadowData * (RenderStyle::* getter)()const,void (RenderStyle::* setter)(ShadowData *,bool))231     PropertyWrapperShadow(int prop, ShadowData* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(ShadowData*, bool))
232         : PropertyWrapperGetter<ShadowData*>(prop, getter)
233         , m_setter(setter)
234     {
235     }
236 
equals(const RenderStyle * a,const RenderStyle * b) const237     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
238     {
239         ShadowData* shadowA = (a->*m_getter)();
240         ShadowData* shadowB = (b->*m_getter)();
241 
242         if (!shadowA && shadowB || shadowA && !shadowB)
243             return false;
244         if (shadowA && shadowB && (*shadowA != *shadowB))
245             return false;
246         return true;
247     }
248 
blend(const AnimationBase * anim,RenderStyle * dst,const RenderStyle * a,const RenderStyle * b,double progress) const249     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
250     {
251         ShadowData* shadowA = (a->*m_getter)();
252         ShadowData* shadowB = (b->*m_getter)();
253         ShadowData defaultShadowData(0, 0, 0, Color::transparent);
254 
255         if (!shadowA)
256             shadowA = &defaultShadowData;
257         if (!shadowB)
258             shadowB = &defaultShadowData;
259 
260         (dst->*m_setter)(blendFunc(anim, shadowA, shadowB, progress), false);
261     }
262 
263 private:
264     void (RenderStyle::*m_setter)(ShadowData*, bool);
265 };
266 
267 class PropertyWrapperMaybeInvalidColor : public PropertyWrapperBase {
268 public:
PropertyWrapperMaybeInvalidColor(int prop,const Color & (RenderStyle::* getter)()const,void (RenderStyle::* setter)(const Color &))269     PropertyWrapperMaybeInvalidColor(int prop, const Color& (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&))
270         : PropertyWrapperBase(prop)
271         , m_getter(getter)
272         , m_setter(setter)
273     {
274     }
275 
equals(const RenderStyle * a,const RenderStyle * b) const276     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
277     {
278         Color fromColor = (a->*m_getter)();
279         Color toColor = (b->*m_getter)();
280         if (!fromColor.isValid())
281             fromColor = a->color();
282         if (!toColor.isValid())
283             toColor = b->color();
284 
285         return fromColor == toColor;
286     }
287 
blend(const AnimationBase * anim,RenderStyle * dst,const RenderStyle * a,const RenderStyle * b,double progress) const288     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
289     {
290         Color fromColor = (a->*m_getter)();
291         Color toColor = (b->*m_getter)();
292         if (!fromColor.isValid())
293             fromColor = a->color();
294         if (!toColor.isValid())
295             toColor = b->color();
296         (dst->*m_setter)(blendFunc(anim, fromColor, toColor, progress));
297     }
298 
299 private:
300     const Color& (RenderStyle::*m_getter)() const;
301     void (RenderStyle::*m_setter)(const Color&);
302 };
303 
304 class ShorthandPropertyWrapper : public PropertyWrapperBase {
305 public:
ShorthandPropertyWrapper(int property,const CSSPropertyLonghand & longhand)306     ShorthandPropertyWrapper(int property, const CSSPropertyLonghand& longhand)
307         : PropertyWrapperBase(property)
308     {
309         for (unsigned i = 0; i < longhand.length(); ++i) {
310             PropertyWrapperBase* wrapper = wrapperForProperty(longhand.properties()[i]);
311             if (wrapper)
312                 m_propertyWrappers.append(wrapper);
313         }
314     }
315 
isShorthandWrapper() const316     virtual bool isShorthandWrapper() const { return true; }
317 
equals(const RenderStyle * a,const RenderStyle * b) const318     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
319     {
320         Vector<PropertyWrapperBase*>::const_iterator end = m_propertyWrappers.end();
321         for (Vector<PropertyWrapperBase*>::const_iterator it = m_propertyWrappers.begin(); it != end; ++it) {
322             if (!(*it)->equals(a, b))
323                 return false;
324         }
325         return true;
326     }
327 
blend(const AnimationBase * anim,RenderStyle * dst,const RenderStyle * a,const RenderStyle * b,double progress) const328     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
329     {
330         Vector<PropertyWrapperBase*>::const_iterator end = m_propertyWrappers.end();
331         for (Vector<PropertyWrapperBase*>::const_iterator it = m_propertyWrappers.begin(); it != end; ++it)
332             (*it)->blend(anim, dst, a, b, progress);
333     }
334 
335 private:
336     Vector<PropertyWrapperBase*> m_propertyWrappers;
337 };
338 
339 
340 static Vector<PropertyWrapperBase*>* gPropertyWrappers = 0;
341 static int gPropertyWrapperMap[numCSSProperties];
342 
343 static const int cInvalidPropertyWrapperIndex = -1;
344 
345 
ensurePropertyMap()346 static void ensurePropertyMap()
347 {
348     // FIXME: This data is never destroyed. Maybe we should ref count it and toss it when the last AnimationController is destroyed?
349     if (gPropertyWrappers == 0) {
350         gPropertyWrappers = new Vector<PropertyWrapperBase*>();
351 
352         // build the list of property wrappers to do the comparisons and blends
353         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyLeft, &RenderStyle::left, &RenderStyle::setLeft));
354         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyRight, &RenderStyle::right, &RenderStyle::setRight));
355         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyTop, &RenderStyle::top, &RenderStyle::setTop));
356         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyBottom, &RenderStyle::bottom, &RenderStyle::setBottom));
357         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWidth, &RenderStyle::width, &RenderStyle::setWidth));
358         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyHeight, &RenderStyle::height, &RenderStyle::setHeight));
359         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderLeftWidth, &RenderStyle::borderLeftWidth, &RenderStyle::setBorderLeftWidth));
360         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderRightWidth, &RenderStyle::borderRightWidth, &RenderStyle::setBorderRightWidth));
361         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderTopWidth, &RenderStyle::borderTopWidth, &RenderStyle::setBorderTopWidth));
362         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderBottomWidth, &RenderStyle::borderBottomWidth, &RenderStyle::setBorderBottomWidth));
363         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginLeft, &RenderStyle::marginLeft, &RenderStyle::setMarginLeft));
364         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginRight, &RenderStyle::marginRight, &RenderStyle::setMarginRight));
365         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginTop, &RenderStyle::marginTop, &RenderStyle::setMarginTop));
366         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginBottom, &RenderStyle::marginBottom, &RenderStyle::setMarginBottom));
367         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingLeft, &RenderStyle::paddingLeft, &RenderStyle::setPaddingLeft));
368         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingRight, &RenderStyle::paddingRight, &RenderStyle::setPaddingRight));
369         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingTop, &RenderStyle::paddingTop, &RenderStyle::setPaddingTop));
370         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingBottom, &RenderStyle::paddingBottom, &RenderStyle::setPaddingBottom));
371         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyOpacity, &RenderStyle::opacity, &RenderStyle::setOpacity));
372         gPropertyWrappers->append(new PropertyWrapper<const Color&>(CSSPropertyColor, &RenderStyle::color, &RenderStyle::setColor));
373         gPropertyWrappers->append(new PropertyWrapper<const Color&>(CSSPropertyBackgroundColor, &RenderStyle::backgroundColor, &RenderStyle::setBackgroundColor));
374         gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyFontSize, &RenderStyle::fontSize, &RenderStyle::setBlendedFontSize));
375         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyWebkitColumnRuleWidth, &RenderStyle::columnRuleWidth, &RenderStyle::setColumnRuleWidth));
376         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyWebkitColumnGap, &RenderStyle::columnGap, &RenderStyle::setColumnGap));
377         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyWebkitColumnCount, &RenderStyle::columnCount, &RenderStyle::setColumnCount));
378         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyWebkitColumnWidth, &RenderStyle::columnWidth, &RenderStyle::setColumnWidth));
379         gPropertyWrappers->append(new PropertyWrapper<short>(CSSPropertyWebkitBorderHorizontalSpacing, &RenderStyle::horizontalBorderSpacing, &RenderStyle::setHorizontalBorderSpacing));
380         gPropertyWrappers->append(new PropertyWrapper<short>(CSSPropertyWebkitBorderVerticalSpacing, &RenderStyle::verticalBorderSpacing, &RenderStyle::setVerticalBorderSpacing));
381         gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyZIndex, &RenderStyle::zIndex, &RenderStyle::setZIndex));
382         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyLineHeight, &RenderStyle::lineHeight, &RenderStyle::setLineHeight));
383         gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyOutlineOffset, &RenderStyle::outlineOffset, &RenderStyle::setOutlineOffset));
384         gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyOutlineWidth, &RenderStyle::outlineWidth, &RenderStyle::setOutlineWidth));
385         gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyLetterSpacing, &RenderStyle::letterSpacing, &RenderStyle::setLetterSpacing));
386         gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyWordSpacing, &RenderStyle::wordSpacing, &RenderStyle::setWordSpacing));
387         gPropertyWrappers->append(new PropertyWrapper<const TransformOperations&>(CSSPropertyWebkitTransform, &RenderStyle::transform, &RenderStyle::setTransform));
388         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWebkitTransformOriginX, &RenderStyle::transformOriginX, &RenderStyle::setTransformOriginX));
389         gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWebkitTransformOriginY, &RenderStyle::transformOriginY, &RenderStyle::setTransformOriginY));
390         gPropertyWrappers->append(new PropertyWrapper<const IntSize&>(CSSPropertyWebkitBorderTopLeftRadius, &RenderStyle::borderTopLeftRadius, &RenderStyle::setBorderTopLeftRadius));
391         gPropertyWrappers->append(new PropertyWrapper<const IntSize&>(CSSPropertyWebkitBorderTopRightRadius, &RenderStyle::borderTopRightRadius, &RenderStyle::setBorderTopRightRadius));
392         gPropertyWrappers->append(new PropertyWrapper<const IntSize&>(CSSPropertyWebkitBorderBottomLeftRadius, &RenderStyle::borderBottomLeftRadius, &RenderStyle::setBorderBottomLeftRadius));
393         gPropertyWrappers->append(new PropertyWrapper<const IntSize&>(CSSPropertyWebkitBorderBottomRightRadius, &RenderStyle::borderBottomRightRadius, &RenderStyle::setBorderBottomRightRadius));
394         gPropertyWrappers->append(new PropertyWrapper<EVisibility>(CSSPropertyVisibility, &RenderStyle::visibility, &RenderStyle::setVisibility));
395         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyZoom, &RenderStyle::zoom, &RenderStyle::setZoom));
396         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyWebkitColumnRuleColor, &RenderStyle::columnRuleColor, &RenderStyle::setColumnRuleColor));
397         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyWebkitTextStrokeColor, &RenderStyle::textStrokeColor, &RenderStyle::setTextStrokeColor));
398         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyWebkitTextFillColor, &RenderStyle::textFillColor, &RenderStyle::setTextFillColor));
399         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderLeftColor, &RenderStyle::borderLeftColor, &RenderStyle::setBorderLeftColor));
400         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderRightColor, &RenderStyle::borderRightColor, &RenderStyle::setBorderRightColor));
401         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderTopColor, &RenderStyle::borderTopColor, &RenderStyle::setBorderTopColor));
402         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderBottomColor, &RenderStyle::borderBottomColor, &RenderStyle::setBorderBottomColor));
403         gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyOutlineColor, &RenderStyle::outlineColor, &RenderStyle::setOutlineColor));
404 
405         // These are for shadows
406         gPropertyWrappers->append(new PropertyWrapperShadow(CSSPropertyWebkitBoxShadow, &RenderStyle::boxShadow, &RenderStyle::setBoxShadow));
407         gPropertyWrappers->append(new PropertyWrapperShadow(CSSPropertyTextShadow, &RenderStyle::textShadow, &RenderStyle::setTextShadow));
408 
409 #if ENABLE(SVG)
410         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyFillOpacity, &RenderStyle::fillOpacity, &RenderStyle::setFillOpacity));
411         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyFloodOpacity, &RenderStyle::floodOpacity, &RenderStyle::setFloodOpacity));
412         gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyStrokeOpacity, &RenderStyle::strokeOpacity, &RenderStyle::setStrokeOpacity));
413 #endif
414 
415         // TODO:
416         //
417         //  CSSPropertyBackground, CSSPropertyBackgroundPosition
418         //  CSSPropertyMinWidth, CSSPropertyMaxWidth, CSSPropertyMinHeight, CSSPropertyMaxHeight
419         //  CSSPropertyTextIndent
420         //  CSSPropertyVerticalAlign
421         //  CSSPropertyWebkitBackgroundOrigin
422         //  CSSPropertyWebkitBackgroundSize
423         //  CSSPropertyWebkitMaskPosition
424         //  CSSPropertyWebkitMaskOrigin
425         //  CSSPropertyWebkitMaskSize
426         //
427         // Compound properties that have components that should be animatable:
428         //
429         //  CSSPropertyWebkitColumns
430         //  CSSPropertyWebkitMask
431         //  CSSPropertyWebkitBoxReflect
432 
433         // Make sure unused slots have a value
434         for (unsigned int i = 0; i < static_cast<unsigned int>(numCSSProperties); ++i)
435             gPropertyWrapperMap[i] = cInvalidPropertyWrapperIndex;
436 
437         // First we put the non-shorthand property wrappers into the map, so the shorthand-building
438         // code can find them.
439         size_t n = gPropertyWrappers->size();
440         for (unsigned int i = 0; i < n; ++i) {
441             ASSERT((*gPropertyWrappers)[i]->property() - firstCSSProperty < numCSSProperties);
442             gPropertyWrapperMap[(*gPropertyWrappers)[i]->property() - firstCSSProperty] = i;
443         }
444 
445         // Now add the shorthand wrappers.
446         addShorthandProperties();
447     }
448 }
449 
addPropertyWrapper(int propertyID,PropertyWrapperBase * wrapper)450 static void addPropertyWrapper(int propertyID, PropertyWrapperBase* wrapper)
451 {
452     int propIndex = propertyID - firstCSSProperty;
453 
454     ASSERT(gPropertyWrapperMap[propIndex] == cInvalidPropertyWrapperIndex);
455 
456     unsigned wrapperIndex = gPropertyWrappers->size();
457     gPropertyWrappers->append(wrapper);
458     gPropertyWrapperMap[propIndex] = wrapperIndex;
459 }
460 
addShorthandProperties()461 static void addShorthandProperties()
462 {
463     static const int animatableShorthandProperties[] = {
464         CSSPropertyBackground,      // for background-color
465         CSSPropertyBorderTop, CSSPropertyBorderRight, CSSPropertyBorderBottom, CSSPropertyBorderLeft,
466         CSSPropertyBorderColor,
467         CSSPropertyBorderWidth,
468         CSSPropertyBorder,
469         CSSPropertyBorderSpacing,
470         CSSPropertyMargin,
471         CSSPropertyOutline,
472         CSSPropertyPadding,
473         CSSPropertyWebkitTextStroke,
474         CSSPropertyWebkitColumnRule,
475         CSSPropertyWebkitBorderRadius,
476         CSSPropertyWebkitTransformOrigin
477     };
478 
479     for (unsigned i = 0; i < sizeof(animatableShorthandProperties) / sizeof(animatableShorthandProperties[0]); ++i) {
480         int propertyID = animatableShorthandProperties[i];
481         CSSPropertyLonghand longhand = longhandForProperty(propertyID);
482         if (longhand.length() > 0)
483             addPropertyWrapper(propertyID, new ShorthandPropertyWrapper(propertyID, longhand));
484     }
485 
486     // 'font' is not in the shorthand map.
487     static const int animatableFontProperties[] = {
488         CSSPropertyFontSize,
489         CSSPropertyFontWeight
490     };
491 
492     CSSPropertyLonghand fontLonghand(animatableFontProperties, sizeof(animatableFontProperties) / sizeof(animatableFontProperties[0]));
493     addPropertyWrapper(CSSPropertyFont, new ShorthandPropertyWrapper(CSSPropertyFont, fontLonghand));
494 }
495 
wrapperForProperty(int propertyID)496 static PropertyWrapperBase* wrapperForProperty(int propertyID)
497 {
498     int propIndex = propertyID - firstCSSProperty;
499     if (propIndex >= 0 && propIndex < numCSSProperties) {
500         int wrapperIndex = gPropertyWrapperMap[propIndex];
501         if (wrapperIndex >= 0)
502             return (*gPropertyWrappers)[wrapperIndex];
503     }
504     return 0;
505 }
506 
AnimationBase(const Animation * transition,RenderObject * renderer,CompositeAnimation * compAnim)507 AnimationBase::AnimationBase(const Animation* transition, RenderObject* renderer, CompositeAnimation* compAnim)
508     : m_animState(AnimationStateNew)
509     , m_isAnimating(false)
510     , m_waitedForResponse(false)
511     , m_startTime(0)
512     , m_pauseTime(-1)
513     , m_requestedStartTime(0)
514     , m_object(renderer)
515     , m_animation(const_cast<Animation*>(transition))
516     , m_compAnim(compAnim)
517     , m_transformFunctionListValid(false)
518     , m_nextIterationDuration(-1)
519     , m_next(0)
520 {
521     // Compute the total duration
522     m_totalDuration = -1;
523     if (m_animation->iterationCount() > 0)
524         m_totalDuration = m_animation->duration() * m_animation->iterationCount();
525 }
526 
~AnimationBase()527 AnimationBase::~AnimationBase()
528 {
529     if (m_animState == AnimationStateStartWaitStyleAvailable)
530         m_compAnim->removeFromStyleAvailableWaitList(this);
531 }
532 
propertiesEqual(int prop,const RenderStyle * a,const RenderStyle * b)533 bool AnimationBase::propertiesEqual(int prop, const RenderStyle* a, const RenderStyle* b)
534 {
535     ensurePropertyMap();
536     if (prop == cAnimateAll) {
537         size_t n = gPropertyWrappers->size();
538         for (unsigned int i = 0; i < n; ++i) {
539             PropertyWrapperBase* wrapper = (*gPropertyWrappers)[i];
540             // No point comparing shorthand wrappers for 'all'.
541             if (!wrapper->isShorthandWrapper() && !wrapper->equals(a, b))
542                 return false;
543         }
544     } else {
545         PropertyWrapperBase* wrapper = wrapperForProperty(prop);
546         if (wrapper)
547             return wrapper->equals(a, b);
548     }
549     return true;
550 }
551 
getPropertyAtIndex(int i,bool & isShorthand)552 int AnimationBase::getPropertyAtIndex(int i, bool& isShorthand)
553 {
554     ensurePropertyMap();
555     if (i < 0 || i >= static_cast<int>(gPropertyWrappers->size()))
556         return CSSPropertyInvalid;
557 
558     PropertyWrapperBase* wrapper = (*gPropertyWrappers)[i];
559     isShorthand = wrapper->isShorthandWrapper();
560     return wrapper->property();
561 }
562 
getNumProperties()563 int AnimationBase::getNumProperties()
564 {
565     ensurePropertyMap();
566     return gPropertyWrappers->size();
567 }
568 
569 // Returns true if we need to start animation timers
blendProperties(const AnimationBase * anim,int prop,RenderStyle * dst,const RenderStyle * a,const RenderStyle * b,double progress)570 bool AnimationBase::blendProperties(const AnimationBase* anim, int prop, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress)
571 {
572     ASSERT(prop != cAnimateAll);
573 
574     ensurePropertyMap();
575     PropertyWrapperBase* wrapper = wrapperForProperty(prop);
576     if (wrapper) {
577         wrapper->blend(anim, dst, a, b, progress);
578         return true;
579     }
580 
581     return false;
582 }
583 
setChanged(Node * node)584 void AnimationBase::setChanged(Node* node)
585 {
586     ASSERT(!node || (node->document() && !node->document()->inPageCache()));
587     if (node)
588         node->setChanged(AnimationStyleChange);
589 }
590 
duration() const591 double AnimationBase::duration() const
592 {
593     return m_animation->duration();
594 }
595 
playStatePlaying() const596 bool AnimationBase::playStatePlaying() const
597 {
598     return m_animation->playState() == AnimPlayStatePlaying;
599 }
600 
animationsMatch(const Animation * anim) const601 bool AnimationBase::animationsMatch(const Animation* anim) const
602 {
603     return m_animation->animationsMatch(anim);
604 }
605 
updateStateMachine(AnimStateInput input,double param)606 void AnimationBase::updateStateMachine(AnimStateInput input, double param)
607 {
608     // If we get AnimationStateInputRestartAnimation then we force a new animation, regardless of state.
609     if (input == AnimationStateInputMakeNew) {
610         if (m_animState == AnimationStateStartWaitStyleAvailable)
611             m_compAnim->removeFromStyleAvailableWaitList(this);
612         m_animState = AnimationStateNew;
613         m_startTime = 0;
614         m_pauseTime = -1;
615         m_requestedStartTime = 0;
616         m_nextIterationDuration = -1;
617         m_waitedForResponse = false;
618         endAnimation(false);
619         return;
620     }
621 
622     if (input == AnimationStateInputRestartAnimation) {
623         if (m_animState == AnimationStateStartWaitStyleAvailable)
624             m_compAnim->removeFromStyleAvailableWaitList(this);
625         m_animState = AnimationStateNew;
626         m_startTime = 0;
627         m_pauseTime = -1;
628         m_requestedStartTime = 0;
629         m_nextIterationDuration = -1;
630         endAnimation(false);
631 
632         if (!paused())
633             updateStateMachine(AnimationStateInputStartAnimation, -1);
634         return;
635     }
636 
637     if (input == AnimationStateInputEndAnimation) {
638         if (m_animState == AnimationStateStartWaitStyleAvailable)
639             m_compAnim->removeFromStyleAvailableWaitList(this);
640         m_animState = AnimationStateDone;
641         endAnimation(true);
642         return;
643     }
644 
645     if (input == AnimationStateInputPauseOverride) {
646         if (m_animState == AnimationStateStartWaitResponse) {
647             // If we are in AnimationStateStartWaitResponse, the animation will get canceled before
648             // we get a response, so move to the next state.
649             endAnimation(false);
650             updateStateMachine(AnimationStateInputStartTimeSet, beginAnimationUpdateTime());
651         }
652         return;
653     }
654 
655     if (input == AnimationStateInputResumeOverride) {
656         if (m_animState == AnimationStateLooping || m_animState == AnimationStateEnding) {
657             // Start the animation
658             startAnimation(m_startTime);
659         }
660         return;
661     }
662 
663     // Execute state machine
664     switch(m_animState) {
665         case AnimationStateNew:
666             ASSERT(input == AnimationStateInputStartAnimation || input == AnimationStateInputPlayStateRunnning || input == AnimationStateInputPlayStatePaused);
667             if (input == AnimationStateInputStartAnimation || input == AnimationStateInputPlayStateRunnning) {
668                 m_waitedForResponse = false;
669                 m_requestedStartTime = beginAnimationUpdateTime();
670                 m_animState = AnimationStateStartWaitTimer;
671             }
672             break;
673         case AnimationStateStartWaitTimer:
674             ASSERT(input == AnimationStateInputStartTimerFired || input == AnimationStateInputPlayStatePaused);
675 
676             if (input == AnimationStateInputStartTimerFired) {
677                 ASSERT(param >= 0);
678                 // Start timer has fired, tell the animation to start and wait for it to respond with start time
679                 m_animState = AnimationStateStartWaitStyleAvailable;
680                 m_compAnim->addToStyleAvailableWaitList(this);
681 
682                 // Trigger a render so we can start the animation
683                 if (m_object)
684                     m_object->animation()->addNodeChangeToDispatch(m_object->element());
685             } else {
686                 ASSERT(!paused());
687                 // We're waiting for the start timer to fire and we got a pause. Cancel the timer, pause and wait
688                 m_pauseTime = beginAnimationUpdateTime();
689                 m_animState = AnimationStatePausedWaitTimer;
690             }
691             break;
692         case AnimationStateStartWaitStyleAvailable:
693             ASSERT(input == AnimationStateInputStyleAvailable || input == AnimationStateInputPlayStatePaused);
694 
695             // Start timer has fired, tell the animation to start and wait for it to respond with start time
696             m_animState = AnimationStateStartWaitResponse;
697 
698             overrideAnimations();
699 
700             // Send start event, if needed
701             onAnimationStart(0); // The elapsedTime is always 0 here
702 
703             // Start the animation
704             if (overridden() || !startAnimation(0)) {
705                 // We're not going to get a startTime callback, so fire the start time here
706                 m_animState = AnimationStateStartWaitResponse;
707                 updateStateMachine(AnimationStateInputStartTimeSet, beginAnimationUpdateTime());
708             } else
709                 m_waitedForResponse = true;
710             break;
711         case AnimationStateStartWaitResponse:
712             ASSERT(input == AnimationStateInputStartTimeSet || input == AnimationStateInputPlayStatePaused);
713 
714             if (input == AnimationStateInputStartTimeSet) {
715                 ASSERT(param >= 0);
716                 // We have a start time, set it, unless the startTime is already set
717                 if (m_startTime <= 0)
718                     m_startTime = param;
719 
720                 // Decide whether to go into looping or ending state
721                 goIntoEndingOrLoopingState();
722 
723                 // Dispatch updateRendering so we can start the animation
724                 if (m_object)
725                     m_object->animation()->addNodeChangeToDispatch(m_object->element());
726             } else {
727                 // We are pausing while waiting for a start response. Cancel the animation and wait. When
728                 // we unpause, we will act as though the start timer just fired
729                 m_pauseTime = -1;
730                 endAnimation(false);
731                 m_animState = AnimationStatePausedWaitResponse;
732             }
733             break;
734         case AnimationStateLooping:
735             ASSERT(input == AnimationStateInputLoopTimerFired || input == AnimationStateInputPlayStatePaused);
736 
737             if (input == AnimationStateInputLoopTimerFired) {
738                 ASSERT(param >= 0);
739                 // Loop timer fired, loop again or end.
740                 onAnimationIteration(param);
741 
742                 // Decide whether to go into looping or ending state
743                 goIntoEndingOrLoopingState();
744             } else {
745                 // We are pausing while running. Cancel the animation and wait
746                 m_pauseTime = beginAnimationUpdateTime();
747                 endAnimation(false);
748                 m_animState = AnimationStatePausedRun;
749             }
750             break;
751         case AnimationStateEnding:
752             ASSERT(input == AnimationStateInputEndTimerFired || input == AnimationStateInputPlayStatePaused);
753 
754             if (input == AnimationStateInputEndTimerFired) {
755                 ASSERT(param >= 0);
756                 // End timer fired, finish up
757                 onAnimationEnd(param);
758 
759                 m_animState = AnimationStateDone;
760 
761                 if (m_object) {
762                     resumeOverriddenAnimations();
763 
764                     // Fire off another style change so we can set the final value
765                     m_object->animation()->addNodeChangeToDispatch(m_object->element());
766                 }
767             } else {
768                 // We are pausing while running. Cancel the animation and wait
769                 m_pauseTime = beginAnimationUpdateTime();
770                 endAnimation(false);
771                 m_animState = AnimationStatePausedRun;
772             }
773             // |this| may be deleted here
774             break;
775         case AnimationStatePausedWaitTimer:
776             ASSERT(input == AnimationStateInputPlayStateRunnning);
777             ASSERT(paused());
778             // Update the times
779             m_startTime += beginAnimationUpdateTime() - m_pauseTime;
780             m_pauseTime = -1;
781 
782             // we were waiting for the start timer to fire, go back and wait again
783             m_animState = AnimationStateNew;
784             updateStateMachine(AnimationStateInputStartAnimation, 0);
785             break;
786         case AnimationStatePausedWaitResponse:
787         case AnimationStatePausedRun:
788             // We treat these two cases the same. The only difference is that, when we are in
789             // AnimationStatePausedWaitResponse, we don't yet have a valid startTime, so we send 0 to startAnimation.
790             // When the AnimationStateInputStartTimeSet comes in and we were in AnimationStatePausedRun, we will notice
791             // that we have already set the startTime and will ignore it.
792             ASSERT(input == AnimationStateInputPlayStateRunnning);
793             ASSERT(paused());
794             // Update the times
795             if (m_animState == AnimationStatePausedRun)
796                 m_startTime += beginAnimationUpdateTime() - m_pauseTime;
797             else
798                 m_startTime = 0;
799             m_pauseTime = -1;
800 
801             // We were waiting for a begin time response from the animation, go back and wait again
802             m_animState = AnimationStateStartWaitResponse;
803 
804             // Start the animation
805             if (overridden() || !startAnimation(m_startTime)) {
806                 // We're not going to get a startTime callback, so fire the start time here
807                 updateStateMachine(AnimationStateInputStartTimeSet, beginAnimationUpdateTime());
808             } else
809                 m_waitedForResponse = true;
810             break;
811         case AnimationStateDone:
812             // We're done. Stay in this state until we are deleted
813             break;
814     }
815 }
816 
fireAnimationEventsIfNeeded()817 void AnimationBase::fireAnimationEventsIfNeeded()
818 {
819     // If we are waiting for the delay time to expire and it has, go to the next state
820     if (m_animState != AnimationStateStartWaitTimer && m_animState != AnimationStateLooping && m_animState != AnimationStateEnding)
821         return;
822 
823     // We have to make sure to keep a ref to the this pointer, because it could get destroyed
824     // during an animation callback that might get called. Since the owner is a CompositeAnimation
825     // and it ref counts this object, we will keep a ref to that instead. That way the AnimationBase
826     // can still access the resources of its CompositeAnimation as needed.
827     RefPtr<AnimationBase> protector(this);
828     RefPtr<CompositeAnimation> compProtector(m_compAnim);
829 
830     // Check for start timeout
831     if (m_animState == AnimationStateStartWaitTimer) {
832         if (beginAnimationUpdateTime() - m_requestedStartTime >= m_animation->delay())
833             updateStateMachine(AnimationStateInputStartTimerFired, 0);
834         return;
835     }
836 
837     double elapsedDuration = beginAnimationUpdateTime() - m_startTime;
838     ASSERT(elapsedDuration >= 0);
839 
840     // Check for end timeout
841     if (m_totalDuration >= 0 && elapsedDuration >= m_totalDuration) {
842         // Fire an end event
843         updateStateMachine(AnimationStateInputEndTimerFired, m_totalDuration);
844     }
845     else {
846         // Check for iteration timeout
847         if (m_nextIterationDuration < 0) {
848             // Hasn't been set yet, set it
849             double durationLeft = m_animation->duration() - fmod(elapsedDuration, m_animation->duration());
850             m_nextIterationDuration = elapsedDuration + durationLeft;
851         }
852 
853         if (elapsedDuration >= m_nextIterationDuration) {
854             // Set to the next iteration
855             double previous = m_nextIterationDuration;
856             double durationLeft = m_animation->duration() - fmod(elapsedDuration, m_animation->duration());
857             m_nextIterationDuration = elapsedDuration + durationLeft;
858 
859             // Send the event
860             updateStateMachine(AnimationStateInputLoopTimerFired, previous);
861         }
862     }
863 }
864 
updatePlayState(bool run)865 void AnimationBase::updatePlayState(bool run)
866 {
867     if (paused() == run || isNew())
868         updateStateMachine(run ? AnimationStateInputPlayStateRunnning : AnimationStateInputPlayStatePaused, -1);
869 }
870 
willNeedService() const871 double AnimationBase::willNeedService() const
872 {
873     // Returns the time at which next service is required. -1 means no service is required. 0 means
874     // service is required now, and > 0 means service is required that many seconds in the future.
875     if (paused() || isNew())
876         return -1;
877 
878     if (m_animState == AnimationStateStartWaitTimer) {
879         double timeFromNow = m_animation->delay() - (beginAnimationUpdateTime() - m_requestedStartTime);
880         return (float) ((timeFromNow > 0) ? timeFromNow : 0);
881     }
882 
883     // In all other cases, we need service right away.
884     return 0;
885 }
886 
progress(double scale,double offset,const TimingFunction * tf) const887 double AnimationBase::progress(double scale, double offset, const TimingFunction* tf) const
888 {
889     if (preActive())
890         return 0;
891 
892     double elapsedTime = getElapsedTime();
893 
894     double dur = m_animation->duration();
895     if (m_animation->iterationCount() > 0)
896         dur *= m_animation->iterationCount();
897 
898     if (postActive() || !m_animation->duration() || (m_animation->iterationCount() > 0 && elapsedTime >= dur))
899         return 1.0;
900 
901     // Compute the fractional time, taking into account direction.
902     // There is no need to worry about iterations, we assume that we would have
903     // short circuited above if we were done.
904     double fractionalTime = elapsedTime / m_animation->duration();
905     int integralTime = static_cast<int>(fractionalTime);
906     fractionalTime -= integralTime;
907 
908     if (m_animation->direction() && (integralTime & 1))
909         fractionalTime = 1 - fractionalTime;
910 
911     if (scale != 1 || offset)
912         fractionalTime = (fractionalTime - offset) * scale;
913 
914     if (!tf)
915         tf = &m_animation->timingFunction();
916 
917     if (tf->type() == LinearTimingFunction)
918         return fractionalTime;
919 
920     // Cubic bezier.
921     double result = solveCubicBezierFunction(tf->x1(),
922                                             tf->y1(),
923                                             tf->x2(),
924                                             tf->y2(),
925                                             fractionalTime, m_animation->duration());
926     return result;
927 }
928 
goIntoEndingOrLoopingState()929 void AnimationBase::goIntoEndingOrLoopingState()
930 {
931     // Decide when the end or loop event needs to fire
932     double totalDuration = -1;
933     if (m_animation->iterationCount() > 0)
934         totalDuration = m_animation->duration() * m_animation->iterationCount();
935 
936     const double elapsedDuration = beginAnimationUpdateTime() - m_startTime;
937     ASSERT(elapsedDuration >= 0);
938     double durationLeft = 0;
939     double nextIterationTime = totalDuration;
940 
941     if (totalDuration < 0 || elapsedDuration < totalDuration) {
942         durationLeft = m_animation->duration() - fmod(elapsedDuration, m_animation->duration());
943         nextIterationTime = elapsedDuration + durationLeft;
944     }
945 
946     if (totalDuration < 0 || nextIterationTime < totalDuration) {
947         // We are not at the end yet
948         ASSERT(nextIterationTime > 0);
949         m_animState = AnimationStateLooping;
950     } else {
951         // We are at the end
952         m_animState = AnimationStateEnding;
953     }
954 }
955 
pauseAtTime(double t)956 void AnimationBase::pauseAtTime(double t)
957 {
958     updatePlayState(false);
959     m_pauseTime = m_startTime + t - m_animation->delay();
960 }
961 
beginAnimationUpdateTime() const962 double AnimationBase::beginAnimationUpdateTime() const
963 {
964     return m_compAnim->animationController()->beginAnimationUpdateTime();
965 }
966 
getElapsedTime() const967 double AnimationBase::getElapsedTime() const
968 {
969     if (paused())
970         return m_pauseTime - m_startTime;
971     if (m_startTime <= 0)
972         return 0;
973     if (postActive())
974         return 1;
975     return beginAnimationUpdateTime() - m_startTime;
976 }
977 
978 } // namespace WebCore
979