Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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
93 const std::vector<std::string>& device_extensions) {
94 device_extensions_ = device_extensions;
95 return *this;
96 }
97
98 /// Set the behavior of vkGetPhysicalDeviceFormatProperties, which needs to
99 /// respond differently for different formats.
101 std::function<void(VkPhysicalDevice physicalDevice,
102 VkFormat format,
103 VkFormatProperties* pFormatProperties)>
105 format_properties_callback_ = std::move(format_properties_callback);
106 return *this;
107 }
108
110 std::function<void(VkPhysicalDevice device,
111 VkPhysicalDeviceProperties* physicalProperties)>
112 physical_properties_callback) {
113 physical_properties_callback_ = std::move(physical_properties_callback);
114 return *this;
115 }
116
118 const ContextVK::EmbedderData& embedder_data) {
119 embedder_data_ = embedder_data;
120 return *this;
121 }
122
124 std::function<std::remove_pointer_t<PFN_vkAcquireNextImageKHR>>
126 acquire_next_image_callback_ = std::move(acquire_next_image_callback);
127 return *this;
128 }
129
131 std::function<std::remove_pointer_t<PFN_vkWaitForFences>>
133 wait_for_fences_callback_ = std::move(wait_for_fences_callback);
134 return *this;
135 }
136
137 private:
138 std::function<void(ContextVK::Settings&)> settings_callback_;
139 std::vector<std::string> instance_extensions_;
140 std::vector<std::string> instance_layers_;
141 std::vector<std::string> device_extensions_;
142 std::optional<ContextVK::EmbedderData> embedder_data_;
143 std::function<void(VkPhysicalDevice physicalDevice,
144 VkFormat format,
145 VkFormatProperties* pFormatProperties)>
146 format_properties_callback_;
147 std::function<void(VkPhysicalDevice device,
148 VkPhysicalDeviceProperties* physicalProperties)>
149 physical_properties_callback_;
150 std::function<std::remove_pointer_t<PFN_vkAcquireNextImageKHR>>
151 acquire_next_image_callback_;
152 std::function<std::remove_pointer_t<PFN_vkWaitForFences>>
153 wait_for_fences_callback_;
154};
155
156/// @brief Override the image size returned by all swapchain images.
158
159std::vector<VkImageMemoryBarrier>& GetImageMemoryBarriers(
160 VkCommandBuffer buffer);
161
162/// @brief Returns the viewports passed to `vkCmdSetViewport` calls on the
163/// given command buffer, in call order.
164const std::vector<VkViewport>& GetRecordedViewports(VkCommandBuffer buffer);
165
166} // namespace testing
167} // namespace impeller
168
169#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
MockVulkanContextBuilder & SetPhysicalPropertiesCallback(std::function< void(VkPhysicalDevice device, VkPhysicalDeviceProperties *physicalProperties)> physical_properties_callback)
MockVulkanContextBuilder & SetDeviceExtensions(const std::vector< std::string > &device_extensions)
Definition mock_vulkan.h:92
MockVulkanContextBuilder SetEmbedderData(const ContextVK::EmbedderData &embedder_data)
MockVulkanContextBuilder SetWaitForFencesCallback(std::function< std::remove_pointer_t< PFN_vkWaitForFences > > wait_for_fences_callback)
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 SetAcquireNextImageCallback(std::function< std::remove_pointer_t< PFN_vkAcquireNextImageKHR > > acquire_next_image_callback)
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)
MockVulkanContextBuilder & SetSettingsCallback(const std::function< void(ContextVK::Settings &)> &settings_callback)
Definition mock_vulkan.h:74
VkDevice device
Definition main.cc:69
std::vector< std::string > instance_layers
std::vector< std::string > device_extensions
std::function< void(VkPhysicalDevice physicalDevice, VkFormat format, VkFormatProperties *pFormatProperties)> format_properties_callback
std::function< std::remove_pointer_t< PFN_vkWaitForFences > > wait_for_fences_callback
std::vector< std::string > instance_extensions
std::function< std::remove_pointer_t< PFN_vkAcquireNextImageKHR > > acquire_next_image_callback
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
std::shared_ptr< std::vector< std::string > > GetMockVulkanFunctions(VkDevice device)
const std::vector< VkViewport > & GetRecordedViewports(VkCommandBuffer buffer)
Returns the viewports passed to vkCmdSetViewport calls on the given command buffer,...
std::vector< VkImageMemoryBarrier > & GetImageMemoryBarriers(VkCommandBuffer buffer)
void SetSwapchainImageSize(ISize size)
Override the image size returned by all swapchain images.