ObjectModel.h (3112B)
1 /* -*- Mode: C++; tab-width: 4; 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 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef GPU_OBJECT_MODEL_H_ 7 #define GPU_OBJECT_MODEL_H_ 8 9 #include "mozilla/webgpu/WebGPUTypes.h" 10 #include "mozilla/webgpu/ffi/wgpu.h" 11 #include "nsString.h" 12 #include "nsWrapperCache.h" 13 14 class nsIGlobalObject; 15 16 namespace mozilla::webgpu { 17 class WebGPUChild; 18 19 template <typename T> 20 class ChildOf { 21 protected: 22 explicit ChildOf(T* const parent); 23 virtual ~ChildOf(); 24 25 RefPtr<T> mParent; 26 27 public: 28 nsIGlobalObject* GetParentObject() const; 29 }; 30 31 /// This class is used to interface with the WebGPUChild IPDL actor. 32 /// 33 /// WebGPU DOM objects that have equivalents in wgpu-core need to 34 /// communicate with the parent actor and should inherit from this class. 35 /// 36 /// It provides access to the WebGPUChild, rust Client, object ID, 37 /// and automatically sends a drop message on object destruction. 38 class ObjectBase { 39 protected: 40 virtual ~ObjectBase(); 41 42 public: 43 ObjectBase(WebGPUChild* const aChild, RawId aId, 44 void (*aDropFnPtr)(const struct ffi::WGPUClient* aClient, 45 RawId aId)); 46 47 void GetLabel(nsAString& aValue) const; 48 void SetLabel(const nsAString& aLabel); 49 50 auto CLabel() const { return NS_ConvertUTF16toUTF8(mLabel); } 51 52 WebGPUChild* GetChild() const; 53 ffi::WGPUClient* GetClient() const; 54 RawId GetId() const { return mId; }; 55 56 private: 57 RefPtr<WebGPUChild> mChild; 58 RawId mId; 59 void (*mDropFnPtr)(const struct ffi::WGPUClient* aClient, RawId aId); 60 61 // Object label, initialized from GPUObjectDescriptorBase.label. 62 nsString mLabel; 63 }; 64 65 } // namespace mozilla::webgpu 66 67 #define GPU_DECL_JS_WRAP(T) \ 68 JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) \ 69 override; 70 71 #define GPU_DECL_CYCLE_COLLECTION(T) \ 72 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(T) \ 73 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(T) 74 75 #define GPU_IMPL_JS_WRAP(T) \ 76 JSObject* T::WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) { \ 77 return dom::GPU##T##_Binding::Wrap(cx, this, givenProto); \ 78 } 79 80 #define GPU_IMPL_CYCLE_COLLECTION(T, ...) \ 81 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(T, __VA_ARGS__) 82 83 template <typename T> 84 void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& callback, 85 nsTArray<RefPtr<const T>>& field, 86 const char* name, uint32_t flags) { 87 for (auto& element : field) { 88 CycleCollectionNoteChild(callback, const_cast<T*>(element.get()), name, 89 flags); 90 } 91 } 92 93 template <typename T> 94 void ImplCycleCollectionUnlink(nsTArray<RefPtr<const T>>& field) { 95 for (auto& element : field) { 96 ImplCycleCollectionUnlink(element); 97 } 98 field.Clear(); 99 } 100 101 #endif // GPU_OBJECT_MODEL_H_