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   WebGPU,
31   ImageBitmap
32 };
33 
34 /**
35  * Povides common RenderingContext functionality used by both OffscreenCanvas
36  * and HTMLCanvasElement.
37  */
38 class CanvasRenderingContextHelper {
39  public:
40   CanvasRenderingContextHelper();
41 
42   virtual already_AddRefed<nsISupports> GetContext(
43       JSContext* aCx, const nsAString& aContextId,
44       JS::Handle<JS::Value> aContextOptions, 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, const nsAString& aType,
54                                const JS::Value& aEncoderOptions,
55                                nsAString& outParams,
56                                bool* const outCustomParseOptions);
57 
58   void ToBlob(JSContext* aCx, nsIGlobalObject* global, BlobCallback& aCallback,
59               const nsAString& aType, JS::Handle<JS::Value> aParams,
60               bool aUsePlaceholder, ErrorResult& aRv);
61 
62   void ToBlob(JSContext* aCx, nsIGlobalObject* aGlobal,
63               EncodeCompleteCallback* aCallback, const nsAString& aType,
64               JS::Handle<JS::Value> aParams, bool aUsePlaceholder,
65               ErrorResult& aRv);
66 
67   virtual already_AddRefed<nsICanvasRenderingContextInternal> CreateContext(
68       CanvasContextType aContextType);
69 
70   already_AddRefed<nsICanvasRenderingContextInternal> CreateContextHelper(
71       CanvasContextType aContextType, layers::LayersBackend aCompositorBackend);
72 
73   virtual nsIntSize GetWidthHeight() = 0;
74 
75   CanvasContextType mCurrentContextType;
76   nsCOMPtr<nsICanvasRenderingContextInternal> mCurrentContext;
77 };
78 
79 }  // namespace dom
80 namespace CanvasUtils {
81 bool GetCanvasContextType(const nsAString&, dom::CanvasContextType* const);
82 }  // namespace CanvasUtils
83 }  // namespace mozilla
84 
85 #endif  // MOZILLA_DOM_CANVASRENDERINGCONTEXTHELPER_H_
86