Ocean
Passthrough.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_META_QUEST_OPENXR_PASSTHROUGH_H
9 #define META_OCEAN_PLATFORM_META_QUEST_OPENXR_PASSTHROUGH_H
10 
12 
14 
15 namespace Ocean
16 {
17 
18 namespace Platform
19 {
20 
21 namespace Meta
22 {
23 
24 namespace Quest
25 {
26 
27 namespace OpenXR
28 {
29 
30 /**
31  * This class implements a wrapper for passthrough.
32  * This object uses the Android feature 'com.oculus.feature.PASSTHROUGH'.
33  * @ingroup platformmetaquestopenxr
34  */
35 class OCEAN_PLATFORM_META_QUEST_OPENXR_EXPORT Passthrough final
36 {
37  public:
38 
39  /**
40  * Definition of an unordered set holding strings.
41  */
42  typedef std::unordered_set<std::string> StringSet;
43 
44  /**
45  * This class implements a scoped state object allowing to reset all passthrough states of a visualizer.
46  * The state can be stored locally or states can be pushed onto a stack.
47  * @see pushState(), popState().
48  */
50  {
51  public:
52 
53  /**
54  * Default constructor.
55  */
56  ScopedState() = default;
57 
58  /**
59  * Creates a new state object.
60  * @param passthrough The visualizer to which this new object belongs
61  */
62  ScopedState(Passthrough& passthrough);
63 
64  /**
65  * Move constructor.
66  * @param scopedState The state object to be moved
67  */
68  ScopedState(ScopedState&& scopedState);
69 
70  /**
71  * Destructs this object and releases the state.
72  */
74 
75  /**
76  * Explicitly releases this state.
77  * The properties of the owning visualizer will be reset to the situation when the state was created.
78  */
79  void release();
80 
81  /**
82  * Move operator.
83  * @param scopedState The state object to be moved
84  * @return Reference to this object
85  */
87 
88  protected:
89 
90  /**
91  * Disabled copy constructor.
92  */
93  ScopedState(const ScopedState&) = delete;
94 
95  /**
96  * Disabled assign operator.
97  * @return The reference to this object
98  */
99  ScopedState& operator=(const ScopedState&) = delete;
100 
101  protected:
102 
103  /// The visualizer to which this state object belongs.
104  Passthrough* passthrough_ = nullptr;
105 
106  /// True, if the passthrough service was actively running when this object was created.
107  bool wasRunning_ = false;
108  };
109 
110  /**
111  * Definition of a vector holding state objects.
112  */
113  typedef std::vector<ScopedState> ScopedStates;
114 
115  public:
116 
117  /**
118  * Default constructor.
119  */
120  Passthrough() = default;
121 
122  /**
123  * Destructs this passthrough object and releases all associated resources.
124  */
126 
127  /**
128  * Move constructor.
129  * @param passthrough The object to be moved
130  */
131  inline Passthrough(Passthrough&& passthrough);
132 
133  /**
134  * Initializes this passthrough object.
135  * @param session The OpenXR session to be used, must be valid
136  * @param xrPassthroughLayerPurposeFB The purpose to be used
137  * @return True, if succeeded
138  */
139  inline bool initialize(const Platform::OpenXR::Session& session, const XrPassthroughLayerPurposeFB& xrPassthroughLayerPurposeFB = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB);
140 
141  /**
142  * Initializes this passthrough object.
143  * @param xrInstance The OpenXR instance to be used, must be valid
144  * @param xrSession The OpenXR session to be used, must be valid
145  * @param xrPassthroughLayerPurposeFB The purpose to be used
146  * @return True, if succeeded
147  */
148  bool initialize(const XrInstance& xrInstance, const XrSession& xrSession, const XrPassthroughLayerPurposeFB& xrPassthroughLayerPurposeFB = XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB);
149 
150  /**
151  * Starts passthrough.
152  * @return True, if succeeded
153  */
154  bool start();
155 
156  /**
157  * Pauses passthrough.
158  * @return True, if succeeded
159  */
160  bool pause();
161 
162  /**
163  * Returns whether passthrough is started.
164  * @return True, if so
165  */
166  bool isStarted() const;
167 
168  /**
169  * Explicitly releases the passthrough object and all associated resources.
170  */
171  void release();
172 
173  /**
174  * Returns the OpenXR passthrough layer.
175  * @return The passthrough's layer
176  */
177  inline XrPassthroughLayerFB xrPassthroughLayerFB() const;
178 
179  /**
180  * Returns the lock of the passthrough.
181  * @return The passthrough's lock.
182  */
183  inline Lock& lock() const;
184 
185  /**
186  * Returns whether this object holds initialized and valid OpenXR hand trackers.
187  * @return True, if so
188  */
189  inline bool isValid() const;
190 
191  /**
192  * Pushes a new configuration state to the stack.
193  * Each push needs to be balanced with a pop.
194  * @see popState().
195  */
196  inline void pushState();
197 
198  /**
199  * Pops the most recent state from the stack and resets the visualizer's configuration accordingly.
200  */
201  inline void popState();
202 
203  /**
204  * Move operator.
205  * @param passthrough The object to be moved
206  * @return Reference to this object
207  */
209 
210  /**
211  * Returns the names of the necessary OpenXR extensions necessary for hand tracking.
212  * @return The necessary OpenXR extensions
213  */
215 
216  protected:
217 
218  /**
219  * Disabled copy constructor.
220  */
221  Passthrough(const Passthrough&) = delete;
222 
223  /**
224  * Disabled assign operator.
225  * @return Reference to this object
226  */
227  Passthrough& operator=(const Passthrough&) = delete;
228 
229  protected:
230 
231  /// The OpenXR instance.
232  XrInstance xrInstance_ = XR_NULL_HANDLE;
233 
234  /// The OpenXR session.
235  XrSession xrSession_ = XR_NULL_HANDLE;
236 
237  /// The OpenXR passthrough handle.
238  XrPassthroughFB xrPassthroughFB_ = XR_NULL_HANDLE;
239 
240  /// The OpenXR passthrough layer handle.
241  XrPassthroughLayerFB xrPassthroughLayerFB_ = XR_NULL_HANDLE;
242 
243  /// The OpenXR passthrough create function.
244  PFN_xrCreatePassthroughFB xrCreatePassthroughFB_ = nullptr;
245 
246  /// The OpenXR passthrough destroy function.
247  PFN_xrDestroyPassthroughFB xrDestroyPassthroughFB_ = nullptr;
248 
249  /// The OpenXR passthrough start function.
250  PFN_xrPassthroughStartFB xrPassthroughStartFB_ = nullptr;
251 
252  /// The OpenXR passthrough pause function.
253  PFN_xrPassthroughPauseFB xrPassthroughPauseFB_ = nullptr;
254 
255  /// The OpenXR passthrough create layer function.
256  PFN_xrCreatePassthroughLayerFB xrCreatePassthroughLayerFB_ = nullptr;
257 
258  /// The OpeXR passthrough destroy layer function.
259  PFN_xrDestroyPassthroughLayerFB xrDestroyPassthroughLayerFB_ = nullptr;
260 
261  /// The OpenXR passthrough resume layer function.
262  PFN_xrPassthroughLayerResumeFB xrPassthroughLayerResumeFB_ = nullptr;
263 
264  /// The OpenXR passthrough set style function.
265  PFN_xrPassthroughLayerSetStyleFB xrPassthroughLayerSetStyleFB_ = nullptr;
266 
267  /// True, if passthrough is currently started.
268  bool isStarted_ = false;
269 
270  /// A stack of visualization states.
272 
273  /// The lock object.
274  mutable Lock lock_;
275 };
276 
278 {
279  *this = std::move(passthrough);
280 }
281 
282 bool Passthrough::initialize(const Platform::OpenXR::Session& session, const XrPassthroughLayerPurposeFB& xrPassthroughLayerPurposeFB)
283 {
284  ocean_assert(session.isValid());
285 
286  return initialize(session.xrInstance(), session, xrPassthroughLayerPurposeFB);
287 }
288 
289 inline XrPassthroughLayerFB Passthrough::xrPassthroughLayerFB() const
290 {
291  return xrPassthroughLayerFB_;
292 }
293 
294 inline Lock& Passthrough::lock() const
295 {
296  return lock_;
297 }
298 
299 inline bool Passthrough::isValid() const
300 {
301  const ScopedLock scopedLock(lock_);
302 
303  return xrInstance_ != XR_NULL_HANDLE;
304 }
305 
307 {
308  const ScopedLock scopedLock(lock_);
309 
310  stateStack_.emplace_back(*this);
311 }
312 
314 {
315  const ScopedLock scopedLock(lock_);
316 
317  ocean_assert(!stateStack_.empty());
318 
319  stateStack_.pop_back();
320 }
321 
322 }
323 
324 }
325 
326 }
327 
328 }
329 
330 }
331 
332 #endif // META_OCEAN_PLATFORM_META_QUEST_OPENXR_PASSTHROUGH_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a scoped state object allowing to reset all passthrough states of a visualizer.
Definition: Passthrough.h:50
ScopedState(Passthrough &passthrough)
Creates a new state object.
ScopedState(ScopedState &&scopedState)
Move constructor.
ScopedState & operator=(ScopedState &&scopedState)
Move operator.
ScopedState & operator=(const ScopedState &)=delete
Disabled assign operator.
ScopedState(const ScopedState &)=delete
Disabled copy constructor.
~ScopedState()
Destructs this object and releases the state.
This class implements a wrapper for passthrough.
Definition: Passthrough.h:36
Lock & lock() const
Returns the lock of the passthrough.
Definition: Passthrough.h:294
Passthrough & operator=(const Passthrough &)=delete
Disabled assign operator.
ScopedStates stateStack_
A stack of visualization states.
Definition: Passthrough.h:271
Lock lock_
The lock object.
Definition: Passthrough.h:274
bool isStarted() const
Returns whether passthrough is started.
XrInstance xrInstance_
The OpenXR instance.
Definition: Passthrough.h:232
bool isValid() const
Returns whether this object holds initialized and valid OpenXR hand trackers.
Definition: Passthrough.h:299
static const StringSet & necessaryOpenXRExtensionNames()
Returns the names of the necessary OpenXR extensions necessary for hand tracking.
~Passthrough()
Destructs this passthrough object and releases all associated resources.
void popState()
Pops the most recent state from the stack and resets the visualizer's configuration accordingly.
Definition: Passthrough.h:313
std::vector< ScopedState > ScopedStates
Definition of a vector holding state objects.
Definition: Passthrough.h:113
XrPassthroughLayerFB xrPassthroughLayerFB_
The OpenXR passthrough layer handle.
Definition: Passthrough.h:241
bool initialize(const XrInstance &xrInstance, const XrSession &xrSession, const XrPassthroughLayerPurposeFB &xrPassthroughLayerPurposeFB=XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB)
Initializes this passthrough object.
Passthrough(const Passthrough &)=delete
Disabled copy constructor.
Passthrough & operator=(Passthrough &&passthrough)
Move operator.
void release()
Explicitly releases the passthrough object and all associated resources.
bool initialize(const Platform::OpenXR::Session &session, const XrPassthroughLayerPurposeFB &xrPassthroughLayerPurposeFB=XR_PASSTHROUGH_LAYER_PURPOSE_RECONSTRUCTION_FB)
Initializes this passthrough object.
Definition: Passthrough.h:282
XrPassthroughLayerFB xrPassthroughLayerFB() const
Returns the OpenXR passthrough layer.
Definition: Passthrough.h:289
std::unordered_set< std::string > StringSet
Definition of an unordered set holding strings.
Definition: Passthrough.h:42
void pushState()
Pushes a new configuration state to the stack.
Definition: Passthrough.h:306
This class wraps an OpenXR session.
Definition: Session.h:32
XrInstance xrInstance() const
Returns the OpenXR instance associated with this session.
Definition: Session.h:359
bool isValid() const
Returns whether this object holds a valid OpenXR instance.
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15