RmlUi_Backend_SDL_GPU.cpp 8.3 KB

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