[WebGPU] Move 'gpu' API entry point from DOMWindow to Navigator
authorjustin_fan@apple.com <justin_fan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 30 Apr 2019 16:33:35 +0000 (16:33 +0000)
committerjustin_fan@apple.com <justin_fan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 30 Apr 2019 16:33:35 +0000 (16:33 +0000)
https://bugs.webkit.org/show_bug.cgi?id=197348

Reviewed by Myles C. Maxfield.

Source/WebCore:

Latest API provides 'gpu' through Navigator instead of DOMWindow. Replace DOMWindowWebGPU with NavigatorGPU.

Existing tests updated to match. Add test: webgpu-enabled-in-worker.html to ensure workers can access WebGPU.

* CMakeLists.txt:
* DerivedSources.make:
* Modules/webgpu/NavigatorGPU.cpp:
(WebCore::NavigatorGPU::from):
(WebCore::NavigatorGPU::supplementName):
(WebCore::NavigatorGPU::gpu):
(WebCore::NavigatorGPU::gpu const):
* Modules/webgpu/NavigatorGPU.h:
* Modules/webgpu/NavigatorGPU.idl:
* Modules/webgpu/WorkerNavigatorGPU.cpp:
(WebCore::WorkerNavigatorGPU::from):
(WebCore::WorkerNavigatorGPU::supplementName):
(WebCore::WorkerNavigatorGPU::gpu):
(WebCore::WorkerNavigatorGPU::gpu const):
* Modules/webgpu/WorkerNavigatorGPU.h:
* Modules/webgpu/WorkerNavigatorGPU.idl:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

LayoutTests:

WebGPU is now accessed through navigator.gpu instead of window.gpu.
Add webgpu-enabled-in-worker to ensure workers can access WebGPU.

* webgpu/adapter-options.html:
* webgpu/js/create-worker-device.js: Added.
(async.tryCreateDevice):
* webgpu/js/webgpu-functions.js:
(async.getBasicDevice):
* webgpu/queue-creation.html:
* webgpu/webgpu-enabled-in-worker-expected.txt: Added.
* webgpu/webgpu-enabled-in-worker.html: Added.
* webgpu/webgpu-enabled.html:
* webgpu/whlsl.html:

Add navigator.gpu to expectations for navigator-detached-no-crash.
* fast/dom/navigator-detached-no-crash-expected.txt:
* platform/mac-highsierra/fast/dom/navigator-detached-no-crash-expected.txt:
* platform/mac-wk2/fast/dom/navigator-detached-no-crash-expected.txt:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244777 268f45cc-cd09-0410-ab3c-d52691b4dbfc

