1 /*
2  * Copyright © 2022 Collabora, Ltd
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef VK_COMMAND_POOL_H
25 #define VK_COMMAND_POOL_H
26 
27 #include "vk_object.h"
28 #include "util/list.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /** Base object for implementin VkCommandPool */
35 struct vk_command_pool {
36    struct vk_object_base base;
37 
38    /** VkCommandPoolCreateInfo::flags */
39    VkCommandPoolCreateFlags flags;
40 
41    /** VkCommandPoolCreateInfo::queueFamilyIndex */
42    uint32_t queue_family_index;
43 
44    /** Allocator passed to vkCreateCommandPool() */
45    VkAllocationCallbacks alloc;
46 
47    /** Command buffer vtable for command buffers allocated from this pool */
48    const struct vk_command_buffer_ops *command_buffer_ops;
49 
50    /** True if we should recycle command buffers */
51    bool recycle_command_buffers;
52 
53    /** List of all command buffers */
54    struct list_head command_buffers;
55 
56    /** List of freed command buffers for trimming. */
57    struct list_head free_command_buffers;
58 };
59 
60 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_command_pool, base, VkCommandPool,
61                                VK_OBJECT_TYPE_COMMAND_POOL);
62 
63 /** Initialize a vk_command_pool
64  *
65  * @param[in]  device      The Vulkan device
66  * @param[out] pool        The command pool to initialize
67  * @param[in]  pCreateInfo VkCommandPoolCreateInfo pointer passed to
68  *                         `vkCreateCommandPool()`
69  * @param[in]  pAllocator  Allocation callbacks passed to
70  *                         `vkCreateCommandPool()`
71  */
72 VkResult MUST_CHECK
73 vk_command_pool_init(struct vk_device *device,
74                      struct vk_command_pool *pool,
75                      const VkCommandPoolCreateInfo *pCreateInfo,
76                      const VkAllocationCallbacks *pAllocator);
77 
78 /** Tear down a vk_command_pool
79  *
80  * @param[inout]  pool     The command pool to tear down
81  */
82 void
83 vk_command_pool_finish(struct vk_command_pool *pool);
84 
85 /** Trim a vk_command_pool
86  *
87  * This discards any resources that may be cached by the common
88  * vk_command_pool code.  For driver-implemented command pools, drivers should
89  * call this function inside their `vkTrimCommandPool()` implementation.  This
90  * should be called before doing any driver-specific trimming in case it ends
91  * up returning driver-internal resources to the pool.
92  *
93  * @param[inout]  pool     The command pool to trim
94  * @param[in]     flags    Flags controling the trim operation
95  */
96 void
97 vk_command_pool_trim(struct vk_command_pool *pool,
98                      VkCommandPoolTrimFlags flags);
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif  /* VK_COMMAND_POOL_H */
105