openxr_composition_layer_extension.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**************************************************************************/
  2. /* openxr_composition_layer_extension.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef OPENXR_COMPOSITION_LAYER_EXTENSION_H
  31. #define OPENXR_COMPOSITION_LAYER_EXTENSION_H
  32. #include "openxr_composition_layer_provider.h"
  33. #include "openxr_extension_wrapper.h"
  34. #include "../openxr_api.h"
  35. #ifdef ANDROID_ENABLED
  36. #include <jni.h>
  37. // Copied here from openxr_platform.h, in order to avoid including that whole header,
  38. // which can cause compilation issues on some platforms.
  39. typedef XrResult(XRAPI_PTR *PFN_xrCreateSwapchainAndroidSurfaceKHR)(XrSession session, const XrSwapchainCreateInfo *info, XrSwapchain *swapchain, jobject *surface);
  40. #endif
  41. class JavaObject;
  42. class OpenXRViewportCompositionLayerProvider;
  43. // This extension provides access to composition layers for displaying 2D content through the XR compositor.
  44. // OpenXRCompositionLayerExtension enables the extensions related to this functionality
  45. class OpenXRCompositionLayerExtension : public OpenXRExtensionWrapper, public OpenXRCompositionLayerProvider {
  46. public:
  47. static OpenXRCompositionLayerExtension *get_singleton();
  48. OpenXRCompositionLayerExtension();
  49. virtual ~OpenXRCompositionLayerExtension() override;
  50. virtual HashMap<String, bool *> get_requested_extensions() override;
  51. virtual void on_instance_created(const XrInstance p_instance) override;
  52. virtual void on_session_created(const XrSession p_session) override;
  53. virtual void on_session_destroyed() override;
  54. virtual void on_pre_render() override;
  55. virtual int get_composition_layer_count() override;
  56. virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index) override;
  57. virtual int get_composition_layer_order(int p_index) override;
  58. void register_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer);
  59. void unregister_viewport_composition_layer_provider(OpenXRViewportCompositionLayerProvider *p_composition_layer);
  60. bool is_available(XrStructureType p_which);
  61. bool is_android_surface_swapchain_available() { return android_surface_ext_available; }
  62. #ifdef ANDROID_ENABLED
  63. bool create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface);
  64. void free_android_surface_swapchain(XrSwapchain p_swapchain);
  65. #endif
  66. private:
  67. static OpenXRCompositionLayerExtension *singleton;
  68. Vector<OpenXRViewportCompositionLayerProvider *> composition_layers;
  69. bool cylinder_ext_available = false;
  70. bool equirect_ext_available = false;
  71. bool android_surface_ext_available = false;
  72. #ifdef ANDROID_ENABLED
  73. Vector<XrSwapchain> android_surface_swapchain_free_queue;
  74. void free_queued_android_surface_swapchains();
  75. EXT_PROTO_XRRESULT_FUNC1(xrDestroySwapchain, (XrSwapchain), swapchain)
  76. EXT_PROTO_XRRESULT_FUNC4(xrCreateSwapchainAndroidSurfaceKHR, (XrSession), session, (const XrSwapchainCreateInfo *), info, (XrSwapchain *), swapchain, (jobject *), surface)
  77. #endif
  78. };
  79. class OpenXRViewportCompositionLayerProvider {
  80. XrCompositionLayerBaseHeader *composition_layer = nullptr;
  81. int sort_order = 1;
  82. bool alpha_blend = false;
  83. Dictionary extension_property_values;
  84. bool extension_property_values_changed = true;
  85. struct {
  86. RID viewport;
  87. Size2i viewport_size;
  88. OpenXRAPI::OpenXRSwapChainInfo swapchain_info;
  89. bool static_image = false;
  90. } subviewport;
  91. #ifdef ANDROID_ENABLED
  92. struct {
  93. XrSwapchain swapchain = XR_NULL_HANDLE;
  94. Ref<JavaObject> surface;
  95. } android_surface;
  96. #endif
  97. bool use_android_surface = false;
  98. Size2i swapchain_size;
  99. OpenXRAPI *openxr_api = nullptr;
  100. OpenXRCompositionLayerExtension *composition_layer_extension = nullptr;
  101. // Only for SubViewports.
  102. bool update_and_acquire_swapchain(bool p_static_image);
  103. RID get_current_swapchain_texture();
  104. void update_swapchain_sub_image(XrSwapchainSubImage &r_swapchain_sub_image);
  105. void free_swapchain();
  106. #ifdef ANDROID_ENABLED
  107. void create_android_surface();
  108. #endif
  109. public:
  110. XrStructureType get_openxr_type() { return composition_layer->type; }
  111. void set_sort_order(int p_sort_order) { sort_order = p_sort_order; }
  112. int get_sort_order() const { return sort_order; }
  113. void set_alpha_blend(bool p_alpha_blend);
  114. bool get_alpha_blend() const { return alpha_blend; }
  115. void set_viewport(RID p_viewport, Size2i p_size);
  116. RID get_viewport() const { return subviewport.viewport; }
  117. void set_use_android_surface(bool p_enable, Size2i p_size);
  118. bool get_use_android_surface() const { return use_android_surface; }
  119. Ref<JavaObject> get_android_surface();
  120. void set_extension_property_values(const Dictionary &p_property_values);
  121. void on_pre_render();
  122. XrCompositionLayerBaseHeader *get_composition_layer();
  123. OpenXRViewportCompositionLayerProvider(XrCompositionLayerBaseHeader *p_composition_layer);
  124. ~OpenXRViewportCompositionLayerProvider();
  125. };
  126. #endif // OPENXR_COMPOSITION_LAYER_EXTENSION_H