TestsShell.cpp 5.5 KB

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