Bug 1848619 [wpt PR 41441] - [@scope] Add WPT for name-defining at rules, a=testonly
[gecko.git] / dom / canvas / ImageBitmapRenderingContext.h
blobbba271ec60a8a9166663b4aaa2e87dcb962564d5
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef ImageBitmapRenderingContext_h
6 #define ImageBitmapRenderingContext_h
8 #include "mozilla/dom/ImageBitmap.h"
9 #include "mozilla/gfx/2D.h"
10 #include "mozilla/gfx/DataSurfaceHelpers.h"
11 #include "mozilla/gfx/Point.h"
12 #include "mozilla/layers/WebRenderUserData.h"
13 #include "imgIEncoder.h"
14 #include "ImageEncoder.h"
15 #include "nsICanvasRenderingContextInternal.h"
16 #include "nsWrapperCache.h"
18 namespace mozilla {
20 namespace gfx {
21 class DataSourceSurface;
22 class DrawTarget;
23 class SourceSurface;
24 } // namespace gfx
26 namespace layers {
27 class Image;
28 class ImageContainer;
29 } // namespace layers
31 namespace dom {
33 /**
34 * The purpose of ImageBitmapRenderingContext is to provide a faster and
35 * efficient way to display ImageBitmap. Simply call TransferFromImageBitmap()
36 * then we'll transfer the surface of ImageBitmap to this context and then to
37 * use it to display.
39 * See more details in spec: https://wiki.whatwg.org/wiki/OffscreenCanvas
41 class ImageBitmapRenderingContext final
42 : public nsICanvasRenderingContextInternal,
43 public nsWrapperCache {
44 virtual ~ImageBitmapRenderingContext();
46 public:
47 ImageBitmapRenderingContext();
49 virtual JSObject* WrapObject(JSContext* aCx,
50 JS::Handle<JSObject*> aGivenProto) override;
52 // nsISupports interface + CC
53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
55 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(ImageBitmapRenderingContext)
57 void GetCanvas(
58 Nullable<OwningHTMLCanvasElementOrOffscreenCanvas>& retval) const;
60 void TransferImageBitmap(ImageBitmap& aImageBitmap, ErrorResult& aRv);
61 void TransferFromImageBitmap(ImageBitmap* aImageBitmap, ErrorResult& aRv);
63 // nsICanvasRenderingContextInternal
64 virtual int32_t GetWidth() override { return mWidth; }
65 virtual int32_t GetHeight() override { return mHeight; }
67 NS_IMETHOD SetDimensions(int32_t aWidth, int32_t aHeight) override;
69 NS_IMETHOD InitializeWithDrawTarget(
70 nsIDocShell* aDocShell, NotNull<gfx::DrawTarget*> aTarget) override;
72 virtual mozilla::UniquePtr<uint8_t[]> GetImageBuffer(
73 int32_t* out_format, gfx::IntSize* out_imageSize) override;
74 NS_IMETHOD GetInputStream(const char* aMimeType,
75 const nsAString& aEncoderOptions,
76 nsIInputStream** aStream) override;
78 virtual already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot(
79 gfxAlphaType* aOutAlphaType) override;
81 virtual void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue) override;
82 virtual bool GetIsOpaque() override;
83 void ResetBitmap() override;
84 virtual already_AddRefed<layers::Image> GetAsImage() override {
85 return ClipToIntrinsicSize();
87 bool UpdateWebRenderCanvasData(nsDisplayListBuilder* aBuilder,
88 WebRenderCanvasData* aCanvasData) override;
89 virtual void MarkContextClean() override;
91 NS_IMETHOD Redraw(const gfxRect& aDirty) override;
93 virtual void DidRefresh() override;
95 void MarkContextCleanForFrameCapture() override {
96 mFrameCaptureState = FrameCaptureState::CLEAN;
98 Watchable<FrameCaptureState>* GetFrameCaptureState() override {
99 return &mFrameCaptureState;
102 protected:
103 already_AddRefed<gfx::DataSourceSurface> MatchWithIntrinsicSize();
104 already_AddRefed<layers::Image> ClipToIntrinsicSize();
105 int32_t mWidth;
106 int32_t mHeight;
108 RefPtr<layers::Image> mImage;
111 * Flag to avoid unnecessary surface copies to FrameCaptureListeners in the
112 * case when the canvas is not currently being drawn into and not rendered
113 * but canvas capturing is still ongoing.
115 Watchable<FrameCaptureState> mFrameCaptureState;
118 } // namespace dom
119 } // namespace mozilla
121 #endif /* ImageBitmapRenderingContext_h */