Title: [292742] trunk/Source/WebCore/PAL
Revision
292742
Author
[email protected]
Date
2022-04-11 17:07:17 -0700 (Mon, 11 Apr 2022)

Log Message

[WebGPU] Implement correct ownership for WGPUQueues
https://bugs.webkit.org/show_bug.cgi?id=239050

Reviewed by Kimmo Kinnunen.

WGPUQueues and WGPUDevices have a somewhat interesting relationship: neither is
reference-counted, and the WGPUDevice owns its WGPUQueue. This means that, if client
code wants to extend the lifetime of a WGPUQueue, it has to do this by extending the
lifetime of its owning WGPUDevice. Both objects are _javascript_ objects, so there has to
be some mechanism for a queue to extend the lifetime of its owning device. And yet, a
device owns a queue, so a queue can't have a queue have a Ref<> to its owning device,
because that would be a circular depndency.

Here's the old ownership graph:

      _javascript_
     ------------------------------------------------
          |                                     |
          |                                     |
          V                                     |
PAL::WebGPU::DeviceImpl                         |
          |            \                        |
          |             -----                   |
          |                   \                 V
          |                    -----> PAL::WebGPU::QueueImpl
          |                                     |
          |                                     |
          V                                     |
      WGPUDevice                                |
                \                               |
                 -------------                  |
                              \                 V
                               ------------> WGPUQueue

You can see that there's a problem here: WGPUQueue has 2 owners, but it's not a
reference-counted object.

The solution is to add a new node into the ownership graph, like this:

      _javascript_
     ------------------------------------------------
          |                                     |
          |                                     |
          V                                     |
PAL::WebGPU::DeviceImpl                         |
           \           \                        |
            \           -----                   |
             \                \                 V
              \                 -----> PAL::WebGPU::QueueImpl
               \                              /
                \                            /
                 \                          /
                  V                        V
                  PAL::WebGPU::DeviceHolder
                  /
                 /
                /
               /
              V
      WGPUDevice
                \
                 -------------
                              \
                               ------------> WGPUQueue

This way, both WGPUDevice and WGPUQueue have a single owner, there are no cycles,
and there is a path from PAL::WebGPU::QueueImpl to WGPUDevice.

* PAL.xcodeproj/project.pbxproj:
* pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.cpp: Added.
(PAL::WebGPU::DeviceHolderImpl::DeviceHolderImpl):
(PAL::WebGPU::DeviceHolderImpl::~DeviceHolderImpl):
* pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.h: Copied from Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h.
* pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp:
(PAL::WebGPU::DeviceImpl::DeviceImpl):
(PAL::WebGPU::DeviceImpl::destroy):
(PAL::WebGPU::DeviceImpl::createBuffer):
(PAL::WebGPU::DeviceImpl::createTexture):
(PAL::WebGPU::DeviceImpl::createSampler):
(PAL::WebGPU::DeviceImpl::createBindGroupLayout):
(PAL::WebGPU::DeviceImpl::createPipelineLayout):
(PAL::WebGPU::DeviceImpl::createBindGroup):
(PAL::WebGPU::DeviceImpl::createShaderModule):
(PAL::WebGPU::DeviceImpl::createComputePipeline):
(PAL::WebGPU::DeviceImpl::createRenderPipeline):
(PAL::WebGPU::DeviceImpl::createComputePipelineAsync):
(PAL::WebGPU::DeviceImpl::createRenderPipelineAsync):
(PAL::WebGPU::DeviceImpl::createCommandEncoder):
(PAL::WebGPU::DeviceImpl::createRenderBundleEncoder):
(PAL::WebGPU::DeviceImpl::createQuerySet):
(PAL::WebGPU::DeviceImpl::pushErrorScope):
(PAL::WebGPU::DeviceImpl::popErrorScope):
(PAL::WebGPU::DeviceImpl::setLabelInternal):
(PAL::WebGPU::DeviceImpl::~DeviceImpl): Deleted.
* pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.h:
* pal/graphics/WebGPU/Impl/WebGPUQueueImpl.cpp:
(PAL::WebGPU::QueueImpl::QueueImpl):
(PAL::WebGPU::QueueImpl::submit):
(PAL::WebGPU::QueueImpl::onSubmittedWorkDone):
(PAL::WebGPU::QueueImpl::writeBuffer):
(PAL::WebGPU::QueueImpl::writeTexture):
(PAL::WebGPU::QueueImpl::setLabelInternal):
(PAL::WebGPU::QueueImpl::~QueueImpl): Deleted.
* pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/PAL/ChangeLog (292741 => 292742)


