Files
palemoon27/image/DecodePool.cpp
T
roytam1 cbd579ebc8 import changes from wicknix/Arctic-Fox:
- backport of Bug 1188696 - Remove the XPCOM dependencies in nsRefPtr.h (6c2860799)
- backport of Bug 1188696 - Hoist nsRefPtr.h into MFBT (e892acb58)
- Backport of Bug 1138967 - Part 3: Add D3D11 YCbCr texture clients and upload on the client side (2e4218167)
- Bug 1038536 - Flatten image/src/ directory (3adb2d635)
- Bug 1038536 - Flatten image/public/ directory (22329f3b4)
- Bug 1038536 - Flatten image/decoders/icon/qt/public/ directory. (7b5b8b2af)
- Bug 1038536 - Update header guard after flatten image/build (20e1614ac)
- Bug 1116905 - part 2 - add MakeAndAddRef helper function to facilitate constructing TemporaryRef (9c85f45a0)
- update (ab2c6eccf)
- Bug 1139781 - Implement VideoPlaybackQuality for MediaCodecReader. (1a7c6c0a7)
- Bug 1138825 - Fix the crash at mAudioPromise: call decode audio data when the audio queue is empty and check the mAudioPromise is empty or not. (3f5d3a1c5)
- Bug 875247 - Add support for DXVA2 via D3D11 (2ca491206)
- Bug 1145513 - Upload YCbCr image data on the client side when using d3d9 layers. (50f7a69fa)
- Bug 1053563 - Use a static create function to replace InitWith for TextureClient. (dd1c8fc89)
- Bug 1145764 - Add some default-disabled logging to TextureClientPool (1cfc0d1b9)
- Bug 1120780 - Fallback on lock_ycbcr when ColorConvertor fails (3a9b893f0)
- Bug 1161815: Use a single ID2D1SolidColorBrush per DrawTarget. (a70b72ef6)
- Bug 1160485 - remove implicit conversion from RefPtr<T> to TemporaryR…ef<T>; (e6e6224c5)
- Bug 1116905 - part 3 - remove dependence on implicit conversion from T* to TemporaryRef<T>, gfx changes; (f71d3ffa8)
- Bug 1116905 - part 1 - remove dependence on implicit conversion from T* to TemporaryRef<T>, non-gfx changes; (f66714955)
- implement Event.srcElement as alias (6c1ee1c6d)
- 1116905 - part 4 - remove implicit conversion from non-nullptr T* to TemporaryRef<T> (f94c680f9)
- Bug 1031152 - Define a JS public API for working with SavedFrame instances (2aa41721a)
- Update TLD's from ESR 60 (b9dbe0ca3)
with some changes to fix building, reported to upstream:
- https://github.com/wicknix/Arctic-Fox/commit/2e421816773b6a57502907ab22c285d994d8b024#r31893045
- https://github.com/wicknix/Arctic-Fox/commit/50f7a69fa9a36634aef1ae5a221415ca98284435#r31892913
- https://github.com/wicknix/Arctic-Fox/commit/f667149556ae0e64c9dbce08836bb5d957db2464#r31892928
2019-01-10 15:30:35 +08:00

380 lines
8.9 KiB
C++

