bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 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 | |
Robert Phillips | 08ba085 | 2019-05-22 20:23:43 +0000 | [diff] [blame] | 8 | #include "tools/gpu/vk/VkTestContext.h" |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 9 | |
| 10 | #ifdef SK_VULKAN |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/gpu/GrContext.h" |
| 13 | #include "include/gpu/vk/GrVkExtensions.h" |
| 14 | #include "tools/gpu/vk/VkTestUtils.h" |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 15 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 16 | namespace { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 17 | |
| 18 | #define ACQUIRE_VK_PROC(name, device) \ |
| 19 | f##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, nullptr, device)); \ |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 20 | SkASSERT(f##name) |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 21 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 22 | class VkTestContextImpl : public sk_gpu_test::VkTestContext { |
| 23 | public: |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 24 | static VkTestContext* Create(VkTestContext* sharedContext) { |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 25 | GrVkBackendContext backendContext; |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 26 | GrVkExtensions* extensions; |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 27 | VkPhysicalDeviceFeatures2* features; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 28 | bool ownsContext = true; |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 29 | VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE; |
Greg Daniel | a31f4e5 | 2018-08-01 16:48:52 -0400 | [diff] [blame] | 30 | PFN_vkDestroyDebugReportCallbackEXT destroyCallback = nullptr; |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 31 | if (sharedContext) { |
| 32 | backendContext = sharedContext->getVkBackendContext(); |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 33 | extensions = const_cast<GrVkExtensions*>(sharedContext->getVkExtensions()); |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 34 | features = const_cast<VkPhysicalDeviceFeatures2*>(sharedContext->getVkFeatures()); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 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; |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 38 | } else { |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 39 | PFN_vkGetInstanceProcAddr instProc; |
| 40 | PFN_vkGetDeviceProcAddr devProc; |
| 41 | if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) { |
| 42 | return nullptr; |
| 43 | } |
Greg Daniel | d3e65aa | 2018-08-01 09:19:45 -0400 | [diff] [blame] | 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 | }; |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 51 | extensions = new GrVkExtensions(); |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 52 | features = new VkPhysicalDeviceFeatures2; |
Greg Daniel | 74b1c01 | 2018-08-29 12:57:02 -0400 | [diff] [blame] | 53 | memset(features, 0, sizeof(VkPhysicalDeviceFeatures2)); |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 54 | if (!sk_gpu_test::CreateVkBackendContext(getProc, &backendContext, extensions, |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 55 | features, &debugCallback)) { |
| 56 | sk_gpu_test::FreeVulkanFeaturesStructs(features); |
| 57 | delete features; |
Greg Daniel | 24d861d | 2019-01-30 15:13:22 -0500 | [diff] [blame] | 58 | delete extensions; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 59 | return nullptr; |
| 60 | } |
Greg Daniel | a31f4e5 | 2018-08-01 16:48:52 -0400 | [diff] [blame] | 61 | if (debugCallback != VK_NULL_HANDLE) { |
| 62 | destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc( |
| 63 | backendContext.fInstance, "vkDestroyDebugReportCallbackEXT"); |
| 64 | } |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 65 | } |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 66 | return new VkTestContextImpl(backendContext, extensions, features, ownsContext, |
| 67 | debugCallback, destroyCallback); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 68 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 69 | |
| 70 | ~VkTestContextImpl() override { this->teardown(); } |
| 71 | |
| 72 | void testAbandon() override {} |
| 73 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 74 | void finish() override {} |
| 75 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 76 | sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override { |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 77 | return GrContext::MakeVulkan(fVk, options); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 78 | } |
| 79 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 80 | protected: |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 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) |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 90 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 91 | void teardown() override { |
| 92 | INHERITED::teardown(); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 93 | fVk.fMemoryAllocator.reset(); |
| 94 | if (fOwnsContext) { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 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); |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 100 | #ifdef SK_ENABLE_VK_LAYERS |
| 101 | if (fDebugCallback != VK_NULL_HANDLE) { |
Greg Daniel | 24d861d | 2019-01-30 15:13:22 -0500 | [diff] [blame] | 102 | fDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr); |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 103 | } |
| 104 | #endif |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 105 | grVkDestroyInstance(fVk.fInstance, nullptr); |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 106 | delete fExtensions; |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 107 | |
| 108 | sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures); |
| 109 | delete fFeatures; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 110 | } |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 111 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 112 | |
| 113 | private: |
Greg Daniel | 98bffae | 2018-08-01 13:25:41 -0400 | [diff] [blame] | 114 | VkTestContextImpl(const GrVkBackendContext& backendContext, const GrVkExtensions* extensions, |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 115 | VkPhysicalDeviceFeatures2* features, bool ownsContext, |
| 116 | VkDebugReportCallbackEXT debugCallback, |
Greg Daniel | a31f4e5 | 2018-08-01 16:48:52 -0400 | [diff] [blame] | 117 | PFN_vkDestroyDebugReportCallbackEXT destroyCallback) |
Greg Daniel | a0651ac | 2018-08-08 09:23:18 -0400 | [diff] [blame] | 118 | : VkTestContext(backendContext, extensions, features, ownsContext, debugCallback, |
Greg Daniel | a31f4e5 | 2018-08-01 16:48:52 -0400 | [diff] [blame] | 119 | destroyCallback) { |
Greg Daniel | 02497d4 | 2020-02-21 15:46:27 -0500 | [diff] [blame^] | 120 | fFenceSupport = true; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Robert Phillips | edf3f38 | 2020-02-13 12:59:19 -0500 | [diff] [blame] | 123 | void onPlatformMakeNotCurrent() const override {} |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 124 | void onPlatformMakeCurrent() const override {} |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 125 | std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 126 | |
| 127 | typedef sk_gpu_test::VkTestContext INHERITED; |
| 128 | }; |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 129 | } // anonymous namespace |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 130 | |
| 131 | namespace sk_gpu_test { |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 132 | VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) { |
| 133 | return VkTestContextImpl::Create(sharedContext); |
| 134 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 135 | } // namespace sk_gpu_test |
| 136 | |
| 137 | #endif |