From d18ae436057a703f3d5f146fd9766d9842d9677e Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Fri, 19 May 2023 17:38:19 -0700 Subject: [PATCH] [Impeller] Made other vulkan objects cleanup properly. (flutter/engine#42113) fixes https://github.com/flutter/flutter/issues/127058 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../renderer/backend/vulkan/context_vk.cc | 20 ++-- .../backend/vulkan/context_vk_unittests.cc | 57 +++++++++ .../backend/vulkan/pipeline_cache_vk.cc | 20 +++- .../backend/vulkan/pipeline_cache_vk.h | 3 +- .../backend/vulkan/pipeline_library_vk.cc | 7 +- .../backend/vulkan/pipeline_library_vk.h | 3 +- .../renderer/backend/vulkan/pipeline_vk.cc | 19 ++- .../renderer/backend/vulkan/pipeline_vk.h | 13 ++- .../backend/vulkan/shader_function_vk.cc | 22 +++- .../backend/vulkan/shader_function_vk.h | 5 +- .../backend/vulkan/shader_library_vk.cc | 18 ++- .../backend/vulkan/shader_library_vk.h | 5 +- .../backend/vulkan/test/mock_vulkan.cc | 109 +++++++++++++++++- .../backend/vulkan/test/mock_vulkan.h | 8 +- 14 files changed, 263 insertions(+), 46 deletions(-) diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc index 7d77740694..ae3ae78d00 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -278,7 +278,10 @@ void ContextVK::Setup(Settings settings) { VALIDATION_LOG << "Could not create logical device."; return; } - vk::UniqueDevice device = std::move(device_result.value); + device_ = std::move(device_result.value); + // This makes sure that the device is deleted at the proper time if there is + // an error. + fml::ScopedCleanupClosure device_resetter([this]() { device_.reset(); }); if (!caps->SetDevice(physical_device.value())) { VALIDATION_LOG << "Capabilities could not be updated."; @@ -292,7 +295,7 @@ void ContextVK::Setup(Settings settings) { weak_from_this(), // application_info.apiVersion, // physical_device.value(), // - device.get(), // + device_.get(), // instance.value.get(), // dispatcher.vkGetInstanceProcAddr, // dispatcher.vkGetDeviceProcAddr // @@ -307,8 +310,7 @@ void ContextVK::Setup(Settings settings) { /// Setup the pipeline library. /// auto pipeline_library = std::shared_ptr( - new PipelineLibraryVK(weak_from_this(), // - device.get(), // + new PipelineLibraryVK(shared_from_this(), // caps, // std::move(settings.cache_directory), // settings.worker_task_runner // @@ -320,10 +322,10 @@ void ContextVK::Setup(Settings settings) { } auto sampler_library = - std::shared_ptr(new SamplerLibraryVK(device.get())); + std::shared_ptr(new SamplerLibraryVK(device_.get())); auto shader_library = std::shared_ptr( - new ShaderLibraryVK(device.get(), // + new ShaderLibraryVK(weak_from_this(), // settings.shader_libraries_data) // ); @@ -336,7 +338,7 @@ void ContextVK::Setup(Settings settings) { /// Create the fence waiter. /// auto fence_waiter = - std::shared_ptr(new FenceWaiterVK(device.get())); + std::shared_ptr(new FenceWaiterVK(device_.get())); if (!fence_waiter->IsValid()) { VALIDATION_LOG << "Could not create fence waiter."; return; @@ -345,7 +347,7 @@ void ContextVK::Setup(Settings settings) { //---------------------------------------------------------------------------- /// Fetch the queues. /// - QueuesVK queues(device.get(), // + QueuesVK queues(device_.get(), // graphics_queue.value(), // compute_queue.value(), // transfer_queue.value() // @@ -365,7 +367,7 @@ void ContextVK::Setup(Settings settings) { instance_ = std::move(instance.value); debug_report_ = std::move(debug_report); physical_device_ = physical_device.value(); - device_ = std::move(device); + device_resetter.Release(); allocator_ = std::move(allocator); shader_library_ = std::move(shader_library); sampler_library_ = std::move(sampler_library); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk_unittests.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk_unittests.cc index 6a2086d311..f584f10468 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk_unittests.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk_unittests.cc @@ -26,5 +26,62 @@ TEST(ContextVKTest, DeletesCommandPools) { ASSERT_FALSE(weak_context.lock()); } +TEST(ContextVKTest, DeletePipelineAfterContext) { + std::shared_ptr> pipeline; + std::shared_ptr> functions; + { + std::shared_ptr context = CreateMockVulkanContext(); + PipelineDescriptor pipeline_desc; + pipeline_desc.SetVertexDescriptor(std::make_shared()); + PipelineFuture pipeline_future = + context->GetPipelineLibrary()->GetPipeline(pipeline_desc); + pipeline = pipeline_future.Get(); + ASSERT_TRUE(pipeline); + functions = GetMockVulkanFunctions(context->GetDevice()); + ASSERT_TRUE(std::find(functions->begin(), functions->end(), + "vkCreateGraphicsPipelines") != functions->end()); + } + ASSERT_TRUE(std::find(functions->begin(), functions->end(), + "vkDestroyDevice") != functions->end()); +} + +TEST(ContextVKTest, DeleteShaderFunctionAfterContext) { + std::shared_ptr shader_function; + std::shared_ptr> functions; + { + std::shared_ptr context = CreateMockVulkanContext(); + PipelineDescriptor pipeline_desc; + pipeline_desc.SetVertexDescriptor(std::make_shared()); + std::vector data = {0x03, 0x02, 0x23, 0x07}; + context->GetShaderLibrary()->RegisterFunction( + "foobar", ShaderStage::kFragment, + std::make_shared(data), [](bool) {}); + shader_function = context->GetShaderLibrary()->GetFunction( + "foobar_fragment_main", ShaderStage::kFragment); + ASSERT_TRUE(shader_function); + functions = GetMockVulkanFunctions(context->GetDevice()); + ASSERT_TRUE(std::find(functions->begin(), functions->end(), + "vkCreateShaderModule") != functions->end()); + } + ASSERT_TRUE(std::find(functions->begin(), functions->end(), + "vkDestroyDevice") != functions->end()); +} + +TEST(ContextVKTest, DeletePipelineLibraryAfterContext) { + std::shared_ptr pipeline_library; + std::shared_ptr> functions; + { + std::shared_ptr context = CreateMockVulkanContext(); + PipelineDescriptor pipeline_desc; + pipeline_desc.SetVertexDescriptor(std::make_shared()); + pipeline_library = context->GetPipelineLibrary(); + functions = GetMockVulkanFunctions(context->GetDevice()); + ASSERT_TRUE(std::find(functions->begin(), functions->end(), + "vkCreatePipelineCache") != functions->end()); + } + ASSERT_TRUE(std::find(functions->begin(), functions->end(), + "vkDestroyDevice") != functions->end()); +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.cc index 6d07881131..01d984e606 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.cc @@ -53,13 +53,12 @@ static std::unique_ptr OpenCacheFile( } PipelineCacheVK::PipelineCacheVK(std::shared_ptr caps, - std::weak_ptr device_holder, - const vk::Device& device, + std::shared_ptr device_holder, fml::UniqueFD cache_directory) : caps_(std::move(caps)), device_holder_(device_holder), cache_directory_(std::move(cache_directory)) { - if (!caps_ || !device) { + if (!caps_ || !device_holder->GetDevice()) { return; } @@ -81,7 +80,8 @@ PipelineCacheVK::PipelineCacheVK(std::shared_ptr caps, cache_info.pInitialData = existing_cache_data->GetMapping(); } - auto [result, existing_cache] = device.createPipelineCacheUnique(cache_info); + auto [result, existing_cache] = + device_holder->GetDevice().createPipelineCacheUnique(cache_info); if (result == vk::Result::eSuccess) { cache_ = std::move(existing_cache); @@ -93,7 +93,8 @@ PipelineCacheVK::PipelineCacheVK(std::shared_ptr caps, << vk::to_string(result) << ". Starting with a fresh cache."; cache_info.pInitialData = nullptr; cache_info.initialDataSize = 0u; - auto [result2, new_cache] = device.createPipelineCacheUnique(cache_info); + auto [result2, new_cache] = + device_holder->GetDevice().createPipelineCacheUnique(cache_info); if (result2 == vk::Result::eSuccess) { cache_ = std::move(new_cache); } else { @@ -105,7 +106,14 @@ PipelineCacheVK::PipelineCacheVK(std::shared_ptr caps, is_valid_ = !!cache_; } -PipelineCacheVK::~PipelineCacheVK() = default; +PipelineCacheVK::~PipelineCacheVK() { + std::shared_ptr device_holder = device_holder_.lock(); + if (device_holder) { + cache_.reset(); + } else { + cache_.release(); + } +} bool PipelineCacheVK::IsValid() const { return is_valid_; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.h index bb90a32cfe..c0c71bcfd6 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_cache_vk.h @@ -20,8 +20,7 @@ class PipelineCacheVK { // at the time of executing `PipelineCacheVK` because of how `ContextVK` does // initialization. explicit PipelineCacheVK(std::shared_ptr caps, - std::weak_ptr device_holder, - const vk::Device& device, + std::shared_ptr device_holder, fml::UniqueFD cache_directory); ~PipelineCacheVK(); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc index 22843ee4dc..3d4a8747fb 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.cc @@ -19,15 +19,13 @@ namespace impeller { PipelineLibraryVK::PipelineLibraryVK( - const std::weak_ptr& device_holder, - const vk::Device& device, + const std::shared_ptr& device_holder, std::shared_ptr caps, fml::UniqueFD cache_directory, std::shared_ptr worker_task_runner) : device_holder_(device_holder), pso_cache_(std::make_shared(std::move(caps), device_holder, - device, std::move(cache_directory))), worker_task_runner_(std::move(worker_task_runner)) { if (!pso_cache_->IsValid() || !worker_task_runner_) { @@ -334,7 +332,8 @@ std::unique_ptr PipelineLibraryVK::CreatePipeline( ContextVK::SetDebugName(strong_device->GetDevice(), *pipeline, "Pipeline " + desc.GetLabel()); - return std::make_unique(weak_from_this(), // + return std::make_unique(device_holder_, + weak_from_this(), // desc, // std::move(pipeline), // std::move(render_pass), // diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h index 44b87e07d9..1302323735 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_library_vk.h @@ -43,8 +43,7 @@ class PipelineLibraryVK final bool is_valid_ = false; PipelineLibraryVK( - const std::weak_ptr& device_holder, - const vk::Device& device, + const std::shared_ptr& device_holder, std::shared_ptr caps, fml::UniqueFD cache_directory, std::shared_ptr worker_task_runner); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc index 208d63ae99..bc971cd769 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.cc @@ -6,13 +6,15 @@ namespace impeller { -PipelineVK::PipelineVK(std::weak_ptr library, +PipelineVK::PipelineVK(std::weak_ptr device_holder, + std::weak_ptr library, const PipelineDescriptor& desc, vk::UniquePipeline pipeline, vk::UniqueRenderPass render_pass, vk::UniquePipelineLayout layout, vk::UniqueDescriptorSetLayout descriptor_set_layout) : Pipeline(std::move(library), desc), + device_holder_(device_holder), pipeline_(std::move(pipeline)), render_pass_(std::move(render_pass)), layout_(std::move(layout)), @@ -20,7 +22,20 @@ PipelineVK::PipelineVK(std::weak_ptr library, is_valid_ = pipeline_ && render_pass_ && layout_ && descriptor_set_layout_; } -PipelineVK::~PipelineVK() = default; +PipelineVK::~PipelineVK() { + std::shared_ptr device_holder = device_holder_.lock(); + if (device_holder) { + descriptor_set_layout_.reset(); + layout_.reset(); + render_pass_.reset(); + pipeline_.reset(); + } else { + descriptor_set_layout_.release(); + layout_.release(); + render_pass_.release(); + pipeline_.release(); + } +} bool PipelineVK::IsValid() const { return is_valid_; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h index 7135dcf03e..c7f4170330 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/pipeline_vk.h @@ -8,6 +8,7 @@ #include "flutter/fml/macros.h" #include "impeller/base/backend_cast.h" +#include "impeller/renderer/backend/vulkan/device_holder.h" #include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/pipeline.h" @@ -17,7 +18,8 @@ class PipelineVK final : public Pipeline, public BackendCast> { public: - PipelineVK(std::weak_ptr library, + PipelineVK(std::weak_ptr device_holder, + std::weak_ptr library, const PipelineDescriptor& desc, vk::UniquePipeline pipeline, vk::UniqueRenderPass render_pass, @@ -38,10 +40,11 @@ class PipelineVK final private: friend class PipelineLibraryVK; - const vk::UniquePipeline pipeline_; - const vk::UniqueRenderPass render_pass_; - const vk::UniquePipelineLayout layout_; - const vk::UniqueDescriptorSetLayout descriptor_set_layout_; + std::weak_ptr device_holder_; + vk::UniquePipeline pipeline_; + vk::UniqueRenderPass render_pass_; + vk::UniquePipelineLayout layout_; + vk::UniqueDescriptorSetLayout descriptor_set_layout_; bool is_valid_ = false; // |Pipeline| diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.cc index 03af4f9193..7f0bdde98c 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.cc @@ -6,14 +6,24 @@ namespace impeller { -ShaderFunctionVK::ShaderFunctionVK(UniqueID parent_library_id, - std::string name, - ShaderStage stage, - vk::UniqueShaderModule module) +ShaderFunctionVK::ShaderFunctionVK( + const std::weak_ptr& device_holder, + UniqueID parent_library_id, + std::string name, + ShaderStage stage, + vk::UniqueShaderModule module) : ShaderFunction(parent_library_id, std::move(name), stage), - module_(std::move(module)) {} + module_(std::move(module)), + device_holder_(device_holder) {} -ShaderFunctionVK::~ShaderFunctionVK() = default; +ShaderFunctionVK::~ShaderFunctionVK() { + std::shared_ptr device_holder = device_holder_.lock(); + if (device_holder) { + module_.reset(); + } else { + module_.release(); + } +} const vk::ShaderModule& ShaderFunctionVK::GetModule() const { return module_.get(); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.h index 5c984980ac..46372aca67 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_function_vk.h @@ -6,6 +6,7 @@ #include "flutter/fml/macros.h" #include "impeller/base/backend_cast.h" +#include "impeller/renderer/backend/vulkan/device_holder.h" #include "impeller/renderer/backend/vulkan/shader_function_vk.h" #include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/shader_function.h" @@ -25,8 +26,10 @@ class ShaderFunctionVK final friend class ShaderLibraryVK; vk::UniqueShaderModule module_; + std::weak_ptr device_holder_; - ShaderFunctionVK(UniqueID parent_library_id, + ShaderFunctionVK(const std::weak_ptr& device_holder, + UniqueID parent_library_id, std::string name, ShaderStage stage, vk::UniqueShaderModule module); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc index dd2d4b96ca..3988e156a6 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc @@ -53,9 +53,9 @@ static std::string VKShaderNameToShaderKeyName(const std::string& name, } ShaderLibraryVK::ShaderLibraryVK( - const vk::Device& device, + std::weak_ptr device_holder, const std::vector>& shader_libraries_data) - : device_(device) { + : device_holder_(device_holder) { TRACE_EVENT0("impeller", "CreateShaderLibrary"); bool success = true; auto iterator = [&](auto type, // @@ -145,7 +145,13 @@ bool ShaderLibraryVK::RegisterFunction( reinterpret_cast(code->GetMapping())); shader_module_info.setCodeSize(code->GetSize()); - auto module = device_.createShaderModuleUnique(shader_module_info); + auto device_holder = device_holder_.lock(); + if (!device_holder) { + return false; + } + FML_DCHECK(device_holder->GetDevice()); + auto module = + device_holder->GetDevice().createShaderModuleUnique(shader_module_info); if (module.result != vk::Result::eSuccess) { VALIDATION_LOG << "Could not create shader module: " @@ -156,11 +162,13 @@ bool ShaderLibraryVK::RegisterFunction( const auto key_name = VKShaderNameToShaderKeyName(name, stage); vk::UniqueShaderModule shader_module = std::move(module.value); - ContextVK::SetDebugName(device_, *shader_module, "Shader " + name); + ContextVK::SetDebugName(device_holder->GetDevice(), *shader_module, + "Shader " + name); WriterLock lock(functions_mutex_); functions_[ShaderKey{key_name, stage}] = std::shared_ptr( - new ShaderFunctionVK(library_id_, // + new ShaderFunctionVK(device_holder_, + library_id_, // key_name, // stage, // std::move(shader_module) // diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.h index b82640ae75..e458b57fca 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.h @@ -7,6 +7,7 @@ #include "flutter/fml/macros.h" #include "impeller/base/comparable.h" #include "impeller/base/thread.h" +#include "impeller/renderer/backend/vulkan/device_holder.h" #include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/shader_key.h" #include "impeller/renderer/shader_library.h" @@ -23,14 +24,14 @@ class ShaderLibraryVK final : public ShaderLibrary { private: friend class ContextVK; - const vk::Device device_; + std::weak_ptr device_holder_; const UniqueID library_id_; mutable RWMutex functions_mutex_; ShaderFunctionMap functions_ IPLR_GUARDED_BY(functions_mutex_); bool is_valid_ = false; ShaderLibraryVK( - const vk::Device& device, + std::weak_ptr device_holder, const std::vector>& shader_libraries_data); // |ShaderLibrary| diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.cc index 100b36b1cb..aac80e1aba 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.cc @@ -8,6 +8,12 @@ namespace impeller { namespace testing { namespace { + +struct MockDevice { + MockDevice() : called_functions_(new std::vector()) {} + std::shared_ptr> called_functions_; +}; + void noop() {} VkResult vkEnumerateInstanceExtensionProperties( @@ -97,7 +103,7 @@ VkResult vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice) { - *pDevice = reinterpret_cast(0xcafebabe); + *pDevice = reinterpret_cast(new MockDevice()); return VK_SUCCESS; } @@ -125,6 +131,8 @@ VkResult vkCreatePipelineCache(VkDevice device, const VkPipelineCacheCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkPipelineCache* pPipelineCache) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkCreatePipelineCache"); *pPipelineCache = reinterpret_cast(0xb000dead); return VK_SUCCESS; } @@ -212,6 +220,81 @@ VkResult vkBindBufferMemory(VkDevice device, return VK_SUCCESS; } +VkResult vkCreateRenderPass(VkDevice device, + const VkRenderPassCreateInfo* pCreateInfo, + const VkAllocationCallbacks* pAllocator, + VkRenderPass* pRenderPass) { + *pRenderPass = reinterpret_cast(0x12341234); + return VK_SUCCESS; +} + +VkResult vkCreateDescriptorSetLayout( + VkDevice device, + const VkDescriptorSetLayoutCreateInfo* pCreateInfo, + const VkAllocationCallbacks* pAllocator, + VkDescriptorSetLayout* pSetLayout) { + *pSetLayout = reinterpret_cast(0x77777777); + return VK_SUCCESS; +} + +VkResult vkCreatePipelineLayout(VkDevice device, + const VkPipelineLayoutCreateInfo* pCreateInfo, + const VkAllocationCallbacks* pAllocator, + VkPipelineLayout* pPipelineLayout) { + *pPipelineLayout = reinterpret_cast(0x88888888); + return VK_SUCCESS; +} + +VkResult vkCreateGraphicsPipelines( + VkDevice device, + VkPipelineCache pipelineCache, + uint32_t createInfoCount, + const VkGraphicsPipelineCreateInfo* pCreateInfos, + const VkAllocationCallbacks* pAllocator, + VkPipeline* pPipelines) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkCreateGraphicsPipelines"); + *pPipelines = reinterpret_cast(0x99999999); + return VK_SUCCESS; +} + +void vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkDestroyDevice"); + delete reinterpret_cast(device); +} + +void vkDestroyPipeline(VkDevice device, + VkPipeline pipeline, + const VkAllocationCallbacks* pAllocator) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkDestroyPipeline"); +} + +VkResult vkCreateShaderModule(VkDevice device, + const VkShaderModuleCreateInfo* pCreateInfo, + const VkAllocationCallbacks* pAllocator, + VkShaderModule* pShaderModule) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkCreateShaderModule"); + *pShaderModule = reinterpret_cast(0x11111111); + return VK_SUCCESS; +} + +void vkDestroyShaderModule(VkDevice device, + VkShaderModule shaderModule, + const VkAllocationCallbacks* pAllocator) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkDestroyShaderModule"); +} + +void vkDestroyPipelineCache(VkDevice device, + VkPipelineCache pipelineCache, + const VkAllocationCallbacks* pAllocator) { + MockDevice* mock_device = reinterpret_cast(device); + mock_device->called_functions_->push_back("vkDestroyPipelineCache"); +} + PFN_vkVoidFunction GetMockVulkanProcAddress(VkInstance instance, const char* pName) { if (strcmp("vkEnumerateInstanceExtensionProperties", pName) == 0) { @@ -264,6 +347,24 @@ PFN_vkVoidFunction GetMockVulkanProcAddress(VkInstance instance, return (PFN_vkVoidFunction)vkGetBufferMemoryRequirements2KHR; } else if (strcmp("vkBindBufferMemory", pName) == 0) { return (PFN_vkVoidFunction)vkBindBufferMemory; + } else if (strcmp("vkCreateRenderPass", pName) == 0) { + return (PFN_vkVoidFunction)vkCreateRenderPass; + } else if (strcmp("vkCreateDescriptorSetLayout", pName) == 0) { + return (PFN_vkVoidFunction)vkCreateDescriptorSetLayout; + } else if (strcmp("vkCreatePipelineLayout", pName) == 0) { + return (PFN_vkVoidFunction)vkCreatePipelineLayout; + } else if (strcmp("vkCreateGraphicsPipelines", pName) == 0) { + return (PFN_vkVoidFunction)vkCreateGraphicsPipelines; + } else if (strcmp("vkDestroyDevice", pName) == 0) { + return (PFN_vkVoidFunction)vkDestroyDevice; + } else if (strcmp("vkDestroyPipeline", pName) == 0) { + return (PFN_vkVoidFunction)vkDestroyPipeline; + } else if (strcmp("vkCreateShaderModule", pName) == 0) { + return (PFN_vkVoidFunction)vkCreateShaderModule; + } else if (strcmp("vkDestroyShaderModule", pName) == 0) { + return (PFN_vkVoidFunction)vkDestroyShaderModule; + } else if (strcmp("vkDestroyPipelineCache", pName) == 0) { + return (PFN_vkVoidFunction)vkDestroyPipelineCache; } return noop; } @@ -279,5 +380,11 @@ std::shared_ptr CreateMockVulkanContext(void) { return ContextVK::Create(std::move(settings)); } +std::shared_ptr> GetMockVulkanFunctions( + VkDevice device) { + MockDevice* mock_device = reinterpret_cast(device); + return mock_device->called_functions_; +} + } // namespace testing } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.h b/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.h index cc075850ff..280b47e981 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/test/mock_vulkan.h @@ -4,12 +4,18 @@ #pragma once +#include +#include + #include "impeller/renderer/backend/vulkan/context_vk.h" namespace impeller { namespace testing { +std::shared_ptr> GetMockVulkanFunctions( + VkDevice device); + std::shared_ptr CreateMockVulkanContext(void); -} +} // namespace testing } // namespace impeller