| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 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/graphite/vk/GraphiteVulkanTestContext.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/Context.h" |
| 11 |
#include "include/gpu/graphite/ContextOptions.h" |
| 12 |
#include "include/gpu/graphite/vk/VulkanGraphiteContext.h" |
| 13 |
#include "include/gpu/vk/VulkanExtensions.h" |
| 14 |
#include "include/gpu/vk/VulkanMemoryAllocator.h" |
| 15 |
#include "src/gpu/graphite/ContextOptionsPriv.h" |
| 16 |
#include "tools/gpu/ContextType.h" |
| 17 |
#include "tools/gpu/vk/VkTestUtils.h" |
| 18 |
#include "tools/graphite/TestOptions.h" |
| 19 |
|
| 20 |
// gCreateProtectedContext lives in GrContextFactory.cpp and so isn't defined if !SK_GANESH |
| 21 |
#if defined(SK_GANESH) |
| 22 |
extern bool gCreateProtectedContext; |
| 23 |
static const bool& createProtectedContext = gCreateProtectedContext; |
| 24 |
#else |
| 25 |
static constexpr bool createProtectedContext = false; |
| 26 |
#endif |
| 27 |
|
| 28 |
namespace skiatest::graphite { |
| 29 |
|
| 30 |
std::unique_ptr<GraphiteTestContext> VulkanTestContext::Make() { |
| 31 |
skgpu::VulkanBackendContext backendContext; |
| 32 |
skgpu::VulkanExtensions* extensions; |
| 33 |
sk_gpu_test::TestVkFeatures* features; |
| 34 |
VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE; |
| 35 |
PFN_vkDestroyDebugUtilsMessengerEXT destroyCallback = nullptr; |
| 36 |
|
| 37 |
PFN_vkGetInstanceProcAddr instProc; |
| 38 |
if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) { |
| 39 |
return nullptr; |
| 40 |
} |
| 41 |
|
| 42 |
extensions = new skgpu::VulkanExtensions(); |
| 43 |
features = new sk_gpu_test::TestVkFeatures; |
| 44 |
memset(&features->deviceFeatures, 0, sizeof(VkPhysicalDeviceFeatures2)); |
| 45 |
if (!sk_gpu_test::CreateVkBackendContext(instProc, |
| 46 |
&backendContext, |
| 47 |
extensions, |
| 48 |
features, |
| 49 |
&debugMessenger, |
| 50 |
nullptr, |
| 51 |
sk_gpu_test::CanPresentFn(), |
| 52 |
createProtectedContext)) { |
| 53 |
delete features; |
| 54 |
delete extensions; |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
if (debugMessenger != VK_NULL_HANDLE) { |
| 58 |
destroyCallback = (PFN_vkDestroyDebugUtilsMessengerEXT)instProc( |
| 59 |
backendContext.fInstance, "vkDestroyDebugUtilsMessengerEXT"); |
| 60 |
} |
| 61 |
|
| 62 |
return std::unique_ptr<GraphiteTestContext>(new VulkanTestContext( |
| 63 |
backendContext, extensions, features, debugMessenger, destroyCallback)); |
| 64 |
} |
| 65 |
|
| 66 |
#define ACQUIRE_VK_PROC_LOCAL(name, inst) \ |
| 67 |
PFN_vk##name localVk##name = \ |
| 68 |
reinterpret_cast<PFN_vk##name>(fVulkan.fGetProc("vk" #name, inst, nullptr)); \ |
| 69 |
do { \ |
| 70 |
if (localVk##name == nullptr) { \ |
| 71 |
SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 72 |
return; \ |
| 73 |
} \ |
| 74 |
} while (0) |
| 75 |
|
| 76 |
VulkanTestContext::~VulkanTestContext() { |
| 77 |
fVulkan.fMemoryAllocator.reset(); |
| 78 |
ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVulkan.fInstance); |
| 79 |
ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVulkan.fInstance); |
| 80 |
ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVulkan.fInstance); |
| 81 |
localVkDeviceWaitIdle(fVulkan.fDevice); |
| 82 |
localVkDestroyDevice(fVulkan.fDevice, nullptr); |
| 83 |
#ifdef SK_ENABLE_VK_LAYERS |
| 84 |
if (fDebugMessenger != VK_NULL_HANDLE) { |
| 85 |
fDestroyDebugUtilsMessengerEXT(fVulkan.fInstance, fDebugMessenger, nullptr); |
| 86 |
} |
| 87 |
#else |
| 88 |
// Surpress unused private member variable warning |
| 89 |
(void)fDebugMessenger; |
| 90 |
(void)fDestroyDebugUtilsMessengerEXT; |
| 91 |
#endif |
| 92 |
localVkDestroyInstance(fVulkan.fInstance, nullptr); |
| 93 |
delete fExtensions; |
| 94 |
delete fFeatures; |
| 95 |
} |
| 96 |
|
| 97 |
skgpu::ContextType VulkanTestContext::contextType() { |
| 98 |
return skgpu::ContextType::kVulkan; |
| 99 |
} |
| 100 |
|
| 101 |
std::unique_ptr<skgpu::graphite::Context> VulkanTestContext::makeContext( |
| 102 |
const TestOptions& options) { |
| 103 |
SkASSERT(!options.hasDawnOptions()); |
| 104 |
skgpu::graphite::ContextOptions revisedContextOptions(options.fContextOptions); |
| 105 |
skgpu::graphite::ContextOptionsPriv contextOptionsPriv; |
| 106 |
if (!options.fContextOptions.fOptionsPriv) { |
| 107 |
revisedContextOptions.fOptionsPriv = &contextOptionsPriv; |
| 108 |
} |
| 109 |
// Needed to make synchronous readPixels work |
| 110 |
revisedContextOptions.fOptionsPriv->fStoreContextRefInRecorder = true; |
| 111 |
SkASSERT(fVulkan.fMemoryAllocator); |
| 112 |
return skgpu::graphite::ContextFactory::MakeVulkan(fVulkan, revisedContextOptions); |
| 113 |
} |
| 114 |
|
| 115 |
} // namespace skiatest::graphite |