33// found in the LICENSE file.
44
55#include " impeller/renderer/backend/vulkan/test/mock_vulkan.h"
6+ #include < vector>
7+ #include " fml/macros.h"
8+ #include " impeller/base/thread_safety.h"
69
710namespace impeller {
811namespace testing {
@@ -16,17 +19,37 @@ struct MockCommandBuffer {
1619 std::shared_ptr<std::vector<std::string>> called_functions_;
1720};
1821
19- struct MockDevice {
20- MockDevice () : called_functions_(new std::vector<std::string>()) {}
22+ class MockDevice final {
23+ public:
24+ explicit MockDevice () : called_functions_(new std::vector<std::string>()) {}
25+
2126 MockCommandBuffer* NewCommandBuffer () {
22- std::unique_ptr<MockCommandBuffer> buffer =
23- std::make_unique<MockCommandBuffer>(called_functions_);
27+ auto buffer = std::make_unique<MockCommandBuffer>(called_functions_);
2428 MockCommandBuffer* result = buffer.get ();
29+ Lock lock (command_buffers_mutex_);
2530 command_buffers_.emplace_back (std::move (buffer));
2631 return result;
2732 }
28- std::shared_ptr<std::vector<std::string>> called_functions_;
29- std::vector<std::unique_ptr<MockCommandBuffer>> command_buffers_;
33+
34+ const std::shared_ptr<std::vector<std::string>>& GetCalledFunctions () {
35+ return called_functions_;
36+ }
37+
38+ void AddCalledFunction (const std::string& function) {
39+ Lock lock (called_functions_mutex_);
40+ called_functions_->push_back (function);
41+ }
42+
43+ private:
44+ FML_DISALLOW_COPY_AND_ASSIGN (MockDevice);
45+
46+ Mutex called_functions_mutex_;
47+ std::shared_ptr<std::vector<std::string>> called_functions_
48+ IPLR_GUARDED_BY (called_functions_mutex_);
49+
50+ Mutex command_buffers_mutex_;
51+ std::vector<std::unique_ptr<MockCommandBuffer>> command_buffers_
52+ IPLR_GUARDED_BY (command_buffers_mutex_);
3053};
3154
3255void noop () {}
@@ -147,7 +170,7 @@ VkResult vkCreatePipelineCache(VkDevice device,
147170 const VkAllocationCallbacks* pAllocator,
148171 VkPipelineCache* pPipelineCache) {
149172 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
150- mock_device->called_functions_ -> push_back (" vkCreatePipelineCache" );
173+ mock_device->AddCalledFunction (" vkCreatePipelineCache" );
151174 *pPipelineCache = reinterpret_cast <VkPipelineCache>(0xb000dead );
152175 return VK_SUCCESS;
153176}
@@ -270,30 +293,30 @@ VkResult vkCreateGraphicsPipelines(
270293 const VkAllocationCallbacks* pAllocator,
271294 VkPipeline* pPipelines) {
272295 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
273- mock_device->called_functions_ -> push_back (" vkCreateGraphicsPipelines" );
296+ mock_device->AddCalledFunction (" vkCreateGraphicsPipelines" );
274297 *pPipelines = reinterpret_cast <VkPipeline>(0x99999999 );
275298 return VK_SUCCESS;
276299}
277300
278301void vkDestroyDevice (VkDevice device, const VkAllocationCallbacks* pAllocator) {
279302 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
280- mock_device->called_functions_ -> push_back (" vkDestroyDevice" );
303+ mock_device->AddCalledFunction (" vkDestroyDevice" );
281304 delete reinterpret_cast <MockDevice*>(device);
282305}
283306
284307void vkDestroyPipeline (VkDevice device,
285308 VkPipeline pipeline,
286309 const VkAllocationCallbacks* pAllocator) {
287310 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
288- mock_device->called_functions_ -> push_back (" vkDestroyPipeline" );
311+ mock_device->AddCalledFunction (" vkDestroyPipeline" );
289312}
290313
291314VkResult vkCreateShaderModule (VkDevice device,
292315 const VkShaderModuleCreateInfo* pCreateInfo,
293316 const VkAllocationCallbacks* pAllocator,
294317 VkShaderModule* pShaderModule) {
295318 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
296- mock_device->called_functions_ -> push_back (" vkCreateShaderModule" );
319+ mock_device->AddCalledFunction (" vkCreateShaderModule" );
297320 *pShaderModule = reinterpret_cast <VkShaderModule>(0x11111111 );
298321 return VK_SUCCESS;
299322}
@@ -302,14 +325,14 @@ void vkDestroyShaderModule(VkDevice device,
302325 VkShaderModule shaderModule,
303326 const VkAllocationCallbacks* pAllocator) {
304327 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
305- mock_device->called_functions_ -> push_back (" vkDestroyShaderModule" );
328+ mock_device->AddCalledFunction (" vkDestroyShaderModule" );
306329}
307330
308331void vkDestroyPipelineCache (VkDevice device,
309332 VkPipelineCache pipelineCache,
310333 const VkAllocationCallbacks* pAllocator) {
311334 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
312- mock_device->called_functions_ -> push_back (" vkDestroyPipelineCache" );
335+ mock_device->AddCalledFunction (" vkDestroyPipelineCache" );
313336}
314337
315338void vkCmdBindPipeline (VkCommandBuffer commandBuffer,
@@ -351,14 +374,14 @@ void vkFreeCommandBuffers(VkDevice device,
351374 uint32_t commandBufferCount,
352375 const VkCommandBuffer* pCommandBuffers) {
353376 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
354- mock_device->called_functions_ -> push_back (" vkFreeCommandBuffers" );
377+ mock_device->AddCalledFunction (" vkFreeCommandBuffers" );
355378}
356379
357380void vkDestroyCommandPool (VkDevice device,
358381 VkCommandPool commandPool,
359382 const VkAllocationCallbacks* pAllocator) {
360383 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
361- mock_device->called_functions_ -> push_back (" vkDestroyCommandPool" );
384+ mock_device->AddCalledFunction (" vkDestroyCommandPool" );
362385}
363386
364387VkResult vkEndCommandBuffer (VkCommandBuffer commandBuffer) {
@@ -500,7 +523,7 @@ std::shared_ptr<ContextVK> CreateMockVulkanContext(void) {
500523std::shared_ptr<std::vector<std::string>> GetMockVulkanFunctions (
501524 VkDevice device) {
502525 MockDevice* mock_device = reinterpret_cast <MockDevice*>(device);
503- return mock_device->called_functions_ ;
526+ return mock_device->GetCalledFunctions () ;
504527}
505528
506529} // namespace testing
0 commit comments