Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reflection usage for most SKObject #1193

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions binding/Binding/GRContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static GRContext Create (GRBackend backend, IntPtr backendContext) =>
backend switch
{
GRBackend.Metal => throw new NotSupportedException (),
GRBackend.OpenGL => GetObject<GRContext> (SkiaApi.gr_context_make_gl (backendContext)),
GRBackend.OpenGL => GRContext.GetObject (SkiaApi.gr_context_make_gl (backendContext)),
GRBackend.Vulkan => throw new NotSupportedException (),
_ => throw new ArgumentOutOfRangeException (nameof (backend)),
};
Expand All @@ -51,7 +51,7 @@ public static GRContext CreateGl () =>
CreateGl (null);

public static GRContext CreateGl (GRGlInterface backendContext) =>
GetObject<GRContext> (SkiaApi.gr_context_make_gl (backendContext == null ? IntPtr.Zero : backendContext.Handle));
GetObject (SkiaApi.gr_context_make_gl (backendContext == null ? IntPtr.Zero : backendContext.Handle));

//

Expand Down Expand Up @@ -104,5 +104,23 @@ public int GetMaxSurfaceSampleCount (SKColorType colorType) =>
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete]
public int GetRecommendedSampleCount (GRPixelConfig config, float dpi) => 0;

internal static GRContext GetObject (IntPtr ptr, bool owns = true, bool unrefExisting = true)
{
if (GetInstance<GRContext> (ptr, out var instance)) {
if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
if (refcnt.GetReferenceCount () == 1)
throw new InvalidOperationException (
$"About to unreference an object that has no references. " +
$"H: {ptr:x} Type: {instance.GetType ()}");
#endif
refcnt.SafeUnRef ();
}
return instance;
}

return new GRContext (ptr, owns);
}
}
}
26 changes: 22 additions & 4 deletions binding/Binding/GRGlInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static GRGlInterface CreateDefaultInterface ()
public static GRGlInterface CreateNativeGlInterface ()
{
// the native code will automatically return null on non-OpenGL platforms, such as UWP
return GetObject<GRGlInterface> (SkiaApi.gr_glinterface_create_native_interface ());
return GetObject (SkiaApi.gr_glinterface_create_native_interface ());
}

public static GRGlInterface CreateNativeAngleInterface ()
Expand Down Expand Up @@ -69,7 +69,7 @@ public static GRGlInterface AssembleInterface (object context, GRGlGetProcDelega
: get;
var proxy = DelegateProxies.Create (del, DelegateProxies.GRGlGetProcDelegateProxy, out var gch, out var ctx);
try {
return GetObject<GRGlInterface> (SkiaApi.gr_glinterface_assemble_interface ((void*)ctx, proxy));
return GetObject (SkiaApi.gr_glinterface_assemble_interface ((void*)ctx, proxy));
} finally {
gch.Free ();
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public static GRGlInterface AssembleGlInterface (object context, GRGlGetProcDele
: get;
var proxy = DelegateProxies.Create (del, DelegateProxies.GRGlGetProcDelegateProxy, out var gch, out var ctx);
try {
return GetObject<GRGlInterface> (SkiaApi.gr_glinterface_assemble_gl_interface ((void*)ctx, proxy));
return GetObject (SkiaApi.gr_glinterface_assemble_gl_interface ((void*)ctx, proxy));
} finally {
gch.Free ();
}
Expand All @@ -116,7 +116,7 @@ public static GRGlInterface AssembleGlesInterface (object context, GRGlGetProcDe
: get;
var proxy = DelegateProxies.Create (del, DelegateProxies.GRGlGetProcDelegateProxy, out var gch, out var ctx);
try {
return GetObject<GRGlInterface> (SkiaApi.gr_glinterface_assemble_gles_interface ((void*)ctx, proxy));
return GetObject (SkiaApi.gr_glinterface_assemble_gles_interface ((void*)ctx, proxy));
} finally {
gch.Free ();
}
Expand All @@ -132,6 +132,24 @@ public bool HasExtension (string extension)
return SkiaApi.gr_glinterface_has_extension (Handle, extension);
}

internal static GRGlInterface GetObject (IntPtr ptr, bool owns = true, bool unrefExisting = true)
{
if (GetInstance<GRGlInterface> (ptr, out var instance)) {
if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
if (refcnt.GetReferenceCount () == 1)
throw new InvalidOperationException (
$"About to unreference an object that has no references. " +
$"H: {ptr:x} Type: {instance.GetType ()}");
#endif
refcnt.SafeUnRef ();
}
return instance;
}

return new GRGlInterface (ptr, owns);
}

private static class AngleLoader
{
private static readonly IntPtr libEGL;
Expand Down
18 changes: 18 additions & 0 deletions binding/Binding/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,24 @@ public void DrawPatch (SKPoint[] cubics, SKColor[] colors, SKPoint[] texCoords,
SkiaApi.sk_canvas_draw_patch (Handle, cubes, (uint*)cols, coords, mode, paint.Handle);
}
}

internal static SKCanvas GetObject (IntPtr ptr, bool owns = true, bool unrefExisting = true)
{
if (GetInstance<SKCanvas> (ptr, out var instance)) {
if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
if (refcnt.GetReferenceCount () == 1)
throw new InvalidOperationException (
$"About to unreference an object that has no references. " +
$"H: {ptr:x} Type: {instance.GetType ()}");
#endif
refcnt.SafeUnRef ();
}
return instance;
}

return new SKCanvas (ptr, owns);
}
}

