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