Peng Huang | dbcbea6 | 2020-05-20 04:50:15 | [diff] [blame^] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "testing/gtest/include/gtest/gtest.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/files/file_path.h" |
| 9 | #include "base/native_library.h" |
| 10 | #include "base/path_service.h" |
| 11 | #include "build/build_config.h" |
| 12 | #include "gpu/vulkan/vulkan_cxx.h" |
| 13 | #include "gpu/vulkan/vulkan_function_pointers.h" |
| 14 | |
| 15 | namespace gpu { |
| 16 | |
| 17 | class VulkanCXXTest : public testing::Test { |
| 18 | public: |
| 19 | VulkanCXXTest() = default; |
| 20 | ~VulkanCXXTest() override = default; |
| 21 | |
| 22 | void SetUp() override { |
| 23 | use_swiftshader_ = |
| 24 | base::CommandLine::ForCurrentProcess()->HasSwitch("use-swiftshader"); |
| 25 | base::FilePath path; |
| 26 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_CHROMEOS) || \ |
| 27 | defined(OS_FUCHSIA) |
| 28 | if (use_swiftshader_) { |
| 29 | #if defined(OS_LINUX) |
| 30 | EXPECT_TRUE(base::PathService::Get(base::DIR_MODULE, &path)); |
| 31 | path = path.Append("libvk_swiftshader.so"); |
| 32 | #else |
| 33 | return; |
| 34 | #endif |
| 35 | } else { |
| 36 | path = base::FilePath("libvulkan.so.1"); |
| 37 | } |
| 38 | #elif defined(OS_WIN) |
| 39 | if (use_swiftshader_) { |
| 40 | EXPECT_TRUE(base::PathService::Get(base::DIR_MODULE, &path)); |
| 41 | path = path.Append(L"vk_swiftshader.dll"); |
| 42 | } else { |
| 43 | path = base::FilePath(L"vulkan-1.dll"); |
| 44 | } |
| 45 | #else |
| 46 | #error "Not supported platform" |
| 47 | #endif |
| 48 | |
| 49 | auto* vulkan_function_pointers = GetVulkanFunctionPointers(); |
| 50 | base::NativeLibraryLoadError native_library_load_error; |
| 51 | vulkan_function_pointers->vulkan_loader_library = |
| 52 | base::LoadNativeLibrary(path, &native_library_load_error); |
| 53 | EXPECT_TRUE(vulkan_function_pointers->vulkan_loader_library); |
| 54 | } |
| 55 | |
| 56 | void TearDown() override { |
| 57 | auto* vulkan_function_pointers = GetVulkanFunctionPointers(); |
| 58 | base::UnloadNativeLibrary(vulkan_function_pointers->vulkan_loader_library); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | bool use_swiftshader_ = false; |
| 63 | }; |
| 64 | |
| 65 | TEST_F(VulkanCXXTest, CreateInstanceUnique) { |
| 66 | auto* vulkan_function_pointers = GetVulkanFunctionPointers(); |
| 67 | EXPECT_TRUE(vulkan_function_pointers->BindUnassociatedFunctionPointers()); |
| 68 | |
| 69 | constexpr uint32_t kRequiredApiVersion = VK_MAKE_VERSION(1, 1, 0); |
| 70 | |
| 71 | vk::Result result; |
| 72 | uint32_t api_version; |
| 73 | std::tie(result, api_version) = vk::enumerateInstanceVersion(); |
| 74 | EXPECT_EQ(result, vk::Result::eSuccess); |
| 75 | EXPECT_GE(api_version, kRequiredApiVersion); |
| 76 | |
| 77 | vk::ApplicationInfo app_info("VulkanCXXTest", 0, nullptr, 0, |
| 78 | kRequiredApiVersion); |
| 79 | vk::InstanceCreateInfo instance_create_info({}, &app_info); |
| 80 | auto result_value = vk::createInstanceUnique(instance_create_info); |
| 81 | EXPECT_EQ(result_value.result, vk::Result::eSuccess); |
| 82 | |
| 83 | vk::UniqueInstance instance = std::move(result_value.value); |
| 84 | EXPECT_TRUE(instance); |
| 85 | |
| 86 | EXPECT_TRUE(vulkan_function_pointers->BindInstanceFunctionPointers( |
| 87 | instance.get(), kRequiredApiVersion, gfx::ExtensionSet())); |
| 88 | |
| 89 | instance.reset(); |
| 90 | } |
| 91 | |
| 92 | } // namespace gpu |