This commit is contained in:
Daniel Holbert 2010-03-10 15:03:47 -08:00
commit 7569307a1d
3 changed files with 43 additions and 54 deletions

View File

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

View File

@ -51,14 +51,16 @@
//----------------------------------------------------------------------
// nsSMILAnimationController implementation
// Helper method
static nsPresContext*
GetPresContextForDoc(nsIDocument* aDoc)
{
nsIPresShell* shell = aDoc->GetPrimaryShell();
return shell ? shell->GetPresContext() : nsnull;
}
// 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;
//----------------------------------------------------------------------
// ctors, dtors, factory methods
@ -73,7 +75,11 @@ nsSMILAnimationController::nsSMILAnimationController()
nsSMILAnimationController::~nsSMILAnimationController()
{
StopTimer();
if (mTimer) {
mTimer->Cancel();
mTimer = nsnull;
}
NS_ASSERTION(mAnimationElementTable.Count() == 0,
"Animation controller shouldn't be tracking any animation"
" elements when it dies");
@ -99,6 +105,9 @@ 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;
@ -139,20 +148,6 @@ 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:
@ -219,38 +214,42 @@ nsSMILAnimationController::Unlink()
//----------------------------------------------------------------------
// Timer-related implementation helpers
/*static*/ void
nsSMILAnimationController::Notify(nsITimer* timer, void* aClosure)
{
nsSMILAnimationController* controller = (nsSMILAnimationController*)aClosure;
NS_ASSERTION(controller->mTimer == timer,
"nsSMILAnimationController::Notify called with incorrect timer");
controller->Sample();
}
nsresult
nsSMILAnimationController::StartTimer()
{
NS_ENSURE_TRUE(mTimer, NS_ERROR_FAILURE);
NS_ASSERTION(mPauseState == 0, "Starting timer but controller is paused");
// Run the first sample manually
Sample();
// Register with PresContext's refresh driver for future samples
nsPresContext* presContext = GetPresContextForDoc(mDocument);
if (!presContext) {
NS_WARNING("Starting timer but have no pres context");
return NS_ERROR_FAILURE;
}
nsRefreshDriver* rd = presContext->RefreshDriver();
return rd->AddRefreshObserver(this, Flush_Style) ?
NS_OK : NS_ERROR_OUT_OF_MEMORY;
//
// 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()
{
nsPresContext* presContext = GetPresContextForDoc(mDocument);
if (!presContext) {
NS_WARNING("Stopping timer but have no pres context");
return NS_ERROR_FAILURE;
}
nsRefreshDriver* rd = presContext->RefreshDriver();
rd->RemoveRefreshObserver(this, Flush_Style); // may fail, if already stopped
NS_ENSURE_TRUE(mTimer, NS_ERROR_FAILURE);
return NS_OK;
return mTimer->Cancel();
}
//----------------------------------------------------------------------

View File

@ -48,7 +48,6 @@
#include "nsSMILTimeContainer.h"
#include "nsSMILCompositorTable.h"
#include "nsSMILMilestone.h"
#include "nsRefreshDriver.h"
struct nsSMILTargetIdentifier;
class nsISMILAnimationElement;
@ -67,8 +66,7 @@ class nsIDocument;
// a compound document. These time containers can be paused individually or
// here, at the document level.
//
class nsSMILAnimationController : public nsSMILTimeContainer,
public nsARefreshObserver
class nsSMILAnimationController : public nsSMILTimeContainer
{
public:
nsSMILAnimationController();
@ -79,12 +77,6 @@ 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);
@ -173,10 +165,8 @@ 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;