Bug 541588: Hook up nsSMILAnimationController to nsRefreshDriver for scheduling samples. r=roc

This commit is contained in:
Daniel Holbert 2010-03-17 01:54:20 -07:00
Родитель a836d78ee8
Коммит 11bb4d8513
4 изменённых файлов: 86 добавлений и 60 удалений

Просмотреть файл

@ -1360,7 +1360,7 @@ protected:
#ifdef MOZ_SMIL
// SMIL Animation Controller, lazily-initialized in GetAnimationController
nsAutoPtr<nsSMILAnimationController> mAnimationController;
nsRefPtr<nsSMILAnimationController> mAnimationController;
#endif // MOZ_SMIL
// Table of element properties for this document.

Просмотреть файл

@ -51,16 +51,19 @@
//----------------------------------------------------------------------
// nsSMILAnimationController implementation
// In my testing the minimum needed for smooth animation is 36 frames per
// second which seems like a lot (Flash traditionally uses 14fps).
//
// Redrawing is synchronous. This is deliberate so that later we can tune the
// timer based on how long the callback takes. To achieve 36fps we'd need 28ms
// between frames. For now we set the timer interval to be a little less than
// this (to allow for the render itself) and then let performance decay as the
// image gets more complicated and render times increase.
//
const PRUint32 nsSMILAnimationController::kTimerInterval = 22;
// Helper method
static nsRefreshDriver*
GetRefreshDriverForDoc(nsIDocument* aDoc)
{
nsIPresShell* shell = aDoc->GetPrimaryShell();
if (!shell) {
return nsnull;
}
nsPresContext* context = shell->GetPresContext();
return context ? context->RefreshDriver() : nsnull;
}
//----------------------------------------------------------------------
// ctors, dtors, factory methods
@ -75,11 +78,7 @@ nsSMILAnimationController::nsSMILAnimationController()
nsSMILAnimationController::~nsSMILAnimationController()
{
if (mTimer) {
mTimer->Cancel();
mTimer = nsnull;
}
StopSampling(GetRefreshDriverForDoc(mDocument));
NS_ASSERTION(mAnimationElementTable.Count() == 0,
"Animation controller shouldn't be tracking any animation"
" elements when it dies");
@ -105,9 +104,6 @@ nsSMILAnimationController::Init(nsIDocument* aDoc)
{
NS_ENSURE_ARG_POINTER(aDoc);
mTimer = do_CreateInstance("@mozilla.org/timer;1");
NS_ENSURE_TRUE(mTimer, NS_ERROR_OUT_OF_MEMORY);
// Keep track of document, so we can traverse its set of animation elements
mDocument = aDoc;
@ -125,7 +121,7 @@ nsSMILAnimationController::Pause(PRUint32 aType)
nsSMILTimeContainer::Pause(aType);
if (mPauseState) {
StopTimer();
StopSampling(GetRefreshDriverForDoc(mDocument));
}
}
@ -137,7 +133,8 @@ nsSMILAnimationController::Resume(PRUint32 aType)
nsSMILTimeContainer::Resume(aType);
if (wasPaused && !mPauseState && mChildContainerTable.Count()) {
StartTimer();
Sample(); // Run the first sample manually
StartSampling(GetRefreshDriverForDoc(mDocument));
}
}
@ -148,6 +145,20 @@ nsSMILAnimationController::GetParentTime() const
return PR_Now() / PR_USEC_PER_MSEC;
}
//----------------------------------------------------------------------
// nsARefreshObserver methods:
NS_IMPL_ADDREF(nsSMILAnimationController)
NS_IMPL_RELEASE(nsSMILAnimationController)
void
nsSMILAnimationController::WillRefresh(mozilla::TimeStamp aTime)
{
// XXXdholbert Eventually we should be sampling based on aTime. For now,
// though, we keep track of the time on our own, and we just use
// nsRefreshDriver for scheduling samples.
Sample();
}
//----------------------------------------------------------------------
// Animation element registration methods:
@ -214,42 +225,29 @@ nsSMILAnimationController::Unlink()
//----------------------------------------------------------------------
// Timer-related implementation helpers
/*static*/ void
nsSMILAnimationController::Notify(nsITimer* timer, void* aClosure)
void
nsSMILAnimationController::StartSampling(nsRefreshDriver* aRefreshDriver)
{
nsSMILAnimationController* controller = (nsSMILAnimationController*)aClosure;
NS_ASSERTION(controller->mTimer == timer,
"nsSMILAnimationController::Notify called with incorrect timer");
controller->Sample();
NS_ASSERTION(mPauseState == 0, "Starting sampling but controller is paused");
if (aRefreshDriver) {
NS_ABORT_IF_FALSE(!GetRefreshDriverForDoc(mDocument) ||
aRefreshDriver == GetRefreshDriverForDoc(mDocument),
"Starting sampling with wrong refresh driver");
aRefreshDriver->AddRefreshObserver(this, Flush_Style);
}
}
nsresult
nsSMILAnimationController::StartTimer()
void
nsSMILAnimationController::StopSampling(nsRefreshDriver* aRefreshDriver)
{
NS_ENSURE_TRUE(mTimer, NS_ERROR_FAILURE);
NS_ASSERTION(mPauseState == 0, "Starting timer but controller is paused");
// Run the first sample manually
Sample();
//
// XXX Make this self-tuning. Sounds like control theory to me and not
// something I'm familiar with.
//
return mTimer->InitWithFuncCallback(nsSMILAnimationController::Notify,
this,
kTimerInterval,
nsITimer::TYPE_REPEATING_SLACK);
}
nsresult
nsSMILAnimationController::StopTimer()
{
NS_ENSURE_TRUE(mTimer, NS_ERROR_FAILURE);
return mTimer->Cancel();
if (aRefreshDriver) {
// NOTE: The document might already have been detached from its PresContext
// (and RefreshDriver), which would make GetRefreshDriverForDoc return null.
NS_ABORT_IF_FALSE(!GetRefreshDriverForDoc(mDocument) ||
aRefreshDriver == GetRefreshDriverForDoc(mDocument),
"Stopping sampling with wrong refresh driver");
aRefreshDriver->RemoveRefreshObserver(this, Flush_Style);
}
}
//----------------------------------------------------------------------
@ -668,7 +666,8 @@ nsSMILAnimationController::AddChild(nsSMILTimeContainer& aChild)
NS_ENSURE_TRUE(key,NS_ERROR_OUT_OF_MEMORY);
if (!mPauseState && mChildContainerTable.Count() == 1) {
StartTimer();
Sample(); // Run the first sample manually
StartSampling(GetRefreshDriverForDoc(mDocument));
}
return NS_OK;
@ -680,6 +679,6 @@ nsSMILAnimationController::RemoveChild(nsSMILTimeContainer& aChild)
mChildContainerTable.RemoveEntry(&aChild);
if (!mPauseState && mChildContainerTable.Count() == 0) {
StopTimer();
StopSampling(GetRefreshDriverForDoc(mDocument));
}
}

