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/. */
|
|
|
|
|
|
|
|
#include "FrameAnimator.h"
|
|
|
|
|
2020-01-20 19:18:20 +03:00
|
|
|
#include <utility>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
|
2015-07-01 04:57:03 +03:00
|
|
|
#include "LookupResult.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
#include "RasterImage.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "mozilla/CheckedInt.h"
|
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2014-07-10 19:00:31 +04:00
|
|
|
namespace mozilla {
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
2014-07-10 19:00:31 +04:00
|
|
|
namespace image {
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AnimationState implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
const gfx::IntRect AnimationState::UpdateState(
|
2020-04-20 06:38:27 +03:00
|
|
|
RasterImage* aImage, const gfx::IntSize& aSize,
|
2017-06-01 10:19:55 +03:00
|
|
|
bool aAllowInvalidation /* = true */) {
|
2017-03-26 08:04:53 +03:00
|
|
|
LookupResult result = SurfaceCache::Lookup(
|
|
|
|
ImageKey(aImage),
|
2018-10-13 08:31:02 +03:00
|
|
|
RasterSurfaceKey(aSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
|
|
|
|
/* aMarkUsed = */ false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-20 06:38:27 +03:00
|
|
|
return UpdateStateInternal(result, aSize, aAllowInvalidation);
|
2017-03-26 08:04:53 +03:00
|
|
|
}
|
2017-03-17 08:41:44 +03:00
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
const gfx::IntRect AnimationState::UpdateStateInternal(
|
2020-04-20 06:38:27 +03:00
|
|
|
LookupResult& aResult, const gfx::IntSize& aSize,
|
2017-06-01 10:19:55 +03:00
|
|
|
bool aAllowInvalidation /* = true */) {
|
2017-03-26 08:04:53 +03:00
|
|
|
// Update mDiscarded and mIsCurrentlyDecoded.
|
|
|
|
if (aResult.Type() == MatchType::NOT_FOUND) {
|
|
|
|
// no frames, we've either been discarded, or never been decoded before.
|
|
|
|
mDiscarded = mHasBeenDecoded;
|
|
|
|
mIsCurrentlyDecoded = false;
|
|
|
|
} else if (aResult.Type() == MatchType::PENDING) {
|
|
|
|
// no frames yet, but a decoder is or will be working on it.
|
|
|
|
mDiscarded = false;
|
|
|
|
mIsCurrentlyDecoded = false;
|
2017-08-10 05:26:55 +03:00
|
|
|
mHasRequestedDecode = true;
|
2017-03-26 08:04:53 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aResult.Type() == MatchType::EXACT);
|
|
|
|
mDiscarded = false;
|
2017-08-10 05:26:55 +03:00
|
|
|
mHasRequestedDecode = true;
|
2017-03-26 08:04:53 +03:00
|
|
|
|
2020-05-04 21:43:32 +03:00
|
|
|
// If we can seek to the current animation frame we consider it decoded.
|
|
|
|
// Animated images are never fully decoded unless very short.
|
2020-11-13 06:25:33 +03:00
|
|
|
// Note that we use GetFrame instead of Seek here. The difference is that
|
|
|
|
// Seek eventually calls AnimationFrameBuffer::Get with aForDisplay == true,
|
|
|
|
// whereas GetFrame calls AnimationFrameBuffer::Get with aForDisplay ==
|
|
|
|
// false. The aForDisplay can change whether those functions succeed or not
|
|
|
|
// (only for the first frame). Since this is not for display we want to pass
|
|
|
|
// aForDisplay == false, but also for consistency with
|
|
|
|
// RequestRefresh/AdvanceFrame, because we want our state to be in sync with
|
|
|
|
// those functions. The user of Seek (GetCompositedFrame) doesn't need to be
|
|
|
|
// in sync with our state.
|
|
|
|
RefPtr<imgFrame> currentFrame =
|
|
|
|
bool(aResult.Surface())
|
|
|
|
? aResult.Surface().GetFrame(mCurrentAnimationFrameIndex)
|
|
|
|
: nullptr;
|
|
|
|
mIsCurrentlyDecoded = !!currentFrame;
|
2017-03-26 08:04:53 +03:00
|
|
|
}
|
|
|
|
|
2017-04-06 12:00:36 +03:00
|
|
|
gfx::IntRect ret;
|
|
|
|
|
2017-06-01 10:19:55 +03:00
|
|
|
if (aAllowInvalidation) {
|
|
|
|
// Update the value of mCompositedFrameInvalid.
|
2020-04-20 06:38:27 +03:00
|
|
|
if (mIsCurrentlyDecoded) {
|
|
|
|
// It is safe to clear mCompositedFrameInvalid safe to do for images that
|
|
|
|
// are fully decoded but aren't finished animating because before we paint
|
|
|
|
// the refresh driver will call into us to advance to the correct frame,
|
|
|
|
// and that will succeed because we have all the frames.
|
2017-06-01 10:19:55 +03:00
|
|
|
if (mCompositedFrameInvalid) {
|
|
|
|
// Invalidate if we are marking the composited frame valid.
|
|
|
|
ret.SizeTo(aSize);
|
|
|
|
}
|
|
|
|
mCompositedFrameInvalid = false;
|
2020-11-13 06:25:33 +03:00
|
|
|
} else {
|
2017-08-10 05:26:55 +03:00
|
|
|
if (mHasRequestedDecode) {
|
2019-07-22 05:10:14 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
|
2017-06-01 10:19:55 +03:00
|
|
|
mCompositedFrameInvalid = true;
|
|
|
|
}
|
2017-03-26 08:04:53 +03:00
|
|
|
}
|
2017-06-01 10:19:55 +03:00
|
|
|
// Otherwise don't change the value of mCompositedFrameInvalid, it will be
|
|
|
|
// updated by RequestRefresh.
|
2017-03-16 11:06:04 +03:00
|
|
|
}
|
2017-04-06 12:00:36 +03:00
|
|
|
|
|
|
|
return ret;
|
2016-07-19 09:26:58 +03:00
|
|
|
}
|
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
void AnimationState::NotifyDecodeComplete() { mHasBeenDecoded = true; }
|
2017-03-16 11:06:04 +03:00
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
void AnimationState::ResetAnimation() { mCurrentAnimationFrameIndex = 0; }
|
|
|
|
|
|
|
|
void AnimationState::SetAnimationMode(uint16_t aAnimationMode) {
|
|
|
|
mAnimationMode = aAnimationMode;
|
|
|
|
}
|
|
|
|
|
2016-07-28 03:12:25 +03:00
|
|
|
void AnimationState::UpdateKnownFrameCount(uint32_t aFrameCount) {
|
|
|
|
if (aFrameCount <= mFrameCount) {
|
|
|
|
// Nothing to do. Since we can redecode animated images, we may see the same
|
|
|
|
// sequence of updates replayed again, so seeing a smaller frame count than
|
|
|
|
// what we already know about doesn't indicate an error.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-02 07:45:54 +03:00
|
|
|
MOZ_ASSERT(!mHasBeenDecoded, "Adding new frames after decoding is finished?");
|
2016-07-28 03:12:25 +03:00
|
|
|
MOZ_ASSERT(aFrameCount <= mFrameCount + 1, "Skipped a frame?");
|
|
|
|
|
|
|
|
mFrameCount = aFrameCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<uint32_t> AnimationState::FrameCount() const {
|
2017-03-02 07:45:54 +03:00
|
|
|
return mHasBeenDecoded ? Some(mFrameCount) : Nothing();
|
2016-07-28 03:12:25 +03:00
|
|
|
}
|
|
|
|
|
2016-07-20 09:43:52 +03:00
|
|
|
void AnimationState::SetFirstFrameRefreshArea(const IntRect& aRefreshArea) {
|
2016-07-20 09:35:41 +03:00
|
|
|
mFirstFrameRefreshArea = aRefreshArea;
|
2016-07-19 09:26:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationState::InitAnimationFrameTimeIfNecessary() {
|
|
|
|
if (mCurrentAnimationFrameTime.IsNull()) {
|
|
|
|
mCurrentAnimationFrameTime = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationState::SetAnimationFrameTime(const TimeStamp& aTime) {
|
|
|
|
mCurrentAnimationFrameTime = 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
|
|
|
bool AnimationState::MaybeAdvanceAnimationFrameTime(const TimeStamp& aTime) {
|
2019-06-26 06:24:21 +03:00
|
|
|
if (!StaticPrefs::image_animated_resume_from_last_displayed() ||
|
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
|
|
|
mCurrentAnimationFrameTime >= aTime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are configured to stop an animation when it is out of view, and restart
|
|
|
|
// it from the same point when it comes back into view. The same applies if it
|
|
|
|
// was discarded while out of view.
|
|
|
|
mCurrentAnimationFrameTime = aTime;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
uint32_t AnimationState::GetCurrentAnimationFrameIndex() const {
|
|
|
|
return mCurrentAnimationFrameIndex;
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:14:30 +03:00
|
|
|
FrameTimeout AnimationState::LoopLength() const {
|
|
|
|
// If we don't know the loop length yet, we have to treat it as infinite.
|
|
|
|
if (!mLoopLength) {
|
2016-07-20 01:48:37 +03:00
|
|
|
return FrameTimeout::Forever();
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
2017-03-02 07:45:54 +03:00
|
|
|
MOZ_ASSERT(mHasBeenDecoded,
|
|
|
|
"We know the loop length but decoding isn't done?");
|
2016-07-20 03:14:30 +03:00
|
|
|
|
2016-07-20 01:48:37 +03:00
|
|
|
// If we're not looping, a single loop time has no meaning.
|
2016-07-20 03:14:30 +03:00
|
|
|
if (mAnimationMode != imgIContainer::kNormalAnimMode) {
|
2016-07-20 01:48:37 +03:00
|
|
|
return FrameTimeout::Forever();
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
2016-07-20 03:14:30 +03:00
|
|
|
return *mLoopLength;
|
|
|
|
}
|
2016-07-20 02:22:34 +03:00
|
|
|
|
2016-07-20 03:14:30 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// FrameAnimator implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2017-03-24 08:57:30 +03:00
|
|
|
TimeStamp FrameAnimator::GetCurrentImgFrameEndTime(
|
2018-05-29 15:36:12 +03:00
|
|
|
AnimationState& aState, FrameTimeout aCurrentTimeout) const {
|
|
|
|
if (aCurrentTimeout == FrameTimeout::Forever()) {
|
2013-07-15 22:38:59 +04:00
|
|
|
// We need to return a sentinel value in this case, because our logic
|
2016-07-20 02:22:34 +03:00
|
|
|
// doesn't work correctly if we have an infinitely long timeout. We use one
|
|
|
|
// year in the future as the sentinel because it works with the loop in
|
|
|
|
// RequestRefresh() below.
|
2013-09-20 16:12:10 +04:00
|
|
|
// XXX(seth): It'd be preferable to make our logic work correctly with
|
2016-07-20 02:22:34 +03:00
|
|
|
// infinitely long timeouts.
|
2018-05-29 15:36:12 +03:00
|
|
|
return TimeStamp::NowLoRes() + TimeDuration::FromMilliseconds(31536000.0);
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TimeDuration durationOfTimeout =
|
2018-05-29 15:36:12 +03:00
|
|
|
TimeDuration::FromMilliseconds(double(aCurrentTimeout.AsMilliseconds()));
|
|
|
|
return aState.mCurrentAnimationFrameTime + durationOfTimeout;
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
2017-03-24 08:57:30 +03:00
|
|
|
RefreshResult FrameAnimator::AdvanceFrame(AnimationState& aState,
|
|
|
|
DrawableSurface& aFrames,
|
2018-06-04 01:49:23 +03:00
|
|
|
RefPtr<imgFrame>& aCurrentFrame,
|
2017-03-24 08:57:30 +03:00
|
|
|
TimeStamp aTime) {
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL("FrameAnimator::AdvanceFrame", GRAPHICS);
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
RefreshResult ret;
|
|
|
|
|
2016-03-03 06:52:36 +03:00
|
|
|
// Determine what the next frame is, taking into account looping.
|
2016-07-19 09:26:58 +03:00
|
|
|
uint32_t currentFrameIndex = aState.mCurrentAnimationFrameIndex;
|
2016-03-03 06:52:36 +03:00
|
|
|
uint32_t nextFrameIndex = currentFrameIndex + 1;
|
2013-12-13 02:17:41 +04:00
|
|
|
|
2016-07-28 03:12:25 +03:00
|
|
|
// Check if we're at the end of the loop. (FrameCount() returns Nothing() if
|
|
|
|
// we don't know the total count yet.)
|
|
|
|
if (aState.FrameCount() == Some(nextFrameIndex)) {
|
2013-12-13 02:17:41 +04:00
|
|
|
// If we are not looping forever, initialize the loop counter
|
2016-07-19 09:26:58 +03:00
|
|
|
if (aState.mLoopRemainingCount < 0 && aState.LoopCount() >= 0) {
|
|
|
|
aState.mLoopRemainingCount = aState.LoopCount();
|
2013-12-13 02:17:41 +04:00
|
|
|
}
|
|
|
|
|
2014-12-15 02:54:00 +03:00
|
|
|
// If animation mode is "loop once", or we're at end of loop counter,
|
2015-01-08 00:07:23 +03:00
|
|
|
// it's time to stop animating.
|
2016-07-19 09:26:58 +03:00
|
|
|
if (aState.mAnimationMode == imgIContainer::kLoopOnceAnimMode ||
|
|
|
|
aState.mLoopRemainingCount == 0) {
|
2016-07-20 04:20:13 +03:00
|
|
|
ret.mAnimationFinished = true;
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 02:17:41 +04:00
|
|
|
nextFrameIndex = 0;
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
if (aState.mLoopRemainingCount > 0) {
|
|
|
|
aState.mLoopRemainingCount--;
|
2013-12-13 02:17:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're done, exit early.
|
2016-07-20 04:20:13 +03:00
|
|
|
if (ret.mAnimationFinished) {
|
2013-12-13 02:17:41 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
2016-07-28 03:12:25 +03:00
|
|
|
if (nextFrameIndex >= aState.KnownFrameCount()) {
|
|
|
|
// We've already advanced to the last decoded frame, nothing more we can do.
|
|
|
|
// We're blocked by network/decoding from displaying the animation at the
|
|
|
|
// rate specified, so that means the frame we are displaying (the latest
|
|
|
|
// available) is the frame we want to be displaying at this time. So we
|
|
|
|
// update the current animation time. If we didn't update the current
|
|
|
|
// animation time then it could lag behind, which would indicate that we are
|
|
|
|
// behind in the animation and should try to catch up. When we are done
|
|
|
|
// decoding (and thus can loop around back to the start of the animation) we
|
|
|
|
// would then jump to a random point in the animation to try to catch up.
|
|
|
|
// But we were never behind in the animation.
|
|
|
|
aState.mCurrentAnimationFrameTime = aTime;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There can be frames in the surface cache with index >= KnownFrameCount()
|
|
|
|
// which GetRawFrame() can access because an async decoder has decoded them,
|
|
|
|
// but which AnimationState doesn't know about yet because we haven't received
|
|
|
|
// the appropriate notification on the main thread. Make sure we stay in sync
|
|
|
|
// with AnimationState.
|
|
|
|
MOZ_ASSERT(nextFrameIndex < aState.KnownFrameCount());
|
2018-06-04 01:49:23 +03:00
|
|
|
RefPtr<imgFrame> nextFrame = aFrames.GetFrame(nextFrameIndex);
|
2016-03-03 06:52:36 +03:00
|
|
|
|
2016-09-21 19:55:26 +03:00
|
|
|
// We should always check to see if we have the next frame even if we have
|
|
|
|
// previously finished decoding. If we needed to redecode (e.g. due to a draw
|
|
|
|
// failure) we would have discarded all the old frames and may not yet have
|
2018-05-29 15:36:12 +03:00
|
|
|
// the new ones. DrawableSurface::RawAccessRef promises to only return
|
|
|
|
// finished frames.
|
|
|
|
if (!nextFrame) {
|
2018-02-28 21:34:52 +03:00
|
|
|
// Uh oh, the frame we want to show is currently being decoded (partial).
|
|
|
|
// Similar to the above case, we could be blocked by network or decoding,
|
|
|
|
// and so we should advance our current time rather than risk jumping
|
|
|
|
// through the animation. We will wait until the next refresh driver tick
|
|
|
|
// and try again.
|
|
|
|
aState.mCurrentAnimationFrameTime = aTime;
|
2016-03-03 06:52:36 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
if (nextFrame->GetTimeout() == FrameTimeout::Forever()) {
|
2016-07-20 04:20:13 +03:00
|
|
|
ret.mAnimationFinished = true;
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nextFrameIndex == 0) {
|
2016-07-20 04:20:13 +03:00
|
|
|
ret.mDirtyRect = aState.FirstFrameRefreshArea();
|
2018-09-17 22:06:29 +03:00
|
|
|
} else {
|
|
|
|
ret.mDirtyRect = nextFrame->GetDirtyRect();
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
aState.mCurrentAnimationFrameTime =
|
|
|
|
GetCurrentImgFrameEndTime(aState, aCurrentFrame->GetTimeout());
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
// If we can get closer to the current time by a multiple of the image's loop
|
2016-07-20 03:14:30 +03:00
|
|
|
// time, we should. We can only do this if we're done decoding; otherwise, we
|
|
|
|
// don't know the full loop length, and LoopLength() will have to return
|
2017-05-25 00:20:41 +03:00
|
|
|
// FrameTimeout::Forever(). We also skip this for images with a finite loop
|
|
|
|
// count if we have initialized mLoopRemainingCount (it only gets initialized
|
|
|
|
// after one full loop).
|
2016-07-20 03:14:30 +03:00
|
|
|
FrameTimeout loopTime = aState.LoopLength();
|
2017-05-25 00:20:41 +03:00
|
|
|
if (loopTime != FrameTimeout::Forever() &&
|
|
|
|
(aState.LoopCount() < 0 || aState.mLoopRemainingCount >= 0)) {
|
2016-07-19 09:26:58 +03:00
|
|
|
TimeDuration delay = aTime - aState.mCurrentAnimationFrameTime;
|
2016-07-20 01:48:37 +03:00
|
|
|
if (delay.ToMilliseconds() > loopTime.AsMilliseconds()) {
|
2013-07-15 22:38:59 +04:00
|
|
|
// Explicitly use integer division to get the floor of the number of
|
|
|
|
// loops.
|
2016-07-20 01:48:37 +03:00
|
|
|
uint64_t loops = static_cast<uint64_t>(delay.ToMilliseconds()) /
|
|
|
|
loopTime.AsMilliseconds();
|
2017-05-25 00:20:41 +03:00
|
|
|
|
|
|
|
// If we have a finite loop count limit the number of loops we advance.
|
|
|
|
if (aState.mLoopRemainingCount >= 0) {
|
|
|
|
MOZ_ASSERT(aState.LoopCount() >= 0);
|
|
|
|
loops =
|
|
|
|
std::min(loops, CheckedUint64(aState.mLoopRemainingCount).value());
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:26:58 +03:00
|
|
|
aState.mCurrentAnimationFrameTime +=
|
2016-07-20 01:48:37 +03:00
|
|
|
TimeDuration::FromMilliseconds(loops * loopTime.AsMilliseconds());
|
2017-05-25 00:20:41 +03:00
|
|
|
|
|
|
|
if (aState.mLoopRemainingCount >= 0) {
|
|
|
|
MOZ_ASSERT(loops <= CheckedUint64(aState.mLoopRemainingCount).value());
|
|
|
|
aState.mLoopRemainingCount -= CheckedInt32(loops).value();
|
|
|
|
}
|
2013-07-15 22:38:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set currentAnimationFrameIndex at the last possible moment
|
2016-07-19 09:26:58 +03:00
|
|
|
aState.mCurrentAnimationFrameIndex = nextFrameIndex;
|
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
|
|
|
aState.mCompositedFrameRequested = false;
|
2018-05-30 22:15:35 +03:00
|
|
|
aCurrentFrame = std::move(nextFrame);
|
2018-02-28 21:34:52 +03:00
|
|
|
aFrames.Advance(nextFrameIndex);
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
// If we're here, we successfully advanced the frame.
|
2016-07-20 04:20:13 +03:00
|
|
|
ret.mFrameAdvanced = true;
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:34:52 +03:00
|
|
|
void FrameAnimator::ResetAnimation(AnimationState& aState) {
|
|
|
|
aState.ResetAnimation();
|
|
|
|
|
|
|
|
// Our surface provider is synchronized to our state, so we need to reset its
|
|
|
|
// state as well, if we still have one.
|
|
|
|
LookupResult result = SurfaceCache::Lookup(
|
|
|
|
ImageKey(mImage),
|
2018-10-13 08:31:02 +03:00
|
|
|
RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
|
|
|
|
/* aMarkUsed = */ false);
|
2018-02-28 21:34:52 +03:00
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Surface().Reset();
|
2020-11-13 06:25:32 +03:00
|
|
|
|
|
|
|
// Calling Reset on the surface of the animation can cause discarding surface
|
|
|
|
// providers to throw out all their frames so refresh our state.
|
|
|
|
aState.UpdateStateInternal(result, mSize);
|
2018-02-28 21:34:52 +03:00
|
|
|
}
|
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
RefreshResult FrameAnimator::RequestRefresh(AnimationState& aState,
|
2020-04-20 06:38:27 +03:00
|
|
|
const TimeStamp& aTime) {
|
2017-03-16 11:06:04 +03:00
|
|
|
// By default, an empty RefreshResult.
|
|
|
|
RefreshResult ret;
|
|
|
|
|
|
|
|
if (aState.IsDiscarded()) {
|
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
|
|
|
aState.MaybeAdvanceAnimationFrameTime(aTime);
|
2017-03-16 11:06:04 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-24 08:57:30 +03:00
|
|
|
// Get the animation frames once now, and pass them down to callees because
|
|
|
|
// the surface could be discarded at anytime on a different thread. This is
|
|
|
|
// must easier to reason about then trying to write code that is safe to
|
|
|
|
// having the surface disappear at anytime.
|
|
|
|
LookupResult result = SurfaceCache::Lookup(
|
|
|
|
ImageKey(mImage),
|
2018-10-13 08:31:02 +03:00
|
|
|
RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
|
|
|
|
/* aMarkUsed = */ true);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-20 06:38:27 +03:00
|
|
|
ret.mDirtyRect = aState.UpdateStateInternal(result, mSize);
|
2017-03-26 08:04:53 +03:00
|
|
|
if (aState.IsDiscarded() || !result) {
|
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
|
|
|
aState.MaybeAdvanceAnimationFrameTime(aTime);
|
2017-03-24 08:57:30 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-04 01:49:23 +03:00
|
|
|
RefPtr<imgFrame> currentFrame =
|
|
|
|
result.Surface().GetFrame(aState.mCurrentAnimationFrameIndex);
|
2018-05-29 15:36:12 +03:00
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
// only advance the frame if the current time is greater than or
|
|
|
|
// equal to the current frame's end time.
|
2018-05-29 15:36:12 +03:00
|
|
|
if (!currentFrame) {
|
2019-07-22 05:10:14 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
|
2017-08-10 05:26:55 +03:00
|
|
|
MOZ_ASSERT(aState.GetHasRequestedDecode() &&
|
|
|
|
!aState.GetIsCurrentlyDecoded());
|
2017-03-23 08:02:54 +03:00
|
|
|
MOZ_ASSERT(aState.mCompositedFrameInvalid);
|
|
|
|
// Nothing we can do but wait for our previous current frame to be decoded
|
|
|
|
// again so we can determine what to do next.
|
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
|
|
|
aState.MaybeAdvanceAnimationFrameTime(aTime);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
TimeStamp currentFrameEndTime =
|
|
|
|
GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
|
|
|
|
|
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
|
|
|
// If nothing has accessed the composited frame since the last time we
|
|
|
|
// advanced, then there is no point in continuing to advance the animation.
|
|
|
|
// This has the effect of freezing the animation while not in view.
|
|
|
|
if (!aState.mCompositedFrameRequested &&
|
|
|
|
aState.MaybeAdvanceAnimationFrameTime(aTime)) {
|
2017-03-23 08:02:54 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
while (currentFrameEndTime <= aTime) {
|
|
|
|
TimeStamp oldFrameEndTime = currentFrameEndTime;
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
RefreshResult frameRes =
|
|
|
|
AdvanceFrame(aState, result.Surface(), currentFrame, aTime);
|
2013-07-15 22:38:59 +04:00
|
|
|
|
|
|
|
// Accumulate our result for returning to callers.
|
|
|
|
ret.Accumulate(frameRes);
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
// currentFrame was updated by AdvanceFrame so it is still current.
|
|
|
|
currentFrameEndTime =
|
|
|
|
GetCurrentImgFrameEndTime(aState, currentFrame->GetTimeout());
|
2013-07-15 22:38:59 +04:00
|
|
|
|
2016-07-20 04:20:13 +03:00
|
|
|
// If we didn't advance a frame, and our frame end time didn't change,
|
2013-07-15 22:38:59 +04:00
|
|
|
// then we need to break out of this loop & wait for the frame(s)
|
2016-07-20 04:20:13 +03:00
|
|
|
// to finish downloading.
|
2018-05-29 15:36:12 +03:00
|
|
|
if (!frameRes.mFrameAdvanced && currentFrameEndTime == oldFrameEndTime) {
|
2013-07-15 22:38:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:06:29 +03:00
|
|
|
// We should only mark the composited frame as valid and reset the dirty rect
|
|
|
|
// if we advanced (meaning the next frame was actually produced somehow), the
|
|
|
|
// composited frame was previously invalid (so we may need to repaint
|
2018-11-20 05:05:54 +03:00
|
|
|
// everything) and either the frame index is valid (to know we were doing
|
|
|
|
// blending on the main thread, instead of on the decoder threads in advance),
|
|
|
|
// or the current frame is a full frame (blends off the main thread).
|
|
|
|
//
|
|
|
|
// If for some reason we forget to reset aState.mCompositedFrameInvalid, then
|
|
|
|
// GetCompositedFrame will fail, even if we have all the data available for
|
|
|
|
// display.
|
2019-03-11 20:20:49 +03:00
|
|
|
if (currentFrameEndTime > aTime && aState.mCompositedFrameInvalid) {
|
2017-03-16 11:06:04 +03:00
|
|
|
aState.mCompositedFrameInvalid = false;
|
2017-05-04 05:20:35 +03:00
|
|
|
ret.mDirtyRect = IntRect(IntPoint(0, 0), mSize);
|
2017-03-16 11:06:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!aState.mIsCurrentlyDecoded || !aState.mCompositedFrameInvalid);
|
|
|
|
|
2013-07-15 22:38:59 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-13 08:31:02 +03:00
|
|
|
LookupResult FrameAnimator::GetCompositedFrame(AnimationState& aState,
|
|
|
|
bool aMarkUsed) {
|
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
|
|
|
aState.mCompositedFrameRequested = true;
|
|
|
|
|
2017-05-04 09:25:10 +03:00
|
|
|
LookupResult result = SurfaceCache::Lookup(
|
|
|
|
ImageKey(mImage),
|
2018-10-13 08:31:02 +03:00
|
|
|
RasterSurfaceKey(mSize, DefaultSurfaceFlags(), PlaybackType::eAnimated),
|
|
|
|
aMarkUsed);
|
2017-05-04 09:25:10 +03:00
|
|
|
|
2017-03-16 11:06:04 +03:00
|
|
|
if (aState.mCompositedFrameInvalid) {
|
2019-07-22 05:10:14 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::image_mem_animated_discardable_AtStartup());
|
2017-08-10 05:26:55 +03:00
|
|
|
MOZ_ASSERT(aState.GetHasRequestedDecode());
|
2017-03-16 11:06:04 +03:00
|
|
|
MOZ_ASSERT(!aState.GetIsCurrentlyDecoded());
|
2020-05-04 21:43:32 +03:00
|
|
|
|
|
|
|
if (result.Type() == MatchType::EXACT) {
|
|
|
|
// If our composited frame is marked as invalid but our frames are in the
|
|
|
|
// surface cache we might just have not updated our internal state yet.
|
|
|
|
// This can happen if the image is not in a document so that
|
|
|
|
// RequestRefresh is not getting called to advance the frame.
|
|
|
|
// RequestRefresh would result in our composited frame getting marked as
|
|
|
|
// valid either at the end of RequestRefresh when we are able to advance
|
|
|
|
// to the current time or if advancing frames eventually causes us to
|
|
|
|
// decode all of the frames of the image resulting in DecodeComplete
|
|
|
|
// getting called which calls UpdateState. The reason we care about this
|
|
|
|
// is that img.decode promises won't resolve until GetCompositedFrame
|
|
|
|
// returns a frame.
|
|
|
|
UnorientedIntRect rect = UnorientedIntRect::FromUnknownRect(
|
|
|
|
aState.UpdateStateInternal(result, mSize));
|
|
|
|
|
|
|
|
if (!rect.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget = do_GetMainThread();
|
|
|
|
RefPtr<RasterImage> image = mImage;
|
|
|
|
nsCOMPtr<nsIRunnable> ev = NS_NewRunnableFunction(
|
|
|
|
"FrameAnimator::GetCompositedFrame",
|
|
|
|
[=]() -> void { image->NotifyProgress(NoProgress, rect); });
|
|
|
|
eventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's still invalid we have to return.
|
|
|
|
if (aState.mCompositedFrameInvalid) {
|
|
|
|
if (result.Type() == MatchType::NOT_FOUND) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return LookupResult(MatchType::PENDING);
|
2017-05-04 09:25:10 +03:00
|
|
|
}
|
2017-03-16 11:06:04 +03:00
|
|
|
}
|
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
// Otherwise return the raw frame. DoBlend is required to ensure that we only
|
|
|
|
// hit this case if the frame is not paletted and doesn't require compositing.
|
2016-08-18 10:06:41 +03:00
|
|
|
if (!result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the appropriate frame. If seeking fails, it means that we couldn't
|
|
|
|
// get the frame we're looking for; treat this as if the lookup failed.
|
2016-11-29 10:01:43 +03:00
|
|
|
if (NS_FAILED(result.Surface().Seek(aState.mCurrentAnimationFrameIndex))) {
|
2017-05-04 09:25:10 +03:00
|
|
|
if (result.Type() == MatchType::NOT_FOUND) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return LookupResult(MatchType::PENDING);
|
2016-08-18 10:06:41 +03:00
|
|
|
}
|
|
|
|
|
2015-07-01 04:57:03 +03:00
|
|
|
return result;
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
|
2014-07-10 19:00:31 +04:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|