Bug 1550519 - Show a translucent parent highlight when a subgrid is highlighted....
[gecko.git] / dom / base / ImageEncoder.h
blobfb6a8b181392584853af508286e15af702116bba
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/. */
7 #ifndef ImageEncoder_h
8 #define ImageEncoder_h
10 #include "imgIEncoder.h"
11 #include "nsError.h"
12 #include "mozilla/dom/File.h"
13 #include "mozilla/dom/HTMLCanvasElementBinding.h"
14 #include "mozilla/UniquePtr.h"
15 #include "nsLayoutUtils.h"
16 #include "nsSize.h"
18 class nsICanvasRenderingContextInternal;
19 class nsIThreadPool;
21 namespace mozilla {
23 namespace layers {
24 class AsyncCanvasRenderer;
25 class Image;
26 } // namespace layers
28 namespace dom {
30 class EncodeCompleteCallback;
31 class EncodingRunnable;
33 class ImageEncoder {
34 public:
35 // Extracts data synchronously and gives you a stream containing the image
36 // represented by aContext. aType may change to "image/png" if we had to fall
37 // back to a PNG encoder. A return value of NS_OK implies successful data
38 // extraction. If there are any unrecognized custom parse options in
39 // aOptions, NS_ERROR_INVALID_ARG will be returned. When encountering this
40 // error it is usual to call this function again without any options at all.
41 static nsresult ExtractData(nsAString& aType, const nsAString& aOptions,
42 const nsIntSize aSize, bool aUsePlaceholder,
43 nsICanvasRenderingContextInternal* aContext,
44 layers::AsyncCanvasRenderer* aRenderer,
45 nsIInputStream** aStream);
47 // Extracts data asynchronously. aType may change to "image/png" if we had to
48 // fall back to a PNG encoder. aOptions are the options to be passed to the
49 // encoder and aUsingCustomOptions specifies whether custom parse options were
50 // used (i.e. by using -moz-parse-options). If there are any unrecognized
51 // custom parse options, we fall back to the default values for the encoder
52 // without any options at all. A return value of NS_OK only implies
53 // successful dispatching of the extraction step to the encoding thread.
54 // aEncodeCallback will be called on main thread when encoding process is
55 // success.
56 // Note: The callback has to set a valid parent for content for the generated
57 // Blob object.
58 static nsresult ExtractDataAsync(nsAString& aType, const nsAString& aOptions,
59 bool aUsingCustomOptions,
60 UniquePtr<uint8_t[]> aImageBuffer,
61 int32_t aFormat, const nsIntSize aSize,
62 bool aUsePlaceholder,
63 EncodeCompleteCallback* aEncodeCallback);
65 // Extract an Image asynchronously. Its function is same as ExtractDataAsync
66 // except for the parameters. aImage is the uncompressed data. aEncodeCallback
67 // will be called on main thread when encoding process is success.
68 // Note: The callback has to set a valid parent for content for the generated
69 // Blob object.
70 static nsresult ExtractDataFromLayersImageAsync(
71 nsAString& aType, const nsAString& aOptions, bool aUsingCustomOptions,
72 layers::Image* aImage, bool aUsePlaceholder,
73 EncodeCompleteCallback* aEncodeCallback);
75 // Gives you a stream containing the image represented by aImageBuffer.
76 // The format is given in aFormat, for example
77 // imgIEncoder::INPUT_FORMAT_HOSTARGB.
78 static nsresult GetInputStream(int32_t aWidth, int32_t aHeight,
79 uint8_t* aImageBuffer, int32_t aFormat,
80 imgIEncoder* aEncoder,
81 const nsAString& aEncoderOptions,
82 nsIInputStream** aStream);
84 private:
85 // When called asynchronously, aContext and aRenderer are null.
86 static nsresult ExtractDataInternal(
87 const nsAString& aType, const nsAString& aOptions, uint8_t* aImageBuffer,
88 int32_t aFormat, const nsIntSize aSize, bool aUsePlaceholder,
89 layers::Image* aImage, nsICanvasRenderingContextInternal* aContext,
90 layers::AsyncCanvasRenderer* aRenderer, nsIInputStream** aStream,
91 imgIEncoder* aEncoder);
93 // Creates and returns an encoder instance of the type specified in aType.
94 // aType may change to "image/png" if no instance of the original type could
95 // be created and we had to fall back to a PNG encoder. A null return value
96 // should be interpreted as NS_IMAGELIB_ERROR_NO_ENCODER and aType is
97 // undefined in this case.
98 static already_AddRefed<imgIEncoder> GetImageEncoder(nsAString& aType);
100 static nsresult EnsureThreadPool();
102 // Thread pool for dispatching EncodingRunnable.
103 static StaticRefPtr<nsIThreadPool> sThreadPool;
105 friend class EncodingRunnable;
106 friend class EncoderThreadPoolTerminator;
110 * The callback interface of ExtractDataAsync and
111 * ExtractDataFromLayersImageAsync. ReceiveBlob() is called on main thread when
112 * encoding is complete.
114 class EncodeCompleteCallback {
115 public:
116 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(EncodeCompleteCallback)
118 MOZ_CAN_RUN_SCRIPT
119 virtual nsresult ReceiveBlob(already_AddRefed<Blob> aBlob) = 0;
121 protected:
122 virtual ~EncodeCompleteCallback() {}
125 } // namespace dom
126 } // namespace mozilla
128 #endif // ImageEncoder_h