blob: 3fa461dd3929990e3e0e79ff74cc7f369847e216 [file] [log] [blame]
bsalomon18a2f9d2016-05-11 10:09:18 -07001/*
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
Robert Phillips08ba0852019-05-22 20:23:43 +00008#include "tools/gpu/vk/VkTestContext.h"
bsalomon18a2f9d2016-05-11 10:09:18 -07009
10#ifdef SK_VULKAN
11
Robert Phillipsf4f80112020-07-13 16:13:31 -040012#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/gpu/vk/GrVkExtensions.h"
14#include "tools/gpu/vk/VkTestUtils.h"
bsalomonedea94c2016-05-16 14:09:56 -070015
bsalomon18a2f9d2016-05-11 10:09:18 -070016namespace {
Greg Danielc8cd45a2018-07-12 10:02:37 -040017
18#define ACQUIRE_VK_PROC(name, device) \
19 f##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, nullptr, device)); \
Brian Salomon23356442018-11-30 15:33:19 -050020 SkASSERT(f##name)
Greg Danielc8cd45a2018-07-12 10:02:37 -040021
bsalomon18a2f9d2016-05-11 10:09:18 -070022class VkTestContextImpl : public sk_gpu_test::VkTestContext {
23public:
Greg Daniel604b1972017-05-15 13:50:35 -040024 static VkTestContext* Create(VkTestContext* sharedContext) {
Greg Danielf730c182018-07-02 20:15:37 +000025 GrVkBackendContext backendContext;
Greg Daniel98bffae2018-08-01 13:25:41 -040026 GrVkExtensions* extensions;
Greg Daniela0651ac2018-08-08 09:23:18 -040027 VkPhysicalDeviceFeatures2* features;
Greg Danielf730c182018-07-02 20:15:37 +000028 bool ownsContext = true;
Greg Daniel37329b32018-07-02 20:16:44 +000029 VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE;
Greg Daniela31f4e52018-08-01 16:48:52 -040030 PFN_vkDestroyDebugReportCallbackEXT destroyCallback = nullptr;
Greg Daniel604b1972017-05-15 13:50:35 -040031 if (sharedContext) {
32 backendContext = sharedContext->getVkBackendContext();
Greg Daniel98bffae2018-08-01 13:25:41 -040033 extensions = const_cast<GrVkExtensions*>(sharedContext->getVkExtensions());
Greg Daniela0651ac2018-08-08 09:23:18 -040034 features = const_cast<VkPhysicalDeviceFeatures2*>(sharedContext->getVkFeatures());
Greg Danielf730c182018-07-02 20:15:37 +000035 // We always delete the parent context last so make sure the child does not think they
36 // own the vulkan context.
37 ownsContext = false;
Greg Daniel604b1972017-05-15 13:50:35 -040038 } else {
Greg Daniel35970ec2017-11-10 10:03:05 -050039 PFN_vkGetInstanceProcAddr instProc;
40 PFN_vkGetDeviceProcAddr devProc;
41 if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
42 return nullptr;
43 }
Greg Danield3e65aa2018-08-01 09:19:45 -040044 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 };
Greg Daniel98bffae2018-08-01 13:25:41 -040051 extensions = new GrVkExtensions();
Greg Daniela0651ac2018-08-08 09:23:18 -040052 features = new VkPhysicalDeviceFeatures2;
Greg Daniel74b1c012018-08-29 12:57:02 -040053 memset(features, 0, sizeof(VkPhysicalDeviceFeatures2));
Greg Daniel98bffae2018-08-01 13:25:41 -040054 if (!sk_gpu_test::CreateVkBackendContext(getProc, &backendContext, extensions,
Greg Daniela0651ac2018-08-08 09:23:18 -040055 features, &debugCallback)) {
56 sk_gpu_test::FreeVulkanFeaturesStructs(features);
57 delete features;
Greg Daniel24d861d2019-01-30 15:13:22 -050058 delete extensions;
Greg Danielf730c182018-07-02 20:15:37 +000059 return nullptr;
60 }
Greg Daniela31f4e52018-08-01 16:48:52 -040061 if (debugCallback != VK_NULL_HANDLE) {
62 destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc(
63 backendContext.fInstance, "vkDestroyDebugReportCallbackEXT");
64 }
Greg Daniel604b1972017-05-15 13:50:35 -040065 }
Greg Daniela0651ac2018-08-08 09:23:18 -040066 return new VkTestContextImpl(backendContext, extensions, features, ownsContext,
67 debugCallback, destroyCallback);
bsalomonedea94c2016-05-16 14:09:56 -070068 }
bsalomon18a2f9d2016-05-11 10:09:18 -070069
70 ~VkTestContextImpl() override { this->teardown(); }
71
72 void testAbandon() override {}
73
bsalomonc8699322016-05-11 11:55:36 -070074 void finish() override {}
75
Robert Phillipsf4f80112020-07-13 16:13:31 -040076 sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override {
77 return GrDirectContext::MakeVulkan(fVk, options);
Greg Daniel02611d92017-07-25 10:05:01 -040078 }
79
bsalomon18a2f9d2016-05-11 10:09:18 -070080protected:
Brian Salomon23356442018-11-30 15:33:19 -050081#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)
Greg Danielc8cd45a2018-07-12 10:02:37 -040090
bsalomonedea94c2016-05-16 14:09:56 -070091 void teardown() override {
92 INHERITED::teardown();
Greg Danielf730c182018-07-02 20:15:37 +000093 fVk.fMemoryAllocator.reset();
94 if (fOwnsContext) {
Greg Danielc8cd45a2018-07-12 10:02:37 -040095 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);
Greg Daniel37329b32018-07-02 20:16:44 +0000100#ifdef SK_ENABLE_VK_LAYERS
101 if (fDebugCallback != VK_NULL_HANDLE) {
Greg Daniel24d861d2019-01-30 15:13:22 -0500102 fDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr);
Greg Daniel37329b32018-07-02 20:16:44 +0000103 }
104#endif
Greg Danielc8cd45a2018-07-12 10:02:37 -0400105 grVkDestroyInstance(fVk.fInstance, nullptr);
Greg Daniel98bffae2018-08-01 13:25:41 -0400106 delete fExtensions;
Greg Daniela0651ac2018-08-08 09:23:18 -0400107
108 sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures);
109 delete fFeatures;
Greg Danielf730c182018-07-02 20:15:37 +0000110 }
bsalomonedea94c2016-05-16 14:09:56 -0700111 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700112
113private:
Greg Daniel98bffae2018-08-01 13:25:41 -0400114 VkTestContextImpl(const GrVkBackendContext& backendContext, const GrVkExtensions* extensions,
Greg Daniela0651ac2018-08-08 09:23:18 -0400115 VkPhysicalDeviceFeatures2* features, bool ownsContext,
116 VkDebugReportCallbackEXT debugCallback,
Greg Daniela31f4e52018-08-01 16:48:52 -0400117 PFN_vkDestroyDebugReportCallbackEXT destroyCallback)
Greg Daniela0651ac2018-08-08 09:23:18 -0400118 : VkTestContext(backendContext, extensions, features, ownsContext, debugCallback,
Greg Daniela31f4e52018-08-01 16:48:52 -0400119 destroyCallback) {
Greg Daniel02497d42020-02-21 15:46:27 -0500120 fFenceSupport = true;
bsalomonedea94c2016-05-16 14:09:56 -0700121 }
122
Robert Phillipsedf3f382020-02-13 12:59:19 -0500123 void onPlatformMakeNotCurrent() const override {}
bsalomon18a2f9d2016-05-11 10:09:18 -0700124 void onPlatformMakeCurrent() const override {}
Brian Salomon55ad7742017-11-17 09:25:23 -0500125 std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
bsalomon18a2f9d2016-05-11 10:09:18 -0700126
John Stiles7571f9e2020-09-02 22:42:33 -0400127 using INHERITED = sk_gpu_test::VkTestContext;
bsalomon18a2f9d2016-05-11 10:09:18 -0700128};
csmartdalton421a3c12016-10-04 11:08:45 -0700129} // anonymous namespace
bsalomon18a2f9d2016-05-11 10:09:18 -0700130
131namespace sk_gpu_test {
Greg Daniel604b1972017-05-15 13:50:35 -0400132VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) {
133 return VkTestContextImpl::Create(sharedContext);
134}
bsalomon18a2f9d2016-05-11 10:09:18 -0700135} // namespace sk_gpu_test
136
137#endif