TestsShell.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "TestsShell.h"
  29. #include "TestsInterface.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Core.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/EventListener.h>
  34. #include <RmlUi/Debugger.h>
  35. #include <RmlUi_Backend.h>
  36. #include <Shell.h>
  37. #include <doctest.h>
  38. // Uncomment the following to render to the shell window instead of the dummy renderer. Useful for viewing the result while building RML.
  39. // #define RMLUI_TESTS_USE_SHELL
  40. namespace {
  41. const Rml::Vector2i window_size(1500, 800);
  42. bool shell_initialized = false;
  43. bool debugger_allowed = true;
  44. int num_documents_begin = 0;
  45. Rml::Context* shell_context = nullptr;
  46. TestsSystemInterface tests_system_interface;
  47. #ifdef RMLUI_TESTS_USE_SHELL
  48. class TestsShellEventListener : public Rml::EventListener {
  49. public:
  50. void ProcessEvent(Rml::Event& event) override
  51. {
  52. if (event.GetId() == Rml::EventId::Keydown)
  53. {
  54. Rml::Input::KeyIdentifier key_identifier = (Rml::Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  55. // Will escape the current render loop
  56. if (key_identifier == Rml::Input::KI_ESCAPE || key_identifier == Rml::Input::KI_RETURN || key_identifier == Rml::Input::KI_NUMPADENTER)
  57. Backend::RequestExit();
  58. }
  59. }
  60. } shell_event_listener;
  61. #else
  62. // The tests renderer only collects statistics, does not render anything.
  63. TestsRenderInterface shell_render_interface;
  64. #endif
  65. } // namespace
  66. static void InitializeShell(bool allow_debugger)
  67. {
  68. if (shell_initialized)
  69. return;
  70. // Initialize shell and create context.
  71. shell_initialized = true;
  72. debugger_allowed = allow_debugger;
  73. REQUIRE(Shell::Initialize());
  74. #ifdef RMLUI_TESTS_USE_SHELL
  75. // Initialize the backend and launch a window.
  76. REQUIRE(Backend::Initialize("RmlUi Tests", window_size.x, window_size.y, true));
  77. // Use our custom tests system interface.
  78. Rml::SetSystemInterface(&tests_system_interface);
  79. // However, use the backend's render interface.
  80. Rml::SetRenderInterface(Backend::GetRenderInterface());
  81. REQUIRE(Rml::Initialise());
  82. shell_context = Rml::CreateContext("main", window_size);
  83. Shell::LoadFonts();
  84. if (allow_debugger)
  85. {
  86. Rml::Debugger::Initialise(shell_context);
  87. num_documents_begin = shell_context->GetNumDocuments();
  88. }
  89. shell_context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, &shell_event_listener, true);
  90. #else
  91. // Set our custom system and render interfaces.
  92. Rml::SetSystemInterface(&tests_system_interface);
  93. Rml::SetRenderInterface(&shell_render_interface);
  94. REQUIRE(Rml::Initialise());
  95. shell_context = Rml::CreateContext("main", window_size);
  96. Shell::LoadFonts();
  97. #endif
  98. }
  99. Rml::Context* TestsShell::GetContext(bool allow_debugger)
  100. {
  101. InitializeShell(allow_debugger);
  102. return shell_context;
  103. }
  104. void TestsShell::BeginFrame()
  105. {
  106. #ifdef RMLUI_TESTS_USE_SHELL
  107. Backend::BeginFrame();
  108. #endif
  109. }
  110. void TestsShell::PresentFrame()
  111. {
  112. #ifdef RMLUI_TESTS_USE_SHELL
  113. Backend::PresentFrame();
  114. #endif
  115. }
  116. void TestsShell::RenderLoop()
  117. {
  118. REQUIRE(shell_context);
  119. #ifdef RMLUI_TESTS_USE_SHELL
  120. bool running = true;
  121. while (running)
  122. {
  123. running = Backend::ProcessEvents(shell_context, &Shell::ProcessKeyDownShortcuts);
  124. shell_context->Update();
  125. BeginFrame();
  126. shell_context->Render();
  127. PresentFrame();
  128. }
  129. #else
  130. shell_context->Update();
  131. shell_context->Render();
  132. #endif
  133. }
  134. void TestsShell::ShutdownShell()
  135. {
  136. if (!shell_initialized)
  137. return;
  138. if (debugger_allowed)
  139. {
  140. RMLUI_ASSERTMSG(shell_context->GetNumDocuments() == num_documents_begin, "Make sure all previously opened documents have been closed.");
  141. (void)num_documents_begin;
  142. }
  143. tests_system_interface.SetNumExpectedWarnings(0);
  144. Rml::Shutdown();
  145. #ifdef RMLUI_TESTS_USE_SHELL
  146. Backend::Shutdown();
  147. #else
  148. shell_render_interface.Reset();
  149. #endif
  150. Shell::Shutdown();
  151. shell_context = nullptr;
  152. shell_initialized = false;
  153. }
  154. void TestsShell::SetNumExpectedWarnings(int num_warnings)
  155. {
  156. tests_system_interface.SetNumExpectedWarnings(num_warnings);
  157. }
  158. Rml::String TestsShell::GetRenderStats()
  159. {
  160. Rml::String result;
  161. #if !defined(RMLUI_TESTS_USE_SHELL)
  162. shell_context->Update();
  163. shell_render_interface.ResetCounters();
  164. shell_context->Render();
  165. auto& counters = shell_render_interface.GetCounters();
  166. result = Rml::CreateString("Context::Render() stats:\n"
  167. " Compile geometry: %zu\n"
  168. " Render geometry: %zu\n"
  169. " Release geometry: %zu\n"
  170. " Texture load: %zu\n"
  171. " Texture generate: %zu\n"
  172. " Texture release: %zu\n"
  173. " Scissor enable: %zu\n"
  174. " Scissor set: %zu\n"
  175. " Clip mask enable: %zu\n"
  176. " Clip mask render: %zu\n"
  177. " Transform set: %zu",
  178. counters.compile_geometry, counters.render_geometry, counters.release_geometry, counters.load_texture, counters.generate_texture,
  179. counters.release_texture, counters.enable_scissor, counters.set_scissor, counters.enable_clip_mask, counters.render_to_clip_mask,
  180. counters.set_transform);
  181. #endif
  182. return result;
  183. }
  184. TestsRenderInterface* TestsShell::GetTestsRenderInterface()
  185. {
  186. #if defined(RMLUI_TESTS_USE_SHELL)
  187. return nullptr;
  188. #else
  189. return &shell_render_interface;
  190. #endif
  191. }
  192. TestsSystemInterface* TestsShell::GetTestsSystemInterface()
  193. {
  194. return &tests_system_interface;
  195. }