Monado OpenXR Runtime
comp_compositor.h
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Main compositor written using Vulkan header.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
8  * @ingroup comp_main
9  */
10 
11 #pragma once
12 
13 #include "xrt/xrt_gfx_vk.h"
14 
15 #include "util/u_threading.h"
16 
17 #include "main/comp_settings.h"
18 #include "main/comp_window.h"
19 #include "main/comp_renderer.h"
20 
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define NUM_FRAME_TIMES 50
27 
28 /*
29  *
30  * Structs
31  *
32  */
33 
34 /*!
35  * A single swapchain image, holds the needed state for tracking image usage.
36  *
37  * @ingroup comp_main
38  */
40 {
41  //! Vulkan image to create view from.
43  //! Exported memory backing the image.
45  //! Sampler used by the renderer and distortion code.
46  VkSampler sampler;
47  //! Views used by the renderer and distortion code, for each array
48  //! layer.
49  VkImageView *views;
50 };
51 
52 /*!
53  * A swapchain that is almost a one to one mapping to a OpenXR swapchain.
54  *
55  * Not used by the window backend that uses the vk_swapchain to render to.
56  *
57  * @ingroup comp_main
58  */
60 {
62 
63  struct comp_compositor *c;
64 
66 };
67 
68 /*!
69  * Main compositor struct tying everything in the compositor together.
70  *
71  * @ingroup comp_main
72  */
74 {
76 
77  //! A link back to the compositor we are presenting to the client.
79 
80  //! Renderer helper.
81  struct comp_renderer *r;
82 
83  //! The window or display we are using.
85 
86  //! The device we are displaying to.
87  struct xrt_device *xdev;
88 
89  //! The settings.
90  struct comp_settings settings;
91 
92  //! Vulkan bundle of things.
93  struct vk_bundle vk;
94 
95  //! Timestamp of last-rendered (immersive) frame.
97 
98  /*!
99  * @brief Data exclusive to the begin_frame/end_frame for computing an
100  * estimate of the app's needs.
101  */
102  struct
103  {
104  int64_t last_begin;
105  int64_t last_end;
106  } app_profiling;
107 
108  //! The time our compositor needs to do rendering
110 
111  struct
112  {
113  //! Current Index for times_ns.
114  int index;
115 
116  //! Timestamps of last-rendered (immersive) frames.
117  int64_t times_ns[NUM_FRAME_TIMES];
118 
119  //! Frametimes between last-rendered (immersive) frames.
120  float timings_ms[NUM_FRAME_TIMES];
121 
122  //! Average FPS of last NUM_FRAME_TIMES rendered frames.
123  float fps;
124 
126  } compositor_frame_times;
127 
128  /*!
129  * @brief Estimated rendering time per frame of the application.
130  *
131  * Set by the begin_frame/end_frame code.
132  *
133  * @todo make this atomic.
134  */
136  //! The last time we provided in the results of wait_frame
138 
139  /*!
140  * The current state we are tracking.
141  *
142  * Settings is supposed to be read only.
143  */
144  struct
145  {
146  uint32_t width;
147  uint32_t height;
148  } current;
149 
150  struct
151  {
152  //! Thread object for safely destroying swapchain.
153  struct u_threading_stack destroy_swapchains;
154  } threading;
155 };
156 
157 
158 /*
159  *
160  * Functions and helpers.
161  *
162  */
163 
164 /*!
165  * Convenience function to convert a xrt_swapchain to a comp_swapchain.
166  *
167  * @ingroup comp_main
168  */
169 static inline struct comp_swapchain *
170 comp_swapchain(struct xrt_swapchain *xsc)
171 {
172  return (struct comp_swapchain *)xsc;
173 }
174 
175 /*!
176  * Convenience function to convert a xrt_compositor to a comp_compositor.
177  *
178  * @ingroup comp_main
179  */
180 static inline struct comp_compositor *
182 {
183  return (struct comp_compositor *)xc;
184 }
185 
186 /*!
187  * Do garbage collection, destroying any resources that has been scheduled for
188  * destruction from other threads.
189  *
190  * @ingroup comp_main
191  */
192 void
194 
195 /*!
196  * A compositor function that is implemented in the swapchain code.
197  *
198  * @ingroup comp_main
199  */
200 struct xrt_swapchain *
202  enum xrt_swapchain_create_flags create,
203  enum xrt_swapchain_usage_bits bits,
204  int64_t format,
205  uint32_t sample_count,
206  uint32_t width,
207  uint32_t height,
208  uint32_t face_count,
209  uint32_t array_size,
210  uint32_t mip_count);
211 
212 /*!
213  * Swapchain destruct is delayed until it is safe to destroy them, this function
214  * does the actual destruction and is called from @ref
215  * comp_compositor_garbage_collect.
216  *
217  * @ingroup comp_main
218  */
219 void
221 
222 /*!
223  * Free and destroy any initialized fields on the given image, safe to pass in
224  * images that has one or all fields set to NULL.
225  *
226  * @ingroup comp_main
227  */
228 void
230  uint32_t array_size,
231  struct comp_swapchain_image *image);
232 
233 /*!
234  * Printer helper.
235  *
236  * @ingroup comp_main
237  */
238 void
240  const char *func,
241  const char *fmt,
242  ...) XRT_PRINTF_FORMAT(3, 4);
243 
244 /*!
245  * Spew level logging.
246  *
247  * @ingroup comp_main
248  */
249 #define COMP_SPEW(c, ...) \
250  do { \
251  if (c->settings.print_spew) { \
252  comp_compositor_print(c, __func__, __VA_ARGS__); \
253  } \
254  } while (false)
255 
256 /*!
257  * Debug level logging.
258  *
259  * @ingroup comp_main
260  */
261 #define COMP_DEBUG(c, ...) \
262  do { \
263  if (c->settings.print_debug) { \
264  comp_compositor_print(c, __func__, __VA_ARGS__); \
265  } \
266  } while (false)
267 
268 /*!
269  * Mode printing.
270  *
271  * @ingroup comp_main
272  */
273 #define COMP_PRINT_MODE(c, ...) \
274  do { \
275  if (c->settings.print_modes) { \
276  comp_compositor_print(c, __func__, __VA_ARGS__); \
277  } \
278  } while (false)
279 
280 /*!
281  * Error level logging.
282  *
283  * @ingroup comp_main
284  */
285 #define COMP_ERROR(c, ...) \
286  do { \
287  comp_compositor_print(c, __func__, __VA_ARGS__); \
288  } while (false)
289 
290 
291 #ifdef __cplusplus
292 }
293 #endif
struct u_var_timing * debug_var
Definition: comp_compositor.h:125
VkImageView * views
Views used by the renderer and distortion code, for each array layer.
Definition: comp_compositor.h:49
Definition: u_var.h:33
Main compositor.
Definition: xrt_compositor.h:527
int64_t expected_app_duration_ns
Estimated rendering time per frame of the application.
Definition: comp_compositor.h:135
Settings for the compositor.
Definition: comp_settings.h:56
struct comp_window * window
The window or display we are using.
Definition: comp_compositor.h:84
struct xrt_swapchain * comp_swapchain_create(struct xrt_compositor *xc, enum xrt_swapchain_create_flags create, enum xrt_swapchain_usage_bits bits, int64_t format, uint32_t sample_count, uint32_t width, uint32_t height, uint32_t face_count, uint32_t array_size, uint32_t mip_count)
A compositor function that is implemented in the swapchain code.
Definition: comp_swapchain.c:210
void comp_compositor_garbage_collect(struct comp_compositor *c)
Do garbage collection, destroying any resources that has been scheduled for destruction from other th...
Definition: comp_compositor.c:891
uint64_t VkImage
Definition: xrt_compositor.h:452
int64_t last_frame_time_ns
Timestamp of last-rendered (immersive) frame.
Definition: comp_compositor.h:96
int64_t last_end
Definition: comp_compositor.h:105
struct xrt_swapchain base
Definition: xrt_compositor.h:517
Settings struct for compositor header.
Main compositor struct tying everything in the compositor together.
Definition: comp_compositor.h:73
A swapchain that exposes fd to be imported into a client API.
Definition: xrt_compositor.h:515
void comp_compositor_print(struct comp_compositor *c, const char *func, const char *fmt,...) XRT_PRINTF_FORMAT(3
Printer helper.
VkDeviceMemory memory
Exported memory backing the image.
Definition: comp_compositor.h:44
int64_t last_next_display_time
The last time we provided in the results of wait_frame.
Definition: comp_compositor.h:137
Holds associated vulkan objects and state to render with a distortion.
Definition: comp_renderer.c:33
struct comp_compositor * c
Definition: comp_compositor.h:63
VkSampler sampler
Sampler used by the renderer and distortion code.
Definition: comp_compositor.h:46
xrt_swapchain_usage_bits
Usage of the swapchain images.
Definition: xrt_compositor.h:48
A single swapchain image, holds the needed state for tracking image usage.
Definition: comp_compositor.h:39
float fps
Average FPS of last NUM_FRAME_TIMES rendered frames.
Definition: comp_compositor.h:123
#define NUM_FRAME_TIMES
Definition: comp_compositor.h:26
Common swapchain base.
Definition: xrt_compositor.h:75
uint32_t height
Definition: comp_compositor.h:147
void comp_swapchain_image_cleanup(struct vk_bundle *vk, uint32_t array_size, struct comp_swapchain_image *image)
Free and destroy any initialized fields on the given image, safe to pass in images that has one or al...
Definition: comp_swapchain.c:303
uint64_t VkDeviceMemory
Definition: xrt_compositor.h:453
struct comp_renderer * r
Renderer helper.
Definition: comp_compositor.h:81
Compositor rendering code header.
uint32_t array_size
Number of array layers per image.
Definition: xrt_compositor.h:85
Common compositor base.
Definition: xrt_compositor.h:169
A output device or a window, often directly connected to the device.
Definition: comp_window.h:32
uint32_t width
Definition: comp_compositor.h:146
#define XRT_MAX_SWAPCHAIN_IMAGES
Max swapchain images, artificial limit.
Definition: xrt_compositor.h:24
int64_t frame_overhead_ns
The time our compositor needs to do rendering.
Definition: comp_compositor.h:109
Compositor window header.
struct xrt_compositor * client
A link back to the compositor we are presenting to the client.
Definition: comp_compositor.h:78
Header defining a XRT graphics provider.
void comp_swapchain_really_destroy(struct comp_swapchain *sc)
Swapchain destruct is delayed until it is safe to destroy them, this function does the actual destruc...
Definition: comp_swapchain.c:340
int64_t last_begin
Definition: comp_compositor.h:104
Slightly higher level thread safe helpers.
int index
Current Index for times_ns.
Definition: comp_compositor.h:114
struct xrt_compositor base
Definition: xrt_compositor.h:529
struct xrt_device * xdev
The device we are displaying to.
Definition: comp_compositor.h:87
#define XRT_PRINTF_FORMAT(fmt, list)
Definition: xrt_compiler.h:40
A swapchain that is almost a one to one mapping to a OpenXR swapchain.
Definition: comp_compositor.h:59
VkImage image
Vulkan image to create view from.
Definition: comp_compositor.h:42
A single HMD or input device.
Definition: xrt_device.h:202
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code...
Definition: vk_helpers.h:34
Definition: u_threading.h:16
xrt_swapchain_create_flags
Special flags for creating swapchain images.
Definition: xrt_compositor.h:38