Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[WebGPU] Move 'gpu' API entry point from DOMWindow to Navigator
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:


Canonical link: https://commits.webkit.org/211602@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244777 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Justin Fan committed Apr 30, 2019
1 parent 4cd3193 commit 5ef00cf
Show file tree
Hide file tree
Showing 23 changed files with 306 additions and 51 deletions.
26 changes: 26 additions & 0 deletions LayoutTests/ChangeLog
@@ -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]
Expand Down
2 changes: 2 additions & 0 deletions LayoutTests/fast/dom/navigator-detached-no-crash-expected.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions LayoutTests/webgpu/adapter-options.html
Expand Up @@ -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.");
Expand Down
11 changes: 11 additions & 0 deletions LayoutTests/webgpu/js/create-worker-device.js
@@ -0,0 +1,11 @@
async function tryCreateDevice() {
if (!navigator.gpu)
postMessage(false);

const adapter = await navigator.gpu.requestAdapter();
await adapter.requestDevice();

postMessage(true);
}

tryCreateDevice();
2 changes: 1 addition & 1 deletion LayoutTests/webgpu/js/webgpu-functions.js
@@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions LayoutTests/webgpu/queue-creation.html
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions LayoutTests/webgpu/webgpu-enabled-in-worker-expected.txt
@@ -0,0 +1,3 @@

PASS Test if WebGPU is enabled for workers.

20 changes: 20 additions & 0 deletions LayoutTests/webgpu/webgpu-enabled-in-worker.html
@@ -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>
5 changes: 3 additions & 2 deletions LayoutTests/webgpu/webgpu-enabled.html
Expand Up @@ -10,9 +10,10 @@
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();
Expand Down
2 changes: 1 addition & 1 deletion LayoutTests/webgpu/whlsl.html
Expand Up @@ -15,7 +15,7 @@
}
`;
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});
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/CMakeLists.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions Source/WebCore/ChangeLog
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/DerivedSources.make
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
Expand Up @@ -24,47 +24,42 @@
*/

#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();
}
Expand Down
Expand Up @@ -27,25 +27,23 @@

#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;
};
Expand Down
33 changes: 33 additions & 0 deletions Source/WebCore/Modules/webgpu/NavigatorGPU.idl
@@ -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;
};

0 comments on commit 5ef00cf

Please sign in to comment.