Element.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #ifdef RMLUI_ENABLE_BENCHMARKS
  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/ElementDocument.h>
  34. #include <RmlUi/Core/Types.h>
  35. #include <RmlUi/Debugger.h>
  36. #include <Shell.h>
  37. #include <Input.h>
  38. #include <ShellRenderInterfaceOpenGL.h>
  39. #include <doctest.h>
  40. #include <nanobench.h>
  41. using namespace ankerl;
  42. using namespace Rml;
  43. static Rml::Context* context = nullptr;
  44. static ShellRenderInterfaceExtensions* shell_renderer;
  45. TEST_CASE("Element benchmark")
  46. {
  47. const Vector2i window_size(1024, 768);
  48. ShellRenderInterfaceOpenGL opengl_renderer;
  49. shell_renderer = &opengl_renderer;
  50. // Generic OS initialisation, creates a window and attaches OpenGL.
  51. REQUIRE(Shell::Initialise());
  52. REQUIRE(Shell::OpenWindow("Element benchmark", shell_renderer, window_size.x, window_size.y, true));
  53. // RmlUi initialisation.
  54. Rml::SetRenderInterface(&opengl_renderer);
  55. shell_renderer->SetViewport(window_size.x, window_size.y);
  56. ShellSystemInterface system_interface;
  57. Rml::SetSystemInterface(&system_interface);
  58. //TestsSystemInterface system_interface;
  59. //TestsRenderInterface render_interface;
  60. //SetRenderInterface(&render_interface);
  61. //SetSystemInterface(&system_interface);
  62. REQUIRE(Initialise());
  63. context = Rml::CreateContext("main", window_size);
  64. REQUIRE(context);
  65. Rml::Debugger::Initialise(context);
  66. ::Input::SetContext(context);
  67. shell_renderer->SetContext(context);
  68. Shell::LoadFonts("assets/");
  69. ElementDocument* document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  70. REQUIRE(document);
  71. document->Show();
  72. Rml::String rml;
  73. Element* el = document->GetElementById("performance");
  74. REQUIRE(el);
  75. nanobench::Bench bench;
  76. bench.title("Elements")
  77. .run("SetInnerRML", [&] {
  78. for (int i = 0; i < 50; i++)
  79. {
  80. int index = rand() % 1000;
  81. int route = rand() % 50;
  82. int max = (rand() % 40) + 10;
  83. int value = rand() % max;
  84. Rml::String rml_row = Rml::CreateString(1000, R"(
  85. <div class="row">
  86. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  87. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  88. <div class="col col4">Assigned</div>
  89. <select>
  90. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  91. </select>
  92. <div class="inrow unmark_collapse">
  93. <div class="col col123 assign_text">Assign to route</div>
  94. <div class="col col4">
  95. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  96. </div>
  97. </div>
  98. </div>)",
  99. index,
  100. route,
  101. max,
  102. value
  103. );
  104. rml += rml_row;
  105. }
  106. el->SetInnerRML(rml);
  107. });
  108. Rml::Shutdown();
  109. Shell::CloseWindow();
  110. }
  111. #endif