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 #include "OffscreenCanvasRenderingContext2D.h"
8 #include "mozilla/CycleCollectedJSRuntime.h"
9 #include "mozilla/dom/OffscreenCanvasRenderingContext2DBinding.h"
10 #include "mozilla/dom/OffscreenCanvas.h"
12 using namespace mozilla
;
14 namespace mozilla::dom
{
16 NS_IMPL_CYCLE_COLLECTION_INHERITED(OffscreenCanvasRenderingContext2D
,
17 CanvasRenderingContext2D
)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(OffscreenCanvasRenderingContext2D
)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_END_INHERITING(CanvasRenderingContext2D
)
23 // Need to use NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED
24 // and dummy trace since we're missing some _SKIPPABLE_ macros without
26 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(
27 OffscreenCanvasRenderingContext2D
, CanvasRenderingContext2D
)
28 NS_IMPL_CYCLE_COLLECTION_TRACE_END
30 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(OffscreenCanvasRenderingContext2D
)
31 return tmp
->HasKnownLiveWrapper();
32 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
34 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(OffscreenCanvasRenderingContext2D
)
35 return tmp
->HasKnownLiveWrapper();
36 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
38 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(OffscreenCanvasRenderingContext2D
)
39 return tmp
->HasKnownLiveWrapper();
40 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
42 NS_IMPL_ADDREF_INHERITED(OffscreenCanvasRenderingContext2D
,
43 CanvasRenderingContext2D
)
44 NS_IMPL_RELEASE_INHERITED(OffscreenCanvasRenderingContext2D
,
45 CanvasRenderingContext2D
)
47 OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D(
48 layers::LayersBackend aCompositorBackend
)
49 : CanvasRenderingContext2D(aCompositorBackend
) {}
51 OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() =
54 JSObject
* OffscreenCanvasRenderingContext2D::WrapObject(
55 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
56 return OffscreenCanvasRenderingContext2D_Binding::Wrap(aCx
, this,
60 nsIGlobalObject
* OffscreenCanvasRenderingContext2D::GetParentObject() const {
61 return mOffscreenCanvas
->GetOwnerGlobal();
64 NS_IMETHODIMP
OffscreenCanvasRenderingContext2D::InitializeWithDrawTarget(
65 nsIDocShell
* aShell
, NotNull
<gfx::DrawTarget
*> aTarget
) {
66 return NS_ERROR_NOT_IMPLEMENTED
;
69 void OffscreenCanvasRenderingContext2D::Commit(ErrorResult
& aRv
) {
70 if (!mOffscreenCanvas
->IsTransferredFromElement()) {
74 mOffscreenCanvas
->CommitFrameToCompositor();
77 void OffscreenCanvasRenderingContext2D::AddZoneWaitingForGC() {
78 JSObject
* wrapper
= GetWrapperPreserveColor();
80 CycleCollectedJSRuntime::Get()->AddZoneWaitingForGC(
81 JS::GetObjectZone(wrapper
));
85 void OffscreenCanvasRenderingContext2D::AddAssociatedMemory() {
86 JSObject
* wrapper
= GetWrapperMaybeDead();
88 JS::AddAssociatedMemory(wrapper
, BindingJSObjectMallocBytes(this),
89 JS::MemoryUse::DOMBinding
);
93 void OffscreenCanvasRenderingContext2D::RemoveAssociatedMemory() {
94 JSObject
* wrapper
= GetWrapperMaybeDead();
96 JS::RemoveAssociatedMemory(wrapper
, BindingJSObjectMallocBytes(this),
97 JS::MemoryUse::DOMBinding
);
101 size_t BindingJSObjectMallocBytes(OffscreenCanvasRenderingContext2D
* aContext
) {
102 gfx::IntSize size
= aContext
->GetSize();
104 // TODO: Bug 1552137: No memory will be allocated if either dimension is
105 // greater than gfxPrefs::gfx_canvas_max_size(). We should check this here
108 CheckedInt
<uint32_t> bytes
=
109 CheckedInt
<uint32_t>(size
.width
) * size
.height
* 4;
110 if (!bytes
.isValid()) {
114 return bytes
.value();
117 } // namespace mozilla::dom