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  public:
39   virtual already_AddRefed<nsISupports> GetContext(
40       JSContext* aCx, const nsAString& aContextId,
41       JS::Handle<JS::Value> aContextOptions, ErrorResult& aRv);
42 
43   virtual bool GetOpaqueAttr() = 0;
44 
45  protected:
46   virtual nsresult UpdateContext(JSContext* aCx,
47                                  JS::Handle<JS::Value> aNewContextOptions,
48                                  ErrorResult& aRvForDictionaryInit);
49 
50   virtual nsresult ParseParams(JSContext* aCx, const nsAString& aType,
51                                const JS::Value& aEncoderOptions,
52                                nsAString& outParams,
53                                bool* const outCustomParseOptions);
54 
55   void ToBlob(JSContext* aCx, nsIGlobalObject* global, BlobCallback& aCallback,
56               const nsAString& aType, JS::Handle<JS::Value> aParams,
57               bool aUsePlaceholder, ErrorResult& aRv);
58 
59   void ToBlob(JSContext* aCx, nsIGlobalObject* aGlobal,
60               EncodeCompleteCallback* aCallback, const nsAString& aType,
61               JS::Handle<JS::Value> aParams, bool aUsePlaceholder,
62               ErrorResult& aRv);
63 
64   virtual already_AddRefed<nsICanvasRenderingContextInternal> CreateContext(
65       CanvasContextType aContextType);
66 
67   already_AddRefed<nsICanvasRenderingContextInternal> CreateContextHelper(
68       CanvasContextType aContextType, layers::LayersBackend aCompositorBackend);
69 
70   virtual nsIntSize GetWidthHeight() = 0;
71 
72   CanvasContextType mCurrentContextType;
73   nsCOMPtr<nsICanvasRenderingContextInternal> mCurrentContext;
74 };
75 
76 }  // namespace dom
77 }  // namespace mozilla
78 
79 #endif  // MOZILLA_DOM_CANVASRENDERINGCONTEXTHELPER_H_
80