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

Clear scene for invisible root visuals. #1106

Merged
merged 3 commits into from
Aug 20, 2017
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
42 changes: 24 additions & 18 deletions src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using Avalonia.Media.Immutable;
using System.Threading;
using System.Linq;

namespace Avalonia.Rendering
{
Expand Down Expand Up @@ -58,7 +59,6 @@ public DeferredRenderer(
_dispatcher = dispatcher ?? Dispatcher.UIThread;
_root = root;
_sceneBuilder = sceneBuilder ?? new SceneBuilder();
_scene = new Scene(root);
_layerFactory = layerFactory ?? new DefaultRenderLayerFactory();
_layers = new RenderLayers(_layerFactory);
_renderLoop = renderLoop;
Expand Down Expand Up @@ -86,7 +86,6 @@ public DeferredRenderer(
_root = root;
_renderTarget = renderTarget;
_sceneBuilder = sceneBuilder ?? new SceneBuilder();
_scene = new Scene(root);
_layerFactory = layerFactory ?? new DefaultRenderLayerFactory();
_layers = new RenderLayers(_layerFactory);
}
Expand Down Expand Up @@ -122,7 +121,7 @@ public IEnumerable<IVisual> HitTest(Point p, Func<IVisual, bool> filter)
UpdateScene();
}

return _scene.HitTest(p, filter);
return _scene?.HitTest(p, filter) ?? Enumerable.Empty<IVisual>();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -186,7 +185,7 @@ private void Render(Scene scene)
_dirtyRectsDisplay.Tick();
}

if (scene.Size != Size.Empty)
if (scene != null && scene.Size != Size.Empty)
{
if (scene.Generation != _lastSceneId)
{
Expand Down Expand Up @@ -366,25 +365,32 @@ private void UpdateScene()

try
{
var scene = _scene.Clone();

if (_dirty == null)
{
_dirty = new DirtyVisuals();
_sceneBuilder.UpdateAll(scene);
}
else if (_dirty.Count > 0)
if (_root.IsVisible)
{
foreach (var visual in _dirty)
var scene = _scene?.Clone() ?? new Scene(_root);

if (_dirty == null)
{
_sceneBuilder.Update(scene, visual);
_dirty = new DirtyVisuals();
_sceneBuilder.UpdateAll(scene);
}
else if (_dirty.Count > 0)
{
foreach (var visual in _dirty)
{
_sceneBuilder.Update(scene, visual);
}
}
}

Interlocked.Exchange(ref _scene, scene);
Interlocked.Exchange(ref _scene, scene);

_dirty.Clear();
(_root as IRenderRoot)?.Invalidate(new Rect(scene.Size));
_dirty.Clear();
(_root as IRenderRoot)?.Invalidate(new Rect(scene.Size));
}
else
{
Interlocked.Exchange(ref _scene, null);
}
}
finally
{
Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ public bool Update(Scene scene, IVisual visual)
{
Contract.Requires<ArgumentNullException>(scene != null);
Contract.Requires<ArgumentNullException>(visual != null);

Dispatcher.UIThread.VerifyAccess();

if (!scene.Root.Visual.IsVisible)
{
throw new AvaloniaInternalException("Cannot update the scene for an invisible root visual.");
}

var node = (VisualNode)scene.FindNode(visual);

if (visual == scene.Root.Visual)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,5 @@ public void GeometryClip_Should_Affect_Child_Layers()
((MockStreamGeometryImpl)borderLayer.GeometryClip).Transform);
}
}

[Fact]
public void Hiding_Root_Should_Not_Remove_Root_Layer()
{
using (TestApplication())
{
Border border;
var tree = new TestRoot
{
Child = border = new Border()
};

var layout = AvaloniaLocator.Current.GetService<ILayoutManager>();
layout.ExecuteInitialLayoutPass(tree);

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

tree.IsVisible = false;

scene = scene.Clone();
sceneBuilder.Update(scene, tree);

Assert.Equal(1, scene.Layers.Count);
}
}
}
}