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
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
2013-07-15 22:38:59 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
#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;
|
2017-03-24 08:57:30 +03:00
|
|
|
class DrawableSurface;
|
2013-09-07 17:01:08 +04:00
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
class AnimationState {
|
2013-07-15 22:38:59 +04:00
|
|
|
public:
|
2016-07-19 09:26:58 +03:00
|
|
|
explicit AnimationState(uint16_t aAnimationMode)
|
2016-07-28 03:12:25 +03:00
|
|
|
: mFrameCount(0),
|
|
|
|
mCurrentAnimationFrameIndex(0),
|
2016-07-19 09:26:58 +03:00
|
|
|
mLoopRemainingCount(-1),
|
2015-01-08 00:07:23 +03:00
|
|
|
mLoopCount(-1),
|
2016-07-20 02:22:34 +03:00
|
|
|
mFirstFrameTimeout(FrameTimeout::FromRawMilliseconds(0)),
|
2015-01-07 12:40:23 +03:00
|
|
|
mAnimationMode(aAnimationMode),
|
2017-03-02 07:45:54 +03:00
|
|
|
mHasBeenDecoded(false),
|
2017-08-10 05:26:55 +03:00
|
|
|
mHasRequestedDecode(false),
|
2017-03-16 11:06:04 +03:00
|
|
|
mIsCurrentlyDecoded(false),
|
|
|
|
mCompositedFrameInvalid(false),
|
|
|
|
mDiscarded(false) {}
|
2016-07-19 09:26:58 +03:00
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
/**
|
|
|
|
* Call this whenever a decode completes, a decode starts, or the image is
|
|
|
|
* discarded. It will update the internal state. Specifically mDiscarded,
|
2017-06-01 10:19:55 +03:00
|
|
|
* mCompositedFrameInvalid, and mIsCurrentlyDecoded. If aAllowInvalidation
|
|
|
|
* is true then returns a rect to invalidate.
|
2017-03-26 08:04:53 +03:00
|
|
|
*/
|
2020-04-20 06:38:27 +03:00
|
|
|
const gfx::IntRect UpdateState(RasterImage* aImage, const gfx::IntSize& aSize,
|
2017-06-01 10:19:55 +03:00
|
|
|
bool aAllowInvalidation = true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
private:
|
2017-04-06 12:00:36 +03:00
|
|
|
const gfx::IntRect UpdateStateInternal(LookupResult& aResult,
|
2017-06-01 10:19:55 +03:00
|
|
|
const gfx::IntSize& aSize,
|
|
|
|
bool aAllowInvalidation = true);
|
2017-03-26 08:04:53 +03:00
|
|
|
|
|
|
|
public:
|
2016-07-19 09:26:58 +03:00
|
|
|
/**
|
2017-03-02 07:45:54 +03:00
|
|
|
* Call when a decode of this image has been completed.
|
2016-07-19 09:26:58 +03:00
|
|
|
*/
|
2017-03-02 07:45:54 +03:00
|
|
|
void NotifyDecodeComplete();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if this image has been fully decoded before.
|
|
|
|
*/
|
|
|
|
bool GetHasBeenDecoded() { return mHasBeenDecoded; }
|
2016-07-19 09:26:58 +03:00
|
|
|
|
2017-08-10 05:26:55 +03:00
|
|
|
/**
|
|
|
|
* Returns true if this image has ever requested a decode before.
|
|
|
|
*/
|
|
|
|
bool GetHasRequestedDecode() { return mHasRequestedDecode; }
|
|
|
|
|
2017-03-16 11:06:04 +03:00
|
|
|
/**
|
|
|
|
* Returns true if this image has been discarded and a decoded has not yet
|
|
|
|
* been created to redecode it.
|
|
|
|
*/
|
|
|
|
bool IsDiscarded() { return mDiscarded; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the composited frame as valid or invalid.
|
|
|
|
*/
|
|
|
|
void SetCompositedFrameInvalid(bool aInvalid) {
|
2019-07-22 05:10:14 +03:00
|
|
|
MOZ_ASSERT(!aInvalid ||
|
|
|
|
StaticPrefs::image_mem_animated_discardable_AtStartup());
|
2017-03-16 11:06:04 +03:00
|
|
|
mCompositedFrameInvalid = aInvalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the composited frame is valid to draw to the screen.
|
|
|
|
*/
|
|
|
|
bool GetCompositedFrameInvalid() { return mCompositedFrameInvalid; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the image is currently full decoded..
|
|
|
|
*/
|
|
|
|
bool GetIsCurrentlyDecoded() { return mIsCurrentlyDecoded; }
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2016-07-28 03:12:25 +03:00
|
|
|
/// Update the number of frames of animation this image is known to have.
|
|
|
|
void UpdateKnownFrameCount(uint32_t aFrameCount);
|
|
|
|
|
|
|
|
/// @return the number of frames of animation we know about so far.
|
|
|
|
uint32_t KnownFrameCount() const { return mFrameCount; }
|
|
|
|
|
|
|
|
/// @return the number of frames this animation has, if we know for sure.
|
|
|
|
/// (In other words, if decoding is finished.) Otherwise, returns Nothing().
|
|
|
|
Maybe<uint32_t> FrameCount() const;
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
/**
|
2016-07-20 09:35:41 +03:00
|
|
|
* Get or set the area of the image to invalidate when we loop around to the
|
|
|
|
* first frame.
|
2016-07-19 09:26:58 +03:00
|
|
|
*/
|
2016-07-20 09:35:41 +03:00
|
|
|
void SetFirstFrameRefreshArea(const gfx::IntRect& aRefreshArea);
|
|
|
|
gfx::IntRect FirstFrameRefreshArea() const { return mFirstFrameRefreshArea; }
|
2016-07-19 09:26:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
Bug 1454149 - Do not advance animated images which are not displayed. r=tnikkel
All animated images on a page are currently registered with the refresh
driver and advance with the tick refresh. These animations may not even
be in view, and if they are large and thus cause redecoding, cause a
marked increase in CPU usage for no benefit to the user.
This patch adds an additional flag, mCompositedFrameRequested, to the
AnimationState used by FrameAnimator. It is set to true each time the
current animated image frame is requested via
FrameAnimator::GetCompositedFrame. It is set to false each time the
frame is advanced in FrameAnimator::AdvanceFrame (via
FrameAnimator::RequestRefresh). If it is true when
FrameAnimator::RequestRefresh is called, then it will advance the
animation according to the normal rules. If it is false, then it will
set the current animation time to the current time instead of advancing.
This should not cause the animation to fall behind anymore or skip
frames more than it does today. This is because if
FrameAnimator::GetCompositedFrame is not called, then the internal state
of the animation is advancing ahead of what the user sees. Once it is
called, the new frame is far ahead of the previously displayed frame.
The only difference now is that we will display the previous frame for
slightly longer until the next refresh tick.
Note that if an animated image is layerized (should not happen today) or
otherwise uses an ImageContainer, this optimization fails. While we know
whether or not we have an image container, we do not know if anything is
actively using it.
2018-05-09 15:04:20 +03:00
|
|
|
/**
|
|
|
|
* Set the animation frame time to @aTime if we are configured to stop the
|
|
|
|
* animation when not visible and aTime is later than the current time.
|
|
|
|
* Returns true if the time was updated, else false.
|
|
|
|
*/
|
|
|
|
bool MaybeAdvanceAnimationFrameTime(const TimeStamp& aTime);
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
/**
|
|
|
|
* The current frame we're on, from 0 to (numFrames - 1).
|
|
|
|
*/
|
|
|
|
uint32_t GetCurrentAnimationFrameIndex() 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; }
|
|
|
|
|
2016-07-20 03:14:30 +03:00
|
|
|
/// Set the @aLength of a single loop through this image.
|
|
|
|
void SetLoopLength(FrameTimeout aLength) { mLoopLength = Some(aLength); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the length of a single loop of this image. If this image is not
|
|
|
|
* finished decoding, is not animated, or it is animated but does not loop,
|
|
|
|
* returns FrameTimeout::Forever().
|
|
|
|
*/
|
|
|
|
FrameTimeout LoopLength() const;
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
/*
|
2016-07-19 23:51:01 +03:00
|
|
|
* Get or set the timeout for the first frame. This is used to allow animation
|
2016-07-19 09:26:58 +03:00
|
|
|
* scheduling even before a full decode runs for this image.
|
|
|
|
*/
|
2016-07-20 02:22:34 +03:00
|
|
|
void SetFirstFrameTimeout(FrameTimeout aTimeout) {
|
|
|
|
mFirstFrameTimeout = aTimeout;
|
|
|
|
}
|
2016-07-19 23:51:01 +03:00
|
|
|
FrameTimeout FirstFrameTimeout() const { return mFirstFrameTimeout; }
|
2016-07-19 09:26:58 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class FrameAnimator;
|
|
|
|
|
|
|
|
//! Area of the first frame that needs to be redrawn on subsequent loops.
|
2016-07-20 09:43:52 +03:00
|
|
|
gfx::IntRect mFirstFrameRefreshArea;
|
2016-07-19 09:26:58 +03:00
|
|
|
|
|
|
|
//! the time that the animation advanced to the current frame
|
|
|
|
TimeStamp mCurrentAnimationFrameTime;
|
|
|
|
|
2016-07-28 03:12:25 +03:00
|
|
|
//! The number of frames of animation this image has.
|
|
|
|
uint32_t mFrameCount;
|
|
|
|
|
|
|
|
//! The current frame index we're on, in the range [0, mFrameCount).
|
2016-07-19 09:26:58 +03:00
|
|
|
uint32_t mCurrentAnimationFrameIndex;
|
|
|
|
|
|
|
|
//! number of loops remaining before animation stops (-1 no stop)
|
|
|
|
int32_t mLoopRemainingCount;
|
|
|
|
|
|
|
|
//! The total number of loops for the image.
|
|
|
|
int32_t mLoopCount;
|
|
|
|
|
2016-07-20 03:14:30 +03:00
|
|
|
//! The length of a single loop through this image.
|
|
|
|
Maybe<FrameTimeout> mLoopLength;
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
//! The timeout for the first frame of this image.
|
2016-07-20 02:22:34 +03:00
|
|
|
FrameTimeout mFirstFrameTimeout;
|
2016-07-19 09:26:58 +03:00
|
|
|
|
|
|
|
//! The animation mode of this image. Constants defined in imgIContainer.
|
|
|
|
uint16_t mAnimationMode;
|
|
|
|
|
2017-03-16 11:06:04 +03:00
|
|
|
/**
|
|
|
|
* The following four bools (mHasBeenDecoded, mIsCurrentlyDecoded,
|
|
|
|
* mCompositedFrameInvalid, mDiscarded) track the state of the image with
|
|
|
|
* regards to decoding. They all start out false, including mDiscarded,
|
|
|
|
* because we want to treat being discarded differently from "not yet decoded
|
|
|
|
* for the first time".
|
|
|
|
*
|
|
|
|
* (When we are decoding the image for the first time we want to show the
|
|
|
|
* image at the speed of data coming in from the network or the speed
|
|
|
|
* specified in the image file, whichever is slower. But when redecoding we
|
|
|
|
* want to show nothing until the frame for the current time has been
|
|
|
|
* decoded. The prevents the user from seeing the image "fast forward"
|
|
|
|
* to the expected spot.)
|
|
|
|
*
|
|
|
|
* When the image is decoded for the first time mHasBeenDecoded and
|
|
|
|
* mIsCurrentlyDecoded get set to true. When the image is discarded
|
|
|
|
* mIsCurrentlyDecoded gets set to false, and mCompositedFrameInvalid
|
|
|
|
* & mDiscarded get set to true. When we create a decoder to redecode the
|
|
|
|
* image mDiscarded gets set to false. mCompositedFrameInvalid gets set to
|
|
|
|
* false when we are able to advance to the frame that should be showing
|
|
|
|
* for the current time. mIsCurrentlyDecoded gets set to true when the
|
|
|
|
* redecode finishes.
|
|
|
|
*/
|
|
|
|
|
2017-03-02 07:45:54 +03:00
|
|
|
//! Whether this image has been decoded at least once.
|
|
|
|
bool mHasBeenDecoded;
|
2017-03-16 11:06:04 +03:00
|
|
|
|
2017-08-10 05:26:55 +03:00
|
|
|
//! Whether this image has ever requested a decode.
|
|
|
|
bool mHasRequestedDecode;
|
|
|
|
|
2017-03-16 11:06:04 +03:00
|
|
|
//! Whether this image is currently fully decoded.
|
|
|
|
bool mIsCurrentlyDecoded;
|
|
|
|
|
|
|
|
//! Whether the composited frame is valid to draw to the screen, note that
|
|
|
|
//! the composited frame can exist and be filled with image data but not
|
|
|
|
//! valid to draw to the screen.
|
|
|
|
bool mCompositedFrameInvalid;
|
|
|
|
|
|
|
|
//! Whether this image is currently discarded. Only set to true after the
|
|
|
|
//! image has been decoded at least once.
|
|
|
|
bool mDiscarded;
|
2016-07-19 09:26:58 +03:00
|
|
|
};
|
|
|
|
|
2016-07-20 04:20:13 +03:00
|
|
|
/**
|
|
|
|
* RefreshResult is used to let callers know how the state of the animation
|
|
|
|
* changed during a call to FrameAnimator::RequestRefresh().
|
|
|
|
*/
|
|
|
|
struct RefreshResult {
|
|
|
|
RefreshResult() : mFrameAdvanced(false), mAnimationFinished(false) {}
|
|
|
|
|
|
|
|
/// Merges another RefreshResult's changes into this RefreshResult.
|
|
|
|
void Accumulate(const RefreshResult& aOther) {
|
|
|
|
mFrameAdvanced = mFrameAdvanced || aOther.mFrameAdvanced;
|
|
|
|
mAnimationFinished = mAnimationFinished || aOther.mAnimationFinished;
|
|
|
|
mDirtyRect = mDirtyRect.Union(aOther.mDirtyRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The region of the image that has changed.
|
|
|
|
gfx::IntRect mDirtyRect;
|
|
|
|
|
|
|
|
// If true, we changed frames at least once. Note that, due to looping, we
|
|
|
|
// could still have ended up on the same frame!
|
|
|
|
bool mFrameAdvanced : 1;
|
|
|
|
|
|
|
|
// Whether the animation has finished playing.
|
|
|
|
bool mAnimationFinished : 1;
|
|
|
|
};
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
class FrameAnimator {
|
|
|
|
public:
|
2016-07-20 09:43:52 +03:00
|
|
|
FrameAnimator(RasterImage* aImage, const gfx::IntSize& aSize)
|
2019-03-11 20:20:49 +03:00
|
|
|
: mImage(aImage), mSize(aSize) {
|
2016-01-26 22:08:26 +03:00
|
|
|
MOZ_COUNT_CTOR(FrameAnimator);
|
|
|
|
}
|
|
|
|
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR(FrameAnimator)
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2018-02-28 21:34:52 +03:00
|
|
|
/**
|
|
|
|
* Call when you need to re-start animating. Ensures we start from the first
|
|
|
|
* frame.
|
|
|
|
*/
|
|
|
|
void ResetAnimation(AnimationState& aState);
|
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-04-20 06:38:27 +03:00
|
|
|
RefreshResult RequestRefresh(AnimationState& aState, const TimeStamp& aTime);
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
/**
|
2016-11-29 10:01:43 +03:00
|
|
|
* Get the full frame for the current frame of the animation (it may or may
|
|
|
|
* not have required compositing). It may not be available because it hasn't
|
|
|
|
* been decoded yet, in which case we return an empty LookupResult.
|
2015-01-08 00:07:23 +03:00
|
|
|
*/
|
2018-10-13 08:31:02 +03:00
|
|
|
LookupResult GetCompositedFrame(AnimationState& aState, bool aMarkUsed);
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
private: // methods
|
|
|
|
/**
|
|
|
|
* 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().
|
|
|
|
*
|
2018-05-29 15:36:12 +03:00
|
|
|
* @param aCurrentFrame the currently displayed frame of the animation. If
|
|
|
|
* we advance, it will replace aCurrentFrame with the
|
|
|
|
* new current frame we advanced to.
|
|
|
|
*
|
2013-07-15 22:38:59 +04:00
|
|
|
* @returns a RefreshResult that shows whether the frame was successfully
|
|
|
|
* advanced, and its resulting dirty rect.
|
|
|
|
*/
|
2017-03-24 08:57:30 +03:00
|
|
|
RefreshResult AdvanceFrame(AnimationState& aState, DrawableSurface& aFrames,
|
2018-06-04 01:49:23 +03:00
|
|
|
RefPtr<imgFrame>& aCurrentFrame, TimeStamp aTime);
|
2016-07-19 09:26:58 +03:00
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
/**
|
|
|
|
* Get the time the frame we're currently displaying is supposed to end.
|
|
|
|
*
|
2017-03-23 08:02:54 +03:00
|
|
|
* In the error case (like if the requested frame is not currently
|
|
|
|
* decoded), returns None().
|
2013-07-15 22:38:59 +04:00
|
|
|
*/
|
2018-05-29 15:36:12 +03:00
|
|
|
TimeStamp GetCurrentImgFrameEndTime(AnimationState& aState,
|
|
|
|
FrameTimeout aCurrentTimeout) const;
|
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;
|
2013-07-15 22:38:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#endif // mozilla_image_FrameAnimator_h
|