Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle reparenting of controls #3004

Merged
merged 3 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ private void UpdateScene()
foreach (var visual in _recalculateChildren)
{
var node = scene.FindNode(visual);
((VisualNode)node)?.SortChildren(scene);
((VisualNode)node)?.UpdateChildren(scene);
}

_recalculateChildren.Clear();
Expand Down
15 changes: 12 additions & 3 deletions src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public void ReplaceDrawOperation(int index, IRef<IDrawOperation> operation)

/// <summary>
/// Sorts the <see cref="Children"/> collection according to the order of the visual's
/// children and their z-index.
/// children and their z-index and removes controls that are no longer children.
/// </summary>
/// <param name="scene">The scene that the node is a part of.</param>
public void SortChildren(Scene scene)
public void UpdateChildren(Scene scene)
{
if (_children == null || _children.Count <= 1)
if (_children == null || _children.Count == 0)
{
return;
}
Expand All @@ -193,9 +193,12 @@ public void SortChildren(Scene scene)
keys.Add(((long)zIndex << 32) + i);
}

var toRemove = _children.ToList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to use a pooled list instance to reduce allocations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I figured that there's already so many allocations in the renderer that one more won't make much difference, and we can address when we address the rest of them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allocating huge arrays (which List internally does) can have a huge impact since they can end up in LOH.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH it would require to have around 20k children to end up on LOH. Although we already have renderer objects in LOH (dirty visuals). We also reallocate all nodes, children, etc. per render. Maybe we should create new task for tracking potential renderer refactor.


keys.Sort();
_children.Clear();


foreach (var i in keys)
{
var child = Visual.VisualChildren[(int)(i & 0xffffffff)];
Expand All @@ -204,8 +207,14 @@ public void SortChildren(Scene scene)
if (node != null)
{
_children.Add(node);
toRemove.Remove(node);
}
}

foreach (var node in toRemove)
{
scene.Remove(node);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,56 @@ public void Should_Update_VisualNode_Order_On_ZIndex_Change_With_Dirty_Ancestor(
Assert.Same(stackNode.Children[1].Visual, canvas1);
}

[Fact]
public void Should_Update_VisualNodes_When_Child_Moved_To_New_Parent()
{
var dispatcher = new ImmediateDispatcher();
var loop = new Mock<IRenderLoop>();

Decorator moveFrom;
Decorator moveTo;
Canvas moveMe;
var root = new TestRoot
{
Child = new StackPanel
{
Children =
{
(moveFrom = new Decorator
{
Child = moveMe = new Canvas(),
}),
(moveTo = new Decorator()),
}
}
};

var sceneBuilder = new SceneBuilder();
var target = new DeferredRenderer(
root,
loop.Object,
sceneBuilder: sceneBuilder,
dispatcher: dispatcher);

root.Renderer = target;
target.Start();
RunFrame(target);

moveFrom.Child = null;
moveTo.Child = moveMe;

RunFrame(target);

var scene = target.UnitTestScene();
var moveFromNode = (VisualNode)scene.FindNode(moveFrom);
var moveToNode = (VisualNode)scene.FindNode(moveTo);

Assert.Empty(moveFromNode.Children);
Assert.Equal(1, moveToNode.Children.Count);
Assert.Same(moveMe, moveToNode.Children[0].Visual);

}

[Fact]
public void Should_Push_Opacity_For_Controls_With_Less_Than_1_Opacity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,64 @@ public void Should_Update_When_Control_Removed()
}
}

[Fact]
public void Should_Update_When_Control_Moved()
{
using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
{
Decorator moveFrom;
Decorator moveTo;
Canvas moveMe;
var tree = new TestRoot
{
Width = 100,
Height = 100,
Child = new StackPanel
{
Children =
{
(moveFrom = new Decorator
{
Child = moveMe = new Canvas(),
}),
(moveTo = new Decorator()),
}
}
};

tree.Measure(Size.Infinity);
tree.Arrange(new Rect(tree.DesiredSize));

var scene = new Scene(tree);
var sceneBuilder = new SceneBuilder();
sceneBuilder.UpdateAll(scene);

var moveFromNode = (VisualNode)scene.FindNode(moveFrom);
var moveToNode = (VisualNode)scene.FindNode(moveTo);

Assert.Equal(1, moveFromNode.Children.Count);
Assert.Same(moveMe, moveFromNode.Children[0].Visual);
Assert.Empty(moveToNode.Children);

moveFrom.Child = null;
moveTo.Child = moveMe;

scene = scene.CloneScene();
moveFromNode = (VisualNode)scene.FindNode(moveFrom);
moveToNode = (VisualNode)scene.FindNode(moveTo);

moveFromNode.UpdateChildren(scene);
moveToNode.UpdateChildren(scene);
sceneBuilder.Update(scene, moveFrom);
sceneBuilder.Update(scene, moveTo);
sceneBuilder.Update(scene, moveMe);

Assert.Empty(moveFromNode.Children);
Assert.Equal(1, moveToNode.Children.Count);
Assert.Same(moveMe, moveToNode.Children[0].Visual);
}
}

[Fact]
public void Should_Update_When_Control_Made_Invisible()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void SortChildren_Does_Not_Throw_On_Null_Children()
var node = new VisualNode(Mock.Of<IVisual>(), null);
var scene = new Scene(Mock.Of<IVisual>());

node.SortChildren(scene);
node.UpdateChildren(scene);
}
}
}