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" |
| 13 | #include "include/gpu/GrContext.h" |
| 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 | |
| 45 | fExtensions = new GrVkExtensions(); |
| 46 | fFeatures = new VkPhysicalDeviceFeatures2; |
| 47 | memset(fFeatures, 0, sizeof(VkPhysicalDeviceFeatures2)); |
| 48 | fFeatures->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 49 | fFeatures->pNext = nullptr; |
| 50 | |
| 51 | fBackendContext.fInstance = VK_NULL_HANDLE; |
| 52 | fBackendContext.fDevice = VK_NULL_HANDLE; |
| 53 | |
| 54 | if (!sk_gpu_test::CreateVkBackendContext(getProc, &fBackendContext, fExtensions, |
| 55 | fFeatures, &fDebugCallback, nullptr, |
| 56 | sk_gpu_test::CanPresentFn(), fIsProtected)) { |
| 57 | return false; |
| 58 | } |
| 59 | fDevice = fBackendContext.fDevice; |
| 60 | |
| 61 | if (fDebugCallback != VK_NULL_HANDLE) { |
| 62 | fDestroyDebugCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc( |
| 63 | fBackendContext.fInstance, "vkDestroyDebugReportCallbackEXT"); |
| 64 | } |
| 65 | ACQUIRE_INST_VK_PROC(DestroyInstance) |
| 66 | ACQUIRE_INST_VK_PROC(DeviceWaitIdle) |
| 67 | ACQUIRE_INST_VK_PROC(DestroyDevice) |
| 68 | |
| 69 | fGrContext = GrContext::MakeVulkan(fBackendContext); |
| 70 | if (!fGrContext) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void VkTestHelper::cleanup() { |
| 78 | fGrContext.reset(); |
| 79 | |
| 80 | fBackendContext.fMemoryAllocator.reset(); |
| 81 | if (fDevice != VK_NULL_HANDLE) { |
| 82 | fVkDeviceWaitIdle(fDevice); |
| 83 | fVkDestroyDevice(fDevice, nullptr); |
| 84 | fDevice = VK_NULL_HANDLE; |
| 85 | } |
| 86 | if (fDebugCallback != VK_NULL_HANDLE) { |
| 87 | fDestroyDebugCallback(fBackendContext.fInstance, fDebugCallback, nullptr); |
| 88 | } |
| 89 | |
| 90 | if (fBackendContext.fInstance != VK_NULL_HANDLE) { |
| 91 | fVkDestroyInstance(fBackendContext.fInstance, nullptr); |
| 92 | fBackendContext.fInstance = VK_NULL_HANDLE; |
| 93 | } |
| 94 | |
| 95 | delete fExtensions; |
| 96 | |
| 97 | sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures); |
| 98 | delete fFeatures; |
| 99 | } |
| 100 | |
| 101 | #endif // SK_VULKAN |