• 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             PFN_vkGetDeviceProcAddr devProc;
41             if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
42                 return nullptr;
43             }
44             auto getProc = [instProc, devProc](const char* proc_name,
45                                                VkInstance instance, VkDevice device) {
46                 if (device != VK_NULL_HANDLE) {
47                     return devProc(device, proc_name);
48                 }
49                 return instProc(instance, proc_name);
50             };
51             extensions = new GrVkExtensions();
52             features = new VkPhysicalDeviceFeatures2;
53             memset(features, 0, sizeof(VkPhysicalDeviceFeatures2));
54             if (!sk_gpu_test::CreateVkBackendContext(getProc, &backendContext, extensions,
55                                                      features, &debugCallback)) {
56                 sk_gpu_test::FreeVulkanFeaturesStructs(features);
57                 delete features;
58                 delete extensions;
59                 return nullptr;
60             }
61             if (debugCallback != VK_NULL_HANDLE) {
62                 destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc(
63                         backendContext.fInstance, "vkDestroyDebugReportCallbackEXT");
64             }
65         }
66         return new VkTestContextImpl(backendContext, extensions, features, ownsContext,
67                                      debugCallback, destroyCallback);
68     }
69 
~VkTestContextImpl()70     ~VkTestContextImpl() override { this->teardown(); }
71 
testAbandon()72     void testAbandon() override {}
73 
finish()74     void finish() override {}
75 
makeContext(const GrContextOptions & options)76     sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override {
77         return GrDirectContext::MakeVulkan(fVk, options);
78     }
79 
80 protected:
81 #define ACQUIRE_VK_PROC_LOCAL(name, inst)                                            \
82     PFN_vk##name grVk##name =                                                        \
83             reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \
84     do {                                                                             \
85         if (grVk##name == nullptr) {                                                 \
86             SkDebugf("Function ptr for vk%s could not be acquired\n", #name);        \
87             return;                                                                  \
88         }                                                                            \
89     } while (0)
90 
teardown()91     void teardown() override {
92         INHERITED::teardown();
93         fVk.fMemoryAllocator.reset();
94         if (fOwnsContext) {
95             ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance);
96             ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance);
97             ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance);
98             grVkDeviceWaitIdle(fVk.fDevice);
99             grVkDestroyDevice(fVk.fDevice, nullptr);
100 #ifdef SK_ENABLE_VK_LAYERS
101             if (fDebugCallback != VK_NULL_HANDLE) {
102                 fDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr);
103             }
104 #endif
105             grVkDestroyInstance(fVk.fInstance, nullptr);
106             delete fExtensions;
107 
108             sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures);
109             delete fFeatures;
110         }
111     }
112 
113 private:
VkTestContextImpl(const GrVkBackendContext & backendContext,const GrVkExtensions * extensions,VkPhysicalDeviceFeatures2 * features,bool ownsContext,VkDebugReportCallbackEXT debugCallback,PFN_vkDestroyDebugReportCallbackEXT destroyCallback)114     VkTestContextImpl(const GrVkBackendContext& backendContext, const GrVkExtensions* extensions,
115                       VkPhysicalDeviceFeatures2* features, bool ownsContext,
116                       VkDebugReportCallbackEXT debugCallback,
117                       PFN_vkDestroyDebugReportCallbackEXT destroyCallback)
118             : VkTestContext(backendContext, extensions, features, ownsContext, 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