Skip to content

Commit 91ec143

Browse files
committed
fix(imaging): Don't cache IgnoreImageCache loads
`IgnoreImageCache` skipped the lookup in the static `_bitmapImageCache` but still added its load task to it, so the decoded image was retained by the cache exactly as if the option were not set. Gate the add (and the cancellation-removal registration) on the option too, so the image lives only as long as the `BitmapImage` that produced it and never replaces an entry other consumers already share. Fixes unoplatform#25098
1 parent 06d268c commit 91ec143

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#if __SKIA__
2+
#nullable enable
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.UI.Xaml.Controls;
7+
using Microsoft.UI.Xaml.Media.Imaging;
8+
using Uno.UI.RuntimeTests.Helpers;
9+
using static Private.Infrastructure.TestServices;
10+
11+
namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Imaging;
12+
13+
[TestClass]
14+
[RunsOnUIThread]
15+
public class Given_BitmapImage
16+
{
17+
private const string AssetUri = "ms-appx:///Uno.UI.RuntimeTests/Assets/Transitive-ingredient01.png";
18+
19+
[TestMethod]
20+
[GitHubWorkItem("https://github.com/unoplatform/uno/issues/25098")]
21+
public async Task When_IgnoreImageCache_Then_Not_Added_To_Cache()
22+
{
23+
// Distinct decode widths give each load its own cache key, independent of other tests.
24+
const int ignoredWidth = 41;
25+
const int cachedWidth = 42;
26+
27+
var uri = new Uri(AssetUri);
28+
var panel = new StackPanel();
29+
var cacheWasEnabled = FeatureConfiguration.Image.EnableBitmapImageCache;
30+
31+
try
32+
{
33+
FeatureConfiguration.Image.EnableBitmapImageCache = true;
34+
35+
await UITestHelper.Load(panel, x => x.IsLoaded);
36+
37+
await LoadAsync(panel, uri, ignoredWidth, BitmapCreateOptions.IgnoreImageCache);
38+
Assert.IsNull(await BitmapImage.GetCachedImageDataTaskForTesting(uri, ignoredWidth, null));
39+
40+
await LoadAsync(panel, uri, cachedWidth, BitmapCreateOptions.None);
41+
var cachedTask = await BitmapImage.GetCachedImageDataTaskForTesting(uri, cachedWidth, null);
42+
Assert.IsNotNull(cachedTask);
43+
44+
// Ignoring the cache must not replace an entry other consumers already share.
45+
await LoadAsync(panel, uri, cachedWidth, BitmapCreateOptions.IgnoreImageCache);
46+
Assert.AreSame(cachedTask, await BitmapImage.GetCachedImageDataTaskForTesting(uri, cachedWidth, null));
47+
}
48+
finally
49+
{
50+
WindowHelper.WindowContent = null;
51+
FeatureConfiguration.Image.EnableBitmapImageCache = cacheWasEnabled;
52+
}
53+
}
54+
55+
private static async Task LoadAsync(Panel panel, Uri uri, int decodePixelWidth, BitmapCreateOptions options)
56+
{
57+
var tcs = new TaskCompletionSource<bool>();
58+
var bitmapImage = new BitmapImage { CreateOptions = options, DecodePixelWidth = decodePixelWidth };
59+
bitmapImage.ImageOpened += (_, _) => tcs.TrySetResult(true);
60+
bitmapImage.ImageFailed += (_, e) => tcs.TrySetException(new Exception($"Failed to load {uri}: {e.ErrorMessage}"));
61+
bitmapImage.UriSource = uri;
62+
63+
panel.Children.Add(new Image { Width = 50, Height = 50, Source = bitmapImage });
64+
65+
await tcs.Task.WaitAsync(TimeSpan.FromSeconds(10));
66+
}
67+
}
68+
#endif

‎src/Uno.UI/UI/Xaml/Media/Imaging/BitmapImage.skia.cs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public sealed partial class BitmapImage : BitmapSource
1717
private readonly record struct BitmapImageCacheKey(Uri Uri, int? DecodeWidth, int? DecodeHeight);
1818

1919
private static readonly LRUCache<BitmapImageCacheKey, Task<ImageData>> _bitmapImageCache = new(FeatureConfiguration.Image.MaxBitmapImageCacheCount);
20+
21+
// Test hook: resolves the uri like the loader does so tests can observe cache membership for a key.
22+
internal static async Task<Task<ImageData>> GetCachedImageDataTaskForTesting(Uri uri, int? decodeWidth, int? decodeHeight)
23+
{
24+
uri = await TryResolveLocalResource(uri);
25+
return _bitmapImageCache.TryGetValue(new BitmapImageCacheKey(uri, decodeWidth, decodeHeight), out var imageDataTask) ? imageDataTask : null;
26+
}
27+
2028
// TODO: Introduce LRU caching if needed
2129
private static readonly Dictionary<string, string> _scaledBitmapPathCache = new();
2230

@@ -141,7 +149,9 @@ private async Task<ImageData> TryOpenSourceAsync(CancellationToken ct)
141149
}
142150
}, ct);
143151

144-
if (FeatureConfiguration.Image.EnableBitmapImageCache)
152+
// IgnoreImageCache must also skip the add: otherwise the decoded image is
153+
// retained by the cache exactly as if the option were not set.
154+
if (!ignoreCache && FeatureConfiguration.Image.EnableBitmapImageCache)
145155
{
146156
_bitmapImageCache.Add(cacheKey, imageDataTask);
147157
// if loading failed not because of an actual failure but because

0 commit comments

Comments
 (0)