2011-04-12 10:18:43 +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:43 +04:00
|
|
|
|
|
|
|
#ifndef mozilla_css_AnimationCommon_h
|
|
|
|
#define mozilla_css_AnimationCommon_h
|
|
|
|
|
2015-09-15 08:05:49 +03:00
|
|
|
#include <algorithm> // For <std::stable_sort>
|
2014-11-17 07:46:00 +03:00
|
|
|
#include "nsChangeHint.h"
|
2011-04-12 10:18:43 +04:00
|
|
|
#include "nsCSSProperty.h"
|
2014-11-17 07:46:00 +03:00
|
|
|
#include "nsDisplayList.h" // For nsDisplayItem::Type
|
2015-09-15 08:05:49 +03:00
|
|
|
#include "mozilla/AnimationComparator.h"
|
2015-07-29 04:57:39 +03:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2015-08-18 07:13:14 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-06-24 10:29:54 +04:00
|
|
|
#include "mozilla/StyleAnimationValue.h"
|
2015-04-21 04:22:09 +03:00
|
|
|
#include "mozilla/dom/Animation.h"
|
2011-04-12 10:18:43 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-07-16 04:02:32 +04:00
|
|
|
#include "mozilla/dom/Nullable.h"
|
2012-06-19 06:30:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-10-17 09:14:02 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-06-11 09:19:07 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2015-06-09 05:13:54 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-10-22 16:14:41 +04:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2014-07-16 04:02:30 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-10-23 00:55:14 +04:00
|
|
|
|
2013-10-22 14:30:45 +04:00
|
|
|
class nsIFrame;
|
2014-06-12 21:09:31 +04:00
|
|
|
class nsPresContext;
|
2011-04-12 10:18:43 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-06-24 10:29:54 +04:00
|
|
|
|
2015-04-21 04:22:10 +03:00
|
|
|
struct AnimationCollection;
|
2014-06-24 10:29:54 +04:00
|
|
|
|
2016-01-15 09:15:47 +03:00
|
|
|
class CommonAnimationManager {
|
2011-04-12 10:18:43 +04:00
|
|
|
public:
|
2014-08-20 08:58:22 +04:00
|
|
|
explicit CommonAnimationManager(nsPresContext *aPresContext);
|
2011-04-12 10:18:43 +04:00
|
|
|
|
2015-04-01 01:05:54 +03:00
|
|
|
// NOTE: This can return null after Disconnect().
|
|
|
|
nsPresContext* PresContext() const { return mPresContext; }
|
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
/**
|
|
|
|
* Notify the manager that the pres context is going away.
|
|
|
|
*/
|
|
|
|
void Disconnect();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool ExtractComputedValueForTransition(
|
2011-04-12 10:18:43 +04:00
|
|
|
nsCSSProperty aProperty,
|
|
|
|
nsStyleContext* aStyleContext,
|
2015-07-29 04:57:39 +03:00
|
|
|
StyleAnimationValue& aComputedValue);
|
2014-11-17 07:45:56 +03:00
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
protected:
|
2014-06-24 02:40:01 +04:00
|
|
|
virtual ~CommonAnimationManager();
|
|
|
|
|
2015-04-21 04:22:10 +03:00
|
|
|
void AddElementCollection(AnimationCollection* aCollection);
|
2014-06-27 03:57:13 +04:00
|
|
|
void RemoveAllElementCollections();
|
2011-04-12 10:18:43 +04:00
|
|
|
|
2014-11-20 05:48:41 +03:00
|
|
|
virtual nsIAtom* GetAnimationsAtom() = 0;
|
|
|
|
virtual nsIAtom* GetAnimationsBeforeAtom() = 0;
|
|
|
|
virtual nsIAtom* GetAnimationsAfterAtom() = 0;
|
|
|
|
|
2015-07-01 18:55:51 +03:00
|
|
|
public:
|
2015-12-04 02:34:17 +03:00
|
|
|
// Get (and optionally create) the collection of animations managed
|
|
|
|
// by this class for the given |aElement| and |aPseudoType|.
|
|
|
|
AnimationCollection*
|
|
|
|
GetAnimationCollection(dom::Element *aElement,
|
2016-02-17 23:37:00 +03:00
|
|
|
CSSPseudoElementType aPseudoType,
|
2015-12-04 02:34:17 +03:00
|
|
|
bool aCreateIfNeeded);
|
|
|
|
|
|
|
|
// Given the frame |aFrame| with possibly animated content, finds its
|
2015-07-01 21:43:13 +03:00
|
|
|
// associated collection of animations. If it is a generated content
|
|
|
|
// frame, it may examine the parent frame to search for such animations.
|
|
|
|
AnimationCollection*
|
|
|
|
GetAnimationCollection(const nsIFrame* aFrame);
|
|
|
|
|
2015-07-01 18:55:51 +03:00
|
|
|
protected:
|
2015-08-18 07:13:14 +03:00
|
|
|
LinkedList<AnimationCollection> mElementCollections;
|
2011-04-12 10:18:43 +04:00
|
|
|
nsPresContext *mPresContext; // weak (non-null from ctor to Disconnect)
|
|
|
|
};
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
typedef InfallibleTArray<RefPtr<dom::Animation>> AnimationPtrArray;
|
2014-05-15 03:38:37 +04:00
|
|
|
|
2015-08-18 07:13:14 +03:00
|
|
|
struct AnimationCollection : public LinkedListElement<AnimationCollection>
|
2011-04-12 10:18:43 +04:00
|
|
|
{
|
2015-04-21 04:22:10 +03:00
|
|
|
AnimationCollection(dom::Element *aElement, nsIAtom *aElementProperty,
|
2015-07-29 04:57:39 +03:00
|
|
|
CommonAnimationManager *aManager)
|
2011-04-12 10:18:43 +04:00
|
|
|
: mElement(aElement)
|
|
|
|
, mElementProperty(aElementProperty)
|
|
|
|
, mManager(aManager)
|
2015-02-17 01:15:03 +03:00
|
|
|
, mCheckGeneration(0)
|
2011-05-01 02:16:19 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mCalledPropertyDtor(false)
|
|
|
|
#endif
|
2011-04-12 10:18:43 +04:00
|
|
|
{
|
2015-04-21 04:22:10 +03:00
|
|
|
MOZ_COUNT_CTOR(AnimationCollection);
|
2011-04-12 10:18:43 +04:00
|
|
|
}
|
2015-04-21 04:22:10 +03:00
|
|
|
~AnimationCollection()
|
2011-04-12 10:18:43 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mCalledPropertyDtor,
|
|
|
|
"must call destructor through element property dtor");
|
2015-04-21 04:22:10 +03:00
|
|
|
MOZ_COUNT_DTOR(AnimationCollection);
|
2015-08-18 07:13:14 +03:00
|
|
|
remove();
|
2011-04-12 10:18:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy()
|
|
|
|
{
|
|
|
|
// This will call our destructor.
|
|
|
|
mElement->DeleteProperty(mElementProperty);
|
|
|
|
}
|
|
|
|
|
2014-06-24 10:29:53 +04:00
|
|
|
static void PropertyDtor(void *aObject, nsIAtom *aPropertyName,
|
|
|
|
void *aPropertyValue, void *aData);
|
|
|
|
|
2015-05-08 16:56:36 +03:00
|
|
|
public:
|
2014-06-23 09:10:18 +04:00
|
|
|
bool IsForElement() const { // rather than for a pseudo-element
|
|
|
|
return mElementProperty == nsGkAtoms::animationsProperty ||
|
|
|
|
mElementProperty == nsGkAtoms::transitionsProperty;
|
|
|
|
}
|
|
|
|
|
2014-08-25 08:48:22 +04:00
|
|
|
bool IsForBeforePseudo() const {
|
|
|
|
return mElementProperty == nsGkAtoms::animationsOfBeforeProperty ||
|
|
|
|
mElementProperty == nsGkAtoms::transitionsOfBeforeProperty;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsForAfterPseudo() const {
|
|
|
|
return mElementProperty == nsGkAtoms::animationsOfAfterProperty ||
|
|
|
|
mElementProperty == nsGkAtoms::transitionsOfAfterProperty;
|
|
|
|
}
|
|
|
|
|
2016-02-17 23:37:00 +03:00
|
|
|
CSSPseudoElementType PseudoElementType() const
|
2015-03-20 07:10:00 +03:00
|
|
|
{
|
|
|
|
if (IsForElement()) {
|
2016-02-17 01:07:00 +03:00
|
|
|
return CSSPseudoElementType::NotPseudo;
|
2015-03-20 07:10:00 +03:00
|
|
|
}
|
|
|
|
if (IsForBeforePseudo()) {
|
2016-02-17 01:07:00 +03:00
|
|
|
return CSSPseudoElementType::before;
|
2015-03-20 07:10:00 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(IsForAfterPseudo(),
|
|
|
|
"::before & ::after should be the only pseudo-elements here");
|
2016-02-17 01:07:00 +03:00
|
|
|
return CSSPseudoElementType::after;
|
2015-03-20 07:10:00 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 23:37:00 +03:00
|
|
|
static nsString PseudoTypeAsString(CSSPseudoElementType aPseudoType);
|
2015-07-29 04:57:40 +03:00
|
|
|
|
2011-04-12 10:18:43 +04:00
|
|
|
dom::Element *mElement;
|
|
|
|
|
|
|
|
// the atom we use in mElement's prop table (must be a static atom,
|
|
|
|
// i.e., in an atom list)
|
|
|
|
nsIAtom *mElementProperty;
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
CommonAnimationManager *mManager;
|
2011-05-01 02:16:19 +04:00
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
AnimationPtrArray mAnimations;
|
2014-05-15 03:38:37 +04:00
|
|
|
|
2016-01-06 05:04:05 +03:00
|
|
|
// For CSS transitions only, we record the most recent generation
|
2015-02-17 01:15:03 +03:00
|
|
|
// for which we've done the transition update, so that we avoid doing
|
2016-01-06 05:04:05 +03:00
|
|
|
// it more than once per style change.
|
|
|
|
// (Note that we also store an animation generation on each EffectSet in
|
|
|
|
// order to track when we need to update animations on layers.)
|
2015-02-17 01:15:03 +03:00
|
|
|
uint64_t mCheckGeneration;
|
2016-01-06 05:04:05 +03:00
|
|
|
// Update mCheckGeneration to RestyleManager's count
|
2015-02-17 01:15:03 +03:00
|
|
|
void UpdateCheckGeneration(nsPresContext* aPresContext);
|
|
|
|
|
2015-08-17 07:59:44 +03:00
|
|
|
private:
|
2011-05-01 02:16:19 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool mCalledPropertyDtor;
|
|
|
|
#endif
|
2011-04-12 10:18:43 +04:00
|
|
|
};
|
|
|
|
|
2015-06-09 05:13:54 +03:00
|
|
|
/**
|
|
|
|
* Utility class for referencing the element that created a CSS animation or
|
|
|
|
* transition. It is non-owning (i.e. it uses a raw pointer) since it is only
|
|
|
|
* expected to be set by the owned animation while it actually being managed
|
|
|
|
* by the owning element.
|
|
|
|
*
|
|
|
|
* This class also abstracts the comparison of an element/pseudo-class pair
|
|
|
|
* for the sake of composite ordering since this logic is common to both CSS
|
|
|
|
* animations and transitions.
|
|
|
|
*
|
|
|
|
* (We call this OwningElementRef instead of just OwningElement so that we can
|
|
|
|
* call the getter on CSSAnimation/CSSTransition OwningElement() without
|
|
|
|
* clashing with this object's contructor.)
|
|
|
|
*/
|
|
|
|
class OwningElementRef final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OwningElementRef()
|
|
|
|
: mElement(nullptr)
|
2016-02-17 01:07:00 +03:00
|
|
|
, mPseudoType(CSSPseudoElementType::NotPseudo)
|
2015-06-09 05:13:54 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
OwningElementRef(dom::Element& aElement,
|
2016-02-17 23:37:00 +03:00
|
|
|
CSSPseudoElementType aPseudoType)
|
2015-06-09 05:13:54 +03:00
|
|
|
: mElement(&aElement)
|
|
|
|
, mPseudoType(aPseudoType)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool Equals(const OwningElementRef& aOther) const
|
|
|
|
{
|
|
|
|
return mElement == aOther.mElement &&
|
|
|
|
mPseudoType == aOther.mPseudoType;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LessThan(const OwningElementRef& aOther) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mElement && aOther.mElement,
|
|
|
|
"Elements to compare should not be null");
|
|
|
|
|
|
|
|
if (mElement != aOther.mElement) {
|
|
|
|
return nsContentUtils::PositionIsBefore(mElement, aOther.mElement);
|
|
|
|
}
|
|
|
|
|
2016-02-17 01:07:00 +03:00
|
|
|
return mPseudoType == CSSPseudoElementType::NotPseudo ||
|
|
|
|
(mPseudoType == CSSPseudoElementType::before &&
|
|
|
|
aOther.mPseudoType == CSSPseudoElementType::after);
|
2015-06-09 05:13:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsSet() const { return !!mElement; }
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
void GetElement(dom::Element*& aElement,
|
2016-02-17 23:37:00 +03:00
|
|
|
CSSPseudoElementType& aPseudoType) const {
|
2015-07-29 04:57:39 +03:00
|
|
|
aElement = mElement;
|
|
|
|
aPseudoType = mPseudoType;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* GetRenderedPresContext() const;
|
|
|
|
|
2015-06-09 05:13:54 +03:00
|
|
|
private:
|
|
|
|
dom::Element* MOZ_NON_OWNING_REF mElement;
|
2016-02-17 23:37:00 +03:00
|
|
|
CSSPseudoElementType mPseudoType;
|
2015-06-09 05:13:54 +03:00
|
|
|
};
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
template <class EventInfo>
|
|
|
|
class DelayedEventDispatcher
|
|
|
|
{
|
|
|
|
public:
|
2015-09-15 08:05:49 +03:00
|
|
|
DelayedEventDispatcher() : mIsSorted(true) { }
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
void QueueEvent(EventInfo&& aEventInfo)
|
|
|
|
{
|
2015-09-15 08:05:49 +03:00
|
|
|
mPendingEvents.AppendElement(Forward<EventInfo>(aEventInfo));
|
|
|
|
mIsSorted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is exposed as a separate method so that when we are dispatching
|
|
|
|
// *both* transition events and animation events we can sort both lists
|
|
|
|
// once using the current state of the document before beginning any
|
|
|
|
// dispatch.
|
|
|
|
void SortEvents()
|
|
|
|
{
|
|
|
|
if (mIsSorted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Replace with mPendingEvents.StableSort when bug 1147091 is
|
|
|
|
// fixed.
|
|
|
|
std::stable_sort(mPendingEvents.begin(), mPendingEvents.end(),
|
|
|
|
EventInfoLessThan());
|
|
|
|
mIsSorted = true;
|
2015-07-29 04:57:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes a reference to the owning manager's pres context so it can
|
|
|
|
// detect if the pres context is destroyed while dispatching one of
|
|
|
|
// the events.
|
2015-09-15 08:05:49 +03:00
|
|
|
//
|
|
|
|
// This will call SortEvents automatically if it has not already been
|
|
|
|
// called.
|
2015-07-29 04:57:39 +03:00
|
|
|
void DispatchEvents(nsPresContext* const & aPresContext)
|
|
|
|
{
|
|
|
|
if (!aPresContext || mPendingEvents.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:05:49 +03:00
|
|
|
SortEvents();
|
|
|
|
|
2015-07-29 04:57:39 +03:00
|
|
|
EventArray events;
|
|
|
|
mPendingEvents.SwapElements(events);
|
2015-09-15 08:05:49 +03:00
|
|
|
// mIsSorted will be set to true by SortEvents above, and we leave it
|
|
|
|
// that way since mPendingEvents is now empty
|
2015-07-29 04:57:39 +03:00
|
|
|
for (EventInfo& info : events) {
|
|
|
|
EventDispatcher::Dispatch(info.mElement, aPresContext, &info.mEvent);
|
|
|
|
|
|
|
|
if (!aPresContext) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 08:05:49 +03:00
|
|
|
void ClearEventQueue()
|
|
|
|
{
|
|
|
|
mPendingEvents.Clear();
|
|
|
|
mIsSorted = true;
|
|
|
|
}
|
2015-07-29 04:57:39 +03:00
|
|
|
bool HasQueuedEvents() const { return !mPendingEvents.IsEmpty(); }
|
|
|
|
|
|
|
|
// Methods for supporting cycle-collection
|
|
|
|
void Traverse(nsCycleCollectionTraversalCallback* aCallback,
|
|
|
|
const char* aName)
|
|
|
|
{
|
|
|
|
for (EventInfo& info : mPendingEvents) {
|
|
|
|
ImplCycleCollectionTraverse(*aCallback, info.mElement, aName);
|
2015-09-15 08:05:44 +03:00
|
|
|
ImplCycleCollectionTraverse(*aCallback, info.mAnimation, aName);
|
2015-07-29 04:57:39 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-15 08:05:49 +03:00
|
|
|
void Unlink() { ClearEventQueue(); }
|
2015-07-29 04:57:39 +03:00
|
|
|
|
|
|
|
protected:
|
2015-09-15 08:05:49 +03:00
|
|
|
class EventInfoLessThan
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const EventInfo& a, const EventInfo& b) const
|
|
|
|
{
|
|
|
|
if (a.mTimeStamp != b.mTimeStamp) {
|
|
|
|
// Null timestamps sort first
|
|
|
|
if (a.mTimeStamp.IsNull() || b.mTimeStamp.IsNull()) {
|
|
|
|
return a.mTimeStamp.IsNull();
|
|
|
|
} else {
|
|
|
|
return a.mTimeStamp < b.mTimeStamp;
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 04:57:39 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
AnimationPtrComparator<RefPtr<dom::Animation>> comparator;
|
2015-09-15 08:05:49 +03:00
|
|
|
return comparator.LessThan(a.mAnimation, b.mAnimation);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef nsTArray<EventInfo> EventArray;
|
2015-07-29 04:57:39 +03:00
|
|
|
EventArray mPendingEvents;
|
2015-09-15 08:05:49 +03:00
|
|
|
bool mIsSorted;
|
2015-07-29 04:57:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class EventInfo>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(DelayedEventDispatcher<EventInfo>& aField)
|
|
|
|
{
|
|
|
|
aField.Unlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class EventInfo>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
DelayedEventDispatcher<EventInfo>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
aField.Traverse(&aCallback, aName);
|
|
|
|
}
|
|
|
|
|
2015-06-09 05:13:54 +03:00
|
|
|
} // namespace mozilla
|
2011-04-12 10:18:43 +04:00
|
|
|
|
|
|
|
#endif /* !defined(mozilla_css_AnimationCommon_h) */
|