TestsShell.cpp 5.7 KB

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