tor-browser

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

ImageDecoder.h (5134B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_ImageDecoder_h
      8 #define mozilla_dom_ImageDecoder_h
      9 
     10 #include "FrameTimeout.h"
     11 #include "mozilla/ErrorResult.h"
     12 #include "mozilla/UniquePtr.h"
     13 #include "mozilla/dom/ImageDecoderBinding.h"
     14 #include "mozilla/dom/WebCodecsUtils.h"
     15 #include "mozilla/gfx/Point.h"
     16 #include "mozilla/media/MediaUtils.h"
     17 #include "nsCycleCollectionParticipant.h"
     18 #include "nsWrapperCache.h"
     19 
     20 class nsIGlobalObject;
     21 
     22 namespace mozilla {
     23 class MediaResult;
     24 
     25 namespace image {
     26 class AnonymousDecoder;
     27 class SourceBuffer;
     28 enum class DecoderType;
     29 enum class SurfaceFlags : uint8_t;
     30 struct DecodeFramesResult;
     31 struct DecodeFrameCountResult;
     32 struct DecodeMetadataResult;
     33 }  // namespace image
     34 
     35 namespace dom {
     36 class Promise;
     37 struct ImageDecoderReadRequest;
     38 
     39 class ImageDecoder final : public nsISupports,
     40                           public nsWrapperCache,
     41                           public media::ShutdownConsumer {
     42 public:
     43  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     44  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ImageDecoder)
     45 
     46 public:
     47  ImageDecoder(nsCOMPtr<nsIGlobalObject>&& aParent, const nsAString& aType);
     48 
     49 public:
     50  nsIGlobalObject* GetParentObject() const { return mParent; }
     51 
     52  JSObject* WrapObject(JSContext* aCx,
     53                       JS::Handle<JSObject*> aGivenProto) override;
     54 
     55  static already_AddRefed<ImageDecoder> Constructor(
     56      const GlobalObject& aGlobal, const ImageDecoderInit& aInit,
     57      ErrorResult& aRv);
     58 
     59  static already_AddRefed<Promise> IsTypeSupported(const GlobalObject& aGlobal,
     60                                                   const nsAString& aType,
     61                                                   ErrorResult& aRv);
     62 
     63  void GetType(nsAString& aType) const;
     64 
     65  bool Complete() const { return mComplete; }
     66 
     67  Promise* Completed() const { return mCompletePromise; }
     68 
     69  ImageTrackList* Tracks() const { return mTracks; }
     70 
     71  already_AddRefed<Promise> Decode(const ImageDecodeOptions& aOptions,
     72                                   ErrorResult& aRv);
     73 
     74  void Reset();
     75 
     76  void Close();
     77 
     78  void OnShutdown() override;
     79 
     80  void OnSourceBufferComplete(const MediaResult& aResult);
     81 
     82  void QueueSelectTrackMessage(uint32_t aSelectedIndex);
     83  void ProcessControlMessageQueue();
     84 
     85 private:
     86  ~ImageDecoder();
     87 
     88  class ControlMessage;
     89  class ConfigureMessage;
     90  class DecodeMetadataMessage;
     91  class DecodeFrameMessage;
     92  class SelectTrackMessage;
     93 
     94  std::queue<UniquePtr<ControlMessage>> mControlMessageQueue;
     95  bool mMessageQueueBlocked = false;
     96  bool mTracksEstablished = false;
     97 
     98  struct OutstandingDecode {
     99    RefPtr<Promise> mPromise;
    100    uint32_t mFrameIndex = 0;
    101    bool mCompleteFramesOnly = true;
    102  };
    103 
    104  // VideoFrame can run on either main thread or worker thread.
    105  void AssertIsOnOwningThread() const { NS_ASSERT_OWNINGTHREAD(ImageDecoder); }
    106 
    107  void Initialize(const GlobalObject& aGLobal, const ImageDecoderInit& aInit,
    108                  ErrorResult& aRv);
    109  void Destroy();
    110  void Reset(const MediaResult& aResult);
    111  void Close(const MediaResult& aResult);
    112 
    113  void QueueConfigureMessage(const Maybe<gfx::IntSize>& aOutputSize,
    114                             ColorSpaceConversion aColorSpaceConversion);
    115  void QueueDecodeMetadataMessage();
    116  void QueueDecodeFrameMessage();
    117 
    118  void ResumeControlMessageQueue();
    119  MessageProcessedResult ProcessConfigureMessage(ConfigureMessage* aMsg);
    120  MessageProcessedResult ProcessDecodeMetadataMessage(
    121      DecodeMetadataMessage* aMsg);
    122  MessageProcessedResult ProcessDecodeFrameMessage(DecodeFrameMessage* aMsg);
    123  MessageProcessedResult ProcessSelectTrackMessage(SelectTrackMessage* aMsg);
    124 
    125  void CheckOutstandingDecodes();
    126 
    127  void OnCompleteSuccess();
    128  void OnCompleteFailed(const MediaResult& aResult);
    129 
    130  void OnMetadataSuccess(const image::DecodeMetadataResult& aMetadata);
    131  void OnMetadataFailed(const nsresult& aErr);
    132 
    133  void RequestFrameCount(uint32_t aKnownFrameCount);
    134  void OnFrameCountSuccess(const image::DecodeFrameCountResult& aResult);
    135  void OnFrameCountFailed(const nsresult& aErr);
    136 
    137  void RequestDecodeFrames(uint32_t aFramesToDecode);
    138  void OnDecodeFramesSuccess(const image::DecodeFramesResult& aResult);
    139  void OnDecodeFramesFailed(const nsresult& aErr);
    140 
    141  nsCOMPtr<nsIGlobalObject> mParent;
    142  RefPtr<media::ShutdownWatcher> mShutdownWatcher;
    143  RefPtr<ImageTrackList> mTracks;
    144  RefPtr<ImageDecoderReadRequest> mReadRequest;
    145  RefPtr<Promise> mCompletePromise;
    146  RefPtr<image::SourceBuffer> mSourceBuffer;
    147  RefPtr<image::AnonymousDecoder> mDecoder;
    148  AutoTArray<OutstandingDecode, 1> mOutstandingDecodes;
    149  nsAutoString mType;
    150  image::FrameTimeout mFramesTimestamp;
    151  bool mComplete = false;
    152  bool mHasFrameCount = false;
    153  bool mHasFramePending = false;
    154  bool mTypeNotSupported = false;
    155  bool mClosed = false;
    156 };
    157 
    158 }  // namespace dom
    159 }  // namespace mozilla
    160 
    161 #endif  // mozilla_dom_ImageDecoder_h