--- trunk/Source/WebCore/PAL/ChangeLog	2022-04-12 00:04:35 UTC (rev 292741)
+++ trunk/Source/WebCore/PAL/ChangeLog	2022-04-12 00:07:17 UTC (rev 292742)
@@ -1,5 +1,112 @@
 2022-04-11  Myles C. Maxfield  <[email protected]>
 
+        [WebGPU] Implement correct ownership for WGPUQueues
+        https://bugs.webkit.org/show_bug.cgi?id=239050
+
+        Reviewed by Kimmo Kinnunen.
+
+        WGPUQueues and WGPUDevices have a somewhat interesting relationship: neither is
+        reference-counted, and the WGPUDevice owns its WGPUQueue. This means that, if client
+        code wants to extend the lifetime of a WGPUQueue, it has to do this by extending the
+        lifetime of its owning WGPUDevice. Both objects are _javascript_ objects, so there has to
+        be some mechanism for a queue to extend the lifetime of its owning device. And yet, a
+        device owns a queue, so a queue can't have a queue have a Ref<> to its owning device,
+        because that would be a circular depndency.
+
+        Here's the old ownership graph:
+
+              _javascript_
+             ------------------------------------------------
+                  |                                     |
+                  |                                     |
+                  V                                     |
+        PAL::WebGPU::DeviceImpl                         |
+                  |            \                        |
+                  |             -----                   |
+                  |                   \                 V
+                  |                    -----> PAL::WebGPU::QueueImpl
+                  |                                     |
+                  |                                     |
+                  V                                     |
+              WGPUDevice                                |
+                        \                               |
+                         -------------                  |
+                                      \                 V
+                                       ------------> WGPUQueue
+
+        You can see that there's a problem here: WGPUQueue has 2 owners, but it's not a
+        reference-counted object.
+
+        The solution is to add a new node into the ownership graph, like this:
+
+              _javascript_
+             ------------------------------------------------
+                  |                                     |
+                  |                                     |
+                  V                                     |
+        PAL::WebGPU::DeviceImpl                         |
+                   \           \                        |
+                    \           -----                   |
+                     \                \                 V
+                      \                 -----> PAL::WebGPU::QueueImpl
+                       \                              / 
+                        \                            /
+                         \                          /
+                          V                        V 
+                          PAL::WebGPU::DeviceHolder
+                          /
+                         /
+                        /
+                       /
+                      V
+              WGPUDevice
+                        \
+                         -------------
+                                      \
+                                       ------------> WGPUQueue
+
+        This way, both WGPUDevice and WGPUQueue have a single owner, there are no cycles,
+        and there is a path from PAL::WebGPU::QueueImpl to WGPUDevice.
+
+        * PAL.xcodeproj/project.pbxproj:
+        * pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.cpp: Added.
+        (PAL::WebGPU::DeviceHolderImpl::DeviceHolderImpl):
+        (PAL::WebGPU::DeviceHolderImpl::~DeviceHolderImpl):
+        * pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.h: Copied from Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h.
+        * pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp:
+        (PAL::WebGPU::DeviceImpl::DeviceImpl):
+        (PAL::WebGPU::DeviceImpl::destroy):
+        (PAL::WebGPU::DeviceImpl::createBuffer):
+        (PAL::WebGPU::DeviceImpl::createTexture):
+        (PAL::WebGPU::DeviceImpl::createSampler):
+        (PAL::WebGPU::DeviceImpl::createBindGroupLayout):
+        (PAL::WebGPU::DeviceImpl::createPipelineLayout):
+        (PAL::WebGPU::DeviceImpl::createBindGroup):
+        (PAL::WebGPU::DeviceImpl::createShaderModule):
+        (PAL::WebGPU::DeviceImpl::createComputePipeline):
+        (PAL::WebGPU::DeviceImpl::createRenderPipeline):
+        (PAL::WebGPU::DeviceImpl::createComputePipelineAsync):
+        (PAL::WebGPU::DeviceImpl::createRenderPipelineAsync):
+        (PAL::WebGPU::DeviceImpl::createCommandEncoder):
+        (PAL::WebGPU::DeviceImpl::createRenderBundleEncoder):
+        (PAL::WebGPU::DeviceImpl::createQuerySet):
+        (PAL::WebGPU::DeviceImpl::pushErrorScope):
+        (PAL::WebGPU::DeviceImpl::popErrorScope):
+        (PAL::WebGPU::DeviceImpl::setLabelInternal):
+        (PAL::WebGPU::DeviceImpl::~DeviceImpl): Deleted.
+        * pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.h:
+        * pal/graphics/WebGPU/Impl/WebGPUQueueImpl.cpp:
+        (PAL::WebGPU::QueueImpl::QueueImpl):
+        (PAL::WebGPU::QueueImpl::submit):
+        (PAL::WebGPU::QueueImpl::onSubmittedWorkDone):
+        (PAL::WebGPU::QueueImpl::writeBuffer):
+        (PAL::WebGPU::QueueImpl::writeTexture):
+        (PAL::WebGPU::QueueImpl::setLabelInternal):
+        (PAL::WebGPU::QueueImpl::~QueueImpl): Deleted.
+        * pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h:
+
+2022-04-11  Myles C. Maxfield  <[email protected]>
+
         [WebGPU] Hook up device.queue to the IDL
         https://bugs.webkit.org/show_bug.cgi?id=239043
 

