Monado OpenXR Runtime
u_var.h
Go to the documentation of this file.
1// Copyright 2019-2024, Collabora, Ltd.
2// Copyright 2024-2025, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Variable tracking code.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @ingroup aux_util
9 */
10
11#pragma once
12
13#include "xrt/xrt_defines.h" // IWYU pragma: keep
14
15#include "util/u_logging.h"
16
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22
23struct xrt_frame_sink;
24struct u_sink_debug;
26struct m_ff_f64;
27struct m_ff_vec3_f32;
28
29/*!
30 * Used to plot an array for values.
31 *
32 * @ingroup aux_util
33 */
35{
36 void *data;
37 int *index_ptr;
38 int length;
39};
40
41/*!
42 * Used to plot a graph of timing information.
43 *
44 * @ingroup aux_util
45 */
47{
48 //! Values to be plotted.
50
51 //! A reference line drawn on the plot.
53
54 //! If false, reference_timing will be the bottom of the graph.
56
57 //! How many units the graph expands by default.
58 float range;
59
60 //! Rescale graph's value range when value exceeds range.
62
63 //! A string describing the unit used, not freed.
64 const char *unit;
65};
66
67/*!
68 * Callback for a button action
69 *
70 * @ingroup aux_util
71 */
72typedef void (*u_var_button_cb)(void *);
73
74/*!
75 * Simple pushable button.
76 *
77 * @ingroup aux_util
78 */
80{
81 //! Callback function to execute on button press
83
84 //! Pointer that will be passed to the function as its only argument
85 void *ptr;
86
87 /*!
88 * Is the pointer pressing down on the button curruently, this is not
89 * edge triggered like the callback. For a mouse this means that the
90 * pointer is hovering the button and the left is held down.
91 */
92 xrt_atomic_s32_t downed;
93
94 //! Button text, use var `name` if zeroed
95 char label[64];
96
97 //! Button dimensions, zero for auto size
98 float width;
99 float height;
100
101 //! Whether this button is disabled
103};
104
105/*!
106 * Combo box information.
107 *
108 * @ingroup aux_util
109 */
111{
112 //! Number of options.
113 int count;
114
115 //! List of `count` option names separated by \0.
116 const char *options;
117
118 //! Pointer to the option value.
119 int *value;
120};
121
122/*!
123 * Draggable single precision float information.
124 *
125 * @ingroup aux_util
126 */
128{
129 float val;
130 float step;
131 float min;
132 float max;
133};
134
135/*!
136 * Draggable usingned 16-bit integer information.
137 *
138 * @ingroup aux_util
139 */
141{
142 //! @note Using a float instead of storing the value like @ref
143 //! u_var_draggable_f32. It seemed better to decouple the UI from the value
144 //! itself.
145 //! @todo Unify "draggable" widgets interface.
146 uint16_t *val;
147 uint16_t step;
148 uint16_t min;
149 uint16_t max;
150};
151
152/*!
153 * Histogram based on single precision bars.
154 *
155 * @ingroup aux_util
156 */
158{
159 float *values; //!< Bin heights
160 int count; //!< Number of bins
161 float width; //!< Widget width or 0 for auto
162 float height; //!< Widget height or 0 for auto
163};
164
165/*!
166 * A point on the curve, uses doubles like ImPlotPoint.
167 *
168 * @ingroup aux_util
169 */
171{
172 double x;
173 double y;
174};
175
176/*!
177 * Callback for getting points on a curve.
178 *
179 * @ingroup aux_util
180 */
181typedef struct u_var_curve_point (*u_var_curve_getter)(void *data, int i);
182
183/*!
184 * A single curve on a plot.
185 *
186 * @ingroup aux_util
187 */
189{
190 u_var_curve_getter getter; //!< Getter of 2D points for the curve
191 void *data; //!< User data for `getter`
192 int count; //!< Number of points to draw; param i < count
193 const char *label; //!< Curve name
194 const char *xlabel; //!< Label of the X axis
195 const char *ylabel; //!< Label of the Y axis
196};
197
198/*!
199 * A collection of curves to be plotted.
200 *
201 * @ingroup aux_util
202 */
204{
205 struct u_var_curve curves[16];
206 int curve_count;
207
208 // These override individual curve axis labels
209 const char *xlabel; //!< Label of the X axis
210 const char *ylabel; //!< Label of the Y axis
211};
212
213/*!
214 * What kind of variable is this tracking.
215 *
216 * @ingroup aux_util
217 */
219{
220 U_VAR_KIND_BOOL,
221 U_VAR_KIND_RGB_U8,
222 U_VAR_KIND_RGB_F32,
223 U_VAR_KIND_U8,
224 U_VAR_KIND_U16,
225 U_VAR_KIND_U64,
226 U_VAR_KIND_I32,
227 U_VAR_KIND_I64,
228 U_VAR_KIND_F32,
229 U_VAR_KIND_DRAGGABLE_F32,
230 U_VAR_KIND_F64,
231 U_VAR_KIND_F32_ARR,
232 U_VAR_KIND_TIMING,
233 U_VAR_KIND_VEC3_I32,
234 U_VAR_KIND_VEC3_F32,
235 U_VAR_KIND_POSE,
236 U_VAR_KIND_SINK_DEBUG,
237 U_VAR_KIND_NATIVE_IMAGES_DEBUG,
238 U_VAR_KIND_LOG_LEVEL,
239 U_VAR_KIND_RO_TEXT,
240 U_VAR_KIND_RO_RAW_TEXT,
241 U_VAR_KIND_RO_I16,
242 U_VAR_KIND_RO_I32,
243 U_VAR_KIND_RO_U16,
244 U_VAR_KIND_RO_U32,
245 U_VAR_KIND_RO_F32,
246 U_VAR_KIND_RO_I64,
247 U_VAR_KIND_RO_U64,
248 U_VAR_KIND_RO_F64,
249 U_VAR_KIND_RO_I64_NS,
250 U_VAR_KIND_RO_VEC3_I32,
251 U_VAR_KIND_RO_VEC3_F32,
252 U_VAR_KIND_RO_QUAT_F32,
253 U_VAR_KIND_RO_FF_F64,
254 U_VAR_KIND_RO_FF_VEC3_F32,
255 U_VAR_KIND_GUI_HEADER,
256 U_VAR_KIND_GUI_HEADER_BEGIN,
257 U_VAR_KIND_GUI_HEADER_END,
258 U_VAR_KIND_GUI_SAMELINE,
259 U_VAR_KIND_BUTTON,
260 U_VAR_KIND_COMBO,
261 U_VAR_KIND_HISTOGRAM_F32,
262 U_VAR_KIND_DRAGGABLE_U16,
263 U_VAR_KIND_CURVE,
264 U_VAR_KIND_CURVES,
265};
266
267/*!
268 * Maximum string length for a tracked variable.
269 *
270 * @ingroup aux_util
271 */
272#define U_VAR_NAME_STRING_SIZE 256
273
274/*!
275 * Struct that keeps all of the information about the variable, some of the UI
276 * state is kept on it.
277 *
278 * @ingroup aux_util
279 */
281{
282 char name[U_VAR_NAME_STRING_SIZE];
283 void *ptr;
284
285 enum u_var_kind kind;
286
287 struct
288 {
289 bool graphed;
290 } gui;
291};
292
293/*!
294 * Struct containing the information about a root object.
295 *
296 * @ingroup aux_util
297 */
299{
300 //! The displayed name.
301 const char *name;
302
303 //! Raw name without any suffix.
304 const char *raw_name;
305
306 //! The number of the window, or zero (name and raw_name are the same).
307 uint32_t number;
308};
309
310/*!
311 * Callback for entering and leaving root nodes.
312 *
313 * @ingroup aux_util
314 */
315typedef void (*u_var_root_cb)(struct u_var_root_info *info, void *);
316
317/*!
318 * Callback on each variable a root node has.
319 *
320 * @ingroup aux_util
321 */
322typedef void (*u_var_elm_cb)(struct u_var_info *info, void *);
323
324/*!
325 * Add a named root object, the u_var subsystem is completely none-invasive
326 * to the object it's tracking. The root pointer is used as a entry into a
327 * hashmap of hidden objects. When not active all calls are stubs and have no
328 * side-effects.
329 *
330 * This is intended only for debugging and is turned off by default, as this all
331 * very very unsafe. It is only pointers straight into objects, completely
332 * ignores ownership or any safe practices.
333 *
334 * The parameter @p suffix_with_number makes the variable tracking code suffix
335 * the name of the object with with a number. This allows multiple objects of
336 * the same name name.
337 *
338 * ```c
339 * // On create
340 * u_var_add_root((void*)psmv, "PS Move Controller", true);
341 * u_var_add_rgb_u8((void*)psmv, &psmv->led_color, "LED");
342 * u_var_add_log_level(psmv, &psmv->log_level, "Log level");
343 *
344 * // On destroy, only need to destroy the root object.
345 * u_var_remove_root((void*)psmv);
346 * ```
347 *
348 * @param root Object to be tracked.
349 * @param c_name Name of object, null terminated "C" string.
350 * @param suffix_with_number Should name be suffixed with a number.
351 *
352 * @ingroup aux_util
353 */
354void
355u_var_add_root(void *root, const char *c_name, bool suffix_with_number);
356
357/*!
358 * Remove the root node.
359 *
360 * @ingroup aux_util
361 */
362void
364
365/*!
366 * Visit all root nodes and their variables.
367 *
368 * @ingroup aux_util
369 */
370void
371u_var_visit(u_var_root_cb enter_cb, u_var_root_cb exit_cb, u_var_elm_cb elem_cb, void *priv);
372
373/*!
374 * This forces the variable tracking code to on, it is disabled by default.
375 *
376 * @ingroup aux_util
377 */
378void
380
381#define U_VAR_ADD_FUNCS() \
382 ADD_FUNC(bool, bool, BOOL) \
383 ADD_FUNC(rgb_u8, struct xrt_colour_rgb_u8, RGB_U8) \
384 ADD_FUNC(rgb_f32, struct xrt_colour_rgb_f32, RGB_F32) \
385 ADD_FUNC(u8, uint8_t, U8) \
386 ADD_FUNC(u16, uint16_t, U16) \
387 ADD_FUNC(u64, uint64_t, U64) \
388 ADD_FUNC(i32, int32_t, I32) \
389 ADD_FUNC(i64, int64_t, I64) \
390 ADD_FUNC(f32, float, F32) \
391 ADD_FUNC(f64, double, F64) \
392 ADD_FUNC(f32_arr, struct u_var_f32_arr, F32_ARR) \
393 ADD_FUNC(f32_timing, struct u_var_timing, TIMING) \
394 ADD_FUNC(vec3_i32, struct xrt_vec3_i32, VEC3_I32) \
395 ADD_FUNC(vec3_f32, struct xrt_vec3, VEC3_F32) \
396 ADD_FUNC(pose, struct xrt_pose, POSE) \
397 ADD_FUNC(sink_debug, struct u_sink_debug, SINK_DEBUG) \
398 ADD_FUNC(native_images_debug, struct u_native_images_debug, NATIVE_IMAGES_DEBUG) \
399 ADD_FUNC(log_level, enum u_logging_level, LOG_LEVEL) \
400 ADD_FUNC(ro_text, const char, RO_TEXT) \
401 ADD_FUNC(ro_raw_text, const char, RO_RAW_TEXT) \
402 ADD_FUNC(ro_i16, int16_t, RO_I16) \
403 ADD_FUNC(ro_i32, int32_t, RO_I32) \
404 ADD_FUNC(ro_u16, uint16_t, RO_U16) \
405 ADD_FUNC(ro_u32, uint32_t, RO_U32) \
406 ADD_FUNC(ro_f32, float, RO_F32) \
407 ADD_FUNC(ro_i64, int64_t, RO_I64) \
408 ADD_FUNC(ro_u64, uint64_t, RO_U64) \
409 ADD_FUNC(ro_f64, double, RO_F64) \
410 ADD_FUNC(ro_i64_ns, int64_t, RO_I64_NS) \
411 ADD_FUNC(ro_vec3_i32, struct xrt_vec3_i32, RO_VEC3_I32) \
412 ADD_FUNC(ro_vec3_f32, struct xrt_vec3, RO_VEC3_F32) \
413 ADD_FUNC(ro_quat_f32, struct xrt_quat, RO_QUAT_F32) \
414 ADD_FUNC(ro_ff_f64, struct m_ff_f64, RO_FF_F64) \
415 ADD_FUNC(ro_ff_vec3_f32, struct m_ff_vec3_f32, RO_FF_VEC3_F32) \
416 ADD_FUNC(gui_header, bool, GUI_HEADER) \
417 ADD_FUNC(gui_header_begin, bool, GUI_HEADER_BEGIN) \
418 ADD_FUNC(gui_header_end, bool, GUI_HEADER_END) \
419 ADD_FUNC(gui_sameline, void, GUI_SAMELINE) \
420 ADD_FUNC(button, struct u_var_button, BUTTON) \
421 ADD_FUNC(combo, struct u_var_combo, COMBO) \
422 ADD_FUNC(draggable_f32, struct u_var_draggable_f32, DRAGGABLE_F32) \
423 ADD_FUNC(draggable_u16, struct u_var_draggable_u16, DRAGGABLE_U16) \
424 ADD_FUNC(histogram_f32, struct u_var_histogram_f32, HISTOGRAM_F32) \
425 ADD_FUNC(curve, struct u_var_curve, CURVE) \
426 ADD_FUNC(curves, struct u_var_curves, CURVES)
427
428#define ADD_FUNC(SUFFIX, TYPE, ENUM) void u_var_add_##SUFFIX(void *, TYPE *, const char *);
429
430U_VAR_ADD_FUNCS()
431
432#undef ADD_FUNC
433
434
435#ifdef __cplusplus
436}
437#endif
void u_var_add_root(void *root, const char *c_name, bool suffix_with_number)
Add a named root object, the u_var subsystem is completely none-invasive to the object it's tracking.
void(* u_var_button_cb)(void *)
Callback for a button action.
Definition: u_var.h:72
void(* u_var_root_cb)(struct u_var_root_info *info, void *)
Callback for entering and leaving root nodes.
Definition: u_var.h:315
#define U_VAR_NAME_STRING_SIZE
Maximum string length for a tracked variable.
Definition: u_var.h:272
void u_var_visit(u_var_root_cb enter_cb, u_var_root_cb exit_cb, u_var_elm_cb elem_cb, void *priv)
Visit all root nodes and their variables.
void u_var_force_on(void)
This forces the variable tracking code to on, it is disabled by default.
void u_var_remove_root(void *root)
Remove the root node.
struct u_var_curve_point(* u_var_curve_getter)(void *data, int i)
Callback for getting points on a curve.
Definition: u_var.h:181
u_var_kind
What kind of variable is this tracking.
Definition: u_var.h:219
void(* u_var_elm_cb)(struct u_var_info *info, void *)
Callback on each variable a root node has.
Definition: u_var.h:322
Definition: m_filter_fifo.c:183
Definition: m_filter_fifo.c:23
A struct for debugging one or more native images.
Definition: u_native_images_debug.h:27
Allows more safely to debug sink inputs and outputs.
Definition: u_sink.h:211
Simple pushable button.
Definition: u_var.h:80
bool disabled
Whether this button is disabled.
Definition: u_var.h:102
char label[64]
Button text, use var name if zeroed.
Definition: u_var.h:95
xrt_atomic_s32_t downed
Is the pointer pressing down on the button curruently, this is not edge triggered like the callback.
Definition: u_var.h:92
u_var_button_cb cb
Callback function to execute on button press.
Definition: u_var.h:82
void * ptr
Pointer that will be passed to the function as its only argument.
Definition: u_var.h:85
float width
Button dimensions, zero for auto size.
Definition: u_var.h:98
Combo box information.
Definition: u_var.h:111
int count
Number of options.
Definition: u_var.h:113
int * value
Pointer to the option value.
Definition: u_var.h:119
const char * options
List of count option names separated by \0.
Definition: u_var.h:116
A point on the curve, uses doubles like ImPlotPoint.
Definition: u_var.h:171
A single curve on a plot.
Definition: u_var.h:189
const char * ylabel
Label of the Y axis.
Definition: u_var.h:195
int count
Number of points to draw; param i < count.
Definition: u_var.h:192
const char * xlabel
Label of the X axis.
Definition: u_var.h:194
void * data
User data for getter
Definition: u_var.h:191
const char * label
Curve name.
Definition: u_var.h:193
u_var_curve_getter getter
Getter of 2D points for the curve.
Definition: u_var.h:190
A collection of curves to be plotted.
Definition: u_var.h:204
const char * ylabel
Label of the Y axis.
Definition: u_var.h:210
const char * xlabel
Label of the X axis.
Definition: u_var.h:209
Draggable single precision float information.
Definition: u_var.h:128
Draggable usingned 16-bit integer information.
Definition: u_var.h:141
uint16_t * val
Definition: u_var.h:146
Used to plot an array for values.
Definition: u_var.h:35
Histogram based on single precision bars.
Definition: u_var.h:158
float height
Widget height or 0 for auto.
Definition: u_var.h:162
float width
Widget width or 0 for auto.
Definition: u_var.h:161
int count
Number of bins.
Definition: u_var.h:160
float * values
Bin heights.
Definition: u_var.h:159
Struct that keeps all of the information about the variable, some of the UI state is kept on it.
Definition: u_var.h:281
Struct containing the information about a root object.
Definition: u_var.h:299
uint32_t number
The number of the window, or zero (name and raw_name are the same).
Definition: u_var.h:307
const char * name
The displayed name.
Definition: u_var.h:301
const char * raw_name
Raw name without any suffix.
Definition: u_var.h:304
Used to plot a graph of timing information.
Definition: u_var.h:47
float range
How many units the graph expands by default.
Definition: u_var.h:58
bool center_reference_timing
If false, reference_timing will be the bottom of the graph.
Definition: u_var.h:55
bool dynamic_rescale
Rescale graph's value range when value exceeds range.
Definition: u_var.h:61
float reference_timing
A reference line drawn on the plot.
Definition: u_var.h:52
struct u_var_f32_arr values
Values to be plotted.
Definition: u_var.h:49
const char * unit
A string describing the unit used, not freed.
Definition: u_var.h:64
A object that is sent frames.
Definition: xrt_frame.h:58
Basic logging functionality.
Common defines and enums for XRT.