Browse Source

Cleanup element update

Michael Ragazzon 6 years ago
parent
commit
1f6f4eeab8

+ 1 - 3
Include/Rocket/Core/Element.h

@@ -580,7 +580,7 @@ public:
 	const ComputedValues& GetComputedValues() const;
 
 protected:
-	void Update();
+	void Update(float dp_ratio);
 	void Render();
 
 	/// Forces the element to generate a local stacking context, regardless of the value of its z-index
@@ -603,8 +603,6 @@ protected:
 	/// @param[in] changed_properties The properties changed on the element.
 	virtual void OnPropertyChange(const PropertyNameList& changed_properties);
 
-	void UpdateDirtyProperties(const DirtyPropertyList& dirty_properties);
-
 	/// Called when a child node has been added up to two levels below us in the hierarchy.
 	/// @param[in] child The element that has been added. This may be this element.
 	virtual void OnChildAdd(Element* child);

+ 3 - 0
Samples/basic/benchmark/src/main.cpp

@@ -99,6 +99,9 @@ public:
 		  Avoid duplicate ToLower++: 103.0  [dec4ef6]
 		  Cleanup and smaller changes: 105.0  [38a559d]
 		  Move dirty properties to elementstyle: 113.0  [0bba316]
+		  (After Windows feature update and MSVC update, no code change): 109.0  [0bba316]
+		  Fixes and element style iterators: 108.0  [0bba316]
+		  Update definition speedup: 115.0  [5d138fa]
 		  
 		*/
 

+ 2 - 2
Source/Core/Context.cpp

@@ -168,7 +168,7 @@ float Context::GetDensityIndependentPixelRatio() const
 // Updates all elements in the element tree.
 bool Context::Update()
 {
-	root->Update();
+	root->Update(density_independent_pixel_ratio);
 
 	for (int i = 0; i < root->GetNumChildren(); ++i)
 		if (auto doc = root->GetChild(i)->GetOwnerDocument())
@@ -200,7 +200,7 @@ bool Context::Render()
 	// Render the cursor proxy so any elements attached the cursor will be rendered below the cursor.
 	if (cursor_proxy != NULL)
 	{
-		cursor_proxy->Update();
+		cursor_proxy->Update(density_independent_pixel_ratio);
 		cursor_proxy->SetOffset(Vector2f((float)Math::Clamp(mouse_position.x, 0, dimensions.x),
 			(float)Math::Clamp(mouse_position.y, 0, dimensions.y)),
 			NULL);

+ 17 - 33
Source/Core/Element.cpp

@@ -202,7 +202,7 @@ Element::~Element()
 		instancer->RemoveReference();
 }
 
-void Element::Update()
+void Element::Update(float dp_ratio)
 {
 	ReleaseElements(deleted_children);
 	active_children = children;
@@ -219,25 +219,30 @@ void Element::Update()
 
 	if(style->AnyPropertiesDirty())
 	{
-		// @performance: Maybe pass the following as function arguments?
-		using namespace Style;
-		const ComputedValues* parent_values = (parent ? &parent->GetComputedValues() : nullptr);
-		float dp_ratio = 1.0f;
+		const ComputedValues* parent_values = nullptr;
 		const ComputedValues* document_values = nullptr;
+		if (parent)
+			parent_values = &parent->GetComputedValues();
 		if (auto doc = GetOwnerDocument())
-		{
 			document_values = &doc->GetComputedValues();
-			if (auto context = doc->GetContext())
-				dp_ratio = context->GetDensityIndependentPixelRatio();
-		}
+
+		// Compute values and clear dirty properties
 		auto dirty_properties = style->ComputeValues(element_meta->computed_values, parent_values, document_values, computed_values_are_default_initialized, dp_ratio);
 
 		computed_values_are_default_initialized = false;
 
-		// Computed values are calculated before OnPropertyChange in UpdateDirtyProperties, thus these can safely be used.
+		// Computed values are just calculated and can safely be used in OnPropertyChange.
 		// However, new properties set during this call will not be available until the next update loop.
 		// Enable ROCKET_DEBUG to get a warning when this happens.
-		UpdateDirtyProperties(dirty_properties);
+		if (dirty_properties.AllDirty())
+			OnPropertyChange(StyleSheetSpecification::GetRegisteredProperties());
+		else if(!dirty_properties.Empty())
+			OnPropertyChange(dirty_properties.GetList());
+
+#ifdef ROCKET_DEBUG
+		if (style->AnyPropertiesDirty())
+			Log::Message(Log::LT_WARNING, "One or more properties were set during OnPropertyChange, these will only be evaluated on the next update call and should be avoided.");
+#endif
 	}
 
 
@@ -250,7 +255,7 @@ void Element::Update()
 	UpdateTransformState();
 
 	for (size_t i = 0; i < active_children.size(); i++)
-		active_children[i]->Update();
+		active_children[i]->Update(dp_ratio);
 }
 
 void Element::Render()
@@ -1884,27 +1889,6 @@ void Element::OnPropertyChange(const PropertyNameList& changed_properties)
 	}
 }
 
-void Element::UpdateDirtyProperties(const DirtyPropertyList& dirty_properties)
-{
-	if (dirty_properties.Empty())
-		return;
-
-	if(dirty_properties.AllDirty())
-	{
-		OnPropertyChange(StyleSheetSpecification::GetRegisteredProperties());
-	}
-	else
-	{
-		OnPropertyChange(dirty_properties.GetList());
-	}
-
-	// TODO: Add the following check back
-//#ifdef ROCKET_DEBUG
-//	if (all_properties_dirty || !dirty_properties.empty())
-//		Log::Message(Log::LT_WARNING, "One or more properties were set during OnPropertyChange, these will only be evaluated on the next update call and should be avoided.");
-//#endif
-}
-
 // Called when a child node has been added somewhere in the hierarchy
 void Element::OnChildAdd(Element* child)
 {

+ 2 - 1
Source/Core/ElementDocument.cpp

@@ -313,7 +313,8 @@ void ElementDocument::LoadScript(Stream* ROCKET_UNUSED_PARAMETER(stream), const
 // Updates the document, including its layout
 void ElementDocument::UpdateDocument()
 {
-	Element::Update();
+	const float dp_ratio = (context ? context->GetDensityIndependentPixelRatio() : 1.0f);
+	Update(dp_ratio);
 	UpdateLayout();
 	UpdatePosition();
 }