TestsShell.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. REQUIRE(!shell_initialized);
  69. // Initialize shell and create context.
  70. shell_initialized = true;
  71. debugger_allowed = allow_debugger;
  72. REQUIRE(Shell::Initialize());
  73. #ifdef RMLUI_TESTS_USE_SHELL
  74. // Initialize the backend and launch a window.
  75. REQUIRE(Backend::Initialize("RmlUi Tests", window_size.x, window_size.y, true));
  76. // Use our custom tests system interface.
  77. Rml::SetSystemInterface(&tests_system_interface);
  78. // However, use the backend's render interface.
  79. Rml::SetRenderInterface(Backend::GetRenderInterface());
  80. REQUIRE(Rml::Initialise());
  81. shell_context = Rml::CreateContext("main", window_size);
  82. Shell::LoadFonts();
  83. if (allow_debugger)
  84. {
  85. Rml::Debugger::Initialise(shell_context);
  86. num_documents_begin = shell_context->GetNumDocuments();
  87. }
  88. shell_context->GetRootElement()->AddEventListener(Rml::EventId::Keydown, &shell_event_listener, true);
  89. #else
  90. // Set our custom system and render interfaces.
  91. Rml::SetSystemInterface(&tests_system_interface);
  92. Rml::SetRenderInterface(&shell_render_interface);
  93. REQUIRE(Rml::Initialise());
  94. shell_context = Rml::CreateContext("main", window_size);
  95. Shell::LoadFonts();
  96. #endif
  97. }
  98. Rml::Context* TestsShell::GetContext(bool allow_debugger)
  99. {
  100. InitializeShell(allow_debugger);
  101. return shell_context;
  102. }
  103. void TestsShell::BeginFrame()
  104. {
  105. #ifdef RMLUI_TESTS_USE_SHELL
  106. Backend::BeginFrame();
  107. #endif
  108. }
  109. void TestsShell::PresentFrame()
  110. {
  111. #ifdef RMLUI_TESTS_USE_SHELL
  112. Backend::PresentFrame();
  113. #endif
  114. }
  115. void TestsShell::RenderLoop()
  116. {
  117. REQUIRE(shell_context);
  118. #ifdef RMLUI_TESTS_USE_SHELL
  119. bool running = true;
  120. while (running)
  121. {
  122. running = Backend::ProcessEvents(shell_context, &Shell::ProcessKeyDownShortcuts);
  123. shell_context->Update();
  124. BeginFrame();
  125. shell_context->Render();
  126. PresentFrame();
  127. }
  128. #else
  129. shell_context->Update();
  130. shell_context->Render();
  131. #endif
  132. }
  133. void TestsShell::ShutdownShell()
  134. {
  135. REQUIRE(shell_initialized);
  136. if (debugger_allowed)
  137. {
  138. RMLUI_ASSERTMSG(shell_context->GetNumDocuments() == num_documents_begin, "Make sure all previously opened documents have been closed.");
  139. (void)num_documents_begin;
  140. }
  141. tests_system_interface.SetNumExpectedWarnings(0);
  142. Rml::Shutdown();
  143. shell_render_interface.Reset();
  144. #ifdef RMLUI_TESTS_USE_SHELL
  145. Backend::Shutdown();
  146. #endif
  147. Shell::Shutdown();
  148. shell_context = nullptr;
  149. shell_initialized = false;
  150. }
  151. void TestsShell::SetNumExpectedWarnings(int num_warnings)
  152. {
  153. tests_system_interface.SetNumExpectedWarnings(num_warnings);
  154. }
  155. Rml::String TestsShell::GetRenderStats()
  156. {
  157. Rml::String result;
  158. #if !defined(RMLUI_TESTS_USE_SHELL)
  159. shell_context->Update();
  160. shell_render_interface.ResetCounters();
  161. shell_context->Render();
  162. auto& counters = shell_render_interface.GetCounters();
  163. result = Rml::CreateString("Context::Render() stats:\n"
  164. " Compile geometry: %zu\n"
  165. " Render geometry: %zu\n"
  166. " Release geometry: %zu\n"
  167. " Texture load: %zu\n"
  168. " Texture generate: %zu\n"
  169. " Texture release: %zu\n"
  170. " Scissor enable: %zu\n"
  171. " Scissor set: %zu\n"
  172. " Clip mask enable: %zu\n"
  173. " Clip mask render: %zu\n"
  174. " Transform set: %zu",
  175. counters.compile_geometry, counters.render_geometry, counters.release_geometry, counters.load_texture, counters.generate_texture,
  176. counters.release_texture, counters.enable_scissor, counters.set_scissor, counters.enable_clip_mask, counters.render_to_clip_mask,
  177. counters.set_transform);
  178. #endif
  179. return result;
  180. }
  181. TestsRenderInterface* TestsShell::GetTestsRenderInterface()
  182. {
  183. #if defined(RMLUI_TESTS_USE_SHELL)
  184. return nullptr;
  185. #else
  186. return &shell_render_interface;
  187. #endif
  188. }
  189. TestsSystemInterface* TestsShell::GetTestsSystemInterface()
  190. {
  191. return &tests_system_interface;
  192. }