2014-11-19 05:17:17 +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 "DecodePool.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2015-05-15 03:08:26 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIThreadPool.h"
|
2015-05-15 03:08:26 +03:00
|
|
|
#include "nsThreadManager.h"
|
2015-01-18 12:27:16 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "prsystem.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gfxPrefs.h"
|
|
|
|
|
|
|
|
#include "Decoder.h"
|
2016-06-29 23:43:19 +03:00
|
|
|
#include "IDecodingTask.h"
|
2014-11-19 05:17:17 +03:00
|
|
|
#include "RasterImage.h"
|
|
|
|
|
|
|
|
using std::max;
|
|
|
|
using std::min;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// DecodePool implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/* static */ StaticRefPtr<DecodePool> DecodePool::sSingleton;
|
2015-07-09 01:52:51 +03:00
|
|
|
/* static */ uint32_t DecodePool::sNumCores = 0;
|
2015-05-15 03:08:26 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(DecodePool, nsIObserver)
|
|
|
|
|
|
|
|
struct Work
|
|
|
|
{
|
|
|
|
enum class Type {
|
2016-06-29 23:43:19 +03:00
|
|
|
TASK,
|
2015-05-15 03:08:26 +03:00
|
|
|
SHUTDOWN
|
|
|
|
} mType;
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
RefPtr<IDecodingTask> mTask;
|
2014-11-19 05:17:17 +03:00
|
|
|
};
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
class DecodePoolImpl
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-05-15 03:08:26 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(DecodePoolImpl)
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DecodePoolImpl)
|
|
|
|
|
|
|
|
DecodePoolImpl()
|
|
|
|
: mMonitor("DecodePoolImpl")
|
|
|
|
, mShuttingDown(false)
|
|
|
|
{ }
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
/// Initialize the current thread for use by the decode pool.
|
|
|
|
void InitCurrentThread()
|
2015-01-18 12:27:16 +03:00
|
|
|
{
|
2015-05-15 03:08:26 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
mThreadNaming.SetThreadPoolName(NS_LITERAL_CSTRING("ImgDecoder"));
|
|
|
|
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
2014-11-19 05:17:17 +03:00
|
|
|
if (IsNuwaProcess()) {
|
2015-01-18 12:27:16 +03:00
|
|
|
NuwaMarkCurrentThread(static_cast<void(*)(void*)>(nullptr), nullptr);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
2015-05-15 03:08:26 +03:00
|
|
|
#endif // MOZ_NUWA_PROCESS
|
2015-01-18 12:27:16 +03:00
|
|
|
}
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
/// Shut down the provided decode pool thread.
|
|
|
|
static void ShutdownThread(nsIThread* aThisThread)
|
|
|
|
{
|
|
|
|
// Threads have to be shut down from another thread, so we'll ask the
|
|
|
|
// main thread to do it for us.
|
2016-05-05 11:45:00 +03:00
|
|
|
NS_DispatchToMainThread(NewRunnableMethod(aThisThread, &nsIThread::Shutdown));
|
2015-05-15 03:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests shutdown. New work items will be dropped on the floor, and all
|
|
|
|
* decode pool threads will be shut down once existing work items have been
|
|
|
|
* processed.
|
|
|
|
*/
|
|
|
|
void RequestShutdown()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mShuttingDown = true;
|
|
|
|
mMonitor.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes a new decode work item.
|
2016-06-29 23:43:19 +03:00
|
|
|
void PushWork(IDecodingTask* aTask)
|
2015-05-15 03:08:26 +03:00
|
|
|
{
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
RefPtr<IDecodingTask> task(aTask);
|
2015-05-15 03:08:26 +03:00
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
if (mShuttingDown) {
|
|
|
|
// Drop any new work on the floor if we're shutting down.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
if (task->Priority() == TaskPriority::eHigh) {
|
|
|
|
mHighPriorityQueue.AppendElement(Move(task));
|
2015-05-08 05:12:00 +03:00
|
|
|
} else {
|
2016-06-29 23:43:19 +03:00
|
|
|
mLowPriorityQueue.AppendElement(Move(task));
|
2015-05-08 05:12:00 +03:00
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
mMonitor.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pops a new work item, blocking if necessary.
|
|
|
|
Work PopWork()
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
do {
|
2016-06-29 23:43:19 +03:00
|
|
|
if (!mHighPriorityQueue.IsEmpty()) {
|
|
|
|
return PopWorkFromQueue(mHighPriorityQueue);
|
2015-05-08 05:12:00 +03:00
|
|
|
}
|
2015-05-15 03:08:26 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
if (!mLowPriorityQueue.IsEmpty()) {
|
|
|
|
return PopWorkFromQueue(mLowPriorityQueue);
|
2015-05-15 03:08:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mShuttingDown) {
|
2015-05-08 05:12:00 +03:00
|
|
|
Work work;
|
2015-05-15 03:08:26 +03:00
|
|
|
work.mType = Work::Type::SHUTDOWN;
|
|
|
|
return work;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do; block until some work is available.
|
|
|
|
mMonitor.Wait();
|
|
|
|
} while (true);
|
|
|
|
}
|
2015-01-18 12:27:16 +03:00
|
|
|
|
|
|
|
private:
|
2015-05-15 03:08:26 +03:00
|
|
|
~DecodePoolImpl() { }
|
2015-01-18 12:27:16 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
Work PopWorkFromQueue(nsTArray<RefPtr<IDecodingTask>>& aQueue)
|
2015-05-08 05:12:00 +03:00
|
|
|
{
|
|
|
|
Work work;
|
2016-06-29 23:43:19 +03:00
|
|
|
work.mType = Work::Type::TASK;
|
|
|
|
work.mTask = aQueue.LastElement().forget();
|
2015-06-24 21:37:59 +03:00
|
|
|
aQueue.RemoveElementAt(aQueue.Length() - 1);
|
2015-05-08 05:12:00 +03:00
|
|
|
|
|
|
|
return work;
|
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
nsThreadPoolNaming mThreadNaming;
|
2015-01-18 12:27:16 +03:00
|
|
|
|
2015-07-23 08:39:54 +03:00
|
|
|
// mMonitor guards the queues and mShuttingDown.
|
2015-05-15 03:08:26 +03:00
|
|
|
Monitor mMonitor;
|
2016-06-29 23:43:19 +03:00
|
|
|
nsTArray<RefPtr<IDecodingTask>> mHighPriorityQueue;
|
|
|
|
nsTArray<RefPtr<IDecodingTask>> mLowPriorityQueue;
|
2015-05-15 03:08:26 +03:00
|
|
|
bool mShuttingDown;
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class DecodePoolWorker : public Runnable
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2015-01-18 12:27:16 +03:00
|
|
|
public:
|
2015-05-15 03:08:26 +03:00
|
|
|
explicit DecodePoolWorker(DecodePoolImpl* aImpl) : mImpl(aImpl) { }
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2015-05-15 03:08:26 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
mImpl->InitCurrentThread();
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
nsCOMPtr<nsIThread> thisThread;
|
|
|
|
nsThreadManager::get()->GetCurrentThread(getter_AddRefs(thisThread));
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
do {
|
|
|
|
Work work = mImpl->PopWork();
|
|
|
|
switch (work.mType) {
|
2016-06-29 23:43:19 +03:00
|
|
|
case Work::Type::TASK:
|
|
|
|
work.mTask->Run();
|
2015-05-15 03:08:26 +03:00
|
|
|
break;
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
case Work::Type::SHUTDOWN:
|
|
|
|
DecodePoolImpl::ShutdownThread(thisThread);
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown work type");
|
|
|
|
}
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Exiting thread without Work::Type::SHUTDOWN");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DecodePoolImpl> mImpl;
|
2015-05-15 03:08:26 +03:00
|
|
|
};
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
/* static */ void
|
|
|
|
DecodePool::Initialize()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-11-01 20:45:40 +03:00
|
|
|
sNumCores = max<int32_t>(PR_GetNumberOfProcessors(), 1);
|
2015-01-18 12:27:16 +03:00
|
|
|
DecodePool::Singleton();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
/* static */ DecodePool*
|
|
|
|
DecodePool::Singleton()
|
|
|
|
{
|
|
|
|
if (!sSingleton) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
sSingleton = new DecodePool();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
2015-07-09 01:52:51 +03:00
|
|
|
/* static */ uint32_t
|
|
|
|
DecodePool::NumberOfCores()
|
|
|
|
{
|
|
|
|
return sNumCores;
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
DecodePool::DecodePool()
|
2015-05-15 03:08:26 +03:00
|
|
|
: mImpl(new DecodePoolImpl)
|
|
|
|
, mMutex("image::DecodePool")
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2015-05-15 03:08:26 +03:00
|
|
|
// Determine the number of threads we want.
|
2015-01-08 11:29:41 +03:00
|
|
|
int32_t prefLimit = gfxPrefs::ImageMTDecodingLimit();
|
|
|
|
uint32_t limit;
|
|
|
|
if (prefLimit <= 0) {
|
2015-07-09 01:52:51 +03:00
|
|
|
int32_t numCores = NumberOfCores();
|
2015-05-21 04:54:16 +03:00
|
|
|
if (numCores <= 1) {
|
|
|
|
limit = 1;
|
|
|
|
} else if (numCores == 2) {
|
|
|
|
// On an otherwise mostly idle system, having two image decoding threads
|
|
|
|
// doubles decoding performance, so it's worth doing on dual-core devices,
|
|
|
|
// even if under load we can't actually get that level of parallelism.
|
|
|
|
limit = 2;
|
|
|
|
} else {
|
|
|
|
limit = numCores - 1;
|
|
|
|
}
|
2015-01-08 11:29:41 +03:00
|
|
|
} else {
|
|
|
|
limit = static_cast<uint32_t>(prefLimit);
|
|
|
|
}
|
2015-11-01 20:45:40 +03:00
|
|
|
if (limit > 32) {
|
|
|
|
limit = 32;
|
|
|
|
}
|
2015-01-08 11:29:41 +03:00
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
// Initialize the thread pool.
|
|
|
|
for (uint32_t i = 0 ; i < limit ; ++i) {
|
|
|
|
nsCOMPtr<nsIRunnable> worker = new DecodePoolWorker(mImpl);
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_NewThread(getter_AddRefs(thread), worker);
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv) && thread,
|
|
|
|
"Should successfully create image decoding threads");
|
|
|
|
mThreads.AppendElement(Move(thread));
|
2015-01-08 11:29:41 +03:00
|
|
|
}
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2015-01-18 12:27:16 +03: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
|
2016-06-26 08:11:41 +03:00
|
|
|
rv = mIOThread->Dispatch(NS_NewRunnableFunction([]() -> void {
|
|
|
|
NuwaMarkCurrentThread(static_cast<void(*)(void*)>(nullptr), nullptr);
|
|
|
|
}), NS_DISPATCH_NORMAL);
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv),
|
|
|
|
"Should register decode IO thread with Nuwa process");
|
|
|
|
#endif
|
|
|
|
|
2015-01-08 11:29:41 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
|
|
|
|
if (obsSvc) {
|
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown-threads", false);
|
2014-11-19 05:17:17 +03: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");
|
|
|
|
|
2016-05-01 21:29:22 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIThread>> threads;
|
2015-01-18 12:27:16 +03:00
|
|
|
nsCOMPtr<nsIThread> ioThread;
|
2014-11-19 05:17:17 +03:00
|
|
|
|
|
|
|
{
|
2015-01-18 12:27:16 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2016-05-01 21:29:22 +03:00
|
|
|
threads.SwapElements(mThreads);
|
2015-01-18 12:27:16 +03:00
|
|
|
ioThread.swap(mIOThread);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2015-05-15 03:08:26 +03:00
|
|
|
mImpl->RequestShutdown();
|
|
|
|
|
2016-05-01 21:29:22 +03:00
|
|
|
for (uint32_t i = 0 ; i < threads.Length() ; ++i) {
|
2015-05-15 03:08:26 +03:00
|
|
|
threads[i]->Shutdown();
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
if (ioThread) {
|
|
|
|
ioThread->Shutdown();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-29 23:43:19 +03:00
|
|
|
DecodePool::AsyncRun(IDecodingTask* aTask)
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
mImpl->PushWork(aTask);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-29 23:43:19 +03:00
|
|
|
DecodePool::SyncRunIfPreferred(IDecodingTask* aTask)
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
2014-11-19 05:17:17 +03:00
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
if (aTask->ShouldPreferSyncRun()) {
|
|
|
|
aTask->Run();
|
2015-01-16 02:11:36 +03:00
|
|
|
return;
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2016-06-29 23:43:19 +03:00
|
|
|
AsyncRun(aTask);
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:11:36 +03:00
|
|
|
void
|
2016-06-29 23:43:19 +03:00
|
|
|
DecodePool::SyncRunIfPossible(IDecodingTask* aTask)
|
2014-11-19 05:17:17 +03:00
|
|
|
{
|
2015-01-16 02:11:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-06-29 23:43:19 +03:00
|
|
|
MOZ_ASSERT(aTask);
|
|
|
|
aTask->Run();
|
2014-11-19 05:17:17 +03:00
|
|
|
}
|
|
|
|
|
2015-01-18 12:27:16 +03:00
|
|
|
already_AddRefed<nsIEventTarget>
|
|
|
|
DecodePool::GetIOEventTarget()
|
|
|
|
{
|
|
|
|
MutexAutoLock threadPoolLock(mMutex);
|
|
|
|
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(mIOThread);
|
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2014-11-19 05:17:17 +03:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|