public class SKAutoCanvasRestore : IDisposable
Expand Down
22 changes: 20 additions & 2 deletions binding/Binding/SKCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static SKCodec Create (SKStream stream, out SKCodecResult result)
throw new ArgumentNullException (nameof (stream));

fixed (SKCodecResult* r = &result) {
var codec = GetObject<SKCodec> (SkiaApi.sk_codec_new_from_stream (stream.Handle, r));
var codec = GetObject (SkiaApi.sk_codec_new_from_stream (stream.Handle, r));
stream.RevokeOwnership (codec);
return codec;
}
Expand All @@ -325,7 +325,7 @@ public static SKCodec Create (SKData data)
if (data == null)
throw new ArgumentNullException (nameof (data));

return GetObject<SKCodec> (SkiaApi.sk_codec_new_from_data (data.Handle));
return GetObject(SkiaApi.sk_codec_new_from_data (data.Handle));
}

// utils
Expand All @@ -343,5 +343,23 @@ internal static SKStream WrapManagedStream (Stream stream)
return new SKFrontBufferedManagedStream (stream, MinBufferedBytesNeeded, true);
}
}

internal static SKCodec GetObject (IntPtr ptr, bool owns = true, bool unrefExisting = true)
{
if (GetInstance<SKCodec> (ptr, out var instance)) {
if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
if (refcnt.GetReferenceCount () == 1)
throw new InvalidOperationException (
$"About to unreference an object that has no references. " +
$"H: {ptr:x} Type: {instance.GetType ()}");
#endif
refcnt.SafeUnRef ();
}
return instance;
}

return new SKCodec (ptr, owns);
}
}
}
34 changes: 26 additions & 8 deletions binding/Binding/SKColorFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ protected override void Dispose (bool disposing) =>

public static SKColorFilter CreateBlendMode(SKColor c, SKBlendMode mode)
{
return GetObject<SKColorFilter>(SkiaApi.sk_colorfilter_new_mode((uint)c, mode));
return GetObject(SkiaApi.sk_colorfilter_new_mode((uint)c, mode));
}

public static SKColorFilter CreateLighting(SKColor mul, SKColor add)
{
return GetObject<SKColorFilter>(SkiaApi.sk_colorfilter_new_lighting((uint)mul, (uint)add));
return GetObject(SkiaApi.sk_colorfilter_new_lighting((uint)mul, (uint)add));
}