Просмотреть файл

@ -48,6 +48,7 @@
#include "nsSMILTimeContainer.h"
#include "nsSMILCompositorTable.h"
#include "nsSMILMilestone.h"
#include "nsRefreshDriver.h"
struct nsSMILTargetIdentifier;
class nsISMILAnimationElement;
@ -66,7 +67,8 @@ class nsIDocument;
// a compound document. These time containers can be paused individually or
// here, at the document level.
//
class nsSMILAnimationController : public nsSMILTimeContainer
class nsSMILAnimationController : public nsSMILTimeContainer,
public nsARefreshObserver
{
public:
nsSMILAnimationController();
@ -77,6 +79,12 @@ public:
virtual void Resume(PRUint32 aType);
virtual nsSMILTime GetParentTime() const;
// nsARefreshObserver
NS_IMETHOD_(nsrefcnt) AddRef();
NS_IMETHOD_(nsrefcnt) Release();
virtual void WillRefresh(mozilla::TimeStamp aTime);
// Methods for registering and enumerating animation elements
void RegisterAnimationElement(nsISMILAnimationElement* aAnimationElement);
void UnregisterAnimationElement(nsISMILAnimationElement* aAnimationElement);
@ -102,6 +110,11 @@ public:
void Traverse(nsCycleCollectionTraversalCallback* aCallback);
void Unlink();
// Methods for controlling whether we're sampling
// (Use to register/unregister us with the given nsRefreshDriver)
void StartSampling(nsRefreshDriver* aRefreshDriver);
void StopSampling(nsRefreshDriver* aRefreshDriver);
protected:
// Typedefs
typedef nsPtrHashKey<nsSMILTimeContainer> TimeContainerPtrKey;
@ -138,8 +151,6 @@ protected:
// Timer-related implementation helpers
static void Notify(nsITimer* aTimer, void* aClosure);
nsresult StartTimer();
nsresult StopTimer();
// Sample-related callbacks and implementation helpers
virtual void DoSample();
@ -165,8 +176,10 @@ protected:
virtual void RemoveChild(nsSMILTimeContainer& aChild);
// Members
nsAutoRefCnt mRefCnt;
NS_DECL_OWNINGTHREAD
static const PRUint32 kTimerInterval;
nsCOMPtr<nsITimer> mTimer;
AnimationElementHashtable mAnimationElementTable;
TimeContainerHashtable mChildContainerTable;
PRPackedBool mResampleNeeded;

Просмотреть файл

@ -1708,6 +1708,15 @@ PresShell::Init(nsIDocument* aDocument,
}
#endif
#ifdef MOZ_SMIL
if (mDocument->HasAnimationController()) {
nsSMILAnimationController* animCtrl = mDocument->GetAnimationController();
if (!animCtrl->IsPaused()) {
animCtrl->StartSampling(GetPresContext()->RefreshDriver());
}
}
#endif // MOZ_SMIL
return NS_OK;
}
@ -1817,10 +1826,15 @@ PresShell::Destroy()
mDocument->DeleteShell();
}
nsRefreshDriver* rd = GetPresContext()->RefreshDriver();
if (mDocument->HasAnimationController()) {
mDocument->GetAnimationController()->StopSampling(rd);
}
// Revoke any pending events. We need to do this and cancel pending reflows
// before we destroy the frame manager, since apparently frame destruction
// sometimes spins the event queue when plug-ins are involved(!).
GetPresContext()->RefreshDriver()->RemoveRefreshObserver(this, Flush_Layout);
rd->RemoveRefreshObserver(this, Flush_Layout);
mResizeEvent.Revoke();
if (mAsyncResizeTimerIsActive) {
mAsyncResizeEventTimer->Cancel();