diff --git a/impeller/renderer/backend/vulkan/test/mock_vulkan.cc b/impeller/renderer/backend/vulkan/test/mock_vulkan.cc index ed311ac017a81..a33f88624b89c 100644 --- a/impeller/renderer/backend/vulkan/test/mock_vulkan.cc +++ b/impeller/renderer/backend/vulkan/test/mock_vulkan.cc @@ -4,10 +4,13 @@ #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h" #include +#include #include #include "fml/macros.h" #include "fml/thread_local.h" #include "impeller/base/thread_safety.h" +#include "vulkan/vulkan_core.h" +#include "vulkan/vulkan_enums.hpp" namespace impeller { namespace testing { @@ -410,7 +413,8 @@ VkResult vkCreateFence(VkDevice device, const VkFenceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkFence* pFence) { - *pFence = reinterpret_cast(0xfe0ce); + MockDevice* mock_device = reinterpret_cast(device); + *pFence = reinterpret_cast(new MockFence()); return VK_SUCCESS; } @@ -430,7 +434,9 @@ VkResult vkWaitForFences(VkDevice device, } VkResult vkGetFenceStatus(VkDevice device, VkFence fence) { - return VK_SUCCESS; + MockDevice* mock_device = reinterpret_cast(device); + MockFence* mock_fence = reinterpret_cast(fence); + return mock_fence->GetStatus(); } VkResult vkCreateDebugUtilsMessengerEXT( diff --git a/impeller/renderer/backend/vulkan/test/mock_vulkan.h b/impeller/renderer/backend/vulkan/test/mock_vulkan.h index 41c82c6da2a90..71a88a42e02eb 100644 --- a/impeller/renderer/backend/vulkan/test/mock_vulkan.h +++ b/impeller/renderer/backend/vulkan/test/mock_vulkan.h @@ -5,10 +5,12 @@ #pragma once #include +#include #include #include #include "impeller/renderer/backend/vulkan/context_vk.h" +#include "vulkan/vulkan_enums.hpp" namespace impeller { namespace testing { @@ -16,6 +18,28 @@ namespace testing { std::shared_ptr> GetMockVulkanFunctions( VkDevice device); +// A test-controlled version of |vk::Fence|. +class MockFence final { + public: + MockFence() = default; + + // Returns the result that was set in the constructor or |SetStatus|. + VkResult GetStatus() { return static_cast(result_); } + + // Sets the result that will be returned by `GetFenceStatus`. + static void SetStatus(vk::UniqueFence& fence, vk::Result result) { + // Cast the fence to a MockFence and set the result. + VkFence raw_fence = fence.get(); + MockFence* mock_fence = reinterpret_cast(raw_fence); + mock_fence->result_ = result; + } + + private: + vk::Result result_ = vk::Result::eSuccess; + + FML_DISALLOW_COPY_AND_ASSIGN(MockFence); +}; + class MockVulkanContextBuilder { public: MockVulkanContextBuilder(); diff --git a/impeller/renderer/backend/vulkan/test/mock_vulkan_unittests.cc b/impeller/renderer/backend/vulkan/test/mock_vulkan_unittests.cc index de28491e69fed..1aa57b0345f21 100644 --- a/impeller/renderer/backend/vulkan/test/mock_vulkan_unittests.cc +++ b/impeller/renderer/backend/vulkan/test/mock_vulkan_unittests.cc @@ -6,6 +6,7 @@ #include "gtest/gtest.h" #include "impeller/renderer/backend/vulkan/command_pool_vk.h" #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h" +#include "vulkan/vulkan_enums.hpp" namespace impeller { namespace testing { @@ -33,5 +34,26 @@ TEST(MockVulkanContextTest, IsThreadSafe) { context->Shutdown(); } +TEST(MockVulkanContextTest, DefaultFenceAlwaysReportsSuccess) { + auto const context = MockVulkanContextBuilder().Build(); + auto const device = context->GetDevice(); + + auto fence = device.createFenceUnique({}).value; + EXPECT_EQ(vk::Result::eSuccess, device.getFenceStatus(*fence)); +} + +TEST(MockVulkanContextTest, MockedFenceReportsStatus) { + auto const context = MockVulkanContextBuilder().Build(); + + auto const device = context->GetDevice(); + auto fence = device.createFenceUnique({}).value; + MockFence::SetStatus(fence, vk::Result::eNotReady); + + EXPECT_EQ(vk::Result::eNotReady, device.getFenceStatus(fence.get())); + + MockFence::SetStatus(fence, vk::Result::eSuccess); + EXPECT_EQ(vk::Result::eSuccess, device.getFenceStatus(*fence)); +} + } // namespace testing } // namespace impeller