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/EventForwards.h"
19 #include "mozilla/Maybe.h"
20 #include "mozilla/RefPtr.h"
21 #include "mozilla/StateWatching.h"
22 #include "mozilla/UniquePtr.h"
23 #include "mozilla/NotNull.h"
24 #include "mozilla/WeakPtr.h"
25 #include "mozilla/layers/LayersSurfaces.h"
27 #define NS_ICANVASRENDERINGCONTEXTINTERNAL_IID \
29 0xb84f2fed, 0x9d4b, 0x430b, { \
30 0xbd, 0xfb, 0x85, 0x57, 0x8a, 0xc2, 0xb4, 0x4b \
34 class nsICookieJarSettings
;
37 class nsRefreshDriver
;
40 class nsDisplayListBuilder
;
41 class ClientWebGLContext
;
43 class WebGLFramebufferJS
;
46 class CompositableForwarder
;
47 class FwdTransactionTracker
;
51 class LayerTransactionChild
;
52 class PersistentBufferProvider
;
53 class WebRenderCanvasData
;
59 } // namespace mozilla
61 enum class FrameCaptureState
: uint8_t { CLEAN
, DIRTY
};
63 class nsICanvasRenderingContextInternal
: public nsISupports
,
64 public mozilla::SupportsWeakPtr
,
65 public nsAPostRefreshObserver
{
67 using CanvasRenderer
= mozilla::layers::CanvasRenderer
;
68 using WebRenderCanvasData
= mozilla::layers::WebRenderCanvasData
;
70 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICANVASRENDERINGCONTEXTINTERNAL_IID
)
72 nsICanvasRenderingContextInternal();
74 ~nsICanvasRenderingContextInternal();
76 void SetCanvasElement(mozilla::dom::HTMLCanvasElement
* parentCanvas
) {
77 RemovePostRefreshObserver();
78 mCanvasElement
= parentCanvas
;
79 AddPostRefreshObserverIfNecessary();
82 virtual mozilla::PresShell
* GetPresShell();
84 void RemovePostRefreshObserver();
86 void AddPostRefreshObserverIfNecessary();
88 nsIGlobalObject
* GetParentObject() const;
90 nsICookieJarSettings
* GetCookieJarSettings() const;
92 nsIPrincipal
* PrincipalOrNull() const;
94 void SetOffscreenCanvas(mozilla::dom::OffscreenCanvas
* aOffscreenCanvas
) {
95 mOffscreenCanvas
= aOffscreenCanvas
;
98 // Dimensions of the canvas, in pixels.
99 virtual int32_t GetWidth() = 0;
100 virtual int32_t GetHeight() = 0;
102 // Sets the dimensions of the canvas, in pixels. Called
103 // whenever the size of the element changes.
104 NS_IMETHOD
SetDimensions(int32_t width
, int32_t height
) = 0;
106 // Initializes the canvas after the object is constructed.
107 virtual nsresult
Initialize() { return NS_OK
; }
109 // Initializes with an nsIDocShell and DrawTarget. The size is taken from the
111 NS_IMETHOD
InitializeWithDrawTarget(
112 nsIDocShell
* aDocShell
,
113 mozilla::NotNull
<mozilla::gfx::DrawTarget
*> aTarget
) = 0;
115 // Creates an image buffer. Returns null on failure.
116 virtual mozilla::UniquePtr
<uint8_t[]> GetImageBuffer(
117 int32_t* out_format
, mozilla::gfx::IntSize
* out_imageSize
) = 0;
119 // Gives you a stream containing the image represented by this context.
120 // The format is given in mimeTime, for example "image/png".
122 // If the image format does not support transparency or includeTransparency
123 // is false, alpha will be discarded and the result will be the image
124 // composited on black.
125 NS_IMETHOD
GetInputStream(const char* mimeType
,
126 const nsAString
& encoderOptions
,
127 nsIInputStream
** stream
) = 0;
129 // This gets an Azure SourceSurface for the canvas, this will be a snapshot
130 // of the canvas at the time it was called.
131 // If premultAlpha is provided, then it assumed the callee can handle
132 // un-premultiplied surfaces, and *premultAlpha will be set to false
133 // if one is returned.
134 virtual already_AddRefed
<mozilla::gfx::SourceSurface
> GetSurfaceSnapshot(
135 gfxAlphaType
* out_alphaType
= nullptr) = 0;
137 // Like GetSurfaceSnapshot, but will attempt to optimize the snapshot for the
138 // provided DrawTarget, which may be nullptr. By default, this will defer to
139 // GetSurfaceSnapshot and ignore target-dependent optimization.
140 virtual already_AddRefed
<mozilla::gfx::SourceSurface
> GetOptimizedSnapshot(
141 mozilla::gfx::DrawTarget
* aTarget
,
142 gfxAlphaType
* out_alphaType
= nullptr) {
143 return GetSurfaceSnapshot(out_alphaType
);
146 virtual RefPtr
<mozilla::gfx::SourceSurface
> GetFrontBufferSnapshot(bool) {
147 return GetSurfaceSnapshot();
150 // If this is called with true, the backing store of the canvas should
151 // be created as opaque; all compositing operators should assume the
152 // dst alpha is always 1.0. If this is never called, the context's
153 // opaqueness is determined by the context attributes that it's initialized
155 virtual void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue
) = 0;
157 // Returns whether the context is opaque. This value can be based both on
158 // the value of the moz-opaque attribute and on the context's initialization
160 virtual bool GetIsOpaque() = 0;
162 // Clear and/or release backing bitmaps, such as for transferToImageBitmap.
163 virtual void ResetBitmap() = 0;
165 virtual already_AddRefed
<mozilla::layers::Image
> GetAsImage() {
169 virtual bool UpdateWebRenderCanvasData(
170 mozilla::nsDisplayListBuilder
* aBuilder
,
171 WebRenderCanvasData
* aCanvasData
) {
175 virtual bool InitializeCanvasRenderer(mozilla::nsDisplayListBuilder
* aBuilder
,
176 CanvasRenderer
* aRenderer
) {
180 virtual void MarkContextClean() = 0;
182 // Called when a frame is captured.
183 virtual void MarkContextCleanForFrameCapture() = 0;
185 // Whether the context is clean or has been invalidated (dirty) since the last
186 // frame was captured. The Watchable allows the caller to get notified of
188 virtual mozilla::Watchable
<FrameCaptureState
>* GetFrameCaptureState() = 0;
190 // Redraw the dirty rectangle of this canvas.
191 NS_IMETHOD
Redraw(const gfxRect
& dirty
) = 0;
193 NS_IMETHOD
SetContextOptions(JSContext
* cx
, JS::Handle
<JS::Value
> options
,
194 mozilla::ErrorResult
& aRvForDictionaryInit
) {
198 virtual void OnMemoryPressure() {}
200 virtual void OnBeforePaintTransaction() {}
201 virtual void OnDidPaintTransaction() {}
203 virtual mozilla::layers::PersistentBufferProvider
* GetBufferProvider() {
207 virtual mozilla::Maybe
<mozilla::layers::SurfaceDescriptor
> GetFrontBuffer(
208 mozilla::WebGLFramebufferJS
*, const bool webvr
= false) {
209 return mozilla::Nothing();
212 virtual mozilla::Maybe
<mozilla::layers::SurfaceDescriptor
> PresentFrontBuffer(
213 mozilla::WebGLFramebufferJS
* fb
, const bool webvr
= false) {
214 return GetFrontBuffer(fb
, webvr
);
217 virtual already_AddRefed
<mozilla::layers::FwdTransactionTracker
>
218 UseCompositableForwarder(mozilla::layers::CompositableForwarder
* aForwarder
) {
222 void DoSecurityCheck(nsIPrincipal
* aPrincipal
, bool forceWriteOnly
,
225 // Checking if fingerprinting protection is enable for the given target.
226 bool ShouldResistFingerprinting(mozilla::RFPTarget aTarget
) const;
228 bool DispatchEvent(const nsAString
& eventName
, mozilla::CanBubble aCanBubble
,
229 mozilla::Cancelable aIsCancelable
) const;
232 RefPtr
<mozilla::dom::HTMLCanvasElement
> mCanvasElement
;
233 RefPtr
<mozilla::dom::OffscreenCanvas
> mOffscreenCanvas
;
234 RefPtr
<nsRefreshDriver
> mRefreshDriver
;
237 NS_DEFINE_STATIC_IID_ACCESSOR(nsICanvasRenderingContextInternal
,
238 NS_ICANVASRENDERINGCONTEXTINTERNAL_IID
)
240 #endif /* nsICanvasRenderingContextInternal_h___ */