Commit b8aa741d authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Make GPU a Supplement<NavigatorBase>

NavigatorBase is a newly-added base class for Navigator and
WorkerNavigator. Making GPU a Supplement of NavigatorBase
allows a single class to handle all of the work of exposing the
navigator.gpu getter for both windows and workers, and
greatly reduces boilerplate.

Bug: 1147612
Change-Id: I8621639935b014eb4905804f432327729619c3d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537775
Commit-Queue: Kenneth Russell <kbr@chromium.org>
Auto-Submit: Nate Chapin <japhet@chromium.org>
Reviewed-by: default avatarKenneth Russell <kbr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827480}
parent 88b177ae
...@@ -74,10 +74,6 @@ blink_modules_sources("webgpu") { ...@@ -74,10 +74,6 @@ blink_modules_sources("webgpu") {
"gpu_uncaptured_error_event.h", "gpu_uncaptured_error_event.h",
"gpu_validation_error.cc", "gpu_validation_error.cc",
"gpu_validation_error.h", "gpu_validation_error.h",
"navigator_gpu.cc",
"navigator_gpu.h",
"worker_navigator_gpu.cc",
"worker_navigator_gpu.h",
] ]
deps = [ deps = [
"//gpu/command_buffer/client:webgpu_interface", "//gpu/command_buffer/client:webgpu_interface",
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_request_adapter_options.h" #include "third_party/blink/renderer/bindings/modules/v8/v8_gpu_request_adapter_options.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h" #include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/execution_context/navigator_base.h"
#include "third_party/blink/renderer/core/inspector/console_message.h" #include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_adapter.h" #include "third_party/blink/renderer/modules/webgpu/gpu_adapter.h"
#include "third_party/blink/renderer/platform/graphics/gpu/dawn_control_client_holder.h" #include "third_party/blink/renderer/platform/graphics/gpu/dawn_control_client_holder.h"
...@@ -81,17 +82,27 @@ std::unique_ptr<WebGraphicsContext3DProvider> CreateContextProvider( ...@@ -81,17 +82,27 @@ std::unique_ptr<WebGraphicsContext3DProvider> CreateContextProvider(
} // anonymous namespace } // anonymous namespace
// static // static
GPU* GPU::Create(ExecutionContext& execution_context) { const char GPU::kSupplementName[] = "GPU";
return MakeGarbageCollected<GPU>(execution_context);
// static
GPU* GPU::gpu(NavigatorBase& navigator) {
GPU* gpu = Supplement<NavigatorBase>::From<GPU>(navigator);
if (!gpu) {
gpu = MakeGarbageCollected<GPU>(navigator);
ProvideTo(navigator, gpu);
}
return gpu;
} }
GPU::GPU(ExecutionContext& execution_context) GPU::GPU(NavigatorBase& navigator)
: ExecutionContextLifecycleObserver(&execution_context) {} : Supplement<NavigatorBase>(navigator),
ExecutionContextLifecycleObserver(navigator.GetExecutionContext()) {}
GPU::~GPU() = default; GPU::~GPU() = default;
void GPU::Trace(Visitor* visitor) const { void GPU::Trace(Visitor* visitor) const {
ScriptWrappable::Trace(visitor); ScriptWrappable::Trace(visitor);
Supplement<NavigatorBase>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor); ExecutionContextLifecycleObserver::Trace(visitor);
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h" #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/supplementable.h"
struct WGPUDeviceProperties; struct WGPUDeviceProperties;
...@@ -17,17 +18,23 @@ namespace blink { ...@@ -17,17 +18,23 @@ namespace blink {
class GPUAdapter; class GPUAdapter;
class GPURequestAdapterOptions; class GPURequestAdapterOptions;
class NavigatorBase;
class ScriptPromiseResolver; class ScriptPromiseResolver;
class ScriptState; class ScriptState;
class DawnControlClientHolder; class DawnControlClientHolder;
class GPU final : public ScriptWrappable, class GPU final : public ScriptWrappable,
public Supplement<NavigatorBase>,
public ExecutionContextLifecycleObserver { public ExecutionContextLifecycleObserver {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
static GPU* Create(ExecutionContext& execution_context); static const char kSupplementName[];
explicit GPU(ExecutionContext& execution_context);
// Getter for navigator.gpu
static GPU* gpu(NavigatorBase&);
explicit GPU(NavigatorBase&);
~GPU() override; ~GPU() override;
// ScriptWrappable overrides // ScriptWrappable overrides
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webgpu/navigator_gpu.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/modules/webgpu/gpu.h"
namespace blink {
// static
NavigatorGPU& NavigatorGPU::From(Navigator& navigator) {
NavigatorGPU* supplement =
Supplement<Navigator>::From<NavigatorGPU>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<NavigatorGPU>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
// static
GPU* NavigatorGPU::gpu(ScriptState* script_state, Navigator& navigator) {
return NavigatorGPU::From(navigator).gpu(script_state);
}
GPU* NavigatorGPU::gpu(ScriptState* script_state) {
if (!gpu_) {
ExecutionContext* context = ExecutionContext::From(script_state);
DCHECK(context);
gpu_ = GPU::Create(*context);
}
return gpu_;
}
void NavigatorGPU::Trace(Visitor* visitor) const {
visitor->Trace(gpu_);
Supplement<Navigator>::Trace(visitor);
}
NavigatorGPU::NavigatorGPU(Navigator& navigator)
: Supplement<Navigator>(navigator) {}
const char NavigatorGPU::kSupplementName[] = "NavigatorGPU";
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_NAVIGATOR_GPU_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_NAVIGATOR_GPU_H_
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class Navigator;
class GPU;
class NavigatorGPU final : public GarbageCollected<NavigatorGPU>,
public Supplement<Navigator> {
public:
static const char kSupplementName[];
// Gets, or creates, NavigatorGPU supplement on Navigator.
// See platform/Supplementable.h
static NavigatorGPU& From(Navigator&);
static GPU* gpu(ScriptState* script_state, Navigator&);
GPU* gpu(ScriptState* script_state);
explicit NavigatorGPU(Navigator&);
void Trace(Visitor*) const override;
private:
Member<GPU> gpu_;
DISALLOW_COPY_AND_ASSIGN(NavigatorGPU);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_NAVIGATOR_GPU_H_
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
[ [
Exposed=Window, Exposed=Window,
ImplementedAs=NavigatorGPU ImplementedAs=GPU
] partial interface Navigator { ] partial interface Navigator {
[SameObject, RuntimeEnabled=WebGPU, CallWith=ScriptState] readonly attribute GPU gpu; [SameObject, RuntimeEnabled=WebGPU] readonly attribute GPU gpu;
}; };
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webgpu/worker_navigator_gpu.h"
#include "third_party/blink/renderer/core/workers/worker_navigator.h"
#include "third_party/blink/renderer/modules/webgpu/gpu.h"
namespace blink {
// static
WorkerNavigatorGPU& WorkerNavigatorGPU::From(WorkerNavigator& navigator) {
WorkerNavigatorGPU* supplement =
Supplement<WorkerNavigator>::From<WorkerNavigatorGPU>(navigator);
if (!supplement) {
supplement = MakeGarbageCollected<WorkerNavigatorGPU>(navigator);
ProvideTo(navigator, supplement);
}
return *supplement;
}
// static
GPU* WorkerNavigatorGPU::gpu(ScriptState* script_state,
WorkerNavigator& navigator) {
return WorkerNavigatorGPU::From(navigator).gpu(script_state);
}
GPU* WorkerNavigatorGPU::gpu(ScriptState* script_state) {
if (!gpu_) {
ExecutionContext* context = ExecutionContext::From(script_state);
DCHECK(context);
gpu_ = GPU::Create(*context);
}
return gpu_;
}
void WorkerNavigatorGPU::Trace(Visitor* visitor) const {
visitor->Trace(gpu_);
Supplement<WorkerNavigator>::Trace(visitor);
}
WorkerNavigatorGPU::WorkerNavigatorGPU(WorkerNavigator& navigator)
: Supplement<WorkerNavigator>(navigator) {}
const char WorkerNavigatorGPU::kSupplementName[] = "WorkerNavigatorGPU";
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_WORKER_NAVIGATOR_GPU_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_WORKER_NAVIGATOR_GPU_H_
#include "third_party/blink/renderer/core/workers/worker_navigator.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class GPU;
class WorkerNavigator;
class WorkerNavigatorGPU final : public GarbageCollected<WorkerNavigatorGPU>,
public Supplement<WorkerNavigator> {
public:
static const char kSupplementName[];
// Gets, or creates, WorkerNavigatorGPU supplement on WorkerNavigator.
// See platform/Supplementable.h
static WorkerNavigatorGPU& From(WorkerNavigator&);
static GPU* gpu(ScriptState* script_state, WorkerNavigator&);
GPU* gpu(ScriptState* script_state);
explicit WorkerNavigatorGPU(WorkerNavigator&);
void Trace(Visitor*) const override;
private:
Member<GPU> gpu_;
DISALLOW_COPY_AND_ASSIGN(WorkerNavigatorGPU);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_WORKER_NAVIGATOR_GPU_H_
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
[ [
Exposed=Worker, Exposed=Worker,
ImplementedAs=WorkerNavigatorGPU ImplementedAs=GPU
] partial interface WorkerNavigator { ] partial interface WorkerNavigator {
[SameObject, RuntimeEnabled=WebGPU, CallWith=ScriptState] readonly attribute GPU gpu; [SameObject, RuntimeEnabled=WebGPU] readonly attribute GPU gpu;
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment