2016-06-29 23:43:19 +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 "IDecodingTask.h"
|
|
|
|
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
#include "Decoder.h"
|
|
|
|
#include "DecodePool.h"
|
|
|
|
#include "RasterImage.h"
|
2016-08-05 04:59:10 +03:00
|
|
|
#include "SurfaceCache.h"
|
2016-06-29 23:43:19 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
2016-08-06 03:09:47 +03:00
|
|
|
|
|
|
|
using gfx::IntRect;
|
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
namespace image {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helpers for sending notifications to the image associated with a decoder.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-07-28 05:40:56 +03:00
|
|
|
/* static */ void
|
|
|
|
IDecodingTask::NotifyProgress(NotNull<RasterImage*> aImage,
|
|
|
|
NotNull<Decoder*> aDecoder)
|
2016-06-29 23:43:19 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDecoder->HasProgress() && !aDecoder->IsMetadataDecode());
|
|
|
|
|
2016-08-02 01:33:13 +03:00
|
|
|
// Capture the decoder's state. If we need to notify asynchronously, it's
|
|
|
|
// important that we don't wait until the lambda actually runs to capture the
|
|
|
|
// state that we're going to notify. That would both introduce data races on
|
|
|
|
// the decoder's state and cause inconsistencies between the NotifyProgress()
|
|
|
|
// calls we make off-main-thread and the notifications that RasterImage
|
|
|
|
// actually receives, which would cause bugs.
|
2016-07-22 18:04:09 +03:00
|
|
|
Progress progress = aDecoder->TakeProgress();
|
2016-07-28 03:12:25 +03:00
|
|
|
IntRect invalidRect = aDecoder->TakeInvalidRect();
|
|
|
|
Maybe<uint32_t> frameCount = aDecoder->TakeCompleteFrameCount();
|
2016-08-18 10:06:41 +03:00
|
|
|
DecoderFlags decoderFlags = aDecoder->GetDecoderFlags();
|
2016-07-22 18:04:09 +03:00
|
|
|
SurfaceFlags surfaceFlags = aDecoder->GetSurfaceFlags();
|
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
// Synchronously notify if we can.
|
2016-08-18 10:06:41 +03:00
|
|
|
if (NS_IsMainThread() && !(decoderFlags & DecoderFlags::ASYNC_NOTIFY)) {
|
|
|
|
aImage->NotifyProgress(progress, invalidRect, frameCount,
|
|
|
|
decoderFlags, surfaceFlags);
|
2016-06-29 23:43:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're forced to notify asynchronously.
|
2016-08-05 05:04:14 +03:00
|
|
|
NotNull<RefPtr<RasterImage>> image = aImage;
|
2017-02-11 09:11:48 +03:00
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction(
|
|
|
|
"IDecodingTask::NotifyProgress",
|
|
|
|
[=]() -> void {
|
2016-08-18 10:06:41 +03:00
|
|
|
image->NotifyProgress(progress, invalidRect, frameCount,
|
|
|
|
decoderFlags, surfaceFlags);
|
2016-06-29 23:43:19 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-07-28 05:40:56 +03:00
|
|
|
/* static */ void
|
|
|
|
IDecodingTask::NotifyDecodeComplete(NotNull<RasterImage*> aImage,
|
|
|
|
NotNull<Decoder*> aDecoder)
|
2016-06-29 23:43:19 +03:00
|
|
|
{
|
2016-08-03 00:26:03 +03:00
|
|
|
MOZ_ASSERT(aDecoder->HasError() || !aDecoder->InFrame(),
|
|
|
|
"Decode complete in the middle of a frame?");
|
|
|
|
|
2016-08-03 01:43:47 +03:00
|
|
|
// Capture the decoder's state.
|
2016-08-03 03:12:36 +03:00
|
|
|
DecoderFinalStatus finalStatus = aDecoder->FinalStatus();
|
2016-08-03 01:43:47 +03:00
|
|
|
ImageMetadata metadata = aDecoder->GetImageMetadata();
|
2016-08-03 02:34:23 +03:00
|
|
|
DecoderTelemetry telemetry = aDecoder->Telemetry();
|
2016-08-03 02:32:07 +03:00
|
|
|
Progress progress = aDecoder->TakeProgress();
|
|
|
|
IntRect invalidRect = aDecoder->TakeInvalidRect();
|
|
|
|
Maybe<uint32_t> frameCount = aDecoder->TakeCompleteFrameCount();
|
2016-08-18 10:06:41 +03:00
|
|
|
DecoderFlags decoderFlags = aDecoder->GetDecoderFlags();
|
2016-08-03 02:32:07 +03:00
|
|
|
SurfaceFlags surfaceFlags = aDecoder->GetSurfaceFlags();
|
2016-08-03 01:43:47 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
// Synchronously notify if we can.
|
2016-08-18 10:06:41 +03:00
|
|
|
if (NS_IsMainThread() && !(decoderFlags & DecoderFlags::ASYNC_NOTIFY)) {
|
2016-08-03 03:22:41 +03:00
|
|
|
aImage->NotifyDecodeComplete(finalStatus, metadata, telemetry, progress,
|
2016-08-18 10:06:41 +03:00
|
|
|
invalidRect, frameCount, decoderFlags,
|
|
|
|
surfaceFlags);
|
2016-06-29 23:43:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're forced to notify asynchronously.
|
2016-08-05 05:04:14 +03:00
|
|
|
NotNull<RefPtr<RasterImage>> image = aImage;
|
2017-02-11 09:11:48 +03:00
|
|
|
NS_DispatchToMainThread(NS_NewRunnableFunction(
|
|
|
|
"IDecodingTask::NotifyDecodeComplete",
|
|
|
|
[=]() -> void {
|
2016-08-03 03:22:41 +03:00
|
|
|
image->NotifyDecodeComplete(finalStatus, metadata, telemetry, progress,
|
2016-08-18 10:06:41 +03:00
|
|
|
invalidRect, frameCount, decoderFlags,
|
|
|
|
surfaceFlags);
|
2016-06-29 23:43:19 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// IDecodingTask implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void
|
|
|
|
IDecodingTask::Resume()
|
|
|
|
{
|
|
|
|
DecodePool::Singleton()->AsyncRun(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MetadataDecodingTask implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
MetadataDecodingTask::MetadataDecodingTask(NotNull<Decoder*> aDecoder)
|
2016-08-18 17:23:42 +03:00
|
|
|
: mMutex("mozilla::image::MetadataDecodingTask")
|
|
|
|
, mDecoder(aDecoder)
|
2016-06-29 23:43:19 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDecoder->IsMetadataDecode(),
|
|
|
|
"Use DecodingTask for non-metadata decodes");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MetadataDecodingTask::Run()
|
|
|
|
{
|
2016-08-18 17:23:42 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2016-07-19 09:46:35 +03:00
|
|
|
LexerResult result = mDecoder->Decode(WrapNotNull(this));
|
2016-06-29 23:43:19 +03:00
|
|
|
|
2016-07-19 09:46:35 +03:00
|
|
|
if (result.is<TerminalState>()) {
|
2016-08-05 05:04:14 +03:00
|
|
|
NotifyDecodeComplete(mDecoder->GetImage(), mDecoder);
|
2016-07-19 09:46:35 +03:00
|
|
|
return; // We're done.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LexerResult(Yield::NEED_MORE_DATA)) {
|
|
|
|
// We can't make any more progress right now. We also don't want to report
|
|
|
|
// any progress, because it's important that metadata decode results are
|
|
|
|
// delivered atomically. The decoder itself will ensure that we get
|
|
|
|
// reenqueued when more data is available; just return for now.
|
2016-06-29 23:43:19 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:46:35 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Metadata decode yielded for an unexpected reason");
|
2016-06-29 23:43:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// AnonymousDecodingTask implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
AnonymousDecodingTask::AnonymousDecodingTask(NotNull<Decoder*> aDecoder)
|
|
|
|
: mDecoder(aDecoder)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
AnonymousDecodingTask::Run()
|
|
|
|
{
|
2016-07-19 09:46:35 +03:00
|
|
|
while (true) {
|
|
|
|
LexerResult result = mDecoder->Decode(WrapNotNull(this));
|
|
|
|
|
|
|
|
if (result.is<TerminalState>()) {
|
|
|
|
return; // We're done.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LexerResult(Yield::NEED_MORE_DATA)) {
|
|
|
|
// We can't make any more progress right now. Let the caller decide how to
|
|
|
|
// handle it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right now we don't do anything special for other kinds of yields, so just
|
|
|
|
// keep working.
|
|
|
|
MOZ_ASSERT(result.is<Yield>());
|
|
|
|
}
|
2016-06-29 23:43:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|