2011-04-11 23:18:44 -07:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 04:12:37 -07: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-11 23:18:44 -07:00
|
|
|
#ifndef nsAnimationManager_h_
|
|
|
|
#define nsAnimationManager_h_
|
|
|
|
|
2012-09-14 09:10:08 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-25 04:21:20 -07:00
|
|
|
#include "mozilla/ContentEvents.h"
|
2011-04-11 23:18:44 -07:00
|
|
|
#include "AnimationCommon.h"
|
|
|
|
#include "nsCSSPseudoElements.h"
|
2015-04-20 18:22:09 -07:00
|
|
|
#include "mozilla/dom/Animation.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-04-11 23:18:44 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2011-04-11 23:18:44 -07:00
|
|
|
|
2013-09-15 18:06:52 -07:00
|
|
|
class nsStyleContext;
|
2011-04-11 23:18:44 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace css {
|
|
|
|
class Declaration;
|
2014-10-19 21:55:46 -07:00
|
|
|
} /* namespace css */
|
2014-12-17 15:42:41 -08:00
|
|
|
namespace dom {
|
|
|
|
class Promise;
|
|
|
|
} /* namespace dom */
|
2011-04-11 23:18:44 -07:00
|
|
|
|
2012-07-31 10:28:21 -07:00
|
|
|
struct AnimationEventInfo {
|
|
|
|
nsRefPtr<mozilla::dom::Element> mElement;
|
2013-09-26 23:20:54 -07:00
|
|
|
mozilla::InternalAnimationEvent mEvent;
|
2012-07-31 10:28:21 -07:00
|
|
|
|
|
|
|
AnimationEventInfo(mozilla::dom::Element *aElement,
|
2014-08-22 05:42:48 -07:00
|
|
|
const nsSubstring& aAnimationName,
|
2014-09-24 22:25:50 -07:00
|
|
|
uint32_t aMessage,
|
|
|
|
const mozilla::StickyTimeDuration& aElapsedTime,
|
2013-05-05 06:22:29 -07:00
|
|
|
const nsAString& aPseudoElement)
|
2014-02-10 21:35:25 -08:00
|
|
|
: mElement(aElement), mEvent(true, aMessage)
|
2012-07-31 10:28:21 -07:00
|
|
|
{
|
2014-02-10 21:35:25 -08:00
|
|
|
// XXX Looks like nobody initialize WidgetEvent::time
|
|
|
|
mEvent.animationName = aAnimationName;
|
|
|
|
mEvent.elapsedTime = aElapsedTime.ToSeconds();
|
|
|
|
mEvent.pseudoElement = aPseudoElement;
|
2012-07-31 10:28:21 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 23:20:54 -07:00
|
|
|
// InternalAnimationEvent doesn't support copy-construction, so we need
|
2012-07-31 10:28:21 -07:00
|
|
|
// to ourselves in order to work with nsTArray
|
|
|
|
AnimationEventInfo(const AnimationEventInfo &aOther)
|
2014-02-10 21:35:25 -08:00
|
|
|
: mElement(aOther.mElement), mEvent(true, aOther.mEvent.message)
|
2012-07-31 10:28:21 -07:00
|
|
|
{
|
2014-02-10 21:35:25 -08:00
|
|
|
mEvent.AssignAnimationEventData(aOther.mEvent, false);
|
2012-07-31 10:28:21 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef InfallibleTArray<AnimationEventInfo> EventArray;
|
|
|
|
|
2015-06-29 18:00:39 -07:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class CSSAnimation final : public Animation
|
2014-10-19 21:55:46 -07:00
|
|
|
{
|
|
|
|
public:
|
2015-04-27 19:29:13 -07:00
|
|
|
explicit CSSAnimation(dom::AnimationTimeline* aTimeline,
|
2015-06-30 23:19:04 -07:00
|
|
|
const nsSubstring& aAnimationName)
|
2015-06-08 19:13:53 -07:00
|
|
|
: dom::Animation(aTimeline)
|
2015-06-30 23:19:04 -07:00
|
|
|
, mAnimationName(aAnimationName)
|
2014-10-19 21:55:46 -07:00
|
|
|
, mIsStylePaused(false)
|
|
|
|
, mPauseShouldStick(false)
|
2015-02-13 07:58:33 -08:00
|
|
|
, mPreviousPhaseOrIteration(PREVIOUS_PHASE_BEFORE)
|
2014-10-19 21:55:46 -07:00
|
|
|
{
|
2015-06-30 23:19:04 -07:00
|
|
|
// We might need to drop this assertion once we add a script-accessible
|
|
|
|
// constructor but for animations generated from CSS markup the
|
|
|
|
// animation-name should never be empty.
|
|
|
|
MOZ_ASSERT(!mAnimationName.IsEmpty(), "animation-name should not be empty");
|
2014-10-19 21:55:46 -07:00
|
|
|
}
|
|
|
|
|
2015-06-29 18:00:39 -07:00
|
|
|
JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
2015-06-08 19:13:53 -07:00
|
|
|
CSSAnimation* AsCSSAnimation() override { return this; }
|
|
|
|
const CSSAnimation* AsCSSAnimation() const override { return this; }
|
2014-10-19 21:55:46 -07:00
|
|
|
|
2015-06-30 20:15:42 -07:00
|
|
|
// CSSAnimation interface
|
2015-06-30 23:19:04 -07:00
|
|
|
void GetAnimationName(nsString& aRetVal) const { aRetVal = mAnimationName; }
|
|
|
|
|
|
|
|
// Alternative to GetAnimationName that returns a reference to the member
|
|
|
|
// for more efficient internal usage.
|
|
|
|
const nsString& AnimationName() const { return mAnimationName; }
|
2015-06-30 20:15:42 -07:00
|
|
|
|
|
|
|
// Animation interface overrides
|
2015-06-29 18:00:39 -07:00
|
|
|
virtual Promise* GetReady(ErrorResult& aRv) override;
|
2015-05-18 22:00:48 -07:00
|
|
|
virtual void Play(ErrorResult& aRv, LimitBehavior aLimitBehavior) override;
|
2015-05-18 22:55:26 -07:00
|
|
|
virtual void Pause(ErrorResult& aRv) override;
|
2014-10-19 21:55:46 -07:00
|
|
|
|
2015-06-29 18:00:39 -07:00
|
|
|
virtual AnimationPlayState PlayStateFromJS() const override;
|
2015-05-18 22:00:48 -07:00
|
|
|
virtual void PlayFromJS(ErrorResult& aRv) override;
|
2014-11-16 20:45:58 -08:00
|
|
|
|
2014-10-19 21:55:46 -07:00
|
|
|
void PlayFromStyle();
|
|
|
|
void PauseFromStyle();
|
2015-06-08 19:13:53 -07:00
|
|
|
void CancelFromStyle() override
|
|
|
|
{
|
2015-06-08 19:13:54 -07:00
|
|
|
mOwningElement = OwningElementRef();
|
2015-06-08 19:13:53 -07:00
|
|
|
Animation::CancelFromStyle();
|
2015-06-08 19:13:54 -07:00
|
|
|
MOZ_ASSERT(mSequenceNum == kUnsequenced);
|
2015-06-08 19:13:53 -07:00
|
|
|
}
|
2014-10-19 21:55:46 -07:00
|
|
|
|
|
|
|
bool IsStylePaused() const { return mIsStylePaused; }
|
|
|
|
|
2015-06-08 19:13:54 -07:00
|
|
|
bool HasLowerCompositeOrderThan(const Animation& aOther) const override;
|
2015-06-08 19:13:54 -07:00
|
|
|
bool IsUsingCustomCompositeOrder() const override
|
|
|
|
{
|
|
|
|
return mOwningElement.IsSet();
|
|
|
|
}
|
2015-06-08 19:13:54 -07:00
|
|
|
|
|
|
|
void SetAnimationIndex(uint64_t aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsUsingCustomCompositeOrder());
|
|
|
|
mSequenceNum = aIndex;
|
|
|
|
}
|
|
|
|
void CopyAnimationIndex(const CSSAnimation& aOther)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsUsingCustomCompositeOrder() &&
|
|
|
|
aOther.IsUsingCustomCompositeOrder());
|
|
|
|
mSequenceNum = aOther.mSequenceNum;
|
|
|
|
}
|
|
|
|
|
2014-10-19 21:55:47 -07:00
|
|
|
void QueueEvents(EventArray& aEventsToDispatch);
|
|
|
|
|
2015-06-08 19:13:53 -07:00
|
|
|
// Returns the element or pseudo-element whose animation-name property
|
|
|
|
// this CSSAnimation corresponds to (if any). This is used for determining
|
|
|
|
// the relative composite order of animations generated from CSS markup.
|
|
|
|
//
|
|
|
|
// Typically this will be the same as the target element of the keyframe
|
|
|
|
// effect associated with this animation. However, it can differ in the
|
|
|
|
// following circumstances:
|
|
|
|
//
|
|
|
|
// a) If script removes or replaces the effect of this animation,
|
|
|
|
// b) If this animation is cancelled (e.g. by updating the
|
|
|
|
// animation-name property or removing the owning element from the
|
|
|
|
// document),
|
|
|
|
// c) If this object is generated from script using the CSSAnimation
|
|
|
|
// constructor.
|
|
|
|
//
|
2015-06-08 19:13:54 -07:00
|
|
|
// For (b) and (c) the returned owning element will return !IsSet().
|
|
|
|
const OwningElementRef& OwningElement() const { return mOwningElement; }
|
2015-06-08 19:13:53 -07:00
|
|
|
|
2015-06-08 19:13:54 -07:00
|
|
|
// Sets the owning element which is used for determining the composite
|
|
|
|
// order of CSSAnimation objects generated from CSS markup.
|
2015-06-08 19:13:53 -07:00
|
|
|
//
|
2015-06-08 19:13:54 -07:00
|
|
|
// @see OwningElement()
|
|
|
|
void SetOwningElement(const OwningElementRef& aElement)
|
2015-06-08 19:13:53 -07:00
|
|
|
{
|
2015-06-08 19:13:54 -07:00
|
|
|
mOwningElement = aElement;
|
2015-06-08 19:13:53 -07:00
|
|
|
}
|
|
|
|
|
2015-03-31 15:05:54 -07:00
|
|
|
// Is this animation currently in effect for the purposes of computing
|
|
|
|
// mWinsInCascade. (In general, this can be computed from the timing
|
|
|
|
// function. This boolean remembers the state as of the last time we
|
|
|
|
// called UpdateCascadeResults so we know if it changes and we need to
|
|
|
|
// call UpdateCascadeResults again.)
|
|
|
|
bool mInEffectForCascadeResults;
|
|
|
|
|
2014-10-19 21:55:46 -07:00
|
|
|
protected:
|
2015-06-08 19:13:53 -07:00
|
|
|
virtual ~CSSAnimation()
|
|
|
|
{
|
2015-06-08 19:13:54 -07:00
|
|
|
MOZ_ASSERT(!mOwningElement.IsSet(), "Owning element should be cleared "
|
|
|
|
"before a CSS animation is destroyed");
|
2015-06-08 19:13:53 -07:00
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual css::CommonAnimationManager* GetAnimationManager() const override;
|
2014-10-19 21:55:46 -07:00
|
|
|
|
2014-10-19 21:55:47 -07:00
|
|
|
static nsString PseudoTypeAsString(nsCSSPseudoElements::Type aPseudoType);
|
|
|
|
|
2015-06-30 23:19:04 -07:00
|
|
|
nsString mAnimationName;
|
|
|
|
|
2015-06-08 19:13:53 -07:00
|
|
|
// The (pseudo-)element whose computed animation-name refers to this
|
|
|
|
// animation (if any).
|
2015-06-08 19:13:54 -07:00
|
|
|
OwningElementRef mOwningElement;
|
2015-06-08 19:13:53 -07:00
|
|
|
|
2014-10-19 21:55:46 -07:00
|
|
|
// When combining animation-play-state with play() / pause() the following
|
|
|
|
// behavior applies:
|
|
|
|
// 1. pause() is sticky and always overrides the underlying
|
|
|
|
// animation-play-state
|
|
|
|
// 2. If animation-play-state is 'paused', play() will temporarily override
|
|
|
|
// it until animation-play-state next becomes 'running'.
|
|
|
|
// 3. Calls to play() trigger finishing behavior but setting the
|
|
|
|
// animation-play-state to 'running' does not.
|
|
|
|
//
|
|
|
|
// This leads to five distinct states:
|
|
|
|
//
|
|
|
|
// A. Running
|
|
|
|
// B. Running and temporarily overriding animation-play-state: paused
|
|
|
|
// C. Paused and sticky overriding animation-play-state: running
|
|
|
|
// D. Paused and sticky overriding animation-play-state: paused
|
|
|
|
// E. Paused by animation-play-state
|
|
|
|
//
|
|
|
|
// C and D may seem redundant but they differ in how to respond to the
|
|
|
|
// sequence: call play(), set animation-play-state: paused.
|
|
|
|
//
|
|
|
|
// C will transition to A then E leaving the animation paused.
|
|
|
|
// D will transition to B then B leaving the animation running.
|
|
|
|
//
|
|
|
|
// A state transition chart is as follows:
|
|
|
|
//
|
|
|
|
// A | B | C | D | E
|
|
|
|
// ---------------------------
|
|
|
|
// play() A | B | A | B | B
|
|
|
|
// pause() C | D | C | D | D
|
|
|
|
// 'running' A | A | C | C | A
|
|
|
|
// 'paused' E | B | D | D | E
|
|
|
|
//
|
2015-04-20 18:22:09 -07:00
|
|
|
// The base class, Animation already provides a boolean value,
|
2014-10-19 21:55:46 -07:00
|
|
|
// mIsPaused which gives us two states. To this we add a further two booleans
|
|
|
|
// to represent the states as follows.
|
|
|
|
//
|
|
|
|
// A. Running
|
|
|
|
// (!mIsPaused; !mIsStylePaused; !mPauseShouldStick)
|
|
|
|
// B. Running and temporarily overriding animation-play-state: paused
|
|
|
|
// (!mIsPaused; mIsStylePaused; !mPauseShouldStick)
|
|
|
|
// C. Paused and sticky overriding animation-play-state: running
|
|
|
|
// (mIsPaused; !mIsStylePaused; mPauseShouldStick)
|
|
|
|
// D. Paused and sticky overriding animation-play-state: paused
|
|
|
|
// (mIsPaused; mIsStylePaused; mPauseShouldStick)
|
|
|
|
// E. Paused by animation-play-state
|
|
|
|
// (mIsPaused; mIsStylePaused; !mPauseShouldStick)
|
|
|
|
//
|
|
|
|
// (That leaves 3 combinations of the boolean values that we never set because
|
|
|
|
// they don't represent valid states.)
|
|
|
|
bool mIsStylePaused;
|
|
|
|
bool mPauseShouldStick;
|
2014-10-19 21:55:47 -07:00
|
|
|
|
|
|
|
enum {
|
2015-02-13 07:58:33 -08:00
|
|
|
PREVIOUS_PHASE_BEFORE = uint64_t(-1),
|
|
|
|
PREVIOUS_PHASE_AFTER = uint64_t(-2)
|
2014-10-19 21:55:47 -07:00
|
|
|
};
|
2015-02-13 07:58:33 -08:00
|
|
|
// One of the PREVIOUS_PHASE_* constants, or an integer for the iteration
|
2014-10-19 21:55:47 -07:00
|
|
|
// whose start we last notified on.
|
2015-02-13 07:58:33 -08:00
|
|
|
uint64_t mPreviousPhaseOrIteration;
|
2014-10-19 21:55:46 -07:00
|
|
|
};
|
|
|
|
|
2015-06-29 18:00:39 -07:00
|
|
|
} /* namespace dom */
|
2014-10-19 21:55:46 -07:00
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class nsAnimationManager final
|
2013-06-25 01:58:46 -07:00
|
|
|
: public mozilla::css::CommonAnimationManager
|
2011-04-11 23:18:44 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-08-19 21:58:22 -07:00
|
|
|
explicit nsAnimationManager(nsPresContext *aPresContext)
|
2012-07-31 10:28:21 -07:00
|
|
|
: mozilla::css::CommonAnimationManager(aPresContext)
|
2011-04-11 23:18:44 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:22:10 -07:00
|
|
|
void UpdateStyleAndEvents(mozilla::AnimationCollection* aEA,
|
2014-06-22 13:23:59 -07:00
|
|
|
mozilla::TimeStamp aRefreshTime,
|
2014-06-19 20:39:25 -07:00
|
|
|
mozilla::EnsureStyleRuleFlags aFlags);
|
2015-04-20 18:22:10 -07:00
|
|
|
void QueueEvents(mozilla::AnimationCollection* aEA,
|
2014-10-19 21:55:47 -07:00
|
|
|
mozilla::EventArray &aEventsToDispatch);
|
2012-12-11 13:12:43 -08:00
|
|
|
|
2015-04-20 18:22:10 -07:00
|
|
|
void MaybeUpdateCascadeResults(mozilla::AnimationCollection* aCollection);
|
2015-03-31 15:05:54 -07:00
|
|
|
|
2011-04-11 23:18:44 -07:00
|
|
|
// nsIStyleRuleProcessor (parts)
|
2013-06-23 05:03:39 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
|
2015-03-21 09:28:04 -07:00
|
|
|
const MOZ_MUST_OVERRIDE override;
|
2013-06-23 05:03:39 -07:00
|
|
|
virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
|
2015-03-21 09:28:04 -07:00
|
|
|
const MOZ_MUST_OVERRIDE override;
|
2011-04-11 23:18:44 -07:00
|
|
|
|
|
|
|
// nsARefreshObserver
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual void WillRefresh(mozilla::TimeStamp aTime) override;
|
2011-04-11 23:18:44 -07:00
|
|
|
|
2012-12-11 13:12:43 -08:00
|
|
|
void FlushAnimations(FlushFlags aFlags);
|
|
|
|
|
2011-04-11 23:18:44 -07: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);
|
|
|
|
|
2011-04-11 23:18:44 -07: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-14 20:42:15 -08:00
|
|
|
void DispatchEvents() {
|
|
|
|
// Fast-path the common case: no events
|
|
|
|
if (!mPendingEvents.IsEmpty()) {
|
|
|
|
DoDispatchEvents();
|
|
|
|
}
|
|
|
|
}
|
2011-04-11 23:18:44 -07:00
|
|
|
|
2014-11-19 18:48:41 -08:00
|
|
|
protected:
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsIAtom* GetAnimationsAtom() override {
|
2014-11-19 18:48:41 -08:00
|
|
|
return nsGkAtoms::animationsProperty;
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsIAtom* GetAnimationsBeforeAtom() override {
|
2014-11-19 18:48:41 -08:00
|
|
|
return nsGkAtoms::animationsOfBeforeProperty;
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual nsIAtom* GetAnimationsAfterAtom() override {
|
2014-11-19 18:48:41 -08:00
|
|
|
return nsGkAtoms::animationsOfAfterProperty;
|
|
|
|
}
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool IsAnimationManager() override {
|
2014-11-19 18:48:41 -08:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-11 13:12:43 -08:00
|
|
|
|
|
|
|
private:
|
2011-04-11 23:18:44 -07:00
|
|
|
void BuildAnimations(nsStyleContext* aStyleContext,
|
2014-10-01 23:14:15 -07:00
|
|
|
mozilla::dom::Element* aTarget,
|
2015-04-27 19:29:13 -07:00
|
|
|
mozilla::dom::AnimationTimeline* aTimeline,
|
2015-04-20 18:22:10 -07:00
|
|
|
mozilla::AnimationPtrArray& aAnimations);
|
2014-04-02 22:57:28 -07:00
|
|
|
bool BuildSegment(InfallibleTArray<mozilla::AnimationPropertySegment>&
|
2014-04-02 22:57:28 -07:00
|
|
|
aSegments,
|
2014-06-26 16:57:11 -07:00
|
|
|
nsCSSProperty aProperty,
|
|
|
|
const mozilla::StyleAnimation& aAnimation,
|
2011-04-11 23:18:44 -07:00
|
|
|
float aFromKey, nsStyleContext* aFromContext,
|
|
|
|
mozilla::css::Declaration* aFromDeclaration,
|
2011-04-22 18:36:23 -07:00
|
|
|
float aToKey, nsStyleContext* aToContext);
|
2011-04-11 23:18:44 -07:00
|
|
|
|
2015-03-31 15:05:54 -07:00
|
|
|
static void UpdateCascadeResults(nsStyleContext* aStyleContext,
|
2015-04-20 18:22:10 -07:00
|
|
|
mozilla::AnimationCollection*
|
2015-03-31 15:05:54 -07:00
|
|
|
aElementAnimations);
|
|
|
|
|
2011-12-14 20:42:15 -08:00
|
|
|
// The guts of DispatchEvents
|
|
|
|
void DoDispatchEvents();
|
|
|
|
|
2014-10-19 21:55:46 -07:00
|
|
|
mozilla::EventArray mPendingEvents;
|
2011-04-11 23:18:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* !defined(nsAnimationManager_h_) */
|