Skip to content

Commit

Permalink
[dawn][test] Update all RequestDevice usages in tests to new API.
Browse files Browse the repository at this point in the history
Bug: 369445924
Change-Id: I518c73c6e63f69e0ea230ecfc3b7f62ecd2d411a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/218696
Reviewed-by: Shrek Shao <[email protected]>
Auto-Submit: Loko Kung <[email protected]>
Commit-Queue: dan sinclair <[email protected]>
Reviewed-by: Kai Ninomiya <[email protected]>
Reviewed-by: dan sinclair <[email protected]>
  • Loading branch information
lokokung authored and Dawn LUCI CQ committed Dec 12, 2024
1 parent 49f7ea9 commit 888be54
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 106 deletions.
1 change: 1 addition & 0 deletions include/dawn/native/DawnNative.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ struct DAWN_NATIVE_EXPORT DawnInstanceDescriptor : wgpu::ChainedStruct {
class DAWN_NATIVE_EXPORT Instance {
public:
explicit Instance(const WGPUInstanceDescriptor* desc = nullptr);
explicit Instance(const wgpu::InstanceDescriptor* desc);
explicit Instance(InstanceBase* impl);
~Instance();

Expand Down
5 changes: 5 additions & 0 deletions src/dawn/native/DawnNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ Instance::Instance(const WGPUInstanceDescriptor* desc)
tint::Initialize();
}

Instance::Instance(const wgpu::InstanceDescriptor* desc)
: mImpl(APICreateInstance(reinterpret_cast<const InstanceDescriptor*>(desc))) {
tint::Initialize();
}

Instance::Instance(InstanceBase* impl) : mImpl(impl) {
if (mImpl != nullptr) {
mImpl->APIAddRef();
Expand Down
95 changes: 37 additions & 58 deletions src/dawn/tests/unittests/native/DeviceCreationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <memory>
#include <optional>
#include <utility>
#include <vector>

#include "dawn/dawn_proc.h"
Expand All @@ -44,21 +45,22 @@
namespace dawn::native {
namespace {

using testing::_;
using testing::Contains;
using testing::EmptySizedString;
using testing::MockCallback;
using testing::IsNull;
using testing::MockCppCallback;
using testing::NonEmptySizedString;
using testing::NotNull;
using testing::SaveArg;
using testing::StrEq;
using testing::WithArg;

class DeviceCreationTest : public testing::Test {
protected:
void SetUp() override {
dawnProcSetProcs(&GetProcs());

// Create an instance with default toggles and create an adapter from it.
WGPUInstanceDescriptor safeInstanceDesc = {};
wgpu::InstanceDescriptor safeInstanceDesc = {};
instance = std::make_unique<Instance>(&safeInstanceDesc);

wgpu::RequestAdapterOptions options = {};
Expand All @@ -70,12 +72,11 @@ class DeviceCreationTest : public testing::Test {
// Create an instance with toggle AllowUnsafeAPIs enabled, and create an unsafe adapter
// from it.
const char* allowUnsafeApisToggle = "allow_unsafe_apis";
WGPUDawnTogglesDescriptor unsafeInstanceTogglesDesc = {};
unsafeInstanceTogglesDesc.chain.sType = WGPUSType::WGPUSType_DawnTogglesDescriptor;
wgpu::DawnTogglesDescriptor unsafeInstanceTogglesDesc = {};
unsafeInstanceTogglesDesc.enabledToggleCount = 1;
unsafeInstanceTogglesDesc.enabledToggles = &allowUnsafeApisToggle;
WGPUInstanceDescriptor unsafeInstanceDesc = {};
unsafeInstanceDesc.nextInChain = &unsafeInstanceTogglesDesc.chain;
wgpu::InstanceDescriptor unsafeInstanceDesc = {};
unsafeInstanceDesc.nextInChain = &unsafeInstanceTogglesDesc;

unsafeInstance = std::make_unique<Instance>(&unsafeInstanceDesc);
unsafeAdapter = unsafeInstance->EnumerateAdapters(&options)[0];
Expand Down Expand Up @@ -276,30 +277,17 @@ TEST_F(DeviceCreationTest, CreateDeviceWithCacheSuccess) {
}
}

class DeviceCreationFutureTest
: public DeviceCreationTest,
public ::testing::WithParamInterface<std::optional<wgpu::CallbackMode>> {
class DeviceCreationFutureTest : public DeviceCreationTest,
public ::testing::WithParamInterface<wgpu::CallbackMode> {
protected:
void RequestDevice(const Adapter& a,
const wgpu::DeviceDescriptor* descriptor,
WGPURequestDeviceCallback callback,
void* userdata) {
wgpu::Adapter wgpuAdapter(a.Get());
if (GetParam() == std::nullopt) {
// Legacy RequestDevice. It should call the callback immediately.
wgpuAdapter.RequestDevice(descriptor, callback, userdata);
return;
}

wgpu::Future future =
wgpuAdapter.RequestDevice(descriptor, {nullptr, *GetParam(), callback, userdata});
void RequestDevice(const wgpu::DeviceDescriptor* descriptor) {
wgpu::Adapter wgpuAdapter(adapter.Get());
wgpu::Future future = wgpuAdapter.RequestDevice(descriptor, GetParam(), mMockCb.Callback());
wgpu::Instance wgpuInstance(instance->Get());
switch (*GetParam()) {
switch (GetParam()) {
case wgpu::CallbackMode::WaitAnyOnly: {
// Callback should complete as soon as poll once.
wgpu::FutureWaitInfo waitInfo = {future};
EXPECT_EQ(wgpuInstance.WaitAny(1, &waitInfo, 0), wgpu::WaitStatus::Success);
ASSERT_TRUE(waitInfo.completed);
EXPECT_EQ(wgpuInstance.WaitAny(future, 0), wgpu::WaitStatus::Success);
break;
}
case wgpu::CallbackMode::AllowSpontaneous:
Expand All @@ -310,58 +298,49 @@ class DeviceCreationFutureTest
break;
}
}

MockCppCallback<wgpu::RequestDeviceCallback2<void>*> mMockCb;
};

INSTANTIATE_TEST_SUITE_P(
,
DeviceCreationFutureTest,
::testing::ValuesIn(std::initializer_list<std::optional<wgpu::CallbackMode>>{
wgpu::CallbackMode::WaitAnyOnly, wgpu::CallbackMode::AllowProcessEvents,
wgpu::CallbackMode::AllowSpontaneous, std::nullopt}));
INSTANTIATE_TEST_SUITE_P(,
DeviceCreationFutureTest,
::testing::ValuesIn(std::initializer_list<wgpu::CallbackMode>{
wgpu::CallbackMode::WaitAnyOnly,
wgpu::CallbackMode::AllowProcessEvents,
wgpu::CallbackMode::AllowSpontaneous}));

// Test successful call to RequestDevice with descriptor
TEST_P(DeviceCreationFutureTest, RequestDeviceSuccess) {
WGPUDevice cDevice;
{
MockCallback<WGPURequestDeviceCallback> cb;
EXPECT_CALL(cb, Call(WGPURequestDeviceStatus_Success, NotNull(), EmptySizedString(), this))
.WillOnce(SaveArg<1>(&cDevice));

wgpu::DeviceDescriptor desc = {};
RequestDevice(adapter, &desc, cb.Callback(), cb.MakeUserdata(this));
}
wgpu::Device device;
EXPECT_CALL(mMockCb, Call(wgpu::RequestDeviceStatus::Success, _, EmptySizedString()))
.WillOnce(WithArg<1>([&](wgpu::Device result) { device = std::move(result); }));

wgpu::Device device = wgpu::Device::Acquire(cDevice);
wgpu::DeviceDescriptor desc = {};
RequestDevice(&desc);
EXPECT_NE(device, nullptr);
}

// Test successful call to RequestDevice with a null descriptor
TEST_P(DeviceCreationFutureTest, RequestDeviceNullDescriptorSuccess) {
WGPUDevice cDevice;
{
MockCallback<WGPURequestDeviceCallback> cb;
EXPECT_CALL(cb, Call(WGPURequestDeviceStatus_Success, NotNull(), EmptySizedString(), this))
.WillOnce(SaveArg<1>(&cDevice));
wgpu::Device device;
EXPECT_CALL(mMockCb, Call(wgpu::RequestDeviceStatus::Success, _, EmptySizedString()))
.WillOnce(WithArg<1>([&](wgpu::Device result) { device = std::move(result); }));

RequestDevice(adapter, nullptr, cb.Callback(), cb.MakeUserdata(this));
}

wgpu::Device device = wgpu::Device::Acquire(cDevice);
RequestDevice(nullptr);
EXPECT_NE(device, nullptr);
}

// Test failing call to RequestDevice with invalid feature
TEST_P(DeviceCreationFutureTest, RequestDeviceFailure) {
MockCallback<WGPURequestDeviceCallback> cb;
EXPECT_CALL(cb, Call(WGPURequestDeviceStatus_Error, nullptr, NonEmptySizedString(), this))
.Times(1);
wgpu::Device device;
EXPECT_CALL(mMockCb, Call(wgpu::RequestDeviceStatus::Error, IsNull(), NonEmptySizedString()))
.WillOnce(WithArg<1>([&](wgpu::Device result) { device = std::move(result); }));

wgpu::DeviceDescriptor desc = {};
wgpu::FeatureName invalidFeature = static_cast<wgpu::FeatureName>(WGPUFeatureName_Force32);
desc.requiredFeatures = &invalidFeature;
desc.requiredFeatureCount = 1;

RequestDevice(adapter, &desc, cb.Callback(), cb.MakeUserdata(this));
RequestDevice(&desc);
}

} // anonymous namespace
Expand Down
79 changes: 31 additions & 48 deletions src/dawn/tests/unittests/wire/WireDeviceLifetimeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ class WireDeviceLifetimeTests : public testing::Test {
// backend device separately.
DawnProcTable BuildProcs() {
DawnProcTable procs = native::GetProcs();
procs.adapterRequestDevice = [](WGPUAdapter self, const WGPUDeviceDescriptor* desc,
WGPURequestDeviceCallback callback, void* userdata) {
using WrappedUserdata = std::pair<WGPURequestDeviceCallback, void*>;
native::GetProcs().adapterRequestDevice(
procs.adapterRequestDevice2 = [](WGPUAdapter self, const WGPUDeviceDescriptor* desc,
WGPURequestDeviceCallbackInfo2 callbackInfo) {
return native::GetProcs().adapterRequestDevice2(
self, desc,
[](WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message,
void* userdata) {
lastBackendDevice = device;
auto* wrappedUserdata = static_cast<WrappedUserdata*>(userdata);
wrappedUserdata->first(status, device, message, wrappedUserdata->second);
delete wrappedUserdata;
},
new WrappedUserdata(callback, userdata));
{nullptr, callbackInfo.mode,
[](WGPURequestDeviceStatus status, WGPUDevice device, WGPUStringView message,
void* userdata, void*) {
lastBackendDevice = device;
std::unique_ptr<WGPURequestDeviceCallbackInfo2> info(
static_cast<WGPURequestDeviceCallbackInfo2*>(userdata));
info->callback(status, device, message, info->userdata1, info->userdata2);
},
new WGPURequestDeviceCallbackInfo2(callbackInfo), nullptr});
};

return procs;
Expand All @@ -99,12 +99,10 @@ class WireDeviceLifetimeTests : public testing::Test {
TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenUncapturedErrorCallback) {
wgpu::Device device;
wgpu::DeviceDescriptor deviceDesc = {};
adapter.RequestDevice(
&deviceDesc,
[](WGPURequestDeviceStatus, WGPUDevice cDevice, WGPUStringView, void* userdata) {
*static_cast<wgpu::Device*>(userdata) = wgpu::Device::Acquire(cDevice);
},
&device);

adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
[&device](wgpu::RequestDeviceStatus status, wgpu::Device d,
wgpu::StringView) { device = std::move(d); });
ASSERT_TRUE(wireHelper->FlushClient());
ASSERT_TRUE(wireHelper->FlushServer());
ASSERT_NE(device, nullptr);
Expand All @@ -122,12 +120,9 @@ TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenUncapturedErrorCallback
device = nullptr;

// Request a new device. This overrides the wire's device-related data.
adapter.RequestDevice(
&deviceDesc,
[](WGPURequestDeviceStatus, WGPUDevice cDevice, WGPUStringView, void* userdata) {
*static_cast<wgpu::Device*>(userdata) = wgpu::Device::Acquire(cDevice);
},
&device);
adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
[&device](wgpu::RequestDeviceStatus status, wgpu::Device d,
wgpu::StringView) { device = std::move(d); });
ASSERT_TRUE(wireHelper->FlushClient());
ASSERT_TRUE(wireHelper->FlushServer());
ASSERT_NE(device, nullptr);
Expand All @@ -142,12 +137,9 @@ TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenUncapturedErrorCallback
TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenLoggingCallback) {
wgpu::Device device;
wgpu::DeviceDescriptor deviceDesc = {};
adapter.RequestDevice(
&deviceDesc,
[](WGPURequestDeviceStatus, WGPUDevice cDevice, WGPUStringView, void* userdata) {
*static_cast<wgpu::Device*>(userdata) = wgpu::Device::Acquire(cDevice);
},
&device);
adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
[&device](wgpu::RequestDeviceStatus status, wgpu::Device d,
wgpu::StringView) { device = std::move(d); });
ASSERT_TRUE(wireHelper->FlushClient());
ASSERT_TRUE(wireHelper->FlushServer());
ASSERT_NE(device, nullptr);
Expand All @@ -170,12 +162,9 @@ TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenLoggingCallback) {
device = nullptr;

// Request a new device. This overrides the wire's device-related data.
adapter.RequestDevice(
&deviceDesc,
[](WGPURequestDeviceStatus, WGPUDevice cDevice, WGPUStringView, void* userdata) {
*static_cast<wgpu::Device*>(userdata) = wgpu::Device::Acquire(cDevice);
},
&device);
adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
[&device](wgpu::RequestDeviceStatus status, wgpu::Device d,
wgpu::StringView) { device = std::move(d); });
ASSERT_TRUE(wireHelper->FlushClient());
ASSERT_TRUE(wireHelper->FlushServer());
ASSERT_NE(device, nullptr);
Expand All @@ -190,12 +179,9 @@ TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenLoggingCallback) {
TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenLostCallback) {
wgpu::Device device;
wgpu::DeviceDescriptor deviceDesc = {};
adapter.RequestDevice(
&deviceDesc,
[](WGPURequestDeviceStatus, WGPUDevice cDevice, WGPUStringView, void* userdata) {
*static_cast<wgpu::Device*>(userdata) = wgpu::Device::Acquire(cDevice);
},
&device);
adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
[&device](wgpu::RequestDeviceStatus status, wgpu::Device d,
wgpu::StringView) { device = std::move(d); });
ASSERT_TRUE(wireHelper->FlushClient());
ASSERT_TRUE(wireHelper->FlushServer());
ASSERT_NE(device, nullptr);
Expand All @@ -210,12 +196,9 @@ TEST_F(WireDeviceLifetimeTests, DeviceDroppedFromWireThenLostCallback) {
nativeProcs.deviceDestroy(oldDevice);

// Request a new device. This overrides the wire's device-related data.
adapter.RequestDevice(
&deviceDesc,
[](WGPURequestDeviceStatus, WGPUDevice cDevice, WGPUStringView, void* userdata) {
*static_cast<wgpu::Device*>(userdata) = wgpu::Device::Acquire(cDevice);
},
&device);
adapter.RequestDevice(&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
[&device](wgpu::RequestDeviceStatus status, wgpu::Device d,
wgpu::StringView) { device = std::move(d); });
ASSERT_TRUE(wireHelper->FlushClient());
ASSERT_TRUE(wireHelper->FlushServer());
ASSERT_NE(device, nullptr);
Expand Down

0 comments on commit 888be54

Please sign in to comment.