Browse Source

Performance improvement when dirtying vw and vh units

Michael Ragazzon 4 years ago
parent
commit
6935810e12

+ 8 - 0
Include/RmlUi/Core/Property.h

@@ -31,6 +31,7 @@
 
 #include "Variant.h"
 #include "Header.h"
+#include <type_traits>
 
 namespace Rml {
 
@@ -130,5 +131,12 @@ public:
 	SharedPtr<const PropertySource> source;
 };
 
+// OR operator for combining multiple units where applicable.
+inline Property::Unit operator|(Property::Unit lhs, Property::Unit rhs)
+{
+	using underlying_t = std::underlying_type<Property::Unit>::type;
+	return static_cast<Property::Unit>(static_cast<underlying_t>(lhs) | static_cast<underlying_t>(rhs));
+}
+
 } // namespace Rml
 #endif

+ 3 - 4
Source/Core/ElementDocument.cpp

@@ -484,13 +484,12 @@ bool ElementDocument::IsLayoutDirty()
 
 void ElementDocument::DirtyDpProperties()
 {
-	GetStyle()->DirtyPropertiesWithUnitRecursive(Property::DP);
+	GetStyle()->DirtyPropertiesWithUnitsRecursive(Property::DP);
 }
 
 void ElementDocument::DirtyVwAndVhProperties()
 {
-	GetStyle()->DirtyPropertiesWithUnitRecursive(Property::VW);
-	GetStyle()->DirtyPropertiesWithUnitRecursive(Property::VH);
+	GetStyle()->DirtyPropertiesWithUnitsRecursive(Property::VW | Property::VH);
 }
 
 // Repositions the document if necessary.
@@ -500,7 +499,7 @@ void ElementDocument::OnPropertyChange(const PropertyIdSet& changed_properties)
 
 	// If the document's font-size has been changed, we need to dirty all rem properties.
 	if (changed_properties.Contains(PropertyId::FontSize))
-		GetStyle()->DirtyPropertiesWithUnitRecursive(Property::REM);
+		GetStyle()->DirtyPropertiesWithUnitsRecursive(Property::REM);
 
 	if (changed_properties.Contains(PropertyId::Top) ||
 		changed_properties.Contains(PropertyId::Right) ||

+ 5 - 5
Source/Core/ElementStyle.cpp

@@ -459,22 +459,22 @@ void ElementStyle::DirtyChildDefinitions()
 		element->GetChild(i)->GetStyle()->DirtyDefinition();
 }
 
-void ElementStyle::DirtyPropertiesWithUnitRecursive(Property::Unit unit)
+void ElementStyle::DirtyPropertiesWithUnitsRecursive(Property::Unit units)
 {
-	// Dirty all the properties of this element that use the unit.
+	// Dirty all the properties of this element that use the unit(s).
 	for (auto it = Iterate(); !it.AtEnd(); ++it)
 	{
 		auto name_property_pair = *it;
 		PropertyId id = name_property_pair.first;
 		const Property& property = name_property_pair.second;
-		if (property.unit == unit)
+		if (property.unit & units)
 			DirtyProperty(id);
 	}
 
-	// Now dirty all of our descendant's properties that use the unit.
+	// Now dirty all of our descendant's properties that use the unit(s).
 	int num_children = element->GetNumChildren(true);
 	for (int i = 0; i < num_children; ++i)
-		element->GetChild(i)->GetStyle()->DirtyPropertiesWithUnitRecursive(unit);
+		element->GetChild(i)->GetStyle()->DirtyPropertiesWithUnitsRecursive(units);
 }
 
 bool ElementStyle::AnyPropertiesDirty() const 

+ 2 - 2
Source/Core/ElementStyle.h

@@ -122,8 +122,8 @@ public:
 	/// some operations may require to dirty these manually, such as when moving an element into another.
 	void DirtyInheritedProperties();
 
-	/// Dirties all properties with a given unit on the current element and recursively on all children.
-	void DirtyPropertiesWithUnitRecursive(Property::Unit unit);
+	/// Dirties all properties with any of the given units (OR-ed together) on the current element and recursively on all children.
+	void DirtyPropertiesWithUnitsRecursive(Property::Unit units);
 
 	/// Returns true if any properties are dirty such that computed values need to be recomputed
 	bool AnyPropertiesDirty() const;

+ 2 - 1
changelog.md

@@ -108,6 +108,7 @@ Use the RCSS `display` property to enable table formatting. See the style sheet
 - Implemented the `word-break` RCSS property.
 - Implemented the `box-sizing` RCSS property.
 - Implemented the `caret-color` RCSS property.
+- New RCSS length units: `vw` and `vh`. [#162](https://github.com/mikke89/RmlUi/pull/162) (thanks @Dakror).
 
 ### New RML elements
 
@@ -115,7 +116,7 @@ Use the RCSS `display` property to enable table formatting. See the style sheet
 
 ### Element improvements
 
-- Implemented `Element::QuerySelector`, `Element::QuerySelectorAll`, and `Element::Closest`.
+- Implemented `Element::QuerySelector`, `Element::QuerySelectorAll`, and `Element::Closest`. [#164](https://github.com/mikke89/RmlUi/pull/164) (thanks @Dakror).
 - The `tab-index: auto` property can now be set on the `<body>` element to enable tabbing back to the document.
 - `<select>` elements now react to changes in the `value` attribute.
 - Element attributes can now use escaped RML characters, eg. `<p example="&quot;Quoted text&quot;"/>`. [#154](https://github.com/mikke89/RmlUi/pull/154) (thanks @actboy168).