2011-04-12 10:18:44 +04:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-04-12 10:18:44 +04:00
|
|
|
#ifndef nsAnimationManager_h_
|
|
|
|
#define nsAnimationManager_h_
|
|
|
|
|
2012-09-14 20:10:08 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-12 10:18:44 +04:00
|
|
|
#include "AnimationCommon.h"
|
|
|
|
#include "nsCSSPseudoElements.h"
|
|
|
|
#include "nsStyleContext.h"
|
2011-08-11 17:29:50 +04:00
|
|
|
#include "nsDataHashtable.h"
|
2011-04-12 10:18:44 +04:00
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "mozilla/TimeStamp.h"
|
2012-07-31 21:28:21 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-04-12 10:18:44 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2011-04-12 10:18:44 +04:00
|
|
|
|
|
|
|
class nsCSSKeyframesRule;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace css {
|
|
|
|
class Declaration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
struct AnimationEventInfo {
|
|
|
|
nsRefPtr<mozilla::dom::Element> mElement;
|
|
|
|
nsAnimationEvent mEvent;
|
|
|
|
|
|
|
|
AnimationEventInfo(mozilla::dom::Element *aElement,
|
|
|
|
const nsString& aAnimationName,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aMessage, mozilla::TimeDuration aElapsedTime)
|
2012-07-31 21:28:21 +04:00
|
|
|
: mElement(aElement),
|
|
|
|
mEvent(true, aMessage, aAnimationName, aElapsedTime.ToSeconds())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsAnimationEvent doesn't support copy-construction, so we need
|
|
|
|
// to ourselves in order to work with nsTArray
|
|
|
|
AnimationEventInfo(const AnimationEventInfo &aOther)
|
|
|
|
: mElement(aOther.mElement),
|
|
|
|
mEvent(true, aOther.mEvent.message,
|
|
|
|
aOther.mEvent.animationName, aOther.mEvent.elapsedTime)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef InfallibleTArray<AnimationEventInfo> EventArray;
|
|
|
|
|
|
|
|
struct AnimationPropertySegment
|
|
|
|
{
|
|
|
|
float mFromKey, mToKey;
|
|
|
|
nsStyleAnimation::Value mFromValue, mToValue;
|
|
|
|
mozilla::css::ComputedTimingFunction mTimingFunction;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AnimationProperty
|
|
|
|
{
|
|
|
|
nsCSSProperty mProperty;
|
|
|
|
InfallibleTArray<AnimationPropertySegment> mSegments;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data about one animation (i.e., one of the values of
|
|
|
|
* 'animation-name') running on an element.
|
|
|
|
*/
|
|
|
|
struct ElementAnimation
|
|
|
|
{
|
|
|
|
ElementAnimation()
|
|
|
|
: mLastNotification(LAST_NOTIFICATION_NONE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString mName; // empty string for 'none'
|
|
|
|
float mIterationCount; // NS_IEEEPositiveInfinity() means infinite
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t mDirection;
|
|
|
|
uint8_t mFillMode;
|
|
|
|
uint8_t mPlayState;
|
2012-07-31 21:28:21 +04:00
|
|
|
|
|
|
|
bool FillsForwards() const {
|
|
|
|
return mFillMode == NS_STYLE_ANIMATION_FILL_MODE_BOTH ||
|
|
|
|
mFillMode == NS_STYLE_ANIMATION_FILL_MODE_FORWARDS;
|
|
|
|
}
|
|
|
|
bool FillsBackwards() const {
|
|
|
|
return mFillMode == NS_STYLE_ANIMATION_FILL_MODE_BOTH ||
|
|
|
|
mFillMode == NS_STYLE_ANIMATION_FILL_MODE_BACKWARDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsPaused() const {
|
|
|
|
return mPlayState == NS_STYLE_ANIMATION_PLAY_STATE_PAUSED;
|
|
|
|
}
|
|
|
|
|
2012-08-21 08:06:47 +04:00
|
|
|
bool HasAnimationOfProperty(nsCSSProperty aProperty) const;
|
2012-08-22 05:48:47 +04:00
|
|
|
bool IsRunningAt(mozilla::TimeStamp aTime) const;
|
2012-07-31 21:28:21 +04:00
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
mozilla::TimeStamp mStartTime; // with delay taken into account
|
|
|
|
mozilla::TimeStamp mPauseStart;
|
|
|
|
mozilla::TimeDuration mIterationDuration;
|
|
|
|
|
|
|
|
enum {
|
2012-08-22 19:56:38 +04:00
|
|
|
LAST_NOTIFICATION_NONE = uint32_t(-1),
|
|
|
|
LAST_NOTIFICATION_END = uint32_t(-2)
|
2012-07-31 21:28:21 +04:00
|
|
|
};
|
|
|
|
// One of the above constants, or an integer for the iteration
|
|
|
|
// whose start we last notified on.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mLastNotification;
|
2012-07-31 21:28:21 +04:00
|
|
|
|
|
|
|
InfallibleTArray<AnimationProperty> mProperties;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data about all of the animations running on an element.
|
|
|
|
*/
|
|
|
|
struct ElementAnimations : public mozilla::css::CommonElementAnimationData
|
|
|
|
{
|
|
|
|
typedef mozilla::TimeStamp TimeStamp;
|
|
|
|
typedef mozilla::TimeDuration TimeDuration;
|
|
|
|
|
|
|
|
ElementAnimations(mozilla::dom::Element *aElement, nsIAtom *aElementProperty,
|
|
|
|
nsAnimationManager *aAnimationManager);
|
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
// This function takes as input the start time, duration, and direction of an
|
|
|
|
// animation and returns the position in the current iteration. Note that
|
|
|
|
// this only works when we know that the animation is currently running.
|
|
|
|
// This way of calling the function can be used from the compositor. Note
|
|
|
|
// that if the animation has not started yet, has already ended, or is paused,
|
|
|
|
// it should not be run from the compositor. When this function is called
|
|
|
|
// from the main thread, we need the actual ElementAnimation* in order to
|
|
|
|
// get correct animation-fill behavior and to fire animation events.
|
|
|
|
// This function returns -1 for the position if the animation should not be
|
|
|
|
// run (because it is not currently active and has no fill behavior.)
|
|
|
|
static double GetPositionInIteration(TimeStamp aStartTime,
|
|
|
|
TimeStamp aCurrentTime,
|
|
|
|
TimeDuration aDuration,
|
|
|
|
double aIterationCount,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aDirection,
|
2012-07-31 21:28:21 +04:00
|
|
|
bool IsForElement = true,
|
|
|
|
ElementAnimation* aAnimation = nullptr,
|
|
|
|
ElementAnimations* aEa = nullptr,
|
|
|
|
EventArray* aEventsToDispatch = nullptr);
|
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
void EnsureStyleRuleFor(TimeStamp aRefreshTime,
|
|
|
|
EventArray &aEventsToDispatch);
|
|
|
|
|
|
|
|
bool IsForElement() const { // rather than for a pseudo-element
|
|
|
|
return mElementProperty == nsGkAtoms::animationsProperty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRestyleForAnimation(nsPresContext *aPresContext) {
|
|
|
|
nsRestyleHint styleHint = IsForElement() ? eRestyle_Self : eRestyle_Subtree;
|
|
|
|
aPresContext->PresShell()->RestyleForAnimation(mElement, styleHint);
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
// True if this animation can be performed on the compositor thread.
|
|
|
|
bool CanPerformOnCompositorThread() const;
|
|
|
|
bool HasAnimationOfProperty(nsCSSProperty aProperty) const;
|
2012-07-31 21:28:21 +04:00
|
|
|
|
|
|
|
// False when we know that our current style rule is valid
|
|
|
|
// indefinitely into the future (because all of our animations are
|
|
|
|
// either completed or paused). May be invalidated by a style change.
|
|
|
|
bool mNeedsRefreshes;
|
|
|
|
|
|
|
|
InfallibleTArray<ElementAnimation> mAnimations;
|
|
|
|
};
|
|
|
|
|
2011-04-12 10:18:44 +04:00
|
|
|
class nsAnimationManager : public mozilla::css::CommonAnimationManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAnimationManager(nsPresContext *aPresContext)
|
2012-07-31 21:28:21 +04:00
|
|
|
: mozilla::css::CommonAnimationManager(aPresContext)
|
|
|
|
, mKeyframesListIsDirty(true)
|
2011-04-12 10:18:44 +04:00
|
|
|
{
|
|
|
|
mKeyframesRules.Init(16); // FIXME: make infallible!
|
|
|
|
}
|
|
|
|
|
2012-07-31 21:28:21 +04:00
|
|
|
static ElementAnimations* GetAnimationsForCompositor(nsIContent* aContent,
|
|
|
|
nsCSSProperty aProperty)
|
|
|
|
{
|
|
|
|
if (!aContent->MayHaveAnimations())
|
|
|
|
return nullptr;
|
|
|
|
ElementAnimations* animations = static_cast<ElementAnimations*>(
|
|
|
|
aContent->GetProperty(nsGkAtoms::animationsProperty));
|
|
|
|
if (!animations)
|
|
|
|
return nullptr;
|
|
|
|
bool propertyMatches = animations->HasAnimationOfProperty(aProperty);
|
|
|
|
return (propertyMatches && animations->CanPerformOnCompositorThread()) ?
|
|
|
|
animations : nullptr;
|
|
|
|
}
|
2011-04-12 10:18:44 +04:00
|
|
|
|
2011-04-12 10:18:44 +04:00
|
|
|
// nsIStyleRuleProcessor (parts)
|
2012-09-14 20:10:08 +04:00
|
|
|
virtual void RulesMatching(ElementRuleProcessorData* aData) MOZ_OVERRIDE;
|
|
|
|
virtual void RulesMatching(PseudoElementRuleProcessorData* aData) MOZ_OVERRIDE;
|
|
|
|
virtual void RulesMatching(AnonBoxRuleProcessorData* aData) MOZ_OVERRIDE;
|
2011-04-12 10:18:44 +04:00
|
|
|
#ifdef MOZ_XUL
|
2012-09-14 20:10:08 +04:00
|
|
|
virtual void RulesMatching(XULTreeRuleProcessorData* aData) MOZ_OVERRIDE;
|
2011-04-12 10:18:44 +04:00
|
|
|
#endif
|
2011-12-09 09:01:52 +04:00
|
|
|
virtual NS_MUST_OVERRIDE size_t
|
|
|
|
SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const MOZ_OVERRIDE;
|
|
|
|
virtual NS_MUST_OVERRIDE size_t
|
|
|
|
SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const MOZ_OVERRIDE;
|
2011-04-12 10:18:44 +04:00
|
|
|
|
|
|
|
// nsARefreshObserver
|
2012-09-14 20:10:08 +04:00
|
|
|
virtual void WillRefresh(mozilla::TimeStamp aTime) MOZ_OVERRIDE;
|
2011-04-12 10:18:44 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the style rule that RulesMatching should add for
|
|
|
|
* aStyleContext. This might be different from what RulesMatching
|
|
|
|
* actually added during aStyleContext's construction because the
|
|
|
|
* element's animation-name may have changed. (However, this does
|
|
|
|
* return null during the non-animation restyling phase, as
|
|
|
|
* RulesMatching does.)
|
|
|
|
*
|
|
|
|
* aStyleContext may be a style context for aElement or for its
|
|
|
|
* :before or :after pseudo-element.
|
|
|
|
*/
|
|
|
|
nsIStyleRule* CheckAnimationRule(nsStyleContext* aStyleContext,
|
|
|
|
mozilla::dom::Element* aElement);
|
|
|
|
|
|
|
|
void KeyframesListIsDirty() {
|
2011-10-17 18:59:28 +04:00
|
|
|
mKeyframesListIsDirty = true;
|
2011-04-12 10:18:44 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 10:18:44 +04:00
|
|
|
/**
|
|
|
|
* Dispatch any pending events. We accumulate animationend and
|
|
|
|
* animationiteration events only during refresh driver notifications
|
|
|
|
* (and dispatch them at the end of such notifications), but we
|
|
|
|
* accumulate animationstart events at other points when style
|
|
|
|
* contexts are created.
|
|
|
|
*/
|
2011-12-15 08:42:15 +04:00
|
|
|
void DispatchEvents() {
|
|
|
|
// Fast-path the common case: no events
|
|
|
|
if (!mPendingEvents.IsEmpty()) {
|
|
|
|
DoDispatchEvents();
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 10:18:44 +04:00
|
|
|
|
2011-04-12 10:18:44 +04:00
|
|
|
private:
|
|
|
|
ElementAnimations* GetElementAnimations(mozilla::dom::Element *aElement,
|
|
|
|
nsCSSPseudoElements::Type aPseudoType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aCreateIfNeeded);
|
2011-04-12 10:18:44 +04:00
|
|
|
void BuildAnimations(nsStyleContext* aStyleContext,
|
|
|
|
InfallibleTArray<ElementAnimation>& aAnimations);
|
2011-04-23 05:36:23 +04:00
|
|
|
bool BuildSegment(InfallibleTArray<AnimationPropertySegment>& aSegments,
|
|
|
|
nsCSSProperty aProperty, const nsAnimation& aAnimation,
|
2011-04-12 10:18:44 +04:00
|
|
|
float aFromKey, nsStyleContext* aFromContext,
|
|
|
|
mozilla::css::Declaration* aFromDeclaration,
|
2011-04-23 05:36:23 +04:00
|
|
|
float aToKey, nsStyleContext* aToContext);
|
2011-04-12 10:18:44 +04:00
|
|
|
nsIStyleRule* GetAnimationRule(mozilla::dom::Element* aElement,
|
|
|
|
nsCSSPseudoElements::Type aPseudoType);
|
|
|
|
|
|
|
|
nsCSSKeyframesRule* KeyframesRuleFor(const nsSubstring& aName);
|
|
|
|
|
2011-12-15 08:42:15 +04:00
|
|
|
// The guts of DispatchEvents
|
|
|
|
void DoDispatchEvents();
|
|
|
|
|
2011-04-12 10:18:44 +04:00
|
|
|
bool mKeyframesListIsDirty;
|
|
|
|
nsDataHashtable<nsStringHashKey, nsCSSKeyframesRule*> mKeyframesRules;
|
2011-04-12 10:18:44 +04:00
|
|
|
|
|
|
|
EventArray mPendingEvents;
|
2011-04-12 10:18:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* !defined(nsAnimationManager_h_) */
|