tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

IDecodingTask.h (3380B)


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