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