Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "tools/gpu/vk/VkTestHelper.h" |
| 9 | |
| 10 | #ifdef SK_VULKAN |
| 11 | |
| 12 | #include "include/core/SkSurface.h" |
Robert Phillips | 2a4acf3 | 2020-07-06 15:50:15 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 14 | #include "tools/gpu/vk/VkTestUtils.h" |
| 15 | |
| 16 | #define ACQUIRE_INST_VK_PROC(name) \ |
| 17 | fVk##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, fBackendContext.fInstance,\ |
| 18 | VK_NULL_HANDLE)); \ |
| 19 | if (fVk##name == nullptr) { \ |
| 20 | SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 21 | return false; \ |
| 22 | } |
| 23 | |
| 24 | #define ACQUIRE_DEVICE_VK_PROC(name) \ |
| 25 | fVk##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, VK_NULL_HANDLE, fDevice)); \ |
| 26 | if (fVk##name == nullptr) { \ |
| 27 | SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 28 | return false; \ |
| 29 | } |
| 30 | |
| 31 | bool VkTestHelper::init() { |
| 32 | PFN_vkGetInstanceProcAddr instProc; |
| 33 | PFN_vkGetDeviceProcAddr devProc; |
| 34 | if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) { |
| 35 | return false; |
| 36 | } |
| 37 | auto getProc = [&instProc, &devProc](const char* proc_name, |
| 38 | VkInstance instance, VkDevice device) { |
| 39 | if (device != VK_NULL_HANDLE) { |
| 40 | return devProc(device, proc_name); |
| 41 | } |
| 42 | return instProc(instance, proc_name); |
| 43 | }; |
| 44 | |
Robert Phillips | 85aa428 | 2020-06-11 10:54:43 -0400 | [diff] [blame] | 45 | fFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 46 | fFeatures.pNext = nullptr; |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 47 | |
| 48 | fBackendContext.fInstance = VK_NULL_HANDLE; |
| 49 | fBackendContext.fDevice = VK_NULL_HANDLE; |
| 50 | |
Robert Phillips | 85aa428 | 2020-06-11 10:54:43 -0400 | [diff] [blame] | 51 | if (!sk_gpu_test::CreateVkBackendContext(getProc, &fBackendContext, &fExtensions, |
| 52 | &fFeatures, &fDebugCallback, nullptr, |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 53 | sk_gpu_test::CanPresentFn(), fIsProtected)) { |
| 54 | return false; |
| 55 | } |
| 56 | fDevice = fBackendContext.fDevice; |
| 57 | |
| 58 | if (fDebugCallback != VK_NULL_HANDLE) { |
Robert Phillips | 85aa428 | 2020-06-11 10:54:43 -0400 | [diff] [blame] | 59 | fDestroyDebugCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 60 | instProc(fBackendContext.fInstance, "vkDestroyDebugReportCallbackEXT")); |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 61 | } |
| 62 | ACQUIRE_INST_VK_PROC(DestroyInstance) |
| 63 | ACQUIRE_INST_VK_PROC(DeviceWaitIdle) |
| 64 | ACQUIRE_INST_VK_PROC(DestroyDevice) |
| 65 | |
Robert Phillips | 85aa428 | 2020-06-11 10:54:43 -0400 | [diff] [blame] | 66 | ACQUIRE_INST_VK_PROC(GetPhysicalDeviceFormatProperties) |
| 67 | ACQUIRE_INST_VK_PROC(GetPhysicalDeviceMemoryProperties) |
| 68 | |
| 69 | ACQUIRE_DEVICE_VK_PROC(CreateImage) |
| 70 | ACQUIRE_DEVICE_VK_PROC(DestroyImage) |
| 71 | ACQUIRE_DEVICE_VK_PROC(GetImageMemoryRequirements) |
| 72 | ACQUIRE_DEVICE_VK_PROC(AllocateMemory) |
| 73 | ACQUIRE_DEVICE_VK_PROC(FreeMemory) |
| 74 | ACQUIRE_DEVICE_VK_PROC(BindImageMemory) |
| 75 | ACQUIRE_DEVICE_VK_PROC(MapMemory) |
| 76 | ACQUIRE_DEVICE_VK_PROC(UnmapMemory) |
| 77 | ACQUIRE_DEVICE_VK_PROC(FlushMappedMemoryRanges) |
| 78 | ACQUIRE_DEVICE_VK_PROC(GetImageSubresourceLayout) |
| 79 | |
Robert Phillips | f4f8011 | 2020-07-13 16:13:31 -0400 | [diff] [blame] | 80 | fDirectContext = GrDirectContext::MakeVulkan(fBackendContext); |
Robert Phillips | 2a4acf3 | 2020-07-06 15:50:15 -0400 | [diff] [blame] | 81 | if (!fDirectContext) { |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | void VkTestHelper::cleanup() { |
Brian Salomon | 7205080 | 2020-10-12 20:45:06 -0400 | [diff] [blame] | 89 | // Make sure any work, release procs, etc left on the context are finished with before we start |
| 90 | // tearing everything down. |
| 91 | if (fDirectContext) { |
| 92 | fDirectContext->flushAndSubmit(true); |
| 93 | } |
| 94 | |
Robert Phillips | 2a4acf3 | 2020-07-06 15:50:15 -0400 | [diff] [blame] | 95 | fDirectContext.reset(); |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 96 | |
| 97 | fBackendContext.fMemoryAllocator.reset(); |
| 98 | if (fDevice != VK_NULL_HANDLE) { |
| 99 | fVkDeviceWaitIdle(fDevice); |
| 100 | fVkDestroyDevice(fDevice, nullptr); |
| 101 | fDevice = VK_NULL_HANDLE; |
| 102 | } |
| 103 | if (fDebugCallback != VK_NULL_HANDLE) { |
| 104 | fDestroyDebugCallback(fBackendContext.fInstance, fDebugCallback, nullptr); |
| 105 | } |
| 106 | |
| 107 | if (fBackendContext.fInstance != VK_NULL_HANDLE) { |
| 108 | fVkDestroyInstance(fBackendContext.fInstance, nullptr); |
| 109 | fBackendContext.fInstance = VK_NULL_HANDLE; |
| 110 | } |
| 111 | |
Robert Phillips | 85aa428 | 2020-06-11 10:54:43 -0400 | [diff] [blame] | 112 | sk_gpu_test::FreeVulkanFeaturesStructs(&fFeatures); |
Robert Phillips | 0d6ce7c | 2020-06-11 08:51:50 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #endif // SK_VULKAN |