DecoderFactory.h (9319B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 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_image_DecoderFactory_h 8 #define mozilla_image_DecoderFactory_h 9 10 #include "DecoderFlags.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/NotNull.h" 13 #include "mozilla/gfx/2D.h" 14 #include "mozilla/image/ImageUtils.h" 15 #include "nsCOMPtr.h" 16 #include "Orientation.h" 17 #include "SurfaceFlags.h" 18 19 namespace mozilla::image { 20 21 class Decoder; 22 class IDecodingTask; 23 class nsICODecoder; 24 class RasterImage; 25 class SourceBuffer; 26 class SourceBufferIterator; 27 28 class DecoderFactory { 29 public: 30 /// @return the type of decoder which is appropriate for @aMimeType. 31 static DecoderType GetDecoderType(const char* aMimeType); 32 33 /// @return the default flags to use when creating a decoder of @aType. 34 static DecoderFlags GetDefaultDecoderFlagsForType(DecoderType aType); 35 36 /** 37 * Creates and initializes a decoder for non-animated images of type @aType. 38 * (If the image *is* animated, only the first frame will be decoded.) The 39 * decoder will send notifications to @aImage. 40 * 41 * @param aType Which type of decoder to create - JPEG, PNG, etc. 42 * @param aImage The image will own the decoder and which should receive 43 * notifications as decoding progresses. 44 * @param aSourceBuffer The SourceBuffer which the decoder will read its data 45 * from. 46 * @param aIntrinsicSize The intrinsic size of the image, normally obtained 47 * during the metadata decode. 48 * @param aOutputSize The output size for the decoder. If this is smaller than 49 * the intrinsic size, the decoder will downscale the 50 * image. 51 * @param aDecoderFlags Flags specifying the behavior of this decoder. 52 * @param aSurfaceFlags Flags specifying the type of output this decoder 53 * should produce. 54 * @param aOutTask Task representing the decoder. 55 * @return NS_OK if the decoder has been created/initialized successfully; 56 * NS_ERROR_ALREADY_INITIALIZED if there is already an active decoder 57 * for this image; 58 * Else some other unrecoverable error occurred. 59 */ 60 static nsresult CreateDecoder(DecoderType aType, NotNull<RasterImage*> aImage, 61 NotNull<SourceBuffer*> aSourceBuffer, 62 const gfx::IntSize& aIntrinsicSize, 63 const gfx::IntSize& aOutputSize, 64 DecoderFlags aDecoderFlags, 65 SurfaceFlags aSurfaceFlags, 66 IDecodingTask** aOutTask); 67 68 /** 69 * Creates and initializes a decoder for animated images of type @aType. 70 * The decoder will send notifications to @aImage. 71 * 72 * @param aType Which type of decoder to create - JPEG, PNG, etc. 73 * @param aImage The image will own the decoder and which should receive 74 * notifications as decoding progresses. 75 * @param aSourceBuffer The SourceBuffer which the decoder will read its data 76 * from. 77 * @param aIntrinsicSize The intrinsic size of the image, normally obtained 78 * during the metadata decode. 79 * @param aDecoderFlags Flags specifying the behavior of this decoder. 80 * @param aSurfaceFlags Flags specifying the type of output this decoder 81 * should produce. 82 * @param aCurrentFrame The current frame the decoder should auto advance to. 83 * @param aOutTask Task representing the decoder. 84 * @return NS_OK if the decoder has been created/initialized successfully; 85 * NS_ERROR_ALREADY_INITIALIZED if there is already an active decoder 86 * for this image; 87 * Else some other unrecoverable error occurred. 88 */ 89 static nsresult CreateAnimationDecoder( 90 DecoderType aType, NotNull<RasterImage*> aImage, 91 NotNull<SourceBuffer*> aSourceBuffer, const gfx::IntSize& aIntrinsicSize, 92 DecoderFlags aDecoderFlags, SurfaceFlags aSurfaceFlags, 93 size_t aCurrentFrame, IDecodingTask** aOutTask); 94 95 /** 96 * Creates and initializes a decoder for animated images, cloned from the 97 * given decoder. 98 * 99 * @param aDecoder Decoder to clone. 100 */ 101 static already_AddRefed<Decoder> CloneAnimationDecoder(Decoder* aDecoder); 102 103 /** 104 * Creates and initializes a metadata decoder for an anonymous image, cloned 105 * from the given decoder. 106 * 107 * @param aDecoder Decoder to clone. 108 */ 109 static already_AddRefed<Decoder> CloneAnonymousMetadataDecoder( 110 Decoder* aDecoder, const Maybe<DecoderFlags>& aDecoderFlags = Nothing()); 111 112 /** 113 * Creates and initializes a metadata decoder of type @aType. This decoder 114 * will only decode the image's header, extracting metadata like the size of 115 * the image. No actual image data will be decoded and no surfaces will be 116 * allocated. The decoder will send notifications to @aImage. 117 * 118 * @param aType Which type of decoder to create - JPEG, PNG, etc. 119 * @param aImage The image will own the decoder and which should receive 120 * notifications as decoding progresses. 121 * @param aSourceBuffer The SourceBuffer which the decoder will read its data 122 * from. 123 */ 124 static already_AddRefed<IDecodingTask> CreateMetadataDecoder( 125 DecoderType aType, NotNull<RasterImage*> aImage, DecoderFlags aFlags, 126 NotNull<SourceBuffer*> aSourceBuffer); 127 128 /** 129 * Creates and initializes a decoder for an ICO resource, which may be either 130 * a BMP or PNG image. 131 * 132 * @param aType Which type of decoder to create. This must be either BMP or 133 * PNG. 134 * @param aIterator The SourceBufferIterator which the decoder will read its 135 * data from. 136 * @param aICODecoder The ICO decoder which is controlling this resource 137 * decoder. @aICODecoder's settings will be copied to the 138 * resource decoder, so the two decoders will have the 139 * same decoder flags, surface flags, target size, and 140 * other parameters. 141 * @param aIsMetadataDecode Indicates whether or not this decoder is for 142 * metadata or not. Independent of the state of the 143 * parent decoder. 144 * @param aExpectedSize The expected size of the resource from the ICO header. 145 * @param aDataOffset If @aType is BMP, specifies the offset at which data 146 * begins in the BMP resource. Must be Some() if and only 147 * if @aType is BMP. 148 */ 149 static already_AddRefed<Decoder> CreateDecoderForICOResource( 150 DecoderType aType, SourceBufferIterator&& aIterator, 151 NotNull<nsICODecoder*> aICODecoder, bool aIsMetadataDecode, 152 const Maybe<OrientedIntSize>& aExpectedSize, 153 const Maybe<uint32_t>& aDataOffset = Nothing()); 154 155 /** 156 * Creates and initializes an anonymous decoder (one which isn't associated 157 * with an Image object). Only the first frame of the image will be decoded. 158 * 159 * @param aType Which type of decoder to create - JPEG, PNG, etc. 160 * @param aSourceBuffer The SourceBuffer which the decoder will read its data 161 * from. 162 * @param aOutputSize If Some(), the output size for the decoder. If this is 163 * smaller than the intrinsic size, the decoder will 164 * downscale the image. If Nothing(), the output size will 165 * be the intrinsic size. 166 * @param aDecoderFlags Flags specifying the behavior of this decoder. 167 * @param aSurfaceFlags Flags specifying the type of output this decoder 168 * should produce. 169 */ 170 static already_AddRefed<Decoder> CreateAnonymousDecoder( 171 DecoderType aType, NotNull<SourceBuffer*> aSourceBuffer, 172 const Maybe<gfx::IntSize>& aOutputSize, DecoderFlags aDecoderFlags, 173 SurfaceFlags aSurfaceFlags); 174 175 /** 176 * Creates and initializes an anonymous metadata decoder (one which isn't 177 * associated with an Image object). This decoder will only decode the image's 178 * header, extracting metadata like the size of the image. No actual image 179 * data will be decoded and no surfaces will be allocated. 180 * 181 * @param aType Which type of decoder to create - JPEG, PNG, etc. 182 * @param aSourceBuffer The SourceBuffer which the decoder will read its data 183 * from. 184 * @param aDecoderFlags Flags specifying the behavior of this decoder. 185 */ 186 static already_AddRefed<Decoder> CreateAnonymousMetadataDecoder( 187 DecoderType aType, NotNull<SourceBuffer*> aSourceBuffer, 188 DecoderFlags aDecoderFlags); 189 190 private: 191 virtual ~DecoderFactory() = 0; 192 193 /** 194 * An internal method which allocates a new decoder of the requested @aType. 195 */ 196 static already_AddRefed<Decoder> GetDecoder(DecoderType aType, 197 RasterImage* aImage, 198 bool aIsRedecode); 199 }; 200 201 } // namespace mozilla::image 202 203 #endif // mozilla_image_DecoderFactory_h