[C# API] Implemented a base type for all Skia objects

- better management of managed instances
 - added more members to SKStream
This commit is contained in:
Matthew Leibowitz 2016-02-02 00:19:33 +02:00
Родитель 0fbdf74632
Коммит 033c724f21
24 изменённых файлов: 616 добавлений и 588 удалений

Просмотреть файл

@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Definitions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SkiaApi.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKImageFilter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKColorFilter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKPaint.cs" />

Просмотреть файл

@ -4,7 +4,7 @@
// Author:
// Miguel de Icaza (miguel@xamarin.com)
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
// TODO:
// Add more ToString, operators, convenience methods to various structures here (point, rect, etc)

Просмотреть файл

@ -4,7 +4,7 @@
// Author:
// Matthew Leibowitz
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
@ -12,18 +12,16 @@ using System.Runtime.InteropServices;
namespace SkiaSharp
{
public class SKBitmap : IDisposable
public class SKBitmap : SKObject
{
internal IntPtr handle;
internal SKBitmap (IntPtr handle)
protected SKBitmap (IntPtr handle)
: base (handle)
{
this.handle = handle;
}
public SKBitmap ()
: this (SkiaApi.sk_bitmap_new ())
{
handle = SkiaApi.sk_bitmap_new ();
}
public SKBitmap (int width, int height, bool isOpaque = false)
@ -44,63 +42,53 @@ namespace SkiaSharp
public SKBitmap (SKImageInfo info, int rowBytes)
: this ()
{
if (!SkiaApi.sk_bitmap_try_alloc_pixels (handle, ref info, (IntPtr)rowBytes)) {
if (!SkiaApi.sk_bitmap_try_alloc_pixels (Handle, ref info, (IntPtr)rowBytes)) {
throw new Exception ("Unable to allocate pixels for the bitmap.");
}
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_bitmap_destructor (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_bitmap_destructor (Handle);
}
}
~SKBitmap ()
{
Dispose (false);
base.Dispose (disposing);
}
public void Reset ()
{
SkiaApi.sk_bitmap_reset (handle);
SkiaApi.sk_bitmap_reset (Handle);
}
public void SetImmutable ()
{
SkiaApi.sk_bitmap_set_immutable (handle);
SkiaApi.sk_bitmap_set_immutable (Handle);
}
public void Erase (SKColor color)
{
SkiaApi.sk_bitmap_erase (handle, color);
SkiaApi.sk_bitmap_erase (Handle, color);
}
public void Erase (SKColor color, SKRectI rect)
{
SkiaApi.sk_bitmap_erase_rect (handle, color, ref rect);
SkiaApi.sk_bitmap_erase_rect (Handle, color, ref rect);
}
public SKColor GetPixel (int x, int y)
{
return SkiaApi.sk_bitmap_get_pixel_color (handle, x, y);
return SkiaApi.sk_bitmap_get_pixel_color (Handle, x, y);
}
public void SetPixel (int x, int y, SKColor color)
{
SkiaApi.sk_bitmap_set_pixel_color (handle, x, y, color);
SkiaApi.sk_bitmap_set_pixel_color (Handle, x, y, color);
}
public bool CanCopyTo (SKColorType colorType)
{
return SkiaApi.sk_bitmap_can_copy_to (handle, colorType);
return SkiaApi.sk_bitmap_can_copy_to (Handle, colorType);
}
public SKBitmap Copy ()
@ -111,7 +99,7 @@ namespace SkiaSharp
public SKBitmap Copy (SKColorType colorType)
{
var destination = new SKBitmap ();
if (!SkiaApi.sk_bitmap_copy (handle, destination.handle, colorType)) {
if (!SkiaApi.sk_bitmap_copy (Handle, destination.Handle, colorType)) {
destination.Dispose ();
destination = null;
}
@ -120,31 +108,31 @@ namespace SkiaSharp
public bool CopyTo (SKBitmap destination)
{
return SkiaApi.sk_bitmap_copy (handle, destination.handle, ColorType);
return SkiaApi.sk_bitmap_copy (Handle, destination.Handle, ColorType);
}
public bool CopyTo (SKBitmap destination, SKColorType colorType)
{
return SkiaApi.sk_bitmap_copy (handle, destination.handle, colorType);
return SkiaApi.sk_bitmap_copy (Handle, destination.Handle, colorType);
}
public SKImageInfo Info {
get {
SKImageInfo info;
SkiaApi.sk_bitmap_get_info (handle, out info);
SkiaApi.sk_bitmap_get_info (Handle, out info);
return info;
}
}
public int Width {
public int Width {
get { return Info.Width; }
}
public int Height {
public int Height {
get { return Info.Height; }
}
public SKColorType ColorType {
public SKColorType ColorType {
get { return Info.ColorType; }
}
@ -157,24 +145,24 @@ namespace SkiaSharp
}
public int RowBytes {
get { return (int)SkiaApi.sk_bitmap_get_row_bytes (handle); }
get { return (int)SkiaApi.sk_bitmap_get_row_bytes (Handle); }
}
public int ByteCount {
get { return (int)SkiaApi.sk_bitmap_get_byte_count (handle); }
get { return (int)SkiaApi.sk_bitmap_get_byte_count (Handle); }
}
public byte[] Bytes {
get {
SkiaApi.sk_bitmap_lock_pixels (handle);
SkiaApi.sk_bitmap_lock_pixels (Handle);
try {
IntPtr length;
var pixelsPtr = SkiaApi.sk_bitmap_get_pixels (handle, out length);
var pixelsPtr = SkiaApi.sk_bitmap_get_pixels (Handle, out length);
byte[] bytes = new byte[(int)length];
Marshal.Copy (pixelsPtr, bytes, 0, (int)length);
return bytes;
} finally {
SkiaApi.sk_bitmap_unlock_pixels (handle);
SkiaApi.sk_bitmap_unlock_pixels (Handle);
}
}
}
@ -183,11 +171,11 @@ namespace SkiaSharp
get {
var info = Info;
var pixels = new SKColor[info.Width * info.Height];
SkiaApi.sk_bitmap_get_pixel_colors (handle, pixels);
return pixels;
SkiaApi.sk_bitmap_get_pixel_colors (Handle, pixels);
return pixels;
}
set {
SkiaApi.sk_bitmap_set_pixel_colors (handle, value);
set {
SkiaApi.sk_bitmap_set_pixel_colors (Handle, value);
}
}
@ -196,20 +184,20 @@ namespace SkiaSharp
}
public bool IsNull {
get { return SkiaApi.sk_bitmap_is_null (handle); }
get { return SkiaApi.sk_bitmap_is_null (Handle); }
}
public bool DrawsNothing {
public bool DrawsNothing {
get { return IsEmpty || IsNull; }
}
public bool IsImmutable {
get { return SkiaApi.sk_bitmap_is_immutable (handle); }
get { return SkiaApi.sk_bitmap_is_immutable (Handle); }
}
public bool IsVolatile {
get { return SkiaApi.sk_bitmap_is_volatile (handle); }
set { SkiaApi.sk_bitmap_set_volatile (handle, value); }
get { return SkiaApi.sk_bitmap_is_volatile (Handle); }
set { SkiaApi.sk_bitmap_set_volatile (Handle, value); }
}
public static SKImageInfo DecodeBounds (SKStreamRewindable stream, SKColorType pref = SKColorType.Unknown)

Просмотреть файл

@ -4,48 +4,47 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
// No dispose, the Canvas is only valid while the Surface is valid.
public class SKCanvas
public class SKCanvas : SKObject
{
internal IntPtr handle;
internal SKCanvas (IntPtr ptr)
internal SKCanvas (IntPtr handle)
: base (handle)
{
handle = ptr;
}
public void Save ()
{
if (handle == IntPtr.Zero)
if (Handle == IntPtr.Zero)
throw new ObjectDisposedException ("SKCanvas");
SkiaApi.sk_canvas_save (handle);
SkiaApi.sk_canvas_save (Handle);
}
public void SaveLayer (SKRect limit, SKPaint paint)
{
SkiaApi.sk_canvas_save_layer (handle, ref limit, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_save_layer (Handle, ref limit, paint == null ? IntPtr.Zero : paint.Handle);
}
public void SaveLayer (SKPaint paint)
{
SkiaApi.sk_canvas_save_layer (handle, IntPtr.Zero, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_save_layer (Handle, IntPtr.Zero, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawColor (SKColor color, SKXferMode mode = SKXferMode.Src)
{
SkiaApi.sk_canvas_draw_color (handle, color, mode);
SkiaApi.sk_canvas_draw_color (Handle, color, mode);
}
public void DrawLine (float x0, float y0, float x1, float y1, SKPaint paint)
{
if (paint == null)
throw new ArgumentNullException ("paint");
SkiaApi.sk_canvas_draw_line (handle, x0, y0, x1, y1, paint.handle);
SkiaApi.sk_canvas_draw_line (Handle, x0, y0, x1, y1, paint.Handle);
}
public void Clear ()
@ -60,62 +59,62 @@ namespace SkiaSharp
public void Restore ()
{
SkiaApi.sk_canvas_restore (handle);
SkiaApi.sk_canvas_restore (Handle);
}
public void RestoreToCount (int count)
{
SkiaApi.sk_canvas_restore_to_count (handle, count);
SkiaApi.sk_canvas_restore_to_count (Handle, count);
}
public void Translate (float dx, float dy)
{
SkiaApi.sk_canvas_translate (handle, dx, dy);
SkiaApi.sk_canvas_translate (Handle, dx, dy);
}
public void Translate (SKPoint point)
{
SkiaApi.sk_canvas_translate (handle, point.X, point.Y);
SkiaApi.sk_canvas_translate (Handle, point.X, point.Y);
}
public void Scale (float sx, float sy)
{
SkiaApi.sk_canvas_scale (handle, sx, sy);
SkiaApi.sk_canvas_scale (Handle, sx, sy);
}
public void Scale (SKPoint size)
{
SkiaApi.sk_canvas_scale (handle, size.X, size.Y);
SkiaApi.sk_canvas_scale (Handle, size.X, size.Y);
}
public void RotateDegrees (float degrees)
{
SkiaApi.sk_canvas_rotate_degrees (handle, degrees);
SkiaApi.sk_canvas_rotate_degrees (Handle, degrees);
}
public void RotateRadians (float radians)
{
SkiaApi.sk_canvas_rotate_radians (handle, radians);
SkiaApi.sk_canvas_rotate_radians (Handle, radians);
}
public void Skew (float sx, float sy)
{
SkiaApi.sk_canvas_skew (handle, sx, sy);
SkiaApi.sk_canvas_skew (Handle, sx, sy);
}
public void Skew (SKPoint skew)
{
SkiaApi.sk_canvas_skew (handle, skew.X, skew.Y);
SkiaApi.sk_canvas_skew (Handle, skew.X, skew.Y);
}
public void Concat (ref SKMatrix m)
{
SkiaApi.sk_canvas_concat (handle, ref m);
SkiaApi.sk_canvas_concat (Handle, ref m);
}
public void ClipRect (SKRect rect)
{
SkiaApi.sk_canvas_clip_rect (handle, ref rect);
SkiaApi.sk_canvas_clip_rect (Handle, ref rect);
}
public void ClipPath (SKPath path)
@ -123,28 +122,28 @@ namespace SkiaSharp
if (path == null)
throw new ArgumentNullException ("path");
SkiaApi.sk_canvas_clip_path (handle, path.handle);
SkiaApi.sk_canvas_clip_path (Handle, path.Handle);
}
public void DrawPaint (SKPaint paint)
{
if (paint == null)
throw new ArgumentNullException ("paint");
SkiaApi.sk_canvas_draw_paint (handle, paint.handle);
SkiaApi.sk_canvas_draw_paint (Handle, paint.Handle);
}
public void DrawRect (SKRect rect, SKPaint paint)
{
if (paint == null)
throw new ArgumentNullException ("paint");
SkiaApi.sk_canvas_draw_rect (handle, ref rect, paint.handle);
SkiaApi.sk_canvas_draw_rect (Handle, ref rect, paint.Handle);
}
public void DrawOval (SKRect rect, SKPaint paint)
{
if (paint == null)
throw new ArgumentNullException ("paint");
SkiaApi.sk_canvas_draw_oval (handle, ref rect, paint.handle);
SkiaApi.sk_canvas_draw_oval (Handle, ref rect, paint.Handle);
}
public void DrawPath (SKPath path, SKPaint paint)
@ -153,7 +152,7 @@ namespace SkiaSharp
throw new ArgumentNullException ("paint");
if (path == null)
throw new ArgumentNullException ("path");
SkiaApi.sk_canvas_draw_path (handle, path.handle, paint.handle);
SkiaApi.sk_canvas_draw_path (Handle, path.Handle, paint.Handle);
}
public void DrawPoints (SKPointMode mode, SKPoint [] points, SKPaint paint)
@ -162,75 +161,75 @@ namespace SkiaSharp
throw new ArgumentNullException ("paint");
if (points == null)
throw new ArgumentNullException ("points");
SkiaApi.sk_canvas_draw_points (handle, mode, (IntPtr)points.Length, points, paint.handle);
SkiaApi.sk_canvas_draw_points (Handle, mode, (IntPtr)points.Length, points, paint.Handle);
}
public void DrawPoint (float x, float y, SKPaint paint)
{
if (paint == null)
throw new ArgumentNullException ("paint");
SkiaApi.sk_canvas_draw_point (handle, x, y, paint.handle);
SkiaApi.sk_canvas_draw_point (Handle, x, y, paint.Handle);
}
public void DrawPoint (float x, float y, SKColor color)
{
SkiaApi.sk_canvas_draw_point_color (handle, x, y, color);
SkiaApi.sk_canvas_draw_point_color (Handle, x, y, color);
}
public void DrawImage (SKImage image, float x, float y, SKPaint paint = null)
{
if (image == null)
throw new ArgumentNullException ("image");
SkiaApi.sk_canvas_draw_image (handle, image.handle, x, y, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_image (Handle, image.Handle, x, y, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawImage (SKImage image, SKRect dest, SKPaint paint = null)
{
if (image == null)
throw new ArgumentNullException ("image");
SkiaApi.sk_canvas_draw_image_rect (handle, image.handle, IntPtr.Zero, ref dest, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_image_rect (Handle, image.Handle, IntPtr.Zero, ref dest, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawImage (SKImage image, SKRect source, SKRect dest, SKPaint paint = null)
{
if (image == null)
throw new ArgumentNullException ("image");
SkiaApi.sk_canvas_draw_image_rect (handle, image.handle, ref source, ref dest, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_image_rect (Handle, image.Handle, ref source, ref dest, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawPicture (SKPicture picture, ref SKMatrix matrix, SKPaint paint = null)
{
if (picture == null)
throw new ArgumentNullException ("picture");
SkiaApi.sk_canvas_draw_picture (handle, picture.handle, ref matrix, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_picture (Handle, picture.Handle, ref matrix, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawPicture (SKPicture picture, SKPaint paint = null)
{
if (picture == null)
throw new ArgumentNullException ("picture");
SkiaApi.sk_canvas_draw_picture (handle, picture.handle, IntPtr.Zero, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_picture (Handle, picture.Handle, IntPtr.Zero, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawBitmap (SKBitmap bitmap, float x, float y, SKPaint paint = null)
{
if (bitmap == null)
throw new ArgumentNullException ("bitmap");
SkiaApi.sk_canvas_draw_bitmap (handle, bitmap.handle, x, y, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_bitmap (Handle, bitmap.Handle, x, y, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawBitmap (SKBitmap bitmap, SKRect dest, SKPaint paint = null)
{
if (bitmap == null)
throw new ArgumentNullException ("bitmap");
SkiaApi.sk_canvas_draw_bitmap_rect (handle, bitmap.handle, IntPtr.Zero, ref dest, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_bitmap_rect (Handle, bitmap.Handle, IntPtr.Zero, ref dest, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawBitmap (SKBitmap bitmap, SKRect source, SKRect dest, SKPaint paint = null)
{
if (bitmap == null)
throw new ArgumentNullException ("bitmap");
SkiaApi.sk_canvas_draw_bitmap_rect (handle, bitmap.handle, ref source, ref dest, paint == null ? IntPtr.Zero : paint.handle);
SkiaApi.sk_canvas_draw_bitmap_rect (Handle, bitmap.Handle, ref source, ref dest, paint == null ? IntPtr.Zero : paint.Handle);
}
public void DrawText (string text, float x, float y, SKPaint paint)
@ -241,7 +240,7 @@ namespace SkiaSharp
throw new ArgumentNullException ("paint");
var bytes = System.Text.Encoding.UTF8.GetBytes (text);
SkiaApi.sk_canvas_draw_text (handle, bytes, bytes.Length, x, y, paint.handle);
SkiaApi.sk_canvas_draw_text (Handle, bytes, bytes.Length, x, y, paint.Handle);
}
public void DrawText (string text, SKPoint [] points, SKPaint paint)
@ -254,7 +253,7 @@ namespace SkiaSharp
throw new ArgumentNullException ("points");
var bytes = System.Text.Encoding.UTF8.GetBytes (text);
SkiaApi.sk_canvas_draw_pos_text (handle, bytes, bytes.Length, points, paint.handle);
SkiaApi.sk_canvas_draw_pos_text (Handle, bytes, bytes.Length, points, paint.Handle);
}
public void DrawText (string text, SKPath path, float hOffset, float vOffset, SKPaint paint)
@ -267,10 +266,10 @@ namespace SkiaSharp
throw new ArgumentNullException ("paint");
var bytes = System.Text.Encoding.UTF8.GetBytes (text);
SkiaApi.sk_canvas_draw_text_on_path (handle, bytes, bytes.Length, path.handle, hOffset, vOffset, paint.handle);
SkiaApi.sk_canvas_draw_text_on_path (Handle, bytes, bytes.Length, path.Handle, hOffset, vOffset, paint.Handle);
}
public int SaveCount => SkiaApi.sk_canvas_get_save_count (handle);
public int SaveCount => SkiaApi.sk_canvas_get_save_count (Handle);
}
public class SKAutoCanvasRestore : IDisposable

Просмотреть файл

@ -4,13 +4,13 @@
// Author:
// Matthew Leibowitz
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKColorFilter : IDisposable
public class SKColorFilter : SKObject
{
public const int MIN_CUBE_SIZE = 4;
public const int MAX_CUBE_SIZE = 64;
@ -23,33 +23,21 @@ namespace SkiaSharp
(null != cubeData) && (cubeData.Size >= minMemorySize);
}
internal IntPtr handle;
internal SKColorFilter(IntPtr handle)
: base (handle)
{
this.handle = handle;
}
public void Dispose()
protected override void Dispose(bool disposing)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (handle != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
SkiaApi.sk_colorfilter_unref(handle);
handle = IntPtr.Zero;
SkiaApi.sk_colorfilter_unref(Handle);
}
}
~SKColorFilter()
{
Dispose(false);
base.Dispose(disposing);
}
public static SKColorFilter CreateXferMode(SKColor c, SKXferMode mode)
{
return new SKColorFilter(SkiaApi.sk_colorfilter_new_mode(c, mode));
@ -66,7 +54,7 @@ namespace SkiaSharp
throw new ArgumentNullException("outer");
if (inner == null)
throw new ArgumentNullException("inner");
return new SKColorFilter(SkiaApi.sk_colorfilter_new_compose(outer.handle, inner.handle));
return new SKColorFilter(SkiaApi.sk_colorfilter_new_compose(outer.Handle, inner.Handle));
}
public static SKColorFilter CreateColorCube(byte[] cubeData, int cubeDimension)
@ -78,7 +66,7 @@ namespace SkiaSharp
{
if (!IsValid3DColorCube(cubeData, cubeDimension))
throw new ArgumentNullException("cubeData");
return new SKColorFilter(SkiaApi.sk_colorfilter_new_color_cube(cubeData.handle, cubeDimension));
return new SKColorFilter(SkiaApi.sk_colorfilter_new_color_cube(cubeData.Handle, cubeDimension));
}
public static SKColorFilter CreateColorMatrix(float[] matrix)

Просмотреть файл

@ -4,7 +4,7 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;

Просмотреть файл

@ -4,7 +4,7 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
@ -13,49 +13,38 @@ using System.IO;
namespace SkiaSharp
{
public class SKData : IDisposable
public class SKData : SKObject
{
internal IntPtr handle;
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_data_unref (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_data_unref (Handle);
}
}
~SKData()
{
Dispose (false);
base.Dispose (disposing);
}
internal SKData (IntPtr x)
: base (x)
{
handle = x;
}
public SKData ()
: this (SkiaApi.sk_data_new_empty ())
{
handle = SkiaApi.sk_data_new_empty ();
}
public SKData (IntPtr bytes, ulong length)
: this (IntPtr.Zero)
{
if (Marshal.SizeOf<IntPtr> () == 4 && length > UInt32.MaxValue)
throw new ArgumentException ("length", "The lenght exceeds the size of pointers");
handle = SkiaApi.sk_data_new_with_copy (bytes, (IntPtr) length);
Handle = SkiaApi.sk_data_new_with_copy (bytes, (IntPtr) length);
}
public SKData (byte[] bytes)
: this (SkiaApi.sk_data_new_with_copy (bytes, (IntPtr) bytes.Length))
{
handle = SkiaApi.sk_data_new_with_copy (bytes, (IntPtr) bytes.Length);
}
public static SKData FromMallocMemory (IntPtr bytes, ulong length)
@ -78,11 +67,11 @@ namespace SkiaSharp
if (offset > UInt32.MaxValue)
throw new ArgumentException ("offset", "The length exceeds the size of pointers");
}
return new SKData (SkiaApi.sk_data_new_subset (handle, (IntPtr) offset, (IntPtr) length));
return new SKData (SkiaApi.sk_data_new_subset (Handle, (IntPtr) offset, (IntPtr) length));
}
public long Size => (long)SkiaApi.sk_data_get_size (handle);
public IntPtr Data => SkiaApi.sk_data_get_data (handle);
public long Size => (long)SkiaApi.sk_data_get_size (Handle);
public IntPtr Data => SkiaApi.sk_data_get_data (Handle);
public void SaveTo (Stream target)
{

Просмотреть файл

@ -4,38 +4,26 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKImage : IDisposable
public class SKImage : SKObject
{
internal IntPtr handle;
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_image_unref (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_image_unref (Handle);
}
}
~SKImage()
{
Dispose (false);
base.Dispose (disposing);
}
internal SKImage (IntPtr x)
: base (x)
{
handle = x;
}
public static SKImage FromPixels (SKImageInfo info, IntPtr pixels, int rowBytes)
@ -51,7 +39,7 @@ namespace SkiaSharp
if (data == null)
throw new ArgumentNullException ("data");
var handle = SkiaApi.sk_image_new_from_encoded (data.handle, ref subset);
var handle = SkiaApi.sk_image_new_from_encoded (data.Handle, ref subset);
if (handle == IntPtr.Zero)
return null;
return new SKImage (handle);
@ -59,12 +47,12 @@ namespace SkiaSharp
public SKData Encode ()
{
return new SKData (SkiaApi.sk_image_encode (handle));
return new SKData (SkiaApi.sk_image_encode (Handle));
}
public int Width => SkiaApi.sk_image_get_width (handle);
public int Height => SkiaApi.sk_image_get_height (handle);
public uint UniqueId => SkiaApi.sk_image_get_unique_id (handle);
public int Width => SkiaApi.sk_image_get_width (Handle);
public int Height => SkiaApi.sk_image_get_height (Handle);
public uint UniqueId => SkiaApi.sk_image_get_unique_id (Handle);
}
}

Просмотреть файл

@ -4,105 +4,93 @@
// Author:
// Matthew Leibowitz
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKImageDecoder : IDisposable
public class SKImageDecoder : SKObject
{
internal IntPtr handle;
public SKImageDecoder(SKStreamRewindable stream)
: this(SkiaApi.sk_imagedecoder_factory(stream.handle))
: this(SkiaApi.sk_imagedecoder_factory(stream.Handle))
{
}
internal SKImageDecoder(IntPtr handle)
: base(handle)
{
this.handle = handle;
}
public void Dispose()
protected override void Dispose(bool disposing)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (handle != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
SkiaApi.sk_imagedecoder_destructor(handle);
handle = IntPtr.Zero;
SkiaApi.sk_imagedecoder_destructor(Handle);
}
}
~SKImageDecoder()
{
Dispose(false);
base.Dispose(disposing);
}
public SKImageDecoderFormat Format
{
get { return SkiaApi.sk_imagedecoder_get_decoder_format(handle); }
get { return SkiaApi.sk_imagedecoder_get_decoder_format(Handle); }
}
public string FormatName
{
get { return SkiaApi.sk_imagedecoder_get_format_name_from_decoder(handle); }
get { return SkiaApi.sk_imagedecoder_get_format_name_from_decoder(Handle); }
}
public bool SkipWritingZeros
{
get { return SkiaApi.sk_imagedecoder_get_skip_writing_zeros(handle); }
set { SkiaApi.sk_imagedecoder_set_skip_writing_zeros(handle, value); }
get { return SkiaApi.sk_imagedecoder_get_skip_writing_zeros(Handle); }
set { SkiaApi.sk_imagedecoder_set_skip_writing_zeros(Handle, value); }
}
public bool DitherImage
{
get { return SkiaApi.sk_imagedecoder_get_dither_image(handle); }
set { SkiaApi.sk_imagedecoder_set_dither_image(handle, value); }
get { return SkiaApi.sk_imagedecoder_get_dither_image(Handle); }
set { SkiaApi.sk_imagedecoder_set_dither_image(Handle, value); }
}
public bool PreferQualityOverSpeed
{
get { return SkiaApi.sk_imagedecoder_get_prefer_quality_over_speed(handle); }
set { SkiaApi.sk_imagedecoder_set_prefer_quality_over_speed(handle, value); }
get { return SkiaApi.sk_imagedecoder_get_prefer_quality_over_speed(Handle); }
set { SkiaApi.sk_imagedecoder_set_prefer_quality_over_speed(Handle, value); }
}
public bool RequireUnpremultipliedColors
{
get { return SkiaApi.sk_imagedecoder_get_require_unpremultiplied_colors(handle); }
set { SkiaApi.sk_imagedecoder_set_require_unpremultiplied_colors(handle, value); }
get { return SkiaApi.sk_imagedecoder_get_require_unpremultiplied_colors(Handle); }
set { SkiaApi.sk_imagedecoder_set_require_unpremultiplied_colors(Handle, value); }
}
public int SampleSize
{
get { return SkiaApi.sk_imagedecoder_get_sample_size(handle); }
set { SkiaApi.sk_imagedecoder_set_sample_size(handle, value); }
get { return SkiaApi.sk_imagedecoder_get_sample_size(Handle); }
set { SkiaApi.sk_imagedecoder_set_sample_size(Handle, value); }
}
public bool ShouldCancelDecode
{
get { return SkiaApi.sk_imagedecoder_should_cancel_decode(handle); }
get { return SkiaApi.sk_imagedecoder_should_cancel_decode(Handle); }
}
public void CancelDecode()
{
SkiaApi.sk_imagedecoder_cancel_decode(handle);
SkiaApi.sk_imagedecoder_cancel_decode(Handle);
}
public SKImageDecoderResult Decode(SKStream stream, SKBitmap bitmap, SKColorType pref = SKColorType.Unknown, SKImageDecoderMode mode = SKImageDecoderMode.DecodePixels)
{
return SkiaApi.sk_imagedecoder_decode(handle, stream.handle, bitmap.handle, pref, mode);
return SkiaApi.sk_imagedecoder_decode(Handle, stream.Handle, bitmap.Handle, pref, mode);
}
public static SKImageDecoderFormat GetFormat(SKStreamRewindable stream)
{
return SkiaApi.sk_imagedecoder_get_stream_format(stream.handle);
return SkiaApi.sk_imagedecoder_get_stream_format(stream.Handle);
}
public static string GetFormatName(SKImageDecoderFormat format)
@ -178,7 +166,7 @@ namespace SkiaSharp
public static bool DecodeStream(SKStreamRewindable stream, SKBitmap bitmap, SKColorType pref, SKImageDecoderMode mode, ref SKImageDecoderFormat format)
{
return SkiaApi.sk_imagedecoder_decode_stream(stream.handle, bitmap.handle, pref, mode, ref format);
return SkiaApi.sk_imagedecoder_decode_stream(stream.Handle, bitmap.Handle, pref, mode, ref format);
}
public static bool DecodeFile(string filename, SKBitmap bitmap, SKColorType pref = SKColorType.Unknown, SKImageDecoderMode mode = SKImageDecoderMode.DecodePixels)
@ -189,7 +177,7 @@ namespace SkiaSharp
public static bool DecodeFile(string filename, SKBitmap bitmap, SKColorType pref, SKImageDecoderMode mode, ref SKImageDecoderFormat format)
{
return SkiaApi.sk_imagedecoder_decode_file(filename, bitmap.handle, pref, mode, ref format);
return SkiaApi.sk_imagedecoder_decode_file(filename, bitmap.Handle, pref, mode, ref format);
}
public static bool DecodeMemory(byte[] buffer, SKBitmap bitmap, SKColorType pref = SKColorType.Unknown, SKImageDecoderMode mode = SKImageDecoderMode.DecodePixels)
@ -200,7 +188,7 @@ namespace SkiaSharp
public static bool DecodeMemory(byte[] buffer, SKBitmap bitmap, SKColorType pref, SKImageDecoderMode mode, ref SKImageDecoderFormat format)
{
return SkiaApi.sk_imagedecoder_decode_memory(buffer, (IntPtr)buffer.Length, bitmap.handle, pref, mode, ref format);
return SkiaApi.sk_imagedecoder_decode_memory(buffer, (IntPtr)buffer.Length, bitmap.Handle, pref, mode, ref format);
}
}
}

Просмотреть файл

@ -4,61 +4,49 @@
// Author:
// Matthew Leibowitz
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKImageFilter : IDisposable
public class SKImageFilter : SKObject
{
internal IntPtr handle;
internal SKImageFilter(IntPtr handle)
: base(handle)
{
this.handle = handle;
}
public void Dispose()
protected override void Dispose(bool disposing)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (handle != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
SkiaApi.sk_imagefilter_unref(handle);
handle = IntPtr.Zero;
SkiaApi.sk_imagefilter_unref(Handle);
}
}
~SKImageFilter()
{
Dispose(false);
base.Dispose(disposing);
}
public static SKImageFilter CreateMatrix(SKMatrix matrix, SKFilterQuality quality, SKImageFilter input = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_matrix(ref matrix, quality, input == null ? IntPtr.Zero : input.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_matrix(ref matrix, quality, input == null ? IntPtr.Zero : input.Handle));
}
public static SKImageFilter CreateAlphaThreshold(SKRectI region, float innerThreshold, float outerThreshold, SKImageFilter input = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_alpha_threshold(ref region, innerThreshold, outerThreshold, input == null ? IntPtr.Zero : input.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_alpha_threshold(ref region, innerThreshold, outerThreshold, input == null ? IntPtr.Zero : input.Handle));
}
public static SKImageFilter CreateBlur(float sigmaX, float sigmaY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_blur(sigmaX, sigmaY, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_blur(sigmaX, sigmaY, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateColorFilter(SKColorFilter cf, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
if (cf == null)
throw new ArgumentNullException("cf");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_color_filter(cf.handle, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_color_filter(cf.Handle, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateCompose(SKImageFilter outer, SKImageFilter inner)
@ -67,59 +55,59 @@ namespace SkiaSharp
throw new ArgumentNullException("outer");
if (inner == null)
throw new ArgumentNullException("inner");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_compose(outer.handle, inner.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_compose(outer.Handle, inner.Handle));
}
public static SKImageFilter CreateCompose(SKDisplacementMapEffectChannelSelectorType xChannelSelector, SKDisplacementMapEffectChannelSelectorType yChannelSelector, float scale, SKImageFilter displacement, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
if (displacement == null)
throw new ArgumentNullException("displacement");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_displacement_map_effect(xChannelSelector, yChannelSelector, scale, displacement.handle, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_displacement_map_effect(xChannelSelector, yChannelSelector, scale, displacement.Handle, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateDownSample(float scale, SKImageFilter input = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_downsample(scale, input == null ? IntPtr.Zero : input.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_downsample(scale, input == null ? IntPtr.Zero : input.Handle));
}
public static SKImageFilter CreateDownSample(float dx, float dy, float sigmaX, float sigmaY, SKColor color, SKDropShadowImageFilterShadowMode shadowMode, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_drop_shadow(dx, dy, sigmaX, sigmaY, color, shadowMode, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_drop_shadow(dx, dy, sigmaX, sigmaY, color, shadowMode, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateDistantLitDiffuse(SKPoint3 direction, SKColor lightColor, float surfaceScale, float kd, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_distant_lit_diffuse(ref direction, lightColor, surfaceScale, kd, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_distant_lit_diffuse(ref direction, lightColor, surfaceScale, kd, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreatePointLitDiffuse(SKPoint3 location, SKColor lightColor, float surfaceScale, float kd, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_point_lit_diffuse(ref location, lightColor, surfaceScale, kd, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_point_lit_diffuse(ref location, lightColor, surfaceScale, kd, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateSpotLitDiffuse(SKPoint3 location, SKPoint3 target, float specularExponent, float cutoffAngle, SKColor lightColor, float surfaceScale, float kd, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_spot_lit_diffuse(ref location, ref target, specularExponent, cutoffAngle, lightColor, surfaceScale, kd, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_spot_lit_diffuse(ref location, ref target, specularExponent, cutoffAngle, lightColor, surfaceScale, kd, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateDistantLitSpecular(SKPoint3 direction, SKColor lightColor, float surfaceScale, float ks, float shininess, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_distant_lit_specular(ref direction, lightColor, surfaceScale, ks, shininess, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_distant_lit_specular(ref direction, lightColor, surfaceScale, ks, shininess, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreatePointLitSpecular(SKPoint3 location, SKColor lightColor, float surfaceScale, float ks, float shininess, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_point_lit_specular(ref location, lightColor, surfaceScale, ks, shininess, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_point_lit_specular(ref location, lightColor, surfaceScale, ks, shininess, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateSpotLitSpecular(SKPoint3 location, SKPoint3 target, float specularExponent, float cutoffAngle, SKColor lightColor, float surfaceScale, float ks, float shininess, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_spot_lit_specular(ref location, ref target, specularExponent, cutoffAngle, lightColor, surfaceScale, ks, shininess, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_spot_lit_specular(ref location, ref target, specularExponent, cutoffAngle, lightColor, surfaceScale, ks, shininess, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateMagnifier(SKRect src, float inset, SKImageFilter input = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_magnifier(ref src, inset, input == null ? IntPtr.Zero : input.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_magnifier(ref src, inset, input == null ? IntPtr.Zero : input.Handle));
}
public static SKImageFilter CreateMatrixConvolution(SKSizeI kernelSize, float[] kernel, float gain, float bias, SKPointI kernelOffset, SKMatrixConvolutionTileMode tileMode, bool convolveAlpha, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
@ -128,7 +116,7 @@ namespace SkiaSharp
throw new ArgumentNullException("kernel");
if (kernel.Length != kernelSize.Width * kernelSize.Height)
throw new ArgumentException("Kernel length must match the dimensions of the kernel size (Width * Height).", "kernel");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_matrix_convolution(ref kernelSize, kernel, gain, bias, ref kernelOffset, tileMode, convolveAlpha, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_matrix_convolution(ref kernelSize, kernel, gain, bias, ref kernelOffset, tileMode, convolveAlpha, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateMerge(SKImageFilter[] filters, SKXferMode[] modes = null, SKImageFilter.CropRect cropRect = null)
@ -140,61 +128,59 @@ namespace SkiaSharp
var f = new IntPtr[filters.Length];
for (int i = 0; i < filters.Length; i++)
{
f[i] = filters[i].handle;
f[i] = filters[i].Handle;
}
return new SKImageFilter(SkiaApi.sk_imagefilter_new_merge(f, filters.Length, modes, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_merge(f, filters.Length, modes, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateDilate(int radiusX, int radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_dilate(radiusX, radiusY, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_dilate(radiusX, radiusY, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateErode(int radiusX, int radiusY, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_erode(radiusX, radiusY, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_erode(radiusX, radiusY, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreateOffset(float dx, float dy, SKImageFilter input = null, SKImageFilter.CropRect cropRect = null)
{
return new SKImageFilter(SkiaApi.sk_imagefilter_new_offset(dx, dy, input == null ? IntPtr.Zero : input.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_offset(dx, dy, input == null ? IntPtr.Zero : input.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public static SKImageFilter CreatePicture(SKPicture picture)
{
if (picture == null)
throw new ArgumentNullException("picture");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_picture(picture.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_picture(picture.Handle));
}
public static SKImageFilter CreatePicture(SKPicture picture, SKRect cropRect)
{
if (picture == null)
throw new ArgumentNullException("picture");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_picture_with_croprect(picture.handle, ref cropRect));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_picture_with_croprect(picture.Handle, ref cropRect));
}
public static SKImageFilter CreatePictureForLocalspace(SKPicture picture, SKRect cropRect, SKFilterQuality filterQuality)
{
if (picture == null)
throw new ArgumentNullException("picture");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_picture_for_localspace(picture.handle, ref cropRect, filterQuality));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_picture_for_localspace(picture.Handle, ref cropRect, filterQuality));
}
public static SKImageFilter CreateRectShader(SKShader shader, SKImageFilter.CropRect cropRect = null)
{
if (shader == null)
throw new ArgumentNullException("shader");
return new SKImageFilter(SkiaApi.sk_imagefilter_new_rect_shader(shader.handle, cropRect == null ? IntPtr.Zero : cropRect.handle));
return new SKImageFilter(SkiaApi.sk_imagefilter_new_rect_shader(shader.Handle, cropRect == null ? IntPtr.Zero : cropRect.Handle));
}
public class CropRect : IDisposable
public class CropRect : SKObject
{
internal IntPtr handle;
internal CropRect(IntPtr handle)
: base(handle)
{
this.handle = handle;
}
public CropRect()
@ -206,40 +192,28 @@ namespace SkiaSharp
: this(SkiaApi.sk_imagefilter_croprect_new_with_rect(ref rect, flags))
{
}
public void Dispose()
protected override void Dispose(bool disposing)
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (handle != IntPtr.Zero)
if (Handle != IntPtr.Zero)
{
SkiaApi.sk_imagefilter_croprect_destructor(handle);
handle = IntPtr.Zero;
SkiaApi.sk_imagefilter_croprect_destructor(Handle);
}
}
~CropRect()
{
Dispose(false);
}
public SKRect Rect
{
get
{
SKRect rect;
SkiaApi.sk_imagefilter_croprect_get_rect(handle, out rect);
SkiaApi.sk_imagefilter_croprect_get_rect(Handle, out rect);
return rect;
}
}
public SKCropRectFlags Flags
{
get { return SkiaApi.sk_imagefilter_croprect_get_flags(handle); }
get { return SkiaApi.sk_imagefilter_croprect_get_flags(Handle); }
}
}
}

Просмотреть файл

@ -4,7 +4,7 @@
// Author:
// Matthew Leibowitz
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
using System.Runtime.InteropServices;
@ -86,9 +86,9 @@ namespace SkiaSharp
}
public SKManagedStream (Stream managedStream, bool disposeManagedStream)
: base (SkiaApi.sk_managedstream_new (), true)
: base (SkiaApi.sk_managedstream_new ())
{
managedStreams.Add (handle, new WeakReference<SKManagedStream>(this));
managedStreams.Add (Handle, new WeakReference<SKManagedStream>(this));
stream = managedStream;
disposeStream = disposeManagedStream;
@ -96,8 +96,8 @@ namespace SkiaSharp
protected override void Dispose (bool disposing)
{
if (managedStreams.ContainsKey(handle)) {
managedStreams.Remove (handle);
if (managedStreams.ContainsKey(Handle)) {
managedStreams.Remove (Handle);
}
if (disposeStream && stream != null) {
@ -181,7 +181,7 @@ namespace SkiaSharp
{
var managedStream = AsManagedStream (managedStreamPtr);
var newStream = new SKManagedStream (managedStream.stream);
return newStream.handle;
return newStream.Handle;
}
#if __IOS__
[MonoPInvokeCallback(typeof(destroy_delegate))]

Просмотреть файл

@ -4,42 +4,30 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKMaskFilter : IDisposable
public class SKMaskFilter : SKObject
{
private const float BlurSigmaScale = 0.57735f;
internal IntPtr handle;
internal SKMaskFilter (IntPtr handle)
: base (handle)
{
this.handle = handle;
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_maskfilter_unref (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_maskfilter_unref (Handle);
}
}
~SKMaskFilter()
{
Dispose (false);
base.Dispose (disposing);
}
public static float ConvertRadiusToSigma(float radius)
{
return radius > 0 ? BlurSigmaScale * radius + 0.5f : 0.0f;

155
binding/Binding/SKObject.cs Normal file
Просмотреть файл

@ -0,0 +1,155 @@
//
// Bindings for SKBitmap
//
// Author:
// Matthew Leibowitz
//
// Copyright 2016 Xamarin Inc
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace SkiaSharp
{
public abstract class SKObject : IDisposable
{
private static readonly Dictionary<IntPtr, WeakReference> instances = new Dictionary<IntPtr, WeakReference>();
private IntPtr handle;
internal SKObject(IntPtr handle)
{
Handle = handle;
}
~SKObject()
{
var h = handle;
Dispose(false);
DeregisterHandle(h, this);
}
public IntPtr Handle
{
get { return handle; }
protected set
{
handle = value;
RegisterHandle(handle, this);
}
}
public void Dispose()
{
var h = handle;
Dispose(true);
if (h != IntPtr.Zero)
{
DeregisterHandle(h, this);
handle = IntPtr.Zero;
}
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
internal static TSkiaObject GetObject<TSkiaObject>(IntPtr handle)
where TSkiaObject : SKObject
{
if (handle == IntPtr.Zero)
{
return null;
}
lock (instances)
{
// find any existing managed werappers
WeakReference reference;
if (instances.TryGetValue(handle, out reference))
{
var instance = reference.Target as TSkiaObject;
if (instance != null && instance.handle != IntPtr.Zero)
{
return instance;
}
}
}
// create a new wrapper
// TODO: we could probably cache this
var type = typeof(TSkiaObject);
var binding = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var constructor = type.GetTypeInfo().GetConstructor(binding, null, new[] { typeof(IntPtr) }, null);
if (constructor == null)
{
throw new MissingMethodException("No constructor found for " + type.FullName + ".ctor(System.IntPtr)");
}
return (TSkiaObject)constructor.Invoke(new object[] { handle });
}
internal static void RegisterHandle(IntPtr handle, SKObject instance)
{
if (handle == IntPtr.Zero)
{
return;
}
lock (instances)
{
// find old references
WeakReference reference;
if (instances.TryGetValue(handle, out reference))
{
var shouldReplace =
reference == null ||
reference.Target != null ||
((SKObject)reference.Target).Handle == IntPtr.Zero;
Debug.WriteLineIf(!shouldReplace, "Not replacing existing, living, managed instance with new object.");
// replace the old one if it is dead
instances[handle] = new WeakReference(instance);
}
else
{
// add a new reference
instances.Add(handle, new WeakReference(instance));
}
}
}
internal static void DeregisterHandle(IntPtr handle, SKObject instance)
{
if (handle == IntPtr.Zero)
{
return;
}
lock (instances)
{
// find any references
WeakReference reference;
if (instances.TryGetValue(handle, out reference))
{
var shouldRemove =
reference == null ||
reference.Target == null ||
reference.Target == instance;
// remove it if it is dead or the correct object
instances.Remove(handle);
}
}
}
}
}

Просмотреть файл

@ -4,199 +4,192 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKPaint : IDisposable
public class SKPaint : SKObject
{
internal IntPtr handle;
internal SKPaint (IntPtr handle)
: base (handle)
{
}
public SKPaint ()
: this (SkiaApi.sk_paint_new ())
{
handle = SkiaApi.sk_paint_new ();
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_paint_delete (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_paint_delete (Handle);
}
}
~SKPaint()
{
Dispose (false);
base.Dispose (disposing);
}
public bool IsAntialias {
get {
return SkiaApi.sk_paint_is_antialias (handle);
return SkiaApi.sk_paint_is_antialias (Handle);
}
set {
SkiaApi.sk_paint_set_antialias (handle, value);
SkiaApi.sk_paint_set_antialias (Handle, value);
}
}
public bool IsStroke {
get {
return SkiaApi.sk_paint_is_stroke (handle);
return SkiaApi.sk_paint_is_stroke (Handle);
}
set {
SkiaApi.sk_paint_set_stroke (handle, value);
SkiaApi.sk_paint_set_stroke (Handle, value);
}
}
public SKColor Color {
get {
return SkiaApi.sk_paint_get_color (handle);
return SkiaApi.sk_paint_get_color (Handle);
}
set {
SkiaApi.sk_paint_set_color (handle, value);
SkiaApi.sk_paint_set_color (Handle, value);
}
}
public float StrokeWidth {
get {
return SkiaApi.sk_paint_get_stroke_width (handle);
return SkiaApi.sk_paint_get_stroke_width (Handle);
}
set {
SkiaApi.sk_paint_set_stroke_width (handle, value);
SkiaApi.sk_paint_set_stroke_width (Handle, value);
}
}
public float StrokeMiter {
get {
return SkiaApi.sk_paint_get_stroke_miter (handle);
return SkiaApi.sk_paint_get_stroke_miter (Handle);
}
set {
SkiaApi.sk_paint_set_stroke_miter (handle, value);
SkiaApi.sk_paint_set_stroke_miter (Handle, value);
}
}
public SKStrokeCap StrokeCap {
get {
return SkiaApi.sk_paint_get_stroke_cap (handle);
return SkiaApi.sk_paint_get_stroke_cap (Handle);
}
set {
SkiaApi.sk_paint_set_stroke_cap (handle, value);
SkiaApi.sk_paint_set_stroke_cap (Handle, value);
}
}
public SKStrokeJoin StrokeJoin {
get {
return SkiaApi.sk_paint_get_stroke_join (handle);
return SkiaApi.sk_paint_get_stroke_join (Handle);
}
set {
SkiaApi.sk_paint_set_stroke_join (handle, value);
SkiaApi.sk_paint_set_stroke_join (Handle, value);
}
}
public SKShader Shader {
get {
return new SKShader(SkiaApi.sk_paint_get_shader(handle));
return GetObject<SKShader>(SkiaApi.sk_paint_get_shader(Handle));
}
set {
SkiaApi.sk_paint_set_shader(handle, value == null ? IntPtr.Zero : value.handle);
SkiaApi.sk_paint_set_shader(Handle, value == null ? IntPtr.Zero : value.Handle);
}
}
public SKMaskFilter MaskFilter {
get {
return new SKMaskFilter(SkiaApi.sk_paint_get_maskfilter(handle));
return GetObject<SKMaskFilter>(SkiaApi.sk_paint_get_maskfilter(Handle));
}
set {
SkiaApi.sk_paint_set_maskfilter (handle, value == null ? IntPtr.Zero : value.handle);
SkiaApi.sk_paint_set_maskfilter (Handle, value == null ? IntPtr.Zero : value.Handle);
}
}
public SKColorFilter ColorFilter {
get {
return new SKColorFilter(SkiaApi.sk_paint_get_colorfilter(handle));
return GetObject<SKColorFilter>(SkiaApi.sk_paint_get_colorfilter(Handle));
}
set {
SkiaApi.sk_paint_set_colorfilter (handle, value == null ? IntPtr.Zero : value.handle);
SkiaApi.sk_paint_set_colorfilter (Handle, value == null ? IntPtr.Zero : value.Handle);
}
}
public SKImageFilter ImageFilter {
get {
return new SKImageFilter(SkiaApi.sk_paint_get_imagefilter(handle));
return GetObject<SKImageFilter>(SkiaApi.sk_paint_get_imagefilter(Handle));
}
set {
SkiaApi.sk_paint_set_imagefilter(handle, value == null ? IntPtr.Zero : value.handle);
SkiaApi.sk_paint_set_imagefilter(Handle, value == null ? IntPtr.Zero : value.Handle);
}
}
public SKXferMode XferMode {
get {
return SkiaApi.sk_paint_get_xfermode_mode(handle);
return SkiaApi.sk_paint_get_xfermode_mode(Handle);
}
set {
SkiaApi.sk_paint_set_xfermode_mode (handle, value);
SkiaApi.sk_paint_set_xfermode_mode (Handle, value);
}
}
public SKTypeface Typeface {
get {
return new SKTypeface (SkiaApi.sk_paint_get_typeface (handle), owns: false);
return GetObject<SKTypeface> (SkiaApi.sk_paint_get_typeface (Handle));
}
set {
SkiaApi.sk_paint_set_typeface (handle, value == null ? IntPtr.Zero : value.handle);
SkiaApi.sk_paint_set_typeface (Handle, value == null ? IntPtr.Zero : value.Handle);
}
}
public float TextSize {
get {
return SkiaApi.sk_paint_get_textsize (handle);
return SkiaApi.sk_paint_get_textsize (Handle);
}
set {
SkiaApi.sk_paint_set_textsize (handle, value);
SkiaApi.sk_paint_set_textsize (Handle, value);
}
}
public SKTextAlign TextAlign {
get {
return SkiaApi.sk_paint_get_text_align (handle);
return SkiaApi.sk_paint_get_text_align (Handle);
}
set {
SkiaApi.sk_paint_set_text_align (handle, value);
SkiaApi.sk_paint_set_text_align (Handle, value);
}
}
public SKTextEncoding TextEncoding {
get {
return SkiaApi.sk_paint_get_text_encoding (handle);
return SkiaApi.sk_paint_get_text_encoding (Handle);
}
set {
SkiaApi.sk_paint_set_text_encoding (handle, value);
SkiaApi.sk_paint_set_text_encoding (Handle, value);
}
}
public float TextScaleX {
get {
return SkiaApi.sk_paint_get_text_scale_x (handle);
return SkiaApi.sk_paint_get_text_scale_x (Handle);
}
set {
SkiaApi.sk_paint_set_text_scale_x (handle, value);
SkiaApi.sk_paint_set_text_scale_x (Handle, value);
}
}
public float TextSkewX {
get {
return SkiaApi.sk_paint_get_text_skew_x (handle);
return SkiaApi.sk_paint_get_text_skew_x (Handle);
}
set {
SkiaApi.sk_paint_set_text_skew_x (handle, value);
SkiaApi.sk_paint_set_text_skew_x (Handle, value);
}
}

Просмотреть файл

@ -4,83 +4,76 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKPath : IDisposable
public class SKPath : SKObject
{
internal IntPtr handle;
internal SKPath (IntPtr handle)
: base (handle)
{
}
public SKPath ()
: this (SkiaApi.sk_path_new ())
{
handle = SkiaApi.sk_path_new ();
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_path_delete (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_path_delete (Handle);
}
}
~SKPath()
{
Dispose (false);
base.Dispose (disposing);
}
public void MoveTo (float x, float y)
{
SkiaApi.sk_path_move_to (handle, x, y);
SkiaApi.sk_path_move_to (Handle, x, y);
}
public void LineTo (float x, float y)
{
SkiaApi.sk_path_line_to (handle, x, y);
SkiaApi.sk_path_line_to (Handle, x, y);
}
public void QuadTo (float x0, float y0, float x1, float y1)
{
SkiaApi.sk_path_quad_to (handle, x0, y0, x1, y1);
SkiaApi.sk_path_quad_to (Handle, x0, y0, x1, y1);
}
public void ConicTo (float x0, float y0, float x1, float y1, float w)
{
SkiaApi.sk_path_conic_to (handle, x0, y0, x1, y1, w);
SkiaApi.sk_path_conic_to (Handle, x0, y0, x1, y1, w);
}
public void CubicTo (float x0, float y0, float x1, float y1, float x2, float y2)
{
SkiaApi.sk_path_cubic_to (handle, x0, y0, x1, y1, x2, y2);
SkiaApi.sk_path_cubic_to (Handle, x0, y0, x1, y1, x2, y2);
}
public void Close ()
{
SkiaApi.sk_path_close (handle);
SkiaApi.sk_path_close (Handle);
}
public void AddRect (SKRect rect, SKPathDirection direction)
{
SkiaApi.sk_path_add_rect (handle, ref rect, direction);
SkiaApi.sk_path_add_rect (Handle, ref rect, direction);
}
public void AddOval (SKRect rect, SKPathDirection direction)
{
SkiaApi.sk_path_add_oval (handle, ref rect, direction);
SkiaApi.sk_path_add_oval (Handle, ref rect, direction);
}
public bool GetBounds (out SKRect rect)
{
return SkiaApi.sk_path_get_bounds (handle, out rect);
return SkiaApi.sk_path_get_bounds (Handle, out rect);
}
}
}

Просмотреть файл

@ -4,43 +4,31 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKPicture : IDisposable
public class SKPicture : SKObject
{
internal IntPtr handle;
internal SKPicture (IntPtr h)
: base (h)
{
handle = h;
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_picture_unref (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_picture_unref (Handle);
}
}
~SKPicture()
{
Dispose (false);
base.Dispose (disposing);
}
public uint UniqueId => SkiaApi.sk_picture_get_unique_id (handle);
public SKRect Bounds => SkiaApi.sk_picture_get_bounds (handle);
public uint UniqueId => SkiaApi.sk_picture_get_unique_id (Handle);
public SKRect Bounds => SkiaApi.sk_picture_get_bounds (Handle);
}
}

Просмотреть файл

@ -4,49 +4,42 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKPictureRecorder : IDisposable
{
internal IntPtr handle;
public void Dispose ()
public class SKPictureRecorder : SKObject
{
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_picture_recorder_delete (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_picture_recorder_delete (Handle);
}
}
~SKPictureRecorder()
base.Dispose (disposing);
}
public SKPictureRecorder (IntPtr handle)
: base (handle)
{
Dispose (false);
}
public SKPictureRecorder ()
{
handle = SkiaApi.sk_picture_recorder_new ();
: this (SkiaApi.sk_picture_recorder_new ())
{
}
public SKCanvas BeginRecording (SKRect rect)
{
return new SKCanvas (SkiaApi.sk_picture_recorder_begin_recording (handle, ref rect));
return GetObject<SKCanvas> (SkiaApi.sk_picture_recorder_begin_recording (Handle, ref rect));
}
public SKPicture EndRecording ()
{
return new SKPicture (SkiaApi.sk_picture_recorder_end_recording (handle));
return GetObject<SKPicture> (SkiaApi.sk_picture_recorder_end_recording (Handle));
}
}
}

Просмотреть файл

@ -4,39 +4,26 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKShader : IDisposable
public class SKShader : SKObject
{
internal IntPtr handle;
internal SKShader (IntPtr handle)
: base (handle)
{
this.handle = handle;
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_shader_unref (handle);
// We set this in case the user tries to use the fetched Canvas (which depends on us) to perform some operations
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_shader_unref (Handle);
}
}
~SKShader()
{
Dispose (false);
base.Dispose (disposing);
}
public static SKShader CreateEmpty ()
@ -51,22 +38,22 @@ namespace SkiaSharp
public static SKShader CreateBitmap (SKBitmap src, SKShaderTileMode tmx, SKShaderTileMode tmy)
{
return new SKShader (SkiaApi.sk_shader_new_bitmap (src.handle, tmx, tmy, IntPtr.Zero));
return new SKShader (SkiaApi.sk_shader_new_bitmap (src.Handle, tmx, tmy, IntPtr.Zero));
}
public static SKShader CreateBitmap (SKBitmap src, SKShaderTileMode tmx, SKShaderTileMode tmy, SKMatrix localMatrix)
{
return new SKShader (SkiaApi.sk_shader_new_bitmap (src.handle, tmx, tmy, ref localMatrix));
return new SKShader (SkiaApi.sk_shader_new_bitmap (src.Handle, tmx, tmy, ref localMatrix));
}
public static SKShader CreateLocalMatrix (SKShader shader)
{
return new SKShader (SkiaApi.sk_shader_new_local_matrix (shader.handle, IntPtr.Zero));
return new SKShader (SkiaApi.sk_shader_new_local_matrix (shader.Handle, IntPtr.Zero));
}
public static SKShader CreateLocalMatrix (SKShader shader, SKMatrix localMatrix)
{
return new SKShader (SkiaApi.sk_shader_new_local_matrix (shader.handle, ref localMatrix));
return new SKShader (SkiaApi.sk_shader_new_local_matrix (shader.Handle, ref localMatrix));
}
public static SKShader CreateLinearGradient (SKPoint start, SKPoint end, SKColor [] colors, float [] colorPos, SKShaderTileMode mode)
@ -131,12 +118,12 @@ namespace SkiaSharp
public static SKShader CreateCompose(SKShader shaderA, SKShader shaderB)
{
return new SKShader(SkiaApi.sk_shader_new_compose(shaderA.handle, shaderB.handle));
return new SKShader(SkiaApi.sk_shader_new_compose(shaderA.Handle, shaderB.Handle));
}
public static SKShader CreateCompose(SKShader shaderA, SKShader shaderB, SKXferMode mode)
{
return new SKShader(SkiaApi.sk_shader_new_compose_with_mode(shaderA.handle, shaderB.handle, mode));
return new SKShader(SkiaApi.sk_shader_new_compose_with_mode(shaderA.Handle, shaderB.Handle, mode));
}
}
}

Просмотреть файл

@ -4,108 +4,140 @@
// Author:
// Matthew Leibowitz
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public abstract class SKStream : IDisposable
public abstract class SKStream : SKObject
{
internal IntPtr handle;
internal bool owns;
internal SKStream (IntPtr handle, bool owns)
internal SKStream (IntPtr handle)
: base (handle)
{
this.owns = owns;
this.handle = handle;
}
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
if (owns) {
}
handle = IntPtr.Zero;
}
}
~SKStream ()
{
Dispose (false);
}
public bool IsAtEnd {
get {
return SkiaApi.sk_stream_is_at_end (handle);
return SkiaApi.sk_stream_is_at_end (Handle);
}
}
public SByte ReadSByte ()
{
return SkiaApi.sk_stream_read_s8 (handle);
return SkiaApi.sk_stream_read_s8 (Handle);
}
public Int16 ReadInt16 ()
{
return SkiaApi.sk_stream_read_s16 (handle);
return SkiaApi.sk_stream_read_s16 (Handle);
}
public Int32 ReadInt32 ()
{
return SkiaApi.sk_stream_read_s32 (handle);
return SkiaApi.sk_stream_read_s32 (Handle);
}
public Byte ReadByte ()
{
return SkiaApi.sk_stream_read_u8 (handle);
return SkiaApi.sk_stream_read_u8 (Handle);
}
public UInt16 ReadUInt16 ()
{
return SkiaApi.sk_stream_read_u16 (handle);
return SkiaApi.sk_stream_read_u16 (Handle);
}
public UInt32 ReadUInt32 ()
{
return SkiaApi.sk_stream_read_u32 (handle);
return SkiaApi.sk_stream_read_u32 (Handle);
}
public int Read (byte[] buffer, int size)
{
unsafe {
fixed (byte *b = &buffer [0]) {
return (int)SkiaApi.sk_stream_read (Handle, (IntPtr) b, (IntPtr)size);
}
}
}
public int Skip (int size)
{
return (int)SkiaApi.sk_stream_skip (Handle, (IntPtr)size);
}
public bool Rewind ()
{
return SkiaApi.sk_stream_rewind (Handle);
}
public bool Seek (int position)
{
return SkiaApi.sk_stream_seek (Handle, (IntPtr)position);
}
public bool Move (long offset)
{
return SkiaApi.sk_stream_move (Handle, offset);
}
public bool HasPosition {
get {
return SkiaApi.sk_stream_has_position (Handle);
}
}
public int Position {
get {
return (int)SkiaApi.sk_stream_get_position (Handle);
}
set {
Seek (value);
}
}
public bool HasLength {
get {
return SkiaApi.sk_stream_has_length (Handle);
}
}
public int Length {
get {
return (int)SkiaApi.sk_stream_get_length (Handle);
}
}
}
public abstract class SKStreamRewindable : SKStream
{
internal SKStreamRewindable (IntPtr handle, bool owns)
: base (handle, owns)
internal SKStreamRewindable (IntPtr handle)
: base (handle)
{
}
}
public abstract class SKStreamSeekable : SKStreamRewindable
{
internal SKStreamSeekable (IntPtr handle, bool owns)
: base (handle, owns)
internal SKStreamSeekable (IntPtr handle)
: base (handle)
{
}
}
public abstract class SKStreamAsset : SKStreamSeekable
{
internal SKStreamAsset (IntPtr handle, bool owns)
: base (handle, owns)
internal SKStreamAsset (IntPtr handle)
: base (handle)
{
}
}
public abstract class SKStreamMemory : SKStreamAsset
{
internal SKStreamMemory (IntPtr handle, bool owns)
: base (handle, owns)
internal SKStreamMemory (IntPtr handle)
: base (handle)
{
}
}
@ -113,30 +145,35 @@ namespace SkiaSharp
public class SKFileStream : SKStreamAsset
{
public SKFileStream (string path)
: base (SkiaApi.sk_filestream_new (path), true)
: base (SkiaApi.sk_filestream_new (path))
{
}
}
public class SKMemoryStream : SKStreamMemory
{
internal SKMemoryStream(IntPtr handle)
: base (handle)
{
}
public SKMemoryStream ()
: base (SkiaApi.sk_memorystream_new (), true)
: this (SkiaApi.sk_memorystream_new ())
{
}
public SKMemoryStream (ulong length)
: base (SkiaApi.sk_memorystream_new_with_length ((IntPtr)length), true)
: this(SkiaApi.sk_memorystream_new_with_length ((IntPtr)length))
{
}
internal SKMemoryStream (IntPtr data, IntPtr length, bool copyData = false)
: base (SkiaApi.sk_memorystream_new_with_data (data, length, copyData), true)
: this(SkiaApi.sk_memorystream_new_with_data (data, length, copyData))
{
}
public SKMemoryStream (SKData data)
: base (SkiaApi.sk_memorystream_new_with_skdata (data), true)
: this(SkiaApi.sk_memorystream_new_with_skdata (data))
{
}
@ -148,12 +185,12 @@ namespace SkiaSharp
internal void SetMemory (IntPtr data, IntPtr length, bool copyData = false)
{
SkiaApi.sk_memorystream_set_memory (handle, data, length, copyData);
SkiaApi.sk_memorystream_set_memory (Handle, data, length, copyData);
}
internal void SetMemory (byte[] data, IntPtr length, bool copyData = false)
{
SkiaApi.sk_memorystream_set_memory (handle, data, length, copyData);
SkiaApi.sk_memorystream_set_memory (Handle, data, length, copyData);
}
public void SetMemory (byte[] data)

Просмотреть файл

@ -4,87 +4,63 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKSurface : IDisposable
public class SKSurface : SKObject
{
internal IntPtr handle;
public static SKSurface Create (int width, int height, SKColorType colorType, SKAlphaType alphaType) => Create (new SKImageInfo (width, height, colorType, alphaType));
public static SKSurface Create (int width, int height, SKColorType colorType, SKAlphaType alphaType, SKSurfaceProps props) => Create (new SKImageInfo (width, height, colorType, alphaType), props);
public static SKSurface Create (int width, int height, SKColorType colorType, SKAlphaType alphaType, IntPtr pixels, int rowBytes) => Create (new SKImageInfo (width, height, colorType, alphaType), pixels, rowBytes);
public static SKSurface Create (int width, int height, SKColorType colorType, SKAlphaType alphaType, IntPtr pixels, int rowBytes, SKSurfaceProps props) => Create (new SKImageInfo (width, height, colorType, alphaType), pixels, rowBytes, props);
SKSurface (IntPtr h)
internal SKSurface (IntPtr h)
: base (h)
{
handle = h;
}
static SKSurface FromHandle (IntPtr h)
{
if (h == IntPtr.Zero)
return null;
return new SKSurface (h);
}
public static SKSurface Create (SKImageInfo info)
{
return FromHandle (SkiaApi.sk_surface_new_raster (ref info, IntPtr.Zero));
return GetObject<SKSurface> (SkiaApi.sk_surface_new_raster (ref info, IntPtr.Zero));
}
public static SKSurface Create (SKImageInfo info, SKSurfaceProps props)
{
return FromHandle (SkiaApi.sk_surface_new_raster (ref info, ref props));
return GetObject<SKSurface> (SkiaApi.sk_surface_new_raster (ref info, ref props));
}
public static SKSurface Create (SKImageInfo info, IntPtr pixels, int rowBytes)
{
return (FromHandle (SkiaApi.sk_surface_new_raster_direct (ref info, pixels, (IntPtr)rowBytes, IntPtr.Zero)));
return GetObject<SKSurface> (SkiaApi.sk_surface_new_raster_direct (ref info, pixels, (IntPtr)rowBytes, IntPtr.Zero));
}
public static SKSurface Create (SKImageInfo info, IntPtr pixels, int rowBytes, SKSurfaceProps props)
{
return (FromHandle (SkiaApi.sk_surface_new_raster_direct (ref info, pixels, (IntPtr)rowBytes, ref props)));
return GetObject<SKSurface> (SkiaApi.sk_surface_new_raster_direct (ref info, pixels, (IntPtr)rowBytes, ref props));
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
SkiaApi.sk_surface_unref (handle);
handle = IntPtr.Zero;
// We set this in case the user tries to use the fetched Canvas (which depends on us) to perform some operations
canvas.handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_surface_unref (Handle);
}
base.Dispose (disposing);
}
~SKSurface()
{
Dispose (false);
}
SKCanvas canvas;
public SKCanvas Canvas {
get {
if (canvas == null)
canvas = new SKCanvas (SkiaApi.sk_surface_get_canvas (handle));
return canvas;
return GetObject<SKCanvas> (SkiaApi.sk_surface_get_canvas (Handle));
}
}
public SKImage Snapshot ()
{
return new SKImage (SkiaApi.sk_surface_new_image_snapshot (handle));
return GetObject<SKImage> (SkiaApi.sk_surface_new_image_snapshot (Handle));
}
}
}

Просмотреть файл

@ -4,69 +4,54 @@
// Author:
// Miguel de Icaza
//
// Copyright 2015 Xamarin Inc
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public class SKTypeface : IDisposable
public class SKTypeface : SKObject
{
internal IntPtr handle;
bool owns;
internal SKTypeface (IntPtr handle, bool owns)
internal SKTypeface (IntPtr handle)
: base (handle)
{
this.owns = owns;
this.handle = handle;
}
public void Dispose ()
protected override void Dispose (bool disposing)
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (handle != IntPtr.Zero) {
if (owns)
SkiaApi.sk_typeface_unref (handle);
handle = IntPtr.Zero;
if (Handle != IntPtr.Zero) {
SkiaApi.sk_typeface_unref (Handle);
}
}
~SKTypeface()
{
Dispose (false);
base.Dispose (disposing);
}
public static SKTypeface FromFamilyName (string familyName, SKTypefaceStyle style = SKTypefaceStyle.Normal)
{
if (familyName == null)
throw new ArgumentNullException ("familyName");
return new SKTypeface (SkiaApi.sk_typeface_create_from_name (familyName, style), owns: true);
return new SKTypeface (SkiaApi.sk_typeface_create_from_name (familyName, style));
}
public static SKTypeface FromTypeface (SKTypeface typeface, SKTypefaceStyle style = SKTypefaceStyle.Normal)
{
if (typeface == null)
throw new ArgumentNullException ("typeface");
return new SKTypeface (SkiaApi.sk_typeface_create_from_typeface (typeface.handle, style), owns: true);
return new SKTypeface (SkiaApi.sk_typeface_create_from_typeface (typeface.Handle, style));
}
public static SKTypeface FromFile (string path, int index = 0)
{
if (path == null)
throw new ArgumentNullException ("path");
return new SKTypeface (SkiaApi.sk_typeface_create_from_file (path, index), owns: true);
return new SKTypeface (SkiaApi.sk_typeface_create_from_file (path, index));
}
public static SKTypeface FromStream (SKStreamAsset stream, int index = 0)
{
if (stream == null)
throw new ArgumentNullException ("stream");
return new SKTypeface (SkiaApi.sk_typeface_create_from_stream (stream.handle, index), owns: true);
return new SKTypeface (SkiaApi.sk_typeface_create_from_stream (stream.Handle, index));
}
public int CountGlyphs (string str)
@ -76,7 +61,7 @@ namespace SkiaSharp
unsafe {
fixed (char *p = str) {
return SkiaApi.sk_typeface_chars_to_glyphs (handle, (IntPtr)p, SKEncoding.Utf16, IntPtr.Zero, str.Length);
return SkiaApi.sk_typeface_chars_to_glyphs (Handle, (IntPtr)p, SKEncoding.Utf16, IntPtr.Zero, str.Length);
}
}
}
@ -86,7 +71,7 @@ namespace SkiaSharp
if (str == IntPtr.Zero)
throw new ArgumentNullException ("str");
return SkiaApi.sk_typeface_chars_to_glyphs (handle, str, encoding, IntPtr.Zero, strLen);
return SkiaApi.sk_typeface_chars_to_glyphs (Handle, str, encoding, IntPtr.Zero, strLen);
}
public int CharsToGlyphs (string chars, out ushort [] glyphs)
@ -97,11 +82,11 @@ namespace SkiaSharp
unsafe {
fixed (char *p = chars){
var n = SkiaApi.sk_typeface_chars_to_glyphs (handle, (IntPtr) p, SKEncoding.Utf16, IntPtr.Zero, chars.Length);
var n = SkiaApi.sk_typeface_chars_to_glyphs (Handle, (IntPtr) p, SKEncoding.Utf16, IntPtr.Zero, chars.Length);
glyphs = new ushort[n];
fixed (ushort *gp = &glyphs [0]){
return SkiaApi.sk_typeface_chars_to_glyphs (handle, (IntPtr) p, SKEncoding.Utf16, (IntPtr) gp, n);
return SkiaApi.sk_typeface_chars_to_glyphs (Handle, (IntPtr) p, SKEncoding.Utf16, (IntPtr) gp, n);
}
}
}
@ -114,11 +99,11 @@ namespace SkiaSharp
unsafe {
var n = SkiaApi.sk_typeface_chars_to_glyphs (handle, str, encoding, IntPtr.Zero, strlen);
var n = SkiaApi.sk_typeface_chars_to_glyphs (Handle, str, encoding, IntPtr.Zero, strlen);
glyphs = new ushort[n];
fixed (ushort *gp = &glyphs [0]){
return SkiaApi.sk_typeface_chars_to_glyphs (handle, str, SKEncoding.Utf16, (IntPtr) gp, n);
return SkiaApi.sk_typeface_chars_to_glyphs (Handle, str, SKEncoding.Utf16, (IntPtr) gp, n);
}
}
}

Просмотреть файл

@ -459,6 +459,10 @@ namespace SkiaSharp
// Streams
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr sk_stream_read(sk_stream_t stream, IntPtr buffer, IntPtr size);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr sk_stream_skip(sk_stream_t stream, IntPtr size);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_is_at_end(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static SByte sk_stream_read_s8(sk_stream_t stream);
@ -475,6 +479,20 @@ namespace SkiaSharp
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_read_bool(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_rewind(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_has_position(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr sk_stream_get_position(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_seek(sk_stream_t stream, IntPtr position);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_move(sk_stream_t stream, long offset);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static bool sk_stream_has_length(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr sk_stream_get_length(sk_stream_t stream);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_stream_filestream_t sk_filestream_new(string path);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_stream_memorystream_t sk_memorystream_new();

Просмотреть файл

@ -1,5 +1,5 @@
/*
* Copyright 2015 Xamarin Inc.
* Copyright 2016 Xamarin Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.

2
skia

@ -1 +1 @@
Subproject commit 5ae3b814705012454c1336e7a814272b46150a2e
Subproject commit 5234c7ad5a1ba72a61bef48bf2dc87cfacba8f6c