RmlUi_Backend_SDL_GPU.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_SDL_GPU.h"
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Core.h>
  33. #include <RmlUi/Core/Log.h>
  34. #ifndef RMLUI_BACKEND_SDL_GPU_DEBUG
  35. #define RMLUI_BACKEND_SDL_GPU_DEBUG false
  36. #endif
  37. /**
  38. Global data used by this backend.
  39. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  40. */
  41. struct BackendData {
  42. BackendData(SDL_GPUDevice* device, SDL_Window* window) : render_interface(device, window) {}
  43. SystemInterface_SDL system_interface;
  44. RenderInterface_SDL_GPU render_interface;
  45. SDL_Window* window = nullptr;
  46. SDL_GPUDevice* device = nullptr;
  47. SDL_GPUCommandBuffer* command_buffer = 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))
  55. return false;
  56. // Submit click events when focusing the window.
  57. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  58. // Touch events are handled natively, no need to generate synthetic mouse events for touch devices.
  59. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  60. #if defined RMLUI_BACKEND_SIMULATE_TOUCH
  61. // Simulate touch events from mouse events for testing touch behavior on a desktop machine.
  62. SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
  63. #endif
  64. const float window_size_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
  65. SDL_PropertiesID props = SDL_CreateProperties();
  66. SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, window_name);
  67. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
  68. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
  69. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, int(width * window_size_scale));
  70. SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, int(height * window_size_scale));
  71. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, allow_resize);
  72. SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
  73. SDL_Window* window = SDL_CreateWindowWithProperties(props);
  74. SDL_DestroyProperties(props);
  75. if (!window)
  76. {
  77. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create window: %s", SDL_GetError());
  78. return false;
  79. }
  80. props = SDL_CreateProperties();
  81. SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, true);
  82. SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, true);
  83. SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN, true);
  84. SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, RMLUI_BACKEND_SDL_GPU_DEBUG);
  85. SDL_GPUDevice* device = SDL_CreateGPUDeviceWithProperties(props);
  86. SDL_DestroyProperties(props);
  87. if (!device)
  88. {
  89. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on create GPU device: %s", SDL_GetError());
  90. return false;
  91. }
  92. if (!SDL_ClaimWindowForGPUDevice(device, window))
  93. {
  94. Rml::Log::Message(Rml::Log::LT_ERROR, "SDL error on claiming window for GPU device: %s", SDL_GetError());
  95. return false;
  96. }
  97. data = Rml::MakeUnique<BackendData>(device, window);
  98. data->window = window;
  99. data->device = device;
  100. data->system_interface.SetWindow(window);
  101. const char* renderer_name = SDL_GetGPUDeviceDriver(device);
  102. data->system_interface.LogMessage(Rml::Log::LT_INFO, Rml::CreateString("Using SDL device driver: %s", renderer_name));
  103. return true;
  104. }
  105. void Backend::Shutdown()
  106. {
  107. RMLUI_ASSERT(data);
  108. data->render_interface.Shutdown();
  109. SDL_ReleaseWindowFromGPUDevice(data->device, data->window);
  110. SDL_DestroyGPUDevice(data->device);
  111. SDL_DestroyWindow(data->window);
  112. data.reset();
  113. SDL_Quit();
  114. }
  115. Rml::SystemInterface* Backend::GetSystemInterface()
  116. {
  117. RMLUI_ASSERT(data);
  118. return &data->system_interface;
  119. }
  120. Rml::RenderInterface* Backend::GetRenderInterface()
  121. {
  122. RMLUI_ASSERT(data);
  123. return &data->render_interface;
  124. }
  125. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback, bool power_save)
  126. {
  127. RMLUI_ASSERT(data && context);
  128. auto GetKey = [](const SDL_Event& event) { return event.key.key; };
  129. auto GetDisplayScale = []() { return SDL_GetWindowDisplayScale(data->window); };
  130. constexpr auto event_quit = SDL_EVENT_QUIT;
  131. constexpr auto event_key_down = SDL_EVENT_KEY_DOWN;
  132. bool has_event = false;
  133. bool result = data->running;
  134. data->running = true;
  135. SDL_Event ev;
  136. if (power_save)
  137. has_event = SDL_WaitEventTimeout(&ev, static_cast<int>(Rml::Math::Min(context->GetNextUpdateDelay(), 10.0) * 1000));
  138. else
  139. has_event = SDL_PollEvent(&ev);
  140. while (has_event)
  141. {
  142. bool propagate_event = true;
  143. switch (ev.type)
  144. {
  145. case event_quit:
  146. {
  147. propagate_event = false;
  148. result = false;
  149. }
  150. break;
  151. case event_key_down:
  152. {
  153. propagate_event = false;
  154. const Rml::Input::KeyIdentifier key = RmlSDL::ConvertKey(GetKey(ev));
  155. const int key_modifier = RmlSDL::GetKeyModifierState();
  156. const float native_dp_ratio = GetDisplayScale();
  157. // See if we have any global shortcuts that take priority over the context.
  158. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  159. break;
  160. // Otherwise, hand the event over to the context by calling the input handler as normal.
  161. if (!RmlSDL::InputEventHandler(context, data->window, ev))
  162. break;
  163. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  164. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  165. break;
  166. }
  167. break;
  168. default: break;
  169. }
  170. if (propagate_event)
  171. RmlSDL::InputEventHandler(context, data->window, ev);
  172. has_event = SDL_PollEvent(&ev);
  173. }
  174. return result;
  175. }
  176. void Backend::RequestExit()
  177. {
  178. RMLUI_ASSERT(data);
  179. data->running = false;
  180. }
  181. void Backend::BeginFrame()
  182. {
  183. RMLUI_ASSERT(data);
  184. data->command_buffer = SDL_AcquireGPUCommandBuffer(data->device);
  185. if (!data->command_buffer)
  186. {
  187. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to acquire command buffer: %s", SDL_GetError());
  188. return;
  189. }
  190. SDL_GPUTexture* swapchain_texture;
  191. uint32_t width;
  192. uint32_t height;
  193. if (!SDL_WaitAndAcquireGPUSwapchainTexture(data->command_buffer, data->window, &swapchain_texture, &width, &height))
  194. {
  195. Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to acquire swapchain texture: %s", SDL_GetError());
  196. return;
  197. }
  198. if (!swapchain_texture || !width || !height)
  199. {
  200. // Not an error. Happens on minimize
  201. SDL_CancelGPUCommandBuffer(data->command_buffer);
  202. return;
  203. }
  204. // Do your normal draw operations (make sure you clear the swapchain texture)
  205. SDL_GPUColorTargetInfo color_info{};
  206. color_info.texture = swapchain_texture;
  207. color_info.load_op = SDL_GPU_LOADOP_CLEAR;
  208. color_info.store_op = SDL_GPU_STOREOP_STORE;
  209. SDL_GPURenderPass* render_pass = SDL_BeginGPURenderPass(data->command_buffer, &color_info, 1, nullptr);
  210. SDL_EndGPURenderPass(render_pass);
  211. data->render_interface.BeginFrame(data->command_buffer, swapchain_texture, width, height);
  212. }
  213. void Backend::PresentFrame()
  214. {
  215. RMLUI_ASSERT(data);
  216. data->render_interface.EndFrame();
  217. SDL_SubmitGPUCommandBuffer(data->command_buffer);
  218. data->command_buffer = nullptr;
  219. }