RmlUi_Backend_SDL_GPU.cpp 7.8 KB

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