2013-07-15 22:38:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* 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/. */
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#ifndef mozilla_image_FrameAnimator_h
|
|
|
|
#define mozilla_image_FrameAnimator_h
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-07-15 22:38:59 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
#include "imgFrame.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2013-07-15 22:38:59 +04:00
|
|
|
#include "nsRect.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
#include "SurfaceCache.h"
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2015-01-07 13:51:58 +03:00
|
|
|
class RasterImage;
|
2013-09-07 17:01:08 +04:00
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
class FrameAnimator
|
|
|
|
{
|
|
|
|
public:
|
2015-01-07 12:40:23 +03:00
|
|
|
FrameAnimator(RasterImage* aImage,
|
2015-01-08 00:07:23 +03:00
|
|
|
gfx::IntSize aSize,
|
2015-01-07 12:40:23 +03:00
|
|
|
uint16_t aAnimationMode)
|
2015-01-08 00:07:23 +03:00
|
|
|
: mImage(aImage)
|
|
|
|
, mSize(aSize)
|
|
|
|
, mCurrentAnimationFrameIndex(0)
|
|
|
|
, mLoopRemainingCount(-1)
|
|
|
|
, mLastCompositedFrameIndex(-1)
|
|
|
|
, mLoopCount(-1)
|
2015-08-14 10:37:13 +03:00
|
|
|
, mFirstFrameTimeout(0)
|
2015-01-07 12:40:23 +03:00
|
|
|
, mAnimationMode(aAnimationMode)
|
|
|
|
, mDoneDecoding(false)
|
|
|
|
{ }
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value from RequestRefresh. Tells callers what happened in that call
|
|
|
|
* to RequestRefresh.
|
|
|
|
*/
|
|
|
|
struct RefreshResult
|
|
|
|
{
|
|
|
|
// The dirty rectangle to be re-drawn after this RequestRefresh().
|
|
|
|
nsIntRect dirtyRect;
|
|
|
|
|
|
|
|
// Whether any frame changed, and hence the dirty rect was set.
|
|
|
|
bool frameAdvanced : 1;
|
|
|
|
|
|
|
|
// Whether the animation has finished playing.
|
|
|
|
bool animationFinished : 1;
|
|
|
|
|
|
|
|
// Whether an error has occurred when trying to advance a frame. Note that
|
|
|
|
// errors do not, on their own, end the animation.
|
|
|
|
bool error : 1;
|
|
|
|
|
|
|
|
RefreshResult()
|
|
|
|
: frameAdvanced(false)
|
|
|
|
, animationFinished(false)
|
|
|
|
, error(false)
|
2014-12-15 02:54:00 +03:00
|
|
|
{ }
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
void Accumulate(const RefreshResult& other)
|
|
|
|
{
|
|
|
|
frameAdvanced = frameAdvanced || other.frameAdvanced;
|
|
|
|
animationFinished = animationFinished || other.animationFinished;
|
|
|
|
error = error || other.error;
|
|
|
|
dirtyRect = dirtyRect.Union(other.dirtyRect);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Re-evaluate what frame we're supposed to be on, and do whatever blending
|
|
|
|
* is necessary to get us to that frame.
|
|
|
|
*
|
|
|
|
* Returns the result of that blending, including whether the current frame
|
|
|
|
* changed and what the resulting dirty rectangle is.
|
|
|
|
*/
|
2014-07-10 19:00:31 +04:00
|
|
|
RefreshResult RequestRefresh(const TimeStamp& aTime);
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Call when this image is finished decoding so we know that there aren't any
|
|
|
|
* more frames coming.
|
|
|
|
*/
|
|
|
|
void SetDoneDecoding(bool aDone);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call when you need to re-start animating. Ensures we start from the first
|
|
|
|
* frame.
|
|
|
|
*/
|
|
|
|
void ResetAnimation();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The animation mode of the image.
|
|
|
|
*
|
|
|
|
* Constants defined in imgIContainer.idl.
|
|
|
|
*/
|
|
|
|
void SetAnimationMode(uint16_t aAnimationMode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Union the area to refresh when we loop around to the first frame with this
|
|
|
|
* rect.
|
|
|
|
*/
|
|
|
|
void UnionFirstFrameRefreshArea(const nsIntRect& aRect);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the animation frame time has not yet been set, set it to
|
|
|
|
* TimeStamp::Now().
|
|
|
|
*/
|
|
|
|
void InitAnimationFrameTimeIfNecessary();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the animation frame time to @aTime.
|
|
|
|
*/
|
|
|
|
void SetAnimationFrameTime(const TimeStamp& aTime);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current frame we're on, from 0 to (numFrames - 1).
|
|
|
|
*/
|
|
|
|
uint32_t GetCurrentAnimationFrameIndex() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the area we refresh when we loop around to the first frame.
|
|
|
|
*/
|
|
|
|
nsIntRect GetFirstFrameRefreshArea() const;
|
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
/**
|
2015-01-16 20:27:00 +03:00
|
|
|
* If we have a composited frame for @aFrameNum, returns it. Otherwise,
|
2015-07-01 04:57:03 +03:00
|
|
|
* returns an empty LookupResult. It is an error to call this method with
|
2015-01-08 00:07:23 +03:00
|
|
|
* aFrameNum == 0, because the first frame is never composited.
|
|
|
|
*/
|
2015-07-01 04:57:03 +03:00
|
|
|
LookupResult GetCompositedFrame(uint32_t aFrameNum);
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the frame's adjusted timeout. If the animation loops and the
|
|
|
|
* timeout falls in between a certain range then the timeout is adjusted so
|
|
|
|
* that it's never 0. If the animation does not loop then no adjustments are
|
|
|
|
* made.
|
|
|
|
*/
|
|
|
|
int32_t GetTimeoutForFrame(uint32_t aFrameNum) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set number of times to loop the image.
|
|
|
|
* @note -1 means loop forever.
|
|
|
|
*/
|
|
|
|
void SetLoopCount(int32_t aLoopCount) { mLoopCount = aLoopCount; }
|
|
|
|
int32_t LoopCount() const { return mLoopCount; }
|
|
|
|
|
2015-08-14 10:37:13 +03:00
|
|
|
/*
|
|
|
|
* Set the timeout for the first frame. This is used to allow animation
|
|
|
|
* scheduling even before a full decode runs for this image.
|
|
|
|
*/
|
|
|
|
void SetFirstFrameTimeout(int32_t aTimeout) { mFirstFrameTimeout = aTimeout; }
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
/**
|
|
|
|
* Collect an accounting of the memory occupied by the compositing surfaces we
|
|
|
|
* use during animation playback. All of the actual animation frames are
|
|
|
|
* stored in the SurfaceCache, so we don't need to report them here.
|
|
|
|
*/
|
|
|
|
void CollectSizeOfCompositingSurfaces(nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
private: // methods
|
|
|
|
/**
|
|
|
|
* Gets the length of a single loop of this image, in milliseconds.
|
|
|
|
*
|
|
|
|
* If this image is not finished decoding, is not animated, or it is animated
|
2014-12-15 02:54:00 +03:00
|
|
|
* but does not loop, returns -1. Can return 0 in the case of an animated
|
|
|
|
* image that has a 0ms delay between its frames and does not loop.
|
2013-07-15 22:38:59 +04:00
|
|
|
*/
|
2013-12-06 13:45:24 +04:00
|
|
|
int32_t GetSingleLoopTime() const;
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Advances the animation. Typically, this will advance a single frame, but it
|
|
|
|
* may advance multiple frames. This may happen if we have infrequently
|
|
|
|
* "ticking" refresh drivers (e.g. in background tabs), or extremely short-
|
|
|
|
* lived animation frames.
|
|
|
|
*
|
|
|
|
* @param aTime the time that the animation should advance to. This will
|
|
|
|
* typically be <= TimeStamp::Now().
|
|
|
|
*
|
|
|
|
* @returns a RefreshResult that shows whether the frame was successfully
|
|
|
|
* advanced, and its resulting dirty rect.
|
|
|
|
*/
|
2014-07-10 19:00:31 +04:00
|
|
|
RefreshResult AdvanceFrame(TimeStamp aTime);
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the time the frame we're currently displaying is supposed to end.
|
|
|
|
*
|
|
|
|
* In the error case, returns an "infinity" timestamp.
|
|
|
|
*/
|
2014-07-10 19:00:31 +04:00
|
|
|
TimeStamp GetCurrentImgFrameEndTime() const;
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
bool DoBlend(nsIntRect* aDirtyRect, uint32_t aPrevFrameIndex,
|
|
|
|
uint32_t aNextFrameIndex);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the @aIndex-th frame in the frame index, ignoring results of blending.
|
|
|
|
*/
|
|
|
|
RawAccessFrameRef GetRawFrame(uint32_t aFrameNum) const;
|
|
|
|
|
|
|
|
/** Clears an area of <aFrame> with transparent black.
|
|
|
|
*
|
|
|
|
* @param aFrameData Target Frame data
|
|
|
|
* @param aFrameRect The rectangle of the data pointed ot by aFrameData
|
|
|
|
*
|
|
|
|
* @note Does also clears the transparency mask
|
|
|
|
*/
|
|
|
|
static void ClearFrame(uint8_t* aFrameData, const nsIntRect& aFrameRect);
|
|
|
|
|
|
|
|
//! @overload
|
|
|
|
static void ClearFrame(uint8_t* aFrameData, const nsIntRect& aFrameRect,
|
|
|
|
const nsIntRect& aRectToClear);
|
|
|
|
|
|
|
|
//! Copy one frame's image and mask into another
|
|
|
|
static bool CopyFrameImage(const uint8_t* aDataSrc, const nsIntRect& aRectSrc,
|
|
|
|
uint8_t* aDataDest, const nsIntRect& aRectDest);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws one frame's image to into another, at the position specified by
|
|
|
|
* aSrcRect.
|
|
|
|
*
|
|
|
|
* @aSrcData the raw data of the current frame being drawn
|
|
|
|
* @aSrcRect the size of the source frame, and the position of that frame in
|
|
|
|
* the composition frame
|
|
|
|
* @aSrcPaletteLength the length (in bytes) of the palette at the beginning
|
|
|
|
* of the source data (0 if image is not paletted)
|
|
|
|
* @aSrcHasAlpha whether the source data represents an image with alpha
|
|
|
|
* @aDstPixels the raw data of the composition frame where the current frame
|
|
|
|
* is drawn into (32-bit ARGB)
|
|
|
|
* @aDstRect the size of the composition frame
|
|
|
|
* @aBlendMethod the blend method for how to blend src on the composition
|
|
|
|
* frame.
|
|
|
|
*/
|
|
|
|
static nsresult DrawFrameTo(const uint8_t* aSrcData,
|
|
|
|
const nsIntRect& aSrcRect,
|
|
|
|
uint32_t aSrcPaletteLength, bool aSrcHasAlpha,
|
|
|
|
uint8_t* aDstPixels, const nsIntRect& aDstRect,
|
|
|
|
BlendMethod aBlendMethod);
|
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
private: // data
|
2015-01-08 00:07:23 +03:00
|
|
|
//! A weak pointer to our owning image.
|
|
|
|
RasterImage* mImage;
|
|
|
|
|
|
|
|
//! The intrinsic size of the image.
|
|
|
|
gfx::IntSize mSize;
|
|
|
|
|
|
|
|
/** For managing blending of frames
|
|
|
|
*
|
|
|
|
* Some animations will use the compositingFrame to composite images
|
|
|
|
* and just hand this back to the caller when it is time to draw the frame.
|
|
|
|
* NOTE: When clearing compositingFrame, remember to set
|
|
|
|
* lastCompositedFrameIndex to -1. Code assume that if
|
|
|
|
* lastCompositedFrameIndex >= 0 then compositingFrame exists.
|
|
|
|
*/
|
|
|
|
RawAccessFrameRef mCompositingFrame;
|
|
|
|
|
|
|
|
/** the previous composited frame, for DISPOSE_RESTORE_PREVIOUS
|
|
|
|
*
|
|
|
|
* The Previous Frame (all frames composited up to the current) needs to be
|
|
|
|
* stored in cases where the image specifies it wants the last frame back
|
|
|
|
* when it's done with the current frame.
|
|
|
|
*/
|
|
|
|
RawAccessFrameRef mCompositingPrevFrame;
|
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
//! Area of the first frame that needs to be redrawn on subsequent loops.
|
|
|
|
nsIntRect mFirstFrameRefreshArea;
|
|
|
|
|
|
|
|
//! the time that the animation advanced to the current frame
|
|
|
|
TimeStamp mCurrentAnimationFrameTime;
|
|
|
|
|
|
|
|
//! The current frame index we're on. 0 to (numFrames - 1).
|
|
|
|
uint32_t mCurrentAnimationFrameIndex;
|
|
|
|
|
|
|
|
//! number of loops remaining before animation stops (-1 no stop)
|
2015-01-08 00:07:23 +03:00
|
|
|
int32_t mLoopRemainingCount;
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
//! Track the last composited frame for Optimizations (See DoComposite code)
|
|
|
|
int32_t mLastCompositedFrameIndex;
|
2015-01-07 12:40:23 +03:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
//! The total number of loops for the image.
|
|
|
|
int32_t mLoopCount;
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2015-08-14 10:37:13 +03:00
|
|
|
//! The timeout for the first frame of this image.
|
|
|
|
int32_t mFirstFrameTimeout;
|
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
//! The animation mode of this image. Constants defined in imgIContainer.
|
|
|
|
uint16_t mAnimationMode;
|
|
|
|
|
|
|
|
//! Whether this image is done being decoded.
|
|
|
|
bool mDoneDecoding;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#endif // mozilla_image_FrameAnimator_h
|