Skip to content

Commit

Permalink
Add some of the new APIs (#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Oct 14, 2021
1 parent 105f902 commit 95c296b
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 135 deletions.
26 changes: 18 additions & 8 deletions binding/Binding/GRContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SkiaSharp
{
public unsafe class GRContext : SKObject, ISKReferenceCounted, ISKSkipObjectRegistration
public unsafe class GRContext : GRRecordingContext
{
internal GRContext (IntPtr h, bool owns)
: base (h, owns)
Expand Down Expand Up @@ -126,7 +126,7 @@ public static GRContext CreateMetal (GRMtlBackendContext backendContext, GRConte

//

public GRBackend Backend => SkiaApi.gr_direct_context_get_backend (Handle).FromNative ();
public new GRBackend Backend => base.Backend;

public bool IsAbandoned => SkiaApi.gr_direct_context_is_abandoned (Handle);

Expand Down Expand Up @@ -175,11 +175,21 @@ public void ResetContext (GRBackendState state = GRBackendState.All) =>
public void ResetContext (uint state) =>
SkiaApi.gr_direct_context_reset_context (Handle, state);

public void Flush () =>
SkiaApi.gr_direct_context_flush (Handle);
public void Flush () => Flush (true);

public int GetMaxSurfaceSampleCount (SKColorType colorType) =>
SkiaApi.gr_direct_context_get_max_surface_sample_count_for_color_type (Handle, colorType.ToNative ());
public void Flush (bool submit, bool synchronous = false)
{
if (submit)
SkiaApi.gr_direct_context_flush_and_submit (Handle, synchronous);
else
SkiaApi.gr_direct_context_flush (Handle);
}

public void Submit (bool synchronous = false) =>
SkiaApi.gr_direct_context_submit (Handle, synchronous);

public new int GetMaxSurfaceSampleCount (SKColorType colorType) =>
base.GetMaxSurfaceSampleCount (colorType);

[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete]
Expand All @@ -200,7 +210,7 @@ public void PurgeUnlockedResources (bool scratchResourcesOnly) =>
public void PurgeUnlockedResources (long bytesToPurge, bool preferScratchResources) =>
SkiaApi.gr_direct_context_purge_unlocked_resources_bytes (Handle, (IntPtr)bytesToPurge, preferScratchResources);

internal static GRContext GetObject (IntPtr handle) =>
handle == IntPtr.Zero ? null : new GRContext (handle, true);
internal static new GRContext GetObject (IntPtr handle, bool owns = true) =>
GetOrAddObject (handle, owns, (h, o) => new GRContext (h, o));
}
}
20 changes: 20 additions & 0 deletions binding/Binding/GRRecordingContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SkiaSharp
{
public unsafe class GRRecordingContext : SKObject, ISKReferenceCounted
{
internal GRRecordingContext (IntPtr h, bool owns)
: base (h, owns)
{
}

public GRBackend Backend => SkiaApi.gr_recording_context_get_backend (Handle).FromNative ();

public int GetMaxSurfaceSampleCount (SKColorType colorType) =>
SkiaApi.gr_recording_context_get_max_surface_sample_count_for_color_type (Handle, colorType.ToNative ());

internal static GRRecordingContext GetObject (IntPtr handle, bool owns = true, bool unrefExisting = true) =>
GetOrAddObject (handle, owns, unrefExisting, (h, o) => new GRRecordingContext (h, o));
}
}
22 changes: 11 additions & 11 deletions binding/Binding/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ public int SaveLayer () =>

// DrawColor

public void DrawColor (SKColor color, SKBlendMode mode = SKBlendMode.Src)
{
public void DrawColor (SKColor color, SKBlendMode mode = SKBlendMode.Src) =>
SkiaApi.sk_canvas_draw_color (Handle, (uint)color, mode);
}

public void DrawColor (SKColorF color, SKBlendMode mode = SKBlendMode.Src) =>
SkiaApi.sk_canvas_draw_color4f (Handle, color, mode);

// DrawLine

Expand All @@ -93,15 +94,14 @@ public void DrawLine (float x0, float y0, float x1, float y1, SKPaint paint)

// Clear

public void Clear ()
{
DrawColor (SKColors.Empty, SKBlendMode.Src);
}
public void Clear () =>
Clear (SKColors.Empty);

public void Clear (SKColor color)
{
DrawColor (color, SKBlendMode.Src);
}
public void Clear (SKColor color) =>
SkiaApi.sk_canvas_clear (Handle, (uint)color);

public void Clear (SKColorF color) =>
SkiaApi.sk_canvas_clear_color4f (Handle, color);

// Restore*

Expand Down
2 changes: 1 addition & 1 deletion binding/Binding/SKColorSpaceStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public SKColorSpaceIccProfile ()
: this (SkiaApi.sk_colorspace_icc_profile_new (), true)
{
if (Handle == IntPtr.Zero)
throw new InvalidOperationException ("Unable to create a new SK3dView instance.");
throw new InvalidOperationException ("Unable to create a new SKColorSpaceIccProfile instance.");
}

protected override void DisposeNative () =>
Expand Down
90 changes: 55 additions & 35 deletions binding/Binding/SKImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,32 +319,40 @@ public static SKImage FromTexture (GRContext context, GRGlBackendTextureDesc des
return FromTexture (context, texture, desc.Origin, desc.Config.ToColorType (), alpha, null, releaseProc, releaseContext);
}

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, SKColorType colorType)
{
return FromTexture (context, texture, GRSurfaceOrigin.BottomLeft, colorType, SKAlphaType.Premul, null, null, null);
}
public static SKImage FromTexture (GRContext context, GRBackendTexture texture, SKColorType colorType) =>
FromTexture ((GRRecordingContext)context, texture, colorType);

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType)
{
return FromTexture (context, texture, origin, colorType, SKAlphaType.Premul, null, null, null);
}
public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType) =>
FromTexture ((GRRecordingContext)context, texture, origin, colorType);

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha)
{
return FromTexture (context, texture, origin, colorType, alpha, null, null, null);
}
public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha) =>
FromTexture ((GRRecordingContext)context, texture, origin, colorType, alpha);

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace)
{
return FromTexture (context, texture, origin, colorType, alpha, colorspace, null, null);
}
public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace) =>
FromTexture ((GRRecordingContext)context, texture, origin, colorType, alpha, colorspace);

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace, SKImageTextureReleaseDelegate releaseProc)
{
return FromTexture (context, texture, origin, colorType, alpha, colorspace, releaseProc, null);
}
public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace, SKImageTextureReleaseDelegate releaseProc) =>
FromTexture ((GRRecordingContext)context, texture, origin, colorType, alpha, colorspace, releaseProc);

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace, SKImageTextureReleaseDelegate releaseProc, object releaseContext) =>
FromTexture ((GRRecordingContext)context, texture, origin, colorType, alpha, colorspace, releaseProc, releaseContext);