Modified: trunk/Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj (292741 => 292742)


--- trunk/Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj	2022-04-12 00:04:35 UTC (rev 292741)
+++ trunk/Source/WebCore/PAL/PAL.xcodeproj/project.pbxproj	2022-04-12 00:07:17 UTC (rev 292742)
@@ -60,6 +60,8 @@
 		1C5C57E92757318E003B540D /* TextEncodingRegistryMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1C5C57E82757318D003B540D /* TextEncodingRegistryMac.mm */; };
 		1C77C8C925D7972000635E0C /* CoreTextSoftLink.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C77C8C725D7972000635E0C /* CoreTextSoftLink.cpp */; };
 		1C77C8CE25D7A4A300635E0C /* OTSVGTable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C77C8CD25D7A4A300635E0C /* OTSVGTable.cpp */; };
+		1CB709FC28034EDF00A3A637 /* WebGPUDeviceHolderImpl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1CB709FA28034EDF00A3A637 /* WebGPUDeviceHolderImpl.cpp */; };
+		1CB709FD28034EDF00A3A637 /* WebGPUDeviceHolderImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 1CB709FB28034EDF00A3A637 /* WebGPUDeviceHolderImpl.h */; };
 		1D2B413425F05E3500A3F70A /* ClockGeneric.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1D2B413225F05E3400A3F70A /* ClockGeneric.cpp */; };
 		293EE4A824154F8F0047493D /* AccessibilitySupportSoftLink.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 293EE4A624154F8F0047493D /* AccessibilitySupportSoftLink.cpp */; };
 		2E1342CD215AA10A007199D2 /* UIKitSoftLink.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2E1342CB215AA10A007199D2 /* UIKitSoftLink.mm */; };
@@ -699,6 +701,8 @@
 		1C77C8C825D7972000635E0C /* CoreTextSoftLink.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CoreTextSoftLink.h; sourceTree = "<group>"; };
 		1C77C8CB25D79B6C00635E0C /* OTSVGTable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OTSVGTable.h; sourceTree = "<group>"; };
 		1C77C8CD25D7A4A300635E0C /* OTSVGTable.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = OTSVGTable.cpp; sourceTree = "<group>"; };
+		1CB709FA28034EDF00A3A637 /* WebGPUDeviceHolderImpl.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WebGPUDeviceHolderImpl.cpp; sourceTree = "<group>"; };
+		1CB709FB28034EDF00A3A637 /* WebGPUDeviceHolderImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebGPUDeviceHolderImpl.h; sourceTree = "<group>"; };
 		1CC3ACE722BD7EB800F360F0 /* MetalSPI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MetalSPI.h; sourceTree = "<group>"; };
 		1CC5E4132737492C006F6FF4 /* WebGPUStorageTextureBindingLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebGPUStorageTextureBindingLayout.h; sourceTree = "<group>"; };
 		1CC5E4142737492C006F6FF4 /* WebGPUTextureViewDescriptor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebGPUTextureViewDescriptor.h; sourceTree = "<group>"; };
@@ -1265,6 +1269,8 @@
 				1C19E6AF273F4245004B17B0 /* WebGPUComputePipelineImpl.h */,
 				1C19E6B4273F85AF004B17B0 /* WebGPUConvertToBackingContext.cpp */,
 				1C19E6B6273F85BC004B17B0 /* WebGPUConvertToBackingContext.h */,
