Skip to content

Commit

Permalink
Prevent the GC from collecting "this" and "pixels" (#1258)
Browse files Browse the repository at this point in the history
* Prevent the GC from collection "this"
* GC.KeepAlive for cases where we only use pixels pointers
  • Loading branch information
mattleibow authored Apr 29, 2020
1 parent 6c2db31 commit 22fcbe7
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 15 deletions.
8 changes: 6 additions & 2 deletions binding/Binding/SKImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes,
public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX, int srcY, SKImageCachingHint cachingHint)
{
var cinfo = SKImageInfoNative.FromManaged (ref dstInfo);
return SkiaApi.sk_image_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY, cachingHint);
var result = SkiaApi.sk_image_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY, cachingHint);
GC.KeepAlive (this);
return result;
}

public bool ReadPixels (SKPixmap pixmap) =>
Expand All @@ -583,7 +585,9 @@ public bool ReadPixels (SKPixmap pixmap, int srcX, int srcY, SKImageCachingHint
if (pixmap == null)
throw new ArgumentNullException (nameof (pixmap));

return SkiaApi.sk_image_read_pixels_into_pixmap (Handle, pixmap.Handle, srcX, srcY, cachingHint);
var result = SkiaApi.sk_image_read_pixels_into_pixmap (Handle, pixmap.Handle, srcX, srcY, cachingHint);
GC.KeepAlive (this);
return result;
}

// ScalePixels
Expand Down
4 changes: 3 additions & 1 deletion binding/Binding/SKSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ public bool PeekPixels (SKPixmap pixmap)
public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes, int srcX, int srcY)
{
var cinfo = SKImageInfoNative.FromManaged (ref dstInfo);
return SkiaApi.sk_surface_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY);
var result = SkiaApi.sk_surface_read_pixels (Handle, &cinfo, (void*)dstPixels, (IntPtr)dstRowBytes, srcX, srcY);
GC.KeepAlive (this);
return result;
}

internal static SKSurface GetObject (IntPtr handle) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Android.Graphics;
using System;
using Android.Graphics;

namespace SkiaSharp.Views.Android
{
Expand Down Expand Up @@ -130,7 +131,9 @@ public static Bitmap ToBitmap(this SKBitmap skiaBitmap)
{
using (var pixmap = skiaBitmap.PeekPixels())
{
return pixmap.ToBitmap();
var bmp = pixmap.ToBitmap();
GC.KeepAlive(skiaBitmap);
return bmp;
}
}

Expand Down Expand Up @@ -183,7 +186,9 @@ public static Bitmap ToBitmap(this SKImage skiaImage)
{
using (var pixmap = skiaImage.PeekPixels())
{
return pixmap.ToBitmap();
var bmp = pixmap.ToBitmap();
GC.KeepAlive(skiaImage);
return bmp;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public static CGImage ToCGImage(this SKBitmap skiaBitmap)
provider,
null, false, CGColorRenderingIntent.Default);
}
GC.KeepAlive(skiaBitmap);
return cgImage;
}

Expand Down
9 changes: 7 additions & 2 deletions source/SkiaSharp.Views/SkiaSharp.Views.Gtk/GTKExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Gdk;

using GC = System.GC;

namespace SkiaSharp.Views.Gtk
{
public static class GTKExtensions
Expand Down Expand Up @@ -91,9 +93,12 @@ public static Pixbuf ToPixbuf(this SKImage skiaImage)

public static Pixbuf ToPixbuf(this SKBitmap skiaBitmap)
{
using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
using (var pixmap = skiaBitmap.PeekPixels())
using (var image = SKImage.FromPixels(pixmap))
{
return image.ToPixbuf();
var pixbuf = image.ToPixbuf();
GC.KeepAlive(skiaBitmap);
return pixbuf;
}
}

Expand Down
7 changes: 5 additions & 2 deletions source/SkiaSharp.Views/SkiaSharp.Views.Shared/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ public static System.Drawing.Bitmap ToBitmap(this SKImage skiaImage)

public static System.Drawing.Bitmap ToBitmap(this SKBitmap skiaBitmap)
{
using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
using (var pixmap = skiaBitmap.PeekPixels())
using (var image = SKImage.FromPixels(pixmap))
{
return image.ToBitmap();
var bmp = image.ToBitmap();
GC.KeepAlive(skiaBitmap);
return bmp;
}
}

Expand Down
7 changes: 5 additions & 2 deletions source/SkiaSharp.Views/SkiaSharp.Views.UWP/UWPExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ public static WriteableBitmap ToWriteableBitmap(this SKImage skiaImage)

public static WriteableBitmap ToWriteableBitmap(this SKBitmap skiaBitmap)
{
using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
using (var pixmap = skiaBitmap.PeekPixels())
using (var image = SKImage.FromPixels(pixmap))
{
return image.ToWriteableBitmap();
var wb = image.ToWriteableBitmap();
GC.KeepAlive(skiaBitmap);
return wb;
}
}

Expand Down
10 changes: 7 additions & 3 deletions source/SkiaSharp.Views/SkiaSharp.Views.WPF/WPFExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

Expand Down Expand Up @@ -85,9 +86,12 @@ public static WriteableBitmap ToWriteableBitmap(this SKImage skiaImage)

public static WriteableBitmap ToWriteableBitmap(this SKBitmap skiaBitmap)
{
using (var image = SKImage.FromPixels(skiaBitmap.PeekPixels()))
using (var pixmap = skiaBitmap.PeekPixels())
using (var image = SKImage.FromPixels(pixmap))
{
return image.ToWriteableBitmap();
var wb = image.ToWriteableBitmap();
GC.KeepAlive(skiaBitmap);
return wb;
}
}

Expand Down

0 comments on commit 22fcbe7

Please sign in to comment.