public static SKImage FromTexture (GRRecordingContext context, GRBackendTexture texture, SKColorType colorType) =>
FromTexture (context, texture, GRSurfaceOrigin.BottomLeft, colorType, SKAlphaType.Premul, null, null, null);

public static SKImage FromTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType) =>
FromTexture (context, texture, origin, colorType, SKAlphaType.Premul, null, null, null);

public static SKImage FromTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha) =>
FromTexture (context, texture, origin, colorType, alpha, null, null, null);

public static SKImage FromTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace, SKImageTextureReleaseDelegate releaseProc, object releaseContext)
public static SKImage FromTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace) =>
FromTexture (context, texture, origin, colorType, alpha, colorspace, null, null);

public static SKImage FromTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace, SKImageTextureReleaseDelegate releaseProc) =>
FromTexture (context, texture, origin, colorType, alpha, colorspace, releaseProc, null);

public static SKImage FromTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace, SKImageTextureReleaseDelegate releaseProc, object releaseContext)
{
if (context == null)
throw new ArgumentNullException (nameof (context));
Expand Down Expand Up @@ -389,22 +397,28 @@ public static SKImage FromAdoptedTexture (GRContext context, GRGlBackendTextureD
return FromAdoptedTexture (context, texture, desc.Origin, desc.Config.ToColorType (), alpha, null);
}

public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, SKColorType colorType)
{
return FromAdoptedTexture (context, texture, GRSurfaceOrigin.BottomLeft, colorType, SKAlphaType.Premul, null);
}
public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, SKColorType colorType) =>
FromAdoptedTexture ((GRRecordingContext)context, texture, colorType);

public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType)
{
return FromAdoptedTexture (context, texture, origin, colorType, SKAlphaType.Premul, null);
}
public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType) =>
FromAdoptedTexture ((GRRecordingContext)context, texture, origin, colorType);

public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha)
{
return FromAdoptedTexture (context, texture, origin, colorType, alpha, null);
}
public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha) =>
FromAdoptedTexture ((GRRecordingContext)context, texture, origin, colorType, alpha);

public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace) =>
FromAdoptedTexture ((GRRecordingContext)context, texture, origin, colorType, alpha, colorspace);