public static SKColorFilter CreateCompose(SKColorFilter outer, SKColorFilter inner)
Expand All @@ -34,7 +34,7 @@ public static SKColorFilter CreateCompose(SKColorFilter outer, SKColorFilter inn
throw new ArgumentNullException(nameof(outer));
if (inner == null)
throw new ArgumentNullException(nameof(inner));
return GetObject<SKColorFilter>(SkiaApi.sk_colorfilter_new_compose(outer.Handle, inner.Handle));
return GetObject(SkiaApi.sk_colorfilter_new_compose(outer.Handle, inner.Handle));
}

public static SKColorFilter CreateColorMatrix(float[] matrix)
Expand All @@ -44,13 +44,13 @@ public static SKColorFilter CreateColorMatrix(float[] matrix)
if (matrix.Length != 20)
throw new ArgumentException("Matrix must have a length of 20.", nameof(matrix));
fixed (float* m = matrix) {
return GetObject<SKColorFilter> (SkiaApi.sk_colorfilter_new_color_matrix (m));
return GetObject (SkiaApi.sk_colorfilter_new_color_matrix (m));
}
}

public static SKColorFilter CreateLumaColor()
{
return GetObject<SKColorFilter>(SkiaApi.sk_colorfilter_new_luma_color());
return GetObject(SkiaApi.sk_colorfilter_new_luma_color());
}

public static SKColorFilter CreateTable(byte[] table)
Expand All @@ -60,7 +60,7 @@ public static SKColorFilter CreateTable(byte[] table)
if (table.Length != TableMaxLength)
throw new ArgumentException($"Table must have a length of {TableMaxLength}.", nameof(table));
fixed (byte* t = table) {
return GetObject<SKColorFilter> (SkiaApi.sk_colorfilter_new_table (t));
return GetObject (SkiaApi.sk_colorfilter_new_table (t));
}
}

Expand All @@ -79,18 +79,36 @@ public static SKColorFilter CreateTable(byte[] tableA, byte[] tableR, byte[] tab
fixed (byte* r = tableR)
fixed (byte* g = tableG)
fixed (byte* b = tableB) {
return GetObject<SKColorFilter> (SkiaApi.sk_colorfilter_new_table_argb (a, r, g, b));
return GetObject (SkiaApi.sk_colorfilter_new_table_argb (a, r, g, b));
}
}

public static SKColorFilter CreateHighContrast(SKHighContrastConfig config)
{
return GetObject<SKColorFilter>(SkiaApi.sk_colorfilter_new_high_contrast(&config));
return GetObject (SkiaApi.sk_colorfilter_new_high_contrast(&config));
}

public static SKColorFilter CreateHighContrast(bool grayscale, SKHighContrastConfigInvertStyle invertStyle, float contrast)
{
return CreateHighContrast(new SKHighContrastConfig(grayscale, invertStyle, contrast));
}

internal static SKColorFilter GetObject (IntPtr ptr, bool owns = true, bool unrefExisting = true)
{
if (GetInstance<SKColorFilter> (ptr, out var instance)) {
if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
if (refcnt.GetReferenceCount () == 1)
throw new InvalidOperationException (
$"About to unreference an object that has no references. " +
$"H: {ptr:x} Type: {instance.GetType ()}");
#endif
refcnt.SafeUnRef ();
}
return instance;
}

return new SKColorFilter (ptr, owns);
}
}
}
40 changes: 29 additions & 11 deletions binding/Binding/SKColorSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static SKColorSpace CreateIcc (IntPtr input, long length)
if (input == IntPtr.Zero)
throw new ArgumentNullException (nameof (input));

return GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_icc ((void*)input, (IntPtr)length));
return GetObject (SkiaApi.sk_colorspace_new_icc ((void*)input, (IntPtr)length));
}

public static SKColorSpace CreateIcc (byte[] input, long length)
Expand All @@ -83,7 +83,7 @@ public static SKColorSpace CreateIcc (byte[] input, long length)
throw new ArgumentNullException (nameof (input));

fixed (byte* i = input) {
return GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_icc (i, (IntPtr)length));
return GetObject (SkiaApi.sk_colorspace_new_icc (i, (IntPtr)length));
}
}

Expand All @@ -93,7 +93,7 @@ public static SKColorSpace CreateIcc (byte[] input)
throw new ArgumentNullException (nameof (input));

