1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "tools/gpu/vk/VkTestContext.h" 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#ifdef SK_VULKAN 11cb93a386Sopenharmony_ci 12cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h" 13cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkExtensions.h" 14cb93a386Sopenharmony_ci#include "tools/gpu/vk/VkTestUtils.h" 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_cinamespace { 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_ci#define ACQUIRE_VK_PROC(name, device) \ 19cb93a386Sopenharmony_ci f##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, nullptr, device)); \ 20cb93a386Sopenharmony_ci SkASSERT(f##name) 21cb93a386Sopenharmony_ci 22cb93a386Sopenharmony_ciclass VkTestContextImpl : public sk_gpu_test::VkTestContext { 23cb93a386Sopenharmony_cipublic: 24cb93a386Sopenharmony_ci static VkTestContext* Create(VkTestContext* sharedContext) { 25cb93a386Sopenharmony_ci GrVkBackendContext backendContext; 26cb93a386Sopenharmony_ci GrVkExtensions* extensions; 27cb93a386Sopenharmony_ci VkPhysicalDeviceFeatures2* features; 28cb93a386Sopenharmony_ci bool ownsContext = true; 29cb93a386Sopenharmony_ci VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE; 30cb93a386Sopenharmony_ci PFN_vkDestroyDebugReportCallbackEXT destroyCallback = nullptr; 31cb93a386Sopenharmony_ci if (sharedContext) { 32cb93a386Sopenharmony_ci backendContext = sharedContext->getVkBackendContext(); 33cb93a386Sopenharmony_ci extensions = const_cast<GrVkExtensions*>(sharedContext->getVkExtensions()); 34cb93a386Sopenharmony_ci features = const_cast<VkPhysicalDeviceFeatures2*>(sharedContext->getVkFeatures()); 35cb93a386Sopenharmony_ci // We always delete the parent context last so make sure the child does not think they 36cb93a386Sopenharmony_ci // own the vulkan context. 37cb93a386Sopenharmony_ci ownsContext = false; 38cb93a386Sopenharmony_ci } else { 39cb93a386Sopenharmony_ci PFN_vkGetInstanceProcAddr instProc; 40cb93a386Sopenharmony_ci PFN_vkGetDeviceProcAddr devProc; 41cb93a386Sopenharmony_ci if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) { 42cb93a386Sopenharmony_ci return nullptr; 43cb93a386Sopenharmony_ci } 44cb93a386Sopenharmony_ci auto getProc = [instProc, devProc](const char* proc_name, 45cb93a386Sopenharmony_ci VkInstance instance, VkDevice device) { 46cb93a386Sopenharmony_ci if (device != VK_NULL_HANDLE) { 47cb93a386Sopenharmony_ci return devProc(device, proc_name); 48cb93a386Sopenharmony_ci } 49cb93a386Sopenharmony_ci return instProc(instance, proc_name); 50cb93a386Sopenharmony_ci }; 51cb93a386Sopenharmony_ci extensions = new GrVkExtensions(); 52cb93a386Sopenharmony_ci features = new VkPhysicalDeviceFeatures2; 53cb93a386Sopenharmony_ci memset(features, 0, sizeof(VkPhysicalDeviceFeatures2)); 54cb93a386Sopenharmony_ci if (!sk_gpu_test::CreateVkBackendContext(getProc, &backendContext, extensions, 55cb93a386Sopenharmony_ci features, &debugCallback)) { 56cb93a386Sopenharmony_ci sk_gpu_test::FreeVulkanFeaturesStructs(features); 57cb93a386Sopenharmony_ci delete features; 58cb93a386Sopenharmony_ci delete extensions; 59cb93a386Sopenharmony_ci return nullptr; 60cb93a386Sopenharmony_ci } 61cb93a386Sopenharmony_ci if (debugCallback != VK_NULL_HANDLE) { 62cb93a386Sopenharmony_ci destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc( 63cb93a386Sopenharmony_ci backendContext.fInstance, "vkDestroyDebugReportCallbackEXT"); 64cb93a386Sopenharmony_ci } 65cb93a386Sopenharmony_ci } 66cb93a386Sopenharmony_ci return new VkTestContextImpl(backendContext, extensions, features, ownsContext, 67cb93a386Sopenharmony_ci debugCallback, destroyCallback); 68cb93a386Sopenharmony_ci } 69cb93a386Sopenharmony_ci 70cb93a386Sopenharmony_ci ~VkTestContextImpl() override { this->teardown(); } 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci void testAbandon() override {} 73cb93a386Sopenharmony_ci 74cb93a386Sopenharmony_ci void finish() override {} 75cb93a386Sopenharmony_ci 76cb93a386Sopenharmony_ci sk_sp<GrDirectContext> makeContext(const GrContextOptions& options) override { 77cb93a386Sopenharmony_ci return GrDirectContext::MakeVulkan(fVk, options); 78cb93a386Sopenharmony_ci } 79cb93a386Sopenharmony_ci 80cb93a386Sopenharmony_ciprotected: 81cb93a386Sopenharmony_ci#define ACQUIRE_VK_PROC_LOCAL(name, inst) \ 82cb93a386Sopenharmony_ci PFN_vk##name grVk##name = \ 83cb93a386Sopenharmony_ci reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \ 84cb93a386Sopenharmony_ci do { \ 85cb93a386Sopenharmony_ci if (grVk##name == nullptr) { \ 86cb93a386Sopenharmony_ci SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ 87cb93a386Sopenharmony_ci return; \ 88cb93a386Sopenharmony_ci } \ 89cb93a386Sopenharmony_ci } while (0) 90cb93a386Sopenharmony_ci 91cb93a386Sopenharmony_ci void teardown() override { 92cb93a386Sopenharmony_ci INHERITED::teardown(); 93cb93a386Sopenharmony_ci fVk.fMemoryAllocator.reset(); 94cb93a386Sopenharmony_ci if (fOwnsContext) { 95cb93a386Sopenharmony_ci ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance); 96cb93a386Sopenharmony_ci ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance); 97cb93a386Sopenharmony_ci ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance); 98cb93a386Sopenharmony_ci grVkDeviceWaitIdle(fVk.fDevice); 99cb93a386Sopenharmony_ci grVkDestroyDevice(fVk.fDevice, nullptr); 100cb93a386Sopenharmony_ci#ifdef SK_ENABLE_VK_LAYERS 101cb93a386Sopenharmony_ci if (fDebugCallback != VK_NULL_HANDLE) { 102cb93a386Sopenharmony_ci fDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr); 103cb93a386Sopenharmony_ci } 104cb93a386Sopenharmony_ci#endif 105cb93a386Sopenharmony_ci grVkDestroyInstance(fVk.fInstance, nullptr); 106cb93a386Sopenharmony_ci delete fExtensions; 107cb93a386Sopenharmony_ci 108cb93a386Sopenharmony_ci sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures); 109cb93a386Sopenharmony_ci delete fFeatures; 110cb93a386Sopenharmony_ci } 111cb93a386Sopenharmony_ci } 112cb93a386Sopenharmony_ci 113cb93a386Sopenharmony_ciprivate: 114cb93a386Sopenharmony_ci VkTestContextImpl(const GrVkBackendContext& backendContext, const GrVkExtensions* extensions, 115cb93a386Sopenharmony_ci VkPhysicalDeviceFeatures2* features, bool ownsContext, 116cb93a386Sopenharmony_ci VkDebugReportCallbackEXT debugCallback, 117cb93a386Sopenharmony_ci PFN_vkDestroyDebugReportCallbackEXT destroyCallback) 118cb93a386Sopenharmony_ci : VkTestContext(backendContext, extensions, features, ownsContext, debugCallback, 119cb93a386Sopenharmony_ci destroyCallback) { 120cb93a386Sopenharmony_ci fFenceSupport = true; 121cb93a386Sopenharmony_ci } 122cb93a386Sopenharmony_ci 123cb93a386Sopenharmony_ci void onPlatformMakeNotCurrent() const override {} 124cb93a386Sopenharmony_ci void onPlatformMakeCurrent() const override {} 125cb93a386Sopenharmony_ci std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; } 126cb93a386Sopenharmony_ci 127cb93a386Sopenharmony_ci using INHERITED = sk_gpu_test::VkTestContext; 128cb93a386Sopenharmony_ci}; 129cb93a386Sopenharmony_ci} // anonymous namespace 130cb93a386Sopenharmony_ci 131cb93a386Sopenharmony_cinamespace sk_gpu_test { 132cb93a386Sopenharmony_ciVkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) { 133cb93a386Sopenharmony_ci return VkTestContextImpl::Create(sharedContext); 134cb93a386Sopenharmony_ci} 135cb93a386Sopenharmony_ci} // namespace sk_gpu_test 136cb93a386Sopenharmony_ci 137cb93a386Sopenharmony_ci#endif 138