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_OFFSCREENCANVAS_H_
8 #define MOZILLA_DOM_OFFSCREENCANVAS_H_
11 #include "mozilla/dom/CanvasRenderingContextHelper.h"
12 #include "mozilla/dom/ImageEncoder.h"
13 #include "mozilla/dom/OffscreenCanvasDisplayHelper.h"
14 #include "mozilla/DOMEventTargetHelper.h"
15 #include "mozilla/layers/LayersTypes.h"
16 #include "mozilla/Maybe.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/UniquePtr.h"
19 #include "nsCycleCollectionParticipant.h"
24 class CancelableRunnable
;
26 enum class RFPTarget
: uint64_t;
33 enum class OffscreenRenderingContextId
: uint8_t;
35 class EncodeCompleteCallback
;
36 class OffscreenCanvasDisplayHelper
;
38 struct ImageEncodeOptions
;
40 using OwningOffscreenRenderingContext
= class
41 OwningOffscreenCanvasRenderingContext2DOrImageBitmapRenderingContextOrWebGLRenderingContextOrWebGL2RenderingContextOrGPUCanvasContext
;
43 // This is helper class for transferring OffscreenCanvas to worker thread.
44 // Because OffscreenCanvas is not thread-safe. So we cannot pass Offscreen-
45 // Canvas to worker thread directly. Thus, we create this helper class and
46 // store necessary data in it then pass it to worker thread.
47 struct OffscreenCanvasCloneData final
{
48 OffscreenCanvasCloneData(OffscreenCanvasDisplayHelper
* aDisplay
,
49 uint32_t aWidth
, uint32_t aHeight
,
50 layers::LayersBackend aCompositorBackend
,
51 bool aNeutered
, bool aIsWriteOnly
,
52 nsIPrincipal
* aExpandedReader
);
53 ~OffscreenCanvasCloneData();
55 RefPtr
<OffscreenCanvasDisplayHelper
> mDisplay
;
58 layers::LayersBackend mCompositorBackendType
;
61 RefPtr
<nsIPrincipal
> mExpandedReader
;
64 class OffscreenCanvas final
: public DOMEventTargetHelper
,
65 public CanvasRenderingContextHelper
{
67 NS_DECL_ISUPPORTS_INHERITED
68 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OffscreenCanvas
,
71 IMPL_EVENT_HANDLER(contextlost
);
72 IMPL_EVENT_HANDLER(contextrestored
);
74 OffscreenCanvas(nsIGlobalObject
* aGlobal
, uint32_t aWidth
, uint32_t aHeight
);
76 OffscreenCanvas(nsIGlobalObject
* aGlobal
, uint32_t aWidth
, uint32_t aHeight
,
77 layers::LayersBackend aCompositorBackend
,
78 already_AddRefed
<OffscreenCanvasDisplayHelper
> aDisplay
);
82 nsIGlobalObject
* GetParentObject() const { return GetOwnerGlobal(); }
84 virtual JSObject
* WrapObject(JSContext
* aCx
,
85 JS::Handle
<JSObject
*> aGivenProto
) override
;
87 static already_AddRefed
<OffscreenCanvas
> Constructor(
88 const GlobalObject
& aGlobal
, uint32_t aWidth
, uint32_t aHeight
,
91 void ClearResources();
93 uint32_t Width() const { return mWidth
; }
94 uint32_t Height() const { return mHeight
; }
95 void SetWidth(uint32_t aWidth
, ErrorResult
& aRv
);
96 void SetHeight(uint32_t aHeight
, ErrorResult
& aRv
);
98 void GetContext(JSContext
* aCx
, const OffscreenRenderingContextId
& aContextId
,
99 JS::Handle
<JS::Value
> aContextOptions
,
100 Nullable
<OwningOffscreenRenderingContext
>& aResult
,
103 already_AddRefed
<ImageBitmap
> TransferToImageBitmap(ErrorResult
& aRv
);
105 already_AddRefed
<Promise
> ConvertToBlob(const ImageEncodeOptions
& aOptions
,
108 already_AddRefed
<Promise
> ToBlob(JSContext
* aCx
, const nsAString
& aType
,
109 JS::Handle
<JS::Value
> aParams
,
112 Maybe
<uint64_t> GetWindowID();
114 nsICanvasRenderingContextInternal
* GetContext() const {
115 return mCurrentContext
;
118 CanvasContextType
GetContextType() const { return mCurrentContextType
; }
120 already_AddRefed
<gfx::SourceSurface
> GetSurfaceSnapshot(
121 gfxAlphaType
* aOutAlphaType
= nullptr);
123 static already_AddRefed
<OffscreenCanvas
> CreateFromCloneData(
124 nsIGlobalObject
* aGlobal
, OffscreenCanvasCloneData
* aData
);
126 UniquePtr
<OffscreenCanvasCloneData
> ToCloneData(JSContext
* aCx
);
128 void UpdateDisplayData(const OffscreenCanvasDisplayData
& aData
);
130 void CommitFrameToCompositor();
131 void DequeueCommitToCompositor();
132 void QueueCommitToCompositor();
134 virtual bool GetOpaqueAttr() override
{ return false; }
136 CSSIntSize
GetWidthHeight() override
{ return CSSIntSize(mWidth
, mHeight
); }
138 virtual already_AddRefed
<nsICanvasRenderingContextInternal
> CreateContext(
139 CanvasContextType aContextType
) override
;
147 bool MayNeuter() const { return !mNeutered
&& !mCurrentContext
; }
149 void SetSize(const nsIntSize
& aSize
, ErrorResult
& aRv
);
151 nsIPrincipal
* GetExpandedReader() const { return mExpandedReader
; }
153 void SetWriteOnly(RefPtr
<nsIPrincipal
>&& aExpandedReader
);
155 void SetWriteOnly(nsIPrincipal
* aExpandedReader
= nullptr) {
156 RefPtr
<nsIPrincipal
> expandedReader(aExpandedReader
);
157 SetWriteOnly(std::move(expandedReader
));
160 bool IsWriteOnly() const { return mIsWriteOnly
; }
162 // Determines if the caller should be able to read the content.
163 bool CallerCanRead(nsIPrincipal
& aPrincipal
) const;
165 layers::LayersBackend
GetCompositorBackendType() const {
166 return mCompositorBackendType
;
169 bool ShouldResistFingerprinting(mozilla::RFPTarget aTarget
) const;
171 bool IsTransferredFromElement() const { return !!mDisplay
; }
176 already_AddRefed
<EncodeCompleteCallback
> CreateEncodeCompleteCallback(
179 void CanvasAttrChanged() {
181 UpdateContext(nullptr, JS::NullHandleValue
, dummy
);
184 bool mNeutered
= false;
185 bool mIsWriteOnly
= false;
188 uint32_t mHeight
= 0;
190 layers::LayersBackend mCompositorBackendType
=
191 layers::LayersBackend::LAYERS_NONE
;
193 RefPtr
<OffscreenCanvasDisplayHelper
> mDisplay
;
194 RefPtr
<CancelableRunnable
> mPendingCommit
;
195 RefPtr
<nsIPrincipal
> mExpandedReader
;
196 Maybe
<OffscreenCanvasDisplayData
> mPendingUpdate
;
200 } // namespace mozilla
202 #endif // MOZILLA_DOM_OFFSCREENCANVAS_H_