23 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/dom/navigator-detached-no-crash-expected.txt
LayoutTests/platform/mac-highsierra/fast/dom/navigator-detached-no-crash-expected.txt
LayoutTests/platform/mac-wk2/fast/dom/navigator-detached-no-crash-expected.txt
LayoutTests/webgpu/adapter-options.html
LayoutTests/webgpu/js/create-worker-device.js [new file with mode: 0644]
LayoutTests/webgpu/js/webgpu-functions.js
LayoutTests/webgpu/queue-creation.html
LayoutTests/webgpu/webgpu-enabled-in-worker-expected.txt [new file with mode: 0644]
LayoutTests/webgpu/webgpu-enabled-in-worker.html [new file with mode: 0644]
LayoutTests/webgpu/webgpu-enabled.html
LayoutTests/webgpu/whlsl.html
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/DerivedSources.make
Source/WebCore/Modules/webgpu/NavigatorGPU.cpp [moved from Source/WebCore/Modules/webgpu/DOMWindowWebGPU.cpp with 71% similarity]
Source/WebCore/Modules/webgpu/NavigatorGPU.h [moved from Source/WebCore/Modules/webgpu/DOMWindowWebGPU.h with 83% similarity]
Source/WebCore/Modules/webgpu/NavigatorGPU.idl [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.idl [moved from Source/WebCore/Modules/webgpu/DOMWindowWebGPU.idl with 89% similarity]
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj

index 47a8225..2d4c1a0 100644 (file)
@@ -1,3 +1,29 @@
+2019-04-30  Justin Fan  <justin_fan@apple.com>
+
+        [WebGPU] Move 'gpu' API entry point from DOMWindow to Navigator
+        https://bugs.webkit.org/show_bug.cgi?id=197348
+
+        Reviewed by Myles C. Maxfield.
+
+        WebGPU is now accessed through navigator.gpu instead of window.gpu.
+        Add webgpu-enabled-in-worker to ensure workers can access WebGPU.
+
+        * webgpu/adapter-options.html:
+        * webgpu/js/create-worker-device.js: Added.
+        (async.tryCreateDevice):
+        * webgpu/js/webgpu-functions.js:
+        (async.getBasicDevice):
+        * webgpu/queue-creation.html:
+        * webgpu/webgpu-enabled-in-worker-expected.txt: Added.
+        * webgpu/webgpu-enabled-in-worker.html: Added.
+        * webgpu/webgpu-enabled.html:
+        * webgpu/whlsl.html:
+
+        Add navigator.gpu to expectations for navigator-detached-no-crash.
+        * fast/dom/navigator-detached-no-crash-expected.txt:
+        * platform/mac-highsierra/fast/dom/navigator-detached-no-crash-expected.txt:
+        * platform/mac-wk2/fast/dom/navigator-detached-no-crash-expected.txt:
+
 2019-04-30  Youenn Fablet  <youenn@apple.com>
 
         Update WPT service-worker resource-timing test to use hosts[alt]
index d9f644f..f2f63e8 100644 (file)
@@ -8,6 +8,7 @@ navigator.appVersion is OK
 navigator.cookieEnabled is OK
 navigator.credentials is OK
 navigator.getStorageUpdates() is OK
+navigator.gpu is OK
 navigator.javaEnabled() is OK
 navigator.language is OK
 navigator.languages is OK
@@ -33,6 +34,7 @@ navigator.appVersion is OK
 navigator.cookieEnabled is OK
 navigator.credentials is OK
 navigator.getStorageUpdates() is OK
+navigator.gpu is OK
 navigator.javaEnabled() is OK
 navigator.language is OK
 navigator.languages is OK
index d93add7..7953fa5 100644 (file)
@@ -7,6 +7,7 @@ navigator.appVersion is OK
 navigator.cookieEnabled is OK
 navigator.credentials is OK
 navigator.getStorageUpdates() is OK
+navigator.gpu is OK
 navigator.javaEnabled() is OK
 navigator.language is OK
 navigator.languages is OK
@@ -30,6 +31,7 @@ navigator.appVersion is OK
 navigator.cookieEnabled is OK
 navigator.credentials is OK
 navigator.getStorageUpdates() is OK
+navigator.gpu is OK
 navigator.javaEnabled() is OK
 navigator.language is OK
 navigator.languages is OK
index 6c99ccb..924b329 100644 (file)
@@ -8,6 +8,7 @@ navigator.appVersion is OK
 navigator.cookieEnabled is OK
 navigator.credentials is OK
 navigator.getStorageUpdates() is OK
+navigator.gpu is OK
 navigator.javaEnabled() is OK
 navigator.language is OK
 navigator.languages is OK
@@ -32,6 +33,7 @@ navigator.appVersion is OK
 navigator.cookieEnabled is OK
 navigator.credentials is OK
 navigator.getStorageUpdates() is OK
+navigator.gpu is OK
 navigator.javaEnabled() is OK
 navigator.language is OK
 navigator.languages is OK
index 986cce1..9f5187a 100644 (file)
@@ -6,21 +6,21 @@
 <script src="../resources/testharnessreport.js"></script>
 <script>
 promise_test(async () => {
-    const defaultAdapter = await gpu.requestAdapter();
+    const defaultAdapter = await navigator.gpu.requestAdapter();
     const device = await defaultAdapter.requestDevice();
 
     assert_true(device instanceof WebGPUDevice, "Default device successfully created.");
 }, "Create the default device.");
 
 promise_test(async () => {
-    const lowPowerAdapter = await gpu.requestAdapter({ powerPreference: "low-power" });
+    const lowPowerAdapter = await navigator.gpu.requestAdapter({ powerPreference: "low-power" });
     const device = await lowPowerAdapter.requestDevice();
 
     assert_true(device instanceof WebGPUDevice, "Device successfully created using low-power option.");
 }, "Create a device with a low-power option.");
 
 promise_test(async () => {
-    const highPerfAdapter = await gpu.requestAdapter({ powerPreference: "high-performance" });
+    const highPerfAdapter = await navigator.gpu.requestAdapter({ powerPreference: "high-performance" });
     const device = await highPerfAdapter.requestDevice();
 
     assert_true(device instanceof WebGPUDevice, "Device successfully created using high-performance option.");
diff --git a/LayoutTests/webgpu/js/create-worker-device.js b/LayoutTests/webgpu/js/create-worker-device.js
new file mode 100644 (file)
index 0000000..ebf75fb
--- /dev/null
@@ -0,0 +1,11 @@
+async function tryCreateDevice() {
+    if (!navigator.gpu)
+        postMessage(false);
+
+    const adapter = await navigator.gpu.requestAdapter();
+    await adapter.requestDevice();
+
+    postMessage(true);
+}
+
+tryCreateDevice();
index a64937b..270876b 100644 (file)
@@ -1,5 +1,5 @@
 async function getBasicDevice() {
-    const adapter = await gpu.requestAdapter({ powerPreference: "low-power" });
+    const adapter = await navigator.gpu.requestAdapter({ powerPreference: "low-power" });
     const device = await adapter.requestDevice();
     return device;
 }
index db43da5..6bd18e1 100644 (file)
@@ -9,12 +9,12 @@
 'use strict';
 
 promise_test(async t => {
-    assert_not_equals(window.gpu, undefined, "window.gpu exists");
+    assert_not_equals(navigator.gpu, undefined, "navigator.gpu exists");
 
     const context = document.createElement("canvas").getContext("gpu");
     assert_true(context instanceof GPUCanvasContext, "getContext returned a GPUCanvasContext.");
 
-    const adapter = await window.gpu.requestAdapter({});
+    const adapter = await navigator.gpu.requestAdapter({});
     assert_true(adapter instanceof WebGPUAdapter, "requestAdapter returned a WebGPUAdapter.");
 
     const device = await adapter.requestDevice();
diff --git a/LayoutTests/webgpu/webgpu-enabled-in-worker-expected.txt b/LayoutTests/webgpu/webgpu-enabled-in-worker-expected.txt
new file mode 100644 (file)
index 0000000..f4faecc
--- /dev/null
@@ -0,0 +1,3 @@
+
+PASS Test if WebGPU is enabled for workers. 
+
diff --git a/LayoutTests/webgpu/webgpu-enabled-in-worker.html b/LayoutTests/webgpu/webgpu-enabled-in-worker.html
new file mode 100644 (file)
index 0000000..33dc5e6
--- /dev/null
@@ -0,0 +1,20 @@
+<!DOCTYPE html><!-- webkit-test-runner [ experimental:WebGPUEnabled=true ] -->
+<html>
+<meta charset=utf-8>
+<title>Test if WebGPU is enabled for workers.</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script>
+if (window.testRunner)
+    testRunner.waitUntilDone();
+
+const worker = new Worker('js/create-worker-device.js');
+
+worker.onmessage = event => {
+    test(() => { assert_true(event.data, "Create GPUDevice with a worker."); });
+
+    if (window.testRunner)
+        testRunner.notifyDone();
+};
+</script>
+</html>
\ No newline at end of file
index 76d6c38..537350b 100644 (file)
@@ -10,9 +10,10 @@ promise_test(async () => {
     const context = canvas.getContext("gpu");
     assert_true(context instanceof GPUCanvasContext, "Created GPUCanvasContext.");
 
-    assert_not_equals(window.gpu, undefined, "window.gpu is defined.");
+    assert_equals(window.gpu, undefined, "window.gpu should not be defined.")
+    assert_not_equals(navigator.gpu, undefined, "navigator.gpu is defined.");
 
-    const adapter = await window.gpu.requestAdapter();
+    const adapter = await navigator.gpu.requestAdapter();
     assert_true(adapter instanceof WebGPUAdapter, "Created default GPUAdapter.");
     
     const device = await adapter.requestDevice();
index 47aa518..7c7d79e 100644 (file)
@@ -15,7 +15,7 @@ fragment float4 fragmentShader(float4 position : SV_Position) : SV_Target 0 {
 }
 `;
 async function start() {
-    const adapter = await window.gpu.requestAdapter();
+    const adapter = await navigator.gpu.requestAdapter();
     const device = await adapter.requestDevice();
 
     const shaderModule = device.createShaderModule({code: shaderSource, isWHLSL: true});
index 2a83ee0..e06abb5 100644 (file)
@@ -468,7 +468,6 @@ set(WebCore_NON_SVG_IDL_FILES
 
     Modules/webdriver/NavigatorWebDriver.idl
 
-    Modules/webgpu/DOMWindowWebGPU.idl
     Modules/webgpu/GPUBindGroupLayoutBinding.idl
     Modules/webgpu/GPUBindGroupLayoutDescriptor.idl
     Modules/webgpu/GPUBlendDescriptor.idl
@@ -493,6 +492,7 @@ set(WebCore_NON_SVG_IDL_FILES
     Modules/webgpu/GPUTextureUsage.idl
     Modules/webgpu/GPUVertexAttributeDescriptor.idl
     Modules/webgpu/GPUVertexInputDescriptor.idl
+    Modules/webgpu/NavigatorGPU.idl
     Modules/webgpu/WebGPU.idl
     Modules/webgpu/WebGPUAdapter.idl
     Modules/webgpu/WebGPUBindGroup.idl
@@ -523,6 +523,7 @@ set(WebCore_NON_SVG_IDL_FILES
     Modules/webgpu/WebGPUSwapChain.idl
     Modules/webgpu/WebGPUTexture.idl
     Modules/webgpu/WebGPUTextureView.idl
+    Modules/webgpu/WorkerNavigatorGPU.idl
 
     Modules/websockets/CloseEvent.idl
     Modules/websockets/WebSocket.idl
index 0e81aeb..0be60be 100644 (file)
@@ -1,3 +1,33 @@
+2019-04-30  Justin Fan  <justin_fan@apple.com>
+
+        [WebGPU] Move 'gpu' API entry point from DOMWindow to Navigator
+        https://bugs.webkit.org/show_bug.cgi?id=197348
+
+        Reviewed by Myles C. Maxfield.
+
+        Latest API provides 'gpu' through Navigator instead of DOMWindow. Replace DOMWindowWebGPU with NavigatorGPU.
+
+        Existing tests updated to match. Add test: webgpu-enabled-in-worker.html to ensure workers can access WebGPU.
+
+        * CMakeLists.txt:
+        * DerivedSources.make:
+        * Modules/webgpu/NavigatorGPU.cpp:
+        (WebCore::NavigatorGPU::from):
+        (WebCore::NavigatorGPU::supplementName):
+        (WebCore::NavigatorGPU::gpu):
+        (WebCore::NavigatorGPU::gpu const):
+        * Modules/webgpu/NavigatorGPU.h:
+        * Modules/webgpu/NavigatorGPU.idl:
+        * Modules/webgpu/WorkerNavigatorGPU.cpp:
+        (WebCore::WorkerNavigatorGPU::from):
+        (WebCore::WorkerNavigatorGPU::supplementName):
+        (WebCore::WorkerNavigatorGPU::gpu):
+        (WebCore::WorkerNavigatorGPU::gpu const):
+        * Modules/webgpu/WorkerNavigatorGPU.h:
+        * Modules/webgpu/WorkerNavigatorGPU.idl:
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
 2019-04-30  Zalan Bujtas  <zalan@apple.com>
 
         Double-tapping a post to like doesn't work on Instagram.com (needs 'dblclick' event)
index 8e1009f..0433fab 100644 (file)
@@ -374,7 +374,6 @@ JS_BINDING_IDLS = \
     $(WebCore)/Modules/webdatabase/SQLTransactionCallback.idl \
     $(WebCore)/Modules/webdatabase/SQLTransactionErrorCallback.idl \
     $(WebCore)/Modules/webdriver/NavigatorWebDriver.idl \
-    $(WebCore)/Modules/webgpu/DOMWindowWebGPU.idl \
     $(WebCore)/Modules/webgpu/GPUCanvasContext.idl \
     $(WebCore)/Modules/webgpu/GPUColor.idl \
     $(WebCore)/Modules/webgpu/GPUColorStateDescriptor.idl \
@@ -399,6 +398,7 @@ JS_BINDING_IDLS = \
     $(WebCore)/Modules/webgpu/GPUTextureUsage.idl \
     $(WebCore)/Modules/webgpu/GPUVertexAttributeDescriptor.idl \
        $(WebCore)/Modules/webgpu/GPUVertexInputDescriptor.idl \
+    $(WebCore)/Modules/webgpu/NavigatorGPU.idl \
     $(WebCore)/Modules/webgpu/WebGPU.idl \
     $(WebCore)/Modules/webgpu/WebGPUAdapter.idl \
     $(WebCore)/Modules/webgpu/WebGPUBindGroup.idl \
@@ -429,6 +429,7 @@ JS_BINDING_IDLS = \
     $(WebCore)/Modules/webgpu/WebGPUSwapChain.idl \
     $(WebCore)/Modules/webgpu/WebGPUTexture.idl \
     $(WebCore)/Modules/webgpu/WebGPUTextureView.idl \
+    $(WebCore)/Modules/webgpu/WorkerNavigatorGPU.idl \
     $(WebCore)/Modules/websockets/CloseEvent.idl \
     $(WebCore)/Modules/websockets/WebSocket.idl \
     $(WebCore)/Modules/webvr/DOMWindowWebVR.idl \
  */
 
 #include "config.h"
-#include "DOMWindowWebGPU.h"
+#include "NavigatorGPU.h"
 
 #if ENABLE(WEBGPU)
 
-#include "DOMWindow.h"
+#include "Navigator.h"
 #include "RuntimeEnabledFeatures.h"
 #include "WebGPU.h"
 
 namespace WebCore {
 
-DOMWindowWebGPU::DOMWindowWebGPU(DOMWindow* window)
-    : DOMWindowProperty(window)
+NavigatorGPU* NavigatorGPU::from(Navigator* navigator)
 {
-}
-
-const char* DOMWindowWebGPU::supplementName()
-{
-    return "DOMWindowWebGPU";
-}
-
-DOMWindowWebGPU* DOMWindowWebGPU::from(DOMWindow* window)
-{
-    DOMWindowWebGPU* supplement = static_cast<DOMWindowWebGPU*>(Supplement<DOMWindow>::from(window, supplementName()));
+    NavigatorGPU* supplement = static_cast<NavigatorGPU*>(Supplement<Navigator>::from(navigator, supplementName()));
     if (!supplement) {
-        auto newSupplement = std::make_unique<DOMWindowWebGPU>(window);
+        auto newSupplement = std::make_unique<NavigatorGPU>();
         supplement = newSupplement.get();
-        provideTo(window, supplementName(), WTFMove(newSupplement));
+        provideTo(navigator, supplementName(), WTFMove(newSupplement));
     }
     return supplement;
 }
 
-WebGPU* DOMWindowWebGPU::gpu(DOMWindow& window)
+const char* NavigatorGPU::supplementName()
+{
+    return "NavigatorGPU";
+}
+
+const WebGPU* NavigatorGPU::gpu(Navigator& navigator)
 {
-    return DOMWindowWebGPU::from(&window)->gpu();
+    return NavigatorGPU::from(&navigator)->gpu();
 }
 
-WebGPU* DOMWindowWebGPU::gpu() const
+const WebGPU* NavigatorGPU::gpu() const
 {
     ASSERT(RuntimeEnabledFeatures::sharedFeatures().webGPUEnabled());
 
-    if (!m_gpu && frame())
+    if (!m_gpu)
         m_gpu = WebGPU::create();
     return m_gpu.get();
 }
 
 #if ENABLE(WEBGPU)
 
-#include "DOMWindowProperty.h"
 #include "Supplementable.h"
 
 namespace WebCore {
 
-class DOMWindow;
+class Navigator;
 class WebGPU;
 
-class DOMWindowWebGPU : public Supplement<DOMWindow>, public DOMWindowProperty {
+class NavigatorGPU final : public Supplement<Navigator> {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    explicit DOMWindowWebGPU(DOMWindow*);
-    virtual ~DOMWindowWebGPU() = default;
-    static DOMWindowWebGPU* from(DOMWindow*);
-    static WebGPU* gpu(DOMWindow&);
-    WebGPU* gpu() const;
+    static const WebGPU* gpu(Navigator&);
 
 private:
+    static NavigatorGPU* from(Navigator*);
     static const char* supplementName();
+    
+    const WebGPU* gpu() const;
 
     mutable RefPtr<WebGPU> m_gpu;
 };
diff --git a/Source/WebCore/Modules/webgpu/NavigatorGPU.idl b/Source/WebCore/Modules/webgpu/NavigatorGPU.idl
new file mode 100644 (file)
index 0000000..fc2ede1
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+// https://github.com/gpuweb/gpuweb/blob/master/spec/index.bs
+
+[
+    Conditional=WEBGPU,
+    EnabledAtRuntime=WebGPU,
+    Exposed=Window
+] partial interface Navigator {
+    [SameObject] readonly attribute WebGPU gpu;
+};
diff --git a/Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.cpp b/Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.cpp
new file mode 100644 (file)
index 0000000..bf90f6c
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2018 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 "WorkerNavigatorGPU.h"
+
+#if ENABLE(WEBGPU)
+
+#include "RuntimeEnabledFeatures.h"
+#include "WebGPU.h"
+#include "WorkerNavigator.h"
+
+namespace WebCore {
+
+WorkerNavigatorGPU* WorkerNavigatorGPU::from(WorkerNavigator* navigator)
+{
+    WorkerNavigatorGPU* supplement = static_cast<WorkerNavigatorGPU*>(Supplement<WorkerNavigator>::from(navigator, supplementName()));
+    if (!supplement) {
+        auto newSupplement = std::make_unique<WorkerNavigatorGPU>();
+        supplement = newSupplement.get();
+        provideTo(navigator, supplementName(), WTFMove(newSupplement));
+    }
+    return supplement;
+}
+
+const char* WorkerNavigatorGPU::supplementName()
+{
+    return "WorkerNavigatorGPU";
+}
+
+const WebGPU* WorkerNavigatorGPU::gpu(WorkerNavigator& navigator)
+{
+    return WorkerNavigatorGPU::from(&navigator)->gpu();
+}
+
+const WebGPU* WorkerNavigatorGPU::gpu() const
+{
+    ASSERT(RuntimeEnabledFeatures::sharedFeatures().webGPUEnabled());
+
+    if (!m_gpu)
+        m_gpu = WebGPU::create();
+    return m_gpu.get();
+}
+
+} // namespace WebCore
+
+#endif // ENABLE(WEBGPU)
diff --git a/Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.h b/Source/WebCore/Modules/webgpu/WorkerNavigatorGPU.h
new file mode 100644 (file)
index 0000000..e67453f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2018 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 ENABLE(WEBGPU)
+
+#include "Supplementable.h"
+
+namespace WebCore {
+
+class WebGPU;
+class WorkerNavigator;
+
+class WorkerNavigatorGPU final : public Supplement<WorkerNavigator> {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    static const WebGPU* gpu(WorkerNavigator&);
+
+private:
+    static WorkerNavigatorGPU* from(WorkerNavigator*);
+    static const char* supplementName();
+    
+    const WebGPU* gpu() const;
+
+    mutable RefPtr<WebGPU> m_gpu;
+};
+
+} // namespace WebCore
+
+#endif // ENABLE(WEBGPU)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
-// https://github.com/gpuweb/gpuweb/blob/master/design/sketch.webidl
+// https://github.com/gpuweb/gpuweb/blob/master/spec/index.bs
 
 [
     Conditional=WEBGPU,
     EnabledAtRuntime=WebGPU
-] partial interface DOMWindow {
-    [Replaceable, SameObject] readonly attribute WebGPU gpu;
+] partial interface WorkerNavigator {
+    [SameObject] readonly attribute WebGPU gpu;
 };
index 3606acb..e09eb2e 100644 (file)
@@ -304,8 +304,8 @@ Modules/websockets/WebSocketFrame.cpp
 Modules/websockets/WebSocketHandshake.cpp
 Modules/websockets/WorkerThreadableWebSocketChannel.cpp
 
-Modules/webgpu/DOMWindowWebGPU.cpp
 Modules/webgpu/GPUCanvasContext.cpp
+Modules/webgpu/NavigatorGPU.cpp
 Modules/webgpu/WHLSL/WHLSLInferTypes.cpp
 Modules/webgpu/WHLSL/WHLSLLexer.cpp
 Modules/webgpu/WHLSL/WHLSLParser.cpp
@@ -375,6 +375,7 @@ Modules/webgpu/WebGPUSwapChain.cpp
 Modules/webgpu/WebGPUSwapChainDescriptor.cpp
 Modules/webgpu/WebGPUTexture.cpp
 Modules/webgpu/WebGPUTextureView.cpp
+Modules/webgpu/WorkerNavigatorGPU.cpp
 
 Modules/webvr/NavigatorWebVR.cpp
 Modules/webvr/VRDisplay.cpp
@@ -2655,7 +2656,6 @@ JSDOMWindowFetch.cpp
 JSDOMWindowIndexedDatabase.cpp
 JSDOMWindowSpeechSynthesis.cpp
 JSDOMWindowWebDatabase.cpp
-JSDOMWindowWebGPU.cpp
 JSDOMWindowWebVR.cpp
 JSDataTransfer.cpp
 JSDataTransferItem.cpp
@@ -2918,6 +2918,7 @@ JSNavigator.cpp
 JSNavigatorBeacon.cpp
 JSNavigatorCredentials.cpp
 JSNavigatorGeolocation.cpp
+JSNavigatorGPU.cpp
 JSNavigatorID.cpp
 JSNavigatorLanguage.cpp
 JSNavigatorMediaCapabilities.cpp
@@ -3327,6 +3328,7 @@ JSWorkerGlobalScopeFetch.cpp
 JSWorkerGlobalScopeIndexedDatabase.cpp
 JSWorkerLocation.cpp
 JSWorkerNavigator.cpp
+JSWorkerNavigatorGPU.cpp
 JSWorkerType.cpp
 JSWritableStream.cpp
 JSXMLDocument.cpp
index d717275..7bfdca2 100644 (file)
                0709FC4E1025DEE30059CDBA /* AccessibilitySlider.h in Headers */ = {isa = PBXBuildFile; fileRef = 0709FC4D1025DEE30059CDBA /* AccessibilitySlider.h */; };
                070E09191875EEFC003A1D3C /* PlatformMediaSession.h in Headers */ = {isa = PBXBuildFile; fileRef = 070E09181875ED93003A1D3C /* PlatformMediaSession.h */; settings = {ATTRIBUTES = (Private, ); }; };
                070E81D11BF27656001FDA48 /* VideoTrackPrivateMediaStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 070E81D01BF27656001FDA48 /* VideoTrackPrivateMediaStream.h */; };
-               CDA79827170A279100D45C55 /* AudioSessionIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = CDA79825170A279000D45C55 /* AudioSessionIOS.mm */; };
                070F549817F12F6B00169E04 /* MediaStreamConstraintsValidationClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 070F549717F12F6B00169E04 /* MediaStreamConstraintsValidationClient.h */; };
                0719427F1D088F21002AA51D /* AVFoundationMIMETypeCache.mm in Sources */ = {isa = PBXBuildFile; fileRef = 07C8AD111D073D630087C5CE /* AVFoundationMIMETypeCache.mm */; };
                071A9EC2168FBC43002629F9 /* TextTrackCueGeneric.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 071A9EC0168FB56C002629F9 /* TextTrackCueGeneric.cpp */; };
                CDA29A321CC01A9500901CCF /* PlaybackSessionInterfaceAVKit.h in Headers */ = {isa = PBXBuildFile; fileRef = CDA29A2E1CBF73FC00901CCF /* PlaybackSessionInterfaceAVKit.h */; settings = {ATTRIBUTES = (Private, ); }; };
                CDA595932146DEC300A84185 /* HEVCUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = CDA595912146DEC300A84185 /* HEVCUtilities.h */; };
                CDA595982146DF7800A84185 /* HEVCUtilitiesCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = CDA595962146DF7800A84185 /* HEVCUtilitiesCocoa.h */; };
+               CDA79827170A279100D45C55 /* AudioSessionIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = CDA79825170A279000D45C55 /* AudioSessionIOS.mm */; };
                CDA7982A170A3D0000D45C55 /* AudioSession.h in Headers */ = {isa = PBXBuildFile; fileRef = CDA79821170A22DC00D45C55 /* AudioSession.h */; settings = {ATTRIBUTES = (Private, ); }; };
                CDA98E0B1603CD6000FEA3B1 /* LegacyCDM.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDA98E091603CD5900FEA3B1 /* LegacyCDM.cpp */; };
                CDAB6D2917C7DE6C00C60B34 /* MediaControlsHost.h in Headers */ = {isa = PBXBuildFile; fileRef = CDAB6D2717C7DE6C00C60B34 /* MediaControlsHost.h */; };
                D003914122248D400098998C /* GPUProgrammablePassEncoder.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = GPUProgrammablePassEncoder.cpp; sourceTree = "<group>"; };
                D00487D222457BEA00EED7D9 /* WebGPUPipelineLayoutDescriptor.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WebGPUPipelineLayoutDescriptor.cpp; sourceTree = "<group>"; };
                D00487E5226A634200EED7D9 /* WebGPUSwapChainDescriptor.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WebGPUSwapChainDescriptor.cpp; sourceTree = "<group>"; };
+               D00487EB2274281200EED7D9 /* NavigatorGPU.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = NavigatorGPU.idl; sourceTree = "<group>"; };
+               D00487ED2274281300EED7D9 /* NavigatorGPU.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = NavigatorGPU.cpp; sourceTree = "<group>"; };
+               D00487EE2274281400EED7D9 /* NavigatorGPU.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NavigatorGPU.h; sourceTree = "<group>"; };
+               D00487FD2277AF8100EED7D9 /* WorkerNavigatorGPU.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = WorkerNavigatorGPU.idl; sourceTree = "<group>"; };
+               D00487FE2277AF8200EED7D9 /* WorkerNavigatorGPU.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WorkerNavigatorGPU.h; sourceTree = "<group>"; };
                D00D77FD2242ED450043F12C /* WebGPUComputePassEncoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebGPUComputePassEncoder.h; sourceTree = "<group>"; };
                D00D77FE2242ED450043F12C /* WebGPUComputePassEncoder.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WebGPUComputePassEncoder.cpp; sourceTree = "<group>"; };
                D00D77FF2242ED450043F12C /* WebGPUComputePassEncoder.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = WebGPUComputePassEncoder.idl; sourceTree = "<group>"; };
-               D00F5940216ECC7A000D71DB /* DOMWindowWebGPU.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DOMWindowWebGPU.h; sourceTree = "<group>"; };
-               D00F5941216ECC7A000D71DB /* DOMWindowWebGPU.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = DOMWindowWebGPU.cpp; sourceTree = "<group>"; };
-               D00F5942216ECC7A000D71DB /* DOMWindowWebGPU.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = DOMWindowWebGPU.idl; sourceTree = "<group>"; };
                D00F5946216EFE54000D71DB /* WebGPU.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebGPU.h; sourceTree = "<group>"; };
                D00F5947216EFE54000D71DB /* WebGPU.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WebGPU.cpp; sourceTree = "<group>"; };
                D00F5948216EFE54000D71DB /* WebGPU.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = WebGPU.idl; sourceTree = "<group>"; };
                D0C419F7218404DA009EC1DE /* WebGPURenderPipeline.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebGPURenderPipeline.h; sourceTree = "<group>"; };
                D0C419F8218404DA009EC1DE /* WebGPURenderPipeline.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WebGPURenderPipeline.cpp; sourceTree = "<group>"; };
                D0C419F9218404DA009EC1DE /* WebGPURenderPipeline.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = WebGPURenderPipeline.idl; sourceTree = "<group>"; };
+               D0CAAD532277C2A600801CDF /* WorkerNavigatorGPU.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WorkerNavigatorGPU.cpp; sourceTree = "<group>"; };
                D0CCA94922299F97006979B6 /* GPUOrigin3D.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPUOrigin3D.h; sourceTree = "<group>"; };
                D0CCA94A22299F97006979B6 /* GPUOrigin3D.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = GPUOrigin3D.idl; sourceTree = "<group>"; };
                D0D69C9C222E00C20032927E /* GPUBindGroupLayoutDescriptor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GPUBindGroupLayoutDescriptor.h; sourceTree = "<group>"; };
                        isa = PBXGroup;
                        children = (
                                C210E90D21B4BCA400B7F83D /* WHLSL */,
-                               D00F5941216ECC7A000D71DB /* DOMWindowWebGPU.cpp */,
-                               D00F5940216ECC7A000D71DB /* DOMWindowWebGPU.h */,
-                               D00F5942216ECC7A000D71DB /* DOMWindowWebGPU.idl */,
                                D0B8BB0121C46E78000C7681 /* GPUBindGroupLayoutBinding.h */,
                                D0D69C9F222E015E0032927E /* GPUBindGroupLayoutBinding.idl */,
                                D0D69C9C222E00C20032927E /* GPUBindGroupLayoutDescriptor.h */,
                                D026F483220A472F00AC5F49 /* GPUTextureUsage.idl */,
                                D0D8649621BA18F4003C983C /* GPUVertexAttributeDescriptor.idl */,
                                D0D8649821BA19A7003C983C /* GPUVertexInputDescriptor.idl */,
+                               D00487ED2274281300EED7D9 /* NavigatorGPU.cpp */,
+                               D00487EE2274281400EED7D9 /* NavigatorGPU.h */,
+                               D00487EB2274281200EED7D9 /* NavigatorGPU.idl */,
                                D00F5947216EFE54000D71DB /* WebGPU.cpp */,
                                D00F5946216EFE54000D71DB /* WebGPU.h */,
                                D00F5948216EFE54000D71DB /* WebGPU.idl */,
                                D0EACF882193EE4E000FA75C /* WebGPUTextureView.cpp */,
                                D0EACF872193EE4E000FA75C /* WebGPUTextureView.h */,
                                D0EACF892193EE4E000FA75C /* WebGPUTextureView.idl */,
+                               D0CAAD532277C2A600801CDF /* WorkerNavigatorGPU.cpp */,
+                               D00487FE2277AF8200EED7D9 /* WorkerNavigatorGPU.h */,
+                               D00487FD2277AF8100EED7D9 /* WorkerNavigatorGPU.idl */,
                        );
                        path = webgpu;
                        sourceTree = "<group>";