IDecodingTask.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * An interface for tasks which can execute on the ImageLib DecodePool, and
  7. * various implementations.
  8. */
  9. #ifndef mozilla_image_IDecodingTask_h
  10. #define mozilla_image_IDecodingTask_h
  11. #include "mozilla/NotNull.h"
  12. #include "mozilla/RefPtr.h"
  13. #include "imgFrame.h"
  14. #include "SourceBuffer.h"
  15. namespace mozilla {
  16. namespace image {
  17. class Decoder;
  18. class RasterImage;
  19. /// A priority hint that DecodePool can use when scheduling an IDecodingTask.
  20. enum class TaskPriority : uint8_t
  21. {
  22. eLow,
  23. eHigh
  24. };
  25. /**
  26. * An interface for tasks which can execute on the ImageLib DecodePool.
  27. */
  28. class IDecodingTask : public IResumable
  29. {
  30. public:
  31. /// Run the task.
  32. virtual void Run() = 0;
  33. /// @return true if, given the option, this task prefers to run synchronously.
  34. virtual bool ShouldPreferSyncRun() const = 0;
  35. /// @return a priority hint that DecodePool can use when scheduling this task.
  36. virtual TaskPriority Priority() const = 0;
  37. /// A default implementation of IResumable which resubmits the task to the
  38. /// DecodePool. Subclasses can override this if they need different behavior.
  39. void Resume() override;
  40. protected:
  41. /// Notify @aImage of @aDecoder's progress.
  42. static void NotifyProgress(NotNull<RasterImage*> aImage,
  43. NotNull<Decoder*> aDecoder);
  44. /// Notify @aImage that @aDecoder has finished.
  45. static void NotifyDecodeComplete(NotNull<RasterImage*> aImage,
  46. NotNull<Decoder*> aDecoder);
  47. virtual ~IDecodingTask() { }
  48. };
  49. /**
  50. * An IDecodingTask implementation for metadata decodes of images.
  51. */
  52. class MetadataDecodingTask final : public IDecodingTask
  53. {
  54. public:
  55. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MetadataDecodingTask, override)
  56. explicit MetadataDecodingTask(NotNull<Decoder*> aDecoder);
  57. void Run() override;
  58. // Metadata decodes are very fast (since they only need to examine an image's
  59. // header) so there's no reason to refuse to run them synchronously if the
  60. // caller will allow us to.
  61. bool ShouldPreferSyncRun() const override { return true; }
  62. // Metadata decodes run at the highest priority because they block layout and
  63. // page load.
  64. TaskPriority Priority() const override { return TaskPriority::eHigh; }
  65. private:
  66. virtual ~MetadataDecodingTask() { }
  67. /// Mutex protecting access to mDecoder.
  68. Mutex mMutex;
  69. NotNull<RefPtr<Decoder>> mDecoder;
  70. };
  71. /**
  72. * An IDecodingTask implementation for anonymous decoders - that is, decoders
  73. * with no associated Image object.
  74. */
  75. class AnonymousDecodingTask final : public IDecodingTask
  76. {
  77. public:
  78. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AnonymousDecodingTask, override)
  79. explicit AnonymousDecodingTask(NotNull<Decoder*> aDecoder, bool aResumable);
  80. void Run() override;
  81. bool ShouldPreferSyncRun() const override { return true; }
  82. TaskPriority Priority() const override { return TaskPriority::eLow; }
  83. void Resume() override;
  84. private:
  85. virtual ~AnonymousDecodingTask() { }
  86. NotNull<RefPtr<Decoder>> mDecoder;
  87. bool mResumable;
  88. };
  89. } // namespace image
  90. } // namespace mozilla
  91. #endif // mozilla_image_IDecodingTask_h