Bug 1700051: part 26) Correct typo in comment of `mozInlineSpellWordUtil::BuildSoftTe...
[gecko.git] / dom / canvas / OffscreenCanvas.h
blob4ca9ad5acfda9f75dbfb0cfd152512c4856dbed5
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_
10 #include "gfxTypes.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"
18 struct JSContext;
20 namespace mozilla {
22 class ErrorResult;
24 namespace gfx {
25 class SourceSurface;
28 namespace layers {
29 class CanvasClient;
30 class CanvasRenderer;
31 class ImageContainer;
32 } // namespace layers
34 namespace dom {
35 class Blob;
36 class ImageBitmap;
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,
44 uint32_t aHeight,
45 layers::LayersBackend aCompositorBackend,
46 bool aNeutered, bool aIsWriteOnly);
47 ~OffscreenCanvasCloneData();
49 RefPtr<layers::CanvasRenderer> mRenderer;
50 uint32_t mWidth;
51 uint32_t mHeight;
52 layers::LayersBackend mCompositorBackendType;
53 bool mNeutered;
54 bool mIsWriteOnly;
57 class OffscreenCanvas final : public DOMEventTargetHelper,
58 public CanvasRenderingContextHelper {
59 public:
60 NS_DECL_ISUPPORTS_INHERITED
61 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OffscreenCanvas,
62 DOMEventTargetHelper)
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) {
83 if (mNeutered) {
84 aRv.Throw(NS_ERROR_FAILURE);
85 return;
88 if (mWidth != aWidth) {
89 mWidth = aWidth;
90 CanvasAttrChanged();
94 void SetHeight(uint32_t aHeight, ErrorResult& aRv) {
95 if (mNeutered) {
96 aRv.Throw(NS_ERROR_FAILURE);
97 return;
100 if (mHeight != aHeight) {
101 mHeight = aHeight;
102 CanvasAttrChanged();
106 already_AddRefed<ImageBitmap> TransferToImageBitmap(ErrorResult& aRv);
108 already_AddRefed<Promise> ToBlob(JSContext* aCx, const nsAString& aType,
109 JS::Handle<JS::Value> aParams,
110 ErrorResult& aRv);
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
123 // on worker thread.
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();
157 private:
158 ~OffscreenCanvas();
160 nsCOMPtr<nsIGlobalObject> GetGlobalObject();
162 void CanvasAttrChanged() {
163 mAttrDirty = true;
164 ErrorResult dummy;
165 UpdateContext(nullptr, JS::NullHandleValue, dummy);
168 bool mAttrDirty;
169 bool mNeutered;
170 bool mIsWriteOnly;
172 uint32_t mWidth;
173 uint32_t mHeight;
175 layers::LayersBackend mCompositorBackendType;
177 RefPtr<layers::CanvasClient> mCanvasClient;
178 RefPtr<layers::CanvasRenderer> mCanvasRenderer;
181 } // namespace dom
182 } // namespace mozilla
184 #endif // MOZILLA_DOM_OFFSCREENCANVAS_H_