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/. */
6 
7 #ifndef MOZILLA_DOM_OFFSCREENCANVASRENDERINGCONTEXT2D_H_
8 #define MOZILLA_DOM_OFFSCREENCANVASRENDERINGCONTEXT2D_H_
9 
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/dom/CanvasRenderingContext2D.h"
12 
13 struct JSContext;
14 class nsIGlobalObject;
15 
16 namespace mozilla::dom {
17 class OffscreenCanvas;
18 class OffscreenCanvasShutdownObserver;
19 class WeakWorkerRef;
20 
21 class OffscreenCanvasRenderingContext2D final
22     : public CanvasRenderingContext2D {
23  public:
24   // nsISupports interface + CC
25   NS_DECL_ISUPPORTS_INHERITED
26   NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED(
27       OffscreenCanvasRenderingContext2D, CanvasRenderingContext2D)
28 
29   explicit OffscreenCanvasRenderingContext2D(
30       layers::LayersBackend aCompositorBackend);
31 
32   nsIGlobalObject* GetParentObject() const;
33 
34   JSObject* WrapObject(JSContext* aCx,
35                        JS::Handle<JSObject*> aGivenProto) override;
36 
Canvas()37   OffscreenCanvas* Canvas() { return mOffscreenCanvas; }
Canvas()38   const OffscreenCanvas* Canvas() const { return mOffscreenCanvas; }
39 
40   void Commit(ErrorResult& aRv);
41 
42   void OnShutdown() override;
43 
44   NS_IMETHOD InitializeWithDrawTarget(
45       nsIDocShell* aShell, NotNull<gfx::DrawTarget*> aTarget) override;
46 
47  private:
48   void AddShutdownObserver() override;
49   void RemoveShutdownObserver() override;
AlreadyShutDown()50   bool AlreadyShutDown() const override {
51     return !mOffscreenShutdownObserver &&
52            CanvasRenderingContext2D::AlreadyShutDown();
53   }
54 
55   void AddZoneWaitingForGC() override;
56   void AddAssociatedMemory() override;
57   void RemoveAssociatedMemory() override;
58 
59   ~OffscreenCanvasRenderingContext2D() override;
60 
61   RefPtr<OffscreenCanvasShutdownObserver> mOffscreenShutdownObserver;
62   RefPtr<WeakWorkerRef> mWorkerRef;
63 };
64 
65 size_t BindingJSObjectMallocBytes(OffscreenCanvasRenderingContext2D* aContext);
66 
67 }  // namespace mozilla::dom
68 
69 #endif  // MOZILLA_DOM_OFFSCREENCANVASRENDERINGCONTEXT2D_H_
70