Commit 418708c1 authored by Austin Eng's avatar Austin Eng Committed by Commit Bot

Add DawnControlClientHolder to track Dawn client state

WebGPU objects will be backed by Dawn objects. If the Dawn client is
destroyed, Dawn objects are destroyed by the Dawn Wire.
The Blink WebGPU objects need to know if the client is destroyed
so that they do not call the destructors and double-free Dawn handles.

Bug: 941536
Change-Id: I288ec5c55a29ae135c4ec0b202bdd7d61e781d57
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1519552
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: default avatarKenneth Russell <kbr@chromium.org>
Reviewed-by: default avatarCorentin Wallez <cwallez@chromium.org>
Reviewed-by: default avatarKai Ninomiya <kainino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#641125}
parent bf917713
...@@ -6,6 +6,10 @@ import("//third_party/blink/renderer/modules/modules.gni") ...@@ -6,6 +6,10 @@ import("//third_party/blink/renderer/modules/modules.gni")
blink_modules_sources("webgpu") { blink_modules_sources("webgpu") {
sources = [ sources = [
"dawn_control_client_holder.cc",
"dawn_control_client_holder.h",
"dawn_object.cc",
"dawn_object.h",
"gpu.cc", "gpu.cc",
"gpu.h", "gpu.h",
"gpu_adapter.cc", "gpu_adapter.cc",
......
// 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/dawn_control_client_holder.h"
#include "base/logging.h"
namespace blink {
DawnControlClientHolder::DawnControlClientHolder(
gpu::webgpu::WebGPUInterface* interface)
: interface_(interface), destroyed_(!interface) {}
void DawnControlClientHolder::MarkDestroyed() {
destroyed_ = true;
}
bool DawnControlClientHolder::IsDestroyed() const {
return destroyed_;
}
gpu::webgpu::WebGPUInterface* DawnControlClientHolder::GetInterface() const {
DCHECK(!destroyed_);
return interface_;
}
} // 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_DAWN_CONTROL_CLIENT_HOLDER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONTROL_CLIENT_HOLDER_H_
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
namespace gpu {
namespace webgpu {
class WebGPUInterface;
} // namespace webgpu
} // namespace gpu
namespace blink {
// This class holds the WebGPUInterface and a |destroyed_| flag.
// DawnControlClientHolder::MarkDestroyed() should be called if the
// backing WebGPUInterface has been destroyed.
class DawnControlClientHolder : public RefCounted<DawnControlClientHolder> {
public:
DawnControlClientHolder(gpu::webgpu::WebGPUInterface* interface);
void MarkDestroyed();
bool IsDestroyed() const;
gpu::webgpu::WebGPUInterface* GetInterface() const;
private:
friend class RefCounted<DawnControlClientHolder>;
~DawnControlClientHolder() = default;
gpu::webgpu::WebGPUInterface* interface_;
bool destroyed_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_CONTROL_CLIENT_HOLDER_H_
// 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/dawn_object.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_control_client_holder.h"
namespace blink {
DawnObject::DawnObject(
scoped_refptr<DawnControlClientHolder> dawn_control_client)
: dawn_control_client_(dawn_control_client) {}
DawnObject::~DawnObject() = default;
const scoped_refptr<DawnControlClientHolder>& DawnObject::GetDawnControlClient()
const {
return dawn_control_client_;
}
bool DawnObject::IsDawnControlClientDestroyed() const {
return dawn_control_client_->IsDestroyed();
}
gpu::webgpu::WebGPUInterface* DawnObject::GetInterface() const {
return dawn_control_client_->GetInterface();
}
} // 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_DAWN_OBJECT_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
namespace gpu {
namespace webgpu {
class WebGPUInterface;
} // namespace webgpu
} // namespace gpu
namespace blink {
class DawnControlClientHolder;
// This class allows objects to hold onto a DawnControlClientHolder.
// The DawnControlClientHolder is used to hold the WebGPUInterface and keep
// track of whether or not the client has been destroyed. If the client is
// destroyed, we should not call any Dawn functions.
class DawnObject : public ScriptWrappable {
public:
DawnObject(scoped_refptr<DawnControlClientHolder> dawn_control_client);
~DawnObject() override;
const scoped_refptr<DawnControlClientHolder>& GetDawnControlClient() const;
bool IsDawnControlClientDestroyed() const;
gpu::webgpu::WebGPUInterface* GetInterface() const;
private:
scoped_refptr<DawnControlClientHolder> dawn_control_client_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h" #include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.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/modules/webgpu/dawn_control_client_holder.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/modules/webgpu/gpu_request_adapter_options.h" #include "third_party/blink/renderer/modules/webgpu/gpu_request_adapter_options.h"
...@@ -44,7 +45,9 @@ GPU* GPU::Create(ExecutionContext& execution_context) { ...@@ -44,7 +45,9 @@ GPU* GPU::Create(ExecutionContext& execution_context) {
GPU::GPU(ExecutionContext& execution_context, GPU::GPU(ExecutionContext& execution_context,
std::unique_ptr<WebGraphicsContext3DProvider> context_provider) std::unique_ptr<WebGraphicsContext3DProvider> context_provider)
: ContextLifecycleObserver(&execution_context), : ContextLifecycleObserver(&execution_context),
context_provider_(std::move(context_provider)) {} context_provider_(std::move(context_provider)),
dawn_control_client_(base::MakeRefCounted<DawnControlClientHolder>(
context_provider_->WebGPUInterface())) {}
GPU::~GPU() = default; GPU::~GPU() = default;
...@@ -54,6 +57,7 @@ void GPU::Trace(blink::Visitor* visitor) { ...@@ -54,6 +57,7 @@ void GPU::Trace(blink::Visitor* visitor) {
} }
void GPU::ContextDestroyed(ExecutionContext* execution_context) { void GPU::ContextDestroyed(ExecutionContext* execution_context) {
dawn_control_client_->MarkDestroyed();
context_provider_.reset(); context_provider_.reset();
} }
...@@ -63,7 +67,7 @@ ScriptPromise GPU::requestAdapter(ScriptState* script_state, ...@@ -63,7 +67,7 @@ ScriptPromise GPU::requestAdapter(ScriptState* script_state,
ScriptPromise promise = resolver->Promise(); ScriptPromise promise = resolver->Promise();
// TODO(enga): Request the adapter from the WebGPUInterface. // TODO(enga): Request the adapter from the WebGPUInterface.
GPUAdapter* adapter = GPUAdapter::Create("Default"); GPUAdapter* adapter = GPUAdapter::Create("Default", dawn_control_client_);
resolver->Resolve(adapter); resolver->Resolve(adapter);
return promise; return promise;
......
...@@ -5,9 +5,7 @@ ...@@ -5,9 +5,7 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_H_
#include <memory> #include "base/memory/scoped_refptr.h"
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/execution_context/context_lifecycle_observer.h" #include "third_party/blink/renderer/core/execution_context/context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
...@@ -17,6 +15,8 @@ namespace blink { ...@@ -17,6 +15,8 @@ namespace blink {
class GPURequestAdapterOptions; class GPURequestAdapterOptions;
class ScriptState; class ScriptState;
class WebGraphicsContext3DProvider;
class DawnControlClientHolder;
class GPU final : public ScriptWrappable, public ContextLifecycleObserver { class GPU final : public ScriptWrappable, public ContextLifecycleObserver {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
...@@ -40,6 +40,7 @@ class GPU final : public ScriptWrappable, public ContextLifecycleObserver { ...@@ -40,6 +40,7 @@ class GPU final : public ScriptWrappable, public ContextLifecycleObserver {
private: private:
std::unique_ptr<WebGraphicsContext3DProvider> context_provider_; std::unique_ptr<WebGraphicsContext3DProvider> context_provider_;
scoped_refptr<DawnControlClientHolder> dawn_control_client_;
DISALLOW_COPY_AND_ASSIGN(GPU); DISALLOW_COPY_AND_ASSIGN(GPU);
}; };
......
...@@ -4,23 +4,29 @@ ...@@ -4,23 +4,29 @@
#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/modules/webgpu/dawn_control_client_holder.h"
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h" #include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
namespace blink { namespace blink {
// static // static
GPUAdapter* GPUAdapter::Create(const String& name) { GPUAdapter* GPUAdapter::Create(
return MakeGarbageCollected<GPUAdapter>(name); const String& name,
scoped_refptr<DawnControlClientHolder> dawn_control_client) {
return MakeGarbageCollected<GPUAdapter>(name, std::move(dawn_control_client));
} }
GPUAdapter::GPUAdapter(
const String& name,
scoped_refptr<DawnControlClientHolder> dawn_control_client)
: DawnObject(std::move(dawn_control_client)), name_(name) {}
const String& GPUAdapter::name() const { const String& GPUAdapter::name() const {
return name_; return name_;
} }
GPUDevice* GPUAdapter::createDevice(const GPUDeviceDescriptor* descriptor) { GPUDevice* GPUAdapter::createDevice(const GPUDeviceDescriptor* descriptor) {
return GPUDevice::Create(this, descriptor); return GPUDevice::Create(GetDawnControlClient(), this, descriptor);
} }
GPUAdapter::GPUAdapter(const String& name) : name_(name) {}
} // namespace blink } // namespace blink
...@@ -5,8 +5,9 @@ ...@@ -5,8 +5,9 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_ADAPTER_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_ADAPTER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_ADAPTER_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_ADAPTER_H_
#include "base/memory/scoped_refptr.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/platform/bindings/script_wrappable.h" #include "third_party/blink/renderer/modules/webgpu/dawn_object.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink { namespace blink {
...@@ -14,13 +15,15 @@ namespace blink { ...@@ -14,13 +15,15 @@ namespace blink {
class GPUDevice; class GPUDevice;
class GPUDeviceDescriptor; class GPUDeviceDescriptor;
class GPUAdapter final : public ScriptWrappable { class GPUAdapter final : public DawnObject {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
static GPUAdapter* Create(const String& name); static GPUAdapter* Create(
const String& name,
GPUAdapter(const String& name); scoped_refptr<DawnControlClientHolder> dawn_control_client);
GPUAdapter(const String& name,
scoped_refptr<DawnControlClientHolder> dawn_control_client);
const String& name() const; const String& name() const;
......
...@@ -5,20 +5,26 @@ ...@@ -5,20 +5,26 @@
#include "third_party/blink/renderer/modules/webgpu/gpu_device.h" #include "third_party/blink/renderer/modules/webgpu/gpu_device.h"
#include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_control_client_holder.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/modules/webgpu/gpu_device_descriptor.h" #include "third_party/blink/renderer/modules/webgpu/gpu_device_descriptor.h"
namespace blink { namespace blink {
// static // static
GPUDevice* GPUDevice::Create(GPUAdapter* adapter, GPUDevice* GPUDevice::Create(
const GPUDeviceDescriptor* descriptor) { scoped_refptr<DawnControlClientHolder> dawn_control_client,
return MakeGarbageCollected<GPUDevice>(adapter, descriptor); GPUAdapter* adapter,
const GPUDeviceDescriptor* descriptor) {
return MakeGarbageCollected<GPUDevice>(std::move(dawn_control_client),
adapter, descriptor);
} }
// TODO(enga): Handle adapter options and device descriptor // TODO(enga): Handle adapter options and device descriptor
GPUDevice::GPUDevice(GPUAdapter* adapter, const GPUDeviceDescriptor* descriptor) GPUDevice::GPUDevice(scoped_refptr<DawnControlClientHolder> dawn_control_client,
: adapter_(adapter) {} GPUAdapter* adapter,
const GPUDeviceDescriptor* descriptor)
: DawnObject(std::move(dawn_control_client)), adapter_(adapter) {}
GPUAdapter* GPUDevice::adapter() const { GPUAdapter* GPUDevice::adapter() const {
return adapter_; return adapter_;
......
...@@ -5,26 +5,31 @@ ...@@ -5,26 +5,31 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_GPU_DEVICE_H_
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" #include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/modules/webgpu/dawn_object.h"
namespace blink { namespace blink {
class GPUAdapter; class GPUAdapter;
class GPUDeviceDescriptor; class GPUDeviceDescriptor;
class GPUDevice final : public ScriptWrappable { class GPUDevice final : public DawnObject {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
static GPUDevice* Create(GPUAdapter* adapter, static GPUDevice* Create(
const GPUDeviceDescriptor* descriptor); scoped_refptr<DawnControlClientHolder> dawn_control_client,
GPUAdapter* adapter,
const GPUDeviceDescriptor* descriptor);
explicit GPUDevice(scoped_refptr<DawnControlClientHolder> dawn_control_client,
GPUAdapter* adapter,
const GPUDeviceDescriptor* descriptor);
GPUDevice(GPUAdapter* adapter, const GPUDeviceDescriptor* descriptor); void Trace(blink::Visitor* visitor) override;
// gpu_device.idl
GPUAdapter* adapter() const; GPUAdapter* adapter() const;
void Trace(blink::Visitor*) override;
private: private:
Member<GPUAdapter> adapter_; Member<GPUAdapter> adapter_;
......
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