| 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/ganesh/vk/VkTestContext.h" |
| 9 |
|
| 10 |
#ifdef SK_VULKAN |
| 11 |
|
| 12 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 13 |
#include "include/gpu/ganesh/vk/GrVkDirectContext.h" |
| 14 |
#include "include/gpu/vk/VulkanBackendContext.h" |
| 15 |
#include "include/gpu/vk/VulkanExtensions.h" |
| 16 |
#include "include/gpu/vk/VulkanMemoryAllocator.h" |
| 17 |
#include "tools/gpu/vk/VkTestUtils.h" |
| 18 |
|
| 19 |
// gCreateProtectedContext lives in GrContextFactory.cpp and so isn't defined if !SK_GANESH |
| 20 |
#if defined(SK_GANESH) |
| 21 |
extern bool gCreateProtectedContext; |
| 22 |
static const bool& createProtectedContext = gCreateProtectedContext; |
| 23 |
#else |
| 24 |
static constexpr bool createProtectedContext = false; |
| 25 |
#endif |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
class VkTestContextImpl : public sk_gpu_test::VkTestContext { |
| 30 |
public: |
| 31 |
static VkTestContext* Create(VkTestContext* sharedContext) { |
| 32 |
skgpu::VulkanBackendContext backendContext; |
| 33 |
const skgpu::VulkanExtensions* extensions; |
| 34 |
const sk_gpu_test::TestVkFeatures* features; |
| 35 |
bool ownsContext = true; |
| 36 |
VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE; |
| 37 |
PFN_vkDestroyDebugUtilsMessengerEXT destroyCallback = nullptr; |
| 38 |
if (sharedContext) { |
| 39 |
backendContext = sharedContext->getVkBackendContext(); |
| 40 |
extensions = sharedContext->getVkExtensions(); |
| 41 |
features = sharedContext->getVkFeatures(); |
| 42 |
// We always delete the parent context last so make sure the child does not think they |
| 43 |
// own the vulkan context. |
| 44 |
ownsContext = false; |
| 45 |
} else { |
| 46 |
PFN_vkGetInstanceProcAddr instProc; |
| 47 |
if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) { |
| 48 |
return nullptr; |
| 49 |
} |
| 50 |
|
| 51 |
skgpu::VulkanExtensions* ownedExtensions = new skgpu::VulkanExtensions(); |
| 52 |
sk_gpu_test::TestVkFeatures* ownedFeatures = new sk_gpu_test::TestVkFeatures; |
| 53 |
extensions = ownedExtensions; |
| 54 |
features = ownedFeatures; |
| 55 |
if (!sk_gpu_test::CreateVkBackendContext(instProc, |
| 56 |
&backendContext, |
| 57 |
ownedExtensions, |
| 58 |
ownedFeatures, |
| 59 |
&debugMessenger, |
| 60 |
nullptr, |
| 61 |
sk_gpu_test::CanPresentFn(), |
| 62 |
createProtectedContext)) { |
| 63 |
delete ownedExtensions; |
| 64 |
delete ownedFeatures; |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
if (debugMessenger != VK_NULL_HANDLE) { |
| 68 |
destroyCallback = (PFN_vkDestroyDebugUtilsMessengerEXT)instProc( |
| 69 |
backendContext.fInstance, "vkDestroyDebugUtilsMessengerEXT"); |
| 70 |
} |
| 71 |
} |
| 72 |
return new VkTestContextImpl( |
| 73 |
backendContext, extensions, features, ownsContext, debugMessenger, destroyCallback); |
| 74 |
} |
| 75 |
|
| 76 |
~VkTestContextImpl() override { this->teardown(); } |
| 77 |
|
| 78 |
void testAbandon() override {} |
| 79 |
|
| 80 |
sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override { |
| 81 |
return GrDirectContexts::MakeVulkan(fVk, options); |
| 82 |
} |
| 83 |
|
| 84 |
protected: |
| 85 |
#define ACQUIRE_VK_PROC_LOCAL(name, inst) \ |
| 86 |
PFN_vk##name grVk##name = \ |
| 87 |
reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \ |
| 88 |
do { \ |
| 89 |
if (grVk##name == nullptr) { \ |
| 90 |
SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 91 |
return; \ |
| 92 |
} \ |
| 93 |
} while (0) |
| 94 |
|
| 95 |
void teardown() override { |
| 96 |
INHERITED::teardown(); |
| 97 |
fVk.fMemoryAllocator.reset(); |
| 98 |
if (fOwnsContext) { |
| 99 |
ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance); |
| 100 |
ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance); |
| 101 |
ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance); |
| 102 |
grVkDeviceWaitIdle(fVk.fDevice); |
| 103 |
grVkDestroyDevice(fVk.fDevice, nullptr); |
| 104 |
#ifdef SK_ENABLE_VK_LAYERS |
| 105 |
if (fDebugMessenger != VK_NULL_HANDLE) { |
| 106 |
fDestroyDebugUtilsMessengerEXT(fVk.fInstance, fDebugMessenger, nullptr); |
| 107 |
} |
| 108 |
#endif |
| 109 |
grVkDestroyInstance(fVk.fInstance, nullptr); |
| 110 |
|
| 111 |
delete fExtensions; |
| 112 |
delete fFeatures; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private: |
| 117 |
VkTestContextImpl(const skgpu::VulkanBackendContext& backendContext, |
| 118 |
const skgpu::VulkanExtensions* extensions, |
| 119 |
const sk_gpu_test::TestVkFeatures* features, |
| 120 |
bool ownsContext, |
| 121 |
VkDebugUtilsMessengerEXT debugMessenger, |
| 122 |
PFN_vkDestroyDebugUtilsMessengerEXT destroyCallback) |
| 123 |
: VkTestContext(backendContext, |
| 124 |
extensions, |
| 125 |
features, |
| 126 |
ownsContext, |
| 127 |
debugMessenger, |
| 128 |
destroyCallback) { |
| 129 |
fFenceSupport = true; |
| 130 |
} |
| 131 |
|
| 132 |
void onPlatformMakeNotCurrent() const override {} |
| 133 |
void onPlatformMakeCurrent() const override {} |
| 134 |
std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; } |
| 135 |
|
| 136 |
using INHERITED = sk_gpu_test::VkTestContext; |
| 137 |
}; |
| 138 |
} // anonymous namespace |
| 139 |
|
| 140 |
namespace sk_gpu_test { |
| 141 |
VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) { |
| 142 |
return VkTestContextImpl::Create(sharedContext); |
| 143 |
} |
| 144 |
} // namespace sk_gpu_test |
| 145 |
|
| 146 |
#endif |