+				1CB709FA28034EDF00A3A637 /* WebGPUDeviceHolderImpl.cpp */,
+				1CB709FB28034EDF00A3A637 /* WebGPUDeviceHolderImpl.h */,
 				1C19E666273F3FFA004B17B0 /* WebGPUDeviceImpl.cpp */,
 				1C19E6AD273F4245004B17B0 /* WebGPUDeviceImpl.h */,
 				1C36C52C2743011A006DA4C1 /* WebGPUDowncastConvertToBackingContext.cpp */,
@@ -1865,6 +1871,7 @@
 				DD20DD6727BC90D70093D175 /* WebGPUDepthStencilState.h in Headers */,
 				DD20DD6827BC90D70093D175 /* WebGPUDevice.h in Headers */,
 				DD20DD6927BC90D70093D175 /* WebGPUDeviceDescriptor.h in Headers */,
+				1CB709FD28034EDF00A3A637 /* WebGPUDeviceHolderImpl.h in Headers */,
 				DD20DD3127BC90D60093D175 /* WebGPUDeviceImpl.h in Headers */,
 				DD20DD6A27BC90D70093D175 /* WebGPUDeviceLostInfo.h in Headers */,
 				DD20DD6B27BC90D70093D175 /* WebGPUDeviceLostReason.h in Headers */,
@@ -2133,6 +2140,7 @@
 				1C19E660273F3FE2004B17B0 /* WebGPUComputePassEncoderImpl.cpp in Sources */,
 				1C19E664273F3FEE004B17B0 /* WebGPUComputePipelineImpl.cpp in Sources */,
 				1C19E6B5273F85AF004B17B0 /* WebGPUConvertToBackingContext.cpp in Sources */,
+				1CB709FC28034EDF00A3A637 /* WebGPUDeviceHolderImpl.cpp in Sources */,
 				1C19E668273F3FFA004B17B0 /* WebGPUDeviceImpl.cpp in Sources */,
 				1C36C52E2743011A006DA4C1 /* WebGPUDowncastConvertToBackingContext.cpp in Sources */,
 				1C19E6BA27405E32004B17B0 /* WebGPUExternalTextureImpl.cpp in Sources */,

Added: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.cpp (0 => 292742)


--- trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.cpp	                        (rev 0)
+++ trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.cpp	2022-04-12 00:07:17 UTC (rev 292742)
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WebGPUDeviceHolderImpl.h"
+
+#if HAVE(WEBGPU_IMPLEMENTATION)
+
+#include <WebGPU/WebGPUExt.h>
+
+namespace PAL::WebGPU {
+
+DeviceHolderImpl::DeviceHolderImpl(WGPUDevice device)
+    : m_device(device)
+{
+}
+
+DeviceHolderImpl::~DeviceHolderImpl()
+{
+    wgpuDeviceRelease(m_device);
+}
+
+} // namespace PAL::WebGPU
+
+#endif // HAVE(WEBGPU_IMPLEMENTATION)
Property changes on: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.cpp
___________________________________________________________________

Added: svn:eol-style

+native \ No newline at end of property

Copied: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.h (from rev 292741, trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h) (0 => 292742)


--- trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.h	                        (rev 0)
+++ trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceHolderImpl.h	2022-04-12 00:07:17 UTC (rev 292742)
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if HAVE(WEBGPU_IMPLEMENTATION)
+
+#include "WebGPUTextureView.h"
+#include <WebGPU/WebGPU.h>
+#include <wtf/Ref.h>
+#include <wtf/RefCounted.h>
+
+namespace PAL::WebGPU {
+
+class DeviceHolderImpl final : public RefCounted<DeviceHolderImpl> {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    static Ref<DeviceHolderImpl> create(WGPUDevice device)
+    {
+        return adoptRef(*new DeviceHolderImpl(device));
+    }
+
+    ~DeviceHolderImpl();
+
+    WGPUDevice backingDevice() { return m_device; }
+    WGPUQueue backingQueue() { return wgpuDeviceGetQueue(m_device); }
+
+private:
+    DeviceHolderImpl(WGPUDevice);
+
+    DeviceHolderImpl(const DeviceHolderImpl&) = delete;
+    DeviceHolderImpl(DeviceHolderImpl&&) = delete;
+    DeviceHolderImpl& operator=(const DeviceHolderImpl&) = delete;
+    DeviceHolderImpl& operator=(DeviceHolderImpl&&) = delete;
+
+    WGPUDevice m_device { nullptr };
+};
+
+} // namespace PAL::WebGPU
+
+#endif // HAVE(WEBGPU_IMPLEMENTATION)

