Skip to content

Commit 124f124

Browse files
authored
Merge pull request #12146 from MrJul/fixes/composition-resources-invalidation
Fix composition render resources invalidation
2 parents 868cb1e + e0f9052 commit 124f124

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

src/Avalonia.Base/Rendering/Composition/Drawing/ServerCompositionRenderData.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,31 @@ protected override void DeserializeChangesCore(BatchStreamReader reader, TimeSpa
4242
_items.Add(reader.ReadObject<IRenderDataItem>());
4343

4444
var collector = s_resourceHashSetPool.Get();
45-
foreach(var item in _items)
46-
if (item is IRenderDataItemWithServerResources resourceItem)
47-
resourceItem.Collect(collector);
48-
45+
CollectResources(_items, collector);
46+
4947
foreach (var r in collector.Resources)
5048
{
5149
_referencedResources.Add(r);
5250
r.AddObserver(this);
5351
}
54-
52+
5553
collector.Resources.Clear();
5654
s_resourceHashSetPool.ReturnAndSetNull(ref collector);
5755

5856
base.DeserializeChangesCore(reader, committedAt);
5957
}
6058

59+
private static void CollectResources(PooledInlineList<IRenderDataItem> items, IRenderDataServerResourcesCollector collector)
60+
{
61+
foreach (var item in items)
62+
{
63+
if (item is IRenderDataItemWithServerResources resourceItem)
64+
resourceItem.Collect(collector);
65+
else if (item is RenderDataPushNode pushNode)
66+
CollectResources(pushNode.Children, collector);
67+
}
68+
}
69+
6170
public Rect? Bounds
6271
{
6372
get
@@ -133,4 +142,4 @@ public override void Dispose()
133142
Reset();
134143
base.Dispose();
135144
}
136-
}
145+
}

src/Avalonia.Base/Utilities/SmallDictionary.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void SetCore(TKey key, TValue value, bool overwrite)
5151
throw new ArgumentException("Key already exists in dictionary");
5252
}
5353

54-
if (arr[c].Key == null)
54+
if (arr[c].Key == null && free == -1)
5555
free = c;
5656
}
5757

@@ -337,11 +337,15 @@ public bool MoveNext()
337337
}
338338
else if (_type == Type.Array)
339339
{
340-
var next = _index + 1;
341-
if (_arr!.Length - 1 < next || _arr[next].Key == null)
342-
return false;
343-
_index = next;
344-
return true;
340+
for (var next = _index + 1; next < _arr!.Length; ++next)
341+
{
342+
if (_arr[next].Key != null)
343+
{
344+
_index = next;
345+
return true;
346+
}
347+
}
348+
return false;
345349
}
346350
else if (_type == Type.Dictionary)
347351
return _inner.MoveNext();
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#nullable enable
2+
3+
using System.Collections.Generic;
4+
using Avalonia.Utilities;
5+
using Xunit;
6+
7+
namespace Avalonia.Base.UnitTests.Utilities;
8+
9+
public class InlineDictionaryTests
10+
{
11+
[Fact]
12+
public void Enumeration_After_Add_With_Internal_Array_Works()
13+
{
14+
var dic = new InlineDictionary<string, int>();
15+
dic.Add("foo", 1);
16+
dic.Add("bar", 2);
17+
dic.Add("baz", 3);
18+
19+
Assert.Equal(
20+
new[] {
21+
new KeyValuePair<string, int>("foo", 1),
22+
new KeyValuePair<string, int>("bar", 2),
23+
new KeyValuePair<string, int>("baz", 3)
24+
},
25+
dic);
26+
}
27+
28+
[Fact]
29+
public void Enumeration_After_Remove_With_Internal_Array_Works()
30+
{
31+
var dic = new InlineDictionary<string, int>();
32+
dic.Add("foo", 1);
33+
dic.Add("bar", 2);
34+
dic.Add("baz", 3);
35+
36+
Assert.Equal(
37+
new[] {
38+
new KeyValuePair<string, int>("foo", 1),
39+
new KeyValuePair<string, int>("bar", 2),
40+
new KeyValuePair<string, int>("baz", 3)
41+
},
42+
dic);
43+
44+
dic.Remove("bar");
45+
46+
Assert.Equal(
47+
new[] {
48+
new KeyValuePair<string, int>("foo", 1),
49+
new KeyValuePair<string, int>("baz", 3)
50+
},
51+
dic);
52+
}
53+
}

0 commit comments

Comments
 (0)