Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / dom / canvas / ImageBitmap.h
blob7eb037e342ee61fbe6a3bb92ed9023d596c892c9
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/. */
7 #ifndef mozilla_dom_ImageBitmap_h
8 #define mozilla_dom_ImageBitmap_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/SurfaceFromElementResult.h"
12 #include "mozilla/dom/ImageBitmapBinding.h"
13 #include "mozilla/dom/ImageBitmapSource.h"
14 #include "mozilla/dom/TypedArray.h"
15 #include "mozilla/gfx/Rect.h"
16 #include "mozilla/Maybe.h"
17 #include "mozilla/UniquePtr.h"
18 #include "ImageData.h"
19 #include "gfxTypes.h" // for gfxAlphaType
20 #include "nsCycleCollectionParticipant.h"
22 struct JSContext;
23 struct JSStructuredCloneReader;
24 struct JSStructuredCloneWriter;
26 class nsIGlobalObject;
28 namespace mozilla {
30 class ErrorResult;
32 namespace gfx {
33 class DataSourceSurface;
34 class DrawTarget;
35 class SourceSurface;
36 } // namespace gfx
38 namespace layers {
39 class Image;
42 namespace dom {
43 class OffscreenCanvas;
45 class ArrayBufferViewOrArrayBuffer;
46 class CanvasRenderingContext2D;
47 class CreateImageBitmapFromBlob;
48 class CreateImageBitmapFromBlobTask;
49 class CreateImageBitmapFromBlobWorkerTask;
50 class ImageBitmapShutdownObserver;
51 class File;
52 class HTMLCanvasElement;
53 class HTMLImageElement;
54 class HTMLVideoElement;
55 class ImageData;
56 class ImageUtils;
57 class Promise;
58 class PostMessageEvent; // For StructuredClone between windows.
59 class SVGImageElement;
60 class VideoFrame;
61 class SendShutdownToWorkerThread;
63 struct ImageBitmapCloneData final {
64 RefPtr<gfx::DataSourceSurface> mSurface;
65 gfx::IntRect mPictureRect;
66 gfxAlphaType mAlphaType;
67 bool mWriteOnly;
71 * ImageBitmap is an opaque handler to several kinds of image-like objects from
72 * HTMLImageElement, HTMLVideoElement, HTMLCanvasElement, ImageData to
73 * CanvasRenderingContext2D and Image Blob.
75 * An ImageBitmap could be painted to a canvas element.
77 * Generally, an ImageBitmap only keeps a reference to its source object's
78 * buffer, but if the source object is an ImageData, an Blob or a
79 * HTMLCanvasElement with WebGL rendering context, the ImageBitmap copy the
80 * source object's buffer.
82 class ImageBitmap final : public nsISupports, public nsWrapperCache {
83 public:
84 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
85 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ImageBitmap)
87 nsCOMPtr<nsIGlobalObject> GetParentObject() const { return mParent; }
89 virtual JSObject* WrapObject(JSContext* aCx,
90 JS::Handle<JSObject*> aGivenProto) override;
92 uint32_t Width() const { return mPictureRect.Width(); }
94 uint32_t Height() const { return mPictureRect.Height(); }
96 void Close();
98 SurfaceFromElementResult SurfaceFrom(uint32_t aSurfaceFlags);
101 * The PrepareForDrawTarget() might return null if the mPictureRect does not
102 * intersect with the size of mData.
104 already_AddRefed<gfx::SourceSurface> PrepareForDrawTarget(
105 gfx::DrawTarget* aTarget);
108 * Transfer ownership of buffer to caller. So this function call
109 * Close() implicitly.
111 already_AddRefed<layers::Image> TransferAsImage();
113 // This method returns null if the image has been already closed.
114 UniquePtr<ImageBitmapCloneData> ToCloneData() const;
116 static already_AddRefed<ImageBitmap> CreateFromSourceSurface(
117 nsIGlobalObject* aGlobal, gfx::SourceSurface* aSource, ErrorResult& aRv);
119 static already_AddRefed<ImageBitmap> CreateFromCloneData(
120 nsIGlobalObject* aGlobal, ImageBitmapCloneData* aData);
122 static already_AddRefed<ImageBitmap> CreateFromOffscreenCanvas(
123 nsIGlobalObject* aGlobal, OffscreenCanvas& aOffscreenCanvas,
124 ErrorResult& aRv);
126 static already_AddRefed<Promise> Create(nsIGlobalObject* aGlobal,
127 const ImageBitmapSource& aSrc,
128 const Maybe<gfx::IntRect>& aCropRect,
129 const ImageBitmapOptions& aOptions,
130 ErrorResult& aRv);
132 static JSObject* ReadStructuredClone(
133 JSContext* aCx, JSStructuredCloneReader* aReader,
134 nsIGlobalObject* aParent,
135 const nsTArray<RefPtr<gfx::DataSourceSurface>>& aClonedSurfaces,
136 uint32_t aIndex);
138 static void WriteStructuredClone(
139 JSStructuredCloneWriter* aWriter,
140 nsTArray<RefPtr<gfx::DataSourceSurface>>& aClonedSurfaces,
141 ImageBitmap* aImageBitmap, ErrorResult& aRv);
143 friend CreateImageBitmapFromBlob;
144 friend CreateImageBitmapFromBlobTask;
145 friend CreateImageBitmapFromBlobWorkerTask;
146 friend ImageBitmapShutdownObserver;
148 size_t GetAllocatedSize() const;
150 void OnShutdown();
152 bool IsWriteOnly() const { return mWriteOnly; }
153 bool IsClosed() const { return !mData; };
155 protected:
157 * The default value of aIsPremultipliedAlpha is TRUE because that the
158 * data stored in HTMLImageElement, HTMLVideoElement, HTMLCanvasElement,
159 * CanvasRenderingContext2D are alpha-premultiplied in default.
161 * Actually, if one HTMLCanvasElement's rendering context is WebGLContext, it
162 * is possible to get un-premultipliedAlpha data out. But, we do not do it in
163 * the CreateInternal(from HTMLCanvasElement) method.
165 * It is also possible to decode an image which is encoded with alpha channel
166 * to be non-premultipliedAlpha. This could be applied in
167 * 1) the CreateInternal(from HTMLImageElement) method (which might trigger
168 * re-decoding if the original decoded data is alpha-premultiplied) and
169 * 2) while decoding a blob. But we do not do it in both code path too.
171 * ImageData's underlying data is triggered as non-premultipliedAlpha, so set
172 * the aIsPremultipliedAlpha to be false in the
173 * CreateInternal(from ImageData) method.
175 ImageBitmap(nsIGlobalObject* aGlobal, layers::Image* aData,
176 bool aAllocatedImageData, bool aWriteOnly,
177 gfxAlphaType aAlphaType = gfxAlphaType::Premult);
179 virtual ~ImageBitmap();
181 void SetPictureRect(const gfx::IntRect& aRect, ErrorResult& aRv);
183 void RemoveAssociatedMemory();
185 static already_AddRefed<ImageBitmap> CreateImageBitmapInternal(
186 nsIGlobalObject* aGlobal, gfx::SourceSurface* aSurface,
187 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
188 const bool aWriteOnly, const bool aAllocatedImageData,
189 const bool aMustCopy, const gfxAlphaType aAlphaType, ErrorResult& aRv);
191 static already_AddRefed<ImageBitmap> CreateInternal(
192 nsIGlobalObject* aGlobal, HTMLImageElement& aImageEl,
193 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
194 ErrorResult& aRv);
196 static already_AddRefed<ImageBitmap> CreateInternal(
197 nsIGlobalObject* aGlobal, SVGImageElement& aImageEl,
198 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
199 ErrorResult& aRv);
201 static already_AddRefed<ImageBitmap> CreateInternal(
202 nsIGlobalObject* aGlobal, HTMLVideoElement& aVideoEl,
203 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
204 ErrorResult& aRv);
206 static already_AddRefed<ImageBitmap> CreateInternal(
207 nsIGlobalObject* aGlobal, HTMLCanvasElement& aCanvasEl,
208 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
209 ErrorResult& aRv);
211 static already_AddRefed<ImageBitmap> CreateInternal(
212 nsIGlobalObject* aGlobal, OffscreenCanvas& aOffscreenCanvas,
213 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
214 ErrorResult& aRv);
216 static already_AddRefed<ImageBitmap> CreateInternal(
217 nsIGlobalObject* aGlobal, ImageData& aImageData,
218 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
219 ErrorResult& aRv);
221 static already_AddRefed<ImageBitmap> CreateInternal(
222 nsIGlobalObject* aGlobal, CanvasRenderingContext2D& aCanvasCtx,
223 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
224 ErrorResult& aRv);
226 static already_AddRefed<ImageBitmap> CreateInternal(
227 nsIGlobalObject* aGlobal, ImageBitmap& aImageBitmap,
228 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
229 ErrorResult& aRv);
231 static already_AddRefed<ImageBitmap> CreateInternal(
232 nsIGlobalObject* aGlobal, VideoFrame& aVideoFrame,
233 const Maybe<gfx::IntRect>& aCropRect, const ImageBitmapOptions& aOptions,
234 ErrorResult& aRv);
236 nsCOMPtr<nsIGlobalObject> mParent;
239 * The mData is the data buffer of an ImageBitmap, so the mData must not be
240 * null.
242 * The mSurface is a cache for drawing the ImageBitmap onto a
243 * HTMLCanvasElement. The mSurface is null while the ImageBitmap is created
244 * and then will be initialized while the PrepareForDrawTarget() method is
245 * called first time.
247 * The mSurface might just be a reference to the same data buffer of the mData
248 * if the are of mPictureRect is just the same as the mData's size. Or, it is
249 * a independent data buffer which is copied and cropped form the mData's data
250 * buffer.
252 RefPtr<layers::Image> mData;
253 RefPtr<gfx::SourceSurface> mSurface;
256 * The mPictureRect is the size of the source image in default, however, if
257 * users specify the cropping area while creating an ImageBitmap, then this
258 * mPictureRect is the cropping area.
260 * Note that if the CreateInternal() copies and crops data from the source
261 * image, then this mPictureRect is just the size of the final mData.
263 * The mPictureRect will be used at PrepareForDrawTarget() while user is going
264 * to draw this ImageBitmap into a HTMLCanvasElement.
266 gfx::IntRect mPictureRect;
268 gfxAlphaType mAlphaType;
270 RefPtr<SendShutdownToWorkerThread> mShutdownRunnable;
273 * Whether this object allocated allocated and owns the image data.
275 bool mAllocatedImageData;
278 * Write-Only flag is set to true if this image has been generated from a
279 * cross-origin source. This is the opposite of what is called 'origin-clean'
280 * in the spec.
282 bool mWriteOnly;
285 size_t BindingJSObjectMallocBytes(ImageBitmap* aBitmap);
287 } // namespace dom
288 } // namespace mozilla
290 #endif // mozilla_dom_ImageBitmap_h