public static SKImage FromAdoptedTexture (GRRecordingContext context, GRBackendTexture texture, SKColorType colorType) =>
FromAdoptedTexture (context, texture, GRSurfaceOrigin.BottomLeft, colorType, SKAlphaType.Premul, null);

public static SKImage FromAdoptedTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType) =>
FromAdoptedTexture (context, texture, origin, colorType, SKAlphaType.Premul, null);

public static SKImage FromAdoptedTexture (GRContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace)
public static SKImage FromAdoptedTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha) =>
FromAdoptedTexture (context, texture, origin, colorType, alpha, null);

public static SKImage FromAdoptedTexture (GRRecordingContext context, GRBackendTexture texture, GRSurfaceOrigin origin, SKColorType colorType, SKAlphaType alpha, SKColorSpace colorspace)
{
if (context == null)
throw new ArgumentNullException (nameof (context));
Expand Down Expand Up @@ -555,6 +569,9 @@ public SKPixmap PeekPixels ()
SkiaApi.sk_image_is_lazy_generated (Handle);

public bool IsValid (GRContext context) =>
IsValid ((GRRecordingContext)context);

public bool IsValid (GRRecordingContext context) =>
SkiaApi.sk_image_is_valid (Handle, context?.Handle ?? IntPtr.Zero);

// ReadPixels
Expand Down Expand Up @@ -661,7 +678,10 @@ public SKImage ApplyImageFilter (SKImageFilter filter, SKRectI subset, SKRectI c
}
}

public SKImage ApplyImageFilter (GRContext context, SKImageFilter filter, SKRectI subset, SKRectI clipBounds, out SKRectI outSubset, out SKPointI outOffset)
public SKImage ApplyImageFilter (GRContext context, SKImageFilter filter, SKRectI subset, SKRectI clipBounds, out SKRectI outSubset, out SKPointI outOffset) =>
ApplyImageFilter ((GRRecordingContext)context, filter, subset, clipBounds, out outSubset, out outOffset);

public SKImage ApplyImageFilter (GRRecordingContext context, SKImageFilter filter, SKRectI subset, SKRectI clipBounds, out SKRectI outSubset, out SKPointI outOffset)
{
if (filter == null)
throw new ArgumentNullException (nameof (filter));
Expand Down
10 changes: 8 additions & 2 deletions binding/Binding/SKImageFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,20 @@ public static SKImageFilter CreateMerge(SKImageFilter[] filters, SKImageFilter.C

// CreateDilate

public static SKImageFilter CreateDilate(int radiusX, int radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
public static SKImageFilter CreateDilate(int radiusX, int radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null) =>
CreateDilate ((float)radiusX, (float)radiusY, input, cropRect);

public static SKImageFilter CreateDilate(float radiusX, float radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return GetObject(SkiaApi.sk_imagefilter_new_dilate(radiusX, radiusY, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}

// CreateErode

public static SKImageFilter CreateErode(int radiusX, int radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
public static SKImageFilter CreateErode(int radiusX, int radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null) =>
CreateErode ((float)radiusX, (float)radiusY, input, cropRect);

public static SKImageFilter CreateErode(float radiusX, float radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return GetObject(SkiaApi.sk_imagefilter_new_erode(radiusX, radiusY, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
Expand Down
19 changes: 9 additions & 10 deletions binding/Binding/SKPixmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,20 @@ public bool ExtractSubset (SKPixmap result, SKRectI subset)

// Erase

public bool Erase (SKColor color)
{
return Erase (color, Rect);
}
public bool Erase (SKColor color) =>
Erase (color, Rect);

public bool Erase (SKColor color, SKRectI subset)
{
return SkiaApi.sk_pixmap_erase_color (Handle, (uint)color, &subset);
}
public bool Erase (SKColor color, SKRectI subset) =>
SkiaApi.sk_pixmap_erase_color (Handle, (uint)color, &subset);

public bool Erase (SKColorF color) =>
Erase (color, Rect);
Erase (color, null, Rect);

public bool Erase (SKColorF color, SKRectI subset) =>
SkiaApi.sk_pixmap_erase_color4f (Handle, &color, &subset);
Erase (color, null, subset);

public bool Erase (SKColorF color, SKColorSpace colorspace, SKRectI subset) =>
SkiaApi.sk_pixmap_erase_color4f (Handle, &color, colorspace?.Handle ?? IntPtr.Zero, &subset);

// With*

Expand Down
Loading

0 comments on commit 95c296b

Please sign in to comment.