xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/vulkan/SecondaryCommandPool.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2023 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // SecondaryCommandPool:
7 //    A class for allocating Command Buffers for VulkanSecondaryCommandBuffer.
8 //
9 
10 #ifndef LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
11 #define LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
12 
13 #include "common/FixedQueue.h"
14 #include "common/SimpleMutex.h"
15 #include "libANGLE/renderer/vulkan/vk_command_buffer_utils.h"
16 #include "libANGLE/renderer/vulkan/vk_wrapper.h"
17 
18 namespace rx
19 {
20 namespace vk
21 {
22 class Context;
23 class VulkanSecondaryCommandBuffer;
24 
25 // VkCommandPool must be externally synchronized when its Command Buffers are: allocated, freed,
26 // reset, or recorded. This class ensures that Command Buffers are freed from the thread that
27 // recording commands (Context thread).
28 class SecondaryCommandPool final : angle::NonCopyable
29 {
30   public:
31     SecondaryCommandPool();
32     ~SecondaryCommandPool();
33 
34     angle::Result init(Context *context, uint32_t queueFamilyIndex, ProtectionType protectionType);
35     void destroy(VkDevice device);
36 
valid()37     bool valid() const { return mCommandPool.valid(); }
38 
39     // Call only from the Context thread that owns the SecondaryCommandPool instance.
40     angle::Result allocate(Context *context, VulkanSecondaryCommandBuffer *buffer);
41 
42     // Single threaded - use external synchronization.
43     // Example threads: any Context thread or "asyncCommandQueue" thread.
44     void collect(VulkanSecondaryCommandBuffer *buffer);
45 
46   private:
47     static constexpr size_t kFixedQueueLimit = 100u;
48 
49     // Context thread access members.
50 
51     void freeCollectedBuffers(VkDevice device);
52 
53     // Use single pool for now.
54     CommandPool mCommandPool;
55 
56     // Other thread access members.
57 
58     // Fast lock free queue for processing buffers while new may be added from the other thread.
59     angle::FixedQueue<VkCommandBuffer> mCollectedBuffers;
60 
61     // Overflow vector to use in cases when FixedQueue is filled.
62     std::vector<VkCommandBuffer> mCollectedBuffersOverflow;
63     angle::SimpleMutex mOverflowMutex;
64     std::atomic<bool> mHasOverflow;
65 };
66 
67 }  // namespace vk
68 }  // namespace rx
69 
70 #endif  // LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
71