Modified: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp (292741 => 292742)


--- trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp	2022-04-12 00:04:35 UTC (rev 292741)
+++ trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.cpp	2022-04-12 00:07:17 UTC (rev 292742)
@@ -64,16 +64,13 @@
 
 DeviceImpl::DeviceImpl(WGPUDevice device, Ref<SupportedFeatures>&& features, Ref<SupportedLimits>&& limits, ConvertToBackingContext& convertToBackingContext)
     : Device(WTFMove(features), WTFMove(limits))
-    , m_backing(device)
+    , m_deviceHolder(DeviceHolderImpl::create(device))
     , m_convertToBackingContext(convertToBackingContext)
-    , m_queue(QueueImpl::create(wgpuDeviceGetQueue(device), convertToBackingContext))
+    , m_queue(QueueImpl::create(m_deviceHolder.copyRef(), convertToBackingContext))
 {
 }
 
-DeviceImpl::~DeviceImpl()
-{
-    wgpuDeviceRelease(m_backing);
-}
+DeviceImpl::~DeviceImpl() = default;
 
 Queue& DeviceImpl::queue()
 {
@@ -82,7 +79,7 @@
 
 void DeviceImpl::destroy()
 {
-    wgpuDeviceDestroy(m_backing);
+    wgpuDeviceDestroy(backing());
 }
 
 Ref<Buffer> DeviceImpl::createBuffer(const BufferDescriptor& descriptor)
@@ -97,7 +94,7 @@
         descriptor.mappedAtCreation,
     };
 
