2014-11-19 02:17:17 +00: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/. */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DecodePool manages the threads used for decoding raster images.
|
|
|
|
*/
|
|
|
|
|
2015-05-15 03:52:05 +00:00
|
|
|
#ifndef mozilla_image_DecodePool_h
|
|
|
|
#define mozilla_image_DecodePool_h
|
2014-11-19 02:17:17 +00:00
|
|
|
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2015-05-15 00:08:26 +00:00
|
|
|
#include "nsCOMArray.h"
|
2014-11-19 02:17:17 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIObserver.h"
|
2015-10-18 05:24:48 +00:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-11-19 02:17:17 +00:00
|
|
|
|
2015-01-18 09:27:16 +00:00
|
|
|
class nsIThread;
|
2014-11-19 02:17:17 +00:00
|
|
|
class nsIThreadPool;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
class Decoder;
|
2015-05-15 00:08:26 +00:00
|
|
|
class DecodePoolImpl;
|
2014-11-19 02:17:17 +00:00
|
|
|
|
|
|
|
/**
|
2015-01-15 23:11:36 +00:00
|
|
|
* DecodePool is a singleton class that manages decoding of raster images. It
|
|
|
|
* owns a pool of image decoding threads that are used for asynchronous
|
|
|
|
* decoding.
|
2014-11-19 02:17:17 +00:00
|
|
|
*
|
2015-01-15 23:11:36 +00:00
|
|
|
* DecodePool allows callers to run a decoder, handling management of the
|
|
|
|
* decoder's lifecycle and whether it executes on the main thread,
|
|
|
|
* off-main-thread in the image decoding thread pool, or on some combination of
|
|
|
|
* the two.
|
2014-11-19 02:17:17 +00:00
|
|
|
*/
|
|
|
|
class DecodePool : public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2015-01-18 09:27:16 +00:00
|
|
|
/// Initializes the singleton instance. Should be called from the main thread.
|
|
|
|
static void Initialize();
|
|
|
|
|
|
|
|
/// Returns the singleton instance.
|
2014-11-19 02:17:17 +00:00
|
|
|
static DecodePool* Singleton();
|
|
|
|
|
2015-07-08 22:52:51 +00:00
|
|
|
/// @return the number of processor cores we have available. This is not the
|
|
|
|
/// same as the number of decoding threads we're actually using.
|
|
|
|
static uint32_t NumberOfCores();
|
|
|
|
|
2015-01-15 23:11:36 +00:00
|
|
|
/// Ask the DecodePool to run @aDecoder asynchronously and return immediately.
|
|
|
|
void AsyncDecode(Decoder* aDecoder);
|
2014-11-19 02:17:17 +00:00
|
|
|
|
|
|
|
/**
|
2015-01-15 23:11:36 +00:00
|
|
|
* Run @aDecoder synchronously if the image it's decoding is small. If the
|
|
|
|
* image is too large, or if the source data isn't complete yet, run @aDecoder
|
|
|
|
* asynchronously instead.
|
2014-11-19 02:17:17 +00:00
|
|
|
*/
|
2015-01-15 23:11:36 +00:00
|
|
|
void SyncDecodeIfSmall(Decoder* aDecoder);
|
2014-11-19 02:17:17 +00:00
|
|
|
|
|
|
|
/**
|
2015-01-15 23:11:36 +00:00
|
|
|
* Run aDecoder synchronously if at all possible. If it can't complete
|
|
|
|
* synchronously because the source data isn't complete, asynchronously decode
|
|
|
|
* the rest.
|
2014-11-19 02:17:17 +00:00
|
|
|
*/
|
2015-01-15 23:11:36 +00:00
|
|
|
void SyncDecodeIfPossible(Decoder* aDecoder);
|
2014-11-19 02:17:17 +00:00
|
|
|
|
2015-01-18 09:27:16 +00:00
|
|
|
/**
|
|
|
|
* Returns an event target interface to the DecodePool's I/O thread. Callers
|
|
|
|
* who want to deliver data to workers on the DecodePool can use this event
|
|
|
|
* target.
|
|
|
|
*
|
|
|
|
* @return An nsIEventTarget interface to the thread pool's I/O thread.
|
|
|
|
*/
|
|
|
|
already_AddRefed<nsIEventTarget> GetIOEventTarget();
|
|
|
|
|
2015-01-11 19:43:32 +00:00
|
|
|
private:
|
2015-05-15 00:08:26 +00:00
|
|
|
friend class DecodePoolWorker;
|
2015-01-15 23:11:36 +00:00
|
|
|
|
2014-11-19 02:17:17 +00:00
|
|
|
DecodePool();
|
|
|
|
virtual ~DecodePool();
|
|
|
|
|
2015-01-15 23:11:36 +00:00
|
|
|
void Decode(Decoder* aDecoder);
|
|
|
|
void NotifyDecodeComplete(Decoder* aDecoder);
|
|
|
|
void NotifyProgress(Decoder* aDecoder);
|
|
|
|
|
2014-11-19 02:17:17 +00:00
|
|
|
static StaticRefPtr<DecodePool> sSingleton;
|
2015-07-08 22:52:51 +00:00
|
|
|
static uint32_t sNumCores;
|
2014-11-19 02:17:17 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DecodePoolImpl> mImpl;
|
2015-05-15 00:08:26 +00:00
|
|
|
|
|
|
|
// mMutex protects mThreads and mIOThread.
|
2015-01-18 09:27:16 +00:00
|
|
|
Mutex mMutex;
|
2015-05-15 00:08:26 +00:00
|
|
|
nsCOMArray<nsIThread> mThreads;
|
2015-01-18 09:27:16 +00:00
|
|
|
nsCOMPtr<nsIThread> mIOThread;
|
2014-11-19 02:17:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 03:52:05 +00:00
|
|
|
#endif // mozilla_image_DecodePool_h
|