Monado OpenXR Runtime
t_helper_debug_sink.hpp
Go to the documentation of this file.
1 // Copyright 2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Small helper struct that for debugging views.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup aux_tracking
8  */
9 
10 #pragma once
11 
12 #ifndef __cplusplus
13 #error "This header is C++-only."
14 #endif
15 
16 #include "util/u_sink.h"
17 #include "util/u_frame.h"
18 #include <opencv2/opencv.hpp>
19 
20 namespace xrt::auxiliary::tracking {
21 
23 {
24 public:
25  enum Kind
26  {
27  AllAvailable,
28  AlwaysSingle,
29  };
30 
31 
32  Kind kind = AllAvailable;
33  struct u_sink_debug usd = {};
34  struct xrt_frame *frame = {};
35 
36  cv::Mat rgb[2] = {};
37 
38 
39 
40  HelperDebugSink(Kind kind)
41  {
42  this->kind = kind;
43  u_sink_debug_init(&usd);
44  }
45 
46  HelperDebugSink() = delete;
47 
49  {
50  u_sink_debug_destroy(&usd);
51  xrt_frame_reference(&frame, NULL);
52  }
53 
54  void
55  refresh(struct xrt_frame *xf)
56  {
57  if (!u_sink_debug_is_active(&usd)) {
58  return;
59  }
60 
61  // But what about second breakfast?
62  bool second_view = false;
63  int rows;
64  int cols;
65  int width;
66  int height;
67 
68  cols = xf->width;
69  rows = xf->height;
70  width = xf->width;
71  height = xf->height;
72  enum xrt_stereo_format stereo_format = xf->stereo_format;
73 
74  switch (xf->stereo_format) {
76  cols /= 2;
77  if (kind == AllAvailable) {
78  second_view = true;
79  } else {
80  stereo_format = XRT_STEREO_FORMAT_NONE;
81  width /= 2;
82  second_view = false;
83  }
84  break;
85  case XRT_STEREO_FORMAT_NONE:
86  // Noop
87  break;
88  default: return;
89  }
90 
91  // Create a new frame and also dereferences the old frame.
92  u_frame_create_one_off(XRT_FORMAT_R8G8B8, width, height, &frame);
93 
94  // Copy needed info.
95  frame->source_sequence = xf->source_sequence;
96  frame->stereo_format = stereo_format;
97 
98  // Doesn't claim ownership of the frame data,
99  // points directly at the frame data.
100  rgb[0] = cv::Mat( //
101  rows, // rows
102  cols, // cols
103  CV_8UC3, // channels
104  frame->data, // data
105  frame->stride); // stride
106 
107  if (second_view) {
108  // Doesn't claim ownership of the frame data,
109  // points directly at the frame data.
110  rgb[1] = cv::Mat( //
111  rows, // rows
112  cols, // cols
113  CV_8UC3, // channels
114  frame->data + 3 * cols, // data
115  frame->stride); // stride
116  }
117  }
118 
119  void
120  submit()
121  {
122  // Make sure that the cv::Mats doesn't use the data.
123  rgb[0] = cv::Mat();
124  rgb[1] = cv::Mat();
125 
126  // Don't try to push null frames.
127  if (frame == nullptr) {
128  return;
129  }
130 
131  // Does checking if the sink is active.
132  u_sink_debug_push_frame(&usd, frame);
133 
134  // We unreference the frame here, downstream is either
135  // done with it or have referenced it themselves.
136  xrt_frame_reference(&frame, NULL);
137  }
138 };
139 
140 } // namespace xrt::auxiliary::tracking
xrt_stereo_format
What type of stereo format a frame has.
Definition: xrt_defines.h:203
static void xrt_frame_reference(struct xrt_frame **dst, struct xrt_frame *src)
Update the reference counts on frame(s).
Definition: xrt_frame.h:131
@ XRT_STEREO_FORMAT_SBS
Side by side.
Definition: xrt_defines.h:205
Definition: u_pacing_compositor.c:55
Allows more safely to debug sink inputs and outputs.
Definition: u_sink.h:185
Definition: t_helper_debug_sink.hpp:23
Basic frame data structure - holds a pointer to buffer.
Definition: xrt_frame.h:25
uint64_t source_sequence
sequence id
Definition: xrt_frame.h:41
void u_frame_create_one_off(enum xrt_format f, uint32_t width, uint32_t height, struct xrt_frame **out_frame)
Creates a single non-pooled frame, when the reference reaches zero it is freed.
Definition: u_frame.c:26
xrt_frame helpers.
xrt_frame_sink converters and other helpers.