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 <opencv2/opencv.hpp>
17 #include "util/u_frame.h"
18 
19 
21 {
22 public:
23  enum Kind
24  {
25  AllAvailable,
26  AlwaysSingle,
27  };
28 
29 public:
30  Kind kind = AllAvailable;
31  struct xrt_frame_sink *sink = {};
32  struct xrt_frame *frame = {};
33 
34  cv::Mat rgb[2] = {};
35 
36 
37 public:
38  HelperDebugSink(Kind kind)
39  {
40  this->kind = kind;
41  }
42 
43  HelperDebugSink() = delete;
44 
46  {
47  xrt_frame_reference(&frame, NULL);
48  }
49 
50  void
51  refresh(struct xrt_frame *xf)
52  {
53  if (sink == NULL) {
54  return;
55  }
56 
57  // But what about second breakfast?
58  bool second_view = false;
59  int rows, cols, width, height;
60 
61  cols = xf->width;
62  rows = xf->height;
63  width = xf->width;
64  height = xf->height;
65  enum xrt_stereo_format stereo_format = xf->stereo_format;
66 
67  switch (xf->stereo_format) {
69  cols /= 2;
70  if (kind == AllAvailable) {
71  second_view = true;
72  } else {
73  stereo_format = XRT_STEREO_FORMAT_NONE;
74  width /= 2;
75  second_view = false;
76  }
77  break;
78  case XRT_STEREO_FORMAT_NONE:
79  // Noop
80  break;
81  default: return;
82  }
83 
84  // Create a new frame and also dereferences the old frame.
85  u_frame_create_one_off(XRT_FORMAT_R8G8B8, width, height,
86  &frame);
87 
88  // Copy needed info.
89  frame->source_sequence = xf->source_sequence;
90  frame->stereo_format = stereo_format;
91 
92  // Doesn't claim ownership of the frame data,
93  // points directly at the frame data.
94  rgb[0] = cv::Mat( //
95  rows, // rows
96  cols, // cols
97  CV_8UC3, // channels
98  frame->data, // data
99  frame->stride); // stride
100 
101  if (second_view) {
102  // Doesn't claim ownership of the frame data,
103  // points directly at the frame data.
104  rgb[1] = cv::Mat( //
105  rows, // rows
106  cols, // cols
107  CV_8UC3, // channels
108  frame->data + 3 * cols, // data
109  frame->stride); // stride
110  }
111  }
112 
113  void
114  submit()
115  {
116  if (frame != NULL) {
117  // Make sure that the cv::Mats doesn't use the data.
118  rgb[0] = cv::Mat();
119  rgb[1] = cv::Mat();
120  sink->push_frame(sink, frame);
121  }
122 
123  // We unreference the frame here, downstream is either
124  // done with it or have referenced it themselves.
125  xrt_frame_reference(&frame, NULL);
126  }
127 };
void(* push_frame)(struct xrt_frame_sink *sink, struct xrt_frame *frame)
Push a frame into the sink.
Definition: xrt_frame.h:56
xrt_frame helpers.
xrt_stereo_format
What type of stereo format a frame has.
Definition: xrt_defines.h:84
A object that is sent frames.
Definition: xrt_frame.h:51
Definition: t_helper_debug_sink.hpp:20
Basic frame data structure - holds a pointer to buffer.
Definition: xrt_frame.h:24
uint64_t source_sequence
sequence id
Definition: xrt_frame.h:41
Side by side.
Definition: xrt_defines.h:87
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