Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
mock_vulkan.cc
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
6
7#include <cstdint>
8#include <cstring>
9#include <utility>
10#include <vector>
11
12#include "flutter/fml/logging.h"
14#include "impeller/renderer/backend/vulkan/vk.h" // IWYU pragma: keep.
15#include "third_party/swiftshader/include/vulkan/vulkan_core.h"
16#include "vulkan/vulkan.hpp"
17
18namespace impeller {
19namespace testing {
20
21namespace {
22
23struct MockCommandBuffer {
24 explicit MockCommandBuffer(
25 std::shared_ptr<std::vector<std::string>> called_functions)
26 : called_functions_(std::move(called_functions)) {}
27 std::shared_ptr<std::vector<std::string>> called_functions_;
28 std::vector<VkImageMemoryBarrier> image_memory_barriers_;
29 std::vector<VkViewport> recorded_viewports_;
30};
31
32struct MockQueryPool {};
33
34struct MockCommandPool {};
35
36struct MockDescriptorPool {};
37
38struct MockSurfaceKHR {};
39
40struct MockImage {};
41
42struct MockSwapchainKHR {
43 std::array<MockImage, 3> images;
44 size_t current_image = 0;
45};
46
47struct MockSemaphore {};
48
49struct MockFramebuffer {};
50
51static ISize currentImageSize = ISize{1, 1};
52
53class MockDevice final {
54 public:
55 explicit MockDevice() : called_functions_(new std::vector<std::string>()) {}
56
57 MockCommandBuffer* NewCommandBuffer() {
58 auto buffer = std::make_unique<MockCommandBuffer>(called_functions_);
59 MockCommandBuffer* result = buffer.get();
60 Lock lock(command_buffers_mutex_);
61 command_buffers_.emplace_back(std::move(buffer));
62 return result;
63 }
64
65 MockCommandPool* NewCommandPool() {
66 auto pool = std::make_unique<MockCommandPool>();
67 MockCommandPool* result = pool.get();
68 Lock lock(commmand_pools_mutex_);
69 command_pools_.emplace_back(std::move(pool));
70 return result;
71 }
72
73 void DeleteCommandPool(MockCommandPool* pool) {
74 Lock lock(commmand_pools_mutex_);
75 auto it = std::find_if(command_pools_.begin(), command_pools_.end(),
76 [pool](const std::unique_ptr<MockCommandPool>& p) {
77 return p.get() == pool;
78 });
79 if (it != command_pools_.end()) {
80 command_pools_.erase(it);
81 }
82 }
83
84 const std::shared_ptr<std::vector<std::string>>& GetCalledFunctions() {
85 return called_functions_;
86 }
87
88 void AddCalledFunction(const std::string& function) {
89 Lock lock(called_functions_mutex_);
90 called_functions_->push_back(function);
91 }
92
93 private:
94 MockDevice(const MockDevice&) = delete;
95
96 MockDevice& operator=(const MockDevice&) = delete;
97
98 Mutex called_functions_mutex_;
99 std::shared_ptr<std::vector<std::string>> called_functions_
100 IPLR_GUARDED_BY(called_functions_mutex_);
101
102 Mutex command_buffers_mutex_;
103 std::vector<std::unique_ptr<MockCommandBuffer>> command_buffers_
104 IPLR_GUARDED_BY(command_buffers_mutex_);
105
106 Mutex commmand_pools_mutex_;
107 std::vector<std::unique_ptr<MockCommandPool>> command_pools_
108 IPLR_GUARDED_BY(commmand_pools_mutex_);
109};
110
111struct MockVulkanState {
112 std::vector<std::string> instance_extensions;
113 std::vector<std::string> instance_layers;
114 std::vector<std::string> device_extensions;
115 std::function<void(VkPhysicalDevice physicalDevice,
116 VkFormat format,
117 VkFormatProperties* pFormatProperties)>
119 std::function<void(VkPhysicalDevice physicalDevice,
120 VkPhysicalDeviceProperties* pProperties)>
122 std::function<std::remove_pointer_t<PFN_vkWaitForFences>>
124 std::function<std::remove_pointer_t<PFN_vkAcquireNextImageKHR>>
126};
127
128class MockVulkanStatePtr {
129 public:
130 MockVulkanStatePtr() = default;
131
132 ~MockVulkanStatePtr() {
133 FML_CHECK(ptr_ == nullptr)
134 << "MockVulkanState was not null upon thread exit. Leak detected!";
135 }
136
137 void reset(MockVulkanState* ptr = nullptr) {
138 if (ptr_) {
139 delete ptr_;
140 }
141 ptr_ = ptr;
142 }
143
144 MockVulkanStatePtr(const MockVulkanStatePtr&) = delete;
145 MockVulkanStatePtr& operator=(const MockVulkanStatePtr&) = delete;
146 MockVulkanState* get() const { return ptr_; }
147 MockVulkanState& operator*() const { return *ptr_; }
148 MockVulkanState* operator->() const { return ptr_; }
149 explicit operator bool() const { return ptr_ != nullptr; }
150
151 private:
152 MockVulkanState* ptr_ = nullptr;
153};
154
155static thread_local MockVulkanStatePtr g_mock_vulkan_state;
156
157static MockVulkanState& GetMockVulkanState() {
158 FML_CHECK(g_mock_vulkan_state) << "MockVulkanState must be initialized.";
159 return *g_mock_vulkan_state;
160}
161
162void noop() {}
163
164VkResult vkEnumerateInstanceExtensionProperties(
165 const char* pLayerName,
166 uint32_t* pPropertyCount,
167 VkExtensionProperties* pProperties) {
168 if (!pProperties) {
169 *pPropertyCount = GetMockVulkanState().instance_extensions.size();
170 return VK_SUCCESS;
171 } else {
172 uint32_t count = 0;
173 VkResult result = VK_SUCCESS;
174 for (const std::string& ext : GetMockVulkanState().instance_extensions) {
175 if (count >= *pPropertyCount) {
176 result = VK_INCOMPLETE;
177 break;
178 }
179 snprintf(pProperties[count].extensionName,
180 sizeof(pProperties[count].extensionName), "%s", ext.c_str());
181 pProperties[count].specVersion = 0;
182 count++;
183 }
184 *pPropertyCount = count;
185 return result;
186 }
187}
188
189VkResult vkEnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
190 VkLayerProperties* pProperties) {
191 if (!pProperties) {
192 *pPropertyCount = GetMockVulkanState().instance_layers.size();
193 return VK_SUCCESS;
194 } else {
195 uint32_t count = 0;
196 VkResult result = VK_SUCCESS;
197 for (const std::string& ext : GetMockVulkanState().instance_layers) {
198 if (count >= *pPropertyCount) {
199 result = VK_INCOMPLETE;
200 break;
201 }
202 snprintf(pProperties[count].layerName,
203 sizeof(pProperties[count].layerName), "%s", ext.c_str());
204 pProperties[count].specVersion = 0;
205 count++;
206 }
207 *pPropertyCount = count;
208 return result;
209 }
210}
211
212VkResult vkEnumeratePhysicalDevices(VkInstance instance,
213 uint32_t* pPhysicalDeviceCount,
214 VkPhysicalDevice* pPhysicalDevices) {
215 if (!pPhysicalDevices) {
216 *pPhysicalDeviceCount = 1;
217 } else {
218 pPhysicalDevices[0] = reinterpret_cast<VkPhysicalDevice>(0xfeedface);
219 }
220 return VK_SUCCESS;
221}
222
223void vkGetPhysicalDeviceFormatProperties(
224 VkPhysicalDevice physicalDevice,
225 VkFormat format,
226 VkFormatProperties* pFormatProperties) {
227 if (GetMockVulkanState().format_properties_callback) {
228 GetMockVulkanState().format_properties_callback(physicalDevice, format,
229 pFormatProperties);
230 }
231}
232
233void vkGetPhysicalDeviceProperties(VkPhysicalDevice physicalDevice,
234 VkPhysicalDeviceProperties* pProperties) {
235 pProperties->limits.framebufferColorSampleCounts =
236 static_cast<VkSampleCountFlags>(VK_SAMPLE_COUNT_1_BIT |
237 VK_SAMPLE_COUNT_4_BIT);
238 pProperties->limits.maxImageDimension2D = 4096;
239 pProperties->limits.timestampPeriod = 1;
240 if (GetMockVulkanState().physical_device_properties_callback) {
241 GetMockVulkanState().physical_device_properties_callback(physicalDevice,
242 pProperties);
243 }
244}
245
246void vkGetPhysicalDeviceQueueFamilyProperties(
247 VkPhysicalDevice physicalDevice,
248 uint32_t* pQueueFamilyPropertyCount,
249 VkQueueFamilyProperties* pQueueFamilyProperties) {
250 if (!pQueueFamilyProperties) {
251 *pQueueFamilyPropertyCount = 1;
252 } else {
253 pQueueFamilyProperties[0].queueCount = 3;
254 pQueueFamilyProperties[0].queueFlags = static_cast<VkQueueFlags>(
255 VK_QUEUE_TRANSFER_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT);
256 }
257}
258
259VkResult vkEnumerateDeviceExtensionProperties(
260 VkPhysicalDevice physicalDevice,
261 const char* pLayerName,
262 uint32_t* pPropertyCount,
263 VkExtensionProperties* pProperties) {
264 if (!pProperties) {
265 *pPropertyCount = GetMockVulkanState().device_extensions.size();
266 return VK_SUCCESS;
267 } else {
268 uint32_t count = 0;
269 VkResult result = VK_SUCCESS;
270 for (const std::string& ext : GetMockVulkanState().device_extensions) {
271 if (count >= *pPropertyCount) {
272 result = VK_INCOMPLETE;
273 break;
274 }
275 snprintf(pProperties[count].extensionName,
276 sizeof(pProperties[count].extensionName), "%s", ext.c_str());
277 pProperties[count].specVersion = 0;
278 count++;
279 }
280 *pPropertyCount = count;
281 return result;
282 }
283}
284
285VkResult vkCreateDevice(VkPhysicalDevice physicalDevice,
286 const VkDeviceCreateInfo* pCreateInfo,
287 const VkAllocationCallbacks* pAllocator,
288 VkDevice* pDevice) {
289 *pDevice = reinterpret_cast<VkDevice>(new MockDevice());
290 return VK_SUCCESS;
291}
292
293VkResult vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo,
294 const VkAllocationCallbacks* pAllocator,
295 VkInstance* pInstance) {
296 *pInstance = reinterpret_cast<VkInstance>(0xbaadf00d);
297 return VK_SUCCESS;
298}
299
300void vkGetPhysicalDeviceMemoryProperties(
301 VkPhysicalDevice physicalDevice,
302 VkPhysicalDeviceMemoryProperties* pMemoryProperties) {
303 pMemoryProperties->memoryTypeCount = 2;
304 pMemoryProperties->memoryHeapCount = 2;
305 pMemoryProperties->memoryTypes[0].heapIndex = 0;
306 pMemoryProperties->memoryTypes[0].propertyFlags =
307 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
308 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
309 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
310 pMemoryProperties->memoryTypes[1].heapIndex = 1;
311 pMemoryProperties->memoryTypes[1].propertyFlags =
312 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
313 pMemoryProperties->memoryHeaps[0].size = 1024 * 1024 * 1024;
314 pMemoryProperties->memoryHeaps[0].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
315 pMemoryProperties->memoryHeaps[1].size = 1024 * 1024 * 1024;
316 pMemoryProperties->memoryHeaps[1].flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT;
317}
318
319VkResult vkCreatePipelineCache(VkDevice device,
320 const VkPipelineCacheCreateInfo* pCreateInfo,
321 const VkAllocationCallbacks* pAllocator,
322 VkPipelineCache* pPipelineCache) {
323 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
324 mock_device->AddCalledFunction("vkCreatePipelineCache");
325 *pPipelineCache = reinterpret_cast<VkPipelineCache>(0xb000dead);
326 return VK_SUCCESS;
327}
328
329VkResult vkCreateCommandPool(VkDevice device,
330 const VkCommandPoolCreateInfo* pCreateInfo,
331 const VkAllocationCallbacks* pAllocator,
332 VkCommandPool* pCommandPool) {
333 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
334 mock_device->AddCalledFunction("vkCreateCommandPool");
335 *pCommandPool =
336 reinterpret_cast<VkCommandPool>(mock_device->NewCommandPool());
337 return VK_SUCCESS;
338}
339
340VkResult vkResetCommandPool(VkDevice device,
341 VkCommandPool commandPool,
342 VkCommandPoolResetFlags flags) {
343 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
344 if (flags & VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT) {
345 mock_device->AddCalledFunction("vkResetCommandPoolReleaseResources");
346 } else {
347 mock_device->AddCalledFunction("vkResetCommandPool");
348 }
349 return VK_SUCCESS;
350}
351
352VkResult vkAllocateCommandBuffers(
353 VkDevice device,
354 const VkCommandBufferAllocateInfo* pAllocateInfo,
355 VkCommandBuffer* pCommandBuffers) {
356 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
357 mock_device->AddCalledFunction("vkAllocateCommandBuffers");
358 *pCommandBuffers =
359 reinterpret_cast<VkCommandBuffer>(mock_device->NewCommandBuffer());
360 return VK_SUCCESS;
361}
362
363VkResult vkBeginCommandBuffer(VkCommandBuffer commandBuffer,
364 const VkCommandBufferBeginInfo* pBeginInfo) {
365 return VK_SUCCESS;
366}
367
368VkResult vkCreateImage(VkDevice device,
369 const VkImageCreateInfo* pCreateInfo,
370 const VkAllocationCallbacks* pAllocator,
371 VkImage* pImage) {
372 *pImage = reinterpret_cast<VkImage>(0xD0D0CACA);
373 return VK_SUCCESS;
374}
375
376void vkGetImageMemoryRequirements2KHR(
377 VkDevice device,
378 const VkImageMemoryRequirementsInfo2* pInfo,
379 VkMemoryRequirements2* pMemoryRequirements) {
380 pMemoryRequirements->memoryRequirements.size = 1024;
381 pMemoryRequirements->memoryRequirements.memoryTypeBits = 1;
382}
383
384VkResult vkAllocateMemory(VkDevice device,
385 const VkMemoryAllocateInfo* pAllocateInfo,
386 const VkAllocationCallbacks* pAllocator,
387 VkDeviceMemory* pMemory) {
388 *pMemory = reinterpret_cast<VkDeviceMemory>(0xCAFEB0BA);
389 return VK_SUCCESS;
390}
391
392VkResult vkBindImageMemory(VkDevice device,
393 VkImage image,
394 VkDeviceMemory memory,
395 VkDeviceSize memoryOffset) {
396 return VK_SUCCESS;
397}
398
399VkResult vkCreateImageView(VkDevice device,
400 const VkImageViewCreateInfo* pCreateInfo,
401 const VkAllocationCallbacks* pAllocator,
402 VkImageView* pView) {
403 *pView = reinterpret_cast<VkImageView>(0xFEE1DEAD);
404 return VK_SUCCESS;
405}
406
407VkResult vkCreateBuffer(VkDevice device,
408 const VkBufferCreateInfo* pCreateInfo,
409 const VkAllocationCallbacks* pAllocator,
410 VkBuffer* pBuffer) {
411 *pBuffer = reinterpret_cast<VkBuffer>(0xDEADDEAD);
412 return VK_SUCCESS;
413}
414
415void vkGetBufferMemoryRequirements2KHR(
416 VkDevice device,
417 const VkBufferMemoryRequirementsInfo2* pInfo,
418 VkMemoryRequirements2* pMemoryRequirements) {
419 pMemoryRequirements->memoryRequirements.size = 1024;
420 pMemoryRequirements->memoryRequirements.memoryTypeBits = 1;
421}
422
423VkResult vkBindBufferMemory(VkDevice device,
424 VkBuffer buffer,
425 VkDeviceMemory memory,
426 VkDeviceSize memoryOffset) {
427 return VK_SUCCESS;
428}
429
430VkResult vkCreateRenderPass(VkDevice device,
431 const VkRenderPassCreateInfo* pCreateInfo,
432 const VkAllocationCallbacks* pAllocator,
433 VkRenderPass* pRenderPass) {
434 *pRenderPass = reinterpret_cast<VkRenderPass>(0x12341234);
435 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
436 mock_device->AddCalledFunction("vkCreateRenderPass");
437 return VK_SUCCESS;
438}
439
440VkResult vkCreateDescriptorSetLayout(
441 VkDevice device,
442 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
443 const VkAllocationCallbacks* pAllocator,
444 VkDescriptorSetLayout* pSetLayout) {
445 *pSetLayout = reinterpret_cast<VkDescriptorSetLayout>(0x77777777);
446 return VK_SUCCESS;
447}
448
449VkResult vkCreatePipelineLayout(VkDevice device,
450 const VkPipelineLayoutCreateInfo* pCreateInfo,
451 const VkAllocationCallbacks* pAllocator,
452 VkPipelineLayout* pPipelineLayout) {
453 *pPipelineLayout = reinterpret_cast<VkPipelineLayout>(0x88888888);
454 return VK_SUCCESS;
455}
456
457VkResult vkCreateGraphicsPipelines(
458 VkDevice device,
459 VkPipelineCache pipelineCache,
460 uint32_t createInfoCount,
461 const VkGraphicsPipelineCreateInfo* pCreateInfos,
462 const VkAllocationCallbacks* pAllocator,
463 VkPipeline* pPipelines) {
464 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
465 mock_device->AddCalledFunction("vkCreateGraphicsPipelines");
466 *pPipelines = reinterpret_cast<VkPipeline>(0x99999999);
467 return VK_SUCCESS;
468}
469
470void vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
471 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
472 mock_device->AddCalledFunction("vkDestroyDevice");
473 delete reinterpret_cast<MockDevice*>(device);
474}
475
476void vkDestroyInstance(VkInstance instance,
477 const VkAllocationCallbacks* pAllocator) {
478 if (g_mock_vulkan_state) {
479 g_mock_vulkan_state.reset();
480 }
481}
482
483void vkDestroyPipeline(VkDevice device,
484 VkPipeline pipeline,
485 const VkAllocationCallbacks* pAllocator) {
486 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
487 mock_device->AddCalledFunction("vkDestroyPipeline");
488}
489
490VkResult vkCreateShaderModule(VkDevice device,
491 const VkShaderModuleCreateInfo* pCreateInfo,
492 const VkAllocationCallbacks* pAllocator,
493 VkShaderModule* pShaderModule) {
494 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
495 mock_device->AddCalledFunction("vkCreateShaderModule");
496 *pShaderModule = reinterpret_cast<VkShaderModule>(0x11111111);
497 return VK_SUCCESS;
498}
499
500void vkDestroyShaderModule(VkDevice device,
501 VkShaderModule shaderModule,
502 const VkAllocationCallbacks* pAllocator) {
503 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
504 mock_device->AddCalledFunction("vkDestroyShaderModule");
505}
506
507void vkDestroyPipelineCache(VkDevice device,
508 VkPipelineCache pipelineCache,
509 const VkAllocationCallbacks* pAllocator) {
510 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
511 mock_device->AddCalledFunction("vkDestroyPipelineCache");
512}
513
514void vkDestroySurfaceKHR(VkInstance instance,
515 VkSurfaceKHR surface,
516 const VkAllocationCallbacks* pAllocator) {
517 return;
518}
519
520void vkCmdBindPipeline(VkCommandBuffer commandBuffer,
521 VkPipelineBindPoint pipelineBindPoint,
522 VkPipeline pipeline) {
523 MockCommandBuffer* mock_command_buffer =
524 reinterpret_cast<MockCommandBuffer*>(commandBuffer);
525 mock_command_buffer->called_functions_->push_back("vkCmdBindPipeline");
526}
527
528void vkCmdPipelineBarrier(VkCommandBuffer commandBuffer,
529 VkPipelineStageFlags srcStageMask,
530 VkPipelineStageFlags dstStageMask,
531 VkDependencyFlags dependencyFlags,
532 uint32_t memoryBarrierCount,
533 const VkMemoryBarrier* pMemoryBarriers,
534 uint32_t bufferMemoryBarrierCount,
535 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
536 uint32_t imageMemoryBarrierCount,
537 const VkImageMemoryBarrier* pImageMemoryBarriers) {
538 MockCommandBuffer* mock_command_buffer =
539 reinterpret_cast<MockCommandBuffer*>(commandBuffer);
540 mock_command_buffer->called_functions_->push_back("vkCmdPipelineBarrier");
541 if (pImageMemoryBarriers) {
542 for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
543 mock_command_buffer->image_memory_barriers_.push_back(
544 pImageMemoryBarriers[i]);
545 }
546 }
547}
548
549void vkCmdSetStencilReference(VkCommandBuffer commandBuffer,
550 VkStencilFaceFlags faceMask,
551 uint32_t reference) {
552 MockCommandBuffer* mock_command_buffer =
553 reinterpret_cast<MockCommandBuffer*>(commandBuffer);
554 mock_command_buffer->called_functions_->push_back("vkCmdSetStencilReference");
555}
556
557void vkCmdSetScissor(VkCommandBuffer commandBuffer,
558 uint32_t firstScissor,
559 uint32_t scissorCount,
560 const VkRect2D* pScissors) {
561 MockCommandBuffer* mock_command_buffer =
562 reinterpret_cast<MockCommandBuffer*>(commandBuffer);
563 mock_command_buffer->called_functions_->push_back("vkCmdSetScissor");
564}
565
566void vkCmdSetViewport(VkCommandBuffer commandBuffer,
567 uint32_t firstViewport,
568 uint32_t viewportCount,
569 const VkViewport* pViewports) {
570 MockCommandBuffer* mock_command_buffer =
571 reinterpret_cast<MockCommandBuffer*>(commandBuffer);
572 mock_command_buffer->called_functions_->push_back("vkCmdSetViewport");
573 for (uint32_t i = 0; i < viewportCount; ++i) {
574 mock_command_buffer->recorded_viewports_.push_back(pViewports[i]);
575 }
576}
577
578void vkFreeCommandBuffers(VkDevice device,
579 VkCommandPool commandPool,
580 uint32_t commandBufferCount,
581 const VkCommandBuffer* pCommandBuffers) {
582 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
583 mock_device->AddCalledFunction("vkFreeCommandBuffers");
584}
585
586void vkDestroyCommandPool(VkDevice device,
587 VkCommandPool commandPool,
588 const VkAllocationCallbacks* pAllocator) {
589 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
590 mock_device->DeleteCommandPool(
591 reinterpret_cast<MockCommandPool*>(commandPool));
592 mock_device->AddCalledFunction("vkDestroyCommandPool");
593}
594
595VkResult vkEndCommandBuffer(VkCommandBuffer commandBuffer) {
596 return VK_SUCCESS;
597}
598
599VkResult vkCreateFence(VkDevice device,
600 const VkFenceCreateInfo* pCreateInfo,
601 const VkAllocationCallbacks* pAllocator,
602 VkFence* pFence) {
603 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
604 mock_device->AddCalledFunction("vkCreateFence");
605 *pFence = reinterpret_cast<VkFence>(new MockFence());
606 return VK_SUCCESS;
607}
608
609VkResult vkDestroyFence(VkDevice device,
610 VkFence fence,
611 const VkAllocationCallbacks* pAllocator) {
612 delete reinterpret_cast<MockFence*>(fence);
613 return VK_SUCCESS;
614}
615
616VkResult vkQueueSubmit(VkQueue queue,
617 uint32_t submitCount,
618 const VkSubmitInfo* pSubmits,
619 VkFence fence) {
620 return VK_SUCCESS;
621}
622
623VkResult vkWaitForFences(VkDevice device,
624 uint32_t fenceCount,
625 const VkFence* pFences,
626 VkBool32 waitAll,
627 uint64_t timeout) {
628 if (g_mock_vulkan_state && GetMockVulkanState().wait_for_fences_callback) {
629 return GetMockVulkanState().wait_for_fences_callback(
630 device, fenceCount, pFences, waitAll, timeout);
631 }
632 return VK_SUCCESS;
633}
634
635VkResult vkGetFenceStatus(VkDevice device, VkFence fence) {
636 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
637 MockFence* mock_fence = reinterpret_cast<MockFence*>(fence);
638 return mock_fence->GetStatus();
639}
640
641VkResult vkResetFences(VkDevice device,
642 uint32_t fenceCount,
643 const VkFence* fences) {
644 return VK_SUCCESS;
645}
646
647VkResult vkCreateDebugUtilsMessengerEXT(
648 VkInstance instance,
649 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
650 const VkAllocationCallbacks* pAllocator,
651 VkDebugUtilsMessengerEXT* pMessenger) {
652 return VK_SUCCESS;
653}
654
655VkResult vkSetDebugUtilsObjectNameEXT(
656 VkDevice device,
657 const VkDebugUtilsObjectNameInfoEXT* pNameInfo) {
658 return VK_SUCCESS;
659}
660
661VkResult vkCreateQueryPool(VkDevice device,
662 const VkQueryPoolCreateInfo* pCreateInfo,
663 const VkAllocationCallbacks* pAllocator,
664 VkQueryPool* pQueryPool) {
665 *pQueryPool = reinterpret_cast<VkQueryPool>(new MockQueryPool());
666 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
667 mock_device->AddCalledFunction("vkCreateQueryPool");
668 return VK_SUCCESS;
669}
670
671void vkDestroyQueryPool(VkDevice device,
672 VkQueryPool queryPool,
673 const VkAllocationCallbacks* pAllocator) {
674 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
675 mock_device->AddCalledFunction("vkDestroyQueryPool");
676 delete reinterpret_cast<MockQueryPool*>(queryPool);
677}
678
679VkResult vkGetQueryPoolResults(VkDevice device,
680 VkQueryPool queryPool,
681 uint32_t firstQuery,
682 uint32_t queryCount,
683 size_t dataSize,
684 void* pData,
685 VkDeviceSize stride,
686 VkQueryResultFlags flags) {
687 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
688 if (dataSize == sizeof(uint32_t)) {
689 uint32_t* data = static_cast<uint32_t*>(pData);
690 for (auto i = firstQuery; i < queryCount; i++) {
691 data[0] = i;
692 }
693 } else if (dataSize == sizeof(int64_t)) {
694 uint64_t* data = static_cast<uint64_t*>(pData);
695 for (auto i = firstQuery; i < queryCount; i++) {
696 data[0] = i;
697 }
698 }
699 mock_device->AddCalledFunction("vkGetQueryPoolResults");
700 return VK_SUCCESS;
701}
702
703VkResult vkCreateDescriptorPool(VkDevice device,
704 const VkDescriptorPoolCreateInfo* pCreateInfo,
705 const VkAllocationCallbacks* pAllocator,
706 VkDescriptorPool* pDescriptorPool) {
707 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
708 *pDescriptorPool =
709 reinterpret_cast<VkDescriptorPool>(new MockDescriptorPool());
710 mock_device->AddCalledFunction("vkCreateDescriptorPool");
711 return VK_SUCCESS;
712}
713
714void vkDestroyDescriptorPool(VkDevice device,
715 VkDescriptorPool descriptorPool,
716 const VkAllocationCallbacks* pAllocator) {
717 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
718 mock_device->AddCalledFunction("vkDestroyDescriptorPool");
719 delete reinterpret_cast<MockDescriptorPool*>(descriptorPool);
720}
721
722VkResult vkResetDescriptorPool(VkDevice device,
723 VkDescriptorPool descriptorPool,
724 VkDescriptorPoolResetFlags flags) {
725 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
726 mock_device->AddCalledFunction("vkResetDescriptorPool");
727 return VK_SUCCESS;
728}
729
730VkResult vkAllocateDescriptorSets(
731 VkDevice device,
732 const VkDescriptorSetAllocateInfo* pAllocateInfo,
733 VkDescriptorSet* pDescriptorSets) {
734 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
735 mock_device->AddCalledFunction("vkAllocateDescriptorSets");
736 return VK_SUCCESS;
737}
738
739VkResult vkGetPhysicalDeviceSurfaceFormatsKHR(
740 VkPhysicalDevice physicalDevice,
741 VkSurfaceKHR surface,
742 uint32_t* pSurfaceFormatCount,
743 VkSurfaceFormatKHR* pSurfaceFormats) {
744 *pSurfaceFormatCount = 1u;
745 if (pSurfaceFormats != nullptr) {
746 pSurfaceFormats[0] =
747 VkSurfaceFormatKHR{.format = VK_FORMAT_R8G8B8A8_UNORM,
748 .colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
749 }
750 return VK_SUCCESS;
751}
752
753VkResult vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
754 VkPhysicalDevice physicalDevice,
755 VkSurfaceKHR surface,
756 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities) {
757 *pSurfaceCapabilities = VkSurfaceCapabilitiesKHR{
758 .minImageCount = 3,
759 .maxImageCount = 6,
760 .currentExtent =
761 VkExtent2D{
762 .width = static_cast<uint32_t>(currentImageSize.width),
763 .height = static_cast<uint32_t>(currentImageSize.height),
764 },
765 .minImageExtent =
766 VkExtent2D{
767 .width = 0,
768 .height = 0,
769 },
770 .maxImageExtent =
771 VkExtent2D{
772 .width = static_cast<uint32_t>(currentImageSize.width),
773 .height = static_cast<uint32_t>(currentImageSize.height),
774 },
775 .maxImageArrayLayers = 1,
776 .supportedTransforms =
777 VkSurfaceTransformFlagBitsKHR::VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
778 .currentTransform =
779 VkSurfaceTransformFlagBitsKHR::VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
780 .supportedCompositeAlpha = VkCompositeAlphaFlagBitsKHR::
781 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
782 .supportedUsageFlags =
783 VkImageUsageFlagBits::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT};
784 return VK_SUCCESS;
785}
786
787VkResult vkGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice physicalDevice,
788 uint32_t queueFamilyIndex,
789 VkSurfaceKHR surface,
790 VkBool32* pSupported) {
791 *pSupported = VK_TRUE;
792 return VK_SUCCESS;
793}
794
795VkResult vkCreateSwapchainKHR(VkDevice device,
796 const VkSwapchainCreateInfoKHR* pCreateInfo,
797 const VkAllocationCallbacks* pAllocator,
798 VkSwapchainKHR* pSwapchain) {
799 *pSwapchain = reinterpret_cast<VkSwapchainKHR>(new MockSwapchainKHR());
800 return VK_SUCCESS;
801}
802
803void vkDestroySwapchainKHR(VkDevice device,
804 VkSwapchainKHR swapchain,
805 const VkAllocationCallbacks* pAllocator) {
806 delete reinterpret_cast<MockSwapchainKHR*>(swapchain);
807}
808
809VkResult vkGetSwapchainImagesKHR(VkDevice device,
810 VkSwapchainKHR swapchain,
811 uint32_t* pSwapchainImageCount,
812 VkImage* pSwapchainImages) {
813 MockSwapchainKHR* mock_swapchain =
814 reinterpret_cast<MockSwapchainKHR*>(swapchain);
815 auto& images = mock_swapchain->images;
816 *pSwapchainImageCount = images.size();
817 if (pSwapchainImages != nullptr) {
818 for (size_t i = 0; i < images.size(); i++) {
819 pSwapchainImages[i] = reinterpret_cast<VkImage>(&images[i]);
820 }
821 }
822 return VK_SUCCESS;
823}
824
825VkResult vkCreateSemaphore(VkDevice device,
826 const VkSemaphoreCreateInfo* pCreateInfo,
827 const VkAllocationCallbacks* pAllocator,
828 VkSemaphore* pSemaphore) {
829 *pSemaphore = reinterpret_cast<VkSemaphore>(new MockSemaphore());
830 return VK_SUCCESS;
831}
832
833void vkDestroySemaphore(VkDevice device,
834 VkSemaphore semaphore,
835 const VkAllocationCallbacks* pAllocator) {
836 delete reinterpret_cast<MockSemaphore*>(semaphore);
837}
838
839VkResult vkAcquireNextImageKHR(VkDevice device,
840 VkSwapchainKHR swapchain,
841 uint64_t timeout,
842 VkSemaphore semaphore,
843 VkFence fence,
844 uint32_t* pImageIndex) {
845 if (g_mock_vulkan_state && GetMockVulkanState().acquire_next_image_callback) {
846 return GetMockVulkanState().acquire_next_image_callback(
847 device, swapchain, timeout, semaphore, fence, pImageIndex);
848 }
849 auto current_index =
850 reinterpret_cast<MockSwapchainKHR*>(swapchain)->current_image++;
851 *pImageIndex = (current_index + 1) % 3u;
852 return VK_SUCCESS;
853}
854
855VkResult vkCreateFramebuffer(VkDevice device,
856 const VkFramebufferCreateInfo* pCreateInfo,
857 const VkAllocationCallbacks* pAllocator,
858 VkFramebuffer* pFramebuffer) {
859 *pFramebuffer = reinterpret_cast<VkFramebuffer>(new MockFramebuffer());
860 return VK_SUCCESS;
861}
862
863void vkDestroyFramebuffer(VkDevice device,
864 VkFramebuffer framebuffer,
865 const VkAllocationCallbacks* pAllocator) {
866 delete reinterpret_cast<MockFramebuffer*>(framebuffer);
867}
868
869void vkTrimCommandPool(VkDevice device,
870 VkCommandPool commandPool,
871 VkCommandPoolTrimFlags flags) {
872 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
873 mock_device->AddCalledFunction("vkTrimCommandPool");
874}
875
876VkResult vkGetPipelineCacheData(VkDevice device,
877 VkPipelineCache pipelineCache,
878 size_t* pDataSize,
879 void* pData) {
880 if (pData) {
881 const std::array<uint8_t, 5> cache_data{1, 2, 3, 4, 5};
882 size_t dst_buffer_size = *pDataSize;
883 size_t length = std::min(dst_buffer_size, cache_data.size());
884 std::memcpy(pData, cache_data.data(), length);
885 *pDataSize = length;
886 return (dst_buffer_size >= length) ? VK_SUCCESS : VK_INCOMPLETE;
887 } else {
888 *pDataSize = 10;
889 return VK_SUCCESS;
890 }
891}
892
893PFN_vkVoidFunction GetMockVulkanProcAddress(VkInstance instance,
894 const char* pName) {
895 if (strcmp("vkEnumerateInstanceExtensionProperties", pName) == 0) {
896 return reinterpret_cast<PFN_vkVoidFunction>(
897 vkEnumerateInstanceExtensionProperties);
898 } else if (strcmp("vkEnumerateInstanceLayerProperties", pName) == 0) {
899 return reinterpret_cast<PFN_vkVoidFunction>(
900 vkEnumerateInstanceLayerProperties);
901 } else if (strcmp("vkEnumeratePhysicalDevices", pName) == 0) {
902 return reinterpret_cast<PFN_vkVoidFunction>(vkEnumeratePhysicalDevices);
903 } else if (strcmp("vkGetPhysicalDeviceFormatProperties", pName) == 0) {
904 return reinterpret_cast<PFN_vkVoidFunction>(
905 vkGetPhysicalDeviceFormatProperties);
906 } else if (strcmp("vkGetPhysicalDeviceProperties", pName) == 0) {
907 return reinterpret_cast<PFN_vkVoidFunction>(vkGetPhysicalDeviceProperties);
908 } else if (strcmp("vkGetPhysicalDeviceQueueFamilyProperties", pName) == 0) {
909 return reinterpret_cast<PFN_vkVoidFunction>(
910 vkGetPhysicalDeviceQueueFamilyProperties);
911 } else if (strcmp("vkEnumerateDeviceExtensionProperties", pName) == 0) {
912 return reinterpret_cast<PFN_vkVoidFunction>(
913 vkEnumerateDeviceExtensionProperties);
914 } else if (strcmp("vkCreateDevice", pName) == 0) {
915 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateDevice);
916 } else if (strcmp("vkCreateInstance", pName) == 0) {
917 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateInstance);
918 } else if (strcmp("vkGetPhysicalDeviceMemoryProperties", pName) == 0) {
919 return reinterpret_cast<PFN_vkVoidFunction>(
920 vkGetPhysicalDeviceMemoryProperties);
921 } else if (strcmp("vkCreatePipelineCache", pName) == 0) {
922 return reinterpret_cast<PFN_vkVoidFunction>(vkCreatePipelineCache);
923 } else if (strcmp("vkCreateCommandPool", pName) == 0) {
924 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateCommandPool);
925 } else if (strcmp("vkResetCommandPool", pName) == 0) {
926 return reinterpret_cast<PFN_vkVoidFunction>(vkResetCommandPool);
927 } else if (strcmp("vkAllocateCommandBuffers", pName) == 0) {
928 return reinterpret_cast<PFN_vkVoidFunction>(vkAllocateCommandBuffers);
929 } else if (strcmp("vkBeginCommandBuffer", pName) == 0) {
930 return reinterpret_cast<PFN_vkVoidFunction>(vkBeginCommandBuffer);
931 } else if (strcmp("vkCreateImage", pName) == 0) {
932 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateImage);
933 } else if (strcmp("vkGetInstanceProcAddr", pName) == 0) {
934 return reinterpret_cast<PFN_vkVoidFunction>(GetMockVulkanProcAddress);
935 } else if (strcmp("vkGetDeviceProcAddr", pName) == 0) {
936 return reinterpret_cast<PFN_vkVoidFunction>(GetMockVulkanProcAddress);
937 } else if (strcmp("vkGetImageMemoryRequirements2KHR", pName) == 0 ||
938 strcmp("vkGetImageMemoryRequirements2", pName) == 0) {
939 return reinterpret_cast<PFN_vkVoidFunction>(
940 vkGetImageMemoryRequirements2KHR);
941 } else if (strcmp("vkAllocateMemory", pName) == 0) {
942 return reinterpret_cast<PFN_vkVoidFunction>(vkAllocateMemory);
943 } else if (strcmp("vkBindImageMemory", pName) == 0) {
944 return reinterpret_cast<PFN_vkVoidFunction>(vkBindImageMemory);
945 } else if (strcmp("vkCreateImageView", pName) == 0) {
946 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateImageView);
947 } else if (strcmp("vkCreateBuffer", pName) == 0) {
948 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateBuffer);
949 } else if (strcmp("vkGetBufferMemoryRequirements2KHR", pName) == 0 ||
950 strcmp("vkGetBufferMemoryRequirements2", pName) == 0) {
951 return reinterpret_cast<PFN_vkVoidFunction>(
952 vkGetBufferMemoryRequirements2KHR);
953 } else if (strcmp("vkBindBufferMemory", pName) == 0) {
954 return reinterpret_cast<PFN_vkVoidFunction>(vkBindBufferMemory);
955 } else if (strcmp("vkCreateRenderPass", pName) == 0) {
956 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateRenderPass);
957 } else if (strcmp("vkCreateDescriptorSetLayout", pName) == 0) {
958 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateDescriptorSetLayout);
959 } else if (strcmp("vkCreatePipelineLayout", pName) == 0) {
960 return reinterpret_cast<PFN_vkVoidFunction>(vkCreatePipelineLayout);
961 } else if (strcmp("vkCreateGraphicsPipelines", pName) == 0) {
962 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateGraphicsPipelines);
963 } else if (strcmp("vkDestroyDevice", pName) == 0) {
964 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyDevice);
965 } else if (strcmp("vkDestroyInstance", pName) == 0) {
966 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyInstance);
967 } else if (strcmp("vkDestroyPipeline", pName) == 0) {
968 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyPipeline);
969 } else if (strcmp("vkCreateShaderModule", pName) == 0) {
970 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateShaderModule);
971 } else if (strcmp("vkDestroyShaderModule", pName) == 0) {
972 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyShaderModule);
973 } else if (strcmp("vkDestroyPipelineCache", pName) == 0) {
974 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyPipelineCache);
975 } else if (strcmp("vkCmdBindPipeline", pName) == 0) {
976 return reinterpret_cast<PFN_vkVoidFunction>(vkCmdBindPipeline);
977 } else if (strcmp("vkCmdPipelineBarrier", pName) == 0) {
978 return reinterpret_cast<PFN_vkVoidFunction>(vkCmdPipelineBarrier);
979 } else if (strcmp("vkCmdSetStencilReference", pName) == 0) {
980 return reinterpret_cast<PFN_vkVoidFunction>(vkCmdSetStencilReference);
981 } else if (strcmp("vkCmdSetScissor", pName) == 0) {
982 return reinterpret_cast<PFN_vkVoidFunction>(vkCmdSetScissor);
983 } else if (strcmp("vkCmdSetViewport", pName) == 0) {
984 return reinterpret_cast<PFN_vkVoidFunction>(vkCmdSetViewport);
985 } else if (strcmp("vkDestroyCommandPool", pName) == 0) {
986 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyCommandPool);
987 } else if (strcmp("vkFreeCommandBuffers", pName) == 0) {
988 return reinterpret_cast<PFN_vkVoidFunction>(vkFreeCommandBuffers);
989 } else if (strcmp("vkEndCommandBuffer", pName) == 0) {
990 return reinterpret_cast<PFN_vkVoidFunction>(vkEndCommandBuffer);
991 } else if (strcmp("vkCreateFence", pName) == 0) {
992 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateFence);
993 } else if (strcmp("vkDestroyFence", pName) == 0) {
994 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyFence);
995 } else if (strcmp("vkQueueSubmit", pName) == 0) {
996 return reinterpret_cast<PFN_vkVoidFunction>(vkQueueSubmit);
997 } else if (strcmp("vkWaitForFences", pName) == 0) {
998 return reinterpret_cast<PFN_vkVoidFunction>(vkWaitForFences);
999 } else if (strcmp("vkGetFenceStatus", pName) == 0) {
1000 return reinterpret_cast<PFN_vkVoidFunction>(vkGetFenceStatus);
1001 } else if (strcmp("vkResetFences", pName) == 0) {
1002 return reinterpret_cast<PFN_vkVoidFunction>(vkResetFences);
1003 } else if (strcmp("vkCreateDebugUtilsMessengerEXT", pName) == 0) {
1004 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateDebugUtilsMessengerEXT);
1005 } else if (strcmp("vkSetDebugUtilsObjectNameEXT", pName) == 0) {
1006 return reinterpret_cast<PFN_vkVoidFunction>(vkSetDebugUtilsObjectNameEXT);
1007 } else if (strcmp("vkCreateQueryPool", pName) == 0) {
1008 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateQueryPool);
1009 } else if (strcmp("vkDestroyQueryPool", pName) == 0) {
1010 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyQueryPool);
1011 } else if (strcmp("vkGetQueryPoolResults", pName) == 0) {
1012 return reinterpret_cast<PFN_vkVoidFunction>(vkGetQueryPoolResults);
1013 } else if (strcmp("vkCreateDescriptorPool", pName) == 0) {
1014 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateDescriptorPool);
1015 } else if (strcmp("vkDestroyDescriptorPool", pName) == 0) {
1016 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyDescriptorPool);
1017 } else if (strcmp("vkResetDescriptorPool", pName) == 0) {
1018 return reinterpret_cast<PFN_vkVoidFunction>(vkResetDescriptorPool);
1019 } else if (strcmp("vkAllocateDescriptorSets", pName) == 0) {
1020 return reinterpret_cast<PFN_vkVoidFunction>(vkAllocateDescriptorSets);
1021 } else if (strcmp("vkGetPhysicalDeviceSurfaceFormatsKHR", pName) == 0) {
1022 return reinterpret_cast<PFN_vkVoidFunction>(
1023 vkGetPhysicalDeviceSurfaceFormatsKHR);
1024 } else if (strcmp("vkGetPhysicalDeviceSurfaceCapabilitiesKHR", pName) == 0) {
1025 return reinterpret_cast<PFN_vkVoidFunction>(
1026 vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
1027 } else if (strcmp("vkGetPhysicalDeviceSurfaceSupportKHR", pName) == 0) {
1028 return reinterpret_cast<PFN_vkVoidFunction>(
1029 vkGetPhysicalDeviceSurfaceSupportKHR);
1030 } else if (strcmp("vkCreateSwapchainKHR", pName) == 0) {
1031 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateSwapchainKHR);
1032 } else if (strcmp("vkDestroySwapchainKHR", pName) == 0) {
1033 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroySwapchainKHR);
1034 } else if (strcmp("vkGetSwapchainImagesKHR", pName) == 0) {
1035 return reinterpret_cast<PFN_vkVoidFunction>(vkGetSwapchainImagesKHR);
1036 } else if (strcmp("vkCreateSemaphore", pName) == 0) {
1037 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateSemaphore);
1038 } else if (strcmp("vkDestroySemaphore", pName) == 0) {
1039 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroySemaphore);
1040 } else if (strcmp("vkDestroySurfaceKHR", pName) == 0) {
1041 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroySurfaceKHR);
1042 } else if (strcmp("vkAcquireNextImageKHR", pName) == 0) {
1043 return reinterpret_cast<PFN_vkVoidFunction>(vkAcquireNextImageKHR);
1044 } else if (strcmp("vkCreateFramebuffer", pName) == 0) {
1045 return reinterpret_cast<PFN_vkVoidFunction>(vkCreateFramebuffer);
1046 } else if (strcmp("vkDestroyFramebuffer", pName) == 0) {
1047 return reinterpret_cast<PFN_vkVoidFunction>(vkDestroyFramebuffer);
1048 } else if (strcmp("vkTrimCommandPool", pName) == 0) {
1049 return reinterpret_cast<PFN_vkVoidFunction>(vkTrimCommandPool);
1050 } else if (strcmp("vkGetPipelineCacheData", pName) == 0) {
1051 return reinterpret_cast<PFN_vkVoidFunction>(vkGetPipelineCacheData);
1052 }
1053 return noop;
1054}
1055
1056} // namespace
1057
1059 : instance_extensions_({"VK_KHR_surface", "VK_MVK_macos_surface"}),
1060 device_extensions_({"VK_KHR_swapchain"}),
1061 format_properties_callback_([](VkPhysicalDevice physicalDevice,
1062 VkFormat format,
1063 VkFormatProperties* pFormatProperties) {
1064 if (format == VK_FORMAT_R8G8B8A8_UNORM) {
1065 pFormatProperties->optimalTilingFeatures =
1066 static_cast<VkFormatFeatureFlags>(
1067 vk::FormatFeatureFlagBits::eColorAttachment);
1068 } else if (format == VK_FORMAT_D32_SFLOAT_S8_UINT) {
1069 pFormatProperties->optimalTilingFeatures =
1070 static_cast<VkFormatFeatureFlags>(
1071 vk::FormatFeatureFlagBits::eDepthStencilAttachment);
1072 } else if (format == VK_FORMAT_S8_UINT) {
1073 pFormatProperties->optimalTilingFeatures =
1074 static_cast<VkFormatFeatureFlags>(
1075 vk::FormatFeatureFlagBits::eDepthStencilAttachment);
1076 }
1077 }) {}
1078
1079std::shared_ptr<ContextVK> MockVulkanContextBuilder::Build() {
1080 auto message_loop = fml::ConcurrentMessageLoop::Create();
1081 ContextVK::Settings settings;
1082 settings.proc_address_callback = GetMockVulkanProcAddress;
1083 if (settings_callback_) {
1084 settings_callback_(settings);
1085 }
1086 g_mock_vulkan_state.reset(new MockVulkanState());
1087 g_mock_vulkan_state->instance_extensions = instance_extensions_;
1088 g_mock_vulkan_state->instance_layers = instance_layers_;
1089 g_mock_vulkan_state->device_extensions = device_extensions_;
1090 g_mock_vulkan_state->format_properties_callback = format_properties_callback_;
1091 g_mock_vulkan_state->physical_device_properties_callback =
1092 physical_properties_callback_;
1093 g_mock_vulkan_state->acquire_next_image_callback =
1094 acquire_next_image_callback_;
1095 g_mock_vulkan_state->wait_for_fences_callback = wait_for_fences_callback_;
1096 settings.embedder_data = embedder_data_;
1097 std::shared_ptr<ContextVK> result = ContextVK::Create(std::move(settings));
1098 return result;
1099}
1100
1101std::shared_ptr<std::vector<std::string>> GetMockVulkanFunctions(
1102 VkDevice device) {
1103 MockDevice* mock_device = reinterpret_cast<MockDevice*>(device);
1104 return mock_device->GetCalledFunctions();
1105}
1106
1108 currentImageSize = size;
1109}
1110
1111std::vector<VkImageMemoryBarrier>& GetImageMemoryBarriers(
1112 VkCommandBuffer buffer) {
1113 MockCommandBuffer* mock_command_buffer =
1114 reinterpret_cast<MockCommandBuffer*>(buffer);
1115 return mock_command_buffer->image_memory_barriers_;
1116}
1117
1118const std::vector<VkViewport>& GetRecordedViewports(VkCommandBuffer buffer) {
1119 MockCommandBuffer* mock_command_buffer =
1120 reinterpret_cast<MockCommandBuffer*>(buffer);
1121 return mock_command_buffer->recorded_viewports_;
1122}
1123
1124} // namespace testing
1125} // namespace impeller
static std::shared_ptr< ConcurrentMessageLoop > Create(size_t worker_count=std::thread::hardware_concurrency())
static std::shared_ptr< ContextVK > Create(Settings settings)
MockCommandBuffer(std::weak_ptr< const Context > context)
Definition mocks.h:143
std::shared_ptr< ContextVK > Build()
Create a Vulkan context with Vulkan functions mocked. The caller is given a chance to tinker on the s...
FlutterVulkanImage * image
VkSwapchainKHR swapchain
Definition main.cc:80
VkDevice device
Definition main.cc:69
VkInstance instance
Definition main.cc:64
VkQueue queue
Definition main.cc:71
#define FML_CHECK(condition)
Definition logging.h:104
size_t length
size_t current_image
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< void(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties)> physical_device_properties_callback
std::function< std::remove_pointer_t< PFN_vkWaitForFences > > wait_for_fences_callback
std::shared_ptr< std::vector< std::string > > called_functions_
std::array< MockImage, 3 > images
std::vector< std::string > instance_extensions
std::function< std::remove_pointer_t< PFN_vkAcquireNextImageKHR > > acquire_next_image_callback
std::vector< VkImageMemoryBarrier > image_memory_barriers_
std::vector< VkViewport > recorded_viewports_
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
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
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.
constexpr Color operator*(T value, const Color &c)
Definition color.h:914
ISize64 ISize
Definition size.h:162
Definition ref_ptr.h:261
int32_t height
PFN_vkGetInstanceProcAddr proc_address_callback
Definition context_vk.h:80
std::optional< EmbedderData > embedder_data
Definition context_vk.h:90
#define IPLR_GUARDED_BY(x)