Skip to content

Commit abd58ca

Browse files
committed
Cache images rather than bitmaps
1 parent 1b7289d commit abd58ca

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
namespace WinDynamicDesktop.Skia
1414
{
15-
sealed class BitmapCache
15+
sealed class ImageCache
1616
{
1717
readonly int maxWidth;
1818
readonly int maxHeight;
1919
readonly object cacheLock = new object();
20-
readonly Dictionary<Uri, SKBitmap> images = new Dictionary<Uri, SKBitmap>();
20+
readonly Dictionary<Uri, SKImage> images = new Dictionary<Uri, SKImage>();
2121

22-
public SKBitmap this[Uri uri]
22+
public SKImage this[Uri uri]
2323
{
2424
get
2525
{
@@ -46,16 +46,16 @@ public void Clear()
4646
{
4747
lock (cacheLock)
4848
{
49-
foreach (var bitmap in images.Values)
49+
foreach (var image in images.Values)
5050
{
51-
bitmap?.Dispose();
51+
image?.Dispose();
5252
}
5353
images.Clear();
5454
}
5555
GC.Collect();
5656
}
5757

58-
public BitmapCache(bool limitDecodeSize = true)
58+
public ImageCache(bool limitDecodeSize = true)
5959
{
6060
if (limitDecodeSize)
6161
{
@@ -78,7 +78,7 @@ public BitmapCache(bool limitDecodeSize = true)
7878
}
7979
}
8080

81-
private SKBitmap CreateImage(Uri uri)
81+
private SKImage CreateImage(Uri uri)
8282
{
8383
try
8484
{
@@ -114,7 +114,7 @@ private SKBitmap CreateImage(Uri uri)
114114

115115
var info = codec.Info;
116116

117-
// Calculate scaled dimensions
117+
// Calculate target dimensions
118118
int targetWidth = info.Width;
119119
int targetHeight = info.Height;
120120

@@ -125,24 +125,31 @@ private SKBitmap CreateImage(Uri uri)
125125
targetHeight = (int)(info.Height * scale);
126126
}
127127

128-
var bitmap = new SKBitmap(targetWidth, targetHeight, info.ColorType, info.AlphaType);
129-
130-
if (targetWidth == info.Width && targetHeight == info.Height)
131-
{
132-
// No scaling needed
133-
codec.GetPixels(bitmap.Info, bitmap.GetPixels());
134-
}
135-
else
128+
// Decode at native size
129+
using (var sourceBitmap = new SKBitmap(info))
136130
{
137-
// Decode at full size then scale down with high quality
138-
using (var fullBitmap = new SKBitmap(info))
131+
if (codec.GetPixels(sourceBitmap.Info, sourceBitmap.GetPixels()) != SKCodecResult.Success)
139132
{
140-
codec.GetPixels(fullBitmap.Info, fullBitmap.GetPixels());
141-
fullBitmap.ScalePixels(bitmap, new SKSamplingOptions(SKCubicResampler.Mitchell));
133+
return null;
134+
}
135+
136+
// If scaling is needed, create scaled version with high quality
137+
if (targetWidth != sourceBitmap.Width || targetHeight != sourceBitmap.Height)
138+
{
139+
using (var scaledBitmap = new SKBitmap(targetWidth, targetHeight, info.ColorType, info.AlphaType))
140+
{
141+
sourceBitmap.ScalePixels(scaledBitmap, new SKSamplingOptions(SKCubicResampler.Mitchell));
142+
var image = SKImage.FromBitmap(scaledBitmap);
143+
return image;
144+
}
145+
}
146+
else
147+
{
148+
// No scaling needed
149+
var image = SKImage.FromBitmap(sourceBitmap);
150+
return image;
142151
}
143152
}
144-
145-
return bitmap;
146153
}
147154
}
148155
}

src/Skia/ThemePreviewer.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,14 @@ protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
142142
}
143143
}
144144

145-
private void DrawImage(SKCanvas canvas, SKBitmap bitmap, SKImageInfo info, float opacity)
145+
private void DrawImage(SKCanvas canvas, SKImage image, SKImageInfo info, float opacity)
146146
{
147147
var destRect = new SKRect(0, 0, info.Width, info.Height);
148148

149149
if (opacity >= 1.0f)
150150
{
151151
// Fast path for fully opaque images
152-
using (var image = SKImage.FromBitmap(bitmap))
153-
{
154-
canvas.DrawImage(image, destRect, samplingOptions, null);
155-
}
152+
canvas.DrawImage(image, destRect, samplingOptions, null);
156153
}
157154
else
158155
{
@@ -164,10 +161,7 @@ private void DrawImage(SKCanvas canvas, SKBitmap bitmap, SKImageInfo info, float
164161
SKColors.White.WithAlpha((byte)(255 * opacity)),
165162
SKBlendMode.DstIn);
166163

167-
using (var image = SKImage.FromBitmap(bitmap))
168-
{
169-
canvas.DrawImage(image, destRect, samplingOptions, paint);
170-
}
164+
canvas.DrawImage(image, destRect, samplingOptions, paint);
171165
}
172166
}
173167
}

src/Skia/ThemePreviewerViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ public string DownloadSize
9797
set => SetProperty(ref downloadSize, value);
9898
}
9999

100-
private SKBitmap backImage;
101-
public SKBitmap BackImage
100+
private SKImage backImage;
101+
public SKImage BackImage
102102
{
103103
get => backImage;
104104
set => SetProperty(ref backImage, value);
105105
}
106106

107-
private SKBitmap frontImage;
108-
public SKBitmap FrontImage
107+
private SKImage frontImage;
108+
public SKImage FrontImage
109109
{
110110
get => frontImage;
111111
set => SetProperty(ref frontImage, value);
@@ -176,7 +176,7 @@ public void InvokeDownload()
176176

177177
private const int TRANSITION_TIME = 5;
178178

179-
private readonly BitmapCache cache = new BitmapCache();
179+
private readonly ImageCache cache = new ImageCache();
180180
private readonly System.Windows.Forms.Timer transitionTimer;
181181
private readonly ConcurrentQueue<int> fadeQueue = new ConcurrentQueue<int>();
182182
private readonly SemaphoreSlim fadeSemaphore = new SemaphoreSlim(1, 1);

0 commit comments

Comments
 (0)