RmlUi_Backend_SDL_VK.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 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 <SDL2/SDL.h>
  35. #include <SDL2/SDL_vulkan.h>
  36. #if !SDL_VIDEO_VULKAN
  37. #error "Only the Vulkan SDL backend is supported."
  38. #endif
  39. /**
  40. Global data used by this backend.
  41. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  42. */
  43. struct BackendData {
  44. SystemInterface_SDL system_interface;
  45. RenderInterface_VK render_interface;
  46. SDL_Window* window = nullptr;
  47. bool running = true;
  48. };
  49. static Rml::UniquePtr<BackendData> data;
  50. bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
  51. {
  52. RMLUI_ASSERT(!data);
  53. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
  54. return false;
  55. const Uint32 window_flags = (SDL_WINDOW_VULKAN | (allow_resize ? SDL_WINDOW_RESIZABLE : 0));
  56. SDL_Window* window = SDL_CreateWindow(window_name, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, window_flags);
  57. if (!window)
  58. {
  59. fprintf(stderr, "SDL error on create window: %s\n", SDL_GetError());
  60. return false;
  61. }
  62. data = Rml::MakeUnique<BackendData>();
  63. data->window = window;
  64. if (!data->render_interface.Initialize(
  65. [](VkInstance instance, VkSurfaceKHR* out_surface) { return (bool)SDL_Vulkan_CreateSurface(data->window, instance, out_surface); }))
  66. {
  67. data.reset();
  68. fprintf(stderr, "Could not initialize Vulkan render interface.");
  69. return false;
  70. }
  71. data->system_interface.SetWindow(window);
  72. data->render_interface.SetViewport(width, height);
  73. return true;
  74. }
  75. void Backend::Shutdown()
  76. {
  77. RMLUI_ASSERT(data);
  78. data->render_interface.Shutdown();
  79. SDL_DestroyWindow(data->window);
  80. data.reset();
  81. SDL_Quit();
  82. }
  83. Rml::SystemInterface* Backend::GetSystemInterface()
  84. {
  85. RMLUI_ASSERT(data);
  86. return &data->system_interface;
  87. }
  88. Rml::RenderInterface* Backend::GetRenderInterface()
  89. {
  90. RMLUI_ASSERT(data);
  91. return &data->render_interface;
  92. }
  93. static bool WaitForValidSwapchain()
  94. {
  95. bool result = true;
  96. // In some situations the swapchain may become invalid, such as when the window is minimized. In this state the renderer cannot accept any render
  97. // 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
  98. // application inside this loop until we are able to recreate the swapchain and render again.
  99. while (!data->render_interface.IsSwapchainValid())
  100. {
  101. SDL_Event ev;
  102. while (SDL_PollEvent(&ev))
  103. {
  104. if (ev.type == SDL_QUIT)
  105. {
  106. // Restore the window so that we can recreate the swapchain, and then properly release all resource and shutdown cleanly.
  107. SDL_RestoreWindow(data->window);
  108. result = false;
  109. }
  110. }
  111. SDL_Delay(10);
  112. data->render_interface.RecreateSwapchain();
  113. }
  114. return result;
  115. }
  116. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback)
  117. {
  118. RMLUI_ASSERT(data && context);
  119. bool result = data->running;
  120. SDL_Event ev;
  121. while (SDL_PollEvent(&ev))
  122. {
  123. switch (ev.type)
  124. {
  125. case SDL_QUIT:
  126. {
  127. result = false;
  128. }
  129. break;
  130. case SDL_KEYDOWN:
  131. {
  132. const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(ev.key.keysym.sym);
  133. const int key_modifier = RmlSDL::GetKeyModifierState();
  134. const float native_dp_ratio = 1.f;
  135. // See if we have any global shortcuts that take priority over the context.
  136. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  137. break;
  138. // Otherwise, hand the event over to the context by calling the input handler as normal.
  139. if (!RmlSDL::InputEventHandler(context, ev))
  140. break;
  141. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  142. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  143. break;
  144. }
  145. break;
  146. case SDL_WINDOWEVENT:
  147. {
  148. if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
  149. {
  150. Rml::Vector2i dimensions(ev.window.data1, ev.window.data2);
  151. context->SetDimensions(dimensions);
  152. data->render_interface.SetViewport(dimensions.x, dimensions.y);
  153. break;
  154. }
  155. }
  156. break;
  157. default:
  158. {
  159. RmlSDL::InputEventHandler(context, ev);
  160. }
  161. break;
  162. }
  163. }
  164. if (!WaitForValidSwapchain())
  165. result = false;
  166. return result;
  167. }
  168. void Backend::RequestExit()
  169. {
  170. RMLUI_ASSERT(data);
  171. data->running = false;
  172. }
  173. void Backend::BeginFrame()
  174. {
  175. RMLUI_ASSERT(data);
  176. data->render_interface.BeginFrame();
  177. }
  178. void Backend::PresentFrame()
  179. {
  180. RMLUI_ASSERT(data);
  181. data->render_interface.EndFrame();
  182. }