Skip to content

Commit

Permalink
Correctly reference the SKImageInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Apr 18, 2020
1 parent beab871 commit 1590d7e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion externals/skia
Submodule skia updated 1 files
+1 −1 src/c/sk_types_priv.h
11 changes: 11 additions & 0 deletions tests/Tests/SKCodecTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public void MinBufferedBytesNeededHasAValue ()
Assert.True (SKCodec.MinBufferedBytesNeeded > 0);
}

[SkippableFact]
public unsafe void StreamLosesOwnershipTddoCodecButIsNotForgotten()
{
var codec = SKCodec.Create(Path.Combine(PathToImages, "color-wheel.png"));

for (var i = 0; i < 1000; ++i)
{
Assert.Equal(SKCodecResult.Success, codec.GetPixels(out _));
}
}

[SkippableFact]
public unsafe void ReleaseDataWasInvokedOnlyAfterTheCodecWasFinished()
{
Expand Down
55 changes: 40 additions & 15 deletions tests/Tests/SKColorSpaceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,31 @@ public void ImageInfoColorSpaceIsReferencedCorrectly()

CollectGarbage();

Assert.Equal(1, colorspaceHandle.GetReferenceCount(false));
Assert.Equal(2, colorspaceHandle.GetReferenceCount(false));

Check();

CollectGarbage();

Assert.Equal(1, colorspaceHandle.GetReferenceCount(false));
Assert.Equal(2, colorspaceHandle.GetReferenceCount(false));

GC.KeepAlive(img);

void Check()
{
var peek = img.PeekPixels();
Assert.Equal(2, colorspaceHandle.GetReferenceCount(false));
Assert.Equal(3, colorspaceHandle.GetReferenceCount(false));

// get the info and color space
var info1 = peek.Info;
var cs1 = info1.ColorSpace;
Assert.Equal(3, colorspaceHandle.GetReferenceCount(false));
Assert.Equal(4, colorspaceHandle.GetReferenceCount(false));
Assert.NotNull(cs1);

// get the info and color space again and make sure we are all using the same things
var info2 = peek.Info;
var cs2 = info2.ColorSpace;
Assert.Equal(3, colorspaceHandle.GetReferenceCount(false));
Assert.Equal(4, colorspaceHandle.GetReferenceCount(false));
Assert.NotNull(cs2);

Assert.Same(cs1, cs2);
Expand All @@ -97,12 +97,37 @@ SKImage DoWork(out IntPtr handle)
Assert.Equal(1, handle.GetReferenceCount(false));

var image = SKImage.Create(info);
Assert.Equal(2, handle.GetReferenceCount(false));
Assert.Equal(3, handle.GetReferenceCount(false));

return image;
}
}

[SkippableFact]
public unsafe void ReferencesCountedCorrectly()
{
var colorspace = SKColorSpace.CreateRgb(
new SKColorSpaceTransferFn { A = 0.1f, B = 0.2f, C = 0.3f, D = 0.4f, E = 0.5f, F = 0.6f },
SKMatrix44.CreateIdentity());
var handle = colorspace.Handle;
Assert.Equal(1, handle.GetReferenceCount(false));

var info = new SKImageInfo(1, 1, SKImageInfo.PlatformColorType, SKAlphaType.Premul, colorspace);
Assert.Equal(1, handle.GetReferenceCount(false));

var pixels = new byte[info.BytesSize];
fixed (byte* p = pixels)
{
var pixmap = new SKPixmap(info, (IntPtr)p);
Assert.Equal(2, handle.GetReferenceCount(false));

pixmap.Dispose();
Assert.Equal(1, handle.GetReferenceCount(false));
}

GC.KeepAlive(colorspace);
}

[SkippableFact]
public void ColorSpaceIsNotDisposedPrematurely()
{
Expand All @@ -112,30 +137,30 @@ public void ColorSpaceIsNotDisposedPrematurely()

CheckBeforeCollection(colorSpaceHandle);

CheckExistingImage(3, img, colorSpaceHandle);
CheckExistingImage(4, img, colorSpaceHandle);

CollectGarbage();

Assert.Null(weakColorspace.Target);
Assert.Equal(1, colorSpaceHandle.GetReferenceCount(false));
Assert.Equal(2, colorSpaceHandle.GetReferenceCount(false));

CheckExistingImage(2, img, colorSpaceHandle);
CheckExistingImage(3, img, colorSpaceHandle);

CollectGarbage();

Assert.Null(weakColorspace.Target);
Assert.Equal(1, colorSpaceHandle.GetReferenceCount(false));
Assert.Equal(2, colorSpaceHandle.GetReferenceCount(false));

CollectGarbage();

Assert.Equal(1, colorSpaceHandle.GetReferenceCount(false));
Assert.Equal(2, colorSpaceHandle.GetReferenceCount(false));

GC.KeepAlive(img);

void CheckBeforeCollection(IntPtr csh)
{
Assert.NotNull(weakColorspace.Target);
Assert.Equal(2, csh.GetReferenceCount(false));
Assert.Equal(3, csh.GetReferenceCount(false));
}

void CheckExistingImage(int expected, SKImage image, IntPtr csh)
Expand All @@ -144,10 +169,10 @@ void CheckExistingImage(int expected, SKImage image, IntPtr csh)
Assert.Equal(expected, csh.GetReferenceCount(false));

var info = peek.Info;
Assert.Equal(3, csh.GetReferenceCount(false));
Assert.Equal(4, csh.GetReferenceCount(false));

var cs = info.ColorSpace;
Assert.Equal(3, csh.GetReferenceCount(false));
Assert.Equal(4, csh.GetReferenceCount(false));
Assert.NotNull(cs);
}

Expand All @@ -170,7 +195,7 @@ SKImage DoWork(out IntPtr handle, out WeakReference weak)

var image = SKImage.Create(info);

Assert.Equal(2, handle.GetReferenceCount(false));
Assert.Equal(3, handle.GetReferenceCount(false));

return image;
}
Expand Down

0 comments on commit 1590d7e

Please sign in to comment.