2009-01-14 20:38:07 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
2009-01-14 20:38:07 -08:00
|
|
|
|
|
|
|
#ifndef NS_SMILANIMATIONCONTROLLER_H_
|
|
|
|
#define NS_SMILANIMATIONCONTROLLER_H_
|
|
|
|
|
2013-05-29 13:43:41 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2009-01-14 20:38:07 -08:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsSMILTimeContainer.h"
|
|
|
|
#include "nsSMILCompositorTable.h"
|
2010-01-12 12:00:49 -08:00
|
|
|
#include "nsSMILMilestone.h"
|
2010-03-18 21:14:40 -07:00
|
|
|
#include "nsRefreshDriver.h"
|
2009-01-14 20:38:07 -08:00
|
|
|
|
2010-02-20 13:13:11 -08:00
|
|
|
struct nsSMILTargetIdentifier;
|
2009-01-14 20:38:07 -08:00
|
|
|
class nsIDocument;
|
|
|
|
|
2013-03-18 20:18:45 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class SVGAnimationElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsSMILAnimationController
|
2009-07-15 11:33:31 -07:00
|
|
|
//
|
2009-01-14 20:38:07 -08:00
|
|
|
// The animation controller maintains the animation timer and determines the
|
|
|
|
// sample times and sample rate for all SMIL animations in a document. There is
|
|
|
|
// at most one animation controller per nsDocument so that frame-rate tuning can
|
|
|
|
// be performed at a document-level.
|
|
|
|
//
|
|
|
|
// The animation controller can contain many child time containers (timed
|
|
|
|
// document root objects) which may correspond to SVG document fragments within
|
|
|
|
// a compound document. These time containers can be paused individually or
|
|
|
|
// here, at the document level.
|
|
|
|
//
|
2010-03-18 21:14:40 -07:00
|
|
|
class nsSMILAnimationController : public nsSMILTimeContainer,
|
|
|
|
public nsARefreshObserver
|
2009-01-14 20:38:07 -08:00
|
|
|
{
|
2010-09-17 20:46:48 -07:00
|
|
|
public:
|
2011-04-28 12:02:20 -07:00
|
|
|
nsSMILAnimationController(nsIDocument* aDoc);
|
2009-01-14 20:38:07 -08:00
|
|
|
~nsSMILAnimationController();
|
|
|
|
|
2011-04-28 12:02:20 -07:00
|
|
|
// Clears mDocument pointer. (Called by our nsIDocument when it's going away)
|
|
|
|
void Disconnect();
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
// nsSMILContainer
|
2013-05-29 13:43:41 -07:00
|
|
|
virtual void Pause(uint32_t aType) MOZ_OVERRIDE;
|
|
|
|
virtual void Resume(uint32_t aType) MOZ_OVERRIDE;
|
|
|
|
virtual nsSMILTime GetParentTime() const MOZ_OVERRIDE;
|
2009-07-15 11:33:31 -07:00
|
|
|
|
2010-03-18 21:14:40 -07:00
|
|
|
// nsARefreshObserver
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef() MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release() MOZ_OVERRIDE;
|
2010-03-18 21:14:40 -07:00
|
|
|
|
2013-05-29 13:43:41 -07:00
|
|
|
virtual void WillRefresh(mozilla::TimeStamp aTime) MOZ_OVERRIDE;
|
2010-03-18 21:14:40 -07:00
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
// Methods for registering and enumerating animation elements
|
2013-03-18 20:18:45 -07:00
|
|
|
void RegisterAnimationElement(mozilla::dom::SVGAnimationElement* aAnimationElement);
|
|
|
|
void UnregisterAnimationElement(mozilla::dom::SVGAnimationElement* aAnimationElement);
|
2009-01-14 20:38:07 -08:00
|
|
|
|
2009-01-21 17:00:27 -08:00
|
|
|
// Methods for resampling all animations
|
|
|
|
// (A resample performs the same operations as a sample but doesn't advance
|
|
|
|
// the current time and doesn't check if the container is paused)
|
2012-03-20 11:02:38 -07:00
|
|
|
// This will flush pending style changes for the document.
|
2011-10-17 07:59:28 -07:00
|
|
|
void Resample() { DoSample(false); }
|
2011-12-14 20:42:15 -08:00
|
|
|
|
2010-11-09 15:21:03 -08:00
|
|
|
void SetResampleNeeded()
|
|
|
|
{
|
|
|
|
if (!mRunningSample) {
|
2011-12-14 20:42:15 -08:00
|
|
|
if (!mResampleNeeded) {
|
|
|
|
FlagDocumentNeedsFlush();
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mResampleNeeded = true;
|
2010-11-09 15:21:03 -08:00
|
|
|
}
|
|
|
|
}
|
2012-03-20 11:02:38 -07:00
|
|
|
|
|
|
|
// This will flush pending style changes for the document.
|
2009-01-21 17:00:27 -08:00
|
|
|
void FlushResampleRequests()
|
|
|
|
{
|
|
|
|
if (!mResampleNeeded)
|
|
|
|
return;
|
|
|
|
|
2010-01-12 12:00:49 -08:00
|
|
|
Resample();
|
2009-01-21 17:00:27 -08:00
|
|
|
}
|
2009-01-14 20:38:07 -08:00
|
|
|
|
|
|
|
// Methods for handling page transitions
|
2009-01-21 17:00:27 -08:00
|
|
|
void OnPageShow();
|
|
|
|
void OnPageHide();
|
2009-01-14 20:38:07 -08:00
|
|
|
|
|
|
|
// Methods for supporting cycle-collection
|
2009-01-21 17:00:27 -08:00
|
|
|
void Traverse(nsCycleCollectionTraversalCallback* aCallback);
|
|
|
|
void Unlink();
|
2009-01-14 20:38:07 -08:00
|
|
|
|
2010-10-19 16:53:52 -07:00
|
|
|
// Methods for relaying the availability of the refresh driver
|
|
|
|
void NotifyRefreshDriverCreated(nsRefreshDriver* aRefreshDriver);
|
|
|
|
void NotifyRefreshDriverDestroying(nsRefreshDriver* aRefreshDriver);
|
2010-03-18 21:14:40 -07:00
|
|
|
|
2010-09-08 13:40:39 -07:00
|
|
|
// Helper to check if we have any animation elements at all
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasRegisteredAnimations()
|
2010-09-08 13:40:39 -07:00
|
|
|
{ return mAnimationElementTable.Count() != 0; }
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
protected:
|
|
|
|
// Typedefs
|
|
|
|
typedef nsPtrHashKey<nsSMILTimeContainer> TimeContainerPtrKey;
|
|
|
|
typedef nsTHashtable<TimeContainerPtrKey> TimeContainerHashtable;
|
2013-03-18 20:18:45 -07:00
|
|
|
typedef nsPtrHashKey<mozilla::dom::SVGAnimationElement> AnimationElementPtrKey;
|
2009-01-14 20:38:07 -08:00
|
|
|
typedef nsTHashtable<AnimationElementPtrKey> AnimationElementHashtable;
|
|
|
|
|
2009-01-21 17:00:27 -08:00
|
|
|
struct SampleTimeContainerParams
|
|
|
|
{
|
|
|
|
TimeContainerHashtable* mActiveContainers;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mSkipUnchangedContainers;
|
2009-01-14 20:38:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SampleAnimationParams
|
|
|
|
{
|
|
|
|
TimeContainerHashtable* mActiveContainers;
|
|
|
|
nsSMILCompositorTable* mCompositorTable;
|
|
|
|
};
|
2009-07-15 11:33:31 -07:00
|
|
|
|
2010-01-12 12:00:49 -08:00
|
|
|
struct GetMilestoneElementsParams
|
|
|
|
{
|
2013-03-18 20:18:45 -07:00
|
|
|
nsTArray<nsRefPtr<mozilla::dom::SVGAnimationElement> > mElements;
|
|
|
|
nsSMILMilestone mMilestone;
|
2010-01-12 12:00:49 -08:00
|
|
|
};
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
// Cycle-collection implementation helpers
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator CompositorTableEntryTraverse(
|
2009-01-14 20:38:07 -08:00
|
|
|
nsSMILCompositor* aCompositor, void* aArg);
|
|
|
|
|
2011-04-28 12:05:24 -07:00
|
|
|
// Returns mDocument's refresh driver, if it's got one.
|
|
|
|
nsRefreshDriver* GetRefreshDriver();
|
|
|
|
|
2010-10-19 16:53:52 -07:00
|
|
|
// Methods for controlling whether we're sampling
|
|
|
|
void StartSampling(nsRefreshDriver* aRefreshDriver);
|
|
|
|
void StopSampling(nsRefreshDriver* aRefreshDriver);
|
|
|
|
|
2011-04-01 13:33:20 -07:00
|
|
|
// Wrapper for StartSampling that defers if no animations are registered.
|
|
|
|
void MaybeStartSampling(nsRefreshDriver* aRefreshDriver);
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
// Sample-related callbacks and implementation helpers
|
2013-05-29 13:43:41 -07:00
|
|
|
virtual void DoSample() MOZ_OVERRIDE;
|
2011-09-28 23:19:26 -07:00
|
|
|
void DoSample(bool aSkipUnchangedContainers);
|
2010-07-02 22:52:51 -07:00
|
|
|
|
|
|
|
void RewindElements();
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator RewindNeeded(
|
2010-07-02 22:52:51 -07:00
|
|
|
TimeContainerPtrKey* aKey, void* aData);
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator RewindAnimation(
|
2010-07-02 22:52:51 -07:00
|
|
|
AnimationElementPtrKey* aKey, void* aData);
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator ClearRewindNeeded(
|
2010-07-02 22:52:51 -07:00
|
|
|
TimeContainerPtrKey* aKey, void* aData);
|
|
|
|
|
2010-01-12 12:00:49 -08:00
|
|
|
void DoMilestoneSamples();
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator GetNextMilestone(
|
2010-01-12 12:00:49 -08:00
|
|
|
TimeContainerPtrKey* aKey, void* aData);
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator GetMilestoneElements(
|
2010-01-12 12:00:49 -08:00
|
|
|
TimeContainerPtrKey* aKey, void* aData);
|
2010-07-02 22:52:51 -07:00
|
|
|
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator SampleTimeContainer(
|
2009-01-14 20:38:07 -08:00
|
|
|
TimeContainerPtrKey* aKey, void* aData);
|
2012-10-08 09:15:12 -07:00
|
|
|
static PLDHashOperator SampleAnimation(
|
2009-01-14 20:38:07 -08:00
|
|
|
AnimationElementPtrKey* aKey, void* aData);
|
2013-03-18 20:18:45 -07:00
|
|
|
static void SampleTimedElement(mozilla::dom::SVGAnimationElement* aElement,
|
2009-01-14 20:38:07 -08:00
|
|
|
TimeContainerHashtable* aActiveContainers);
|
|
|
|
static void AddAnimationToCompositorTable(
|
2013-03-18 20:18:45 -07:00
|
|
|
mozilla::dom::SVGAnimationElement* aElement, nsSMILCompositorTable* aCompositorTable);
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool GetTargetIdentifierForAnimation(
|
2013-03-18 20:18:45 -07:00
|
|
|
mozilla::dom::SVGAnimationElement* aAnimElem, nsSMILTargetIdentifier& aResult);
|
2009-01-14 20:38:07 -08:00
|
|
|
|
|
|
|
// Methods for adding/removing time containers
|
2013-05-29 13:43:41 -07:00
|
|
|
virtual nsresult AddChild(nsSMILTimeContainer& aChild) MOZ_OVERRIDE;
|
|
|
|
virtual void RemoveChild(nsSMILTimeContainer& aChild) MOZ_OVERRIDE;
|
2009-01-14 20:38:07 -08:00
|
|
|
|
2011-12-14 20:42:15 -08:00
|
|
|
void FlagDocumentNeedsFlush();
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
// Members
|
2010-03-18 21:14:40 -07:00
|
|
|
nsAutoRefCnt mRefCnt;
|
|
|
|
NS_DECL_OWNINGTHREAD
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
AnimationElementHashtable mAnimationElementTable;
|
|
|
|
TimeContainerHashtable mChildContainerTable;
|
2010-09-17 20:46:48 -07:00
|
|
|
mozilla::TimeStamp mCurrentSampleTime;
|
|
|
|
mozilla::TimeStamp mStartTime;
|
2010-11-23 15:31:17 -08:00
|
|
|
|
|
|
|
// Average time between samples from the refresh driver. This is used to
|
|
|
|
// detect large unexpected gaps between samples such as can occur when the
|
|
|
|
// computer sleeps. The nature of the SMIL model means that catching up these
|
|
|
|
// large gaps can be expensive as, for example, many events may need to be
|
|
|
|
// dispatched for the intervening time when no samples were received.
|
|
|
|
//
|
|
|
|
// In such cases, we ignore the intervening gap and continue sampling from
|
|
|
|
// when we were expecting the next sample to arrive.
|
|
|
|
//
|
|
|
|
// Note that we only do this for SMIL and not CSS transitions (which doesn't
|
|
|
|
// have so much work to do to catch up) nor scripted animations (which expect
|
|
|
|
// animation time to follow real time).
|
|
|
|
//
|
|
|
|
// This behaviour does not affect pausing (since we're not *expecting* any
|
|
|
|
// samples then) nor seeking (where the SMIL model behaves somewhat
|
|
|
|
// differently such as not dispatching events).
|
|
|
|
nsSMILTime mAvgTimeBetweenSamples;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mResampleNeeded;
|
2010-10-19 16:52:49 -07:00
|
|
|
// If we're told to start sampling but there are no animation elements we just
|
|
|
|
// record the time, set the following flag, and then wait until we have an
|
|
|
|
// animation element. Then we'll reset this flag and actually start sampling.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDeferredStartSampling;
|
|
|
|
bool mRunningSample;
|
2009-07-15 11:33:31 -07:00
|
|
|
|
2014-05-20 05:34:26 -07:00
|
|
|
// Are we registered with our document's refresh driver?
|
|
|
|
bool mRegisteredWithRefreshDriver;
|
|
|
|
|
2009-01-14 20:38:07 -08:00
|
|
|
// Store raw ptr to mDocument. It owns the controller, so controller
|
|
|
|
// shouldn't outlive it
|
|
|
|
nsIDocument* mDocument;
|
|
|
|
|
|
|
|
// Contains compositors used in our last sample. We keep this around
|
|
|
|
// so we can detect when an element/attribute used to be animated,
|
2009-07-15 11:33:31 -07:00
|
|
|
// but isn't anymore for some reason. (e.g. if its <animate> element is
|
2009-01-14 20:38:07 -08:00
|
|
|
// removed or retargeted)
|
|
|
|
nsAutoPtr<nsSMILCompositorTable> mLastCompositorTable;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // NS_SMILANIMATIONCONTROLLER_H_
|