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"
|
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/Move.h"
|
2017-05-25 00:20:41 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2013-07-15 22:38:59 +04:00
|
|
|
#include "imgIContainer.h"
|
2015-07-01 04:57:03 +03:00
|
|
|
#include "LookupResult.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "RasterImage.h"
|
2017-03-02 08:05:44 +03:00
|
|
|
#include "gfxPrefs.h"
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
#include "pixman.h"
|
2017-05-25 00:20:41 +03:00
|
|
|
#include <algorithm>
|
2013-07-15 22:38:59 +04: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(
|
2017-06-01 10:19:55 +03:00
|
|
|
bool aAnimationFinished, RasterImage* aImage, const gfx::IntSize& aSize,
|
|
|
|
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
|
|
|
|
2017-06-19 23:21:53 +03:00
|
|
|
return UpdateStateInternal(result, aAnimationFinished, 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(
|
2017-06-01 10:19:55 +03:00
|
|
|
LookupResult& aResult, bool aAnimationFinished, const gfx::IntSize& aSize,
|
|
|
|
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
|
|
|
|
|
|
|
// If mHasBeenDecoded is true then we know the true total frame count and
|
|
|
|
// we can use it to determine if we have all the frames now so we know if
|
|
|
|
// we are currently fully decoded.
|
|
|
|
// If mHasBeenDecoded is false then we'll get another UpdateState call
|
|
|
|
// when the decode finishes.
|
|
|
|
if (mHasBeenDecoded) {
|
|
|
|
Maybe<uint32_t> frameCount = FrameCount();
|
|
|
|
MOZ_ASSERT(frameCount.isSome());
|
2018-02-28 21:34:52 +03:00
|
|
|
mIsCurrentlyDecoded = aResult.Surface().IsFullyDecoded();
|
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.
|
|
|
|
if (mIsCurrentlyDecoded || aAnimationFinished) {
|
|
|
|
// Animated images that have finished their animation (ie because it is a
|
|
|
|
// finite length animation) don't have RequestRefresh called on them, and
|
|
|
|
// so mCompositedFrameInvalid would never get cleared. We clear it here
|
|
|
|
// (and also in RasterImage::Decode when we create a decoder for an image
|
|
|
|
// that has finished animated so it can display sooner than waiting until
|
|
|
|
// the decode completes). We also do it if we are fully decoded. This is
|
|
|
|
// safe to do for images that 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.
|
|
|
|
if (mCompositedFrameInvalid) {
|
|
|
|
// Invalidate if we are marking the composited frame valid.
|
|
|
|
ret.SizeTo(aSize);
|
|
|
|
}
|
|
|
|
mCompositedFrameInvalid = false;
|
|
|
|
} else if (aResult.Type() == MatchType::NOT_FOUND ||
|
|
|
|
aResult.Type() == MatchType::PENDING) {
|
2017-08-10 05:26:55 +03:00
|
|
|
if (mHasRequestedDecode) {
|
2017-06-01 10:19:55 +03:00
|
|
|
MOZ_ASSERT(gfxPrefs::ImageMemAnimatedDiscardable());
|
|
|
|
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) {
|
|
|
|
if (!gfxPrefs::ImageAnimatedResumeFromLastDisplayed() ||
|
|
|
|
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) {
|
2018-09-17 22:06:29 +03:00
|
|
|
MOZ_ASSERT(nextFrame->IsFullFrame());
|
2016-07-20 04:20:13 +03:00
|
|
|
ret.mDirtyRect = aState.FirstFrameRefreshArea();
|
2018-09-17 22:06:29 +03:00
|
|
|
} else if (!nextFrame->IsFullFrame()) {
|
2016-03-02 07:34:39 +03:00
|
|
|
MOZ_ASSERT(nextFrameIndex == currentFrameIndex + 1);
|
2018-06-04 01:49:23 +03:00
|
|
|
RawAccessFrameRef currentRef =
|
|
|
|
aCurrentFrame->RawAccessRef(/* aFinished */ true);
|
|
|
|
RawAccessFrameRef nextRef = nextFrame->RawAccessRef(/* aFinished */ true);
|
|
|
|
|
2016-03-02 07:34:39 +03:00
|
|
|
// Change frame
|
2018-06-04 01:49:23 +03:00
|
|
|
if (!DoBlend(currentRef, nextRef, nextFrameIndex, &ret.mDirtyRect)) {
|
2013-07-15 22:38:59 +04:00
|
|
|
// something went wrong, move on to next
|
|
|
|
NS_WARNING("FrameAnimator::AdvanceFrame(): Compositing of frame failed");
|
2014-08-23 00:49:54 +04:00
|
|
|
nextFrame->SetCompositingFailed(true);
|
2018-05-29 15:36:12 +03:00
|
|
|
aState.mCurrentAnimationFrameTime =
|
|
|
|
GetCurrentImgFrameEndTime(aState, aCurrentFrame->GetTimeout());
|
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
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-23 00:49:54 +04:00
|
|
|
nextFrame->SetCompositingFailed(false);
|
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();
|
|
|
|
}
|
|
|
|
|
2017-03-26 08:04:53 +03:00
|
|
|
RefreshResult FrameAnimator::RequestRefresh(AnimationState& aState,
|
|
|
|
const TimeStamp& aTime,
|
|
|
|
bool aAnimationFinished) {
|
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
|
|
|
|
2017-04-06 12:00:36 +03:00
|
|
|
ret.mDirtyRect =
|
|
|
|
aState.UpdateStateInternal(result, aAnimationFinished, 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-04-06 12:00:36 +03:00
|
|
|
if (!ret.mDirtyRect.IsEmpty()) {
|
|
|
|
ret.mFrameAdvanced = true;
|
|
|
|
}
|
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) {
|
2017-03-23 08:02:54 +03:00
|
|
|
MOZ_ASSERT(gfxPrefs::ImageMemAnimatedDiscardable());
|
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.
|
2018-09-17 22:06:29 +03:00
|
|
|
if (currentFrameEndTime > aTime && aState.mCompositedFrameInvalid &&
|
2018-11-20 05:05:54 +03:00
|
|
|
(mLastCompositedFrameIndex >= 0 || currentFrame->IsFullFrame())) {
|
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;
|
|
|
|
|
2018-05-29 15:36:13 +03:00
|
|
|
// If we have a composited version of this frame, return that.
|
|
|
|
if (!aState.mCompositedFrameInvalid && mLastCompositedFrameIndex >= 0 &&
|
|
|
|
(uint32_t(mLastCompositedFrameIndex) ==
|
|
|
|
aState.mCurrentAnimationFrameIndex)) {
|
|
|
|
return LookupResult(DrawableSurface(mCompositingFrame->DrawableRef()),
|
|
|
|
MatchType::EXACT);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
MOZ_ASSERT(gfxPrefs::ImageMemAnimatedDiscardable());
|
2017-08-10 05:26:55 +03:00
|
|
|
MOZ_ASSERT(aState.GetHasRequestedDecode());
|
2017-03-16 11:06:04 +03:00
|
|
|
MOZ_ASSERT(!aState.GetIsCurrentlyDecoded());
|
2017-05-04 09:25:10 +03:00
|
|
|
if (result.Type() == MatchType::NOT_FOUND) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return LookupResult(MatchType::PENDING);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!result.Surface()->GetIsPaletted(),
|
2015-07-01 04:57:03 +03:00
|
|
|
"About to return a paletted frame");
|
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
|
|
|
}
|
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
static void DoCollectSizeOfCompositingSurfaces(
|
|
|
|
const RawAccessFrameRef& aSurface, SurfaceMemoryCounterType aType,
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters, MallocSizeOf aMallocSizeOf) {
|
|
|
|
// Concoct a SurfaceKey for this surface.
|
|
|
|
SurfaceKey key = RasterSurfaceKey(
|
|
|
|
aSurface->GetImageSize(), DefaultSurfaceFlags(), PlaybackType::eStatic);
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2015-04-28 21:46:17 +03:00
|
|
|
// Extract the surface's memory usage information.
|
2018-09-25 16:13:51 +03:00
|
|
|
aSurface->AddSizeOfExcludingThis(
|
|
|
|
aMallocSizeOf, [&](imgFrame::AddSizeOfCbData& aMetadata) {
|
|
|
|
// Create a counter for this surface.
|
|
|
|
SurfaceMemoryCounter counter(key, /* aIsLocked = */ true,
|
|
|
|
/* aCannotSubstitute */ false,
|
|
|
|
/* aIsFactor2 */ false, aType);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-25 16:13:51 +03:00
|
|
|
// Record it.
|
|
|
|
counter.Values().SetDecodedHeap(aMetadata.heap);
|
|
|
|
counter.Values().SetDecodedNonHeap(aMetadata.nonHeap);
|
|
|
|
counter.Values().SetExternalHandles(aMetadata.handles);
|
|
|
|
counter.Values().SetFrameIndex(aMetadata.index);
|
|
|
|
counter.Values().SetExternalId(aMetadata.externalId);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-25 16:13:51 +03:00
|
|
|
aCounters.AppendElement(counter);
|
|
|
|
});
|
2015-04-28 21:46:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameAnimator::CollectSizeOfCompositingSurfaces(
|
|
|
|
nsTArray<SurfaceMemoryCounter>& aCounters,
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2015-01-08 00:07:23 +03:00
|
|
|
if (mCompositingFrame) {
|
2015-04-28 21:46:17 +03:00
|
|
|
DoCollectSizeOfCompositingSurfaces(mCompositingFrame,
|
|
|
|
SurfaceMemoryCounterType::COMPOSITING,
|
|
|
|
aCounters, aMallocSizeOf);
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
2015-04-28 21:46:17 +03:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
if (mCompositingPrevFrame) {
|
2015-04-28 21:46:17 +03:00
|
|
|
DoCollectSizeOfCompositingSurfaces(
|
|
|
|
mCompositingPrevFrame, SurfaceMemoryCounterType::COMPOSITING_PREV,
|
|
|
|
aCounters, aMallocSizeOf);
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// DoBlend gets called when the timer for animation get fired and we have to
|
|
|
|
// update the composited frame of the animation.
|
2018-05-29 15:36:12 +03:00
|
|
|
bool FrameAnimator::DoBlend(const RawAccessFrameRef& aPrevFrame,
|
|
|
|
const RawAccessFrameRef& aNextFrame,
|
|
|
|
uint32_t aNextFrameIndex, IntRect* aDirtyRect) {
|
2018-06-04 01:49:23 +03:00
|
|
|
if (!aPrevFrame || !aNextFrame) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Should have RawAccessFrameRefs to blend!");
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
DisposalMethod prevDisposalMethod = aPrevFrame->GetDisposalMethod();
|
|
|
|
bool prevHasAlpha = aPrevFrame->FormatHasAlpha();
|
|
|
|
if (prevDisposalMethod == DisposalMethod::RESTORE_PREVIOUS &&
|
2015-01-08 00:07:23 +03:00
|
|
|
!mCompositingPrevFrame) {
|
2018-05-29 15:36:12 +03:00
|
|
|
prevDisposalMethod = DisposalMethod::CLEAR;
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
IntRect prevRect = aPrevFrame->GetBoundedBlendRect();
|
2017-12-21 00:46:28 +03:00
|
|
|
bool isFullPrevFrame = prevRect.IsEqualRect(0, 0, mSize.width, mSize.height);
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
// Optimization: DisposeClearAll if the previous frame is the same size as
|
|
|
|
// container and it's clearing itself
|
2018-05-29 15:36:12 +03:00
|
|
|
if (isFullPrevFrame && (prevDisposalMethod == DisposalMethod::CLEAR)) {
|
|
|
|
prevDisposalMethod = DisposalMethod::CLEAR_ALL;
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
DisposalMethod nextDisposalMethod = aNextFrame->GetDisposalMethod();
|
|
|
|
bool nextHasAlpha = aNextFrame->FormatHasAlpha();
|
2016-11-25 18:38:37 +03:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
IntRect nextRect = aNextFrame->GetBoundedBlendRect();
|
2017-12-21 00:46:28 +03:00
|
|
|
bool isFullNextFrame = nextRect.IsEqualRect(0, 0, mSize.width, mSize.height);
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
if (!aNextFrame->GetIsPaletted()) {
|
2015-01-08 00:07:23 +03:00
|
|
|
// Optimization: Skip compositing if the previous frame wants to clear the
|
|
|
|
// whole image
|
2018-05-29 15:36:12 +03:00
|
|
|
if (prevDisposalMethod == DisposalMethod::CLEAR_ALL) {
|
2015-01-08 00:07:23 +03:00
|
|
|
aDirtyRect->SetRect(0, 0, mSize.width, mSize.height);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization: Skip compositing if this frame is the same size as the
|
|
|
|
// container and it's fully drawing over prev frame (no alpha)
|
|
|
|
if (isFullNextFrame &&
|
2018-05-29 15:36:12 +03:00
|
|
|
(nextDisposalMethod != DisposalMethod::RESTORE_PREVIOUS) &&
|
|
|
|
!nextHasAlpha) {
|
2015-01-08 00:07:23 +03:00
|
|
|
aDirtyRect->SetRect(0, 0, mSize.width, mSize.height);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate area that needs updating
|
2018-05-29 15:36:12 +03:00
|
|
|
switch (prevDisposalMethod) {
|
2015-01-08 00:07:23 +03:00
|
|
|
default:
|
2015-12-25 09:50:32 +03:00
|
|
|
MOZ_FALLTHROUGH_ASSERT("Unexpected DisposalMethod");
|
2015-01-08 00:07:23 +03:00
|
|
|
case DisposalMethod::NOT_SPECIFIED:
|
|
|
|
case DisposalMethod::KEEP:
|
2016-11-25 18:38:37 +03:00
|
|
|
*aDirtyRect = nextRect;
|
2015-01-08 00:07:23 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DisposalMethod::CLEAR_ALL:
|
|
|
|
// Whole image container is cleared
|
|
|
|
aDirtyRect->SetRect(0, 0, mSize.width, mSize.height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisposalMethod::CLEAR:
|
|
|
|
// Calc area that needs to be redrawn (the combination of previous and
|
|
|
|
// this frame)
|
|
|
|
// XXX - This could be done with multiple framechanged calls
|
2018-05-29 15:36:12 +03:00
|
|
|
// Having aPrevFrame way at the top of the image, and aNextFrame
|
2015-01-08 00:07:23 +03:00
|
|
|
// way at the bottom, and both frames being small, we'd be
|
|
|
|
// telling framechanged to refresh the whole image when only two
|
|
|
|
// small areas are needed.
|
2016-11-25 18:38:37 +03:00
|
|
|
aDirtyRect->UnionRect(nextRect, prevRect);
|
2015-01-08 00:07:23 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DisposalMethod::RESTORE_PREVIOUS:
|
|
|
|
aDirtyRect->SetRect(0, 0, mSize.width, mSize.height);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization:
|
|
|
|
// Skip compositing if the last composited frame is this frame
|
|
|
|
// (Only one composited frame was made for this animation. Example:
|
|
|
|
// Only Frame 3 of a 10 frame image required us to build a composite frame
|
|
|
|
// On the second loop, we do not need to rebuild the frame
|
|
|
|
// since it's still sitting in compositingFrame)
|
2016-07-19 23:31:44 +03:00
|
|
|
if (mLastCompositedFrameIndex == int32_t(aNextFrameIndex)) {
|
2015-01-08 00:07:23 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool needToBlankComposite = false;
|
|
|
|
|
|
|
|
// Create the Compositing Frame
|
|
|
|
if (!mCompositingFrame) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgFrame> newFrame = new imgFrame;
|
2017-01-18 21:31:20 +03:00
|
|
|
nsresult rv = newFrame->InitForAnimator(mSize, SurfaceFormat::B8G8R8A8);
|
2015-01-08 00:07:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCompositingFrame.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mCompositingFrame = newFrame->RawAccessRef();
|
|
|
|
needToBlankComposite = true;
|
2016-07-19 23:31:44 +03:00
|
|
|
} else if (int32_t(aNextFrameIndex) != mLastCompositedFrameIndex + 1) {
|
2015-01-08 00:07:23 +03:00
|
|
|
// If we are not drawing on top of last composited frame,
|
|
|
|
// then we are building a new composite frame, so let's clear it first.
|
|
|
|
needToBlankComposite = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// More optimizations possible when next frame is not transparent
|
|
|
|
// But if the next frame has DisposalMethod::RESTORE_PREVIOUS,
|
|
|
|
// this "no disposal" optimization is not possible,
|
|
|
|
// because the frame in "after disposal operation" state
|
|
|
|
// needs to be stored in compositingFrame, so it can be
|
|
|
|
// copied into compositingPrevFrame later.
|
|
|
|
bool doDisposal = true;
|
2018-05-29 15:36:12 +03:00
|
|
|
if (!nextHasAlpha && nextDisposalMethod != DisposalMethod::RESTORE_PREVIOUS) {
|
2015-01-08 00:07:23 +03:00
|
|
|
if (isFullNextFrame) {
|
|
|
|
// Optimization: No need to dispose prev.frame when
|
|
|
|
// next frame is full frame and not transparent.
|
|
|
|
doDisposal = false;
|
|
|
|
// No need to blank the composite frame
|
|
|
|
needToBlankComposite = false;
|
|
|
|
} else {
|
2017-12-21 00:46:28 +03:00
|
|
|
if ((prevRect.X() >= nextRect.X()) && (prevRect.Y() >= nextRect.Y()) &&
|
2017-08-14 15:29:56 +03:00
|
|
|
(prevRect.XMost() <= nextRect.XMost()) &&
|
|
|
|
(prevRect.YMost() <= nextRect.YMost())) {
|
2015-01-08 00:07:23 +03:00
|
|
|
// Optimization: No need to dispose prev.frame when
|
|
|
|
// next frame fully overlaps previous frame.
|
|
|
|
doDisposal = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doDisposal) {
|
|
|
|
// Dispose of previous: clear, restore, or keep (copy)
|
2018-05-29 15:36:12 +03:00
|
|
|
switch (prevDisposalMethod) {
|
2015-01-08 00:07:23 +03:00
|
|
|
case DisposalMethod::CLEAR:
|
|
|
|
if (needToBlankComposite) {
|
|
|
|
// If we just created the composite, it could have anything in its
|
|
|
|
// buffer. Clear whole frame
|
2018-05-29 15:36:12 +03:00
|
|
|
ClearFrame(mCompositingFrame.Data(), mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
} else {
|
|
|
|
// Only blank out previous frame area (both color & Mask/Alpha)
|
2018-05-29 15:36:12 +03:00
|
|
|
ClearFrame(mCompositingFrame.Data(), mCompositingFrame->GetRect(),
|
2016-11-25 18:38:37 +03:00
|
|
|
prevRect);
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DisposalMethod::CLEAR_ALL:
|
2018-05-29 15:36:12 +03:00
|
|
|
ClearFrame(mCompositingFrame.Data(), mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DisposalMethod::RESTORE_PREVIOUS:
|
|
|
|
// It would be better to copy only the area changed back to
|
|
|
|
// compositingFrame.
|
|
|
|
if (mCompositingPrevFrame) {
|
2018-05-29 15:36:12 +03:00
|
|
|
CopyFrameImage(
|
|
|
|
mCompositingPrevFrame.Data(), mCompositingPrevFrame->GetRect(),
|
|
|
|
mCompositingFrame.Data(), mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
// destroy only if we don't need it for this frame's disposal
|
2018-05-29 15:36:12 +03:00
|
|
|
if (nextDisposalMethod != DisposalMethod::RESTORE_PREVIOUS) {
|
2015-01-08 00:07:23 +03:00
|
|
|
mCompositingPrevFrame.reset();
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-29 15:36:12 +03:00
|
|
|
ClearFrame(mCompositingFrame.Data(), mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-12-25 09:50:32 +03:00
|
|
|
MOZ_FALLTHROUGH_ASSERT("Unexpected DisposalMethod");
|
2015-03-17 07:02:15 +03:00
|
|
|
case DisposalMethod::NOT_SPECIFIED:
|
|
|
|
case DisposalMethod::KEEP:
|
2015-01-08 00:07:23 +03:00
|
|
|
// Copy previous frame into compositingFrame before we put the new
|
|
|
|
// frame on top
|
|
|
|
// Assumes that the previous frame represents a full frame (it could be
|
|
|
|
// smaller in size than the container, as long as the frame before it
|
|
|
|
// erased itself)
|
|
|
|
// Note: Frame 1 never gets into DoBlend(), so (aNextFrameIndex - 1)
|
|
|
|
// will always be a valid frame number.
|
2016-07-19 23:31:44 +03:00
|
|
|
if (mLastCompositedFrameIndex != int32_t(aNextFrameIndex - 1)) {
|
2018-05-29 15:36:12 +03:00
|
|
|
if (isFullPrevFrame && !aPrevFrame->GetIsPaletted()) {
|
2015-01-08 00:07:23 +03:00
|
|
|
// Just copy the bits
|
2018-05-29 15:36:12 +03:00
|
|
|
CopyFrameImage(aPrevFrame.Data(), prevRect,
|
|
|
|
mCompositingFrame.Data(),
|
|
|
|
mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
} else {
|
|
|
|
if (needToBlankComposite) {
|
|
|
|
// Only blank composite when prev is transparent or not full.
|
2018-05-29 15:36:12 +03:00
|
|
|
if (prevHasAlpha || !isFullPrevFrame) {
|
|
|
|
ClearFrame(mCompositingFrame.Data(),
|
|
|
|
mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
}
|
2018-05-29 15:36:12 +03:00
|
|
|
DrawFrameTo(aPrevFrame.Data(), aPrevFrame->GetRect(),
|
|
|
|
aPrevFrame.PaletteDataLength(), prevHasAlpha,
|
|
|
|
mCompositingFrame.Data(), mCompositingFrame->GetRect(),
|
|
|
|
aPrevFrame->GetBlendMethod(),
|
|
|
|
aPrevFrame->GetBlendRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (needToBlankComposite) {
|
|
|
|
// If we just created the composite, it could have anything in its
|
|
|
|
// buffers. Clear them
|
2018-05-29 15:36:12 +03:00
|
|
|
ClearFrame(mCompositingFrame.Data(), mCompositingFrame->GetRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the frame we are composing wants the previous image restored after
|
|
|
|
// it is done. Don't store it (again) if last frame wanted its image restored
|
|
|
|
// too
|
2018-05-29 15:36:12 +03:00
|
|
|
if ((nextDisposalMethod == DisposalMethod::RESTORE_PREVIOUS) &&
|
|
|
|
(prevDisposalMethod != DisposalMethod::RESTORE_PREVIOUS)) {
|
2015-01-08 00:07:23 +03:00
|
|
|
// We are storing the whole image.
|
2018-05-29 15:36:12 +03:00
|
|
|
// It would be better if we just stored the area that aNextFrame is going to
|
2015-01-08 00:07:23 +03:00
|
|
|
// overwrite.
|
|
|
|
if (!mCompositingPrevFrame) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgFrame> newFrame = new imgFrame;
|
2017-01-18 21:31:20 +03:00
|
|
|
nsresult rv = newFrame->InitForAnimator(mSize, SurfaceFormat::B8G8R8A8);
|
2015-01-08 00:07:23 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCompositingPrevFrame.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCompositingPrevFrame = newFrame->RawAccessRef();
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
CopyFrameImage(mCompositingFrame.Data(), mCompositingFrame->GetRect(),
|
|
|
|
mCompositingPrevFrame.Data(),
|
|
|
|
mCompositingPrevFrame->GetRect());
|
2015-01-12 06:28:02 +03:00
|
|
|
|
|
|
|
mCompositingPrevFrame->Finish();
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// blit next frame into it's correct spot
|
2018-05-29 15:36:12 +03:00
|
|
|
DrawFrameTo(aNextFrame.Data(), aNextFrame->GetRect(),
|
|
|
|
aNextFrame.PaletteDataLength(), nextHasAlpha,
|
|
|
|
mCompositingFrame.Data(), mCompositingFrame->GetRect(),
|
|
|
|
aNextFrame->GetBlendMethod(), aNextFrame->GetBlendRect());
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
// Tell the image that it is fully 'downloaded'.
|
2015-01-12 06:28:02 +03:00
|
|
|
mCompositingFrame->Finish();
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2016-07-19 23:31:44 +03:00
|
|
|
mLastCompositedFrameIndex = int32_t(aNextFrameIndex);
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// Fill aFrame with black. Does also clears the mask.
|
2016-07-20 09:43:52 +03:00
|
|
|
void FrameAnimator::ClearFrame(uint8_t* aFrameData, const IntRect& aFrameRect) {
|
2015-01-08 00:07:23 +03:00
|
|
|
if (!aFrameData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:29:56 +03:00
|
|
|
memset(aFrameData, 0, aFrameRect.Width() * aFrameRect.Height() * 4);
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2016-07-20 09:43:52 +03:00
|
|
|
void FrameAnimator::ClearFrame(uint8_t* aFrameData, const IntRect& aFrameRect,
|
|
|
|
const IntRect& aRectToClear) {
|
2017-08-14 15:29:56 +03:00
|
|
|
if (!aFrameData || aFrameRect.Width() <= 0 || aFrameRect.Height() <= 0 ||
|
|
|
|
aRectToClear.Width() <= 0 || aRectToClear.Height() <= 0) {
|
2015-01-08 00:07:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-20 09:43:52 +03:00
|
|
|
IntRect toClear = aFrameRect.Intersect(aRectToClear);
|
2015-01-08 00:07:23 +03:00
|
|
|
if (toClear.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:29:56 +03:00
|
|
|
uint32_t bytesPerRow = aFrameRect.Width() * 4;
|
2017-12-21 00:46:28 +03:00
|
|
|
for (int row = toClear.Y(); row < toClear.YMost(); ++row) {
|
|
|
|
memset(aFrameData + toClear.X() * 4 + row * bytesPerRow, 0,
|
2017-08-14 15:29:56 +03:00
|
|
|
toClear.Width() * 4);
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// Whether we succeed or fail will not cause a crash, and there's not much
|
|
|
|
// we can do about a failure, so there we don't return a nsresult
|
2015-01-16 20:27:00 +03:00
|
|
|
bool FrameAnimator::CopyFrameImage(const uint8_t* aDataSrc,
|
2016-07-20 09:43:52 +03:00
|
|
|
const IntRect& aRectSrc, uint8_t* aDataDest,
|
|
|
|
const IntRect& aRectDest) {
|
2017-08-14 15:29:56 +03:00
|
|
|
uint32_t dataLengthSrc = aRectSrc.Width() * aRectSrc.Height() * 4;
|
|
|
|
uint32_t dataLengthDest = aRectDest.Width() * aRectDest.Height() * 4;
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
if (!aDataDest || !aDataSrc || dataLengthSrc != dataLengthDest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(aDataDest, aDataSrc, dataLengthDest);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-20 09:43:52 +03:00
|
|
|
nsresult FrameAnimator::DrawFrameTo(const uint8_t* aSrcData,
|
|
|
|
const IntRect& aSrcRect,
|
2015-01-08 00:07:23 +03:00
|
|
|
uint32_t aSrcPaletteLength,
|
2016-07-20 09:43:52 +03:00
|
|
|
bool aSrcHasAlpha, uint8_t* aDstPixels,
|
|
|
|
const IntRect& aDstRect,
|
2018-05-29 15:36:12 +03:00
|
|
|
BlendMethod aBlendMethod,
|
|
|
|
const IntRect& aBlendRect) {
|
2015-01-08 00:07:23 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aSrcData);
|
|
|
|
NS_ENSURE_ARG_POINTER(aDstPixels);
|
|
|
|
|
|
|
|
// According to both AGIF and APNG specs, offsets are unsigned
|
2017-12-21 00:46:28 +03:00
|
|
|
if (aSrcRect.X() < 0 || aSrcRect.Y() < 0) {
|
2015-01-08 00:07:23 +03:00
|
|
|
NS_WARNING("FrameAnimator::DrawFrameTo: negative offsets not allowed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-12-21 00:46:28 +03:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
// Outside the destination frame, skip it
|
2017-12-21 00:46:28 +03:00
|
|
|
if ((aSrcRect.X() > aDstRect.Width()) || (aSrcRect.Y() > aDstRect.Height())) {
|
2015-01-08 00:07:23 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSrcPaletteLength) {
|
|
|
|
// Larger than the destination frame, clip it
|
2017-12-21 00:46:28 +03:00
|
|
|
int32_t width = std::min(aSrcRect.Width(), aDstRect.Width() - aSrcRect.X());
|
|
|
|
int32_t height =
|
|
|
|
std::min(aSrcRect.Height(), aDstRect.Height() - aSrcRect.Y());
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
// The clipped image must now fully fit within destination image frame
|
2017-12-21 00:46:28 +03:00
|
|
|
NS_ASSERTION((aSrcRect.X() >= 0) && (aSrcRect.Y() >= 0) &&
|
|
|
|
(aSrcRect.X() + width <= aDstRect.Width()) &&
|
|
|
|
(aSrcRect.Y() + height <= aDstRect.Height()),
|
2015-01-08 00:07:23 +03:00
|
|
|
"FrameAnimator::DrawFrameTo: Invalid aSrcRect");
|
|
|
|
|
|
|
|
// clipped image size may be smaller than source, but not larger
|
2017-08-14 15:29:56 +03:00
|
|
|
NS_ASSERTION(
|
|
|
|
(width <= aSrcRect.Width()) && (height <= aSrcRect.Height()),
|
2015-01-16 20:27:00 +03:00
|
|
|
"FrameAnimator::DrawFrameTo: source must be smaller than dest");
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
// Get pointers to image data
|
|
|
|
const uint8_t* srcPixels = aSrcData + aSrcPaletteLength;
|
|
|
|
uint32_t* dstPixels = reinterpret_cast<uint32_t*>(aDstPixels);
|
|
|
|
const uint32_t* colormap = reinterpret_cast<const uint32_t*>(aSrcData);
|
|
|
|
|
|
|
|
// Skip to the right offset
|
2017-12-21 00:46:28 +03:00
|
|
|
dstPixels += aSrcRect.X() + (aSrcRect.Y() * aDstRect.Width());
|
2015-01-08 00:07:23 +03:00
|
|
|
if (!aSrcHasAlpha) {
|
|
|
|
for (int32_t r = height; r > 0; --r) {
|
|
|
|
for (int32_t c = 0; c < width; c++) {
|
|
|
|
dstPixels[c] = colormap[srcPixels[c]];
|
|
|
|
}
|
|
|
|
// Go to the next row in the source resp. destination image
|
2017-08-14 15:29:56 +03:00
|
|
|
srcPixels += aSrcRect.Width();
|
|
|
|
dstPixels += aDstRect.Width();
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int32_t r = height; r > 0; --r) {
|
|
|
|
for (int32_t c = 0; c < width; c++) {
|
|
|
|
const uint32_t color = colormap[srcPixels[c]];
|
|
|
|
if (color) {
|
|
|
|
dstPixels[c] = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Go to the next row in the source resp. destination image
|
2017-08-14 15:29:56 +03:00
|
|
|
srcPixels += aSrcRect.Width();
|
|
|
|
dstPixels += aDstRect.Width();
|
2015-01-08 00:07:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pixman_image_t* src = pixman_image_create_bits(
|
|
|
|
aSrcHasAlpha ? PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8, aSrcRect.Width(),
|
2017-08-14 15:29:56 +03:00
|
|
|
aSrcRect.Height(),
|
2015-01-08 00:07:23 +03:00
|
|
|
reinterpret_cast<uint32_t*>(const_cast<uint8_t*>(aSrcData)),
|
2017-08-14 15:29:56 +03:00
|
|
|
aSrcRect.Width() * 4);
|
2016-06-01 07:27:30 +03:00
|
|
|
if (!src) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2015-01-08 00:07:23 +03:00
|
|
|
pixman_image_t* dst = pixman_image_create_bits(
|
2017-08-14 15:29:56 +03:00
|
|
|
PIXMAN_a8r8g8b8, aDstRect.Width(), aDstRect.Height(),
|
2015-01-08 00:07:23 +03:00
|
|
|
reinterpret_cast<uint32_t*>(aDstPixels), aDstRect.Width() * 4);
|
2016-06-01 07:27:30 +03:00
|
|
|
if (!dst) {
|
|
|
|
pixman_image_unref(src);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2015-01-08 00:07:23 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// XXX(seth): This is inefficient but we'll remove it quite soon when we
|
|
|
|
// move frame compositing into SurfacePipe. For now we need this because
|
|
|
|
// RemoveFrameRectFilter has transformed PNG frames with frame rects into
|
|
|
|
// imgFrame's with no frame rects, but with a region of 0 alpha where the
|
|
|
|
// frame rect should be. This works really nicely if we're using
|
|
|
|
// BlendMethod::OVER, but BlendMethod::SOURCE will result in that frame rect
|
|
|
|
// area overwriting the previous frame, which makes the animation look
|
|
|
|
// wrong. This quick hack fixes that by first compositing the whle new frame
|
|
|
|
// with BlendMethod::OVER, and then recopying the area that uses
|
|
|
|
// BlendMethod::SOURCE if needed. To make this work, the decoder has to
|
|
|
|
// provide a "blend rect" that tells us where to do this. This is just the
|
|
|
|
// frame rect, but hidden in a way that makes it invisible to most of the
|
|
|
|
// system, so we can keep eliminating dependencies on it.
|
2015-01-08 00:07:23 +03:00
|
|
|
auto op =
|
|
|
|
aBlendMethod == BlendMethod::SOURCE ? PIXMAN_OP_SRC : PIXMAN_OP_OVER;
|
2016-06-25 01:20:32 +03:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
if (aBlendMethod == BlendMethod::OVER ||
|
|
|
|
(aBlendMethod == BlendMethod::SOURCE &&
|
|
|
|
aSrcRect.IsEqualEdges(aBlendRect))) {
|
2016-06-25 01:20:32 +03:00
|
|
|
// We don't need to do anything clever. (Or, in the case where no blend
|
|
|
|
// rect was specified, we can't.)
|
|
|
|
pixman_image_composite32(op, src, nullptr, dst, 0, 0, 0, 0, aSrcRect.X(),
|
2017-12-21 00:46:28 +03:00
|
|
|
aSrcRect.Y(), aSrcRect.Width(),
|
2017-08-14 15:29:56 +03:00
|
|
|
aSrcRect.Height());
|
2016-06-25 01:20:32 +03:00
|
|
|
} else {
|
|
|
|
// We need to do the OVER followed by SOURCE trick above.
|
|
|
|
pixman_image_composite32(PIXMAN_OP_OVER, src, nullptr, dst, 0, 0, 0, 0,
|
2017-12-21 00:46:28 +03:00
|
|
|
aSrcRect.X(), aSrcRect.Y(), aSrcRect.Width(),
|
2017-08-14 15:29:56 +03:00
|
|
|
aSrcRect.Height());
|
2016-06-25 01:20:32 +03:00
|
|
|
pixman_image_composite32(PIXMAN_OP_SRC, src, nullptr, dst, aBlendRect.X(),
|
2018-05-29 15:36:12 +03:00
|
|
|
aBlendRect.Y(), 0, 0, aBlendRect.X(),
|
|
|
|
aBlendRect.Y(), aBlendRect.Width(),
|
|
|
|
aBlendRect.Height());
|
2016-06-25 01:20:32 +03:00
|
|
|
}
|
2015-01-08 00:07:23 +03:00
|
|
|
|
|
|
|
pixman_image_unref(src);
|
|
|
|
pixman_image_unref(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:00:31 +04:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|