nsICanvasRenderingContextInternal.h (8524B)
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/. */ 5 6 #ifndef nsICanvasRenderingContextInternal_h___ 7 #define nsICanvasRenderingContextInternal_h___ 8 9 #include "gfxRect.h" 10 #include "mozilla/EventForwards.h" 11 #include "mozilla/Maybe.h" 12 #include "mozilla/NotNull.h" 13 #include "mozilla/RefPtr.h" 14 #include "mozilla/StateWatching.h" 15 #include "mozilla/UniquePtr.h" 16 #include "mozilla/WeakPtr.h" 17 #include "mozilla/dom/HTMLCanvasElement.h" 18 #include "mozilla/dom/OffscreenCanvas.h" 19 #include "mozilla/gfx/2D.h" 20 #include "mozilla/layers/LayersSurfaces.h" 21 #include "nsIDocShell.h" 22 #include "nsIInputStream.h" 23 #include "nsISupports.h" 24 #include "nsRefreshObservers.h" 25 26 #define NS_ICANVASRENDERINGCONTEXTINTERNAL_IID \ 27 {0xb84f2fed, 0x9d4b, 0x430b, {0xbd, 0xfb, 0x85, 0x57, 0x8a, 0xc2, 0xb4, 0x4b}} 28 29 class nsICookieJarSettings; 30 class nsIDocShell; 31 class nsIPrincipal; 32 class nsRefreshDriver; 33 34 namespace mozilla { 35 class nsDisplayListBuilder; 36 class ClientWebGLContext; 37 class PresShell; 38 class WebGLFramebufferJS; 39 namespace ipc { 40 class IProtocol; 41 } // namespace ipc 42 namespace layers { 43 class CanvasRenderer; 44 class CompositableForwarder; 45 class FwdTransactionTracker; 46 class Layer; 47 class Image; 48 class LayerManager; 49 class LayerTransactionChild; 50 class PersistentBufferProvider; 51 class WebRenderCanvasData; 52 } // namespace layers 53 namespace gfx { 54 class DrawTarget; 55 class SourceSurface; 56 } // namespace gfx 57 } // namespace mozilla 58 59 enum class FrameCaptureState : uint8_t { CLEAN, DIRTY }; 60 61 class nsICanvasRenderingContextInternal : public nsISupports, 62 public mozilla::SupportsWeakPtr, 63 public nsAPostRefreshObserver { 64 public: 65 using CanvasRenderer = mozilla::layers::CanvasRenderer; 66 using WebRenderCanvasData = mozilla::layers::WebRenderCanvasData; 67 68 NS_INLINE_DECL_STATIC_IID(NS_ICANVASRENDERINGCONTEXTINTERNAL_IID) 69 70 nsICanvasRenderingContextInternal(); 71 72 ~nsICanvasRenderingContextInternal(); 73 74 void SetCanvasElement(mozilla::dom::HTMLCanvasElement* parentCanvas) { 75 RemovePostRefreshObserver(); 76 mCanvasElement = parentCanvas; 77 AddPostRefreshObserverIfNecessary(); 78 } 79 80 virtual mozilla::PresShell* GetPresShell(); 81 82 void RemovePostRefreshObserver(); 83 84 void AddPostRefreshObserverIfNecessary(); 85 86 nsIGlobalObject* GetParentObject() const; 87 88 nsICookieJarSettings* GetCookieJarSettings() const; 89 90 nsIPrincipal* PrincipalOrNull() const; 91 92 void SetOffscreenCanvas(mozilla::dom::OffscreenCanvas* aOffscreenCanvas) { 93 mOffscreenCanvas = aOffscreenCanvas; 94 } 95 96 // Dimensions of the canvas, in pixels. 97 virtual int32_t GetWidth() = 0; 98 virtual int32_t GetHeight() = 0; 99 100 // Sets the dimensions of the canvas, in pixels. Called 101 // whenever the size of the element changes. 102 NS_IMETHOD SetDimensions(int32_t width, int32_t height) = 0; 103 104 // Initializes the canvas after the object is constructed. 105 virtual nsresult Initialize() { return NS_OK; } 106 107 // Initializes with an nsIDocShell and DrawTarget. The size is taken from the 108 // DrawTarget. 109 NS_IMETHOD InitializeWithDrawTarget( 110 nsIDocShell* aDocShell, 111 mozilla::NotNull<mozilla::gfx::DrawTarget*> aTarget) = 0; 112 113 // Creates an image buffer. Returns null on failure. 114 virtual mozilla::UniquePtr<uint8_t[]> GetImageBuffer( 115 mozilla::CanvasUtils::ImageExtraction aExtractionBehavior, 116 int32_t* out_format, mozilla::gfx::IntSize* out_imageSize) = 0; 117 118 // Gives you a stream containing the image represented by this context. 119 // The format is given in mimeTime, for example "image/png". 120 // 121 // If the image format does not support transparency or includeTransparency 122 // is false, alpha will be discarded and the result will be the image 123 // composited on black. 124 NS_IMETHOD GetInputStream( 125 const char* mimeType, const nsAString& encoderOptions, 126 mozilla::CanvasUtils::ImageExtraction extractionBehavior, 127 const nsACString& randomizationKey, nsIInputStream** stream) = 0; 128 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; 136 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, gfxAlphaType* out_alphaType = nullptr); 142 143 virtual mozilla::ipc::IProtocol* SupportsSnapshotExternalCanvas() const { 144 return nullptr; 145 } 146 147 virtual void SyncSnapshot() {} 148 149 virtual RefPtr<mozilla::gfx::SourceSurface> GetFrontBufferSnapshot(bool) { 150 return GetSurfaceSnapshot(); 151 } 152 153 // If this is called with true, the backing store of the canvas should 154 // be created as opaque; all compositing operators should assume the 155 // dst alpha is always 1.0. If this is never called, the context's 156 // opaqueness is determined by the context attributes that it's initialized 157 // with. 158 virtual void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue) = 0; 159 160 // Returns whether the context is opaque. This value can be based both on 161 // the value of the moz-opaque attribute and on the context's initialization 162 // attributes. 163 virtual bool GetIsOpaque() = 0; 164 165 // Clear and/or release backing bitmaps, such as for transferToImageBitmap. 166 virtual void ResetBitmap() = 0; 167 168 virtual already_AddRefed<mozilla::layers::Image> GetAsImage() { 169 return nullptr; 170 } 171 172 virtual bool UpdateWebRenderCanvasData( 173 mozilla::nsDisplayListBuilder* aBuilder, 174 WebRenderCanvasData* aCanvasData) { 175 return false; 176 } 177 178 virtual bool InitializeCanvasRenderer(mozilla::nsDisplayListBuilder* aBuilder, 179 CanvasRenderer* aRenderer) { 180 return false; 181 } 182 183 virtual void MarkContextClean() = 0; 184 185 // Called when a frame is captured. 186 virtual void MarkContextCleanForFrameCapture() = 0; 187 188 // Whether the context is clean or has been invalidated (dirty) since the last 189 // frame was captured. The Watchable allows the caller to get notified of 190 // state changes. 191 virtual mozilla::Watchable<FrameCaptureState>* GetFrameCaptureState() = 0; 192 193 // Redraw the dirty rectangle of this canvas. 194 NS_IMETHOD Redraw(const gfxRect& dirty) = 0; 195 196 NS_IMETHOD SetContextOptions(JSContext* cx, JS::Handle<JS::Value> options, 197 mozilla::ErrorResult& aRvForDictionaryInit) { 198 return NS_OK; 199 } 200 201 virtual void OnMemoryPressure() {} 202 203 virtual void OnBeforePaintTransaction() {} 204 virtual void OnDidPaintTransaction() {} 205 206 virtual mozilla::layers::PersistentBufferProvider* GetBufferProvider() { 207 return nullptr; 208 } 209 210 virtual mozilla::Maybe<mozilla::layers::SurfaceDescriptor> GetFrontBuffer( 211 mozilla::WebGLFramebufferJS*, const bool webvr = false) { 212 return mozilla::Nothing(); 213 } 214 215 virtual mozilla::Maybe<mozilla::layers::SurfaceDescriptor> PresentFrontBuffer( 216 mozilla::WebGLFramebufferJS* fb, const bool webvr = false) { 217 return GetFrontBuffer(fb, webvr); 218 } 219 220 virtual already_AddRefed<mozilla::layers::FwdTransactionTracker> 221 UseCompositableForwarder(mozilla::layers::CompositableForwarder* aForwarder) { 222 return nullptr; 223 } 224 225 void DoSecurityCheck(nsIPrincipal* aPrincipal, bool forceWriteOnly, 226 bool CORSUsed); 227 228 // Checking if fingerprinting protection is enable for the given target. 229 bool ShouldResistFingerprinting(mozilla::RFPTarget aTarget) const; 230 231 bool DispatchEvent(const nsAString& eventName, mozilla::CanBubble aCanBubble, 232 mozilla::Cancelable aIsCancelable) const; 233 234 void RecordCanvasUsage(mozilla::CanvasExtractionAPI aAPI, 235 mozilla::CSSIntSize size) const; 236 237 protected: 238 RefPtr<mozilla::dom::HTMLCanvasElement> mCanvasElement; 239 RefPtr<mozilla::dom::OffscreenCanvas> mOffscreenCanvas; 240 RefPtr<nsRefreshDriver> mRefreshDriver; 241 }; 242 243 #endif /* nsICanvasRenderingContextInternal_h___ */