-    return BufferImpl::create(wgpuDeviceCreateBuffer(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return BufferImpl::create(wgpuDeviceCreateBuffer(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<Texture> DeviceImpl::createTexture(const TextureDescriptor& descriptor)
@@ -130,7 +127,7 @@
         descriptor.sampleCount,
     };
 
-    return TextureImpl::create(wgpuDeviceCreateTexture(m_backing, &backingDescriptor), descriptor.format, descriptor.dimension, m_convertToBackingContext);
+    return TextureImpl::create(wgpuDeviceCreateTexture(backing(), &backingDescriptor), descriptor.format, descriptor.dimension, m_convertToBackingContext);
 }
 
 Ref<Sampler> DeviceImpl::createSampler(const SamplerDescriptor& descriptor)
@@ -152,7 +149,7 @@
         descriptor.maxAnisotropy,
     };
 
-    return SamplerImpl::create(wgpuDeviceCreateSampler(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return SamplerImpl::create(wgpuDeviceCreateSampler(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<ExternalTexture> DeviceImpl::importExternalTexture(const ExternalTextureDescriptor&)
@@ -197,7 +194,7 @@
         backingEntries.data(),
     };
 
-    return BindGroupLayoutImpl::create(wgpuDeviceCreateBindGroupLayout(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return BindGroupLayoutImpl::create(wgpuDeviceCreateBindGroupLayout(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<PipelineLayout> DeviceImpl::createPipelineLayout(const PipelineLayoutDescriptor& descriptor)
@@ -215,7 +212,7 @@
         backingBindGroupLayouts.data(),
     };
 
-    return PipelineLayoutImpl::create(wgpuDeviceCreatePipelineLayout(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return PipelineLayoutImpl::create(wgpuDeviceCreatePipelineLayout(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<BindGroup> DeviceImpl::createBindGroup(const BindGroupDescriptor& descriptor)
@@ -242,7 +239,7 @@
         backingEntries.data(),
     };
 
-    return BindGroupImpl::create(wgpuDeviceCreateBindGroup(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return BindGroupImpl::create(wgpuDeviceCreateBindGroup(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<ShaderModule> DeviceImpl::createShaderModule(const ShaderModuleDescriptor& descriptor)
@@ -289,7 +286,7 @@
         label.data(),
     };
 
-    return ShaderModuleImpl::create(wgpuDeviceCreateShaderModule(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return ShaderModuleImpl::create(wgpuDeviceCreateShaderModule(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 template <typename T>
@@ -332,7 +329,7 @@
 Ref<ComputePipeline> DeviceImpl::createComputePipeline(const ComputePipelineDescriptor& descriptor)
 {
     return convertToBacking(descriptor, m_convertToBackingContext, [this] (const WGPUComputePipelineDescriptor& backingDescriptor) {
-        return ComputePipelineImpl::create(wgpuDeviceCreateComputePipeline(m_backing, &backingDescriptor), m_convertToBackingContext);
+        return ComputePipelineImpl::create(wgpuDeviceCreateComputePipeline(backing(), &backingDescriptor), m_convertToBackingContext);
     });
 }
 
@@ -512,7 +509,7 @@
 Ref<RenderPipeline> DeviceImpl::createRenderPipeline(const RenderPipelineDescriptor& descriptor)
 {
     return convertToBacking(descriptor, m_convertToBackingContext, [this] (const WGPURenderPipelineDescriptor& backingDescriptor) {
-        return RenderPipelineImpl::create(wgpuDeviceCreateRenderPipeline(m_backing, &backingDescriptor), m_convertToBackingContext);
+        return RenderPipelineImpl::create(wgpuDeviceCreateRenderPipeline(backing(), &backingDescriptor), m_convertToBackingContext);
     });
 }
 
@@ -519,7 +516,7 @@
 void DeviceImpl::createComputePipelineAsync(const ComputePipelineDescriptor& descriptor, CompletionHandler<void(Ref<ComputePipeline>&&)>&& callback)
 {
     convertToBacking(descriptor, m_convertToBackingContext, [this, callback = WTFMove(callback)] (const WGPUComputePipelineDescriptor& backingDescriptor) mutable {
-        wgpuDeviceCreateComputePipelineAsyncWithBlock(m_backing, &backingDescriptor, makeBlockPtr([convertToBackingContext = m_convertToBackingContext.copyRef(), callback = WTFMove(callback)](WGPUCreatePipelineAsyncStatus, WGPUComputePipeline pipeline, const char*) mutable {
+        wgpuDeviceCreateComputePipelineAsyncWithBlock(backing(), &backingDescriptor, makeBlockPtr([convertToBackingContext = m_convertToBackingContext.copyRef(), callback = WTFMove(callback)](WGPUCreatePipelineAsyncStatus, WGPUComputePipeline pipeline, const char*) mutable {
             callback(ComputePipelineImpl::create(pipeline, convertToBackingContext));
         }).get());
     });
@@ -528,7 +525,7 @@
 void DeviceImpl::createRenderPipelineAsync(const RenderPipelineDescriptor& descriptor, CompletionHandler<void(Ref<RenderPipeline>&&)>&& callback)
 {
     convertToBacking(descriptor, m_convertToBackingContext, [this, callback = WTFMove(callback)] (const WGPURenderPipelineDescriptor& backingDescriptor) mutable {
-        wgpuDeviceCreateRenderPipelineAsyncWithBlock(m_backing, &backingDescriptor, makeBlockPtr([convertToBackingContext = m_convertToBackingContext.copyRef(), callback = WTFMove(callback)](WGPUCreatePipelineAsyncStatus, WGPURenderPipeline pipeline, const char*) mutable {
+        wgpuDeviceCreateRenderPipelineAsyncWithBlock(backing(), &backingDescriptor, makeBlockPtr([convertToBackingContext = m_convertToBackingContext.copyRef(), callback = WTFMove(callback)](WGPUCreatePipelineAsyncStatus, WGPURenderPipeline pipeline, const char*) mutable {
             callback(RenderPipelineImpl::create(pipeline, convertToBackingContext));
         }).get());
     });
@@ -543,7 +540,7 @@
         label.data(),
     };
 
-    return CommandEncoderImpl::create(wgpuDeviceCreateCommandEncoder(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return CommandEncoderImpl::create(wgpuDeviceCreateCommandEncoder(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<RenderBundleEncoder> DeviceImpl::createRenderBundleEncoder(const RenderBundleEncoderDescriptor& descriptor)
@@ -565,7 +562,7 @@
         descriptor.stencilReadOnly,
     };
 
-    return RenderBundleEncoderImpl::create(wgpuDeviceCreateRenderBundleEncoder(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return RenderBundleEncoderImpl::create(wgpuDeviceCreateRenderBundleEncoder(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 Ref<QuerySet> DeviceImpl::createQuerySet(const QuerySetDescriptor& descriptor)
@@ -581,17 +578,17 @@
         0,
     };
 
-    return QuerySetImpl::create(wgpuDeviceCreateQuerySet(m_backing, &backingDescriptor), m_convertToBackingContext);
+    return QuerySetImpl::create(wgpuDeviceCreateQuerySet(backing(), &backingDescriptor), m_convertToBackingContext);
 }
 
 void DeviceImpl::pushErrorScope(ErrorFilter errorFilter)
 {
-    wgpuDevicePushErrorScope(m_backing, m_convertToBackingContext->convertToBacking(errorFilter));
+    wgpuDevicePushErrorScope(backing(), m_convertToBackingContext->convertToBacking(errorFilter));
 }
 
 void DeviceImpl::popErrorScope(CompletionHandler<void(std::optional<Error>&&)>&& callback)
 {
-    wgpuDevicePopErrorScopeWithBlock(m_backing, makeBlockPtr([callback = WTFMove(callback)](WGPUErrorType errorType, const char* message) mutable {
+    wgpuDevicePopErrorScopeWithBlock(backing(), makeBlockPtr([callback = WTFMove(callback)](WGPUErrorType errorType, const char* message) mutable {
         std::optional<Error> error;
         switch (errorType) {
         case WGPUErrorType_NoError:
@@ -617,7 +614,7 @@
 
 void DeviceImpl::setLabelInternal(const String& label)
 {
-    wgpuDeviceSetLabel(m_backing, label.utf8().data());
+    wgpuDeviceSetLabel(backing(), label.utf8().data());
 }
 
 } // namespace PAL::WebGPU

Modified: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.h (292741 => 292742)


--- trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.h	2022-04-12 00:04:35 UTC (rev 292741)
+++ trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUDeviceImpl.h	2022-04-12 00:07:17 UTC (rev 292742)
@@ -28,6 +28,7 @@
 #if HAVE(WEBGPU_IMPLEMENTATION)
 
 #include "WebGPUDevice.h"
+#include "WebGPUDeviceHolderImpl.h"
 #include "WebGPUQueueImpl.h"
 #include <WebGPU/WebGPU.h>
 #include <wtf/Deque.h>
@@ -56,7 +57,7 @@
     DeviceImpl& operator=(const DeviceImpl&) = delete;
     DeviceImpl& operator=(DeviceImpl&&) = delete;
 
-    WGPUDevice backing() const { return m_backing; }
+    WGPUDevice backing() const { return m_deviceHolder->backingDevice(); }
 
     Queue& queue() final;
 
@@ -87,7 +88,7 @@
 
     void setLabelInternal(const String&) final;
 
-    WGPUDevice m_backing { nullptr };
+    Ref<DeviceHolderImpl> m_deviceHolder;
     Ref<ConvertToBackingContext> m_convertToBackingContext;
     Ref<QueueImpl> m_queue;
 };

Modified: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.cpp (292741 => 292742)


--- trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.cpp	2022-04-12 00:04:35 UTC (rev 292741)
+++ trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.cpp	2022-04-12 00:07:17 UTC (rev 292742)
@@ -37,16 +37,13 @@
 
 namespace PAL::WebGPU {
 
-QueueImpl::QueueImpl(WGPUQueue queue, ConvertToBackingContext& convertToBackingContext)
-    : m_backing(queue)
+QueueImpl::QueueImpl(Ref<DeviceHolderImpl>&& deviceHolder, ConvertToBackingContext& convertToBackingContext)
+    : m_deviceHolder(WTFMove(deviceHolder))
     , m_convertToBackingContext(convertToBackingContext)
 {
 }
 
-QueueImpl::~QueueImpl()
-{
-    wgpuQueueRelease(m_backing);
-}
+QueueImpl::~QueueImpl() = default;
 
 void QueueImpl::submit(Vector<std::reference_wrapper<CommandBuffer>>&& commandBuffers)
 {
@@ -54,12 +51,12 @@
         return m_convertToBackingContext->convertToBacking(commandBuffer);
     });
 
-    wgpuQueueSubmit(m_backing, backingCommandBuffers.size(), backingCommandBuffers.data());
+    wgpuQueueSubmit(backing(), backingCommandBuffers.size(), backingCommandBuffers.data());
 }
 
 void QueueImpl::onSubmittedWorkDone(CompletionHandler<void()>&& callback)
 {
-    wgpuQueueOnSubmittedWorkDoneWithBlock(m_backing, m_signalValue, makeBlockPtr([callback = WTFMove(callback)](WGPUQueueWorkDoneStatus) mutable {
+    wgpuQueueOnSubmittedWorkDoneWithBlock(backing(), m_signalValue, makeBlockPtr([callback = WTFMove(callback)](WGPUQueueWorkDoneStatus) mutable {
         callback();
     }).get());
 
@@ -75,7 +72,7 @@
     std::optional<Size64> size)
 {
     // FIXME: Use checked arithmetic and check the cast
-    wgpuQueueWriteBuffer(m_backing, m_convertToBackingContext->convertToBacking(buffer), bufferOffset, static_cast<const uint8_t*>(source) + dataOffset, static_cast<size_t>(size.value_or(byteLength - dataOffset)));
+    wgpuQueueWriteBuffer(backing(), m_convertToBackingContext->convertToBacking(buffer), bufferOffset, static_cast<const uint8_t*>(source) + dataOffset, static_cast<size_t>(size.value_or(byteLength - dataOffset)));
 }
 
 void QueueImpl::writeTexture(
@@ -102,7 +99,7 @@
 
     WGPUExtent3D backingSize = m_convertToBackingContext->convertToBacking(size);
 
-    wgpuQueueWriteTexture(m_backing, &backingDestination, source, byteLength, &backingDataLayout, &backingSize);
+    wgpuQueueWriteTexture(backing(), &backingDestination, source, byteLength, &backingDataLayout, &backingSize);
 }
 
 void QueueImpl::copyExternalImageToTexture(
@@ -117,7 +114,7 @@
 
 void QueueImpl::setLabelInternal(const String& label)
 {
-    wgpuQueueSetLabel(m_backing, label.utf8().data());
+    wgpuQueueSetLabel(backing(), label.utf8().data());
 }
 
 } // namespace PAL::WebGPU

Modified: trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h (292741 => 292742)


--- trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h	2022-04-12 00:04:35 UTC (rev 292741)
+++ trunk/Source/WebCore/PAL/pal/graphics/WebGPU/Impl/WebGPUQueueImpl.h	2022-04-12 00:07:17 UTC (rev 292742)
@@ -27,6 +27,7 @@
 
 #if HAVE(WEBGPU_IMPLEMENTATION)
 
+#include "WebGPUDeviceHolderImpl.h"
 #include "WebGPUQueue.h"
 #include <WebGPU/WebGPU.h>
 #include <wtf/Deque.h>
@@ -38,9 +39,9 @@
 class QueueImpl final : public Queue {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static Ref<QueueImpl> create(WGPUQueue queue, ConvertToBackingContext& convertToBackingContext)
+    static Ref<QueueImpl> create(Ref<DeviceHolderImpl>&& deviceHolder, ConvertToBackingContext& convertToBackingContext)
     {
-        return adoptRef(*new QueueImpl(queue, convertToBackingContext));
+        return adoptRef(*new QueueImpl(WTFMove(deviceHolder), convertToBackingContext));
     }
 
     virtual ~QueueImpl();
@@ -48,7 +49,7 @@
 private:
     friend class DowncastConvertToBackingContext;
 
-    QueueImpl(WGPUQueue, ConvertToBackingContext&);
+    QueueImpl(Ref<DeviceHolderImpl>&&, ConvertToBackingContext&);
 
     QueueImpl(const QueueImpl&) = delete;
     QueueImpl(QueueImpl&&) = delete;
@@ -55,7 +56,7 @@
     QueueImpl& operator=(const QueueImpl&) = delete;
     QueueImpl& operator=(QueueImpl&&) = delete;
 
-    WGPUQueue backing() const { return m_backing; }
+    WGPUQueue backing() const { return m_deviceHolder->backingQueue(); }
 
     void submit(Vector<std::reference_wrapper<CommandBuffer>>&&) final;
 
@@ -85,7 +86,7 @@
 
     uint64_t m_signalValue { 1 };
 
-    WGPUQueue m_backing { nullptr };
+    Ref<DeviceHolderImpl> m_deviceHolder;
     Ref<ConvertToBackingContext> m_convertToBackingContext;
 };
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to