RmlUi_Backend_SDL_VK.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RmlUi_Backend.h"
  29. #include "RmlUi_Platform_SDL.h"
  30. #include "RmlUi_Renderer_VK.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Core.h>
  33. #include <RmlUi/Core/FileInterface.h>
  34. #include <RmlUi/Core/Log.h>
  35. #include <SDL2/SDL.h>
  36. #include <SDL2/SDL_vulkan.h>
  37. #if !SDL_VIDEO_VULKAN
  38. #error "Only the Vulkan SDL backend is supported."
  39. #endif
  40. /**
  41. Global data used by this backend.
  42. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  43. */
  44. struct BackendData {
  45. SystemInterface_SDL system_interface;
  46. RenderInterface_VK render_interface;
  47. SDL_Window* window = nullptr;
  48. bool running = true;
  49. };
  50. static Rml::UniquePtr<BackendData> data;
  51. bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
  52. {
  53. RMLUI_ASSERT(!data);
  54. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
  55. return false;
  56. // Submit click events when focusing the window.
  57. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  58. const Uint32 window_flags = (SDL_WINDOW_VULKAN | (allow_resize ? SDL_WINDOW_RESIZABLE : 0));
  59. SDL_Window* window = SDL_CreateWindow(window_name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
  60. if (!window)
  61. {
  62. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s", SDL_GetError());
  63. return false;
  64. }
  65. data = Rml::MakeUnique<BackendData>();
  66. data->window = window;
  67. Rml::Vector<const char*> extensions;
  68. {
  69. unsigned int count;
  70. if (!SDL_Vulkan_GetInstanceExtensions(window, &count, nullptr))
  71. {
  72. data.reset();
  73. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to get required vulkan extensions");
  74. return false;
  75. }
  76. extensions.resize(count);
  77. if (!SDL_Vulkan_GetInstanceExtensions(window, &count, extensions.data()))
  78. {
  79. data.reset();
  80. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to get required vulkan extensions");
  81. return false;
  82. }
  83. }
  84. if (!data->render_interface.Initialize(std::move(extensions),
  85. [](VkInstance instance, VkSurfaceKHR* out_surface) { return (bool)SDL_Vulkan_CreateSurface(data->window, instance, out_surface); }))
  86. {
  87. data.reset();
  88. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to initialize Vulkan render interface");
  89. return false;
  90. }
  91. data->system_interface.SetWindow(window);
  92. data->render_interface.SetViewport(width, height);
  93. return true;
  94. }
  95. void Backend::Shutdown()
  96. {
  97. RMLUI_ASSERT(data);
  98. data->render_interface.Shutdown();
  99. SDL_DestroyWindow(data->window);
  100. data.reset();
  101. SDL_Quit();
  102. }
  103. Rml::SystemInterface* Backend::GetSystemInterface()
  104. {
  105. RMLUI_ASSERT(data);
  106. return &data->system_interface;
  107. }
  108. Rml::RenderInterface* Backend::GetRenderInterface()
  109. {
  110. RMLUI_ASSERT(data);
  111. return &data->render_interface;
  112. }
  113. static bool WaitForValidSwapchain()
  114. {
  115. bool result = true;
  116. // In some situations the swapchain may become invalid, such as when the window is minimized. In this state the renderer cannot accept any render
  117. // calls. Since we don't have full control over the main loop here we may risk calls to Context::Render if we were to return. Instead, we keep the
  118. // application inside this loop until we are able to recreate the swapchain and render again.
  119. while (!data->render_interface.IsSwapchainValid())
  120. {
  121. SDL_Event ev;
  122. while (SDL_PollEvent(&ev))
  123. {
  124. if (ev.type == SDL_QUIT)
  125. {
  126. // Restore the window so that we can recreate the swapchain, and then properly release all resource and shutdown cleanly.
  127. SDL_RestoreWindow(data->window);
  128. result = false;
  129. }
  130. }
  131. SDL_Delay(10);
  132. data->render_interface.RecreateSwapchain();
  133. }
  134. return result;
  135. }
  136. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  137. {
  138. RMLUI_ASSERT(data && context);
  139. bool result = data->running;
  140. SDL_Event ev;
  141. int has_event = 0;
  142. if (power_save)
  143. has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
  144. else
  145. has_event = SDL_PollEvent(&ev);
  146. while (has_event)
  147. {
  148. switch (ev.type)
  149. {
  150. case SDL_QUIT:
  151. {
  152. result = false;
  153. }
  154. break;
  155. case SDL_KEYDOWN:
  156. {
  157. const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(ev.key.keysym.sym);
  158. const int key_modifier = RmlSDL::GetKeyModifierState();
  159. const float native_dp_ratio = 1.f;
  160. // See if we have any global shortcuts that take priority over the context.
  161. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  162. break;
  163. // Otherwise, hand the event over to the context by calling the input handler as normal.
  164. if (!RmlSDL::InputEventHandler(context, ev))
  165. break;
  166. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  167. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  168. break;
  169. }
  170. break;
  171. case SDL_WINDOWEVENT:
  172. {
  173. if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
  174. {
  175. Rml::Vector2i dimensions(ev.window.data1, ev.window.data2);
  176. context->SetDimensions(dimensions);
  177. data->render_interface.SetViewport(dimensions.x, dimensions.y);
  178. break;
  179. }
  180. }
  181. break;
  182. default:
  183. {
  184. RmlSDL::InputEventHandler(context, ev);
  185. }
  186. break;
  187. }
  188. has_event = SDL_PollEvent(&ev);
  189. }
  190. if (!WaitForValidSwapchain())
  191. result = false;
  192. return result;
  193. }
  194. void Backend::RequestExit()
  195. {
  196. RMLUI_ASSERT(data);
  197. data->running = false;
  198. }
  199. void Backend::BeginFrame()
  200. {
  201. RMLUI_ASSERT(data);
  202. data->render_interface.BeginFrame();
  203. }
  204. void Backend::PresentFrame()
  205. {
  206. RMLUI_ASSERT(data);
  207. data->render_interface.EndFrame();
  208. }