1 /* -*- Mode: C++; tab-width: 2; 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 file,
4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef MOZILLA_DOM_CANVASRENDERINGCONTEXTHELPER_H_
7 #define MOZILLA_DOM_CANVASRENDERINGCONTEXTHELPER_H_
8 
9 #include "mozilla/dom/BindingDeclarations.h"
10 #include "mozilla/layers/LayersTypes.h"
11 #include "nsSize.h"
12 
13 class nsICanvasRenderingContextInternal;
14 class nsIGlobalObject;
15 
16 namespace mozilla {
17 
18 class ErrorResult;
19 
20 namespace dom {
21 
22 class BlobCallback;
23 class EncodeCompleteCallback;
24 
25 enum class CanvasContextType : uint8_t {
26   NoContext,
27   Canvas2D,
28   WebGL1,
29   WebGL2,
30   ImageBitmap
31 };
32 
33 /**
34  * Povides common RenderingContext functionality used by both OffscreenCanvas
35  * and HTMLCanvasElement.
36  */
37 class CanvasRenderingContextHelper
38 {
39 public:
40   virtual already_AddRefed<nsISupports>
41   GetContext(JSContext* aCx,
42              const nsAString& aContextId,
43              JS::Handle<JS::Value> aContextOptions,
44              ErrorResult& aRv);
45 
46   virtual bool GetOpaqueAttr() = 0;
47 
48 protected:
49   virtual nsresult UpdateContext(JSContext* aCx,
50                                  JS::Handle<JS::Value> aNewContextOptions,
51                                  ErrorResult& aRvForDictionaryInit);
52 
53   virtual nsresult ParseParams(JSContext* aCx,
54                                const nsAString& aType,
55                                const JS::Value& aEncoderOptions,
56                                nsAString& outParams,
57                                bool* const outCustomParseOptions);
58 
59   void ToBlob(JSContext* aCx, nsIGlobalObject* global, BlobCallback& aCallback,
60               const nsAString& aType, JS::Handle<JS::Value> aParams,
61               ErrorResult& aRv);
62 
63   void ToBlob(JSContext* aCx, nsIGlobalObject* aGlobal, EncodeCompleteCallback* aCallback,
64               const nsAString& aType, JS::Handle<JS::Value> aParams,
65               ErrorResult& aRv);
66 
67   virtual already_AddRefed<nsICanvasRenderingContextInternal>
68   CreateContext(CanvasContextType aContextType);
69 
70   already_AddRefed<nsICanvasRenderingContextInternal>
71   CreateContextHelper(CanvasContextType aContextType,
72                       layers::LayersBackend aCompositorBackend);
73 
74   virtual nsIntSize GetWidthHeight() = 0;
75 
76   CanvasContextType mCurrentContextType;
77   nsCOMPtr<nsICanvasRenderingContextInternal> mCurrentContext;
78 };
79 
80 } // namespace dom
81 } // namespace mozilla
82 
83 #endif // MOZILLA_DOM_CANVASRENDERINGCONTEXTHELPER_H_
84