Ocean
Instance.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_OPENXR_INSTANCE_H
9 #define META_OCEAN_PLATFORM_OPENXR_INSTANCE_H
10 
12 
13 #include "ocean/base/Lock.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Platform
19 {
20 
21 namespace OpenXR
22 {
23 
24 /**
25  * This class wraps an OpenXR instance.
26  * An OpenXR instance is an object that allows an OpenXR application to communicate with an OpenXR runtime.
27  * @ingroup platformopenxr
28  */
29 class OCEAN_PLATFORM_OPENXR_EXPORT Instance final
30 {
31  public:
32 
33  /**
34  * Definition of a vector holding extension properties.
35  */
36  typedef std::vector<XrExtensionProperties> XrExtensionPropertyGroups;
37 
38  /**
39  * Definition of an unordered set holding strings.
40  */
41  typedef std::unordered_set<std::string> StringSet;
42 
43  /**
44  * Definition of a vector holding XrViewConfigurationView objects.
45  */
46  typedef std::vector<XrViewConfigurationView> XrViewConfigurationViews;
47 
48  public:
49 
50  /**
51  * Default constructor creating an invalid instance.
52  */
53  Instance() = default;
54 
55  /**
56  * Move constructor.
57  * @param instance The instance to be moved
58  */
59  inline Instance(Instance&& instance);
60 
61  /**
62  * Destructs the instance and releases all associated resources.
63  */
65 
66  /**
67  * Initializes the instance.
68  * @param necessaryExtensions The names of the necessary extensions for the instance
69  * @param applicationName The name of the application
70  * @return True, if succeeded
71  */
72  bool initialize(const StringSet& necessaryExtensions, const std::string& applicationName = "OceanOpenXR");
73 
74  /**
75  * Explicitly releases the instance and all associated resources.
76  * @see initialize().
77  */
78  void release();
79 
80  /**
81  * Determines the view configurations for a specified view type available for this instance.
82  * @param xrViewConfigurationType The view type for which the configuration will be determined, must be valid
83  * @param xrViewConfigurationViews The resulting view configurations, e.g., one for each eye/display
84  * @return True, if succeeded
85  */
86  inline bool determineViewConfigurations(const XrViewConfigurationType xrViewConfigurationType, XrViewConfigurationViews& xrViewConfigurationViews) const;
87 
88  /**
89  * Translates an OpenXR result associated with this instance into a readable string.
90  * @param xrResult The OpenXR result to translate
91  * @return The translated result
92  */
93  std::string translateResult(const XrResult xrResult) const;
94 
95  /**
96  * Returns whether this object holds a valid OpenXR instance.
97  * @return True, if so
98  * @see initialize().
99  */
100  bool isValid() const;
101 
102  /**
103  * Returns the identifier for the runtime.
104  * @return The runtime's identifier
105  */
106  XrSystemId xrSystemId() const;
107 
108  /**
109  * Returns the enabled extensions of this instance.
110  * @return The instance's enabled extensions
111  */
112  inline const StringSet& enabledExtensions() const;
113 
114  /**
115  * Move operator.
116  * @param instance The instance to be moved
117  * @return Reference to this object
118  */
120 
121  /**
122  * Returns the wrapped OpenXR instance.
123  * @return The actual instance, nullptr if not initialized
124  * @see isValid().
125  */
126  operator XrInstance() const;
127 
128  /**
129  * Returns whether this object holds a valid OpenXR instance.
130  * @return True, if so
131  * @see isValid().
132  */
133  explicit inline operator bool() const;
134 
135  /**
136  * Determines the available OpenXR API layers.
137  * @param apiLayers Optional resulting names of the API layers, nullptr if not of interest
138  * @return True, if succeeded
139  */
140  static bool determineApiLayers(std::vector<std::string>* apiLayers = nullptr);
141 
142  /**
143  * Determines the properties of the available OpenXR extension.
144  * @param extensionNames Optional resulting names of the extensions, nullptr if not of interest
145  * @return True, if succeeded
146  */
147  static XrExtensionPropertyGroups determineExtensionProperties(std::vector<std::string>* extensionNames = nullptr);
148 
149  /**
150  * Determines the names of existing OpenXR extensions which match the set of given necessary extensions.
151  * @param xrExtensionPropertyGroups The groups of available OpenXR extension properties
152  * @param necessaryExtensions The OpenXR extension which are necessary for the application
153  * @return The resulting names of all existing extensions which are also necessary
154  */
155  static std::vector<const char*> determineExistingExtensionNames(const XrExtensionPropertyGroups& xrExtensionPropertyGroups, const StringSet& necessaryExtensions);
156 
157  /**
158  * Determines the view configurations for a specified view type.
159  * @param xrInstance The OpenXR instance for which the view configurations will be determined, must be valid
160  * @param xrSystemId The OpenXR runtime's identifier associated with the instance, must be valid
161  * @param xrViewConfigurationType The view type for which the configuration will be determined, must be valid
162  * @param xrViewConfigurationViews The resulting view configurations, e.g., one for each eye/display
163  * @return True, if succeeded
164  */
165  static bool determineViewConfigurations(const XrInstance& xrInstance, const XrSystemId& xrSystemId, const XrViewConfigurationType xrViewConfigurationType, XrViewConfigurationViews& xrViewConfigurationViews);
166 
167  protected:
168 
169  /**
170  * Disabled copy constructor.
171  */
172  Instance(const Instance&) = delete;
173 
174  /**
175  * Disabled assign operator.
176  * @return Reference to this object
177  */
178  Instance& operator=(const Instance&) = delete;
179 
180  protected:
181 
182  /// The actual OpenXR instance.
183  XrInstance xrInstance_ = XR_NULL_HANDLE;
184 
185  /// The identifier for the runtime.
186  XrSystemId xrSystemId_ = XR_NULL_SYSTEM_ID;
187 
188  /// The instance's enabled extensions.
190 
191  /// The instance's lock.
192  mutable Lock lock_;
193 };
194 
195 inline Instance::Instance(Instance&& instance)
196 {
197  *this = std::move(instance);
198 }
199 
200 inline bool Instance::determineViewConfigurations(const XrViewConfigurationType xrViewConfigurationType, XrViewConfigurationViews& xrViewConfigurationViews) const
201 {
202  ocean_assert(isValid());
203 
204  return determineViewConfigurations(xrInstance_, xrSystemId_, xrViewConfigurationType, xrViewConfigurationViews);
205 }
206 
208 {
209  return enabledExtensions_;
210 }
211 
212 inline Instance::operator bool() const
213 {
214  return isValid();
215 }
216 
217 }
218 
219 }
220 
221 }
222 
223 #endif // META_OCEAN_PLATFORM_OPENXR_INSTANCE_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class wraps an OpenXR instance.
Definition: Instance.h:30
void release()
Explicitly releases the instance and all associated resources.
bool isValid() const
Returns whether this object holds a valid OpenXR instance.
bool initialize(const StringSet &necessaryExtensions, const std::string &applicationName="OceanOpenXR")
Initializes the instance.
static std::vector< const char * > determineExistingExtensionNames(const XrExtensionPropertyGroups &xrExtensionPropertyGroups, const StringSet &necessaryExtensions)
Determines the names of existing OpenXR extensions which match the set of given necessary extensions.
Instance()=default
Default constructor creating an invalid instance.
static XrExtensionPropertyGroups determineExtensionProperties(std::vector< std::string > *extensionNames=nullptr)
Determines the properties of the available OpenXR extension.
std::unordered_set< std::string > StringSet
Definition of an unordered set holding strings.
Definition: Instance.h:41
Instance(const Instance &)=delete
Disabled copy constructor.
static bool determineApiLayers(std::vector< std::string > *apiLayers=nullptr)
Determines the available OpenXR API layers.
std::string translateResult(const XrResult xrResult) const
Translates an OpenXR result associated with this instance into a readable string.
std::vector< XrViewConfigurationView > XrViewConfigurationViews
Definition of a vector holding XrViewConfigurationView objects.
Definition: Instance.h:46
Instance & operator=(const Instance &)=delete
Disabled assign operator.
Instance & operator=(Instance &&instance)
Move operator.
bool determineViewConfigurations(const XrViewConfigurationType xrViewConfigurationType, XrViewConfigurationViews &xrViewConfigurationViews) const
Determines the view configurations for a specified view type available for this instance.
Definition: Instance.h:200
Lock lock_
The instance's lock.
Definition: Instance.h:192
StringSet enabledExtensions_
The instance's enabled extensions.
Definition: Instance.h:189
static bool determineViewConfigurations(const XrInstance &xrInstance, const XrSystemId &xrSystemId, const XrViewConfigurationType xrViewConfigurationType, XrViewConfigurationViews &xrViewConfigurationViews)
Determines the view configurations for a specified view type.
XrInstance xrInstance_
The actual OpenXR instance.
Definition: Instance.h:183
XrSystemId xrSystemId_
The identifier for the runtime.
Definition: Instance.h:186
~Instance()
Destructs the instance and releases all associated resources.
std::vector< XrExtensionProperties > XrExtensionPropertyGroups
Definition of a vector holding extension properties.
Definition: Instance.h:36
XrSystemId xrSystemId() const
Returns the identifier for the runtime.
const StringSet & enabledExtensions() const
Returns the enabled extensions of this instance.
Definition: Instance.h:207
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15