OffscreenCanvasRenderingContext2D.cpp (4146B)
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 #include "OffscreenCanvasRenderingContext2D.h" 8 9 #include "mozilla/CycleCollectedJSRuntime.h" 10 #include "mozilla/dom/OffscreenCanvas.h" 11 #include "mozilla/dom/OffscreenCanvasRenderingContext2DBinding.h" 12 13 using namespace mozilla; 14 15 namespace mozilla::dom { 16 17 NS_IMPL_CYCLE_COLLECTION_INHERITED(OffscreenCanvasRenderingContext2D, 18 CanvasRenderingContext2D) 19 20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(OffscreenCanvasRenderingContext2D) 21 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 22 NS_INTERFACE_MAP_END_INHERITING(CanvasRenderingContext2D) 23 24 // Need to use NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_INHERITED 25 // and dummy trace since we're missing some _SKIPPABLE_ macros without 26 // SCRIPT_HOLDER. 27 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED( 28 OffscreenCanvasRenderingContext2D, CanvasRenderingContext2D) 29 NS_IMPL_CYCLE_COLLECTION_TRACE_END 30 31 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(OffscreenCanvasRenderingContext2D) 32 return tmp->HasKnownLiveWrapper(); 33 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END 34 35 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(OffscreenCanvasRenderingContext2D) 36 return tmp->HasKnownLiveWrapper(); 37 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END 38 39 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(OffscreenCanvasRenderingContext2D) 40 return tmp->HasKnownLiveWrapper(); 41 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END 42 43 NS_IMPL_ADDREF_INHERITED(OffscreenCanvasRenderingContext2D, 44 CanvasRenderingContext2D) 45 NS_IMPL_RELEASE_INHERITED(OffscreenCanvasRenderingContext2D, 46 CanvasRenderingContext2D) 47 48 OffscreenCanvasRenderingContext2D::OffscreenCanvasRenderingContext2D( 49 layers::LayersBackend aCompositorBackend) 50 : CanvasRenderingContext2D(aCompositorBackend) {} 51 52 OffscreenCanvasRenderingContext2D::~OffscreenCanvasRenderingContext2D() = 53 default; 54 55 JSObject* OffscreenCanvasRenderingContext2D::WrapObject( 56 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 57 return OffscreenCanvasRenderingContext2D_Binding::Wrap(aCx, this, 58 aGivenProto); 59 } 60 61 nsIGlobalObject* OffscreenCanvasRenderingContext2D::GetParentObject() const { 62 return mOffscreenCanvas->GetOwnerGlobal(); 63 } 64 65 NS_IMETHODIMP OffscreenCanvasRenderingContext2D::InitializeWithDrawTarget( 66 nsIDocShell* aShell, NotNull<gfx::DrawTarget*> aTarget) { 67 return NS_ERROR_NOT_IMPLEMENTED; 68 } 69 70 void OffscreenCanvasRenderingContext2D::Commit(ErrorResult& aRv) { 71 if (!mOffscreenCanvas->IsTransferredFromElement()) { 72 return; 73 } 74 75 mOffscreenCanvas->CommitFrameToCompositor(); 76 } 77 78 void OffscreenCanvasRenderingContext2D::AddZoneWaitingForGC() { 79 JSObject* wrapper = GetWrapperPreserveColor(); 80 if (wrapper) { 81 CycleCollectedJSRuntime::Get()->AddZoneWaitingForGC( 82 JS::GetObjectZone(wrapper)); 83 } 84 } 85 86 void OffscreenCanvasRenderingContext2D::AddAssociatedMemory() { 87 JSObject* wrapper = GetWrapperMaybeDead(); 88 if (wrapper) { 89 JS::AddAssociatedMemory(wrapper, BindingJSObjectMallocBytes(this), 90 JS::MemoryUse::DOMBinding); 91 } 92 } 93 94 void OffscreenCanvasRenderingContext2D::RemoveAssociatedMemory() { 95 JSObject* wrapper = GetWrapperMaybeDead(); 96 if (wrapper) { 97 JS::RemoveAssociatedMemory(wrapper, BindingJSObjectMallocBytes(this), 98 JS::MemoryUse::DOMBinding); 99 } 100 } 101 102 size_t BindingJSObjectMallocBytes(OffscreenCanvasRenderingContext2D* aContext) { 103 gfx::IntSize size = aContext->GetSize(); 104 105 // TODO: Bug 1552137: No memory will be allocated if either dimension is 106 // greater than gfxPrefs::gfx_canvas_max_size(). We should check this here 107 // too. 108 109 CheckedInt<uint32_t> bytes = 110 CheckedInt<uint32_t>(size.width) * size.height * 4; 111 if (!bytes.isValid()) { 112 return 0; 113 } 114 115 return bytes.value(); 116 } 117 118 } // namespace mozilla::dom