1 | // Copyright (c) 2017-2023, 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 <marky@lunarg.com> |
8 | // |
9 | |
10 | #pragma once |
11 | |
12 | #include "loader_platform.hpp" |
13 | |
14 | #include <openxr/openxr.h> |
15 | |
16 | #include <string> |
17 | #include <vector> |
18 | #include <unordered_map> |
19 | #include <mutex> |
20 | #include <memory> |
21 | |
22 | #ifdef XR_USE_PLATFORM_ANDROID |
23 | #define XR_KHR_LOADER_INIT_SUPPORT |
24 | #endif |
25 | |
26 | namespace Json { |
27 | class Value; |
28 | } |
29 | |
30 | #ifdef XR_KHR_LOADER_INIT_SUPPORT |
31 | //! Initialize loader, where required. |
32 | XrResult InitializeLoader(const XrLoaderInitInfoBaseHeaderKHR* loaderInitInfo); |
33 | XrResult GetPlatformRuntimeVirtualManifest(Json::Value& out_manifest); |
34 | std::string GetAndroidNativeLibraryDir(); |
35 | void* Android_Get_Asset_Manager(); |
36 | #endif |
37 | |
38 | class RuntimeManifestFile; |
39 | struct XrGeneratedDispatchTable; |
40 | |
41 | class RuntimeInterface { |
42 | public: |
43 | virtual ~RuntimeInterface(); |
44 | |
45 | // Helper functions for loading and unloading the runtime (but only when necessary) |
46 | static XrResult LoadRuntime(const std::string& openxr_command); |
47 | static void UnloadRuntime(const std::string& openxr_command); |
48 | static RuntimeInterface& GetRuntime() { return *(GetInstance().get()); } |
49 | static XrResult GetInstanceProcAddr(XrInstance instance, const char* name, PFN_xrVoidFunction* function); |
50 | |
51 | // Get the direct dispatch table to this runtime, without API layers or loader terminators. |
52 | static const XrGeneratedDispatchTable* GetDispatchTable(XrInstance instance); |
53 | static const XrGeneratedDispatchTable* GetDebugUtilsMessengerDispatchTable(XrDebugUtilsMessengerEXT messenger); |
54 | |
55 | void GetInstanceExtensionProperties(std::vector<XrExtensionProperties>& extension_properties); |
56 | bool SupportsExtension(const std::string& extension_name); |
57 | XrResult CreateInstance(const XrInstanceCreateInfo* info, XrInstance* instance); |
58 | XrResult DestroyInstance(XrInstance instance); |
59 | bool TrackDebugMessenger(XrInstance instance, XrDebugUtilsMessengerEXT messenger); |
60 | void ForgetDebugMessenger(XrDebugUtilsMessengerEXT messenger); |
61 | |
62 | // No default construction |
63 | RuntimeInterface() = delete; |
64 | |
65 | // Non-copyable |
66 | RuntimeInterface(const RuntimeInterface&) = delete; |
67 | RuntimeInterface& operator=(const RuntimeInterface&) = delete; |
68 | |
69 | private: |
70 | RuntimeInterface(LoaderPlatformLibraryHandle runtime_library, PFN_xrGetInstanceProcAddr get_instance_proc_addr); |
71 | void SetSupportedExtensions(std::vector<std::string>& supported_extensions); |
72 | static XrResult TryLoadingSingleRuntime(const std::string& openxr_command, std::unique_ptr<RuntimeManifestFile>& manifest_file); |
73 | |
74 | static std::unique_ptr<RuntimeInterface>& GetInstance() { |
75 | static std::unique_ptr<RuntimeInterface> instance; |
76 | return instance; |
77 | } |
78 | |
79 | LoaderPlatformLibraryHandle _runtime_library; |
80 | PFN_xrGetInstanceProcAddr _get_instance_proc_addr; |
81 | std::unordered_map<XrInstance, std::unique_ptr<XrGeneratedDispatchTable>> _dispatch_table_map; |
82 | std::mutex _dispatch_table_mutex; |
83 | std::unordered_map<XrDebugUtilsMessengerEXT, XrInstance> _messenger_to_instance_map; |
84 | std::mutex _messenger_to_instance_mutex; |
85 | std::vector<std::string> _supported_extensions; |
86 | }; |
87 | |