2016-08-18 10:01:10 +03: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 "AnimationSurfaceProvider.h"
|
|
|
|
|
2019-05-26 17:30:14 +03:00
|
|
|
#include "mozilla/StaticPrefs.h"
|
2018-06-04 15:33:16 +03:00
|
|
|
#include "mozilla/gfx/gfxVars.h"
|
2016-08-18 10:01:10 +03:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
#include "DecodePool.h"
|
2016-08-18 10:01:10 +03:00
|
|
|
#include "Decoder.h"
|
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
AnimationSurfaceProvider::AnimationSurfaceProvider(
|
2016-08-25 00:50:49 +03:00
|
|
|
NotNull<RasterImage*> aImage, const SurfaceKey& aSurfaceKey,
|
2018-02-28 21:34:52 +03:00
|
|
|
NotNull<Decoder*> aDecoder, size_t aCurrentFrame)
|
2016-08-25 00:50:49 +03:00
|
|
|
: ISurfaceProvider(ImageKey(aImage.get()), aSurfaceKey,
|
|
|
|
AvailabilityState::StartAsPlaceholder()),
|
2016-08-18 10:01:10 +03:00
|
|
|
mImage(aImage.get()),
|
|
|
|
mDecodingMutex("AnimationSurfaceProvider::mDecoder"),
|
|
|
|
mDecoder(aDecoder.get()),
|
|
|
|
mFramesMutex("AnimationSurfaceProvider::mFrames") {
|
|
|
|
MOZ_ASSERT(!mDecoder->IsMetadataDecode(),
|
|
|
|
"Use MetadataDecodingTask for metadata decodes");
|
|
|
|
MOZ_ASSERT(!mDecoder->IsFirstFrameDecode(),
|
|
|
|
"Use DecodedSurfaceProvider for single-frame image decodes");
|
2018-02-28 21:34:53 +03:00
|
|
|
|
|
|
|
// Calculate how many frames we need to decode in this animation before we
|
|
|
|
// enter decode-on-demand mode.
|
|
|
|
IntSize frameSize = aSurfaceKey.Size();
|
|
|
|
size_t threshold =
|
2019-05-26 17:29:42 +03:00
|
|
|
(size_t(StaticPrefs::ImageAnimatedDecodeOnDemandThresholdKB()) * 1024) /
|
2019-03-15 20:29:02 +03:00
|
|
|
(sizeof(uint32_t) * frameSize.width * frameSize.height);
|
2019-05-26 17:29:42 +03:00
|
|
|
size_t batch = StaticPrefs::ImageAnimatedDecodeOnDemandBatchSize();
|
2018-02-28 21:34:53 +03:00
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
mFrames.reset(
|
|
|
|
new AnimationFrameRetainedBuffer(threshold, batch, aCurrentFrame));
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationSurfaceProvider::~AnimationSurfaceProvider() {
|
|
|
|
DropImageReference();
|
2018-06-04 15:33:16 +03:00
|
|
|
|
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->SetFrameRecycler(nullptr);
|
|
|
|
}
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationSurfaceProvider::DropImageReference() {
|
|
|
|
if (!mImage) {
|
|
|
|
return; // Nothing to do.
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:14 +03:00
|
|
|
// RasterImage objects need to be destroyed on the main thread.
|
|
|
|
NS_ReleaseOnMainThreadSystemGroup("AnimationSurfaceProvider::mImage",
|
|
|
|
mImage.forget());
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
void AnimationSurfaceProvider::Reset() {
|
|
|
|
// We want to go back to the beginning.
|
|
|
|
bool mayDiscard;
|
2018-11-06 18:15:07 +03:00
|
|
|
bool restartDecoder = false;
|
2018-02-28 21:34:53 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
|
|
|
|
// If we have not crossed the threshold, we know we haven't discarded any
|
|
|
|
// frames, and thus we know it is safe move our display index back to the
|
|
|
|
// very beginning. It would be cleaner to let the frame buffer make this
|
|
|
|
// decision inside the AnimationFrameBuffer::Reset method, but if we have
|
|
|
|
// crossed the threshold, we need to hold onto the decoding mutex too. We
|
|
|
|
// should avoid blocking the main thread on the decoder threads.
|
2018-06-04 15:23:00 +03:00
|
|
|
mayDiscard = mFrames->MayDiscard();
|
2018-02-28 21:34:53 +03:00
|
|
|
if (!mayDiscard) {
|
2018-06-04 15:23:00 +03:00
|
|
|
restartDecoder = mFrames->Reset();
|
2018-02-28 21:34:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mayDiscard) {
|
|
|
|
// We are over the threshold and have started discarding old frames. In
|
|
|
|
// that case we need to seize the decoding mutex. Thankfully we know that
|
|
|
|
// we are in the process of decoding at most the batch size frames, so
|
|
|
|
// this should not take too long to acquire.
|
|
|
|
MutexAutoLock lock(mDecodingMutex);
|
|
|
|
|
2018-11-06 18:15:07 +03:00
|
|
|
// We may have hit an error while redecoding. Because FrameAnimator is
|
|
|
|
// tightly coupled to our own state, that means we would need to go through
|
|
|
|
// some heroics to resume animating in those cases. The typical reason for
|
|
|
|
// a redecode to fail is out of memory, and recycling should prevent most of
|
|
|
|
// those errors. When image.animated.generate-full-frames has shipped
|
|
|
|
// enabled on a release or two, we can simply remove the old FrameAnimator
|
|
|
|
// blending code and simplify this quite a bit -- just always pop the next
|
|
|
|
// full frame and timeout off the stack.
|
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder = DecoderFactory::CloneAnimationDecoder(mDecoder);
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
|
|
|
|
MutexAutoLock lock2(mFramesMutex);
|
|
|
|
restartDecoder = mFrames->Reset();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mFrames->HasRedecodeError());
|
|
|
|
}
|
2018-02-28 21:34:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (restartDecoder) {
|
|
|
|
DecodePool::Singleton()->AsyncRun(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationSurfaceProvider::Advance(size_t aFrame) {
|
|
|
|
bool restartDecoder;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Typical advancement of a frame.
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
2018-06-04 15:23:00 +03:00
|
|
|
restartDecoder = mFrames->AdvanceTo(aFrame);
|
2018-02-28 21:34:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (restartDecoder) {
|
|
|
|
DecodePool::Singleton()->AsyncRun(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:01:10 +03:00
|
|
|
DrawableFrameRef AnimationSurfaceProvider::DrawableRef(size_t aFrame) {
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
|
|
|
|
if (Availability().IsPlaceholder()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Calling DrawableRef() on a placeholder");
|
|
|
|
return DrawableFrameRef();
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
imgFrame* frame = mFrames->Get(aFrame, /* aForDisplay */ true);
|
2018-05-29 15:36:12 +03:00
|
|
|
if (!frame) {
|
|
|
|
return DrawableFrameRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame->DrawableRef();
|
|
|
|
}
|
|
|
|
|
2018-06-04 01:49:23 +03:00
|
|
|
already_AddRefed<imgFrame> AnimationSurfaceProvider::GetFrame(size_t aFrame) {
|
2018-05-29 15:36:12 +03:00
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
|
|
|
|
if (Availability().IsPlaceholder()) {
|
2018-06-04 01:49:23 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Calling GetFrame() on a placeholder");
|
2018-06-04 15:23:00 +03:00
|
|
|
return nullptr;
|
2018-05-29 15:36:12 +03:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
RefPtr<imgFrame> frame = mFrames->Get(aFrame, /* aForDisplay */ false);
|
2018-06-04 01:49:23 +03:00
|
|
|
MOZ_ASSERT_IF(frame, frame->IsFinished());
|
|
|
|
return frame.forget();
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationSurfaceProvider::IsFinished() const {
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
|
|
|
|
if (Availability().IsPlaceholder()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Calling IsFinished() on a placeholder");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
return mFrames->IsFirstFrameFinished();
|
2018-02-28 21:34:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationSurfaceProvider::IsFullyDecoded() const {
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
2018-06-04 15:23:00 +03:00
|
|
|
return mFrames->SizeKnown() && !mFrames->MayDiscard();
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t AnimationSurfaceProvider::LogicalSizeInBytes() const {
|
|
|
|
// When decoding animated images, we need at most three live surfaces: the
|
|
|
|
// composited surface, the previous composited surface for
|
|
|
|
// DisposalMethod::RESTORE_PREVIOUS, and the surface we're currently decoding
|
|
|
|
// into. The composited surfaces are always BGRA. Although the surface we're
|
|
|
|
// decoding into may be paletted, and may be smaller than the real size of the
|
|
|
|
// image, we assume the worst case here.
|
|
|
|
// XXX(seth): Note that this is actually not accurate yet; we're storing the
|
|
|
|
// full sequence of frames, not just the three live surfaces mentioned above.
|
|
|
|
// Unfortunately there's no way to know in advance how many frames an
|
|
|
|
// animation has, so we really can't do better here. This will become correct
|
|
|
|
// once bug 1289954 is complete.
|
2016-08-25 00:50:49 +03:00
|
|
|
IntSize size = GetSurfaceKey().Size();
|
2016-08-18 10:01:10 +03:00
|
|
|
return 3 * size.width * size.height * sizeof(uint32_t);
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:06:41 +03:00
|
|
|
void AnimationSurfaceProvider::AddSizeOfExcludingThis(
|
2018-09-25 16:13:51 +03:00
|
|
|
MallocSizeOf aMallocSizeOf, const AddSizeOfCb& aCallback) {
|
2016-08-18 10:06:41 +03:00
|
|
|
// Note that the surface cache lock is already held here, and then we acquire
|
|
|
|
// mFramesMutex. For this method, this ordering is unavoidable, which means
|
|
|
|
// that we must be careful to always use the same ordering elsewhere.
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
2018-06-04 15:23:00 +03:00
|
|
|
mFrames->AddSizeOfExcludingThis(aMallocSizeOf, aCallback);
|
2016-08-18 10:06:41 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 10:01:10 +03:00
|
|
|
void AnimationSurfaceProvider::Run() {
|
|
|
|
MutexAutoLock lock(mDecodingMutex);
|
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
if (!mDecoder) {
|
2016-08-18 10:01:10 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Running after decoding finished?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Run the decoder.
|
|
|
|
LexerResult result = mDecoder->Decode(WrapNotNull(this));
|
|
|
|
|
|
|
|
if (result.is<TerminalState>()) {
|
|
|
|
// We may have a new frame now, but it's not guaranteed - a decoding
|
|
|
|
// failure or truncated data may mean that no new frame got produced.
|
|
|
|
// Since we're not sure, rather than call CheckForNewFrameAtYield() here
|
|
|
|
// we call CheckForNewFrameAtTerminalState(), which handles both of these
|
|
|
|
// possibilities.
|
2018-02-28 21:34:53 +03:00
|
|
|
bool continueDecoding = CheckForNewFrameAtTerminalState();
|
2016-08-18 10:01:10 +03:00
|
|
|
FinishDecoding();
|
2018-02-28 21:34:53 +03:00
|
|
|
|
|
|
|
// Even if it is the last frame, we may not have enough frames buffered
|
2018-04-24 20:51:35 +03:00
|
|
|
// ahead of the current. If we are shutting down, we want to ensure we
|
|
|
|
// release the thread as soon as possible. The animation may advance even
|
|
|
|
// during shutdown, which keeps us decoding, and thus blocking the decode
|
|
|
|
// pool during teardown.
|
|
|
|
if (!mDecoder || !continueDecoding ||
|
|
|
|
DecodePool::Singleton()->IsShuttingDown()) {
|
|
|
|
return;
|
2018-02-28 21:34:53 +03:00
|
|
|
}
|
2018-04-15 02:40:30 +03:00
|
|
|
|
2018-04-24 20:51:35 +03:00
|
|
|
// Restart from the very beginning because the decoder was recreated.
|
|
|
|
continue;
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify for the progress we've made so far.
|
2018-02-28 21:34:53 +03:00
|
|
|
if (mImage && mDecoder->HasProgress()) {
|
2016-08-18 10:01:10 +03:00
|
|
|
NotifyProgress(WrapNotNull(mImage), WrapNotNull(mDecoder));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LexerResult(Yield::NEED_MORE_DATA)) {
|
|
|
|
// We can't make any more progress right now. The decoder itself will
|
|
|
|
// ensure that we get reenqueued when more data is available; just return
|
|
|
|
// for now.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
// There's new output available - a new frame! Grab it. If we don't need any
|
2018-04-24 20:51:35 +03:00
|
|
|
// more for the moment we can break out of the loop. If we are shutting
|
|
|
|
// down, we want to ensure we release the thread as soon as possible. The
|
|
|
|
// animation may advance even during shutdown, which keeps us decoding, and
|
|
|
|
// thus blocking the decode pool during teardown.
|
2016-08-18 10:01:10 +03:00
|
|
|
MOZ_ASSERT(result == LexerResult(Yield::OUTPUT_AVAILABLE));
|
2018-04-24 20:51:35 +03:00
|
|
|
if (!CheckForNewFrameAtYield() ||
|
|
|
|
DecodePool::Singleton()->IsShuttingDown()) {
|
2018-02-28 21:34:53 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationSurfaceProvider::CheckForNewFrameAtYield() {
|
|
|
|
mDecodingMutex.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
|
|
|
|
bool justGotFirstFrame = false;
|
2018-06-04 15:23:00 +03:00
|
|
|
bool continueDecoding = false;
|
2016-08-18 10:01:10 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
|
|
|
|
// Try to get the new frame from the decoder.
|
2018-06-04 15:23:00 +03:00
|
|
|
RefPtr<imgFrame> frame = mDecoder->GetCurrentFrame();
|
2018-03-09 02:33:04 +03:00
|
|
|
MOZ_ASSERT(mDecoder->HasFrameToTake());
|
|
|
|
mDecoder->ClearHasFrameToTake();
|
|
|
|
|
2016-08-18 10:01:10 +03:00
|
|
|
if (!frame) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Decoder yielded but didn't produce a frame?");
|
2018-02-28 21:34:53 +03:00
|
|
|
return true;
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should've gotten a different frame than last time.
|
2018-06-04 15:23:00 +03:00
|
|
|
MOZ_ASSERT(!mFrames->IsLastInsertedFrame(frame));
|
2016-08-18 10:01:10 +03:00
|
|
|
|
|
|
|
// Append the new frame to the list.
|
2018-06-04 15:23:00 +03:00
|
|
|
AnimationFrameBuffer::InsertStatus status =
|
|
|
|
mFrames->Insert(std::move(frame));
|
2018-11-29 22:38:28 +03:00
|
|
|
|
|
|
|
// If we hit a redecode error, then we actually want to stop. This happens
|
|
|
|
// when we tried to insert more frames than we originally had (e.g. the
|
|
|
|
// original decoder attempt hit an OOM error sooner than we did). Better to
|
|
|
|
// stop the animation than to get out of sync with FrameAnimator.
|
|
|
|
if (mFrames->HasRedecodeError()) {
|
|
|
|
mDecoder = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
switch (status) {
|
|
|
|
case AnimationFrameBuffer::InsertStatus::DISCARD_CONTINUE:
|
|
|
|
continueDecoding = true;
|
|
|
|
MOZ_FALLTHROUGH;
|
|
|
|
case AnimationFrameBuffer::InsertStatus::DISCARD_YIELD:
|
|
|
|
RequestFrameDiscarding();
|
|
|
|
break;
|
|
|
|
case AnimationFrameBuffer::InsertStatus::CONTINUE:
|
|
|
|
continueDecoding = true;
|
|
|
|
break;
|
|
|
|
case AnimationFrameBuffer::InsertStatus::YIELD:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled insert status!");
|
|
|
|
break;
|
|
|
|
}
|
2016-08-18 10:01:10 +03:00
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
// We only want to handle the first frame if it is the first pass for the
|
|
|
|
// animation decoder. The owning image will be cleared after that.
|
2018-06-04 15:23:00 +03:00
|
|
|
size_t frameCount = mFrames->Size();
|
2018-02-28 21:34:53 +03:00
|
|
|
if (frameCount == 1 && mImage) {
|
2016-08-18 10:01:10 +03:00
|
|
|
justGotFirstFrame = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (justGotFirstFrame) {
|
|
|
|
AnnounceSurfaceAvailable();
|
|
|
|
}
|
2018-02-28 21:34:53 +03:00
|
|
|
|
2018-04-24 20:51:35 +03:00
|
|
|
return continueDecoding;
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationSurfaceProvider::CheckForNewFrameAtTerminalState() {
|
|
|
|
mDecodingMutex.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
|
|
|
|
bool justGotFirstFrame = false;
|
2018-02-28 21:34:53 +03:00
|
|
|
bool continueDecoding;
|
2016-08-18 10:01:10 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
// The decoder may or may not have a new frame for us at this point. Avoid
|
|
|
|
// reinserting the same frame again.
|
2018-06-04 15:23:00 +03:00
|
|
|
RefPtr<imgFrame> frame = mDecoder->GetCurrentFrame();
|
2018-03-09 02:33:04 +03:00
|
|
|
|
|
|
|
// If the decoder didn't finish a new frame (ie if, after starting the
|
|
|
|
// frame, it got an error and aborted the frame and the rest of the decode)
|
|
|
|
// that means it won't be reporting it to the image or FrameAnimator so we
|
|
|
|
// should ignore it too, that's what HasFrameToTake tracks basically.
|
|
|
|
if (!mDecoder->HasFrameToTake()) {
|
2018-06-04 15:23:00 +03:00
|
|
|
frame = nullptr;
|
2018-03-09 02:33:04 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(frame);
|
|
|
|
mDecoder->ClearHasFrameToTake();
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
if (!frame || mFrames->IsLastInsertedFrame(frame)) {
|
|
|
|
return mFrames->MarkComplete(mDecoder->GetFirstFrameRefreshArea());
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append the new frame to the list.
|
2018-06-04 15:23:00 +03:00
|
|
|
AnimationFrameBuffer::InsertStatus status =
|
|
|
|
mFrames->Insert(std::move(frame));
|
2018-11-29 22:38:28 +03:00
|
|
|
|
|
|
|
// If we hit a redecode error, then we actually want to stop. This will be
|
|
|
|
// fully handled in FinishDecoding.
|
|
|
|
if (mFrames->HasRedecodeError()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
switch (status) {
|
|
|
|
case AnimationFrameBuffer::InsertStatus::DISCARD_CONTINUE:
|
|
|
|
case AnimationFrameBuffer::InsertStatus::DISCARD_YIELD:
|
|
|
|
RequestFrameDiscarding();
|
|
|
|
break;
|
|
|
|
case AnimationFrameBuffer::InsertStatus::CONTINUE:
|
|
|
|
case AnimationFrameBuffer::InsertStatus::YIELD:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled insert status!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continueDecoding =
|
|
|
|
mFrames->MarkComplete(mDecoder->GetFirstFrameRefreshArea());
|
2016-08-18 10:01:10 +03:00
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
// We only want to handle the first frame if it is the first pass for the
|
|
|
|
// animation decoder. The owning image will be cleared after that.
|
2018-06-04 15:23:00 +03:00
|
|
|
if (mFrames->Size() == 1 && mImage) {
|
2016-08-18 10:01:10 +03:00
|
|
|
justGotFirstFrame = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (justGotFirstFrame) {
|
|
|
|
AnnounceSurfaceAvailable();
|
|
|
|
}
|
2018-02-28 21:34:53 +03:00
|
|
|
|
2018-04-24 20:51:35 +03:00
|
|
|
return continueDecoding;
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:23:00 +03:00
|
|
|
void AnimationSurfaceProvider::RequestFrameDiscarding() {
|
2018-06-04 15:29:50 +03:00
|
|
|
mDecodingMutex.AssertCurrentThreadOwns();
|
|
|
|
mFramesMutex.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
|
2018-06-04 15:33:16 +03:00
|
|
|
if (mFrames->MayDiscard() || mFrames->IsRecycling()) {
|
2018-06-04 15:29:50 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Already replaced frame queue!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto oldFrameQueue =
|
|
|
|
static_cast<AnimationFrameRetainedBuffer*>(mFrames.get());
|
2018-06-04 15:33:16 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!mDecoder->GetFrameRecycler());
|
2019-05-26 17:29:42 +03:00
|
|
|
if (StaticPrefs::ImageAnimatedDecodeOnDemandRecycle()) {
|
2018-06-04 15:33:16 +03:00
|
|
|
mFrames.reset(new AnimationFrameRecyclingQueue(std::move(*oldFrameQueue)));
|
|
|
|
mDecoder->SetFrameRecycler(this);
|
|
|
|
} else {
|
|
|
|
mFrames.reset(new AnimationFrameDiscardingQueue(std::move(*oldFrameQueue)));
|
|
|
|
}
|
2018-06-04 15:23:00 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 10:01:10 +03:00
|
|
|
void AnimationSurfaceProvider::AnnounceSurfaceAvailable() {
|
|
|
|
mFramesMutex.AssertNotCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mImage);
|
|
|
|
|
2016-08-18 10:06:41 +03:00
|
|
|
// We just got the first frame; let the surface cache know. We deliberately do
|
|
|
|
// this outside of mFramesMutex to avoid a potential deadlock with
|
|
|
|
// AddSizeOfExcludingThis(), since otherwise we'd be acquiring mFramesMutex
|
|
|
|
// and then the surface cache lock, while the memory reporting code would
|
|
|
|
// acquire the surface cache lock and then mFramesMutex.
|
2016-08-20 06:26:59 +03:00
|
|
|
SurfaceCache::SurfaceAvailable(WrapNotNull(this));
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationSurfaceProvider::FinishDecoding() {
|
|
|
|
mDecodingMutex.AssertCurrentThreadOwns();
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
|
2018-02-28 21:34:53 +03:00
|
|
|
if (mImage) {
|
|
|
|
// Send notifications.
|
|
|
|
NotifyDecodeComplete(WrapNotNull(mImage), WrapNotNull(mDecoder));
|
|
|
|
}
|
2016-08-18 10:01:10 +03:00
|
|
|
|
2018-04-24 20:51:35 +03:00
|
|
|
// Determine if we need to recreate the decoder, in case we are discarding
|
|
|
|
// frames and need to loop back to the beginning.
|
|
|
|
bool recreateDecoder;
|
2018-02-28 21:34:53 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
2018-06-04 15:23:00 +03:00
|
|
|
recreateDecoder = !mFrames->HasRedecodeError() && mFrames->MayDiscard();
|
2018-02-28 21:34:53 +03:00
|
|
|
}
|
|
|
|
|
2018-04-24 20:51:35 +03:00
|
|
|
if (recreateDecoder) {
|
2018-02-28 21:34:53 +03:00
|
|
|
mDecoder = DecoderFactory::CloneAnimationDecoder(mDecoder);
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
} else {
|
|
|
|
mDecoder = nullptr;
|
|
|
|
}
|
2016-08-18 10:01:10 +03:00
|
|
|
|
|
|
|
// We don't need a reference to our image anymore, either, and we don't want
|
|
|
|
// one. We may be stored in the surface cache for a long time after decoding
|
|
|
|
// finishes. If we don't drop our reference to the image, we'll end up
|
|
|
|
// keeping it alive as long as we remain in the surface cache, which could
|
|
|
|
// greatly extend the image's lifetime - in fact, if the image isn't
|
|
|
|
// discardable, it'd result in a leak!
|
|
|
|
DropImageReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimationSurfaceProvider::ShouldPreferSyncRun() const {
|
|
|
|
MutexAutoLock lock(mDecodingMutex);
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
|
2019-05-26 17:30:14 +03:00
|
|
|
return mDecoder->ShouldSyncDecode(StaticPrefs::ImageMemDecodeBytesAtATime());
|
2016-08-18 10:01:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:33:16 +03:00
|
|
|
RawAccessFrameRef AnimationSurfaceProvider::RecycleFrame(
|
|
|
|
gfx::IntRect& aRecycleRect) {
|
|
|
|
MutexAutoLock lock(mFramesMutex);
|
|
|
|
MOZ_ASSERT(mFrames->IsRecycling());
|
|
|
|
return mFrames->RecycleFrame(aRecycleRect);
|
|
|
|
}
|
|
|
|
|
2016-08-18 10:01:10 +03:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|