TestsShell.cpp 7.1 KB

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