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/ImageEncoder.h"
12 #include "mozilla/DOMEventTargetHelper.h"
13 #include "mozilla/layers/LayersTypes.h"
14 #include "mozilla/RefPtr.h"
15 #include "CanvasRenderingContextHelper.h"
16 #include "nsCycleCollectionParticipant.h"
38 // This is helper class for transferring OffscreenCanvas to worker thread.
39 // Because OffscreenCanvas is not thread-safe. So we cannot pass Offscreen-
40 // Canvas to worker thread directly. Thus, we create this helper class and
41 // store necessary data in it then pass it to worker thread.
42 struct OffscreenCanvasCloneData final
{
43 OffscreenCanvasCloneData(layers::CanvasRenderer
* aRenderer
, uint32_t aWidth
,
45 layers::LayersBackend aCompositorBackend
,
46 bool aNeutered
, bool aIsWriteOnly
);
47 ~OffscreenCanvasCloneData();
49 RefPtr
<layers::CanvasRenderer
> mRenderer
;
52 layers::LayersBackend mCompositorBackendType
;
57 class OffscreenCanvas final
: public DOMEventTargetHelper
,
58 public CanvasRenderingContextHelper
{
60 NS_DECL_ISUPPORTS_INHERITED
61 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OffscreenCanvas
,
64 OffscreenCanvas(nsIGlobalObject
* aGlobal
, uint32_t aWidth
, uint32_t aHeight
,
65 layers::LayersBackend aCompositorBackend
,
66 layers::CanvasRenderer
* aRenderer
);
68 nsCOMPtr
<nsIGlobalObject
> GetParentObject() const { return GetOwnerGlobal(); }
70 virtual JSObject
* WrapObject(JSContext
* aCx
,
71 JS::Handle
<JSObject
*> aGivenProto
) override
;
73 static already_AddRefed
<OffscreenCanvas
> Constructor(
74 const GlobalObject
& aGlobal
, uint32_t aWidth
, uint32_t aHeight
);
76 void ClearResources();
78 uint32_t Width() const { return mWidth
; }
80 uint32_t Height() const { return mHeight
; }
82 void SetWidth(uint32_t aWidth
, ErrorResult
& aRv
) {
84 aRv
.Throw(NS_ERROR_FAILURE
);
88 if (mWidth
!= aWidth
) {
94 void SetHeight(uint32_t aHeight
, ErrorResult
& aRv
) {
96 aRv
.Throw(NS_ERROR_FAILURE
);
100 if (mHeight
!= aHeight
) {
106 already_AddRefed
<ImageBitmap
> TransferToImageBitmap(ErrorResult
& aRv
);
108 already_AddRefed
<Promise
> ToBlob(JSContext
* aCx
, const nsAString
& aType
,
109 JS::Handle
<JS::Value
> aParams
,
112 nsICanvasRenderingContextInternal
* GetContext() const {
113 return mCurrentContext
;
116 already_AddRefed
<gfx::SourceSurface
> GetSurfaceSnapshot(
117 gfxAlphaType
* aOutAlphaType
= nullptr);
119 static already_AddRefed
<OffscreenCanvas
> CreateFromCloneData(
120 nsIGlobalObject
* aGlobal
, OffscreenCanvasCloneData
* aData
);
122 // Return true on main-thread, and return gfx.offscreencanvas.enabled
124 static bool PrefEnabledOnWorkerThread(JSContext
* aCx
, JSObject
* aObj
);
126 OffscreenCanvasCloneData
* ToCloneData();
128 void CommitFrameToCompositor();
130 virtual bool GetOpaqueAttr() override
{ return false; }
132 virtual nsIntSize
GetWidthHeight() override
{
133 return nsIntSize(mWidth
, mHeight
);
136 virtual already_AddRefed
<nsICanvasRenderingContextInternal
> CreateContext(
137 CanvasContextType aContextType
) override
;
139 virtual already_AddRefed
<nsISupports
> GetContext(
140 JSContext
* aCx
, const nsAString
& aContextId
,
141 JS::Handle
<JS::Value
> aContextOptions
, ErrorResult
& aRv
) override
;
143 void SetNeutered() { mNeutered
= true; }
145 bool IsNeutered() const { return mNeutered
; }
147 void SetWriteOnly() { mIsWriteOnly
= true; }
149 bool IsWriteOnly() const { return mIsWriteOnly
; }
151 layers::LayersBackend
GetCompositorBackendType() const {
152 return mCompositorBackendType
;
155 layers::ImageContainer
* GetImageContainer();
160 nsCOMPtr
<nsIGlobalObject
> GetGlobalObject();
162 void CanvasAttrChanged() {
165 UpdateContext(nullptr, JS::NullHandleValue
, dummy
);
175 layers::LayersBackend mCompositorBackendType
;
177 RefPtr
<layers::CanvasClient
> mCanvasClient
;
178 RefPtr
<layers::CanvasRenderer
> mCanvasRenderer
;
182 } // namespace mozilla
184 #endif // MOZILLA_DOM_OFFSCREENCANVAS_H_