FontTextureAtlas.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "../Common/TestsShell.h"
  2. #include <RmlUi/Core/Context.h>
  3. #include <RmlUi/Core/Core.h>
  4. #include <RmlUi/Core/Element.h>
  5. #include <RmlUi/Core/ElementDocument.h>
  6. #include <RmlUi/Core/Profiling.h>
  7. #include <RmlUi/Core/Types.h>
  8. #include <doctest.h>
  9. #include <nanobench.h>
  10. using namespace ankerl;
  11. using namespace Rml;
  12. // Start of the "CJK unified ideographs" Unicode block.
  13. static constexpr int rml_font_texture_atlas_start_codepoint = 0x4E00;
  14. static const String rml_font_texture_atlas_document = R"(
  15. <rml>
  16. <head>
  17. <title>Font texture atlas benchmark</title>
  18. <link type="text/rcss" href="/../Tests/Data/style.rcss"/>
  19. <style>
  20. body {
  21. font-family: "Noto Sans JP";
  22. font-size: %dpx;
  23. }
  24. </style>
  25. </head>
  26. <body id="body">
  27. </body>
  28. </rml>
  29. )";
  30. TEST_CASE("font_texture_atlas")
  31. {
  32. Context* context = TestsShell::GetContext();
  33. REQUIRE(context);
  34. LoadFontFace("assets/NotoSansJP-Regular.ttf");
  35. nanobench::Bench bench;
  36. bool incremental = false;
  37. // SUBCASE("all at once")
  38. //{
  39. // incremental = false;
  40. // bench.title("Font texture atlas (all at once)");
  41. // }
  42. // SUBCASE("incremental")
  43. //{
  44. incremental = true;
  45. bench.title("Font texture atlas (incremental)");
  46. //}
  47. bench.relative(true);
  48. bench.epochIterations(50);
  49. for (const int font_size : {12})
  50. {
  51. const String rml_document = CreateString(rml_font_texture_atlas_document.c_str(), font_size);
  52. ElementDocument* const document = context->LoadDocumentFromMemory(rml_document);
  53. REQUIRE(document);
  54. Element* const body = document->GetElementById("body");
  55. REQUIRE(body);
  56. document->Show();
  57. context->Update();
  58. context->Render();
  59. for (const int glyph_count : {10})
  60. {
  61. const String benchmark_name = CreateString("Size %d with %d glyphs", font_size, glyph_count);
  62. if (incremental)
  63. {
  64. bench.run(benchmark_name, [&]() {
  65. RMLUI_ZoneScopedN("Font texture atlas incremental");
  66. {
  67. RMLUI_ZoneScopedN("ReleaseFontResources");
  68. ReleaseFontResources();
  69. }
  70. {
  71. RMLUI_ZoneScopedN("Change RML");
  72. std::string inner_rml;
  73. for (int i = 0; i < glyph_count; ++i)
  74. {
  75. inner_rml += StringUtilities::ToUTF8(static_cast<Character>(rml_font_texture_atlas_start_codepoint + i));
  76. body->SetInnerRML(inner_rml);
  77. }
  78. context->Update();
  79. context->Render();
  80. }
  81. });
  82. }
  83. else
  84. {
  85. bench.run(benchmark_name, [&]() {
  86. ReleaseFontResources();
  87. for (int i = 0; i < glyph_count; ++i)
  88. {
  89. body->SetInnerRML(StringUtilities::ToUTF8(static_cast<Character>(rml_font_texture_atlas_start_codepoint + i)));
  90. context->Update();
  91. context->Render();
  92. }
  93. });
  94. }
  95. }
  96. document->Close();
  97. }
  98. TestsShell::ShutdownShell();
  99. }