runtime_interface.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2017-2025 The Khronos Group Inc.
  2. // Copyright (c) 2017-2019 Valve Corporation
  3. // Copyright (c) 2017-2019 LunarG, Inc.
  4. //
  5. // SPDX-License-Identifier: Apache-2.0 OR MIT
  6. //
  7. // Initial Author: Mark Young <[email protected]>
  8. //
  9. #pragma once
  10. #include "loader_platform.hpp"
  11. #include <openxr/openxr.h>
  12. #include <string>
  13. #include <vector>
  14. #include <unordered_map>
  15. #include <mutex>
  16. #include <memory>
  17. namespace Json {
  18. class Value;
  19. }
  20. class RuntimeManifestFile;
  21. struct XrGeneratedDispatchTableCore;
  22. class RuntimeInterface {
  23. public:
  24. virtual ~RuntimeInterface();
  25. // Helper functions for loading and unloading the runtime (but only when necessary)
  26. static XrResult LoadRuntime(const std::string& openxr_command);
  27. static void UnloadRuntime(const std::string& openxr_command);
  28. static RuntimeInterface& GetRuntime() { return *(GetInstance().get()); }
  29. static XrResult GetInstanceProcAddr(XrInstance instance, const char* name, PFN_xrVoidFunction* function);
  30. // Get the direct dispatch table to this runtime, without API layers or loader terminators.
  31. static const XrGeneratedDispatchTableCore* GetDispatchTable(XrInstance instance);
  32. static const XrGeneratedDispatchTableCore* GetDebugUtilsMessengerDispatchTable(XrDebugUtilsMessengerEXT messenger);
  33. void GetInstanceExtensionProperties(std::vector<XrExtensionProperties>& extension_properties);
  34. bool SupportsExtension(const std::string& extension_name);
  35. XrResult CreateInstance(const XrInstanceCreateInfo* info, XrInstance* instance);
  36. XrResult DestroyInstance(XrInstance instance);
  37. bool TrackDebugMessenger(XrInstance instance, XrDebugUtilsMessengerEXT messenger);
  38. void ForgetDebugMessenger(XrDebugUtilsMessengerEXT messenger);
  39. // No default construction
  40. RuntimeInterface() = delete;
  41. // Non-copyable
  42. RuntimeInterface(const RuntimeInterface&) = delete;
  43. RuntimeInterface& operator=(const RuntimeInterface&) = delete;
  44. private:
  45. RuntimeInterface(LoaderPlatformLibraryHandle runtime_library, PFN_xrGetInstanceProcAddr get_instance_proc_addr);
  46. void SetSupportedExtensions(std::vector<std::string>& supported_extensions);
  47. static XrResult TryLoadingSingleRuntime(const std::string& openxr_command, std::unique_ptr<RuntimeManifestFile>& manifest_file);
  48. static std::unique_ptr<RuntimeInterface>& GetInstance() {
  49. static std::unique_ptr<RuntimeInterface> instance;
  50. return instance;
  51. }
  52. LoaderPlatformLibraryHandle _runtime_library;
  53. PFN_xrGetInstanceProcAddr _get_instance_proc_addr;
  54. std::unordered_map<XrInstance, std::unique_ptr<XrGeneratedDispatchTableCore>> _dispatch_table_map;
  55. std::mutex _dispatch_table_mutex;
  56. std::unordered_map<XrDebugUtilsMessengerEXT, XrInstance> _messenger_to_instance_map;
  57. std::mutex _messenger_to_instance_mutex;
  58. std::vector<std::string> _supported_extensions;
  59. };