diff options
Diffstat (limited to 'Source/WebCore/page/animation')
-rw-r--r-- | Source/WebCore/page/animation/AnimationBase.cpp | 1399 | ||||
-rw-r--r-- | Source/WebCore/page/animation/AnimationBase.h | 241 | ||||
-rw-r--r-- | Source/WebCore/page/animation/AnimationController.cpp | 615 | ||||
-rw-r--r-- | Source/WebCore/page/animation/AnimationController.h | 82 | ||||
-rw-r--r-- | Source/WebCore/page/animation/AnimationControllerPrivate.h | 131 | ||||
-rw-r--r-- | Source/WebCore/page/animation/CompositeAnimation.cpp | 563 | ||||
-rw-r--r-- | Source/WebCore/page/animation/CompositeAnimation.h | 107 | ||||
-rw-r--r-- | Source/WebCore/page/animation/ImplicitAnimation.cpp | 303 | ||||
-rw-r--r-- | Source/WebCore/page/animation/ImplicitAnimation.h | 96 | ||||
-rw-r--r-- | Source/WebCore/page/animation/KeyframeAnimation.cpp | 461 | ||||
-rw-r--r-- | Source/WebCore/page/animation/KeyframeAnimation.h | 101 |
11 files changed, 4099 insertions, 0 deletions
diff --git a/Source/WebCore/page/animation/AnimationBase.cpp b/Source/WebCore/page/animation/AnimationBase.cpp new file mode 100644 index 0000000..14a44d2 --- /dev/null +++ b/Source/WebCore/page/animation/AnimationBase.cpp @@ -0,0 +1,1399 @@ +/* + * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "AnimationBase.h" + +#include "AnimationControllerPrivate.h" +#include "CSSMutableStyleDeclaration.h" +#include "CSSPropertyLonghand.h" +#include "CSSPropertyNames.h" +#include "CompositeAnimation.h" +#include "Document.h" +#include "EventNames.h" +#include "FloatConversion.h" +#include "Frame.h" +#include "IdentityTransformOperation.h" +#include "ImplicitAnimation.h" +#include "KeyframeAnimation.h" +#include "MatrixTransformOperation.h" +#include "Matrix3DTransformOperation.h" +#include "RenderBox.h" +#include "RenderLayer.h" +#include "RenderLayerBacking.h" +#include "RenderStyle.h" +#include "UnitBezier.h" +#include <algorithm> +#include <wtf/CurrentTime.h> + +using namespace std; + +namespace WebCore { + +// The epsilon value we pass to UnitBezier::solve given that the animation is going to run over |dur| seconds. The longer the +// animation, the more precision we need in the timing function result to avoid ugly discontinuities. +static inline double solveEpsilon(double duration) +{ + return 1.0 / (200.0 * duration); +} + +static inline double solveCubicBezierFunction(double p1x, double p1y, double p2x, double p2y, double t, double duration) +{ + // Convert from input time to parametric value in curve, then from + // that to output time. + UnitBezier bezier(p1x, p1y, p2x, p2y); + return bezier.solve(t, solveEpsilon(duration)); +} + +static inline double solveStepsFunction(int numSteps, bool stepAtStart, double t) +{ + if (stepAtStart) + return min(1.0, (floor(numSteps * t) + 1) / numSteps); + return floor(numSteps * t) / numSteps; +} + +static inline int blendFunc(const AnimationBase*, int from, int to, double progress) +{ + return int(from + (to - from) * progress); +} + +static inline double blendFunc(const AnimationBase*, double from, double to, double progress) +{ + return from + (to - from) * progress; +} + +static inline float blendFunc(const AnimationBase*, float from, float to, double progress) +{ + return narrowPrecisionToFloat(from + (to - from) * progress); +} + +static inline Color blendFunc(const AnimationBase* anim, const Color& from, const Color& to, double progress) +{ + // We need to preserve the state of the valid flag at the end of the animation + if (progress == 1 && !to.isValid()) + return Color(); + + // Contrary to the name, RGBA32 actually stores ARGB, so we can initialize Color directly from premultipliedARGBFromColor(). + // Also, premultipliedARGBFromColor() bails on zero alpha, so special-case that. + Color premultFrom = from.alpha() ? premultipliedARGBFromColor(from) : 0; + Color premultTo = to.alpha() ? premultipliedARGBFromColor(to) : 0; + + Color premultBlended(blendFunc(anim, premultFrom.red(), premultTo.red(), progress), + blendFunc(anim, premultFrom.green(), premultTo.green(), progress), + blendFunc(anim, premultFrom.blue(), premultTo.blue(), progress), + blendFunc(anim, premultFrom.alpha(), premultTo.alpha(), progress)); + + return Color(colorFromPremultipliedARGB(premultBlended.rgb())); +} + +static inline Length blendFunc(const AnimationBase*, const Length& from, const Length& to, double progress) +{ + return to.blend(from, progress); +} + +static inline LengthSize blendFunc(const AnimationBase* anim, const LengthSize& from, const LengthSize& to, double progress) +{ + return LengthSize(blendFunc(anim, from.width(), to.width(), progress), + blendFunc(anim, from.height(), to.height(), progress)); +} + +static inline IntSize blendFunc(const AnimationBase* anim, const IntSize& from, const IntSize& to, double progress) +{ + return IntSize(blendFunc(anim, from.width(), to.width(), progress), + blendFunc(anim, from.height(), to.height(), progress)); +} + +static inline ShadowStyle blendFunc(const AnimationBase* anim, ShadowStyle from, ShadowStyle to, double progress) +{ + if (from == to) + return to; + + double fromVal = from == Normal ? 1 : 0; + double toVal = to == Normal ? 1 : 0; + double result = blendFunc(anim, fromVal, toVal, progress); + return result > 0 ? Normal : Inset; +} + +static inline ShadowData* blendFunc(const AnimationBase* anim, const ShadowData* from, const ShadowData* to, double progress) +{ + ASSERT(from && to); + return new ShadowData(blendFunc(anim, from->x(), to->x(), progress), + blendFunc(anim, from->y(), to->y(), progress), + blendFunc(anim, from->blur(), to->blur(), progress), + blendFunc(anim, from->spread(), to->spread(), progress), + blendFunc(anim, from->style(), to->style(), progress), + from->isWebkitBoxShadow(), + blendFunc(anim, from->color(), to->color(), progress)); +} + +static inline TransformOperations blendFunc(const AnimationBase* anim, const TransformOperations& from, const TransformOperations& to, double progress) +{ + TransformOperations result; + + // If we have a transform function list, use that to do a per-function animation. Otherwise do a Matrix animation + if (anim->isTransformFunctionListValid()) { + unsigned fromSize = from.operations().size(); + unsigned toSize = to.operations().size(); + unsigned size = max(fromSize, toSize); + for (unsigned i = 0; i < size; i++) { + RefPtr<TransformOperation> fromOp = (i < fromSize) ? from.operations()[i].get() : 0; + RefPtr<TransformOperation> toOp = (i < toSize) ? to.operations()[i].get() : 0; + RefPtr<TransformOperation> blendedOp = toOp ? toOp->blend(fromOp.get(), progress) : (fromOp ? fromOp->blend(0, progress, true) : 0); + if (blendedOp) + result.operations().append(blendedOp); + else { + RefPtr<TransformOperation> identityOp = IdentityTransformOperation::create(); + if (progress > 0.5) + result.operations().append(toOp ? toOp : identityOp); + else + result.operations().append(fromOp ? fromOp : identityOp); + } + } + } else { + // Convert the TransformOperations into matrices + IntSize size = anim->renderer()->isBox() ? toRenderBox(anim->renderer())->borderBoxRect().size() : IntSize(); + TransformationMatrix fromT; + TransformationMatrix toT; + from.apply(size, fromT); + to.apply(size, toT); + + toT.blend(fromT, progress); + + // Append the result + result.operations().append(Matrix3DTransformOperation::create(toT)); + } + return result; +} + +static inline EVisibility blendFunc(const AnimationBase* anim, EVisibility from, EVisibility to, double progress) +{ + // Any non-zero result means we consider the object to be visible. Only at 0 do we consider the object to be + // invisible. The invisible value we use (HIDDEN vs. COLLAPSE) depends on the specified from/to values. + double fromVal = from == VISIBLE ? 1. : 0.; + double toVal = to == VISIBLE ? 1. : 0.; + if (fromVal == toVal) + return to; + double result = blendFunc(anim, fromVal, toVal, progress); + return result > 0. ? VISIBLE : (to != VISIBLE ? to : from); +} + +static inline LengthBox blendFunc(const AnimationBase* anim, const LengthBox& from, const LengthBox& to, double progress) +{ + // Length types have to match to animate + if (from.top().type() != to.top().type() + || from.right().type() != to.right().type() + || from.bottom().type() != to.bottom().type() + || from.left().type() != to.left().type()) + return to; + + LengthBox result(blendFunc(anim, from.top(), to.top(), progress), + blendFunc(anim, from.right(), to.right(), progress), + blendFunc(anim, from.bottom(), to.bottom(), progress), + blendFunc(anim, from.left(), to.left(), progress)); + return result; +} + +class PropertyWrapperBase; + +static void addShorthandProperties(); +static PropertyWrapperBase* wrapperForProperty(int propertyID); + +class PropertyWrapperBase : public Noncopyable { +public: + PropertyWrapperBase(int prop) + : m_prop(prop) + { + } + + virtual ~PropertyWrapperBase() { } + + virtual bool isShorthandWrapper() const { return false; } + virtual bool equals(const RenderStyle* a, const RenderStyle* b) const = 0; + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const = 0; + + int property() const { return m_prop; } + +#if USE(ACCELERATED_COMPOSITING) + virtual bool animationIsAccelerated() const { return false; } +#endif + +private: + int m_prop; +}; + +template <typename T> +class PropertyWrapperGetter : public PropertyWrapperBase { +public: + PropertyWrapperGetter(int prop, T (RenderStyle::*getter)() const) + : PropertyWrapperBase(prop) + , m_getter(getter) + { + } + + virtual bool equals(const RenderStyle* a, const RenderStyle* b) const + { + // If the style pointers are the same, don't bother doing the test. + // If either is null, return false. If both are null, return true. + if ((!a && !b) || a == b) + return true; + if (!a || !b) + return false; + return (a->*m_getter)() == (b->*m_getter)(); + } + +protected: + T (RenderStyle::*m_getter)() const; +}; + +template <typename T> +class PropertyWrapper : public PropertyWrapperGetter<T> { +public: + PropertyWrapper(int prop, T (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T)) + : PropertyWrapperGetter<T>(prop, getter) + , m_setter(setter) + { + } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + (dst->*m_setter)(blendFunc(anim, (a->*PropertyWrapperGetter<T>::m_getter)(), (b->*PropertyWrapperGetter<T>::m_getter)(), progress)); + } + +protected: + void (RenderStyle::*m_setter)(T); +}; + +#if USE(ACCELERATED_COMPOSITING) +class PropertyWrapperAcceleratedOpacity : public PropertyWrapper<float> { +public: + PropertyWrapperAcceleratedOpacity() + : PropertyWrapper<float>(CSSPropertyOpacity, &RenderStyle::opacity, &RenderStyle::setOpacity) + { + } + + virtual bool animationIsAccelerated() const { return true; } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + float fromOpacity = a->opacity(); + + // This makes sure we put the object being animated into a RenderLayer during the animation + dst->setOpacity(blendFunc(anim, (fromOpacity == 1) ? 0.999999f : fromOpacity, b->opacity(), progress)); + } +}; + +class PropertyWrapperAcceleratedTransform : public PropertyWrapper<const TransformOperations&> { +public: + PropertyWrapperAcceleratedTransform() + : PropertyWrapper<const TransformOperations&>(CSSPropertyWebkitTransform, &RenderStyle::transform, &RenderStyle::setTransform) + { + } + + virtual bool animationIsAccelerated() const { return true; } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + dst->setTransform(blendFunc(anim, a->transform(), b->transform(), progress)); + } +}; +#endif // USE(ACCELERATED_COMPOSITING) + +class PropertyWrapperShadow : public PropertyWrapperBase { +public: + PropertyWrapperShadow(int prop, const ShadowData* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(ShadowData*, bool)) + : PropertyWrapperBase(prop) + , m_getter(getter) + , m_setter(setter) + { + } + + virtual bool equals(const RenderStyle* a, const RenderStyle* b) const + { + const ShadowData* shadowA = (a->*m_getter)(); + const ShadowData* shadowB = (b->*m_getter)(); + + while (true) { + if (!shadowA && !shadowB) // end of both lists + return true; + + if (!shadowA || !shadowB) // end of just one of the lists + return false; + + if (*shadowA != *shadowB) + return false; + + shadowA = shadowA->next(); + shadowB = shadowB->next(); + } + + return true; + } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + const ShadowData* shadowA = (a->*m_getter)(); + const ShadowData* shadowB = (b->*m_getter)(); + ShadowData defaultShadowData(0, 0, 0, 0, Normal, property() == CSSPropertyWebkitBoxShadow, Color::transparent); + + ShadowData* newShadowData = 0; + ShadowData* lastShadow = 0; + + while (shadowA || shadowB) { + const ShadowData* srcShadow = shadowA ? shadowA : &defaultShadowData; + const ShadowData* dstShadow = shadowB ? shadowB : &defaultShadowData; + + ShadowData* blendedShadow = blendFunc(anim, srcShadow, dstShadow, progress); + if (!lastShadow) + newShadowData = blendedShadow; + else + lastShadow->setNext(blendedShadow); + + lastShadow = blendedShadow; + + shadowA = shadowA ? shadowA->next() : 0; + shadowB = shadowB ? shadowB->next() : 0; + } + + (dst->*m_setter)(newShadowData, false); + } + +private: + const ShadowData* (RenderStyle::*m_getter)() const; + void (RenderStyle::*m_setter)(ShadowData*, bool); +}; + +class PropertyWrapperMaybeInvalidColor : public PropertyWrapperBase { +public: + PropertyWrapperMaybeInvalidColor(int prop, const Color& (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&)) + : PropertyWrapperBase(prop) + , m_getter(getter) + , m_setter(setter) + { + } + + virtual bool equals(const RenderStyle* a, const RenderStyle* b) const + { + Color fromColor = (a->*m_getter)(); + Color toColor = (b->*m_getter)(); + + if (!fromColor.isValid() && !toColor.isValid()) + return true; + + if (!fromColor.isValid()) + fromColor = a->color(); + if (!toColor.isValid()) + toColor = b->color(); + + return fromColor == toColor; + } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + Color fromColor = (a->*m_getter)(); + Color toColor = (b->*m_getter)(); + + if (!fromColor.isValid() && !toColor.isValid()) + return; + + if (!fromColor.isValid()) + fromColor = a->color(); + if (!toColor.isValid()) + toColor = b->color(); + (dst->*m_setter)(blendFunc(anim, fromColor, toColor, progress)); + } + +private: + const Color& (RenderStyle::*m_getter)() const; + void (RenderStyle::*m_setter)(const Color&); +}; + +// Wrapper base class for an animatable property in a FillLayer +class FillLayerPropertyWrapperBase { +public: + FillLayerPropertyWrapperBase() + { + } + + virtual ~FillLayerPropertyWrapperBase() { } + + virtual bool equals(const FillLayer* a, const FillLayer* b) const = 0; + virtual void blend(const AnimationBase* anim, FillLayer* dst, const FillLayer* a, const FillLayer* b, double progress) const = 0; +}; + +template <typename T> +class FillLayerPropertyWrapperGetter : public FillLayerPropertyWrapperBase, public Noncopyable { +public: + FillLayerPropertyWrapperGetter(T (FillLayer::*getter)() const) + : m_getter(getter) + { + } + + virtual bool equals(const FillLayer* a, const FillLayer* b) const + { + // If the style pointers are the same, don't bother doing the test. + // If either is null, return false. If both are null, return true. + if ((!a && !b) || a == b) + return true; + if (!a || !b) + return false; + return (a->*m_getter)() == (b->*m_getter)(); + } + +protected: + T (FillLayer::*m_getter)() const; +}; + +template <typename T> +class FillLayerPropertyWrapper : public FillLayerPropertyWrapperGetter<T> { +public: + FillLayerPropertyWrapper(T (FillLayer::*getter)() const, void (FillLayer::*setter)(T)) + : FillLayerPropertyWrapperGetter<T>(getter) + , m_setter(setter) + { + } + + virtual void blend(const AnimationBase* anim, FillLayer* dst, const FillLayer* a, const FillLayer* b, double progress) const + { + (dst->*m_setter)(blendFunc(anim, (a->*FillLayerPropertyWrapperGetter<T>::m_getter)(), (b->*FillLayerPropertyWrapperGetter<T>::m_getter)(), progress)); + } + +protected: + void (FillLayer::*m_setter)(T); +}; + + +class FillLayersPropertyWrapper : public PropertyWrapperBase { +public: + typedef const FillLayer* (RenderStyle::*LayersGetter)() const; + typedef FillLayer* (RenderStyle::*LayersAccessor)(); + + FillLayersPropertyWrapper(int prop, LayersGetter getter, LayersAccessor accessor) + : PropertyWrapperBase(prop) + , m_layersGetter(getter) + , m_layersAccessor(accessor) + { + switch (prop) { + case CSSPropertyBackgroundPositionX: + case CSSPropertyWebkitMaskPositionX: + m_fillLayerPropertyWrapper = new FillLayerPropertyWrapper<Length>(&FillLayer::xPosition, &FillLayer::setXPosition); + break; + case CSSPropertyBackgroundPositionY: + case CSSPropertyWebkitMaskPositionY: + m_fillLayerPropertyWrapper = new FillLayerPropertyWrapper<Length>(&FillLayer::yPosition, &FillLayer::setYPosition); + break; + case CSSPropertyBackgroundSize: + case CSSPropertyWebkitBackgroundSize: + case CSSPropertyWebkitMaskSize: + m_fillLayerPropertyWrapper = new FillLayerPropertyWrapper<LengthSize>(&FillLayer::sizeLength, &FillLayer::setSizeLength); + break; + } + } + + virtual bool equals(const RenderStyle* a, const RenderStyle* b) const + { + const FillLayer* fromLayer = (a->*m_layersGetter)(); + const FillLayer* toLayer = (b->*m_layersGetter)(); + + while (fromLayer && toLayer) { + if (!m_fillLayerPropertyWrapper->equals(fromLayer, toLayer)) + return false; + + fromLayer = fromLayer->next(); + toLayer = toLayer->next(); + } + + return true; + } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + const FillLayer* aLayer = (a->*m_layersGetter)(); + const FillLayer* bLayer = (b->*m_layersGetter)(); + FillLayer* dstLayer = (dst->*m_layersAccessor)(); + + while (aLayer && bLayer && dstLayer) { + m_fillLayerPropertyWrapper->blend(anim, dstLayer, aLayer, bLayer, progress); + aLayer = aLayer->next(); + bLayer = bLayer->next(); + dstLayer = dstLayer->next(); + } + } + +private: + FillLayerPropertyWrapperBase* m_fillLayerPropertyWrapper; + + LayersGetter m_layersGetter; + LayersAccessor m_layersAccessor; +}; + +class ShorthandPropertyWrapper : public PropertyWrapperBase { +public: + ShorthandPropertyWrapper(int property, const CSSPropertyLonghand& longhand) + : PropertyWrapperBase(property) + { + for (unsigned i = 0; i < longhand.length(); ++i) { + PropertyWrapperBase* wrapper = wrapperForProperty(longhand.properties()[i]); + if (wrapper) + m_propertyWrappers.append(wrapper); + } + } + + virtual bool isShorthandWrapper() const { return true; } + + virtual bool equals(const RenderStyle* a, const RenderStyle* b) const + { + Vector<PropertyWrapperBase*>::const_iterator end = m_propertyWrappers.end(); + for (Vector<PropertyWrapperBase*>::const_iterator it = m_propertyWrappers.begin(); it != end; ++it) { + if (!(*it)->equals(a, b)) + return false; + } + return true; + } + + virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const + { + Vector<PropertyWrapperBase*>::const_iterator end = m_propertyWrappers.end(); + for (Vector<PropertyWrapperBase*>::const_iterator it = m_propertyWrappers.begin(); it != end; ++it) + (*it)->blend(anim, dst, a, b, progress); + } + + const Vector<PropertyWrapperBase*> propertyWrappers() const { return m_propertyWrappers; } + +private: + Vector<PropertyWrapperBase*> m_propertyWrappers; +}; + + +static Vector<PropertyWrapperBase*>* gPropertyWrappers = 0; +static int gPropertyWrapperMap[numCSSProperties]; + +static const int cInvalidPropertyWrapperIndex = -1; + + +void AnimationBase::ensurePropertyMap() +{ + // FIXME: This data is never destroyed. Maybe we should ref count it and toss it when the last AnimationController is destroyed? + if (gPropertyWrappers == 0) { + gPropertyWrappers = new Vector<PropertyWrapperBase*>(); + + // build the list of property wrappers to do the comparisons and blends + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyLeft, &RenderStyle::left, &RenderStyle::setLeft)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyRight, &RenderStyle::right, &RenderStyle::setRight)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyTop, &RenderStyle::top, &RenderStyle::setTop)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyBottom, &RenderStyle::bottom, &RenderStyle::setBottom)); + + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWidth, &RenderStyle::width, &RenderStyle::setWidth)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMinWidth, &RenderStyle::minWidth, &RenderStyle::setMinWidth)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMaxWidth, &RenderStyle::maxWidth, &RenderStyle::setMaxWidth)); + + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyHeight, &RenderStyle::height, &RenderStyle::setHeight)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMinHeight, &RenderStyle::minHeight, &RenderStyle::setMinHeight)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMaxHeight, &RenderStyle::maxHeight, &RenderStyle::setMaxHeight)); + + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderLeftWidth, &RenderStyle::borderLeftWidth, &RenderStyle::setBorderLeftWidth)); + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderRightWidth, &RenderStyle::borderRightWidth, &RenderStyle::setBorderRightWidth)); + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderTopWidth, &RenderStyle::borderTopWidth, &RenderStyle::setBorderTopWidth)); + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyBorderBottomWidth, &RenderStyle::borderBottomWidth, &RenderStyle::setBorderBottomWidth)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginLeft, &RenderStyle::marginLeft, &RenderStyle::setMarginLeft)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginRight, &RenderStyle::marginRight, &RenderStyle::setMarginRight)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginTop, &RenderStyle::marginTop, &RenderStyle::setMarginTop)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyMarginBottom, &RenderStyle::marginBottom, &RenderStyle::setMarginBottom)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingLeft, &RenderStyle::paddingLeft, &RenderStyle::setPaddingLeft)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingRight, &RenderStyle::paddingRight, &RenderStyle::setPaddingRight)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingTop, &RenderStyle::paddingTop, &RenderStyle::setPaddingTop)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyPaddingBottom, &RenderStyle::paddingBottom, &RenderStyle::setPaddingBottom)); + gPropertyWrappers->append(new PropertyWrapper<const Color&>(CSSPropertyColor, &RenderStyle::color, &RenderStyle::setColor)); + + gPropertyWrappers->append(new PropertyWrapper<const Color&>(CSSPropertyBackgroundColor, &RenderStyle::backgroundColor, &RenderStyle::setBackgroundColor)); + + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyBackgroundPositionX, &RenderStyle::backgroundLayers, &RenderStyle::accessBackgroundLayers)); + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyBackgroundPositionY, &RenderStyle::backgroundLayers, &RenderStyle::accessBackgroundLayers)); + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyBackgroundSize, &RenderStyle::backgroundLayers, &RenderStyle::accessBackgroundLayers)); + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyWebkitBackgroundSize, &RenderStyle::backgroundLayers, &RenderStyle::accessBackgroundLayers)); + + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyWebkitMaskPositionX, &RenderStyle::maskLayers, &RenderStyle::accessMaskLayers)); + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyWebkitMaskPositionY, &RenderStyle::maskLayers, &RenderStyle::accessMaskLayers)); + gPropertyWrappers->append(new FillLayersPropertyWrapper(CSSPropertyWebkitMaskSize, &RenderStyle::maskLayers, &RenderStyle::accessMaskLayers)); + + gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyFontSize, &RenderStyle::fontSize, &RenderStyle::setBlendedFontSize)); + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyWebkitColumnRuleWidth, &RenderStyle::columnRuleWidth, &RenderStyle::setColumnRuleWidth)); + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyWebkitColumnGap, &RenderStyle::columnGap, &RenderStyle::setColumnGap)); + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyWebkitColumnCount, &RenderStyle::columnCount, &RenderStyle::setColumnCount)); + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyWebkitColumnWidth, &RenderStyle::columnWidth, &RenderStyle::setColumnWidth)); + gPropertyWrappers->append(new PropertyWrapper<short>(CSSPropertyWebkitBorderHorizontalSpacing, &RenderStyle::horizontalBorderSpacing, &RenderStyle::setHorizontalBorderSpacing)); + gPropertyWrappers->append(new PropertyWrapper<short>(CSSPropertyWebkitBorderVerticalSpacing, &RenderStyle::verticalBorderSpacing, &RenderStyle::setVerticalBorderSpacing)); + gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyZIndex, &RenderStyle::zIndex, &RenderStyle::setZIndex)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyLineHeight, &RenderStyle::lineHeight, &RenderStyle::setLineHeight)); + gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyOutlineOffset, &RenderStyle::outlineOffset, &RenderStyle::setOutlineOffset)); + gPropertyWrappers->append(new PropertyWrapper<unsigned short>(CSSPropertyOutlineWidth, &RenderStyle::outlineWidth, &RenderStyle::setOutlineWidth)); + gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyLetterSpacing, &RenderStyle::letterSpacing, &RenderStyle::setLetterSpacing)); + gPropertyWrappers->append(new PropertyWrapper<int>(CSSPropertyWordSpacing, &RenderStyle::wordSpacing, &RenderStyle::setWordSpacing)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyTextIndent, &RenderStyle::textIndent, &RenderStyle::setTextIndent)); + + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyWebkitPerspective, &RenderStyle::perspective, &RenderStyle::setPerspective)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWebkitPerspectiveOriginX, &RenderStyle::perspectiveOriginX, &RenderStyle::setPerspectiveOriginX)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWebkitPerspectiveOriginY, &RenderStyle::perspectiveOriginY, &RenderStyle::setPerspectiveOriginY)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWebkitTransformOriginX, &RenderStyle::transformOriginX, &RenderStyle::setTransformOriginX)); + gPropertyWrappers->append(new PropertyWrapper<Length>(CSSPropertyWebkitTransformOriginY, &RenderStyle::transformOriginY, &RenderStyle::setTransformOriginY)); + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyWebkitTransformOriginZ, &RenderStyle::transformOriginZ, &RenderStyle::setTransformOriginZ)); + gPropertyWrappers->append(new PropertyWrapper<const LengthSize&>(CSSPropertyBorderTopLeftRadius, &RenderStyle::borderTopLeftRadius, &RenderStyle::setBorderTopLeftRadius)); + gPropertyWrappers->append(new PropertyWrapper<const LengthSize&>(CSSPropertyBorderTopRightRadius, &RenderStyle::borderTopRightRadius, &RenderStyle::setBorderTopRightRadius)); + gPropertyWrappers->append(new PropertyWrapper<const LengthSize&>(CSSPropertyBorderBottomLeftRadius, &RenderStyle::borderBottomLeftRadius, &RenderStyle::setBorderBottomLeftRadius)); + gPropertyWrappers->append(new PropertyWrapper<const LengthSize&>(CSSPropertyBorderBottomRightRadius, &RenderStyle::borderBottomRightRadius, &RenderStyle::setBorderBottomRightRadius)); + gPropertyWrappers->append(new PropertyWrapper<EVisibility>(CSSPropertyVisibility, &RenderStyle::visibility, &RenderStyle::setVisibility)); + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyZoom, &RenderStyle::zoom, &RenderStyle::setZoom)); + + gPropertyWrappers->append(new PropertyWrapper<LengthBox>(CSSPropertyClip, &RenderStyle::clip, &RenderStyle::setClip)); + +#if USE(ACCELERATED_COMPOSITING) + gPropertyWrappers->append(new PropertyWrapperAcceleratedOpacity()); + gPropertyWrappers->append(new PropertyWrapperAcceleratedTransform()); +#else + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyOpacity, &RenderStyle::opacity, &RenderStyle::setOpacity)); + gPropertyWrappers->append(new PropertyWrapper<const TransformOperations&>(CSSPropertyWebkitTransform, &RenderStyle::transform, &RenderStyle::setTransform)); +#endif + + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyWebkitColumnRuleColor, &RenderStyle::columnRuleColor, &RenderStyle::setColumnRuleColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyWebkitTextStrokeColor, &RenderStyle::textStrokeColor, &RenderStyle::setTextStrokeColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyWebkitTextFillColor, &RenderStyle::textFillColor, &RenderStyle::setTextFillColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderLeftColor, &RenderStyle::borderLeftColor, &RenderStyle::setBorderLeftColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderRightColor, &RenderStyle::borderRightColor, &RenderStyle::setBorderRightColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderTopColor, &RenderStyle::borderTopColor, &RenderStyle::setBorderTopColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyBorderBottomColor, &RenderStyle::borderBottomColor, &RenderStyle::setBorderBottomColor)); + gPropertyWrappers->append(new PropertyWrapperMaybeInvalidColor(CSSPropertyOutlineColor, &RenderStyle::outlineColor, &RenderStyle::setOutlineColor)); + + gPropertyWrappers->append(new PropertyWrapperShadow(CSSPropertyBoxShadow, &RenderStyle::boxShadow, &RenderStyle::setBoxShadow)); + gPropertyWrappers->append(new PropertyWrapperShadow(CSSPropertyWebkitBoxShadow, &RenderStyle::boxShadow, &RenderStyle::setBoxShadow)); + gPropertyWrappers->append(new PropertyWrapperShadow(CSSPropertyTextShadow, &RenderStyle::textShadow, &RenderStyle::setTextShadow)); + +#if ENABLE(SVG) + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyFillOpacity, &RenderStyle::fillOpacity, &RenderStyle::setFillOpacity)); + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyFloodOpacity, &RenderStyle::floodOpacity, &RenderStyle::setFloodOpacity)); + gPropertyWrappers->append(new PropertyWrapper<float>(CSSPropertyStrokeOpacity, &RenderStyle::strokeOpacity, &RenderStyle::setStrokeOpacity)); +#endif + + // TODO: + // + // CSSPropertyVerticalAlign + // + // Compound properties that have components that should be animatable: + // + // CSSPropertyWebkitColumns + // CSSPropertyWebkitBoxReflect + + // Make sure unused slots have a value + for (unsigned int i = 0; i < static_cast<unsigned int>(numCSSProperties); ++i) + gPropertyWrapperMap[i] = cInvalidPropertyWrapperIndex; + + // First we put the non-shorthand property wrappers into the map, so the shorthand-building + // code can find them. + size_t n = gPropertyWrappers->size(); + for (unsigned int i = 0; i < n; ++i) { + ASSERT((*gPropertyWrappers)[i]->property() - firstCSSProperty < numCSSProperties); + gPropertyWrapperMap[(*gPropertyWrappers)[i]->property() - firstCSSProperty] = i; + } + + // Now add the shorthand wrappers. + addShorthandProperties(); + } +} + +static void addPropertyWrapper(int propertyID, PropertyWrapperBase* wrapper) +{ + int propIndex = propertyID - firstCSSProperty; + + ASSERT(gPropertyWrapperMap[propIndex] == cInvalidPropertyWrapperIndex); + + unsigned wrapperIndex = gPropertyWrappers->size(); + gPropertyWrappers->append(wrapper); + gPropertyWrapperMap[propIndex] = wrapperIndex; +} + +static void addShorthandProperties() +{ + static const int animatableShorthandProperties[] = { + CSSPropertyBackground, // for background-color, background-position + CSSPropertyBackgroundPosition, + CSSPropertyWebkitMask, // for mask-position + CSSPropertyWebkitMaskPosition, + CSSPropertyBorderTop, CSSPropertyBorderRight, CSSPropertyBorderBottom, CSSPropertyBorderLeft, + CSSPropertyBorderColor, + CSSPropertyBorderRadius, + CSSPropertyBorderWidth, + CSSPropertyBorder, + CSSPropertyBorderSpacing, + CSSPropertyMargin, + CSSPropertyOutline, + CSSPropertyPadding, + CSSPropertyWebkitTextStroke, + CSSPropertyWebkitColumnRule, + CSSPropertyWebkitBorderRadius, + CSSPropertyWebkitTransformOrigin + }; + + for (size_t i = 0; i < WTF_ARRAY_LENGTH(animatableShorthandProperties); ++i) { + int propertyID = animatableShorthandProperties[i]; + CSSPropertyLonghand longhand = longhandForProperty(propertyID); + if (longhand.length() > 0) + addPropertyWrapper(propertyID, new ShorthandPropertyWrapper(propertyID, longhand)); + } + + // 'font' is not in the shorthand map. + static const int animatableFontProperties[] = { + CSSPropertyFontSize, + CSSPropertyFontWeight + }; + + CSSPropertyLonghand fontLonghand(animatableFontProperties, WTF_ARRAY_LENGTH(animatableFontProperties)); + addPropertyWrapper(CSSPropertyFont, new ShorthandPropertyWrapper(CSSPropertyFont, fontLonghand)); +} + +static PropertyWrapperBase* wrapperForProperty(int propertyID) +{ + int propIndex = propertyID - firstCSSProperty; + if (propIndex >= 0 && propIndex < numCSSProperties) { + int wrapperIndex = gPropertyWrapperMap[propIndex]; + if (wrapperIndex >= 0) + return (*gPropertyWrappers)[wrapperIndex]; + } + return 0; +} + +AnimationBase::AnimationBase(const Animation* transition, RenderObject* renderer, CompositeAnimation* compAnim) + : m_animState(AnimationStateNew) + , m_isAnimating(false) + , m_startTime(0) + , m_pauseTime(-1) + , m_requestedStartTime(0) + , m_object(renderer) + , m_animation(const_cast<Animation*>(transition)) + , m_compAnim(compAnim) + , m_isAccelerated(false) + , m_transformFunctionListValid(false) + , m_nextIterationDuration(-1) + , m_next(0) +{ + // Compute the total duration + m_totalDuration = -1; + if (m_animation->iterationCount() > 0) + m_totalDuration = m_animation->duration() * m_animation->iterationCount(); +} + +AnimationBase::~AnimationBase() +{ + m_compAnim->animationController()->removeFromStyleAvailableWaitList(this); + m_compAnim->animationController()->removeFromStartTimeResponseWaitList(this); +} + +bool AnimationBase::propertiesEqual(int prop, const RenderStyle* a, const RenderStyle* b) +{ + ensurePropertyMap(); + if (prop == cAnimateAll) { + size_t n = gPropertyWrappers->size(); + for (unsigned int i = 0; i < n; ++i) { + PropertyWrapperBase* wrapper = (*gPropertyWrappers)[i]; + // No point comparing shorthand wrappers for 'all'. + if (!wrapper->isShorthandWrapper() && !wrapper->equals(a, b)) + return false; + } + } else { + PropertyWrapperBase* wrapper = wrapperForProperty(prop); + if (wrapper) + return wrapper->equals(a, b); + } + return true; +} + +int AnimationBase::getPropertyAtIndex(int i, bool& isShorthand) +{ + ensurePropertyMap(); + if (i < 0 || i >= static_cast<int>(gPropertyWrappers->size())) + return CSSPropertyInvalid; + + PropertyWrapperBase* wrapper = (*gPropertyWrappers)[i]; + isShorthand = wrapper->isShorthandWrapper(); + return wrapper->property(); +} + +int AnimationBase::getNumProperties() +{ + ensurePropertyMap(); + return gPropertyWrappers->size(); +} + +// Returns true if we need to start animation timers +bool AnimationBase::blendProperties(const AnimationBase* anim, int prop, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) +{ + ASSERT(prop != cAnimateAll); + + ensurePropertyMap(); + PropertyWrapperBase* wrapper = wrapperForProperty(prop); + if (wrapper) { + wrapper->blend(anim, dst, a, b, progress); +#if USE(ACCELERATED_COMPOSITING) + return !wrapper->animationIsAccelerated() || !anim->isAccelerated(); +#else + return true; +#endif + } + + return false; +} + +#if USE(ACCELERATED_COMPOSITING) +bool AnimationBase::animationOfPropertyIsAccelerated(int prop) +{ + ensurePropertyMap(); + PropertyWrapperBase* wrapper = wrapperForProperty(prop); + return wrapper ? wrapper->animationIsAccelerated() : false; +} +#endif + +static bool gatherEnclosingShorthandProperties(int property, PropertyWrapperBase* wrapper, HashSet<int>& propertySet) +{ + if (!wrapper->isShorthandWrapper()) + return false; + + ShorthandPropertyWrapper* shorthandWrapper = static_cast<ShorthandPropertyWrapper*>(wrapper); + + bool contained = false; + for (size_t i = 0; i < shorthandWrapper->propertyWrappers().size(); ++i) { + PropertyWrapperBase* currWrapper = shorthandWrapper->propertyWrappers()[i]; + + if (gatherEnclosingShorthandProperties(property, currWrapper, propertySet) || currWrapper->property() == property) + contained = true; + } + + if (contained) + propertySet.add(wrapper->property()); + + return contained; +} + +// Note: this is inefficient. It's only called from pauseTransitionAtTime(). +HashSet<int> AnimationBase::animatableShorthandsAffectingProperty(int property) +{ + ensurePropertyMap(); + + HashSet<int> foundProperties; + for (int i = 0; i < getNumProperties(); ++i) + gatherEnclosingShorthandProperties(property, (*gPropertyWrappers)[i], foundProperties); + + return foundProperties; +} + +void AnimationBase::setNeedsStyleRecalc(Node* node) +{ + ASSERT(!node || (node->document() && !node->document()->inPageCache())); + if (node) + node->setNeedsStyleRecalc(SyntheticStyleChange); +} + +double AnimationBase::duration() const +{ + return m_animation->duration(); +} + +bool AnimationBase::playStatePlaying() const +{ + return m_animation->playState() == AnimPlayStatePlaying; +} + +bool AnimationBase::animationsMatch(const Animation* anim) const +{ + return m_animation->animationsMatch(anim); +} + +void AnimationBase::updateStateMachine(AnimStateInput input, double param) +{ + // If we get AnimationStateInputRestartAnimation then we force a new animation, regardless of state. + if (input == AnimationStateInputMakeNew) { + if (m_animState == AnimationStateStartWaitStyleAvailable) + m_compAnim->animationController()->removeFromStyleAvailableWaitList(this); + m_animState = AnimationStateNew; + m_startTime = 0; + m_pauseTime = -1; + m_requestedStartTime = 0; + m_nextIterationDuration = -1; + endAnimation(); + return; + } + + if (input == AnimationStateInputRestartAnimation) { + if (m_animState == AnimationStateStartWaitStyleAvailable) + m_compAnim->animationController()->removeFromStyleAvailableWaitList(this); + m_animState = AnimationStateNew; + m_startTime = 0; + m_pauseTime = -1; + m_requestedStartTime = 0; + m_nextIterationDuration = -1; + endAnimation(); + + if (!paused()) + updateStateMachine(AnimationStateInputStartAnimation, -1); + return; + } + + if (input == AnimationStateInputEndAnimation) { + if (m_animState == AnimationStateStartWaitStyleAvailable) + m_compAnim->animationController()->removeFromStyleAvailableWaitList(this); + m_animState = AnimationStateDone; + endAnimation(); + return; + } + + if (input == AnimationStateInputPauseOverride) { + if (m_animState == AnimationStateStartWaitResponse) { + // If we are in AnimationStateStartWaitResponse, the animation will get canceled before + // we get a response, so move to the next state. + endAnimation(); + updateStateMachine(AnimationStateInputStartTimeSet, beginAnimationUpdateTime()); + } + return; + } + + if (input == AnimationStateInputResumeOverride) { + if (m_animState == AnimationStateLooping || m_animState == AnimationStateEnding) { + // Start the animation + startAnimation(beginAnimationUpdateTime() - m_startTime); + } + return; + } + + // Execute state machine + switch (m_animState) { + case AnimationStateNew: + ASSERT(input == AnimationStateInputStartAnimation || input == AnimationStateInputPlayStateRunning || input == AnimationStateInputPlayStatePaused); + if (input == AnimationStateInputStartAnimation || input == AnimationStateInputPlayStateRunning) { + m_requestedStartTime = beginAnimationUpdateTime(); + m_animState = AnimationStateStartWaitTimer; + } + break; + case AnimationStateStartWaitTimer: + ASSERT(input == AnimationStateInputStartTimerFired || input == AnimationStateInputPlayStatePaused); + + if (input == AnimationStateInputStartTimerFired) { + ASSERT(param >= 0); + // Start timer has fired, tell the animation to start and wait for it to respond with start time + m_animState = AnimationStateStartWaitStyleAvailable; + m_compAnim->animationController()->addToStyleAvailableWaitList(this); + + // Trigger a render so we can start the animation + if (m_object) + m_compAnim->animationController()->addNodeChangeToDispatch(m_object->node()); + } else { + ASSERT(!paused()); + // We're waiting for the start timer to fire and we got a pause. Cancel the timer, pause and wait + m_pauseTime = beginAnimationUpdateTime(); + m_animState = AnimationStatePausedWaitTimer; + } + break; + case AnimationStateStartWaitStyleAvailable: + ASSERT(input == AnimationStateInputStyleAvailable || input == AnimationStateInputPlayStatePaused); + + if (input == AnimationStateInputStyleAvailable) { + // Start timer has fired, tell the animation to start and wait for it to respond with start time + m_animState = AnimationStateStartWaitResponse; + + overrideAnimations(); + + // Start the animation + if (overridden()) { + // We won't try to start accelerated animations if we are overridden and + // just move on to the next state. + m_animState = AnimationStateStartWaitResponse; + m_isAccelerated = false; + updateStateMachine(AnimationStateInputStartTimeSet, beginAnimationUpdateTime()); + } else { + double timeOffset = 0; + // If the value for 'animation-delay' is negative then the animation appears to have started in the past. + if (m_animation->delay() < 0) + timeOffset = -m_animation->delay(); + bool started = startAnimation(timeOffset); + + m_compAnim->animationController()->addToStartTimeResponseWaitList(this, started); + m_isAccelerated = started; + } + } else { + // We're waiting for the style to be available and we got a pause. Pause and wait + m_pauseTime = beginAnimationUpdateTime(); + m_animState = AnimationStatePausedWaitStyleAvailable; + } + break; + case AnimationStateStartWaitResponse: + ASSERT(input == AnimationStateInputStartTimeSet || input == AnimationStateInputPlayStatePaused); + + if (input == AnimationStateInputStartTimeSet) { + ASSERT(param >= 0); + // We have a start time, set it, unless the startTime is already set + if (m_startTime <= 0) { + m_startTime = param; + // If the value for 'animation-delay' is negative then the animation appears to have started in the past. + if (m_animation->delay() < 0) + m_startTime += m_animation->delay(); + } + + // Now that we know the start time, fire the start event. + onAnimationStart(0); // The elapsedTime is 0. + + // Decide whether to go into looping or ending state + goIntoEndingOrLoopingState(); + + // Dispatch updateStyleIfNeeded so we can start the animation + if (m_object) + m_compAnim->animationController()->addNodeChangeToDispatch(m_object->node()); + } else { + // We are pausing while waiting for a start response. Cancel the animation and wait. When + // we unpause, we will act as though the start timer just fired + m_pauseTime = -1; + pauseAnimation(beginAnimationUpdateTime() - m_startTime); + m_animState = AnimationStatePausedWaitResponse; + } + break; + case AnimationStateLooping: + ASSERT(input == AnimationStateInputLoopTimerFired || input == AnimationStateInputPlayStatePaused); + + if (input == AnimationStateInputLoopTimerFired) { + ASSERT(param >= 0); + // Loop timer fired, loop again or end. + onAnimationIteration(param); + + // Decide whether to go into looping or ending state + goIntoEndingOrLoopingState(); + } else { + // We are pausing while running. Cancel the animation and wait + m_pauseTime = beginAnimationUpdateTime(); + pauseAnimation(beginAnimationUpdateTime() - m_startTime); + m_animState = AnimationStatePausedRun; + } + break; + case AnimationStateEnding: + ASSERT(input == AnimationStateInputEndTimerFired || input == AnimationStateInputPlayStatePaused); + + if (input == AnimationStateInputEndTimerFired) { + + ASSERT(param >= 0); + // End timer fired, finish up + onAnimationEnd(param); + + m_animState = AnimationStateDone; + + if (m_object) { + if (m_animation->fillsForwards()) + m_animState = AnimationStateFillingForwards; + else + resumeOverriddenAnimations(); + + // Fire off another style change so we can set the final value + m_compAnim->animationController()->addNodeChangeToDispatch(m_object->node()); + } + } else { + // We are pausing while running. Cancel the animation and wait + m_pauseTime = beginAnimationUpdateTime(); + pauseAnimation(beginAnimationUpdateTime() - m_startTime); + m_animState = AnimationStatePausedRun; + } + // |this| may be deleted here + break; + case AnimationStatePausedWaitTimer: + ASSERT(input == AnimationStateInputPlayStateRunning); + ASSERT(paused()); + // Update the times + m_startTime += beginAnimationUpdateTime() - m_pauseTime; + m_pauseTime = -1; + + // we were waiting for the start timer to fire, go back and wait again + m_animState = AnimationStateNew; + updateStateMachine(AnimationStateInputStartAnimation, 0); + break; + case AnimationStatePausedWaitResponse: + case AnimationStatePausedWaitStyleAvailable: + case AnimationStatePausedRun: + // We treat these two cases the same. The only difference is that, when we are in + // AnimationStatePausedWaitResponse, we don't yet have a valid startTime, so we send 0 to startAnimation. + // When the AnimationStateInputStartTimeSet comes in and we were in AnimationStatePausedRun, we will notice + // that we have already set the startTime and will ignore it. + ASSERT(input == AnimationStateInputPlayStateRunning || input == AnimationStateInputStartTimeSet || input == AnimationStateInputStyleAvailable); + ASSERT(paused()); + + if (input == AnimationStateInputPlayStateRunning) { + // Update the times + if (m_animState == AnimationStatePausedRun) + m_startTime += beginAnimationUpdateTime() - m_pauseTime; + else + m_startTime = 0; + m_pauseTime = -1; + + if (m_animState == AnimationStatePausedWaitStyleAvailable) + m_animState = AnimationStateStartWaitStyleAvailable; + else { + // We were either running or waiting for a begin time response from the animation. + // Either way we need to restart the animation (possibly with an offset if we + // had already been running) and wait for it to start. + m_animState = AnimationStateStartWaitResponse; + + // Start the animation + if (overridden()) { + // We won't try to start accelerated animations if we are overridden and + // just move on to the next state. + updateStateMachine(AnimationStateInputStartTimeSet, beginAnimationUpdateTime()); + m_isAccelerated = true; + } else { + bool started = startAnimation(beginAnimationUpdateTime() - m_startTime); + m_compAnim->animationController()->addToStartTimeResponseWaitList(this, started); + m_isAccelerated = started; + } + } + break; + } + + if (input == AnimationStateInputStartTimeSet) { + ASSERT(m_animState == AnimationStatePausedWaitResponse); + + // We are paused but we got the callback that notifies us that an accelerated animation started. + // We ignore the start time and just move into the paused-run state. + m_animState = AnimationStatePausedRun; + ASSERT(m_startTime == 0); + m_startTime = param; + m_pauseTime += m_startTime; + break; + } + + ASSERT(m_animState == AnimationStatePausedWaitStyleAvailable); + // We are paused but we got the callback that notifies us that style has been updated. + // We move to the AnimationStatePausedWaitResponse state + m_animState = AnimationStatePausedWaitResponse; + overrideAnimations(); + break; + case AnimationStateFillingForwards: + case AnimationStateDone: + // We're done. Stay in this state until we are deleted + break; + } +} + +void AnimationBase::fireAnimationEventsIfNeeded() +{ + // If we are waiting for the delay time to expire and it has, go to the next state + if (m_animState != AnimationStateStartWaitTimer && m_animState != AnimationStateLooping && m_animState != AnimationStateEnding) + return; + + // We have to make sure to keep a ref to the this pointer, because it could get destroyed + // during an animation callback that might get called. Since the owner is a CompositeAnimation + // and it ref counts this object, we will keep a ref to that instead. That way the AnimationBase + // can still access the resources of its CompositeAnimation as needed. + RefPtr<AnimationBase> protector(this); + RefPtr<CompositeAnimation> compProtector(m_compAnim); + + // Check for start timeout + if (m_animState == AnimationStateStartWaitTimer) { + if (beginAnimationUpdateTime() - m_requestedStartTime >= m_animation->delay()) + updateStateMachine(AnimationStateInputStartTimerFired, 0); + return; + } + + double elapsedDuration = beginAnimationUpdateTime() - m_startTime; + // FIXME: we need to ensure that elapsedDuration is never < 0. If it is, this suggests that + // we had a recalcStyle() outside of beginAnimationUpdate()/endAnimationUpdate(). + // Also check in getTimeToNextEvent(). + elapsedDuration = max(elapsedDuration, 0.0); + + // Check for end timeout + if (m_totalDuration >= 0 && elapsedDuration >= m_totalDuration) { + // We may still be in AnimationStateLooping if we've managed to skip a + // whole iteration, in which case we should jump to the end state. + m_animState = AnimationStateEnding; + + // Fire an end event + updateStateMachine(AnimationStateInputEndTimerFired, m_totalDuration); + } else { + // Check for iteration timeout + if (m_nextIterationDuration < 0) { + // Hasn't been set yet, set it + double durationLeft = m_animation->duration() - fmod(elapsedDuration, m_animation->duration()); + m_nextIterationDuration = elapsedDuration + durationLeft; + } + + if (elapsedDuration >= m_nextIterationDuration) { + // Set to the next iteration + double previous = m_nextIterationDuration; + double durationLeft = m_animation->duration() - fmod(elapsedDuration, m_animation->duration()); + m_nextIterationDuration = elapsedDuration + durationLeft; + + // Send the event + updateStateMachine(AnimationStateInputLoopTimerFired, previous); + } + } +} + +void AnimationBase::updatePlayState(EAnimPlayState playState) +{ + // When we get here, we can have one of 4 desired states: running, paused, suspended, paused & suspended. + // The state machine can be in one of two states: running, paused. + // Set the state machine to the desired state. + bool pause = playState == AnimPlayStatePaused || m_compAnim->suspended(); + + if (pause == paused() && !isNew()) + return; + + updateStateMachine(pause ? AnimationStateInputPlayStatePaused : AnimationStateInputPlayStateRunning, -1); +} + +double AnimationBase::timeToNextService() +{ + // Returns the time at which next service is required. -1 means no service is required. 0 means + // service is required now, and > 0 means service is required that many seconds in the future. + if (paused() || isNew() || m_animState == AnimationStateFillingForwards) + return -1; + + if (m_animState == AnimationStateStartWaitTimer) { + double timeFromNow = m_animation->delay() - (beginAnimationUpdateTime() - m_requestedStartTime); + return max(timeFromNow, 0.0); + } + + fireAnimationEventsIfNeeded(); + + // In all other cases, we need service right away. + return 0; +} + +double AnimationBase::progress(double scale, double offset, const TimingFunction* tf) const +{ + if (preActive()) + return 0; + + double elapsedTime = getElapsedTime(); + + double dur = m_animation->duration(); + if (m_animation->iterationCount() > 0) + dur *= m_animation->iterationCount(); + + if (postActive() || !m_animation->duration()) + return 1.0; + if (m_animation->iterationCount() > 0 && elapsedTime >= dur) + return (m_animation->iterationCount() % 2) ? 1.0 : 0.0; + + // Compute the fractional time, taking into account direction. + // There is no need to worry about iterations, we assume that we would have + // short circuited above if we were done. + double fractionalTime = elapsedTime / m_animation->duration(); + int integralTime = static_cast<int>(fractionalTime); + fractionalTime -= integralTime; + + if ((m_animation->direction() == Animation::AnimationDirectionAlternate) && (integralTime & 1)) + fractionalTime = 1 - fractionalTime; + + if (scale != 1 || offset) + fractionalTime = (fractionalTime - offset) * scale; + + if (!tf) + tf = m_animation->timingFunction().get(); + + if (tf->isCubicBezierTimingFunction()) { + const CubicBezierTimingFunction* ctf = static_cast<const CubicBezierTimingFunction*>(tf); + return solveCubicBezierFunction(ctf->x1(), + ctf->y1(), + ctf->x2(), + ctf->y2(), + fractionalTime, m_animation->duration()); + } else if (tf->isStepsTimingFunction()) { + const StepsTimingFunction* stf = static_cast<const StepsTimingFunction*>(tf); + return solveStepsFunction(stf->numberOfSteps(), stf->stepAtStart(), fractionalTime); + } else + return fractionalTime; +} + +void AnimationBase::getTimeToNextEvent(double& time, bool& isLooping) const +{ + // Decide when the end or loop event needs to fire + const double elapsedDuration = max(beginAnimationUpdateTime() - m_startTime, 0.0); + double durationLeft = 0; + double nextIterationTime = m_totalDuration; + + if (m_totalDuration < 0 || elapsedDuration < m_totalDuration) { + durationLeft = m_animation->duration() > 0 ? (m_animation->duration() - fmod(elapsedDuration, m_animation->duration())) : 0; + nextIterationTime = elapsedDuration + durationLeft; + } + + if (m_totalDuration < 0 || nextIterationTime < m_totalDuration) { + // We are not at the end yet + ASSERT(nextIterationTime > 0); + isLooping = true; + } else { + // We are at the end + isLooping = false; + } + + time = durationLeft; +} + +void AnimationBase::goIntoEndingOrLoopingState() +{ + double t; + bool isLooping; + getTimeToNextEvent(t, isLooping); + m_animState = isLooping ? AnimationStateLooping : AnimationStateEnding; +} + +void AnimationBase::freezeAtTime(double t) +{ + if (!m_startTime) { + // If we haven't started yet, just generate the start event now + m_compAnim->animationController()->receivedStartTimeResponse(currentTime()); + } + + ASSERT(m_startTime); // if m_startTime is zero, we haven't started yet, so we'll get a bad pause time. + m_pauseTime = m_startTime + t - m_animation->delay(); + +#if USE(ACCELERATED_COMPOSITING) + if (m_object && m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + layer->backing()->suspendAnimations(m_pauseTime); + } +#endif +} + +double AnimationBase::beginAnimationUpdateTime() const +{ + return m_compAnim->animationController()->beginAnimationUpdateTime(); +} + +double AnimationBase::getElapsedTime() const +{ + if (paused()) + return m_pauseTime - m_startTime; + if (m_startTime <= 0) + return 0; + if (postActive()) + return 1; + + return beginAnimationUpdateTime() - m_startTime; +} + +} // namespace WebCore diff --git a/Source/WebCore/page/animation/AnimationBase.h b/Source/WebCore/page/animation/AnimationBase.h new file mode 100644 index 0000000..877d649 --- /dev/null +++ b/Source/WebCore/page/animation/AnimationBase.h @@ -0,0 +1,241 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef AnimationBase_h +#define AnimationBase_h + +#include "RenderStyleConstants.h" +#include <wtf/HashMap.h> +#include <wtf/HashSet.h> +#include <wtf/text/AtomicString.h> + +namespace WebCore { + +class Animation; +class AnimationBase; +class AnimationController; +class CompositeAnimation; +class Element; +class Node; +class RenderObject; +class RenderStyle; +class TimingFunction; + +class AnimationBase : public RefCounted<AnimationBase> { + friend class CompositeAnimation; + +public: + AnimationBase(const Animation* transition, RenderObject* renderer, CompositeAnimation* compAnim); + virtual ~AnimationBase(); + + RenderObject* renderer() const { return m_object; } + void clearRenderer() { m_object = 0; } + + double duration() const; + + // Animations and Transitions go through the states below. When entering the STARTED state + // the animation is started. This may or may not require deferred response from the animator. + // If so, we stay in this state until that response is received (and it returns the start time). + // Otherwise, we use the current time as the start time and go immediately to AnimationStateLooping + // or AnimationStateEnding. + enum AnimState { + AnimationStateNew, // animation just created, animation not running yet + AnimationStateStartWaitTimer, // start timer running, waiting for fire + AnimationStateStartWaitStyleAvailable, // waiting for style setup so we can start animations + AnimationStateStartWaitResponse, // animation started, waiting for response + AnimationStateLooping, // response received, animation running, loop timer running, waiting for fire + AnimationStateEnding, // received, animation running, end timer running, waiting for fire + AnimationStatePausedWaitTimer, // in pause mode when animation started + AnimationStatePausedWaitStyleAvailable, // in pause mode when waiting for style setup + AnimationStatePausedWaitResponse, // animation paused when in STARTING state + AnimationStatePausedRun, // animation paused when in LOOPING or ENDING state + AnimationStateDone, // end timer fired, animation finished and removed + AnimationStateFillingForwards // animation has ended and is retaining its final value + }; + + enum AnimStateInput { + AnimationStateInputMakeNew, // reset back to new from any state + AnimationStateInputStartAnimation, // animation requests a start + AnimationStateInputRestartAnimation, // force a restart from any state + AnimationStateInputStartTimerFired, // start timer fired + AnimationStateInputStyleAvailable, // style is setup, ready to start animating + AnimationStateInputStartTimeSet, // m_startTime was set + AnimationStateInputLoopTimerFired, // loop timer fired + AnimationStateInputEndTimerFired, // end timer fired + AnimationStateInputPauseOverride, // pause an animation due to override + AnimationStateInputResumeOverride, // resume an overridden animation + AnimationStateInputPlayStateRunning, // play state paused -> running + AnimationStateInputPlayStatePaused, // play state running -> paused + AnimationStateInputEndAnimation // force an end from any state + }; + + // Called when animation is in AnimationStateNew to start animation + void updateStateMachine(AnimStateInput, double param); + + // Animation has actually started, at passed time + void onAnimationStartResponse(double startTime) + { + updateStateMachine(AnimationBase::AnimationStateInputStartTimeSet, startTime); + } + + // Called to change to or from paused state + void updatePlayState(EAnimPlayState); + bool playStatePlaying() const; + + bool waitingToStart() const { return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer; } + bool preActive() const + { + return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStateStartWaitStyleAvailable || m_animState == AnimationStateStartWaitResponse; + } + + bool postActive() const { return m_animState == AnimationStateDone; } + bool active() const { return !postActive() && !preActive(); } + bool running() const { return !isNew() && !postActive(); } + bool paused() const { return m_pauseTime >= 0; } + bool isNew() const { return m_animState == AnimationStateNew; } + bool waitingForStartTime() const { return m_animState == AnimationStateStartWaitResponse; } + bool waitingForStyleAvailable() const { return m_animState == AnimationStateStartWaitStyleAvailable; } + + // "animating" means that something is running that requires a timer to keep firing + // (e.g. a software animation) + void setAnimating(bool inAnimating = true) { m_isAnimating = inAnimating; } + virtual double timeToNextService(); + + double progress(double scale, double offset, const TimingFunction*) const; + + virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* /*currentStyle*/, RenderStyle* /*targetStyle*/, RefPtr<RenderStyle>& /*animatedStyle*/) = 0; + virtual void getAnimatedStyle(RefPtr<RenderStyle>& /*animatedStyle*/) = 0; + + virtual bool shouldFireEvents() const { return false; } + + void fireAnimationEventsIfNeeded(); + + bool animationsMatch(const Animation*) const; + + void setAnimation(const Animation* anim) { m_animation = const_cast<Animation*>(anim); } + + // Return true if this animation is overridden. This will only be the case for + // ImplicitAnimations and is used to determine whether or not we should force + // set the start time. If an animation is overridden, it will probably not get + // back the AnimationStateInputStartTimeSet input. + virtual bool overridden() const { return false; } + + // Does this animation/transition involve the given property? + virtual bool affectsProperty(int /*property*/) const { return false; } + + bool isAnimatingProperty(int property, bool acceleratedOnly, bool isRunningNow) const + { + if (acceleratedOnly && !m_isAccelerated) + return false; + + if (isRunningNow) + return (!waitingToStart() && !postActive()) && affectsProperty(property); + + return !postActive() && affectsProperty(property); + } + + bool isTransformFunctionListValid() const { return m_transformFunctionListValid; } + + // Freeze the animation; used by DumpRenderTree. + void freezeAtTime(double t); + + double beginAnimationUpdateTime() const; + + double getElapsedTime() const; + + AnimationBase* next() const { return m_next; } + void setNext(AnimationBase* animation) { m_next = animation; } + + void styleAvailable() + { + ASSERT(waitingForStyleAvailable()); + updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1); + } + +#if USE(ACCELERATED_COMPOSITING) + static bool animationOfPropertyIsAccelerated(int prop); +#endif + + static HashSet<int> animatableShorthandsAffectingProperty(int property); + +protected: + virtual void overrideAnimations() { } + virtual void resumeOverriddenAnimations() { } + + CompositeAnimation* compositeAnimation() { return m_compAnim; } + + // These are called when the corresponding timer fires so subclasses can do any extra work + virtual void onAnimationStart(double /*elapsedTime*/) { } + virtual void onAnimationIteration(double /*elapsedTime*/) { } + virtual void onAnimationEnd(double /*elapsedTime*/) { } + + // timeOffset is an offset from the current time when the animation should start. Negative values are OK. + // Return value indicates whether to expect an asynchronous notifyAnimationStarted() callback. + virtual bool startAnimation(double /*timeOffset*/) { return false; } + // timeOffset is the time at which the animation is being paused. + virtual void pauseAnimation(double /*timeOffset*/) { } + virtual void endAnimation() { } + + void goIntoEndingOrLoopingState(); + + bool isAccelerated() const { return m_isAccelerated; } + + static bool propertiesEqual(int prop, const RenderStyle* a, const RenderStyle* b); + static int getPropertyAtIndex(int, bool& isShorthand); + static int getNumProperties(); + + // Return true if we need to start software animation timers + static bool blendProperties(const AnimationBase* anim, int prop, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress); + + static void setNeedsStyleRecalc(Node*); + + void getTimeToNextEvent(double& time, bool& isLooping) const; + + AnimState m_animState; + + bool m_isAnimating; // transition/animation requires continual timer firing + double m_startTime; + double m_pauseTime; + double m_requestedStartTime; + RenderObject* m_object; + + RefPtr<Animation> m_animation; + CompositeAnimation* m_compAnim; + bool m_isAccelerated; + bool m_transformFunctionListValid; + double m_totalDuration, m_nextIterationDuration; + + AnimationBase* m_next; + +private: + static void ensurePropertyMap(); +}; + +} // namespace WebCore + +#endif // AnimationBase_h diff --git a/Source/WebCore/page/animation/AnimationController.cpp b/Source/WebCore/page/animation/AnimationController.cpp new file mode 100644 index 0000000..e1281dd --- /dev/null +++ b/Source/WebCore/page/animation/AnimationController.cpp @@ -0,0 +1,615 @@ +/* + * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "AnimationController.h" + +#include "AnimationBase.h" +#include "AnimationControllerPrivate.h" +#include "CSSParser.h" +#include "CompositeAnimation.h" +#include "EventNames.h" +#include "Frame.h" +#include "RenderView.h" +#include "WebKitAnimationEvent.h" +#include "WebKitTransitionEvent.h" +#include <wtf/CurrentTime.h> +#include <wtf/UnusedParam.h> + +namespace WebCore { + +static const double cAnimationTimerDelay = 0.025; +static const double cBeginAnimationUpdateTimeNotSet = -1; + +AnimationControllerPrivate::AnimationControllerPrivate(Frame* frame) + : m_animationTimer(this, &AnimationControllerPrivate::animationTimerFired) + , m_updateStyleIfNeededDispatcher(this, &AnimationControllerPrivate::updateStyleIfNeededDispatcherFired) + , m_frame(frame) + , m_beginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet) + , m_styleAvailableWaiters(0) + , m_lastStyleAvailableWaiter(0) + , m_startTimeResponseWaiters(0) + , m_lastStartTimeResponseWaiter(0) + , m_waitingForStartTimeResponse(false) +{ +} + +AnimationControllerPrivate::~AnimationControllerPrivate() +{ +} + +PassRefPtr<CompositeAnimation> AnimationControllerPrivate::accessCompositeAnimation(RenderObject* renderer) +{ + RefPtr<CompositeAnimation> animation = m_compositeAnimations.get(renderer); + if (!animation) { + animation = CompositeAnimation::create(this); + m_compositeAnimations.set(renderer, animation); + } + return animation; +} + +bool AnimationControllerPrivate::clear(RenderObject* renderer) +{ + // Return false if we didn't do anything OR we are suspended (so we don't try to + // do a setNeedsStyleRecalc() when suspended). + PassRefPtr<CompositeAnimation> animation = m_compositeAnimations.take(renderer); + if (!animation) + return false; + animation->clearRenderer(); + return animation->suspended(); +} + +void AnimationControllerPrivate::updateAnimationTimer(bool callSetChanged/* = false*/) +{ + double needsService = -1; + bool calledSetChanged = false; + + RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end(); + for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) { + CompositeAnimation* compAnim = it->second.get(); + if (!compAnim->suspended() && compAnim->hasAnimations()) { + double t = compAnim->timeToNextService(); + if (t != -1 && (t < needsService || needsService == -1)) + needsService = t; + if (needsService == 0) { + if (callSetChanged) { + Node* node = it->first->node(); + ASSERT(!node || (node->document() && !node->document()->inPageCache())); + node->setNeedsStyleRecalc(SyntheticStyleChange); + calledSetChanged = true; + } + else + break; + } + } + } + + if (calledSetChanged) + m_frame->document()->updateStyleIfNeeded(); + + // If we want service immediately, we start a repeating timer to reduce the overhead of starting + if (needsService == 0) { + if (!m_animationTimer.isActive() || m_animationTimer.repeatInterval() == 0) + m_animationTimer.startRepeating(cAnimationTimerDelay); + return; + } + + // If we don't need service, we want to make sure the timer is no longer running + if (needsService < 0) { + if (m_animationTimer.isActive()) + m_animationTimer.stop(); + return; + } + + // Otherwise, we want to start a one-shot timer so we get here again + if (m_animationTimer.isActive()) + m_animationTimer.stop(); + m_animationTimer.startOneShot(needsService); +} + +void AnimationControllerPrivate::updateStyleIfNeededDispatcherFired(Timer<AnimationControllerPrivate>*) +{ + fireEventsAndUpdateStyle(); +} + +void AnimationControllerPrivate::fireEventsAndUpdateStyle() +{ + // Protect the frame from getting destroyed in the event handler + RefPtr<Frame> protector = m_frame; + + bool updateStyle = !m_eventsToDispatch.isEmpty() || !m_nodeChangesToDispatch.isEmpty(); + + // fire all the events + Vector<EventToDispatch>::const_iterator eventsToDispatchEnd = m_eventsToDispatch.end(); + for (Vector<EventToDispatch>::const_iterator it = m_eventsToDispatch.begin(); it != eventsToDispatchEnd; ++it) { + if (it->eventType == eventNames().webkitTransitionEndEvent) + it->element->dispatchEvent(WebKitTransitionEvent::create(it->eventType, it->name, it->elapsedTime)); + else + it->element->dispatchEvent(WebKitAnimationEvent::create(it->eventType, it->name, it->elapsedTime)); + } + + m_eventsToDispatch.clear(); + + // call setChanged on all the elements + Vector<RefPtr<Node> >::const_iterator nodeChangesToDispatchEnd = m_nodeChangesToDispatch.end(); + for (Vector<RefPtr<Node> >::const_iterator it = m_nodeChangesToDispatch.begin(); it != nodeChangesToDispatchEnd; ++it) + (*it)->setNeedsStyleRecalc(SyntheticStyleChange); + + m_nodeChangesToDispatch.clear(); + + if (updateStyle && m_frame) + m_frame->document()->updateStyleIfNeeded(); +} + +void AnimationControllerPrivate::startUpdateStyleIfNeededDispatcher() +{ + if (!m_updateStyleIfNeededDispatcher.isActive()) + m_updateStyleIfNeededDispatcher.startOneShot(0); +} + +void AnimationControllerPrivate::addEventToDispatch(PassRefPtr<Element> element, const AtomicString& eventType, const String& name, double elapsedTime) +{ + m_eventsToDispatch.grow(m_eventsToDispatch.size()+1); + EventToDispatch& event = m_eventsToDispatch[m_eventsToDispatch.size()-1]; + event.element = element; + event.eventType = eventType; + event.name = name; + event.elapsedTime = elapsedTime; + + startUpdateStyleIfNeededDispatcher(); +} + +void AnimationControllerPrivate::addNodeChangeToDispatch(PassRefPtr<Node> node) +{ + ASSERT(!node || (node->document() && !node->document()->inPageCache())); + if (!node) + return; + + m_nodeChangesToDispatch.append(node); + startUpdateStyleIfNeededDispatcher(); +} + +void AnimationControllerPrivate::animationTimerFired(Timer<AnimationControllerPrivate>*) +{ + // Make sure animationUpdateTime is updated, so that it is current even if no + // styleChange has happened (e.g. accelerated animations) + setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet); + + // When the timer fires, all we do is call setChanged on all DOM nodes with running animations and then do an immediate + // updateStyleIfNeeded. It will then call back to us with new information. + updateAnimationTimer(true); + + // Fire events right away, to avoid a flash of unanimated style after an animation completes, and before + // the 'end' event fires. + fireEventsAndUpdateStyle(); +} + +bool AnimationControllerPrivate::isRunningAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const +{ + RefPtr<CompositeAnimation> animation = m_compositeAnimations.get(renderer); + if (!animation) + return false; + + return animation->isAnimatingProperty(property, false, isRunningNow); +} + +bool AnimationControllerPrivate::isRunningAcceleratedAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const +{ + RefPtr<CompositeAnimation> animation = m_compositeAnimations.get(renderer); + if (!animation) + return false; + + return animation->isAnimatingProperty(property, true, isRunningNow); +} + +void AnimationControllerPrivate::suspendAnimations() +{ + suspendAnimationsForDocument(m_frame->document()); + + // Traverse subframes + for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling()) + child->animation()->suspendAnimations(); +} + +void AnimationControllerPrivate::resumeAnimations() +{ + resumeAnimationsForDocument(m_frame->document()); + + // Traverse subframes + for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling()) + child->animation()->resumeAnimations(); +} + +void AnimationControllerPrivate::suspendAnimationsForDocument(Document* document) +{ + setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet); + + RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end(); + for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) { + RenderObject* renderer = it->first; + if (renderer->document() == document) { + CompositeAnimation* compAnim = it->second.get(); + compAnim->suspendAnimations(); + } + } + + updateAnimationTimer(); +} + +void AnimationControllerPrivate::resumeAnimationsForDocument(Document* document) +{ + setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet); + + RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end(); + for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) { + RenderObject* renderer = it->first; + if (renderer->document() == document) { + CompositeAnimation* compAnim = it->second.get(); + compAnim->resumeAnimations(); + } + } + + updateAnimationTimer(); +} + +bool AnimationControllerPrivate::pauseAnimationAtTime(RenderObject* renderer, const String& name, double t) +{ + if (!renderer) + return false; + + RefPtr<CompositeAnimation> compAnim = accessCompositeAnimation(renderer); + if (!compAnim) + return false; + + if (compAnim->pauseAnimationAtTime(name, t)) { + renderer->node()->setNeedsStyleRecalc(SyntheticStyleChange); + startUpdateStyleIfNeededDispatcher(); + return true; + } + + return false; +} + +bool AnimationControllerPrivate::pauseTransitionAtTime(RenderObject* renderer, const String& property, double t) +{ + if (!renderer) + return false; + + RefPtr<CompositeAnimation> compAnim = accessCompositeAnimation(renderer); + if (!compAnim) + return false; + + if (compAnim->pauseTransitionAtTime(cssPropertyID(property), t)) { + renderer->node()->setNeedsStyleRecalc(SyntheticStyleChange); + startUpdateStyleIfNeededDispatcher(); + return true; + } + + return false; +} + +double AnimationControllerPrivate::beginAnimationUpdateTime() +{ + if (m_beginAnimationUpdateTime == cBeginAnimationUpdateTimeNotSet) + m_beginAnimationUpdateTime = currentTime(); + return m_beginAnimationUpdateTime; +} + +void AnimationControllerPrivate::endAnimationUpdate() +{ + styleAvailable(); + if (!m_waitingForStartTimeResponse) + startTimeResponse(beginAnimationUpdateTime()); +} + +void AnimationControllerPrivate::receivedStartTimeResponse(double time) +{ + m_waitingForStartTimeResponse = false; + startTimeResponse(time); +} + +PassRefPtr<RenderStyle> AnimationControllerPrivate::getAnimatedStyleForRenderer(RenderObject* renderer) +{ + if (!renderer) + return 0; + + RefPtr<CompositeAnimation> rendererAnimations = m_compositeAnimations.get(renderer); + if (!rendererAnimations) + return renderer->style(); + + // Make sure animationUpdateTime is updated, so that it is current even if no + // styleChange has happened (e.g. accelerated animations). + setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet); + RefPtr<RenderStyle> animatingStyle = rendererAnimations->getAnimatedStyle(); + if (!animatingStyle) + animatingStyle = renderer->style(); + + return animatingStyle.release(); +} + +unsigned AnimationControllerPrivate::numberOfActiveAnimations() const +{ + unsigned count = 0; + + RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end(); + for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) { + CompositeAnimation* compAnim = it->second.get(); + count += compAnim->numberOfActiveAnimations(); + } + + return count; +} + +void AnimationControllerPrivate::addToStyleAvailableWaitList(AnimationBase* animation) +{ + ASSERT(!animation->next()); + + if (m_styleAvailableWaiters) + m_lastStyleAvailableWaiter->setNext(animation); + else + m_styleAvailableWaiters = animation; + + m_lastStyleAvailableWaiter = animation; + animation->setNext(0); +} + +void AnimationControllerPrivate::removeFromStyleAvailableWaitList(AnimationBase* animationToRemove) +{ + AnimationBase* prevAnimation = 0; + for (AnimationBase* animation = m_styleAvailableWaiters; animation; animation = animation->next()) { + if (animation == animationToRemove) { + if (prevAnimation) + prevAnimation->setNext(animation->next()); + else + m_styleAvailableWaiters = animation->next(); + + if (m_lastStyleAvailableWaiter == animation) + m_lastStyleAvailableWaiter = prevAnimation; + + animationToRemove->setNext(0); + } + } +} + +void AnimationControllerPrivate::styleAvailable() +{ + // Go through list of waiters and send them on their way + for (AnimationBase* animation = m_styleAvailableWaiters; animation; ) { + AnimationBase* nextAnimation = animation->next(); + animation->setNext(0); + animation->styleAvailable(); + animation = nextAnimation; + } + + m_styleAvailableWaiters = 0; + m_lastStyleAvailableWaiter = 0; +} + +void AnimationControllerPrivate::addToStartTimeResponseWaitList(AnimationBase* animation, bool willGetResponse) +{ + // If willGetResponse is true, it means this animation is actually waiting for a response + // (which will come in as a call to notifyAnimationStarted()). + // In that case we don't need to add it to this list. We just set a waitingForAResponse flag + // which says we are waiting for the response. If willGetResponse is false, this animation + // is not waiting for a response for itself, but rather for a notifyXXXStarted() call for + // another animation to which it will sync. + // + // When endAnimationUpdate() is called we check to see if the waitingForAResponse flag is + // true. If so, we just return and will do our work when the first notifyXXXStarted() call + // comes in. If it is false, we will not be getting a notifyXXXStarted() call, so we will + // do our work right away. In both cases we call the onAnimationStartResponse() method + // on each animation. In the first case we send in the time we got from notifyXXXStarted(). + // In the second case, we just pass in the beginAnimationUpdateTime(). + // + // This will synchronize all software and accelerated animations started in the same + // updateStyleIfNeeded cycle. + // + ASSERT(!animation->next()); + + if (willGetResponse) + m_waitingForStartTimeResponse = true; + + if (m_startTimeResponseWaiters) + m_lastStartTimeResponseWaiter->setNext(animation); + else + m_startTimeResponseWaiters = animation; + + m_lastStartTimeResponseWaiter = animation; + animation->setNext(0); +} + +void AnimationControllerPrivate::removeFromStartTimeResponseWaitList(AnimationBase* animationToRemove) +{ + AnimationBase* prevAnimation = 0; + for (AnimationBase* animation = m_startTimeResponseWaiters; animation; animation = animation->next()) { + if (animation == animationToRemove) { + if (prevAnimation) + prevAnimation->setNext(animation->next()); + else + m_startTimeResponseWaiters = animation->next(); + + if (m_lastStartTimeResponseWaiter == animation) + m_lastStartTimeResponseWaiter = prevAnimation; + + animationToRemove->setNext(0); + } + prevAnimation = animation; + } + + if (!m_startTimeResponseWaiters) + m_waitingForStartTimeResponse = false; +} + +void AnimationControllerPrivate::startTimeResponse(double time) +{ + // Go through list of waiters and send them on their way + for (AnimationBase* animation = m_startTimeResponseWaiters; animation; ) { + AnimationBase* nextAnimation = animation->next(); + animation->setNext(0); + animation->onAnimationStartResponse(time); + animation = nextAnimation; + } + + m_startTimeResponseWaiters = 0; + m_lastStartTimeResponseWaiter = 0; +} + +AnimationController::AnimationController(Frame* frame) + : m_data(new AnimationControllerPrivate(frame)) +{ +} + +AnimationController::~AnimationController() +{ + delete m_data; +} + +void AnimationController::cancelAnimations(RenderObject* renderer) +{ + if (!m_data->hasAnimations()) + return; + + if (m_data->clear(renderer)) { + Node* node = renderer->node(); + ASSERT(!node || (node->document() && !node->document()->inPageCache())); + node->setNeedsStyleRecalc(SyntheticStyleChange); + } +} + +PassRefPtr<RenderStyle> AnimationController::updateAnimations(RenderObject* renderer, RenderStyle* newStyle) +{ + // Don't do anything if we're in the cache + if (!renderer->document() || renderer->document()->inPageCache()) + return newStyle; + + RenderStyle* oldStyle = renderer->style(); + + if ((!oldStyle || (!oldStyle->animations() && !oldStyle->transitions())) && (!newStyle->animations() && !newStyle->transitions())) + return newStyle; + + // Don't run transitions when printing. + if (renderer->view()->printing()) + return newStyle; + + // Fetch our current set of implicit animations from a hashtable. We then compare them + // against the animations in the style and make sure we're in sync. If destination values + // have changed, we reset the animation. We then do a blend to get new values and we return + // a new style. + ASSERT(renderer->node()); // FIXME: We do not animate generated content yet. + + RefPtr<CompositeAnimation> rendererAnimations = m_data->accessCompositeAnimation(renderer); + RefPtr<RenderStyle> blendedStyle = rendererAnimations->animate(renderer, oldStyle, newStyle); + + m_data->updateAnimationTimer(); + + if (blendedStyle != newStyle) { + // If the animations/transitions change opacity or transform, we need to update + // the style to impose the stacking rules. Note that this is also + // done in CSSStyleSelector::adjustRenderStyle(). + if (blendedStyle->hasAutoZIndex() && (blendedStyle->opacity() < 1.0f || blendedStyle->hasTransform())) + blendedStyle->setZIndex(0); + } + return blendedStyle.release(); +} + +PassRefPtr<RenderStyle> AnimationController::getAnimatedStyleForRenderer(RenderObject* renderer) +{ + return m_data->getAnimatedStyleForRenderer(renderer); +} + +void AnimationController::notifyAnimationStarted(RenderObject*, double startTime) +{ + m_data->receivedStartTimeResponse(startTime); +} + +bool AnimationController::pauseAnimationAtTime(RenderObject* renderer, const String& name, double t) +{ + return m_data->pauseAnimationAtTime(renderer, name, t); +} + +unsigned AnimationController::numberOfActiveAnimations() const +{ + return m_data->numberOfActiveAnimations(); +} + +bool AnimationController::pauseTransitionAtTime(RenderObject* renderer, const String& property, double t) +{ + return m_data->pauseTransitionAtTime(renderer, property, t); +} + +bool AnimationController::isRunningAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const +{ + return m_data->isRunningAnimationOnRenderer(renderer, property, isRunningNow); +} + +bool AnimationController::isRunningAcceleratedAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const +{ + return m_data->isRunningAcceleratedAnimationOnRenderer(renderer, property, isRunningNow); +} + +void AnimationController::suspendAnimations() +{ + m_data->suspendAnimations(); +} + +void AnimationController::resumeAnimations() +{ + m_data->resumeAnimations(); +} + +void AnimationController::suspendAnimationsForDocument(Document* document) +{ + m_data->suspendAnimationsForDocument(document); +} + +void AnimationController::resumeAnimationsForDocument(Document* document) +{ + m_data->resumeAnimationsForDocument(document); +} + +void AnimationController::beginAnimationUpdate() +{ + m_data->setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet); +} + +void AnimationController::endAnimationUpdate() +{ + m_data->endAnimationUpdate(); +} + +bool AnimationController::supportsAcceleratedAnimationOfProperty(CSSPropertyID property) +{ +#if USE(ACCELERATED_COMPOSITING) + return AnimationBase::animationOfPropertyIsAccelerated(property); +#else + UNUSED_PARAM(property); + return false; +#endif +} + +} // namespace WebCore diff --git a/Source/WebCore/page/animation/AnimationController.h b/Source/WebCore/page/animation/AnimationController.h new file mode 100644 index 0000000..5279467 --- /dev/null +++ b/Source/WebCore/page/animation/AnimationController.h @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef AnimationController_h +#define AnimationController_h + +#include "CSSPropertyNames.h" +#include <wtf/Forward.h> + +namespace WebCore { + +class AnimationBase; +class AnimationControllerPrivate; +class Document; +class Element; +class Frame; +class Node; +class RenderObject; +class RenderStyle; + +class AnimationController { +public: + AnimationController(Frame*); + ~AnimationController(); + + void cancelAnimations(RenderObject*); + PassRefPtr<RenderStyle> updateAnimations(RenderObject*, RenderStyle* newStyle); + PassRefPtr<RenderStyle> getAnimatedStyleForRenderer(RenderObject*); + + // This is called when an accelerated animation or transition has actually started to animate. + void notifyAnimationStarted(RenderObject*, double startTime); + + bool pauseAnimationAtTime(RenderObject*, const String& name, double t); // To be used only for testing + bool pauseTransitionAtTime(RenderObject*, const String& property, double t); // To be used only for testing + unsigned numberOfActiveAnimations() const; // To be used only for testing + + bool isRunningAnimationOnRenderer(RenderObject*, CSSPropertyID, bool isRunningNow = true) const; + bool isRunningAcceleratedAnimationOnRenderer(RenderObject*, CSSPropertyID, bool isRunningNow = true) const; + + void suspendAnimations(); + void resumeAnimations(); + + void suspendAnimationsForDocument(Document*); + void resumeAnimationsForDocument(Document*); + + void beginAnimationUpdate(); + void endAnimationUpdate(); + + static bool supportsAcceleratedAnimationOfProperty(CSSPropertyID); + +private: + AnimationControllerPrivate* m_data; +}; + +} // namespace WebCore + +#endif // AnimationController_h diff --git a/Source/WebCore/page/animation/AnimationControllerPrivate.h b/Source/WebCore/page/animation/AnimationControllerPrivate.h new file mode 100644 index 0000000..6812e09 --- /dev/null +++ b/Source/WebCore/page/animation/AnimationControllerPrivate.h @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2009 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef AnimationControllerPrivate_h +#define AnimationControllerPrivate_h + +#include "CSSPropertyNames.h" +#include "PlatformString.h" +#include "Timer.h" +#include <wtf/HashMap.h> +#include <wtf/PassRefPtr.h> +#include <wtf/RefPtr.h> +#include <wtf/Vector.h> +#include <wtf/text/AtomicString.h> + +namespace WebCore { + +class AnimationBase; +class CompositeAnimation; +class Document; +class Element; +class Frame; +class Node; +class RenderObject; +class RenderStyle; + +class AnimationControllerPrivate : public Noncopyable { +public: + AnimationControllerPrivate(Frame*); + ~AnimationControllerPrivate(); + + void updateAnimationTimer(bool callSetChanged = false); + + PassRefPtr<CompositeAnimation> accessCompositeAnimation(RenderObject*); + bool clear(RenderObject*); + + void updateStyleIfNeededDispatcherFired(Timer<AnimationControllerPrivate>*); + void startUpdateStyleIfNeededDispatcher(); + void addEventToDispatch(PassRefPtr<Element> element, const AtomicString& eventType, const String& name, double elapsedTime); + void addNodeChangeToDispatch(PassRefPtr<Node>); + + bool hasAnimations() const { return !m_compositeAnimations.isEmpty(); } + + void suspendAnimations(); + void resumeAnimations(); + + void suspendAnimationsForDocument(Document*); + void resumeAnimationsForDocument(Document*); + + bool isRunningAnimationOnRenderer(RenderObject*, CSSPropertyID, bool isRunningNow) const; + bool isRunningAcceleratedAnimationOnRenderer(RenderObject*, CSSPropertyID, bool isRunningNow) const; + + bool pauseAnimationAtTime(RenderObject*, const String& name, double t); + bool pauseTransitionAtTime(RenderObject*, const String& property, double t); + unsigned numberOfActiveAnimations() const; + + PassRefPtr<RenderStyle> getAnimatedStyleForRenderer(RenderObject* renderer); + + double beginAnimationUpdateTime(); + void setBeginAnimationUpdateTime(double t) { m_beginAnimationUpdateTime = t; } + void endAnimationUpdate(); + void receivedStartTimeResponse(double); + + void addToStyleAvailableWaitList(AnimationBase*); + void removeFromStyleAvailableWaitList(AnimationBase*); + + void addToStartTimeResponseWaitList(AnimationBase*, bool willGetResponse); + void removeFromStartTimeResponseWaitList(AnimationBase*); + +private: + void animationTimerFired(Timer<AnimationControllerPrivate>*); + + void styleAvailable(); + void fireEventsAndUpdateStyle(); + void startTimeResponse(double t); + + typedef HashMap<RenderObject*, RefPtr<CompositeAnimation> > RenderObjectAnimationMap; + + RenderObjectAnimationMap m_compositeAnimations; + Timer<AnimationControllerPrivate> m_animationTimer; + Timer<AnimationControllerPrivate> m_updateStyleIfNeededDispatcher; + Frame* m_frame; + + class EventToDispatch { + public: + RefPtr<Element> element; + AtomicString eventType; + String name; + double elapsedTime; + }; + + Vector<EventToDispatch> m_eventsToDispatch; + Vector<RefPtr<Node> > m_nodeChangesToDispatch; + + double m_beginAnimationUpdateTime; + AnimationBase* m_styleAvailableWaiters; + AnimationBase* m_lastStyleAvailableWaiter; + + AnimationBase* m_startTimeResponseWaiters; + AnimationBase* m_lastStartTimeResponseWaiter; + bool m_waitingForStartTimeResponse; +}; + +} // namespace WebCore + +#endif // AnimationControllerPrivate_h diff --git a/Source/WebCore/page/animation/CompositeAnimation.cpp b/Source/WebCore/page/animation/CompositeAnimation.cpp new file mode 100644 index 0000000..602491e --- /dev/null +++ b/Source/WebCore/page/animation/CompositeAnimation.cpp @@ -0,0 +1,563 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "CompositeAnimation.h" + +#include "AnimationControllerPrivate.h" +#include "CSSPropertyLonghand.h" +#include "CSSPropertyNames.h" +#include "ImplicitAnimation.h" +#include "KeyframeAnimation.h" +#include "RenderObject.h" +#include "RenderStyle.h" + +namespace WebCore { + +CompositeAnimation::~CompositeAnimation() +{ + // Toss the refs to all animations + m_transitions.clear(); + m_keyframeAnimations.clear(); +} + +void CompositeAnimation::clearRenderer() +{ + if (!m_transitions.isEmpty()) { + // Clear the renderers from all running animations, in case we are in the middle of + // an animation callback (see https://bugs.webkit.org/show_bug.cgi?id=22052) + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* transition = it->second.get(); + transition->clearRenderer(); + } + } + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + KeyframeAnimation* anim = it->second.get(); + anim->clearRenderer(); + } + } +} + +void CompositeAnimation::updateTransitions(RenderObject* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle) +{ + // If currentStyle is null or there are no old or new transitions, just skip it + if (!currentStyle || (!targetStyle->transitions() && m_transitions.isEmpty())) + return; + + // Mark all existing transitions as no longer active. We will mark the still active ones + // in the next loop and then toss the ones that didn't get marked. + CSSPropertyTransitionsMap::const_iterator end = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) + it->second->setActive(false); + + RefPtr<RenderStyle> modifiedCurrentStyle; + + // Check to see if we need to update the active transitions + if (targetStyle->transitions()) { + for (size_t i = 0; i < targetStyle->transitions()->size(); ++i) { + const Animation* anim = targetStyle->transitions()->animation(i); + bool isActiveTransition = anim->duration() || anim->delay() > 0; + + int prop = anim->property(); + + if (prop == cAnimateNone) + continue; + + bool all = prop == cAnimateAll; + + // Handle both the 'all' and single property cases. For the single prop case, we make only one pass + // through the loop. + for (int propertyIndex = 0; propertyIndex < AnimationBase::getNumProperties(); ++propertyIndex) { + if (all) { + // Get the next property which is not a shorthand. + bool isShorthand; + prop = AnimationBase::getPropertyAtIndex(propertyIndex, isShorthand); + if (isShorthand) + continue; + } + + // ImplicitAnimations are always hashed by actual properties, never cAnimateAll + ASSERT(prop >= firstCSSProperty && prop < (firstCSSProperty + numCSSProperties)); + + // If there is a running animation for this property, the transition is overridden + // and we have to use the unanimatedStyle from the animation. We do the test + // against the unanimated style here, but we "override" the transition later. + RefPtr<KeyframeAnimation> keyframeAnim = getAnimationForProperty(prop); + RenderStyle* fromStyle = keyframeAnim ? keyframeAnim->unanimatedStyle() : currentStyle; + + // See if there is a current transition for this prop + ImplicitAnimation* implAnim = m_transitions.get(prop).get(); + bool equal = true; + + if (implAnim) { + // If we are post active don't bother setting the active flag. This will cause + // this animation to get removed at the end of this function. + if (!implAnim->postActive()) + implAnim->setActive(true); + + // This might be a transition that is just finishing. That would be the case + // if it were postActive. But we still need to check for equality because + // it could be just finishing AND changing to a new goal state. + // + // This implAnim might also not be an already running transition. It might be + // newly added to the list in a previous iteration. This would happen if + // you have both an explicit transition-property and 'all' in the same + // list. In this case, the latter one overrides the earlier one, so we + // behave as though this is a running animation being replaced. + if (!implAnim->isTargetPropertyEqual(prop, targetStyle)) { + #if USE(ACCELERATED_COMPOSITING) + // For accelerated animations we need to return a new RenderStyle with the _current_ value + // of the property, so that restarted transitions use the correct starting point. + if (AnimationBase::animationOfPropertyIsAccelerated(prop) && implAnim->isAccelerated()) { + if (!modifiedCurrentStyle) + modifiedCurrentStyle = RenderStyle::clone(currentStyle); + + implAnim->blendPropertyValueInStyle(prop, modifiedCurrentStyle.get()); + } + #endif + m_transitions.remove(prop); + equal = false; + } + } else { + // We need to start a transition if it is active and the properties don't match + equal = !isActiveTransition || AnimationBase::propertiesEqual(prop, fromStyle, targetStyle); + } + + // We can be in this loop with an inactive transition (!isActiveTransition). We need + // to do that to check to see if we are canceling a transition. But we don't want to + // start one of the inactive transitions. So short circuit that here. (See + // <https://bugs.webkit.org/show_bug.cgi?id=24787> + if (!equal && isActiveTransition) { + // Add the new transition + m_transitions.set(prop, ImplicitAnimation::create(const_cast<Animation*>(anim), prop, renderer, this, modifiedCurrentStyle ? modifiedCurrentStyle.get() : fromStyle)); + } + + // We only need one pass for the single prop case + if (!all) + break; + } + } + } + + // Make a list of transitions to be removed + Vector<int> toBeRemoved; + end = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (!anim->active()) + toBeRemoved.append(anim->animatingProperty()); + } + + // Now remove the transitions from the list + for (size_t j = 0; j < toBeRemoved.size(); ++j) + m_transitions.remove(toBeRemoved[j]); +} + +void CompositeAnimation::updateKeyframeAnimations(RenderObject* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle) +{ + // Nothing to do if we don't have any animations, and didn't have any before + if (m_keyframeAnimations.isEmpty() && !targetStyle->hasAnimations()) + return; + + m_keyframeAnimations.checkConsistency(); + + AnimationNameMap::const_iterator kfend = m_keyframeAnimations.end(); + + if (currentStyle && currentStyle->hasAnimations() && targetStyle->hasAnimations() && *(currentStyle->animations()) == *(targetStyle->animations())) { + // The current and target animations are the same so we just need to toss any + // animation which is finished (postActive). + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it) { + if (it->second->postActive()) + it->second->setIndex(-1); + } + } else { + // Mark all existing animations as no longer active. + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it) + it->second->setIndex(-1); + + // Toss the animation order map. + m_keyframeAnimationOrderMap.clear(); + + DEFINE_STATIC_LOCAL(const AtomicString, none, ("none")); + + // Now mark any still active animations as active and add any new animations. + if (targetStyle->animations()) { + int numAnims = targetStyle->animations()->size(); + for (int i = 0; i < numAnims; ++i) { + const Animation* anim = targetStyle->animations()->animation(i); + AtomicString animationName(anim->name()); + + if (!anim->isValidAnimation()) + continue; + + // See if there is a current animation for this name. + RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(animationName.impl()); + + if (keyframeAnim) { + // If this animation is postActive, skip it so it gets removed at the end of this function. + if (keyframeAnim->postActive()) + continue; + + // This one is still active. + + // Animations match, but play states may differ. Update if needed. + keyframeAnim->updatePlayState(anim->playState()); + + // Set the saved animation to this new one, just in case the play state has changed. + keyframeAnim->setAnimation(anim); + keyframeAnim->setIndex(i); + } else if ((anim->duration() || anim->delay()) && anim->iterationCount() && animationName != none) { + keyframeAnim = KeyframeAnimation::create(const_cast<Animation*>(anim), renderer, i, this, targetStyle); + m_keyframeAnimations.set(keyframeAnim->name().impl(), keyframeAnim); + } + + // Add this to the animation order map. + if (keyframeAnim) + m_keyframeAnimationOrderMap.append(keyframeAnim->name().impl()); + } + } + } + + // Make a list of animations to be removed. + Vector<AtomicStringImpl*> animsToBeRemoved; + kfend = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it) { + KeyframeAnimation* keyframeAnim = it->second.get(); + if (keyframeAnim->index() < 0) + animsToBeRemoved.append(keyframeAnim->name().impl()); + } + + // Now remove the animations from the list. + for (size_t j = 0; j < animsToBeRemoved.size(); ++j) + m_keyframeAnimations.remove(animsToBeRemoved[j]); +} + +PassRefPtr<RenderStyle> CompositeAnimation::animate(RenderObject* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle) +{ + RefPtr<RenderStyle> resultStyle; + + // We don't do any transitions if we don't have a currentStyle (on startup). + updateTransitions(renderer, currentStyle, targetStyle); + updateKeyframeAnimations(renderer, currentStyle, targetStyle); + m_keyframeAnimations.checkConsistency(); + + if (currentStyle) { + // Now that we have transition objects ready, let them know about the new goal state. We want them + // to fill in a RenderStyle*& only if needed. + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator end = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) { + if (ImplicitAnimation* anim = it->second.get()) + anim->animate(this, renderer, currentStyle, targetStyle, resultStyle); + } + } + } + + // Now that we have animation objects ready, let them know about the new goal state. We want them + // to fill in a RenderStyle*& only if needed. + for (Vector<AtomicStringImpl*>::const_iterator it = m_keyframeAnimationOrderMap.begin(); it != m_keyframeAnimationOrderMap.end(); ++it) { + RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(*it); + if (keyframeAnim) + keyframeAnim->animate(this, renderer, currentStyle, targetStyle, resultStyle); + } + + return resultStyle ? resultStyle.release() : targetStyle; +} + +PassRefPtr<RenderStyle> CompositeAnimation::getAnimatedStyle() const +{ + RefPtr<RenderStyle> resultStyle; + CSSPropertyTransitionsMap::const_iterator end = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) { + if (ImplicitAnimation* implicitAnimation = it->second.get()) + implicitAnimation->getAnimatedStyle(resultStyle); + } + + m_keyframeAnimations.checkConsistency(); + + for (Vector<AtomicStringImpl*>::const_iterator it = m_keyframeAnimationOrderMap.begin(); it != m_keyframeAnimationOrderMap.end(); ++it) { + RefPtr<KeyframeAnimation> keyframeAnimation = m_keyframeAnimations.get(*it); + if (keyframeAnimation) + keyframeAnimation->getAnimatedStyle(resultStyle); + } + + return resultStyle; +} + +// "animating" means that something is running that requires the timer to keep firing +void CompositeAnimation::setAnimating(bool animating) +{ + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* transition = it->second.get(); + transition->setAnimating(animating); + } + } + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + KeyframeAnimation* anim = it->second.get(); + anim->setAnimating(animating); + } + } +} + +double CompositeAnimation::timeToNextService() const +{ + // Returns the time at which next service is required. -1 means no service is required. 0 means + // service is required now, and > 0 means service is required that many seconds in the future. + double minT = -1; + + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* transition = it->second.get(); + double t = transition ? transition->timeToNextService() : -1; + if (t < minT || minT == -1) + minT = t; + if (minT == 0) + return 0; + } + } + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + KeyframeAnimation* animation = it->second.get(); + double t = animation ? animation->timeToNextService() : -1; + if (t < minT || minT == -1) + minT = t; + if (minT == 0) + return 0; + } + } + + return minT; +} + +PassRefPtr<KeyframeAnimation> CompositeAnimation::getAnimationForProperty(int property) const +{ + RefPtr<KeyframeAnimation> retval; + + // We want to send back the last animation with the property if there are multiples. + // So we need to iterate through all animations + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + RefPtr<KeyframeAnimation> anim = it->second; + if (anim->hasAnimationForProperty(property)) + retval = anim; + } + } + + return retval; +} + +void CompositeAnimation::suspendAnimations() +{ + if (m_suspended) + return; + + m_suspended = true; + + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + if (KeyframeAnimation* anim = it->second.get()) + anim->updatePlayState(AnimPlayStatePaused); + } + } + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (anim && anim->hasStyle()) + anim->updatePlayState(AnimPlayStatePaused); + } + } +} + +void CompositeAnimation::resumeAnimations() +{ + if (!m_suspended) + return; + + m_suspended = false; + + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + KeyframeAnimation* anim = it->second.get(); + if (anim && anim->playStatePlaying()) + anim->updatePlayState(AnimPlayStatePlaying); + } + } + + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (anim && anim->hasStyle()) + anim->updatePlayState(AnimPlayStatePlaying); + } + } +} + +void CompositeAnimation::overrideImplicitAnimations(int property) +{ + CSSPropertyTransitionsMap::const_iterator end = m_transitions.end(); + if (!m_transitions.isEmpty()) { + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (anim && anim->animatingProperty() == property) + anim->setOverridden(true); + } + } +} + +void CompositeAnimation::resumeOverriddenImplicitAnimations(int property) +{ + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator end = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (anim && anim->animatingProperty() == property) + anim->setOverridden(false); + } + } +} + +bool CompositeAnimation::isAnimatingProperty(int property, bool acceleratedOnly, bool isRunningNow) const +{ + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + KeyframeAnimation* anim = it->second.get(); + if (anim && anim->isAnimatingProperty(property, acceleratedOnly, isRunningNow)) + return true; + } + } + + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (anim && anim->isAnimatingProperty(property, acceleratedOnly, isRunningNow)) + return true; + } + } + return false; +} + +bool CompositeAnimation::pauseAnimationAtTime(const AtomicString& name, double t) +{ + if (!name) + return false; + + m_keyframeAnimations.checkConsistency(); + + RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name.impl()); + if (!keyframeAnim || !keyframeAnim->running()) + return false; + + int count = keyframeAnim->m_animation->iterationCount(); + if ((t >= 0.0) && (!count || (t <= count * keyframeAnim->duration()))) { + keyframeAnim->freezeAtTime(t); + return true; + } + + return false; +} + +bool CompositeAnimation::pauseTransitionAtTime(int property, double t) +{ + if ((property < firstCSSProperty) || (property >= firstCSSProperty + numCSSProperties)) + return false; + + ImplicitAnimation* implAnim = m_transitions.get(property).get(); + if (!implAnim) { + // Check to see if this property is being animated via a shorthand. + // This code is only used for testing, so performance is not critical here. + HashSet<int> shorthandProperties = AnimationBase::animatableShorthandsAffectingProperty(property); + bool anyPaused = false; + HashSet<int>::const_iterator end = shorthandProperties.end(); + for (HashSet<int>::const_iterator it = shorthandProperties.begin(); it != end; ++it) { + if (pauseTransitionAtTime(*it, t)) + anyPaused = true; + } + return anyPaused; + } + + if (!implAnim->running()) + return false; + + if ((t >= 0.0) && (t <= implAnim->duration())) { + implAnim->freezeAtTime(t); + return true; + } + + return false; +} + +unsigned CompositeAnimation::numberOfActiveAnimations() const +{ + unsigned count = 0; + + if (!m_keyframeAnimations.isEmpty()) { + m_keyframeAnimations.checkConsistency(); + AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end(); + for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) { + KeyframeAnimation* anim = it->second.get(); + if (anim->running()) + ++count; + } + } + + if (!m_transitions.isEmpty()) { + CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end(); + for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) { + ImplicitAnimation* anim = it->second.get(); + if (anim->running()) + ++count; + } + } + + return count; +} + +} // namespace WebCore diff --git a/Source/WebCore/page/animation/CompositeAnimation.h b/Source/WebCore/page/animation/CompositeAnimation.h new file mode 100644 index 0000000..249f4c3 --- /dev/null +++ b/Source/WebCore/page/animation/CompositeAnimation.h @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef CompositeAnimation_h +#define CompositeAnimation_h + +#include "ImplicitAnimation.h" +#include "KeyframeAnimation.h" +#include <wtf/HashMap.h> +#include <wtf/Noncopyable.h> +#include <wtf/text/AtomicString.h> + +namespace WebCore { + +class AnimationControllerPrivate; +class AnimationController; +class RenderObject; +class RenderStyle; + +// A CompositeAnimation represents a collection of animations that are running +// on a single RenderObject, such as a number of properties transitioning at once. +class CompositeAnimation : public RefCounted<CompositeAnimation> { +public: + static PassRefPtr<CompositeAnimation> create(AnimationControllerPrivate* animationController) + { + return adoptRef(new CompositeAnimation(animationController)); + }; + + ~CompositeAnimation(); + + void clearRenderer(); + + PassRefPtr<RenderStyle> animate(RenderObject*, RenderStyle* currentStyle, RenderStyle* targetStyle); + PassRefPtr<RenderStyle> getAnimatedStyle() const; + + double timeToNextService() const; + + AnimationControllerPrivate* animationController() const { return m_animationController; } + + void suspendAnimations(); + void resumeAnimations(); + bool suspended() const { return m_suspended; } + + bool hasAnimations() const { return !m_transitions.isEmpty() || !m_keyframeAnimations.isEmpty(); } + + void setAnimating(bool); + bool isAnimatingProperty(int property, bool acceleratedOnly, bool isRunningNow) const; + + PassRefPtr<KeyframeAnimation> getAnimationForProperty(int property) const; + + void overrideImplicitAnimations(int property); + void resumeOverriddenImplicitAnimations(int property); + + bool pauseAnimationAtTime(const AtomicString& name, double t); + bool pauseTransitionAtTime(int property, double t); + unsigned numberOfActiveAnimations() const; + +private: + CompositeAnimation(AnimationControllerPrivate* animationController) + : m_animationController(animationController) + , m_numStyleAvailableWaiters(0) + , m_suspended(false) + { + } + + void updateTransitions(RenderObject*, RenderStyle* currentStyle, RenderStyle* targetStyle); + void updateKeyframeAnimations(RenderObject*, RenderStyle* currentStyle, RenderStyle* targetStyle); + + typedef HashMap<int, RefPtr<ImplicitAnimation> > CSSPropertyTransitionsMap; + typedef HashMap<AtomicStringImpl*, RefPtr<KeyframeAnimation> > AnimationNameMap; + + AnimationControllerPrivate* m_animationController; + CSSPropertyTransitionsMap m_transitions; + AnimationNameMap m_keyframeAnimations; + Vector<AtomicStringImpl*> m_keyframeAnimationOrderMap; + unsigned m_numStyleAvailableWaiters; + bool m_suspended; +}; + +} // namespace WebCore + +#endif // CompositeAnimation_h diff --git a/Source/WebCore/page/animation/ImplicitAnimation.cpp b/Source/WebCore/page/animation/ImplicitAnimation.cpp new file mode 100644 index 0000000..34607f6 --- /dev/null +++ b/Source/WebCore/page/animation/ImplicitAnimation.cpp @@ -0,0 +1,303 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#include "AnimationControllerPrivate.h" +#include "CompositeAnimation.h" +#include "CSSPropertyNames.h" +#include "EventNames.h" +#include "ImplicitAnimation.h" +#include "KeyframeAnimation.h" +#include "RenderLayer.h" +#include "RenderLayerBacking.h" +#include <wtf/UnusedParam.h> + +namespace WebCore { + +ImplicitAnimation::ImplicitAnimation(const Animation* transition, int animatingProperty, RenderObject* renderer, CompositeAnimation* compAnim, RenderStyle* fromStyle) + : AnimationBase(transition, renderer, compAnim) + , m_transitionProperty(transition->property()) + , m_animatingProperty(animatingProperty) + , m_overridden(false) + , m_active(true) + , m_fromStyle(fromStyle) +{ + ASSERT(animatingProperty != cAnimateAll); +} + +ImplicitAnimation::~ImplicitAnimation() +{ + // // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed. + if (!postActive()) + endAnimation(); +} + +bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const +{ + return m_object->document()->hasListenerType(inListenerType); +} + +void ImplicitAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle) +{ + // If we get this far and the animation is done, it means we are cleaning up a just finished animation. + // So just return. Everything is already all cleaned up. + if (postActive()) + return; + + // Reset to start the transition if we are new + if (isNew()) + reset(targetStyle); + + // Run a cycle of animation. + // We know we will need a new render style, so make one if needed + if (!animatedStyle) + animatedStyle = RenderStyle::clone(targetStyle); + + bool needsAnim = blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)); + // FIXME: we also need to detect cases where we have to software animate for other reasons, + // such as a child using inheriting the transform. https://bugs.webkit.org/show_bug.cgi?id=23902 + if (needsAnim) + setAnimating(); + else { +#if USE(ACCELERATED_COMPOSITING) + // If we are running an accelerated animation, set a flag in the style which causes the style + // to compare as different to any other style. This ensures that changes to the property + // that is animating are correctly detected during the animation (e.g. when a transition + // gets interrupted). + animatedStyle->setIsRunningAcceleratedAnimation(); +#endif + } + + // Fire the start timeout if needed + fireAnimationEventsIfNeeded(); +} + +void ImplicitAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle) +{ + if (!animatedStyle) + animatedStyle = RenderStyle::clone(m_toStyle.get()); + + blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)); +} + +bool ImplicitAnimation::startAnimation(double timeOffset) +{ +#if USE(ACCELERATED_COMPOSITING) + if (m_object && m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + return layer->backing()->startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get()); + } +#else + UNUSED_PARAM(timeOffset); +#endif + return false; +} + +void ImplicitAnimation::pauseAnimation(double timeOffset) +{ + if (!m_object) + return; + +#if USE(ACCELERATED_COMPOSITING) + if (m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + layer->backing()->transitionPaused(timeOffset, m_animatingProperty); + } +#else + UNUSED_PARAM(timeOffset); +#endif + // Restore the original (unanimated) style + if (!paused()) + setNeedsStyleRecalc(m_object->node()); +} + +void ImplicitAnimation::endAnimation() +{ +#if USE(ACCELERATED_COMPOSITING) + if (m_object && m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + layer->backing()->transitionFinished(m_animatingProperty); + } +#endif +} + +void ImplicitAnimation::onAnimationEnd(double elapsedTime) +{ + // If we have a keyframe animation on this property, this transition is being overridden. The keyframe + // animation keeps an unanimated style in case a transition starts while the keyframe animation is + // running. But now that the transition has completed, we need to update this style with its new + // destination. If we didn't, the next time through we would think a transition had started + // (comparing the old unanimated style with the new final style of the transition). + RefPtr<KeyframeAnimation> keyframeAnim = m_compAnim->getAnimationForProperty(m_animatingProperty); + if (keyframeAnim) + keyframeAnim->setUnanimatedStyle(m_toStyle); + + sendTransitionEvent(eventNames().webkitTransitionEndEvent, elapsedTime); + endAnimation(); +} + +bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime) +{ + if (eventType == eventNames().webkitTransitionEndEvent) { + Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER; + + if (shouldSendEventForListener(listenerType)) { + String propertyName; + if (m_animatingProperty != cAnimateAll) + propertyName = getPropertyName(static_cast<CSSPropertyID>(m_animatingProperty)); + + // Dispatch the event + RefPtr<Element> element = 0; + if (m_object->node() && m_object->node()->isElementNode()) + element = static_cast<Element*>(m_object->node()); + + ASSERT(!element || (element->document() && !element->document()->inPageCache())); + if (!element) + return false; + + // Schedule event handling + m_compAnim->animationController()->addEventToDispatch(element, eventType, propertyName, elapsedTime); + + // Restore the original (unanimated) style + if (eventType == eventNames().webkitTransitionEndEvent && element->renderer()) + setNeedsStyleRecalc(element.get()); + + return true; // Did dispatch an event + } + } + + return false; // Didn't dispatch an event +} + +void ImplicitAnimation::reset(RenderStyle* to) +{ + ASSERT(to); + ASSERT(m_fromStyle); + + m_toStyle = to; + + // Restart the transition + if (m_fromStyle && m_toStyle) + updateStateMachine(AnimationStateInputRestartAnimation, -1); + + // set the transform animation list + validateTransformFunctionList(); +} + +void ImplicitAnimation::setOverridden(bool b) +{ + if (b == m_overridden) + return; + + m_overridden = b; + updateStateMachine(m_overridden ? AnimationStateInputPauseOverride : AnimationStateInputResumeOverride, -1); +} + +bool ImplicitAnimation::affectsProperty(int property) const +{ + return (m_animatingProperty == property); +} + +bool ImplicitAnimation::isTargetPropertyEqual(int prop, const RenderStyle* targetStyle) +{ + // We can get here for a transition that has not started yet. This would make m_toStyle unset and null. + // So we check that here (see <https://bugs.webkit.org/show_bug.cgi?id=26706>) + if (!m_toStyle) + return false; + return propertiesEqual(prop, m_toStyle.get(), targetStyle); +} + +void ImplicitAnimation::blendPropertyValueInStyle(int prop, RenderStyle* currentStyle) +{ + // We should never add a transition with a 0 duration and delay. But if we ever did + // it would have a null toStyle. So just in case, let's check that here. (See + // <https://bugs.webkit.org/show_bug.cgi?id=24787> + if (!m_toStyle) + return; + + blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress(1, 0, 0)); +} + +void ImplicitAnimation::validateTransformFunctionList() +{ + m_transformFunctionListValid = false; + + if (!m_fromStyle || !m_toStyle) + return; + + const TransformOperations* val = &m_fromStyle->transform(); + const TransformOperations* toVal = &m_toStyle->transform(); + + if (val->operations().isEmpty()) + val = toVal; + + if (val->operations().isEmpty()) + return; + + // See if the keyframes are valid + if (val != toVal) { + // A list of length 0 matches anything + if (!toVal->operations().isEmpty()) { + // If the sizes of the function lists don't match, the lists don't match + if (val->operations().size() != toVal->operations().size()) + return; + + // If the types of each function are not the same, the lists don't match + for (size_t j = 0; j < val->operations().size(); ++j) { + if (!val->operations()[j]->isSameType(*toVal->operations()[j])) + return; + } + } + } + + // Keyframes are valid + m_transformFunctionListValid = true; +} + +double ImplicitAnimation::timeToNextService() +{ + double t = AnimationBase::timeToNextService(); +#if USE(ACCELERATED_COMPOSITING) + if (t != 0 || preActive()) + return t; + + // A return value of 0 means we need service. But if this is an accelerated animation we + // only need service at the end of the transition. + if (animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) { + bool isLooping; + getTimeToNextEvent(t, isLooping); + } +#endif + return t; +} + +} // namespace WebCore diff --git a/Source/WebCore/page/animation/ImplicitAnimation.h b/Source/WebCore/page/animation/ImplicitAnimation.h new file mode 100644 index 0000000..c40c00a --- /dev/null +++ b/Source/WebCore/page/animation/ImplicitAnimation.h @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ImplicitAnimation_h +#define ImplicitAnimation_h + +#include "AnimationBase.h" +#include "Document.h" + +namespace WebCore { + +// An ImplicitAnimation tracks the state of a transition of a specific CSS property +// for a single RenderObject. +class ImplicitAnimation : public AnimationBase { +public: + static PassRefPtr<ImplicitAnimation> create(const Animation* animation, int animatingProperty, RenderObject* renderer, CompositeAnimation* compositeAnimation, RenderStyle* fromStyle) + { + return adoptRef(new ImplicitAnimation(animation, animatingProperty, renderer, compositeAnimation, fromStyle)); + }; + + int transitionProperty() const { return m_transitionProperty; } + int animatingProperty() const { return m_animatingProperty; } + + virtual void onAnimationEnd(double elapsedTime); + virtual bool startAnimation(double timeOffset); + virtual void pauseAnimation(double /*timeOffset*/); + virtual void endAnimation(); + + virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* currentStyle, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle); + virtual void getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle); + virtual void reset(RenderStyle* to); + + void setOverridden(bool); + virtual bool overridden() const { return m_overridden; } + + virtual bool affectsProperty(int) const; + + bool hasStyle() const { return m_fromStyle && m_toStyle; } + + bool isTargetPropertyEqual(int, const RenderStyle* targetStyle); + + void blendPropertyValueInStyle(int, RenderStyle* currentStyle); + + virtual double timeToNextService(); + + bool active() const { return m_active; } + void setActive(bool b) { m_active = b; } + +protected: + bool shouldSendEventForListener(Document::ListenerType) const; + bool sendTransitionEvent(const AtomicString&, double elapsedTime); + + void validateTransformFunctionList(); + +private: + ImplicitAnimation(const Animation*, int animatingProperty, RenderObject*, CompositeAnimation*, RenderStyle* fromStyle); + virtual ~ImplicitAnimation(); + + int m_transitionProperty; // Transition property as specified in the RenderStyle. May be cAnimateAll + int m_animatingProperty; // Specific property for this ImplicitAnimation + bool m_overridden; // true when there is a keyframe animation that overrides the transitioning property + bool m_active; // used for culling the list of transitions + + // The two styles that we are blending. + RefPtr<RenderStyle> m_fromStyle; + RefPtr<RenderStyle> m_toStyle; +}; + +} // namespace WebCore + +#endif // ImplicitAnimation_h diff --git a/Source/WebCore/page/animation/KeyframeAnimation.cpp b/Source/WebCore/page/animation/KeyframeAnimation.cpp new file mode 100644 index 0000000..a499188 --- /dev/null +++ b/Source/WebCore/page/animation/KeyframeAnimation.cpp @@ -0,0 +1,461 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "KeyframeAnimation.h" + +#include "AnimationControllerPrivate.h" +#include "CSSPropertyNames.h" +#include "CSSStyleSelector.h" +#include "CompositeAnimation.h" +#include "EventNames.h" +#include "RenderLayer.h" +#include "RenderLayerBacking.h" +#include "RenderStyle.h" +#include <wtf/UnusedParam.h> + +using namespace std; + +namespace WebCore { + +KeyframeAnimation::KeyframeAnimation(const Animation* animation, RenderObject* renderer, int index, CompositeAnimation* compAnim, RenderStyle* unanimatedStyle) + : AnimationBase(animation, renderer, compAnim) + , m_keyframes(renderer, animation->name()) + , m_index(index) + , m_startEventDispatched(false) + , m_unanimatedStyle(unanimatedStyle) +{ + // Get the keyframe RenderStyles + if (m_object && m_object->node() && m_object->node()->isElementNode()) + m_object->document()->styleSelector()->keyframeStylesForAnimation(static_cast<Element*>(m_object->node()), unanimatedStyle, m_keyframes); + + // Update the m_transformFunctionListValid flag based on whether the function lists in the keyframes match. + validateTransformFunctionList(); +} + +KeyframeAnimation::~KeyframeAnimation() +{ + // Make sure to tell the renderer that we are ending. This will make sure any accelerated animations are removed. + if (!postActive()) + endAnimation(); +} + +void KeyframeAnimation::fetchIntervalEndpointsForProperty(int property, const RenderStyle*& fromStyle, const RenderStyle*& toStyle, double& prog) const +{ + // Find the first key + double elapsedTime = getElapsedTime(); + if (m_animation->duration() && m_animation->iterationCount() != Animation::IterationCountInfinite) + elapsedTime = min(elapsedTime, m_animation->duration() * m_animation->iterationCount()); + + double fractionalTime = m_animation->duration() ? (elapsedTime / m_animation->duration()) : 1; + + // FIXME: startTime can be before the current animation "frame" time. This is to sync with the frame time + // concept in AnimationTimeController. So we need to somehow sync the two. Until then, the possible + // error is small and will probably not be noticeable. Until we fix this, remove the assert. + // https://bugs.webkit.org/show_bug.cgi?id=52037 + // ASSERT(fractionalTime >= 0); + if (fractionalTime < 0) + fractionalTime = 0; + + // FIXME: share this code with AnimationBase::progress(). + int iteration = static_cast<int>(fractionalTime); + if (m_animation->iterationCount() != Animation::IterationCountInfinite) + iteration = min(iteration, m_animation->iterationCount() - 1); + fractionalTime -= iteration; + + bool reversing = (m_animation->direction() == Animation::AnimationDirectionAlternate) && (iteration & 1); + if (reversing) + fractionalTime = 1 - fractionalTime; + + size_t numKeyframes = m_keyframes.size(); + if (!numKeyframes) + return; + + ASSERT(!m_keyframes[0].key()); + ASSERT(m_keyframes[m_keyframes.size() - 1].key() == 1); + + int prevIndex = -1; + int nextIndex = -1; + + // FIXME: with a lot of keys, this linear search will be slow. We could binary search. + for (size_t i = 0; i < numKeyframes; ++i) { + const KeyframeValue& currKeyFrame = m_keyframes[i]; + + if (!currKeyFrame.containsProperty(property)) + continue; + + if (fractionalTime < currKeyFrame.key()) { + nextIndex = i; + break; + } + + prevIndex = i; + } + + double scale = 1; + double offset = 0; + + if (prevIndex == -1) + prevIndex = 0; + + if (nextIndex == -1) + nextIndex = m_keyframes.size() - 1; + + const KeyframeValue& prevKeyframe = m_keyframes[prevIndex]; + const KeyframeValue& nextKeyframe = m_keyframes[nextIndex]; + + fromStyle = prevKeyframe.style(); + toStyle = nextKeyframe.style(); + + offset = prevKeyframe.key(); + scale = 1.0 / (nextKeyframe.key() - prevKeyframe.key()); + + const TimingFunction* timingFunction = 0; + if (fromStyle->animations() && fromStyle->animations()->size() > 0) { + // We get the timing function from the first animation, because we've synthesized a RenderStyle for each keyframe. + timingFunction = fromStyle->animations()->animation(0)->timingFunction().get(); + } + + prog = progress(scale, offset, timingFunction); +} + +void KeyframeAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle) +{ + // Fire the start timeout if needed + fireAnimationEventsIfNeeded(); + + // If we have not yet started, we will not have a valid start time, so just start the animation if needed. + if (isNew() && m_animation->playState() == AnimPlayStatePlaying) + updateStateMachine(AnimationStateInputStartAnimation, -1); + + // If we get this far and the animation is done, it means we are cleaning up a just finished animation. + // If so, we need to send back the targetStyle. + if (postActive()) { + if (!animatedStyle) + animatedStyle = const_cast<RenderStyle*>(targetStyle); + return; + } + + // If we are waiting for the start timer, we don't want to change the style yet. + // Special case 1 - if the delay time is 0, then we do want to set the first frame of the + // animation right away. This avoids a flash when the animation starts. + // Special case 2 - if there is a backwards fill mode, then we want to continue + // through to the style blend so that we get the fromStyle. + if (waitingToStart() && m_animation->delay() > 0 && !m_animation->fillsBackwards()) + return; + + // If we have no keyframes, don't animate. + if (!m_keyframes.size()) { + updateStateMachine(AnimationStateInputEndAnimation, -1); + return; + } + + // Run a cycle of animation. + // We know we will need a new render style, so make one if needed. + if (!animatedStyle) + animatedStyle = RenderStyle::clone(targetStyle); + + // FIXME: we need to be more efficient about determining which keyframes we are animating between. + // We should cache the last pair or something. + HashSet<int>::const_iterator endProperties = m_keyframes.endProperties(); + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) { + int property = *it; + + // Get the from/to styles and progress between + const RenderStyle* fromStyle = 0; + const RenderStyle* toStyle = 0; + double progress; + fetchIntervalEndpointsForProperty(property, fromStyle, toStyle, progress); + + bool needsAnim = blendProperties(this, property, animatedStyle.get(), fromStyle, toStyle, progress); + if (needsAnim) + setAnimating(); + else { +#if USE(ACCELERATED_COMPOSITING) + // If we are running an accelerated animation, set a flag in the style + // to indicate it. This can be used to make sure we get an updated + // style for hit testing, etc. + animatedStyle->setIsRunningAcceleratedAnimation(); +#endif + } + } +} + +void KeyframeAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle) +{ + // If we're in the delay phase and we're not backwards filling, tell the caller + // to use the current style. + if (waitingToStart() && m_animation->delay() > 0 && !m_animation->fillsBackwards()) + return; + + if (!m_keyframes.size()) + return; + + if (!animatedStyle) + animatedStyle = RenderStyle::clone(m_object->style()); + + HashSet<int>::const_iterator endProperties = m_keyframes.endProperties(); + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) { + int property = *it; + + // Get the from/to styles and progress between + const RenderStyle* fromStyle = 0; + const RenderStyle* toStyle = 0; + double progress; + fetchIntervalEndpointsForProperty(property, fromStyle, toStyle, progress); + + blendProperties(this, property, animatedStyle.get(), fromStyle, toStyle, progress); + } +} + +bool KeyframeAnimation::hasAnimationForProperty(int property) const +{ + // FIXME: why not just m_keyframes.containsProperty()? + HashSet<int>::const_iterator end = m_keyframes.endProperties(); + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it) { + if (*it == property) + return true; + } + + return false; +} + +bool KeyframeAnimation::startAnimation(double timeOffset) +{ +#if USE(ACCELERATED_COMPOSITING) + if (m_object && m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + return layer->backing()->startAnimation(timeOffset, m_animation.get(), m_keyframes); + } +#else + UNUSED_PARAM(timeOffset); +#endif + return false; +} + +void KeyframeAnimation::pauseAnimation(double timeOffset) +{ + if (!m_object) + return; + +#if USE(ACCELERATED_COMPOSITING) + if (m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + layer->backing()->animationPaused(timeOffset, m_keyframes.animationName()); + } +#else + UNUSED_PARAM(timeOffset); +#endif + // Restore the original (unanimated) style + if (!paused()) + setNeedsStyleRecalc(m_object->node()); +} + +void KeyframeAnimation::endAnimation() +{ + if (!m_object) + return; + +#if USE(ACCELERATED_COMPOSITING) + if (m_object->hasLayer()) { + RenderLayer* layer = toRenderBoxModelObject(m_object)->layer(); + if (layer->isComposited()) + layer->backing()->animationFinished(m_keyframes.animationName()); + } +#endif + // Restore the original (unanimated) style + if (!paused()) + setNeedsStyleRecalc(m_object->node()); +} + +bool KeyframeAnimation::shouldSendEventForListener(Document::ListenerType listenerType) const +{ + return m_object->document()->hasListenerType(listenerType); +} + +void KeyframeAnimation::onAnimationStart(double elapsedTime) +{ + sendAnimationEvent(eventNames().webkitAnimationStartEvent, elapsedTime); +} + +void KeyframeAnimation::onAnimationIteration(double elapsedTime) +{ + sendAnimationEvent(eventNames().webkitAnimationIterationEvent, elapsedTime); +} + +void KeyframeAnimation::onAnimationEnd(double elapsedTime) +{ + sendAnimationEvent(eventNames().webkitAnimationEndEvent, elapsedTime); + // End the animation if we don't fill forwards. Forward filling + // animations are ended properly in the class destructor. + if (!m_animation->fillsForwards()) + endAnimation(); +} + +bool KeyframeAnimation::sendAnimationEvent(const AtomicString& eventType, double elapsedTime) +{ + Document::ListenerType listenerType; + if (eventType == eventNames().webkitAnimationIterationEvent) + listenerType = Document::ANIMATIONITERATION_LISTENER; + else if (eventType == eventNames().webkitAnimationEndEvent) + listenerType = Document::ANIMATIONEND_LISTENER; + else { + ASSERT(eventType == eventNames().webkitAnimationStartEvent); + if (m_startEventDispatched) + return false; + m_startEventDispatched = true; + listenerType = Document::ANIMATIONSTART_LISTENER; + } + + if (shouldSendEventForListener(listenerType)) { + // Dispatch the event + RefPtr<Element> element; + if (m_object->node() && m_object->node()->isElementNode()) + element = static_cast<Element*>(m_object->node()); + + ASSERT(!element || (element->document() && !element->document()->inPageCache())); + if (!element) + return false; + + // Schedule event handling + m_compAnim->animationController()->addEventToDispatch(element, eventType, m_keyframes.animationName(), elapsedTime); + + // Restore the original (unanimated) style + if (eventType == eventNames().webkitAnimationEndEvent && element->renderer()) + setNeedsStyleRecalc(element.get()); + + return true; // Did dispatch an event + } + + return false; // Did not dispatch an event +} + +void KeyframeAnimation::overrideAnimations() +{ + // This will override implicit animations that match the properties in the keyframe animation + HashSet<int>::const_iterator end = m_keyframes.endProperties(); + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it) + compositeAnimation()->overrideImplicitAnimations(*it); +} + +void KeyframeAnimation::resumeOverriddenAnimations() +{ + // This will resume overridden implicit animations + HashSet<int>::const_iterator end = m_keyframes.endProperties(); + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it) + compositeAnimation()->resumeOverriddenImplicitAnimations(*it); +} + +bool KeyframeAnimation::affectsProperty(int property) const +{ + HashSet<int>::const_iterator end = m_keyframes.endProperties(); + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it) { + if (*it == property) + return true; + } + return false; +} + +void KeyframeAnimation::validateTransformFunctionList() +{ + m_transformFunctionListValid = false; + + if (m_keyframes.size() < 2 || !m_keyframes.containsProperty(CSSPropertyWebkitTransform)) + return; + + // Empty transforms match anything, so find the first non-empty entry as the reference + size_t numKeyframes = m_keyframes.size(); + size_t firstNonEmptyTransformKeyframeIndex = numKeyframes; + + for (size_t i = 0; i < numKeyframes; ++i) { + const KeyframeValue& currentKeyframe = m_keyframes[i]; + if (currentKeyframe.style()->transform().operations().size()) { + firstNonEmptyTransformKeyframeIndex = i; + break; + } + } + + if (firstNonEmptyTransformKeyframeIndex == numKeyframes) + return; + + const TransformOperations* firstVal = &m_keyframes[firstNonEmptyTransformKeyframeIndex].style()->transform(); + + // See if the keyframes are valid + for (size_t i = firstNonEmptyTransformKeyframeIndex + 1; i < numKeyframes; ++i) { + const KeyframeValue& currentKeyframe = m_keyframes[i]; + const TransformOperations* val = ¤tKeyframe.style()->transform(); + + // A null transform matches anything + if (val->operations().isEmpty()) + continue; + + // If the sizes of the function lists don't match, the lists don't match + if (firstVal->operations().size() != val->operations().size()) + return; + + // If the types of each function are not the same, the lists don't match + for (size_t j = 0; j < firstVal->operations().size(); ++j) { + if (!firstVal->operations()[j]->isSameType(*val->operations()[j])) + return; + } + } + + // Keyframes are valid + m_transformFunctionListValid = true; +} + +double KeyframeAnimation::timeToNextService() +{ + double t = AnimationBase::timeToNextService(); +#if USE(ACCELERATED_COMPOSITING) + if (t != 0 || preActive()) + return t; + + // A return value of 0 means we need service. But if we only have accelerated animations we + // only need service at the end of the transition + HashSet<int>::const_iterator endProperties = m_keyframes.endProperties(); + bool acceleratedPropertiesOnly = true; + + for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) { + if (!animationOfPropertyIsAccelerated(*it) || !isAccelerated()) { + acceleratedPropertiesOnly = false; + break; + } + } + + if (acceleratedPropertiesOnly) { + bool isLooping; + getTimeToNextEvent(t, isLooping); + } +#endif + return t; +} + +} // namespace WebCore diff --git a/Source/WebCore/page/animation/KeyframeAnimation.h b/Source/WebCore/page/animation/KeyframeAnimation.h new file mode 100644 index 0000000..5099b50 --- /dev/null +++ b/Source/WebCore/page/animation/KeyframeAnimation.h @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2007 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef KeyframeAnimation_h +#define KeyframeAnimation_h + +#include "AnimationBase.h" +#include "Document.h" +#include "KeyframeList.h" + +namespace WebCore { + +class RenderStyle; + +// A KeyframeAnimation tracks the state of an explicit animation +// for a single RenderObject. +class KeyframeAnimation : public AnimationBase { +public: + static PassRefPtr<KeyframeAnimation> create(const Animation* animation, RenderObject* renderer, int index, CompositeAnimation* compositeAnimation, RenderStyle* unanimatedStyle) + { + return adoptRef(new KeyframeAnimation(animation, renderer, index, compositeAnimation, unanimatedStyle)); + }; + + virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* currentStyle, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle); + virtual void getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle); + + const AtomicString& name() const { return m_keyframes.animationName(); } + int index() const { return m_index; } + void setIndex(int i) { m_index = i; } + + bool hasAnimationForProperty(int property) const; + + void setUnanimatedStyle(PassRefPtr<RenderStyle> style) { m_unanimatedStyle = style; } + RenderStyle* unanimatedStyle() const { return m_unanimatedStyle.get(); } + + virtual double timeToNextService(); + +protected: + virtual void onAnimationStart(double elapsedTime); + virtual void onAnimationIteration(double elapsedTime); + virtual void onAnimationEnd(double elapsedTime); + virtual bool startAnimation(double timeOffset); + virtual void pauseAnimation(double timeOffset); + virtual void endAnimation(); + + virtual void overrideAnimations(); + virtual void resumeOverriddenAnimations(); + + bool shouldSendEventForListener(Document::ListenerType inListenerType) const; + bool sendAnimationEvent(const AtomicString&, double elapsedTime); + + virtual bool affectsProperty(int) const; + + void validateTransformFunctionList(); + +private: + KeyframeAnimation(const Animation* animation, RenderObject*, int index, CompositeAnimation*, RenderStyle* unanimatedStyle); + virtual ~KeyframeAnimation(); + + // Get the styles for the given property surrounding the current animation time and the progress between them. + void fetchIntervalEndpointsForProperty(int property, const RenderStyle*& fromStyle, const RenderStyle*& toStyle, double& progress) const; + + // The keyframes that we are blending. + KeyframeList m_keyframes; + + // The order in which this animation appears in the animation-name style. + int m_index; + bool m_startEventDispatched; + + // The style just before we started animation + RefPtr<RenderStyle> m_unanimatedStyle; +}; + +} // namespace WebCore + +#endif // KeyframeAnimation_h |