Add Instance and CreateInstance to webgpu.h

This is the first step in making the API before WGPUDevice creation
match webgpu.h and is necessary to implement WGPUSwapChain.

BUG=dawn:269

Change-Id: If92ced42d7683d79e67c02738949ff8b483d22c4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/14061
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Corentin Wallez
2020-01-10 13:06:48 +00:00
committed by Commit Bot service account
parent e6441b604f
commit 5fc2c82c11
14 changed files with 97 additions and 17 deletions

View File

@@ -80,12 +80,14 @@ namespace dawn_native {
// Instance
Instance::Instance() : mImpl(new InstanceBase()) {
Instance::Instance() : mImpl(InstanceBase::Create()) {
}
Instance::~Instance() {
delete mImpl;
mImpl = nullptr;
if (mImpl != nullptr) {
mImpl->Release();
mImpl = nullptr;
}
}
void Instance::DiscoverDefaultAdapters() {
@@ -121,6 +123,10 @@ namespace dawn_native {
mImpl->SetPlatform(platform);
}
WGPUInstance Instance::Get() const {
return reinterpret_cast<WGPUInstance>(mImpl);
}
size_t GetLazyClearCountForTesting(WGPUDevice device) {
dawn_native::DeviceBase* deviceBase = reinterpret_cast<dawn_native::DeviceBase*>(device);
return deviceBase->GetLazyClearCountForTesting();

View File

@@ -50,6 +50,19 @@ namespace dawn_native {
// InstanceBase
// static
InstanceBase* InstanceBase::Create(const InstanceDescriptor* descriptor) {
Ref<InstanceBase> instance = AcquireRef(new InstanceBase);
if (!instance->Initialize(descriptor)) {
return nullptr;
}
return instance.Detach();
}
bool InstanceBase::Initialize(const InstanceDescriptor*) {
return true;
}
void InstanceBase::DiscoverDefaultAdapters() {
EnsureBackendConnections();

View File

@@ -18,7 +18,9 @@
#include "dawn_native/Adapter.h"
#include "dawn_native/BackendConnection.h"
#include "dawn_native/Extensions.h"
#include "dawn_native/RefCounted.h"
#include "dawn_native/Toggles.h"
#include "dawn_native/dawn_platform.h"
#include <array>
#include <memory>
@@ -29,13 +31,9 @@ namespace dawn_native {
// This is called InstanceBase for consistency across the frontend, even if the backends don't
// specialize this class.
class InstanceBase final {
class InstanceBase final : public RefCounted {
public:
InstanceBase() = default;
~InstanceBase() = default;
InstanceBase(const InstanceBase& other) = delete;
InstanceBase& operator=(const InstanceBase& other) = delete;
static InstanceBase* Create(const InstanceDescriptor* descriptor = nullptr);
void DiscoverDefaultAdapters();
bool DiscoverAdapters(const AdapterDiscoveryOptionsBase* options);
@@ -67,6 +65,14 @@ namespace dawn_native {
dawn_platform::Platform* GetPlatform() const;
private:
InstanceBase() = default;
~InstanceBase() = default;
InstanceBase(const InstanceBase& other) = delete;
InstanceBase& operator=(const InstanceBase& other) = delete;
bool Initialize(const InstanceDescriptor* descriptor);
// Lazily creates connections to all backends that have been compiled.
void EnsureBackendConnections();

View File

@@ -146,6 +146,9 @@ namespace dawn_native {
void SetPlatform(dawn_platform::Platform* platform);
// Returns the underlying WGPUInstance object.
WGPUInstance Get() const;
private:
InstanceBase* mImpl = nullptr;
};

View File

@@ -20,7 +20,10 @@
class ExtensionTests : public testing::Test {
public:
ExtensionTests() : testing::Test(), mInstanceBase(), mAdapterBase(&mInstanceBase) {
ExtensionTests()
: testing::Test(),
mInstanceBase(dawn_native::InstanceBase::Create()),
mAdapterBase(mInstanceBase.Get()) {
}
std::vector<const char*> GetAllExtensionNames() {
@@ -35,7 +38,7 @@ class ExtensionTests : public testing::Test {
static_cast<size_t>(dawn_native::Extension::EnumCount);
protected:
dawn_native::InstanceBase mInstanceBase;
dawn_native::Ref<dawn_native::InstanceBase> mInstanceBase;
dawn_native::null::Adapter mAdapterBase;
};

View File

@@ -51,8 +51,8 @@ namespace {
public:
GetProcAddressTests()
: testing::TestWithParam<DawnFlavor>(),
mNativeInstance(),
mNativeAdapter(&mNativeInstance) {
mNativeInstance(dawn_native::InstanceBase::Create()),
mNativeAdapter(mNativeInstance.Get()) {
}
void SetUp() override {
@@ -90,7 +90,7 @@ namespace {
}
protected:
dawn_native::InstanceBase mNativeInstance;
dawn_native::Ref<dawn_native::InstanceBase> mNativeInstance;
dawn_native::null::Adapter mNativeAdapter;
std::unique_ptr<utils::TerribleCommandBuffer> mC2sBuf;
@@ -136,13 +136,17 @@ namespace {
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "0"), nullptr);
}
// Test that GetProcAddress supports itself: it is handled specially because it is a
// freestanding function and not a method on an object.
TEST_P(GetProcAddressTests, GetProcAddressItself) {
// Test that GetProcAddress supports freestanding function that are handled specially
TEST_P(GetProcAddressTests, FreeStandingFunctions) {
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuGetProcAddress"),
reinterpret_cast<WGPUProc>(mProcs.getProcAddress));
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuGetProcAddress"),
reinterpret_cast<WGPUProc>(mProcs.getProcAddress));
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuCreateInstance"),
reinterpret_cast<WGPUProc>(mProcs.createInstance));
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuCreateInstance"),
reinterpret_cast<WGPUProc>(mProcs.createInstance));
}
INSTANTIATE_TEST_SUITE_P(,