Blame image/DecodePool.h

Packit f0b94e
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Packit f0b94e
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit f0b94e
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit f0b94e
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * DecodePool manages the threads used for decoding raster images.
Packit f0b94e
 */
Packit f0b94e
Packit f0b94e
#ifndef mozilla_image_DecodePool_h
Packit f0b94e
#define mozilla_image_DecodePool_h
Packit f0b94e
Packit f0b94e
#include "mozilla/Mutex.h"
Packit f0b94e
#include "mozilla/StaticPtr.h"
Packit f0b94e
#include "nsCOMArray.h"
Packit f0b94e
#include "nsCOMPtr.h"
Packit f0b94e
#include "nsIEventTarget.h"
Packit f0b94e
#include "nsIObserver.h"
Packit f0b94e
#include "mozilla/RefPtr.h"
Packit f0b94e
#include "nsStringFwd.h"
Packit f0b94e
Packit f0b94e
class nsIThread;
Packit f0b94e
class nsIThreadPool;
Packit f0b94e
Packit f0b94e
namespace mozilla {
Packit f0b94e
namespace image {
Packit f0b94e
Packit f0b94e
class Decoder;
Packit f0b94e
class DecodePoolImpl;
Packit f0b94e
class IDecodingTask;
Packit f0b94e
Packit f0b94e
/**
Packit f0b94e
 * DecodePool is a singleton class that manages decoding of raster images. It
Packit f0b94e
 * owns a pool of image decoding threads that are used for asynchronous
Packit f0b94e
 * decoding.
Packit f0b94e
 *
Packit f0b94e
 * DecodePool allows callers to run a decoder, handling management of the
Packit f0b94e
 * decoder's lifecycle and whether it executes on the main thread,
Packit f0b94e
 * off-main-thread in the image decoding thread pool, or on some combination of
Packit f0b94e
 * the two.
Packit f0b94e
 */
Packit f0b94e
class DecodePool final : public nsIObserver {
Packit f0b94e
 public:
Packit f0b94e
  NS_DECL_THREADSAFE_ISUPPORTS
Packit f0b94e
  NS_DECL_NSIOBSERVER
Packit f0b94e
Packit f0b94e
  /// Initializes the singleton instance. Should be called from the main thread.
Packit f0b94e
  static void Initialize();
Packit f0b94e
Packit f0b94e
  /// Returns the singleton instance.
Packit f0b94e
  static DecodePool* Singleton();
Packit f0b94e
Packit f0b94e
  /// @return the number of processor cores we have available. This is not the
Packit f0b94e
  /// same as the number of decoding threads we're actually using.
Packit f0b94e
  static uint32_t NumberOfCores();
Packit f0b94e
Packit f0b94e
  /// True if the DecodePool is being shutdown. This may only be called by
Packit f0b94e
  /// threads from the pool to check if they should keep working or not.
Packit f0b94e
  bool IsShuttingDown() const;
Packit f0b94e
Packit f0b94e
  /// Ask the DecodePool to run @aTask asynchronously and return immediately.
Packit f0b94e
  void AsyncRun(IDecodingTask* aTask);
Packit f0b94e
Packit f0b94e
  /**
Packit f0b94e
   * Run @aTask synchronously if the task would prefer it. It's up to the task
Packit f0b94e
   * itself to make this decision; @see IDecodingTask::ShouldPreferSyncRun(). If
Packit f0b94e
   * @aTask doesn't prefer it, just run @aTask asynchronously and return
Packit f0b94e
   * immediately.
Packit f0b94e
   * @return true if the task was run sync, false otherwise.
Packit f0b94e
   */
Packit f0b94e
  bool SyncRunIfPreferred(IDecodingTask* aTask, const nsCString& aURI);
Packit f0b94e
Packit f0b94e
  /**
Packit f0b94e
   * Run @aTask synchronously. This does not guarantee that @aTask will complete
Packit f0b94e
   * synchronously. If, for example, @aTask doesn't yet have the data it needs
Packit f0b94e
   * to run synchronously, it may recover by scheduling an async task to finish
Packit f0b94e
   * up the work when the remaining data is available.
Packit f0b94e
   */
Packit f0b94e
  void SyncRunIfPossible(IDecodingTask* aTask, const nsCString& aURI);
Packit f0b94e
Packit f0b94e
  /**
Packit f0b94e
   * Returns an event target interface to the DecodePool's I/O thread. Callers
Packit f0b94e
   * who want to deliver data to workers on the DecodePool can use this event
Packit f0b94e
   * target.
Packit f0b94e
   *
Packit f0b94e
   * @return An nsIEventTarget interface to the thread pool's I/O thread.
Packit f0b94e
   */
Packit f0b94e
  already_AddRefed<nsIEventTarget> GetIOEventTarget();
Packit f0b94e
Packit f0b94e
 private:
Packit f0b94e
  friend class DecodePoolWorker;
Packit f0b94e
Packit f0b94e
  DecodePool();
Packit f0b94e
  virtual ~DecodePool();
Packit f0b94e
Packit f0b94e
  static StaticRefPtr<DecodePool> sSingleton;
Packit f0b94e
  static uint32_t sNumCores;
Packit f0b94e
Packit f0b94e
  RefPtr<DecodePoolImpl> mImpl;
Packit f0b94e
Packit f0b94e
  // mMutex protects mIOThread.
Packit f0b94e
  Mutex mMutex;
Packit f0b94e
  nsCOMPtr<nsIThread> mIOThread;
Packit f0b94e
};
Packit f0b94e
Packit f0b94e
}  // namespace image
Packit f0b94e
}  // namespace mozilla
Packit f0b94e
Packit f0b94e
#endif  // mozilla_image_DecodePool_h