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