Bug 611797 part 2: Shift shared code from VectorImage/RasterImage to Image superclass, and add impl for VectorImage::ShouldAnimate. r+a=joe
authorDaniel Holbert <dholbert@cs.stanford.edu>
Wed, 17 Nov 2010 12:39:23 -0800
changeset 57656 453184e9da057f6125a0b9f365920718fe686198
parent 57655 a65974f1daad1b2131870ee9e6613519d4527150
child 57657 6d7b682a3cc45179fbc0c2e769eb4ba4d6cac325
push id17027
push userdholbert@mozilla.com
push dateWed, 17 Nov 2010 20:40:13 +0000
treeherdermozilla-central@6d7b682a3cc4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs611797
milestone2.0b8pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 611797 part 2: Shift shared code from VectorImage/RasterImage to Image superclass, and add impl for VectorImage::ShouldAnimate. r+a=joe
modules/libpr0n/src/Image.cpp
modules/libpr0n/src/Image.h
modules/libpr0n/src/RasterImage.cpp
modules/libpr0n/src/RasterImage.h
modules/libpr0n/src/VectorImage.cpp
modules/libpr0n/src/VectorImage.h
--- a/modules/libpr0n/src/Image.cpp
+++ b/modules/libpr0n/src/Image.cpp
@@ -38,16 +38,17 @@
 #include "Image.h"
 
 namespace mozilla {
 namespace imagelib {
 
 // Constructor
 Image::Image(imgStatusTracker* aStatusTracker) :
   mAnimationConsumers(0),
+  mAnimationMode(kNormalAnimMode),
   mInitialized(PR_FALSE),
   mAnimating(PR_FALSE),
   mError(PR_FALSE)
 {
   if (aStatusTracker) {
     mStatusTracker = aStatusTracker;
     mStatusTracker->SetImage(this);
   } else {
@@ -120,16 +121,50 @@ Image::IncrementAnimationConsumers()
 void
 Image::DecrementAnimationConsumers()
 {
   NS_ABORT_IF_FALSE(mAnimationConsumers >= 1, "Invalid no. of animation consumers!");
   mAnimationConsumers--;
   EvaluateAnimation();
 }
 
+//******************************************************************************
+/* attribute unsigned short animationMode; */
+NS_IMETHODIMP
+Image::GetAnimationMode(PRUint16* aAnimationMode)
+{
+  if (mError)
+    return NS_ERROR_FAILURE;
+
+  NS_ENSURE_ARG_POINTER(aAnimationMode);
+  
+  *aAnimationMode = mAnimationMode;
+  return NS_OK;
+}
+
+//******************************************************************************
+/* attribute unsigned short animationMode; */
+NS_IMETHODIMP
+Image::SetAnimationMode(PRUint16 aAnimationMode)
+{
+  if (mError)
+    return NS_ERROR_FAILURE;
+
+  NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
+               aAnimationMode == kDontAnimMode ||
+               aAnimationMode == kLoopOnceAnimMode,
+               "Wrong Animation Mode is being set!");
+  
+  mAnimationMode = aAnimationMode;
+
+  EvaluateAnimation();
+
+  return NS_OK;
+}
+
 void
 Image::EvaluateAnimation()
 {
   if (!mAnimating && ShouldAnimate()) {
     nsresult rv = StartAnimation();
     mAnimating = NS_SUCCEEDED(rv);
   } else if (mAnimating && !ShouldAnimate()) {
     StopAnimation();
--- a/modules/libpr0n/src/Image.h
+++ b/modules/libpr0n/src/Image.h
@@ -43,16 +43,20 @@
 #include "prtypes.h"
 
 namespace mozilla {
 namespace imagelib {
 
 class Image : public imgIContainer
 {
 public:
+  // From NS_DECL_IMGICONTAINER:
+  NS_SCRIPTABLE NS_IMETHOD GetAnimationMode(PRUint16 *aAnimationMode);
+  NS_SCRIPTABLE NS_IMETHOD SetAnimationMode(PRUint16 aAnimationMode);
+
   imgStatusTracker& GetStatusTracker() { return *mStatusTracker; }
 
   /**
    * Flags for Image initialization.
    *
    * Meanings:
    *
    * INIT_FLAG_NONE: Lack of flags
@@ -129,23 +133,26 @@ protected:
   virtual void EvaluateAnimation();
 
   virtual nsresult StartAnimation() = 0;
   virtual nsresult StopAnimation() = 0;
 
   // Member data shared by all implementations of this abstract class
   nsAutoPtr<imgStatusTracker> mStatusTracker;
   PRUint32                    mAnimationConsumers;
-  PRPackedBool                mInitialized;   // Have we been initalized?
-  PRPackedBool                mAnimating;
-  PRPackedBool                mError;         // Error handling
+  PRUint16                    mAnimationMode;   // Enum values in imgIContainer
+  PRPackedBool                mInitialized:1;   // Have we been initalized?
+  PRPackedBool                mAnimating:1;     // Are we currently animating?
+  PRPackedBool                mError:1;         // Error handling
 
   /**
    * Extended by child classes, if they have additional
    * conditions for being able to animate
    */
-  virtual PRBool ShouldAnimate() { return mAnimationConsumers > 0; }
+  virtual PRBool ShouldAnimate() {
+    return mAnimationConsumers > 0 && mAnimationMode != kDontAnimMode;
+  }
 };
 
 } // namespace imagelib
 } // namespace mozilla
 
 #endif // MOZILLA_IMAGELIB_IMAGE_H_
--- a/modules/libpr0n/src/RasterImage.cpp
+++ b/modules/libpr0n/src/RasterImage.cpp
@@ -174,17 +174,16 @@ NS_IMPL_ISUPPORTS5(RasterImage, imgICont
                    imgIContainerDebug, nsISupportsWeakReference)
 #endif
 
 //******************************************************************************
 RasterImage::RasterImage(imgStatusTracker* aStatusTracker) :
   Image(aStatusTracker), // invoke superclass's constructor
   mSize(0,0),
   mAnim(nsnull),
-  mAnimationMode(kNormalAnimMode),
   mLoopCount(-1),
   mObserver(nsnull),
   mLockCount(0),
   mDecoder(nsnull),
   mWorker(nsnull),
   mBytesDecoded(0),
 #ifdef DEBUG
   mFramesNotified(0),
@@ -1047,50 +1046,16 @@ RasterImage::DecodingComplete()
     rv = mFrames[0]->Optimize();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
 //******************************************************************************
-/* attribute unsigned short animationMode; */
-NS_IMETHODIMP
-RasterImage::GetAnimationMode(PRUint16 *aAnimationMode)
-{
-  if (mError)
-    return NS_ERROR_FAILURE;
-
-  NS_ENSURE_ARG_POINTER(aAnimationMode);
-  
-  *aAnimationMode = mAnimationMode;
-  return NS_OK;
-}
-
-//******************************************************************************
-/* attribute unsigned short animationMode; */
-NS_IMETHODIMP
-RasterImage::SetAnimationMode(PRUint16 aAnimationMode)
-{
-  if (mError)
-    return NS_ERROR_FAILURE;
-
-  NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
-               aAnimationMode == kDontAnimMode ||
-               aAnimationMode == kLoopOnceAnimMode,
-               "Wrong Animation Mode is being set!");
-  
-  mAnimationMode = aAnimationMode;
-
-  EvaluateAnimation();
-
-  return NS_OK;
-}
-
-//******************************************************************************
 /* void StartAnimation () */
 nsresult
 RasterImage::StartAnimation()
 {
   if (mError)
     return NS_ERROR_FAILURE;
 
   NS_ABORT_IF_FALSE(ShouldAnimate(), "Should not animate!");
@@ -2702,17 +2667,17 @@ RasterImage::WriteToRasterImage(nsIInput
 
   return NS_OK;
 }
 
 PRBool
 RasterImage::ShouldAnimate()
 {
   return Image::ShouldAnimate() && mFrames.Length() >= 2 &&
-         mAnimationMode != kDontAnimMode && !mAnimationFinished;
+         !mAnimationFinished;
 }
 
 //******************************************************************************
 /* readonly attribute PRUint32 framesNotified; */
 #ifdef DEBUG
 NS_IMETHODIMP
 RasterImage::GetFramesNotified(PRUint32 *aFramesNotified)
 {
--- a/modules/libpr0n/src/RasterImage.h
+++ b/modules/libpr0n/src/RasterImage.h
@@ -159,33 +159,31 @@ class RasterImage : public mozilla::imag
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSITIMERCALLBACK
   NS_DECL_NSIPROPERTIES
 #ifdef DEBUG
   NS_DECL_IMGICONTAINERDEBUG
 #endif
 
-  // BEGIN NS_DECL_IMGICONTAINER
+  // BEGIN NS_DECL_IMGICONTAINER (minus GetAnimationMode/SetAnimationMode)
   // ** Don't edit this chunk except to mirror changes in imgIContainer.idl **
   NS_SCRIPTABLE NS_IMETHOD GetWidth(PRInt32 *aWidth);
   NS_SCRIPTABLE NS_IMETHOD GetHeight(PRInt32 *aHeight);
   NS_SCRIPTABLE NS_IMETHOD GetType(PRUint16 *aType);
   NS_SCRIPTABLE NS_IMETHOD GetAnimated(PRBool *aAnimated);
   NS_SCRIPTABLE NS_IMETHOD GetCurrentFrameIsOpaque(PRBool *aCurrentFrameIsOpaque);
   NS_IMETHOD GetFrame(PRUint32 aWhichFrame, PRUint32 aFlags, gfxASurface **_retval NS_OUTPARAM);
   NS_IMETHOD CopyFrame(PRUint32 aWhichFrame, PRUint32 aFlags, gfxImageSurface **_retval NS_OUTPARAM);
   NS_IMETHOD ExtractFrame(PRUint32 aWhichFrame, const nsIntRect & aRect, PRUint32 aFlags, imgIContainer **_retval NS_OUTPARAM);
   NS_IMETHOD Draw(gfxContext *aContext, gfxPattern::GraphicsFilter aFilter, const gfxMatrix & aUserSpaceToImageSpace, const gfxRect & aFill, const nsIntRect & aSubimage, const nsIntSize & aViewportSize, PRUint32 aFlags);
   NS_IMETHOD_(nsIFrame *) GetRootLayoutFrame(void);
   NS_SCRIPTABLE NS_IMETHOD RequestDecode(void);
   NS_SCRIPTABLE NS_IMETHOD LockImage(void);
   NS_SCRIPTABLE NS_IMETHOD UnlockImage(void);
-  NS_SCRIPTABLE NS_IMETHOD GetAnimationMode(PRUint16 *aAnimationMode);
-  NS_SCRIPTABLE NS_IMETHOD SetAnimationMode(PRUint16 aAnimationMode);
   NS_SCRIPTABLE NS_IMETHOD ResetAnimation(void);
   // END NS_DECL_IMGICONTAINER
 
   RasterImage(imgStatusTracker* aStatusTracker = nsnull);
   virtual ~RasterImage();
 
   virtual nsresult StartAnimation();
   virtual nsresult StopAnimation();
@@ -463,19 +461,16 @@ private: // data
   
   nsCOMPtr<nsIProperties>    mProperties;
 
   // IMPORTANT: if you use mAnim in a method, call EnsureImageIsDecoded() first to ensure
   // that the frames actually exist (they may have been discarded to save memory, or
   // we maybe decoding on draw).
   RasterImage::Anim*        mAnim;
   
-  //! See imgIContainer for mode constants
-  PRUint16                   mAnimationMode;
-  
   //! # loops remaining before animation stops (-1 no stop)
   PRInt32                    mLoopCount;
   
   //! imgIDecoderObserver
   nsWeakPtr                  mObserver;
 
   // Discard members
   PRUint32                   mLockCount;
--- a/modules/libpr0n/src/VectorImage.cpp
+++ b/modules/libpr0n/src/VectorImage.cpp
@@ -205,17 +205,16 @@ NS_IMPL_ISUPPORTS3(VectorImage,
 
 //------------------------------------------------------------------------------
 // Constructor / Destructor
 
 VectorImage::VectorImage(imgStatusTracker* aStatusTracker) :
   Image(aStatusTracker), // invoke superclass's constructor
   mRestrictedRegion(0, 0, 0, 0),
   mLastRenderedSize(0, 0),
-  mAnimationMode(kNormalAnimMode),
   mIsInitialized(PR_FALSE),
   mIsFullyLoaded(PR_FALSE),
   mHaveAnimations(PR_FALSE),
   mHaveRestrictedRegion(PR_FALSE)
 {
 }
 
 VectorImage::~VectorImage()
@@ -270,40 +269,39 @@ VectorImage::GetSourceDataSize()
 }
 
 nsresult
 VectorImage::StartAnimation()
 {
   if (mError)
     return NS_ERROR_FAILURE;
 
-  if (mAnimationMode == kDontAnimMode ||
-      !mIsFullyLoaded || !mHaveAnimations) {
-    // Animation disabled, or helper-document not finished or lacks animations.
-    return NS_OK;
-  }
+  NS_ABORT_IF_FALSE(ShouldAnimate(), "Should not animate!");
 
   mSVGDocumentWrapper->StartAnimation();
-
   return NS_OK;
 }
 
 nsresult
 VectorImage::StopAnimation()
 {
   if (mError)
     return NS_ERROR_FAILURE;
 
-  if (!mIsFullyLoaded || !mHaveAnimations) {
-    return NS_OK;
-  }
+  NS_ABORT_IF_FALSE(mIsFullyLoaded && mHaveAnimations,
+                    "Should not have been animating!");
 
   mSVGDocumentWrapper->StopAnimation();
+  return NS_OK;
+}
 
-  return NS_OK;
+PRBool
+VectorImage::ShouldAnimate()
+{
+  return Image::ShouldAnimate() && mIsFullyLoaded && mHaveAnimations;
 }
 
 //------------------------------------------------------------------------------
 // imgIContainer methods
 
 //******************************************************************************
 /* readonly attribute PRInt32 width; */
 NS_IMETHODIMP
@@ -588,56 +586,16 @@ VectorImage::LockImage()
 NS_IMETHODIMP
 VectorImage::UnlockImage()
 {
   // This method is for image-discarding, which only applies to RasterImages.
   return NS_OK;
 }
 
 //******************************************************************************
-/* attribute unsigned short animationMode; */
-NS_IMETHODIMP
-VectorImage::GetAnimationMode(PRUint16* aAnimationMode)
-{
-  if (mError)
-    return NS_ERROR_FAILURE;
-
-  NS_ENSURE_ARG_POINTER(aAnimationMode);
-  
-  *aAnimationMode = mAnimationMode;
-  return NS_OK;
-}
-
-//******************************************************************************
-/* attribute unsigned short animationMode; */
-NS_IMETHODIMP
-VectorImage::SetAnimationMode(PRUint16 aAnimationMode)
-{
-  // NOTE: This is just a simpler form of RasterImage::SetAnimationMode.
-  // (Simpler because SVG animations don't have a concept of "loop once" mode)
-  if (mError)
-    return NS_ERROR_FAILURE;
-
-  NS_ASSERTION(aAnimationMode == kNormalAnimMode ||
-               aAnimationMode == kDontAnimMode ||
-               aAnimationMode == kLoopOnceAnimMode,
-               "An unrecognized Animation Mode is being set!");
-
-  mAnimationMode = aAnimationMode;
-
-  if (mAnimationMode == kDontAnimMode) {
-    StopAnimation();
-  } else { // kNormalAnimMode or kLoopOnceAnimMode (treated the same here)
-    StartAnimation();
-  }
-
-  return NS_OK;
-}
-
-//******************************************************************************
 /* void resetAnimation (); */
 NS_IMETHODIMP
 VectorImage::ResetAnimation()
 {
   if (mError)
     return NS_ERROR_FAILURE;
 
   if (!mIsFullyLoaded || !mHaveAnimations) {
--- a/modules/libpr0n/src/VectorImage.h
+++ b/modules/libpr0n/src/VectorImage.h
@@ -54,33 +54,31 @@ class SVGRootRenderingObserver;
 class VectorImage : public Image,
                     public nsIStreamListener
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIREQUESTOBSERVER
   NS_DECL_NSISTREAMLISTENER
 
-  // BEGIN NS_DECL_IMGICONTAINER
+  // BEGIN NS_DECL_IMGICONTAINER (minus GetAnimationMode/SetAnimationMode)
   // ** Don't edit this chunk except to mirror changes in imgIContainer.idl **
   NS_SCRIPTABLE NS_IMETHOD GetWidth(PRInt32 *aWidth);
   NS_SCRIPTABLE NS_IMETHOD GetHeight(PRInt32 *aHeight);
   NS_SCRIPTABLE NS_IMETHOD GetType(PRUint16 *aType);
   NS_SCRIPTABLE NS_IMETHOD GetAnimated(PRBool *aAnimated);
   NS_SCRIPTABLE NS_IMETHOD GetCurrentFrameIsOpaque(PRBool *aCurrentFrameIsOpaque);
   NS_IMETHOD GetFrame(PRUint32 aWhichFrame, PRUint32 aFlags, gfxASurface **_retval NS_OUTPARAM);
   NS_IMETHOD CopyFrame(PRUint32 aWhichFrame, PRUint32 aFlags, gfxImageSurface **_retval NS_OUTPARAM);
   NS_IMETHOD ExtractFrame(PRUint32 aWhichFrame, const nsIntRect & aRect, PRUint32 aFlags, imgIContainer **_retval NS_OUTPARAM);
   NS_IMETHOD Draw(gfxContext *aContext, gfxPattern::GraphicsFilter aFilter, const gfxMatrix & aUserSpaceToImageSpace, const gfxRect & aFill, const nsIntRect & aSubimage, const nsIntSize & aViewportSize, PRUint32 aFlags);
   NS_IMETHOD_(nsIFrame *) GetRootLayoutFrame(void);
   NS_SCRIPTABLE NS_IMETHOD RequestDecode(void);
   NS_SCRIPTABLE NS_IMETHOD LockImage(void);
   NS_SCRIPTABLE NS_IMETHOD UnlockImage(void);
-  NS_SCRIPTABLE NS_IMETHOD GetAnimationMode(PRUint16 *aAnimationMode);
-  NS_SCRIPTABLE NS_IMETHOD SetAnimationMode(PRUint16 aAnimationMode);
   NS_SCRIPTABLE NS_IMETHOD ResetAnimation(void);
   // END NS_DECL_IMGICONTAINER
 
   VectorImage(imgStatusTracker* aStatusTracker = nsnull);
   virtual ~VectorImage();
 
   // C++-only version of imgIContainer::GetType, for convenience
   PRUint16 GetType() { return imgIContainer::TYPE_VECTOR; }
@@ -95,16 +93,17 @@ public:
   PRUint32 GetSourceDataSize();
 
   // Callback for SVGRootRenderingObserver
   void InvalidateObserver();
 
 protected:
   virtual nsresult StartAnimation();
   virtual nsresult StopAnimation();
+  virtual PRBool   ShouldAnimate();
 
 private:
   nsWeakPtr                          mObserver;   //! imgIDecoderObserver
   nsRefPtr<SVGDocumentWrapper>       mSVGDocumentWrapper;
 #ifdef MOZ_ENABLE_LIBXUL
   nsRefPtr<SVGRootRenderingObserver> mRenderingObserver;
 #endif // MOZ_ENABLE_LIBXUL
 
@@ -113,18 +112,16 @@ private:
                                           // that we're restricted to using.
                                           // Otherwise, this is ignored.
 
   nsIntSize      mLastRenderedSize;       // The viewport-size that we've
                                           // most recently passed to
                                           // mSVGDocumentWrapper as its
                                           // viewport-bounds.
 
-  PRUint16       mAnimationMode;          // Are we allowed to animate?
-
   PRPackedBool   mIsInitialized:1;        // Have we been initalized?
   PRPackedBool   mIsFullyLoaded:1;        // Has OnStopRequest been called?
   PRPackedBool   mHaveAnimations:1;       // Is our SVG content SMIL-animated?
                                           // (Only set after mIsFullyLoaded.)
   PRPackedBool   mHaveRestrictedRegion:1; // Are we a restricted-region clone
                                           // created via ExtractFrame?
 };