ImageEncoder.h (5789B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 ImageEncoder_h 8 #define ImageEncoder_h 9 10 #include "imgIEncoder.h" 11 #include "mozilla/UniquePtr.h" 12 #include "mozilla/dom/CanvasUtils.h" 13 #include "mozilla/dom/File.h" 14 #include "mozilla/dom/HTMLCanvasElementBinding.h" 15 #include "nsError.h" 16 #include "nsSize.h" 17 18 class nsICanvasRenderingContextInternal; 19 20 namespace mozilla { 21 22 namespace layers { 23 class Image; 24 } // namespace layers 25 26 namespace dom { 27 28 class EncodeCompleteCallback; 29 class EncodingRunnable; 30 class OffscreenCanvasDisplayHelper; 31 32 class ImageEncoder { 33 public: 34 // Extracts data synchronously and gives you a stream containing the image 35 // represented by aContext. aType may change to "image/png" if we had to fall 36 // back to a PNG encoder. A return value of NS_OK implies successful data 37 // extraction. If there are any unrecognized custom parse options in 38 // aOptions, NS_ERROR_INVALID_ARG will be returned. When encountering this 39 // error it is usual to call this function again without any options at all. 40 static nsresult ExtractData(nsAString& aType, const nsAString& aOptions, 41 const CSSIntSize aSize, 42 CanvasUtils::ImageExtraction aExtractionBehavior, 43 const nsCString& aRandomizationKey, 44 nsICanvasRenderingContextInternal* aContext, 45 OffscreenCanvasDisplayHelper* aOffscreenDisplay, 46 nsIInputStream** aStream); 47 48 // Extracts data asynchronously. aType may change to "image/png" if we had to 49 // fall back to a PNG encoder. aOptions are the options to be passed to the 50 // encoder and aUsingCustomOptions specifies whether custom parse options were 51 // used (i.e. by using -moz-parse-options). If there are any unrecognized 52 // custom parse options, we fall back to the default values for the encoder 53 // without any options at all. A return value of NS_OK only implies 54 // successful dispatching of the extraction step to the encoding thread. 55 // aEncodeCallback will be called on main thread when encoding process is 56 // success. 57 // Note: The callback has to set a valid parent for content for the generated 58 // Blob object. 59 static nsresult ExtractDataAsync( 60 nsAString& aType, const nsAString& aOptions, bool aUsingCustomOptions, 61 UniquePtr<uint8_t[]> aImageBuffer, int32_t aFormat, 62 const CSSIntSize aSize, CanvasUtils::ImageExtraction aExtractionBehavior, 63 const nsCString& aRandomizationKey, 64 EncodeCompleteCallback* aEncodeCallback); 65 66 // Extract an Image asynchronously. Its function is same as ExtractDataAsync 67 // except for the parameters. aImage is the uncompressed data. aEncodeCallback 68 // will be called on main thread when encoding process is success. 69 // Note: The callback has to set a valid parent for content for the generated 70 // Blob object. 71 static nsresult ExtractDataFromLayersImageAsync( 72 nsAString& aType, const nsAString& aOptions, bool aUsingCustomOptions, 73 layers::Image* aImage, CanvasUtils::ImageExtraction aExtractionBehavior, 74 const nsCString& aRandomizationKey, 75 EncodeCompleteCallback* aEncodeCallback); 76 77 // Gives you a stream containing the image represented by aImageBuffer. 78 // The format is given in aFormat, for example 79 // imgIEncoder::INPUT_FORMAT_HOSTARGB. 80 static nsresult GetInputStream(int32_t aWidth, int32_t aHeight, 81 uint8_t* aImageBuffer, int32_t aFormat, 82 imgIEncoder* aEncoder, 83 const nsAString& aEncoderOptions, 84 const nsACString& aRandomizationKey, 85 nsIInputStream** aStream); 86 87 private: 88 // When called asynchronously, aContext and aRenderer are null. 89 static nsresult ExtractDataInternal( 90 const nsAString& aType, const nsAString& aOptions, uint8_t* aImageBuffer, 91 int32_t aFormat, const CSSIntSize aSize, 92 CanvasUtils::ImageExtraction aExtractionBehavior, 93 const nsCString& aRandomizationKey, layers::Image* aImage, 94 nsICanvasRenderingContextInternal* aContext, 95 OffscreenCanvasDisplayHelper* aOffscreenDisplay, nsIInputStream** aStream, 96 imgIEncoder* aEncoder); 97 98 // Creates and returns an encoder instance of the type specified in aType. 99 // aType may change to "image/png" if no instance of the original type could 100 // be created and we had to fall back to a PNG encoder. A null return value 101 // should be interpreted as NS_IMAGELIB_ERROR_NO_ENCODER and aType is 102 // undefined in this case. 103 static already_AddRefed<imgIEncoder> GetImageEncoder(nsAString& aType); 104 105 friend class EncodingRunnable; 106 friend class EncoderThreadPoolTerminator; 107 }; 108 109 /** 110 * The callback interface of ExtractDataAsync and 111 * ExtractDataFromLayersImageAsync. ReceiveBlobImpl() is called on main thread 112 * when encoding is complete. 113 */ 114 class EncodeCompleteCallback { 115 public: 116 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(EncodeCompleteCallback) 117 118 MOZ_CAN_RUN_SCRIPT 119 virtual nsresult ReceiveBlobImpl(already_AddRefed<BlobImpl> aBlobImpl) = 0; 120 121 // CanBeDeletedOnAnyThread is pure virtual, so that whoever extends this class 122 // needs to think how to handle cases like the owning DOM worker thread 123 // shutting down. 124 virtual bool CanBeDeletedOnAnyThread() = 0; 125 126 protected: 127 virtual ~EncodeCompleteCallback() = default; 128 }; 129 130 } // namespace dom 131 } // namespace mozilla 132 133 #endif // ImageEncoder_h