2014-11-18 18:17:17 -08: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 "DecodePool.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIThreadPool.h"
|
2015-01-18 01:27:16 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2014-11-18 18:17:17 -08:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "prsystem.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
|
|
|
|
#include "Decoder.h"
|
|
|
|
#include "RasterImage.h"
|
|
|
|
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper runnables.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class NotifyProgressWorker : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Called by the DecodePool when it's done some significant portion of
|
|
|
|
* decoding, so that progress can be recorded and notifications can be sent.
|
|
|
|
*/
|
2015-01-15 15:11:36 -08:00
|
|
|
static void Dispatch(RasterImage* aImage,
|
|
|
|
Progress aProgress,
|
|
|
|
const nsIntRect& aInvalidRect,
|
|
|
|
uint32_t aFlags)
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> worker =
|
|
|
|
new NotifyProgressWorker(aImage, aProgress, aInvalidRect, aFlags);
|
2014-11-18 18:17:17 -08:00
|
|
|
NS_DispatchToMainThread(worker);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-15 15:11:36 -08:00
|
|
|
mImage->NotifyProgress(mProgress, mInvalidRect, mFlags);
|
2014-11-18 18:17:17 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-15 15:11:36 -08:00
|
|
|
NotifyProgressWorker(RasterImage* aImage, Progress aProgress,
|
|
|
|
const nsIntRect& aInvalidRect, uint32_t aFlags)
|
2014-11-18 18:17:17 -08:00
|
|
|
: mImage(aImage)
|
2015-01-15 15:11:36 -08:00
|
|
|
, mProgress(aProgress)
|
|
|
|
, mInvalidRect(aInvalidRect)
|
|
|
|
, mFlags(aFlags)
|
2014-11-18 18:17:17 -08:00
|
|
|
{ }
|
|
|
|
|
|
|
|
nsRefPtr<RasterImage> mImage;
|
2015-01-15 15:11:36 -08:00
|
|
|
const Progress mProgress;
|
|
|
|
const nsIntRect mInvalidRect;
|
|
|
|
const uint32_t mFlags;
|
2014-11-18 18:17:17 -08:00
|
|
|
};
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
class NotifyDecodeCompleteWorker : public nsRunnable
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
|
|
|
public:
|
2015-01-15 15:11:36 -08:00
|
|
|
/**
|
|
|
|
* Called by the DecodePool when decoding is complete, so that final cleanup
|
|
|
|
* can be performed.
|
|
|
|
*/
|
|
|
|
static void Dispatch(Decoder* aDecoder)
|
2015-01-11 05:34:20 -08:00
|
|
|
{
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
nsCOMPtr<nsIRunnable> worker = new NotifyDecodeCompleteWorker(aDecoder);
|
|
|
|
NS_DispatchToMainThread(worker);
|
|
|
|
}
|
2015-01-11 11:43:32 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
DecodePool::Singleton()->NotifyDecodeComplete(mDecoder);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-11 11:43:32 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
private:
|
|
|
|
explicit NotifyDecodeCompleteWorker(Decoder* aDecoder)
|
|
|
|
: mDecoder(aDecoder)
|
|
|
|
{ }
|
2015-01-11 11:43:32 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
nsRefPtr<Decoder> mDecoder;
|
|
|
|
};
|
2015-01-11 11:43:32 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
class DecodeWorker : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit DecodeWorker(Decoder* aDecoder)
|
|
|
|
: mDecoder(aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
DecodePool::Singleton()->Decode(mDecoder);
|
|
|
|
return NS_OK;
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-15 15:11:36 -08:00
|
|
|
nsRefPtr<Decoder> mDecoder;
|
2014-11-18 18:17:17 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
class DecodePoolNuwaListener MOZ_FINAL : public nsIThreadPoolListener
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
NS_IMETHODIMP OnThreadCreated()
|
|
|
|
{
|
2014-11-18 18:17:17 -08:00
|
|
|
if (IsNuwaProcess()) {
|
2015-01-18 01:27:16 -08:00
|
|
|
NuwaMarkCurrentThread(static_cast<void(*)(void*)>(nullptr), nullptr);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2015-01-18 01:27:16 -08:00
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
NS_IMETHODIMP OnThreadShuttingDown() { return NS_OK; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~DecodePoolNuwaListener() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(DecodePoolNuwaListener, nsIThreadPoolListener)
|
|
|
|
|
|
|
|
class RegisterDecodeIOThreadWithNuwaRunnable : public nsRunnable
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-18 01:27:16 -08:00
|
|
|
public:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NuwaMarkCurrentThread(static_cast<void(*)(void*)>(nullptr), nullptr);
|
2014-11-18 18:17:17 -08:00
|
|
|
return NS_OK;
|
2015-01-18 01:27:16 -08:00
|
|
|
}
|
|
|
|
};
|
2014-11-18 18:17:17 -08:00
|
|
|
#endif // MOZ_NUWA_PROCESS
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DecodePool implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* static */ StaticRefPtr<DecodePool> DecodePool::sSingleton;
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(DecodePool, nsIObserver)
|
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
/* static */ void
|
|
|
|
DecodePool::Initialize()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
DecodePool::Singleton();
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:17:17 -08:00
|
|
|
/* static */ DecodePool*
|
|
|
|
DecodePool::Singleton()
|
|
|
|
{
|
|
|
|
if (!sSingleton) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
sSingleton = new DecodePool();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
DecodePool::DecodePool()
|
2015-01-18 01:27:16 -08:00
|
|
|
: mMutex("image::DecodePool")
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-18 01:27:16 -08:00
|
|
|
// Initialize the thread pool.
|
2015-01-08 00:29:41 -08:00
|
|
|
mThreadPool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
|
|
|
|
MOZ_RELEASE_ASSERT(mThreadPool,
|
|
|
|
"Should succeed in creating image decoding thread pool");
|
|
|
|
|
|
|
|
mThreadPool->SetName(NS_LITERAL_CSTRING("ImageDecoder"));
|
|
|
|
int32_t prefLimit = gfxPrefs::ImageMTDecodingLimit();
|
|
|
|
uint32_t limit;
|
|
|
|
if (prefLimit <= 0) {
|
|
|
|
limit = max(PR_GetNumberOfProcessors(), 2) - 1;
|
|
|
|
} else {
|
|
|
|
limit = static_cast<uint32_t>(prefLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
mThreadPool->SetThreadLimit(limit);
|
|
|
|
mThreadPool->SetIdleThreadLimit(limit);
|
2014-11-18 18:17:17 -08:00
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
2015-01-08 00:29:41 -08:00
|
|
|
if (IsNuwaProcess()) {
|
2015-01-18 01:27:16 -08:00
|
|
|
mThreadPool->SetListener(new DecodePoolNuwaListener());
|
2015-01-08 00:29:41 -08:00
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
#endif
|
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
// Initialize the I/O thread.
|
|
|
|
nsresult rv = NS_NewNamedThread("ImageIO", getter_AddRefs(mIOThread));
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv) && mIOThread,
|
|
|
|
"Should successfully create image I/O thread");
|
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
nsCOMPtr<nsIRunnable> worker = new RegisterDecodeIOThreadWithNuwaRunnable();
|
|
|
|
rv = mIOThread->Dispatch(worker, NS_DISPATCH_NORMAL);
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv),
|
|
|
|
"Should register decode IO thread with Nuwa process");
|
|
|
|
#endif
|
|
|
|
|
2015-01-08 00:29:41 -08:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
|
|
|
|
if (obsSvc) {
|
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown-threads", false);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DecodePool::~DecodePool()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must shut down DecodePool on main thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DecodePool::Observe(nsISupports*, const char* aTopic, const char16_t*)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(strcmp(aTopic, "xpcom-shutdown-threads") == 0, "Unexpected topic");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThreadPool> threadPool;
|
2015-01-18 01:27:16 -08:00
|
|
|
nsCOMPtr<nsIThread> ioThread;
|
2014-11-18 18:17:17 -08:00
|
|
|
|
|
|
|
{
|
2015-01-18 01:27:16 -08:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
threadPool.swap(mThreadPool);
|
|
|
|
ioThread.swap(mIOThread);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (threadPool) {
|
|
|
|
threadPool->Shutdown();
|
|
|
|
}
|
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
if (ioThread) {
|
|
|
|
ioThread->Shutdown();
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:17:17 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-15 15:11:36 -08:00
|
|
|
DecodePool::AsyncDecode(Decoder* aDecoder)
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
nsCOMPtr<nsIRunnable> worker = new DecodeWorker(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-08 00:29:41 -08:00
|
|
|
// Dispatch to the thread pool if it exists. If it doesn't, we're currently
|
|
|
|
// shutting down, so it's OK to just drop the job on the floor.
|
2015-01-18 01:27:16 -08:00
|
|
|
MutexAutoLock threadPoolLock(mMutex);
|
2015-01-08 00:29:41 -08:00
|
|
|
if (mThreadPool) {
|
2015-01-08 00:04:31 -08:00
|
|
|
mThreadPool->Dispatch(worker, nsIEventTarget::DISPATCH_NORMAL);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-15 15:11:36 -08:00
|
|
|
DecodePool::SyncDecodeIfSmall(Decoder* aDecoder)
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
if (aDecoder->ShouldSyncDecode(gfxPrefs::ImageMemDecodeBytesAtATime())) {
|
|
|
|
Decode(aDecoder);
|
|
|
|
return;
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
AsyncDecode(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
void
|
|
|
|
DecodePool::SyncDecodeIfPossible(Decoder* aDecoder)
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
Decode(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
DecodePool::GetEventTarget()
|
2014-11-18 18:17:17 -08:00
|
|
|
{
|
2015-01-18 01:27:16 -08:00
|
|
|
MutexAutoLock threadPoolLock(mMutex);
|
2015-01-15 15:11:36 -08:00
|
|
|
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(mThreadPool);
|
|
|
|
return target.forget();
|
2015-01-11 05:34:20 -08:00
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-18 01:27:16 -08:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
DecodePool::GetIOEventTarget()
|
|
|
|
{
|
|
|
|
MutexAutoLock threadPoolLock(mMutex);
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(mIOThread);
|
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
already_AddRefed<nsIRunnable>
|
|
|
|
DecodePool::CreateDecodeWorker(Decoder* aDecoder)
|
2015-01-11 05:34:20 -08:00
|
|
|
{
|
2015-01-15 15:11:36 -08:00
|
|
|
MOZ_ASSERT(aDecoder);
|
|
|
|
nsCOMPtr<nsIRunnable> worker = new DecodeWorker(aDecoder);
|
|
|
|
return worker.forget();
|
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
void
|
|
|
|
DecodePool::Decode(Decoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDecoder);
|
2015-01-11 05:34:20 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
nsresult rv = aDecoder->Decode();
|
2015-01-11 11:43:32 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
if (NS_SUCCEEDED(rv) && !aDecoder->GetDecodeDone()) {
|
|
|
|
if (aDecoder->HasProgress()) {
|
|
|
|
NotifyProgress(aDecoder);
|
|
|
|
}
|
|
|
|
// The decoder will ensure that a new worker gets enqueued to continue
|
|
|
|
// decoding when more data is available.
|
2015-01-11 11:43:32 -08:00
|
|
|
} else {
|
2015-01-15 15:11:36 -08:00
|
|
|
NotifyDecodeComplete(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
2015-01-15 15:11:36 -08:00
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
void
|
|
|
|
DecodePool::NotifyProgress(Decoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDecoder);
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NotifyProgressWorker::Dispatch(aDecoder->GetImage(),
|
|
|
|
aDecoder->TakeProgress(),
|
|
|
|
aDecoder->TakeInvalidRect(),
|
|
|
|
aDecoder->GetDecodeFlags());
|
|
|
|
return;
|
2015-01-11 11:43:32 -08:00
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
aDecoder->GetImage()->NotifyProgress(aDecoder->TakeProgress(),
|
|
|
|
aDecoder->TakeInvalidRect(),
|
|
|
|
aDecoder->GetDecodeFlags());
|
|
|
|
}
|
2014-11-18 18:17:17 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
void
|
|
|
|
DecodePool::NotifyDecodeComplete(Decoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDecoder);
|
2015-01-11 11:43:32 -08:00
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NotifyDecodeCompleteWorker::Dispatch(aDecoder);
|
|
|
|
return;
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:11:36 -08:00
|
|
|
aDecoder->Finish();
|
|
|
|
aDecoder->GetImage()->FinalizeDecoder(aDecoder);
|
2014-11-18 18:17:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|