Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
mock_vulkan.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEST_MOCK_VULKAN_H_
6#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEST_MOCK_VULKAN_H_
7
8#include <functional>
9#include <memory>
10#include <string>
11#include <vector>
12
15#include "vulkan/vulkan_core.h"
16#include "vulkan/vulkan_enums.hpp"
17
18namespace impeller {
19namespace testing {
20
21std::shared_ptr<std::vector<std::string>> GetMockVulkanFunctions(
22 VkDevice device);
23
24// A test-controlled version of |vk::Fence|.
25class MockFence final {
26 public:
27 MockFence() = default;
28
29 // Returns the result that was set in the constructor or |SetStatus|.
30 VkResult GetStatus() { return static_cast<VkResult>(result_.load()); }
31
32 // Sets the result that will be returned by `GetFenceStatus`.
33 void SetStatus(vk::Result result) { result_ = result; }
34
35 // Sets the result that will be returned by `GetFenceStatus`.
36 static void SetStatus(vk::UniqueFence& fence, vk::Result result) {
37 // Cast the fence to a MockFence and set the result.
38 VkFence raw_fence = fence.get();
39 MockFence* mock_fence = reinterpret_cast<MockFence*>(raw_fence);
40 mock_fence->SetStatus(result);
41 }
42
43 // Gets a raw pointer to manipulate the fence after it's been moved.
44 static MockFence* GetRawPointer(vk::UniqueFence& fence) {
45 // Cast the fence to a MockFence and get the result.
46 VkFence raw_fence = fence.get();
47 MockFence* mock_fence = reinterpret_cast<MockFence*>(raw_fence);
48 return mock_fence;
49 }
50
51 private:
52 std::atomic<vk::Result> result_ = vk::Result::eSuccess;
53
54 MockFence(const MockFence&) = delete;
55
56 MockFence& operator=(const MockFence&) = delete;
57};
58
60 public:
62
63 //------------------------------------------------------------------------------
64 /// @brief Create a Vulkan context with Vulkan functions mocked. The
65 /// caller is given a chance to tinker on the settings right
66 /// before a context is created.
67 ///
68 /// @return A context if one can be created.
69 ///
70 std::shared_ptr<ContextVK> Build();
71
72 /// A callback that allows the modification of the ContextVK::Settings before
73 /// the context is made.
75 const std::function<void(ContextVK::Settings&)>& settings_callback) {
76 settings_callback_ = settings_callback;
77 return *this;
78 }
79
81 const std::vector<std::string>& instance_extensions) {
82 instance_extensions_ = instance_extensions;
83 return *this;
84 }
85
87 const std::vector<std::string>& instance_layers) {
88 instance_layers_ = instance_layers;
89 return *this;
90 }
91
92 /// Set the behavior of vkGetPhysicalDeviceFormatProperties, which needs to
93 /// respond differently for different formats.
95 std::function<void(VkPhysicalDevice physicalDevice,
97 VkFormatProperties* pFormatProperties)>
98 format_properties_callback) {
99 format_properties_callback_ = std::move(format_properties_callback);
100 return *this;
101 }
102
103 private:
104 std::function<void(ContextVK::Settings&)> settings_callback_;
105 std::vector<std::string> instance_extensions_;
106 std::vector<std::string> instance_layers_;
107 std::function<void(VkPhysicalDevice physicalDevice,
109 VkFormatProperties* pFormatProperties)>
110 format_properties_callback_;
111};
112
113/// @brief Override the image size returned by all swapchain images.
115
116} // namespace testing
117} // namespace impeller
118
119#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEST_MOCK_VULKAN_H_
void SetStatus(vk::Result result)
Definition mock_vulkan.h:33
static MockFence * GetRawPointer(vk::UniqueFence &fence)
Definition mock_vulkan.h:44
static void SetStatus(vk::UniqueFence &fence, vk::Result result)
Definition mock_vulkan.h:36
std::shared_ptr< ContextVK > Build()
Create a Vulkan context with Vulkan functions mocked. The caller is given a chance to tinker on the s...
MockVulkanContextBuilder & SetInstanceExtensions(const std::vector< std::string > &instance_extensions)
Definition mock_vulkan.h:80
MockVulkanContextBuilder & SetInstanceLayers(const std::vector< std::string > &instance_layers)
Definition mock_vulkan.h:86
MockVulkanContextBuilder & SetPhysicalDeviceFormatPropertiesCallback(std::function< void(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties *pFormatProperties)> format_properties_callback)
Definition mock_vulkan.h:94
MockVulkanContextBuilder & SetSettingsCallback(const std::function< void(ContextVK::Settings &)> &settings_callback)
Definition mock_vulkan.h:74
VkDevice device
Definition main.cc:53
GAsyncResult * result
uint32_t uint32_t * format
std::shared_ptr< std::vector< std::string > > GetMockVulkanFunctions(VkDevice device)
void SetSwapchainImageSize(ISize size)
Override the image size returned by all swapchain images.
VkResult
VkFormat