Monado OpenXR Runtime
u_native_images_debug.h
Go to the documentation of this file.
1 // Copyright 2023-2024, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Special code for managing a variable tracked swapchain.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup aux_util
8  */
9 
10 #pragma once
11 
12 #include "os/os_threading.h"
13 #include "xrt/xrt_compositor.h"
14 
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 
21 /*!
22  * A struct for debugging one or more native images.
23  *
24  * @ingroup aux_util
25  */
27 {
28  //! Is initialised/destroyed when added or root is removed.
29  struct os_mutex mutex;
30 
31  /*!
32  * Process unique id for the set of images, protected by @p mutex,
33  * allows caching of imports. Created by @ref u_limited_unique_id_get.
34  */
36 
37  //! List to current set of native images, protected by @p mutex.
39 
40  //! Count of @p native_images, protected by @p mutex.
42 
43  /*!
44  * Information needed to import the native images, information in the
45  * struct is immutable, the pointer is protected by @p mutex.
46  */
48 
49  /*!
50  * The native image that was last filled in by the source, only
51  * valid if @p native_images is non-null, protected by @p mutex.
52  */
53  uint32_t active_index;
54 
55  //! Should the image be flipped in y direction.
56  bool flip_y;
57 };
58 
59 /*!
60  * Must be called before variable is tracked.
61  *
62  * @ingroup aux_util
63  */
64 static inline void
66 {
67  os_mutex_init(&unid->mutex);
68 }
69 
70 /*!
71  * Must not be called while variable longer tracked, after @p u_var_remove_root.
72  *
73  * @ingroup aux_util
74  */
75 static inline void
77 {
78  os_mutex_destroy(&unid->mutex);
79  unid->native_images = NULL;
80  unid->native_image_count = 0;
81  unid->xscci = NULL;
82  unid->active_index = 0;
83  unid->flip_y = false;
84 }
85 
86 /*!
87  * Simple lock helper.
88  *
89  * @ingroup aux_util
90  */
91 static inline void
93 {
94  os_mutex_lock(&unid->mutex);
95 }
96 
97 /*!
98  * Simple lock helper.
99  *
100  * @ingroup aux_util
101  */
102 static inline void
104 {
105  os_mutex_unlock(&unid->mutex);
106 }
107 
108 /*!
109  * Helper function to update all variables, must be called with the lock held.
110  *
111  * @ingroup aux_util
112  */
113 static inline void
115  xrt_limited_unique_id_t limited_unique_id,
116  struct xrt_image_native *native_images,
117  uint32_t native_image_count,
118  const struct xrt_swapchain_create_info *xscci,
119  uint32_t active_index,
120  bool flip_y)
121 {
122  unid->limited_unique_id = limited_unique_id;
123  unid->native_images = native_images;
124  unid->native_image_count = native_image_count;
125  unid->active_index = active_index;
126  unid->xscci = xscci;
127  unid->flip_y = flip_y;
128 }
129 
130 /*!
131  * Updates all variables atomically by holding the lock.
132  *
133  * @ingroup aux_util
134  */
135 static inline void
137  xrt_limited_unique_id_t limited_unique_id,
138  struct xrt_image_native *native_images,
139  uint32_t native_image_count,
140  const struct xrt_swapchain_create_info *xscci,
141  uint32_t active_index,
142  bool flip_y)
143 {
146  unid, //
147  limited_unique_id, //
148  native_images, //
149  native_image_count, //
150  xscci, //
151  active_index, //
152  flip_y); //
154 }
155 
156 /*!
157  * Clear all variables, must be called with the lock held.
158  *
159  * @ingroup aux_util
160  */
161 static inline void
163 {
164  unid->limited_unique_id.data = 0;
165  unid->xscci = NULL;
166  unid->active_index = 0;
167  unid->native_images = NULL;
168  unid->native_image_count = 0;
169 }
170 
171 /*!
172  * Clear all variables atomically by holding the lock, still valid to use.
173  *
174  * @ingroup aux_util
175  */
176 static inline void
178 {
182 }
183 
184 
185 /*
186  *
187  * Swapchain.
188  *
189  */
190 
191 /*!
192  * Allows to debug image that is in GPU memory.
193  *
194  * @ingroup aux_util
195  */
197 {
198  //! Base for native image debugging.
200 
201  //! Protected by @p base::mutex.
203 };
204 
205 /*!
206  * Must be called before variable is tracked.
207  *
208  * @ingroup aux_util
209  */
210 static inline void
212 {
214 }
215 
216 /*!
217  * Updates all variables atomically by holding the lock.
218  *
219  * @ingroup aux_util
220  */
221 static inline void
223  struct xrt_swapchain_native *xscn,
224  const struct xrt_swapchain_create_info *xscci,
225  uint32_t active_index,
226  bool flip_y)
227 {
229 
231  &uscd->base, //
232  xscn->limited_unique_id, //
233  xscn->images, //
234  xscn->base.image_count, //
235  xscci, //
236  active_index, //
237  flip_y); //
238 
239  xrt_swapchain_native_reference(&uscd->xscn, xscn);
240 
242 }
243 
244 /*!
245  * Clear all variables atomically by holding the lock, still valid to use.
246  *
247  * @ingroup aux_util
248  */
249 static inline void
251 {
254  xrt_swapchain_native_reference(&uscd->xscn, NULL);
256 }
257 
258 /*!
259  * Must not be called while variable longer tracked, after @p u_var_remove_root.
260  *
261  * @ingroup aux_util
262  */
263 static inline void
265 {
266  xrt_swapchain_native_reference(&uscd->xscn, NULL);
267  uscd->base.active_index = 0;
268  os_mutex_destroy(&uscd->base.mutex);
269 }
270 
271 /*!
272  * Simple lock helper.
273  *
274  * @ingroup aux_util
275  */
276 static inline void
278 {
280 }
281 
282 /*!
283  * Simple lock helper.
284  *
285  * @ingroup aux_util
286  */
287 static inline void
289 {
291 }
292 
293 
294 #ifdef __cplusplus
295 }
296 #endif
static int os_mutex_init(struct os_mutex *om)
Init.
Definition: os_threading.h:70
static void os_mutex_lock(struct os_mutex *om)
Lock.
Definition: os_threading.h:86
static void os_mutex_unlock(struct os_mutex *om)
Unlock.
Definition: os_threading.h:110
static void os_mutex_destroy(struct os_mutex *om)
Clean up.
Definition: os_threading.h:122
static void u_swapchain_debug_unlock(struct u_swapchain_debug *uscd)
Simple lock helper.
Definition: u_native_images_debug.h:288
static void u_native_images_debug_set_locked(struct u_native_images_debug *unid, xrt_limited_unique_id_t limited_unique_id, struct xrt_image_native *native_images, uint32_t native_image_count, const struct xrt_swapchain_create_info *xscci, uint32_t active_index, bool flip_y)
Helper function to update all variables, must be called with the lock held.
Definition: u_native_images_debug.h:114
static void u_native_images_debug_clear(struct u_native_images_debug *unid)
Clear all variables atomically by holding the lock, still valid to use.
Definition: u_native_images_debug.h:177
static void u_native_images_debug_unlock(struct u_native_images_debug *unid)
Simple lock helper.
Definition: u_native_images_debug.h:103
static void u_swapchain_debug_init(struct u_swapchain_debug *uscd)
Must be called before variable is tracked.
Definition: u_native_images_debug.h:211
static void u_native_images_debug_lock(struct u_native_images_debug *unid)
Simple lock helper.
Definition: u_native_images_debug.h:92
static void u_swapchain_debug_clear(struct u_swapchain_debug *uscd)
Clear all variables atomically by holding the lock, still valid to use.
Definition: u_native_images_debug.h:250
static void u_native_images_debug_clear_locked(struct u_native_images_debug *unid)
Clear all variables, must be called with the lock held.
Definition: u_native_images_debug.h:162
static void u_native_images_debug_set(struct u_native_images_debug *unid, xrt_limited_unique_id_t limited_unique_id, struct xrt_image_native *native_images, uint32_t native_image_count, const struct xrt_swapchain_create_info *xscci, uint32_t active_index, bool flip_y)
Updates all variables atomically by holding the lock.
Definition: u_native_images_debug.h:136
static void u_swapchain_debug_destroy(struct u_swapchain_debug *uscd)
Must not be called while variable longer tracked, after u_var_remove_root.
Definition: u_native_images_debug.h:264
static void u_native_images_debug_init(struct u_native_images_debug *unid)
Must be called before variable is tracked.
Definition: u_native_images_debug.h:65
static void u_swapchain_debug_lock(struct u_swapchain_debug *uscd)
Simple lock helper.
Definition: u_native_images_debug.h:277
static void u_swapchain_debug_set(struct u_swapchain_debug *uscd, struct xrt_swapchain_native *xscn, const struct xrt_swapchain_create_info *xscci, uint32_t active_index, bool flip_y)
Updates all variables atomically by holding the lock.
Definition: u_native_images_debug.h:222
static void u_native_images_debug_destroy(struct u_native_images_debug *unid)
Must not be called while variable longer tracked, after u_var_remove_root.
Definition: u_native_images_debug.h:76
static void xrt_swapchain_native_reference(struct xrt_swapchain_native **dst, struct xrt_swapchain_native *src)
Update the reference counts on swapchain(s).
Definition: xrt_compositor.h:2182
Wrapper around OS threading native functions.
A wrapper around a native mutex.
Definition: os_threading.h:55
A struct for debugging one or more native images.
Definition: u_native_images_debug.h:27
uint32_t active_index
The native image that was last filled in by the source, only valid if native_images is non-null,...
Definition: u_native_images_debug.h:53
struct os_mutex mutex
Is initialised/destroyed when added or root is removed.
Definition: u_native_images_debug.h:29
struct xrt_image_native * native_images
List to current set of native images, protected by mutex.
Definition: u_native_images_debug.h:38
const struct xrt_swapchain_create_info * xscci
Information needed to import the native images, information in the struct is immutable,...
Definition: u_native_images_debug.h:47
bool flip_y
Should the image be flipped in y direction.
Definition: u_native_images_debug.h:56
xrt_limited_unique_id_t limited_unique_id
Process unique id for the set of images, protected by mutex, allows caching of imports.
Definition: u_native_images_debug.h:35
uint32_t native_image_count
Count of native_images, protected by mutex.
Definition: u_native_images_debug.h:41
Allows to debug image that is in GPU memory.
Definition: u_native_images_debug.h:197
struct u_native_images_debug base
Base for native image debugging.
Definition: u_native_images_debug.h:199
struct xrt_swapchain_native * xscn
Protected by base::mutex.
Definition: u_native_images_debug.h:202
A single image of a swapchain based on native buffer handles.
Definition: xrt_compositor.h:2128
A limited unique id, it is only unique for the process it is in, so must not be used or synchronized ...
Definition: xrt_defines.h:79
Swapchain creation info.
Definition: xrt_compositor.h:876
Base class for a swapchain that exposes a native buffer handle to be imported into a client API.
Definition: xrt_compositor.h:2163
struct xrt_swapchain base
Base.
Definition: xrt_compositor.h:2165
xrt_limited_unique_id_t limited_unique_id
Unique id for the swapchain, only unique for the current process, is not synchronized between service...
Definition: xrt_compositor.h:2171
uint32_t image_count
Number of images.
Definition: xrt_compositor.h:547
Header declaring XRT graphics interfaces.