1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsICanvasRenderingContextInternal_h___
7 #define nsICanvasRenderingContextInternal_h___
10 #include "mozilla/gfx/2D.h"
11 #include "nsISupports.h"
12 #include "nsIInputStream.h"
13 #include "nsIDocShell.h"
14 #include "nsRefreshObservers.h"
15 #include "nsRFPService.h"
16 #include "mozilla/dom/HTMLCanvasElement.h"
17 #include "mozilla/dom/OffscreenCanvas.h"
18 #include "mozilla/Maybe.h"
19 #include "mozilla/RefPtr.h"
20 #include "mozilla/StateWatching.h"
21 #include "mozilla/UniquePtr.h"
22 #include "mozilla/NotNull.h"
23 #include "mozilla/WeakPtr.h"
24 #include "mozilla/layers/LayersSurfaces.h"
26 #define NS_ICANVASRENDERINGCONTEXTINTERNAL_IID \
28 0xb84f2fed, 0x9d4b, 0x430b, { \
29 0xbd, 0xfb, 0x85, 0x57, 0x8a, 0xc2, 0xb4, 0x4b \
33 class nsICookieJarSettings
;
36 class nsRefreshDriver
;
39 class nsDisplayListBuilder
;
40 class ClientWebGLContext
;
42 class WebGLFramebufferJS
;
48 class LayerTransactionChild
;
49 class PersistentBufferProvider
;
50 class WebRenderCanvasData
;
56 } // namespace mozilla
58 enum class FrameCaptureState
: uint8_t { CLEAN
, DIRTY
};
60 class nsICanvasRenderingContextInternal
: public nsISupports
,
61 public mozilla::SupportsWeakPtr
,
62 public nsAPostRefreshObserver
{
64 using CanvasRenderer
= mozilla::layers::CanvasRenderer
;
65 using WebRenderCanvasData
= mozilla::layers::WebRenderCanvasData
;
67 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICANVASRENDERINGCONTEXTINTERNAL_IID
)
69 nsICanvasRenderingContextInternal();
71 ~nsICanvasRenderingContextInternal();
73 void SetCanvasElement(mozilla::dom::HTMLCanvasElement
* parentCanvas
) {
74 RemovePostRefreshObserver();
75 mCanvasElement
= parentCanvas
;
76 AddPostRefreshObserverIfNecessary();
79 virtual mozilla::PresShell
* GetPresShell();
81 void RemovePostRefreshObserver();
83 void AddPostRefreshObserverIfNecessary();
85 nsIGlobalObject
* GetParentObject() const;
87 nsICookieJarSettings
* GetCookieJarSettings() const;
89 nsIPrincipal
* PrincipalOrNull() const;
91 void SetOffscreenCanvas(mozilla::dom::OffscreenCanvas
* aOffscreenCanvas
) {
92 mOffscreenCanvas
= aOffscreenCanvas
;
95 // Dimensions of the canvas, in pixels.
96 virtual int32_t GetWidth() = 0;
97 virtual int32_t GetHeight() = 0;
99 // Sets the dimensions of the canvas, in pixels. Called
100 // whenever the size of the element changes.
101 NS_IMETHOD
SetDimensions(int32_t width
, int32_t height
) = 0;
103 // Initializes the canvas after the object is constructed.
104 virtual void Initialize() {}
106 // Initializes with an nsIDocShell and DrawTarget. The size is taken from the
108 NS_IMETHOD
InitializeWithDrawTarget(
109 nsIDocShell
* aDocShell
,
110 mozilla::NotNull
<mozilla::gfx::DrawTarget
*> aTarget
) = 0;
112 // Creates an image buffer. Returns null on failure.
113 virtual mozilla::UniquePtr
<uint8_t[]> GetImageBuffer(
114 int32_t* out_format
, mozilla::gfx::IntSize
* out_imageSize
) = 0;
116 // Gives you a stream containing the image represented by this context.
117 // The format is given in mimeTime, for example "image/png".
119 // If the image format does not support transparency or includeTransparency
120 // is false, alpha will be discarded and the result will be the image
121 // composited on black.
122 NS_IMETHOD
GetInputStream(const char* mimeType
,
123 const nsAString
& encoderOptions
,
124 nsIInputStream
** stream
) = 0;
126 // This gets an Azure SourceSurface for the canvas, this will be a snapshot
127 // of the canvas at the time it was called.
128 // If premultAlpha is provided, then it assumed the callee can handle
129 // un-premultiplied surfaces, and *premultAlpha will be set to false
130 // if one is returned.
131 virtual already_AddRefed
<mozilla::gfx::SourceSurface
> GetSurfaceSnapshot(
132 gfxAlphaType
* out_alphaType
= nullptr) = 0;
134 // Like GetSurfaceSnapshot, but will attempt to optimize the snapshot for the
135 // provided DrawTarget, which may be nullptr. By default, this will defer to
136 // GetSurfaceSnapshot and ignore target-dependent optimization.
137 virtual already_AddRefed
<mozilla::gfx::SourceSurface
> GetOptimizedSnapshot(
138 mozilla::gfx::DrawTarget
* aTarget
,
139 gfxAlphaType
* out_alphaType
= nullptr) {
140 return GetSurfaceSnapshot(out_alphaType
);
143 virtual RefPtr
<mozilla::gfx::SourceSurface
> GetFrontBufferSnapshot(bool) {
144 return GetSurfaceSnapshot();
147 // If this is called with true, the backing store of the canvas should
148 // be created as opaque; all compositing operators should assume the
149 // dst alpha is always 1.0. If this is never called, the context's
150 // opaqueness is determined by the context attributes that it's initialized
152 virtual void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue
) = 0;
154 // Returns whether the context is opaque. This value can be based both on
155 // the value of the moz-opaque attribute and on the context's initialization
157 virtual bool GetIsOpaque() = 0;
159 // Clear and/or release backing bitmaps, such as for transferToImageBitmap.
160 virtual void ResetBitmap() = 0;
162 virtual already_AddRefed
<mozilla::layers::Image
> GetAsImage() {
166 virtual bool UpdateWebRenderCanvasData(
167 mozilla::nsDisplayListBuilder
* aBuilder
,
168 WebRenderCanvasData
* aCanvasData
) {
172 virtual bool InitializeCanvasRenderer(mozilla::nsDisplayListBuilder
* aBuilder
,
173 CanvasRenderer
* aRenderer
) {
177 virtual void MarkContextClean() = 0;
179 // Called when a frame is captured.
180 virtual void MarkContextCleanForFrameCapture() = 0;
182 // Whether the context is clean or has been invalidated (dirty) since the last
183 // frame was captured. The Watchable allows the caller to get notified of
185 virtual mozilla::Watchable
<FrameCaptureState
>* GetFrameCaptureState() = 0;
187 // Redraw the dirty rectangle of this canvas.
188 NS_IMETHOD
Redraw(const gfxRect
& dirty
) = 0;
190 NS_IMETHOD
SetContextOptions(JSContext
* cx
, JS::Handle
<JS::Value
> options
,
191 mozilla::ErrorResult
& aRvForDictionaryInit
) {
195 virtual void OnMemoryPressure() {}
197 virtual void OnBeforePaintTransaction() {}
198 virtual void OnDidPaintTransaction() {}
200 virtual mozilla::layers::PersistentBufferProvider
* GetBufferProvider() {
204 virtual mozilla::Maybe
<mozilla::layers::SurfaceDescriptor
> GetFrontBuffer(
205 mozilla::WebGLFramebufferJS
*, const bool webvr
= false) {
206 return mozilla::Nothing();
209 virtual mozilla::Maybe
<mozilla::layers::SurfaceDescriptor
> PresentFrontBuffer(
210 mozilla::WebGLFramebufferJS
* fb
, mozilla::layers::TextureType
,
211 const bool webvr
= false) {
212 return GetFrontBuffer(fb
, webvr
);
215 void DoSecurityCheck(nsIPrincipal
* aPrincipal
, bool forceWriteOnly
,
218 // Checking if fingerprinting protection is enable for the given target.
219 bool ShouldResistFingerprinting(mozilla::RFPTarget aTarget
) const;
222 RefPtr
<mozilla::dom::HTMLCanvasElement
> mCanvasElement
;
223 RefPtr
<mozilla::dom::OffscreenCanvas
> mOffscreenCanvas
;
224 RefPtr
<nsRefreshDriver
> mRefreshDriver
;
227 NS_DEFINE_STATIC_IID_ACCESSOR(nsICanvasRenderingContextInternal
,
228 NS_ICANVASRENDERINGCONTEXTINTERNAL_IID
)
230 #endif /* nsICanvasRenderingContextInternal_h___ */