/* -*- 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"
#include "nsThreadUtils.h"
#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.
*/
static void Dispatch(RasterImage* aImage,
Progress aProgress,
const nsIntRect& aInvalidRect,
uint32_t aFlags)
{
MOZ_ASSERT(aImage);
nsCOMPtr<nsIRunnable> worker =
new NotifyProgressWorker(aImage, aProgress, aInvalidRect, aFlags);
NS_DispatchToMainThread(worker);
}
NS_IMETHOD Run() override
{
MOZ_ASSERT(NS_IsMainThread());
mImage->NotifyProgress(mProgress, mInvalidRect, mFlags);
return NS_OK;
}
private:
NotifyProgressWorker(RasterImage* aImage, Progress aProgress,
const nsIntRect& aInvalidRect, uint32_t aFlags)
: mImage(aImage)
, mProgress(aProgress)
, mInvalidRect(aInvalidRect)
, mFlags(aFlags)
{ }
nsRefPtr<RasterImage> mImage;
const Progress mProgress;
const nsIntRect mInvalidRect;
const uint32_t mFlags;
};
class NotifyDecodeCompleteWorker : public nsRunnable
{
public:
/**
* Called by the DecodePool when decoding is complete, so that final cleanup
* can be performed.
*/
static void Dispatch(Decoder* aDecoder)
{
MOZ_ASSERT(aDecoder);
nsCOMPtr<nsIRunnable> worker = new NotifyDecodeCompleteWorker(aDecoder);
NS_DispatchToMainThread(worker);
}
NS_IMETHOD Run() override
{
MOZ_ASSERT(NS_IsMainThread());
mDecoder->Finish();
mDecoder->GetImage()->FinalizeDecoder(mDecoder);
return NS_OK;
}
private:
explicit NotifyDecodeCompleteWorker(Decoder* aDecoder)
: mDecoder(aDecoder)
{ }
nsRefPtr<Decoder> mDecoder;
};
class DecodeWorker : public nsRunnable
{
public:
explicit DecodeWorker(Decoder* aDecoder)
: mDecoder(aDecoder)
{
MOZ_ASSERT(mDecoder);
}
NS_IMETHOD Run() override
{
MOZ_ASSERT(!NS_IsMainThread());
DecodePool::Singleton()->Decode(mDecoder);
return NS_OK;
}
private:
nsRefPtr<Decoder> mDecoder;
};
#ifdef MOZ_NUWA_PROCESS
class DecodePoolNuwaListener final : public nsIThreadPoolListener
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHODIMP OnThreadCreated()
{
if (IsNuwaProcess()) {
NuwaMarkCurrentThread(static_cast<void(*)(void*)>(nullptr), nullptr);
}
return NS_OK;
}
NS_IMETHODIMP OnThreadShuttingDown() { return NS_OK; }
private:
~DecodePoolNuwaListener() { }
};
NS_IMPL_ISUPPORTS(DecodePoolNuwaListener, nsIThreadPoolListener)
class RegisterDecodeIOThreadWithNuwaRunnable : public nsRunnable
{
public:
NS_IMETHOD Run()
{
NuwaMarkCurrentThread(static_cast<void(*)(void*)>(nullptr), nullptr);
return NS_OK;
}
};
#endif // MOZ_NUWA_PROCESS
///////////////////////////////////////////////////////////////////////////////
// DecodePool implementation.
///////////////////////////////////////////////////////////////////////////////
/* static */ StaticRefPtr<DecodePool> DecodePool::sSingleton;
NS_IMPL_ISUPPORTS(DecodePool, nsIObserver)
/* static */ void
DecodePool::Initialize()
{
MOZ_ASSERT(NS_IsMainThread());
DecodePool::Singleton();
}
/* static */ DecodePool*
DecodePool::Singleton()
{
if (!sSingleton) {
MOZ_ASSERT(NS_IsMainThread());
sSingleton = new DecodePool();
ClearOnShutdown(&sSingleton);
}
return sSingleton;
}
DecodePool::DecodePool()
: mMutex("image::DecodePool")
{
// Initialize the thread pool.
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);
#ifdef MOZ_NUWA_PROCESS
if (IsNuwaProcess()) {
mThreadPool->SetListener(new DecodePoolNuwaListener());
}
#endif
// 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
nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
if (obsSvc) {
obsSvc->AddObserver(this, "xpcom-shutdown-threads", false);
}
}
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;
nsCOMPtr<nsIThread> ioThread;
{
MutexAutoLock lock(mMutex);
threadPool.swap(mThreadPool);
ioThread.swap(mIOThread);
}
if (threadPool) {
threadPool->Shutdown();
}
if (ioThread) {
ioThread->Shutdown();
}
return NS_OK;
}
void
DecodePool::AsyncDecode(Decoder* aDecoder)
{
MOZ_ASSERT(aDecoder);
nsCOMPtr<nsIRunnable> worker = new DecodeWorker(aDecoder);
// 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.
MutexAutoLock threadPoolLock(mMutex);
if (mThreadPool) {
mThreadPool->Dispatch(worker, nsIEventTarget::DISPATCH_NORMAL);
}
}
void
DecodePool::SyncDecodeIfSmall(Decoder* aDecoder)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aDecoder);
if (aDecoder->ShouldSyncDecode(gfxPrefs::ImageMemDecodeBytesAtATime())) {
Decode(aDecoder);
return;
}
AsyncDecode(aDecoder);
}
void
DecodePool::SyncDecodeIfPossible(Decoder* aDecoder)
{
MOZ_ASSERT(NS_IsMainThread());
Decode(aDecoder);
}
already_AddRefed<nsIEventTarget>
DecodePool::GetEventTarget()
{
MutexAutoLock threadPoolLock(mMutex);
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(mThreadPool);
return target.forget();
}
already_AddRefed<nsIEventTarget>
DecodePool::GetIOEventTarget()
{
MutexAutoLock threadPoolLock(mMutex);
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(mIOThread);
return target.forget();
}
already_AddRefed<nsIRunnable>
DecodePool::CreateDecodeWorker(Decoder* aDecoder)
{
MOZ_ASSERT(aDecoder);
nsCOMPtr<nsIRunnable> worker = new DecodeWorker(aDecoder);
return worker.forget();
}
void
DecodePool::Decode(Decoder* aDecoder)
{
MOZ_ASSERT(aDecoder);
nsresult rv = aDecoder->Decode();
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.
} else {
NotifyDecodeComplete(aDecoder);
}
}
void
DecodePool::NotifyProgress(Decoder* aDecoder)
{
MOZ_ASSERT(aDecoder);
if (!NS_IsMainThread() ||
(aDecoder->GetFlags() & imgIContainer::FLAG_ASYNC_NOTIFY)) {
NotifyProgressWorker::Dispatch(aDecoder->GetImage(),
aDecoder->TakeProgress(),
aDecoder->TakeInvalidRect(),
aDecoder->GetDecodeFlags());
return;
}
aDecoder->GetImage()->NotifyProgress(aDecoder->TakeProgress(),
aDecoder->TakeInvalidRect(),
aDecoder->GetDecodeFlags());
}
void
DecodePool::NotifyDecodeComplete(Decoder* aDecoder)
{
MOZ_ASSERT(aDecoder);
if (!NS_IsMainThread() ||
(aDecoder->GetFlags() & imgIContainer::FLAG_ASYNC_NOTIFY)) {
NotifyDecodeCompleteWorker::Dispatch(aDecoder);
return;
}
aDecoder->Finish();
aDecoder->GetImage()->FinalizeDecoder(aDecoder);
}
} // namespace image
} // namespace mozilla