fixed (byte* i = input) {
return GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_icc (i, (IntPtr)input.Length));
return GetObject (SkiaApi.sk_colorspace_new_icc (i, (IntPtr)input.Length));
}
}

Expand Down Expand Up @@ -124,33 +124,33 @@ public static SKColorSpace CreateRgb (SKColorSpaceRenderTargetGamma gamma, SKMat
if (toXyzD50 == null)
throw new ArgumentNullException (nameof (toXyzD50));

return GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_rgb_with_gamma (gamma, toXyzD50.Handle));
return GetObject (SkiaApi.sk_colorspace_new_rgb_with_gamma (gamma, toXyzD50.Handle));
}

public static SKColorSpace CreateRgb (SKColorSpaceRenderTargetGamma gamma, SKColorSpaceGamut gamut) =>
GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_rgb_with_gamma_and_gamut (gamma, gamut));
GetObject (SkiaApi.sk_colorspace_new_rgb_with_gamma_and_gamut (gamma, gamut));

public static SKColorSpace CreateRgb (SKColorSpaceTransferFn coeffs, SKMatrix44 toXyzD50)
{
if (toXyzD50 == null)
throw new ArgumentNullException (nameof (toXyzD50));

return GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_rgb_with_coeffs (&coeffs, toXyzD50.Handle));
return GetObject (SkiaApi.sk_colorspace_new_rgb_with_coeffs (&coeffs, toXyzD50.Handle));
}

public static SKColorSpace CreateRgb (SKColorSpaceTransferFn coeffs, SKColorSpaceGamut gamut) =>
GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_rgb_with_coeffs_and_gamut (&coeffs, gamut));
GetObject (SkiaApi.sk_colorspace_new_rgb_with_coeffs_and_gamut (&coeffs, gamut));

public static SKColorSpace CreateRgb (SKNamedGamma gamma, SKMatrix44 toXyzD50)
{
if (toXyzD50 == null)
throw new ArgumentNullException (nameof (toXyzD50));

return GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_rgb_with_gamma_named (gamma, toXyzD50.Handle));
return GetObject (SkiaApi.sk_colorspace_new_rgb_with_gamma_named (gamma, toXyzD50.Handle));
}

public static SKColorSpace CreateRgb (SKNamedGamma gamma, SKColorSpaceGamut gamut) =>
GetObject<SKColorSpace> (SkiaApi.sk_colorspace_new_rgb_with_gamma_named_and_gamut (gamma, gamut));
GetObject (SkiaApi.sk_colorspace_new_rgb_with_gamma_named_and_gamut (gamma, gamut));

// GetNumericalTransferFunction

Expand All @@ -164,7 +164,7 @@ public bool GetNumericalTransferFunction (out SKColorSpaceTransferFn fn)
// *XyzD50

public SKMatrix44 ToXyzD50 () =>
GetObject<SKMatrix44> (SkiaApi.sk_colorspace_as_to_xyzd50 (Handle), false);
SKMatrix44.GetObject (SkiaApi.sk_colorspace_as_to_xyzd50 (Handle), false);

public bool ToXyzD50 (SKMatrix44 toXyzD50)
{
Expand All @@ -175,7 +175,25 @@ public bool ToXyzD50 (SKMatrix44 toXyzD50)
}

public SKMatrix44 FromXyzD50 () =>
GetObject<SKMatrix44> (SkiaApi.sk_colorspace_as_from_xyzd50 (Handle), false);
SKMatrix44.GetObject (SkiaApi.sk_colorspace_as_from_xyzd50 (Handle), false);

internal static SKColorSpace GetObject (IntPtr ptr, bool owns = true, bool unrefExisting = true)
{
if (GetInstance<SKColorSpace> (ptr, out var instance)) {
if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
if (refcnt.GetReferenceCount () == 1)
throw new InvalidOperationException (
$"About to unreference an object that has no references. " +
$"H: {ptr:x} Type: {instance.GetType ()}");
#endif
refcnt.SafeUnRef ();
}
return instance;
}

return new SKColorSpace (ptr, owns);
}

//

Expand Down
Loading