• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "tests/DawnNativeTest.h"
16 
17 #include "absl/strings/str_cat.h"
18 #include "common/Assert.h"
19 #include "dawn/dawn_proc.h"
20 #include "dawn_native/ErrorData.h"
21 
22 namespace dawn_native {
23 
AddFatalDawnFailure(const char * expression,const ErrorData * error)24     void AddFatalDawnFailure(const char* expression, const ErrorData* error) {
25         const auto& backtrace = error->GetBacktrace();
26         GTEST_MESSAGE_AT_(
27             backtrace.at(0).file, backtrace.at(0).line,
28             absl::StrCat(expression, " returned error: ", error->GetMessage()).c_str(),
29             ::testing::TestPartResult::kFatalFailure);
30     }
31 
32 }  // namespace dawn_native
33 
DawnNativeTest()34 DawnNativeTest::DawnNativeTest() {
35     dawnProcSetProcs(&dawn_native::GetProcs());
36 }
37 
~DawnNativeTest()38 DawnNativeTest::~DawnNativeTest() {
39     device = wgpu::Device();
40     dawnProcSetProcs(nullptr);
41 }
42 
SetUp()43 void DawnNativeTest::SetUp() {
44     instance = std::make_unique<dawn_native::Instance>();
45     instance->DiscoverDefaultAdapters();
46 
47     std::vector<dawn_native::Adapter> adapters = instance->GetAdapters();
48 
49     // DawnNative unittests run against the null backend, find the corresponding adapter
50     bool foundNullAdapter = false;
51     for (auto& currentAdapter : adapters) {
52         wgpu::AdapterProperties adapterProperties;
53         currentAdapter.GetProperties(&adapterProperties);
54 
55         if (adapterProperties.backendType == wgpu::BackendType::Null) {
56             adapter = currentAdapter;
57             foundNullAdapter = true;
58             break;
59         }
60     }
61 
62     ASSERT(foundNullAdapter);
63 
64     device = wgpu::Device(CreateTestDevice());
65     device.SetUncapturedErrorCallback(DawnNativeTest::OnDeviceError, nullptr);
66 }
67 
TearDown()68 void DawnNativeTest::TearDown() {
69 }
70 
CreateTestDevice()71 WGPUDevice DawnNativeTest::CreateTestDevice() {
72     // Disabled disallowing unsafe APIs so we can test them.
73     dawn_native::DawnDeviceDescriptor deviceDescriptor;
74     deviceDescriptor.forceDisabledToggles.push_back("disallow_unsafe_apis");
75 
76     return adapter.CreateDevice(&deviceDescriptor);
77 }
78 
79 // static
OnDeviceError(WGPUErrorType type,const char * message,void * userdata)80 void DawnNativeTest::OnDeviceError(WGPUErrorType type, const char* message, void* userdata) {
81     ASSERT(type != WGPUErrorType_NoError);
82     FAIL() << "Unexpected error: " << message;
83 }
84