ImageUtils.h (3625B)
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 #ifndef mozilla_image_ImageUtils_h 7 #define mozilla_image_ImageUtils_h 8 9 #include "FrameTimeout.h" 10 #include "Orientation.h" 11 #include "mozilla/image/SurfaceFlags.h" 12 #include "mozilla/Maybe.h" 13 #include "mozilla/MozPromise.h" 14 #include "mozilla/RefPtr.h" 15 #include "mozilla/ThreadSafeWeakPtr.h" 16 #include "nsString.h" 17 #include "nsTArray.h" 18 19 namespace mozilla { 20 class ErrorResult; 21 22 namespace gfx { 23 class SourceSurface; 24 } 25 26 namespace image { 27 class Decoder; 28 class imgFrame; 29 class ImageMetadata; 30 class SourceBuffer; 31 32 /** 33 * The type of decoder; this is usually determined from a MIME type using 34 * DecoderFactory::GetDecoderType() or ImageUtils::GetDecoderType(). 35 */ 36 enum class DecoderType { 37 PNG, 38 GIF, 39 JPEG, 40 JPEG_PDF, 41 BMP, 42 BMP_CLIPBOARD, 43 ICO, 44 ICON, 45 WEBP, 46 AVIF, 47 JXL, 48 UNKNOWN 49 }; 50 51 struct DecodeMetadataResult { 52 CopyableTArray<OrientedIntSize> mNativeSizes; 53 int32_t mWidth = 0; 54 int32_t mHeight = 0; 55 int32_t mRepetitions = -1; 56 uint32_t mFrameCount = 0; 57 bool mAnimated = false; 58 bool mFrameCountComplete = true; 59 }; 60 61 struct DecodeFrameCountResult { 62 uint32_t mFrameCount = 0; 63 bool mFinished = false; 64 }; 65 66 struct DecodedFrame { 67 RefPtr<gfx::SourceSurface> mSurface; 68 FrameTimeout mTimeout; 69 }; 70 71 struct DecodeFramesResult { 72 nsTArray<DecodedFrame> mFrames; 73 bool mFinished = false; 74 }; 75 76 using DecodeMetadataPromise = MozPromise<DecodeMetadataResult, nsresult, true>; 77 using DecodeFrameCountPromise = 78 MozPromise<DecodeFrameCountResult, nsresult, true>; 79 using DecodeFramesPromise = MozPromise<DecodeFramesResult, nsresult, true>; 80 81 class AnonymousMetadataDecoderTask; 82 class AnonymousFrameCountDecoderTask; 83 class AnonymousFramesDecoderTask; 84 85 class AnonymousDecoder : public SupportsThreadSafeWeakPtr<AnonymousDecoder> { 86 public: 87 virtual RefPtr<DecodeMetadataPromise> DecodeMetadata() = 0; 88 89 virtual void Destroy() = 0; 90 91 virtual RefPtr<DecodeFrameCountPromise> DecodeFrameCount( 92 uint32_t aKnownFrameCount) = 0; 93 94 virtual RefPtr<DecodeFramesPromise> DecodeFrames(size_t aCount) = 0; 95 96 virtual void CancelDecodeFrames() = 0; 97 98 #ifdef MOZ_REFCOUNTED_LEAK_CHECKING 99 virtual const char* typeName() const = 0; 100 virtual size_t typeSize() const = 0; 101 #endif 102 103 virtual ~AnonymousDecoder(); 104 105 protected: 106 AnonymousDecoder(); 107 108 // Returns true if successfully initialized else false. 109 virtual bool Initialize(RefPtr<Decoder>&& aDecoder) = 0; 110 111 virtual void OnMetadata(const ImageMetadata* aMetadata) = 0; 112 113 virtual void OnFrameCount(uint32_t aFrameCount, bool aComplete) = 0; 114 115 // Returns true if the caller should continue decoding more frames if 116 // possible. 117 virtual bool OnFrameAvailable(RefPtr<imgFrame>&& aFrame, 118 RefPtr<gfx::SourceSurface>&& aSurface) = 0; 119 120 virtual void OnFramesComplete() = 0; 121 122 friend class AnonymousMetadataDecoderTask; 123 friend class AnonymousFrameCountDecoderTask; 124 friend class AnonymousFramesDecoderTask; 125 }; 126 127 class ImageUtils { 128 public: 129 static already_AddRefed<AnonymousDecoder> CreateDecoder( 130 SourceBuffer* aSourceBuffer, DecoderType aType, 131 const Maybe<gfx::IntSize>& aOutputSize, SurfaceFlags aSurfaceFlags); 132 133 static DecoderType GetDecoderType(const nsACString& aMimeType); 134 135 private: 136 ImageUtils() = delete; 137 ~ImageUtils() = delete; 138 }; 139 140 } // namespace image 141 } // namespace mozilla 142 143 #endif // mozilla_image_ImageUtils_h