• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrDirectContext.h"
14 #include "tools/gpu/vk/VkTestUtils.h"
15 
16 #define ACQUIRE_INST_VK_PROC(name)                                                               \
17     fVk##name = reinterpret_cast<PFN_vk##name>(instProc(fBackendContext.fInstance, "vk" #name)); \
18     if (fVk##name == nullptr) {                                                                  \
19         SkDebugf("Function ptr for vk%s could not be acquired\n", #name);                        \
20         return false;                                                                            \
21     }
22 
23 #define ACQUIRE_DEVICE_VK_PROC(name)                                                          \
24     fVk##name = reinterpret_cast<PFN_vk##name>(fVkGetDeviceProcAddr(fDevice, "vk" #name));    \
25     if (fVk##name == nullptr) {                                                               \
26         SkDebugf("Function ptr for vk%s could not be acquired\n", #name);                     \
27         return false;                                                                         \
28     }
29 
init()30 bool VkTestHelper::init() {
31     PFN_vkGetInstanceProcAddr instProc;
32     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
33         return false;
34     }
35 
36     fFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
37     fFeatures.pNext = nullptr;
38 
39     fBackendContext.fInstance = VK_NULL_HANDLE;
40     fBackendContext.fDevice = VK_NULL_HANDLE;
41 
42     if (!sk_gpu_test::CreateVkBackendContext(instProc, &fBackendContext, &fExtensions,
43                                              &fFeatures, &fDebugCallback, nullptr,
44                                              sk_gpu_test::CanPresentFn(), fIsProtected)) {
45         return false;
46     }
47     fDevice = fBackendContext.fDevice;
48 
49     if (fDebugCallback != VK_NULL_HANDLE) {
50         fDestroyDebugCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
51                 instProc(fBackendContext.fInstance, "vkDestroyDebugReportCallbackEXT"));
52     }
53     ACQUIRE_INST_VK_PROC(DestroyInstance)
54     ACQUIRE_INST_VK_PROC(DeviceWaitIdle)
55     ACQUIRE_INST_VK_PROC(DestroyDevice)
56 
57     ACQUIRE_INST_VK_PROC(GetPhysicalDeviceFormatProperties)
58     ACQUIRE_INST_VK_PROC(GetPhysicalDeviceMemoryProperties)
59 
60     ACQUIRE_INST_VK_PROC(GetDeviceProcAddr)
61 
62     ACQUIRE_DEVICE_VK_PROC(CreateImage)
63     ACQUIRE_DEVICE_VK_PROC(DestroyImage)
64     ACQUIRE_DEVICE_VK_PROC(GetImageMemoryRequirements)
65     ACQUIRE_DEVICE_VK_PROC(AllocateMemory)
66     ACQUIRE_DEVICE_VK_PROC(FreeMemory)
67     ACQUIRE_DEVICE_VK_PROC(BindImageMemory)
68     ACQUIRE_DEVICE_VK_PROC(MapMemory)
69     ACQUIRE_DEVICE_VK_PROC(UnmapMemory)
70     ACQUIRE_DEVICE_VK_PROC(FlushMappedMemoryRanges)
71     ACQUIRE_DEVICE_VK_PROC(GetImageSubresourceLayout)
72 
73     fDirectContext = GrDirectContext::MakeVulkan(fBackendContext);
74     if (!fDirectContext) {
75         return false;
76     }
77 
78     return true;
79 }
80 
cleanup()81 void VkTestHelper::cleanup() {
82     // Make sure any work, release procs, etc left on the context are finished with before we start
83     // tearing everything down.
84     if (fDirectContext) {
85         fDirectContext->flushAndSubmit(true);
86     }
87 
88     fDirectContext.reset();
89 
90     fBackendContext.fMemoryAllocator.reset();
91     if (fDevice != VK_NULL_HANDLE) {
92         fVkDeviceWaitIdle(fDevice);
93         fVkDestroyDevice(fDevice, nullptr);
94         fDevice = VK_NULL_HANDLE;
95     }
96     if (fDebugCallback != VK_NULL_HANDLE) {
97         fDestroyDebugCallback(fBackendContext.fInstance, fDebugCallback, nullptr);
98     }
99 
100     if (fBackendContext.fInstance != VK_NULL_HANDLE) {
101         fVkDestroyInstance(fBackendContext.fInstance, nullptr);
102         fBackendContext.fInstance = VK_NULL_HANDLE;
103     }
104 
105     sk_gpu_test::FreeVulkanFeaturesStructs(&fFeatures);
106 }
107 
108 #endif // SK_VULKAN
109