openxr_fb_display_refresh_rate_extension.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**************************************************************************/
  2. /* openxr_fb_display_refresh_rate_extension.cpp */
  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. #include "openxr_fb_display_refresh_rate_extension.h"
  31. #include "../openxr_interface.h"
  32. OpenXRDisplayRefreshRateExtension *OpenXRDisplayRefreshRateExtension::singleton = nullptr;
  33. OpenXRDisplayRefreshRateExtension *OpenXRDisplayRefreshRateExtension::get_singleton() {
  34. return singleton;
  35. }
  36. OpenXRDisplayRefreshRateExtension::OpenXRDisplayRefreshRateExtension() {
  37. singleton = this;
  38. }
  39. OpenXRDisplayRefreshRateExtension::~OpenXRDisplayRefreshRateExtension() {
  40. display_refresh_rate_ext = false;
  41. }
  42. HashMap<String, bool *> OpenXRDisplayRefreshRateExtension::get_requested_extensions() {
  43. HashMap<String, bool *> request_extensions;
  44. request_extensions[XR_FB_DISPLAY_REFRESH_RATE_EXTENSION_NAME] = &display_refresh_rate_ext;
  45. return request_extensions;
  46. }
  47. void OpenXRDisplayRefreshRateExtension::on_instance_created(const XrInstance p_instance) {
  48. if (display_refresh_rate_ext) {
  49. EXT_INIT_XR_FUNC(xrEnumerateDisplayRefreshRatesFB);
  50. EXT_INIT_XR_FUNC(xrGetDisplayRefreshRateFB);
  51. EXT_INIT_XR_FUNC(xrRequestDisplayRefreshRateFB);
  52. }
  53. }
  54. void OpenXRDisplayRefreshRateExtension::on_instance_destroyed() {
  55. display_refresh_rate_ext = false;
  56. }
  57. bool OpenXRDisplayRefreshRateExtension::on_event_polled(const XrEventDataBuffer &event) {
  58. switch (event.type) {
  59. case XR_TYPE_EVENT_DATA_DISPLAY_REFRESH_RATE_CHANGED_FB: {
  60. const XrEventDataDisplayRefreshRateChangedFB *event_fb = (XrEventDataDisplayRefreshRateChangedFB *)&event;
  61. OpenXRInterface *xr_interface = OpenXRAPI::get_singleton()->get_xr_interface();
  62. if (xr_interface) {
  63. xr_interface->on_refresh_rate_changes(event_fb->toDisplayRefreshRate);
  64. }
  65. return true;
  66. } break;
  67. default:
  68. return false;
  69. }
  70. }
  71. float OpenXRDisplayRefreshRateExtension::get_refresh_rate() const {
  72. float refresh_rate = 0.0;
  73. if (display_refresh_rate_ext) {
  74. float rate;
  75. XrResult result = xrGetDisplayRefreshRateFB(OpenXRAPI::get_singleton()->get_session(), &rate);
  76. if (XR_FAILED(result)) {
  77. print_line("OpenXR: Failed to obtain refresh rate [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  78. } else {
  79. refresh_rate = rate;
  80. }
  81. }
  82. return refresh_rate;
  83. }
  84. void OpenXRDisplayRefreshRateExtension::set_refresh_rate(float p_refresh_rate) {
  85. if (display_refresh_rate_ext) {
  86. XrResult result = xrRequestDisplayRefreshRateFB(OpenXRAPI::get_singleton()->get_session(), p_refresh_rate);
  87. if (XR_FAILED(result)) {
  88. print_line("OpenXR: Failed to set refresh rate [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  89. }
  90. }
  91. }
  92. Array OpenXRDisplayRefreshRateExtension::get_available_refresh_rates() const {
  93. Array arr;
  94. XrResult result;
  95. if (display_refresh_rate_ext) {
  96. uint32_t display_refresh_rate_count = 0;
  97. result = xrEnumerateDisplayRefreshRatesFB(OpenXRAPI::get_singleton()->get_session(), 0, &display_refresh_rate_count, nullptr);
  98. if (XR_FAILED(result)) {
  99. print_line("OpenXR: Failed to obtain refresh rates count [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  100. }
  101. if (display_refresh_rate_count > 0) {
  102. float *display_refresh_rates = (float *)memalloc(sizeof(float) * display_refresh_rate_count);
  103. if (display_refresh_rates == nullptr) {
  104. print_line("OpenXR: Failed to obtain refresh rates memory buffer [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  105. return arr;
  106. }
  107. result = xrEnumerateDisplayRefreshRatesFB(OpenXRAPI::get_singleton()->get_session(), display_refresh_rate_count, &display_refresh_rate_count, display_refresh_rates);
  108. if (XR_FAILED(result)) {
  109. print_line("OpenXR: Failed to obtain refresh rates count [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  110. memfree(display_refresh_rates);
  111. return arr;
  112. }
  113. for (uint32_t i = 0; i < display_refresh_rate_count; i++) {
  114. float refresh_rate = display_refresh_rates[i];
  115. arr.push_back(Variant(refresh_rate));
  116. }
  117. memfree(display_refresh_rates);
  118. }
  119. }
  120. return arr;
  121. }