summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/ObjectModel.h
diff options
context:
space:
mode:
Diffstat (limited to 'dom/webgpu/ObjectModel.h')
-rw-r--r--dom/webgpu/ObjectModel.h97
1 files changed, 97 insertions, 0 deletions
diff --git a/dom/webgpu/ObjectModel.h b/dom/webgpu/ObjectModel.h
new file mode 100644
index 0000000000..f56a6b49bc
--- /dev/null
+++ b/dom/webgpu/ObjectModel.h
@@ -0,0 +1,97 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef GPU_OBJECT_MODEL_H_
+#define GPU_OBJECT_MODEL_H_
+
+#include "nsWrapperCache.h"
+#include "nsString.h"
+
+class nsIGlobalObject;
+
+namespace mozilla {
+namespace webgpu {
+class WebGPUChild;
+
+template <typename T>
+class ChildOf {
+ protected:
+ explicit ChildOf(T* const parent);
+ virtual ~ChildOf();
+
+ RefPtr<T> mParent;
+
+ public:
+ nsIGlobalObject* GetParentObject() const;
+};
+
+class ObjectBase : public nsWrapperCache {
+ private:
+ nsString mLabel;
+
+ protected:
+ virtual ~ObjectBase() = default;
+ // Internal mutability model for WebGPU objects.
+ bool mValid = true;
+
+ public:
+ void GetLabel(nsAString& aValue) const;
+ void SetLabel(const nsAString& aLabel);
+};
+
+} // namespace webgpu
+} // namespace mozilla
+
+#define GPU_DECL_JS_WRAP(T) \
+ JSObject* WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) \
+ override;
+
+#define GPU_DECL_CYCLE_COLLECTION(T) \
+ NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(T) \
+ NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(T)
+
+#define GPU_IMPL_JS_WRAP(T) \
+ JSObject* T::WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto) { \
+ return dom::GPU##T##_Binding::Wrap(cx, this, givenProto); \
+ }
+
+// Note: we don't use `NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE` directly
+// because there is a custom action we need to always do.
+#define GPU_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(T, ...) \
+ NS_IMPL_CYCLE_COLLECTION_CLASS(T) \
+ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(T) \
+ tmp->Cleanup(); \
+ NS_IMPL_CYCLE_COLLECTION_UNLINK(__VA_ARGS__) \
+ NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER \
+ NS_IMPL_CYCLE_COLLECTION_UNLINK_END \
+ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(T) \
+ NS_IMPL_CYCLE_COLLECTION_TRAVERSE(__VA_ARGS__) \
+ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END \
+ NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(T)
+
+#define GPU_IMPL_CYCLE_COLLECTION(T, ...) \
+ NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(T, AddRef) \
+ NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(T, Release) \
+ GPU_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(T, __VA_ARGS__)
+
+template <typename T>
+void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& callback,
+ nsTArray<RefPtr<const T>>& field,
+ const char* name, uint32_t flags) {
+ for (auto& element : field) {
+ CycleCollectionNoteChild(callback, const_cast<T*>(element.get()), name,
+ flags);
+ }
+}
+
+template <typename T>
+void ImplCycleCollectionUnlink(nsTArray<RefPtr<const T>>& field) {
+ for (auto& element : field) {
+ ImplCycleCollectionUnlink(element);
+ }
+ field.Clear();
+}
+
+#endif // GPU_OBJECT_MODEL_H_