TestsShell.cpp 5.7 KB

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