xref: /aosp_15_r20/external/skia/tools/gpu/vk/VkTestContext.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 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/VkTestContext.h"
9 
10 #ifdef SK_VULKAN
11 
12 #include "include/gpu/ganesh/GrDirectContext.h"
13 #include "include/gpu/ganesh/vk/GrVkDirectContext.h"
14 #include "include/gpu/vk/VulkanBackendContext.h"
15 #include "include/gpu/vk/VulkanExtensions.h"
16 #include "include/gpu/vk/VulkanMemoryAllocator.h"
17 #include "tools/gpu/vk/VkTestUtils.h"
18 
19 extern bool gCreateProtectedContext;
20 
21 namespace {
22 
23 class VkTestContextImpl : public sk_gpu_test::VkTestContext {
24 public:
Create(VkTestContext * sharedContext)25     static VkTestContext* Create(VkTestContext* sharedContext) {
26         skgpu::VulkanBackendContext backendContext;
27         skgpu::VulkanExtensions* extensions;
28         VkPhysicalDeviceFeatures2* features;
29         bool ownsContext = true;
30         VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE;
31         PFN_vkDestroyDebugReportCallbackEXT destroyCallback = nullptr;
32         if (sharedContext) {
33             backendContext = sharedContext->getVkBackendContext();
34             extensions = const_cast<skgpu::VulkanExtensions*>(sharedContext->getVkExtensions());
35             features = const_cast<VkPhysicalDeviceFeatures2*>(sharedContext->getVkFeatures());
36             // We always delete the parent context last so make sure the child does not think they
37             // own the vulkan context.
38             ownsContext = false;
39         } else {
40             PFN_vkGetInstanceProcAddr instProc;
41             if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
42                 return nullptr;
43             }
44 
45             extensions = new skgpu::VulkanExtensions();
46             features = new VkPhysicalDeviceFeatures2;
47             memset(features, 0, sizeof(VkPhysicalDeviceFeatures2));
48             if (!sk_gpu_test::CreateVkBackendContext(instProc, &backendContext, extensions,
49                                                      features, &debugCallback,
50                                                      nullptr, sk_gpu_test::CanPresentFn(),
51                                                      gCreateProtectedContext)) {
52                 sk_gpu_test::FreeVulkanFeaturesStructs(features);
53                 delete features;
54                 delete extensions;
55                 return nullptr;
56             }
57             if (debugCallback != VK_NULL_HANDLE) {
58                 destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc(
59                         backendContext.fInstance, "vkDestroyDebugReportCallbackEXT");
60             }
61         }
62         return new VkTestContextImpl(backendContext, extensions, features, ownsContext,
63                                      debugCallback, destroyCallback);
64     }
65 
~VkTestContextImpl()66     ~VkTestContextImpl() override { this->teardown(); }
67 
testAbandon()68     void testAbandon() override {}
69 
makeContext(const GrContextOptions & options)70     sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override {
71         return GrDirectContexts::MakeVulkan(fVk, options);
72     }
73 
74 protected:
75 #define ACQUIRE_VK_PROC_LOCAL(name, inst)                                            \
76     PFN_vk##name grVk##name =                                                        \
77             reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \
78     do {                                                                             \
79         if (grVk##name == nullptr) {                                                 \
80             SkDebugf("Function ptr for vk%s could not be acquired\n", #name);        \
81             return;                                                                  \
82         }                                                                            \
83     } while (0)
84 
teardown()85     void teardown() override {
86         INHERITED::teardown();
87         fVk.fMemoryAllocator.reset();
88         if (fOwnsContext) {
89             ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance);
90             ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance);
91             ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance);
92             grVkDeviceWaitIdle(fVk.fDevice);
93             grVkDestroyDevice(fVk.fDevice, nullptr);
94 #ifdef SK_ENABLE_VK_LAYERS
95             if (fDebugCallback != VK_NULL_HANDLE) {
96                 fDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr);
97             }
98 #endif
99             grVkDestroyInstance(fVk.fInstance, nullptr);
100             delete fExtensions;
101 
102             sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures);
103             delete fFeatures;
104         }
105     }
106 
107 private:
VkTestContextImpl(const skgpu::VulkanBackendContext & backendContext,const skgpu::VulkanExtensions * extensions,VkPhysicalDeviceFeatures2 * features,bool ownsContext,VkDebugReportCallbackEXT debugCallback,PFN_vkDestroyDebugReportCallbackEXT destroyCallback)108     VkTestContextImpl(const skgpu::VulkanBackendContext& backendContext,
109                       const skgpu::VulkanExtensions* extensions,
110                       VkPhysicalDeviceFeatures2* features,
111                       bool ownsContext,
112                       VkDebugReportCallbackEXT debugCallback,
113                       PFN_vkDestroyDebugReportCallbackEXT destroyCallback)
114             : VkTestContext(backendContext,
115                             extensions,
116                             features,
117                             ownsContext,
118                             debugCallback,
119                             destroyCallback) {
120         fFenceSupport = true;
121     }
122 
onPlatformMakeNotCurrent() const123     void onPlatformMakeNotCurrent() const override {}
onPlatformMakeCurrent() const124     void onPlatformMakeCurrent() const override {}
onPlatformGetAutoContextRestore() const125     std::function<void()> onPlatformGetAutoContextRestore() const override  { return nullptr; }
126 
127     using INHERITED = sk_gpu_test::VkTestContext;
128 };
129 }  // anonymous namespace
130 
131 namespace sk_gpu_test {
CreatePlatformVkTestContext(VkTestContext * sharedContext)132 VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) {
133     return VkTestContextImpl::Create(sharedContext);
134 }
135 }  // namespace sk_gpu_test
136 
137 #endif
138