blob: d7e36161f79f89e20c19036bf583b272a23da452 [file] [log] [blame]
Robert Phillips0d6ce7c2020-06-11 08:51:50 -04001/*
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 Phillips2a4acf32020-07-06 15:50:15 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040014#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
31bool 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 Phillips85aa4282020-06-11 10:54:43 -040045 fFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
46 fFeatures.pNext = nullptr;
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040047
48 fBackendContext.fInstance = VK_NULL_HANDLE;
49 fBackendContext.fDevice = VK_NULL_HANDLE;
50
Robert Phillips85aa4282020-06-11 10:54:43 -040051 if (!sk_gpu_test::CreateVkBackendContext(getProc, &fBackendContext, &fExtensions,
52 &fFeatures, &fDebugCallback, nullptr,
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040053 sk_gpu_test::CanPresentFn(), fIsProtected)) {
54 return false;
55 }
56 fDevice = fBackendContext.fDevice;
57
58 if (fDebugCallback != VK_NULL_HANDLE) {
Robert Phillips85aa4282020-06-11 10:54:43 -040059 fDestroyDebugCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
60 instProc(fBackendContext.fInstance, "vkDestroyDebugReportCallbackEXT"));
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040061 }
62 ACQUIRE_INST_VK_PROC(DestroyInstance)
63 ACQUIRE_INST_VK_PROC(DeviceWaitIdle)
64 ACQUIRE_INST_VK_PROC(DestroyDevice)
65
Robert Phillips85aa4282020-06-11 10:54:43 -040066 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 Phillips2a4acf32020-07-06 15:50:15 -040080 // CONTEXT TODO: MakeVulkan should return an sk_sp<GrDirectContext>
81 auto tmp = GrContext::MakeVulkan(fBackendContext);
82 if (tmp) {
83 fDirectContext = sk_ref_sp<GrDirectContext>(tmp->asDirectContext());
84 }
85 if (!fDirectContext) {
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040086 return false;
87 }
88
89 return true;
90}
91
92void VkTestHelper::cleanup() {
Robert Phillips2a4acf32020-07-06 15:50:15 -040093 fDirectContext.reset();
Robert Phillips0d6ce7c2020-06-11 08:51:50 -040094
95 fBackendContext.fMemoryAllocator.reset();
96 if (fDevice != VK_NULL_HANDLE) {
97 fVkDeviceWaitIdle(fDevice);
98 fVkDestroyDevice(fDevice, nullptr);
99 fDevice = VK_NULL_HANDLE;
100 }
101 if (fDebugCallback != VK_NULL_HANDLE) {
102 fDestroyDebugCallback(fBackendContext.fInstance, fDebugCallback, nullptr);
103 }
104
105 if (fBackendContext.fInstance != VK_NULL_HANDLE) {
106 fVkDestroyInstance(fBackendContext.fInstance, nullptr);
107 fBackendContext.fInstance = VK_NULL_HANDLE;
108 }
109
Robert Phillips85aa4282020-06-11 10:54:43 -0400110 sk_gpu_test::FreeVulkanFeaturesStructs(&fFeatures);
Robert Phillips0d6ce7c2020-06-11 08:51:50 -0400111}
112
113#endif // SK_VULKAN