DecodePool.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * DecodePool manages the threads used for decoding raster images.
  7. */
  8. #ifndef mozilla_image_DecodePool_h
  9. #define mozilla_image_DecodePool_h
  10. #include "mozilla/Mutex.h"
  11. #include "mozilla/StaticPtr.h"
  12. #include "nsCOMArray.h"
  13. #include "nsCOMPtr.h"
  14. #include "nsIEventTarget.h"
  15. #include "nsIObserver.h"
  16. #include "mozilla/RefPtr.h"
  17. class nsIThread;
  18. class nsIThreadPool;
  19. namespace mozilla {
  20. namespace image {
  21. class Decoder;
  22. class DecodePoolImpl;
  23. class IDecodingTask;
  24. /**
  25. * DecodePool is a singleton class that manages decoding of raster images. It
  26. * owns a pool of image decoding threads that are used for asynchronous
  27. * decoding.
  28. *
  29. * DecodePool allows callers to run a decoder, handling management of the
  30. * decoder's lifecycle and whether it executes on the main thread,
  31. * off-main-thread in the image decoding thread pool, or on some combination of
  32. * the two.
  33. */
  34. class DecodePool : public nsIObserver
  35. {
  36. public:
  37. NS_DECL_THREADSAFE_ISUPPORTS
  38. NS_DECL_NSIOBSERVER
  39. /// Initializes the singleton instance. Should be called from the main thread.
  40. static void Initialize();
  41. /// Returns the singleton instance.
  42. static DecodePool* Singleton();
  43. /// @return the number of processor cores we have available. This is not the
  44. /// same as the number of decoding threads we're actually using.
  45. static uint32_t NumberOfCores();
  46. /// Ask the DecodePool to run @aTask asynchronously and return immediately.
  47. void AsyncRun(IDecodingTask* aTask);
  48. /**
  49. * Run @aTask synchronously if the task would prefer it. It's up to the task
  50. * itself to make this decision; @see IDecodingTask::ShouldPreferSyncRun(). If
  51. * @aTask doesn't prefer it, just run @aTask asynchronously and return
  52. * immediately.
  53. */
  54. void SyncRunIfPreferred(IDecodingTask* aTask);
  55. /**
  56. * Run @aTask synchronously. This does not guarantee that @aTask will complete
  57. * synchronously. If, for example, @aTask doesn't yet have the data it needs to
  58. * run synchronously, it may recover by scheduling an async task to finish up
  59. * the work when the remaining data is available.
  60. */
  61. void SyncRunIfPossible(IDecodingTask* aTask);
  62. /**
  63. * Returns an event target interface to the DecodePool's I/O thread. Callers
  64. * who want to deliver data to workers on the DecodePool can use this event
  65. * target.
  66. *
  67. * @return An nsIEventTarget interface to the thread pool's I/O thread.
  68. */
  69. already_AddRefed<nsIEventTarget> GetIOEventTarget();
  70. private:
  71. friend class DecodePoolWorker;
  72. DecodePool();
  73. virtual ~DecodePool();
  74. static StaticRefPtr<DecodePool> sSingleton;
  75. static uint32_t sNumCores;
  76. RefPtr<DecodePoolImpl> mImpl;
  77. // mMutex protects mThreads and mIOThread.
  78. Mutex mMutex;
  79. nsTArray<nsCOMPtr<nsIThread>> mThreads;
  80. nsCOMPtr<nsIThread> mIOThread;
  81. };
  82. } // namespace image
  83. } // namespace mozilla
  84. #endif // mozilla_image_DecodePool_h