Filter.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-2023 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 "../Common/TestsInterface.h"
  29. #include "../Common/TestsShell.h"
  30. #include <RmlUi/Core/CompiledFilterShader.h>
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/Element.h>
  33. #include <RmlUi/Core/ElementDocument.h>
  34. #include <RmlUi/Core/Factory.h>
  35. #include <RmlUi/Core/Filter.h>
  36. #include <RmlUi/Core/PropertyDefinition.h>
  37. #include <RmlUi/Core/PropertyDictionary.h>
  38. #include <RmlUi/Core/RenderManager.h>
  39. #include <algorithm>
  40. #include <doctest.h>
  41. using namespace Rml;
  42. class FilterTest;
  43. struct CompiledTestFilter {
  44. String element_id;
  45. const FilterTest* filter;
  46. };
  47. static Vector<CompiledTestFilter> compiled_test_filters;
  48. class FilterTest : public Filter {
  49. public:
  50. FilterTest(float value, Unit unit) : value(value), unit(unit) {}
  51. CompiledFilter CompileFilter(Element* element) const override
  52. {
  53. compiled_test_filters.push_back({element->GetId(), this});
  54. return element->GetRenderManager()->CompileFilter("FilterTest", {});
  55. }
  56. float value = 0.f;
  57. Unit unit = Unit::UNKNOWN;
  58. };
  59. class FilterTestInstancer : public FilterInstancer {
  60. public:
  61. enum class ValueType { NumberPercent, Angle };
  62. FilterTestInstancer()
  63. {
  64. id = RegisterProperty("value", "10cm").AddParser("length").GetId();
  65. RegisterShorthand("filter", "value", ShorthandType::FallThrough);
  66. }
  67. SharedPtr<Filter> InstanceFilter(const String& /*name*/, const PropertyDictionary& properties) override
  68. {
  69. const Property* p_value = properties.GetProperty(id);
  70. if (!p_value)
  71. return nullptr;
  72. CHECK(Any(p_value->unit & Unit::LENGTH));
  73. num_instances += 1;
  74. return MakeShared<FilterTest>(p_value->Get<float>(), p_value->unit);
  75. }
  76. int num_instances = 0;
  77. private:
  78. PropertyId id = {};
  79. };
  80. static const String document_filter_rml = R"(
  81. <rml>
  82. <head>
  83. <style>
  84. body {
  85. top: 100px;
  86. left: 200px;
  87. width: 800px;
  88. height: 600px;
  89. }
  90. div {
  91. display: block;
  92. width: 400px;
  93. height: 300px;
  94. }
  95. #a { filter: test(); }
  96. #b { filter: test(0); }
  97. #c,#d { filter: test(1dp); }
  98. </style>
  99. </head>
  100. <body>
  101. <div id="a"/>
  102. <div id="b"/>
  103. <div id="c"/>
  104. <div id="d"/>
  105. </body>
  106. </rml>
  107. )";
  108. TEST_CASE("filter")
  109. {
  110. compiled_test_filters.clear();
  111. Context* context = TestsShell::GetContext();
  112. REQUIRE(context);
  113. FilterTestInstancer instancer;
  114. Rml::Factory::RegisterFilterInstancer("test", &instancer);
  115. ElementDocument* document = context->LoadDocumentFromMemory(document_filter_rml);
  116. document->Show();
  117. TestsShell::RenderLoop();
  118. struct ExpectedCompiledFilter {
  119. float value;
  120. Unit unit;
  121. };
  122. // Map element ID to its expected filter value and unit.
  123. UnorderedMap<String, ExpectedCompiledFilter> expected_compiled_filters = {
  124. {"a", {10.f, Unit::CM}},
  125. {"b", {0.f, Unit::PX}},
  126. {"c", {1.f, Unit::DP}},
  127. {"d", {1.f, Unit::DP}},
  128. };
  129. for (const auto& compiled_filter : compiled_test_filters)
  130. {
  131. INFO("ID #", compiled_filter.element_id);
  132. auto it = expected_compiled_filters.find(compiled_filter.element_id);
  133. const bool id_found = (it != expected_compiled_filters.end());
  134. CHECK(id_found);
  135. if (!id_found)
  136. continue;
  137. const ExpectedCompiledFilter& expected = it->second;
  138. CHECK(compiled_filter.filter->value == expected.value);
  139. CHECK(compiled_filter.filter->unit == expected.unit);
  140. }
  141. // Check that filters are not compiled more than once for each element.
  142. CHECK(compiled_test_filters.size() == expected_compiled_filters.size());
  143. // Filters aren't cached like decorators are, so each element will instance a new decorator even if they refer to
  144. // the same style rule. Thus, here producing 4 instead of 3 unique instances.
  145. CHECK(instancer.num_instances == expected_compiled_filters.size());
  146. auto& counters = TestsShell::GetTestsRenderInterface()->GetCounters();
  147. CHECK(counters.compile_filter == expected_compiled_filters.size());
  148. CHECK(counters.release_filter == 0);
  149. document->Close();
  150. context->Update();
  151. CHECK(counters.compile_filter == expected_compiled_filters.size());
  152. CHECK(counters.release_filter == expected_compiled_filters.size());
  153. TestsShell::ShutdownShell();
  154. }