Added C# bindings for SKPixmap

This commit is contained in:
Matthew Leibowitz 2017-01-07 01:40:41 +02:00
Родитель af3f21616b
Коммит 0e9b000090
7 изменённых файлов: 171 добавлений и 1 удалений

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

@ -37,6 +37,7 @@
<Compile Include="$(MSBuildThisFileDirectory)SKStream.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKManagedStream.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKBitmap.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKPixmap.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKCodec.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SKRegion.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PreserveAttribute.cs" />

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

@ -460,6 +460,23 @@ namespace SkiaSharp
}
}
public bool InstallPixels (SKPixmap pixmap)
{
return SkiaApi.sk_bitmap_install_pixels_with_pixmap (Handle, pixmap.Handle);
}
public SKPixmap PeekPixels ()
{
SKPixmap pixmap = new SKPixmap ();
var result = SkiaApi.sk_bitmap_peek_pixels (Handle, pixmap.Handle);
if (result) {
return pixmap;
} else {
pixmap.Dispose ();
return null;
}
}
// internal proxy
#if __IOS__
[ObjCRuntime.MonoPInvokeCallback (typeof (SKBitmapReleaseDelegateInternal))]

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

@ -0,0 +1,102 @@
//
// Bindings for SKPixmap
//
// Author:
// Matthew Leibowitz
//
// Copyright 2017 Xamarin Inc
//
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace SkiaSharp
{
public class SKPixmap : SKObject
{
private const string UnableToCreateInstanceMessage = "Unable to create a new SKPixmap instance.";
[Preserve]
internal SKPixmap (IntPtr handle, bool owns)
: base (handle, owns)
{
}
public SKPixmap ()
: this (SkiaApi.sk_pixmap_new (), true)
{
if (Handle == IntPtr.Zero) {
throw new InvalidOperationException (UnableToCreateInstanceMessage);
}
}
public SKPixmap (SKImageInfo info, IntPtr addr, int rowBytes, SKColorTable ctable = null)
: this (SkiaApi.sk_pixmap_new_with_params (ref info, addr, (IntPtr)rowBytes, ctable == null ? IntPtr.Zero : ctable.Handle), true)
{
if (Handle == IntPtr.Zero) {
throw new InvalidOperationException (UnableToCreateInstanceMessage);
}
}
protected override void Dispose (bool disposing)
{
if (Handle != IntPtr.Zero && OwnsHandle) {
SkiaApi.sk_pixmap_destructor (Handle);
}
base.Dispose (disposing);
}
public void Reset ()
{
SkiaApi.sk_pixmap_reset (Handle);
}
public void Reset (SKImageInfo info, IntPtr addr, int rowBytes, SKColorTable ctable = null)
{
SkiaApi.sk_pixmap_reset_with_params (Handle, ref info, addr, (IntPtr)rowBytes, ctable == null ? IntPtr.Zero : ctable.Handle);
}
public SKImageInfo Info {
get {
SKImageInfo info;
SkiaApi.sk_pixmap_get_info (Handle, out info);
return info;
}
}
public int Width {
get { return Info.Width; }
}
public int Height {
get { return Info.Height; }
}
public SKColorType ColorType {
get { return Info.ColorType; }
}
public SKAlphaType AlphaType {
get { return Info.AlphaType; }
}
public int BytesPerPixel {
get { return Info.BytesPerPixel; }
}
public int RowBytes {
get { return (int)SkiaApi.sk_pixmap_get_row_bytes (Handle); }
}
public IntPtr GetPixels ()
{
return SkiaApi.sk_pixmap_get_pixels (Handle);
}
public SKColorTable ColorTable {
get { return GetObject<SKColorTable> (SkiaApi.sk_pixmap_get_colortable (Handle)); }
}
}
}

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

@ -35,6 +35,7 @@ using sk_wstream_t = System.IntPtr;
using sk_wstream_dynamicmemorystream_t = System.IntPtr;
using sk_wstream_filestream_t = System.IntPtr;
using sk_bitmap_t = System.IntPtr;
using sk_pixmap_t = System.IntPtr;
using sk_codec_t = System.IntPtr;
using sk_imagefilter_croprect_t = System.IntPtr;
using sk_imagefilter_t = System.IntPtr;
@ -1129,6 +1130,9 @@ namespace SkiaSharp
public extern static bool sk_bitmap_install_pixels(sk_bitmap_t cbitmap, ref SKImageInfo cinfo, IntPtr pixels, IntPtr rowBytes, sk_colortable_t ctable, IntPtr releaseProc, IntPtr context);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public extern static bool sk_bitmap_install_pixels_with_pixmap(sk_bitmap_t cbitmap, sk_pixmap_t cpixmap);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public extern static bool sk_bitmap_try_alloc_pixels(sk_bitmap_t cbitmap, ref SKImageInfo requestedInfo, IntPtr rowBytes);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
@ -1137,6 +1141,30 @@ namespace SkiaSharp
public extern static sk_colortable_t sk_bitmap_get_colortable(sk_bitmap_t cbitmap);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_bitmap_set_pixels(sk_bitmap_t cbitmap, IntPtr pixels, sk_colortable_t ctable);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public extern static bool sk_bitmap_peek_pixels(sk_bitmap_t cbitmap, sk_pixmap_t cpixmap);
// SkPixmap
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_pixmap_destructor(sk_pixmap_t cpixmap);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_pixmap_t sk_pixmap_new();
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_pixmap_t sk_pixmap_new_with_params(ref SKImageInfo cinfo, IntPtr addr, IntPtr rowBytes, sk_colortable_t ctable);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_pixmap_reset(sk_pixmap_t cpixmap);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_pixmap_reset_with_params(sk_pixmap_t cpixmap, ref SKImageInfo cinfo, IntPtr addr, IntPtr rowBytes, sk_colortable_t ctable);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static void sk_pixmap_get_info(sk_pixmap_t cpixmap, out SKImageInfo cinfo);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr sk_pixmap_get_row_bytes(sk_pixmap_t cpixmap);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr sk_pixmap_get_pixels(sk_pixmap_t cpixmap);
[DllImport(SKIA, CallingConvention = CallingConvention.Cdecl)]
public extern static sk_colortable_t sk_pixmap_get_colortable(sk_pixmap_t cpixmap);
// Matrix

2
externals/skia поставляемый

@ -1 +1 @@
Subproject commit 86a7b2680b89bf3c0a408e2c6fed9a00e7493f51
Subproject commit eeb1435233f45a2cdb053c522fd26fb1e5355a89

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

@ -23,6 +23,7 @@
#include "sk_path.h"
#include "sk_patheffect.h"
#include "sk_picture.h"
#include "sk_pixmap.h"
#include "sk_region.h"
#include "sk_shader.h"
#include "sk_shader.h"
@ -53,6 +54,7 @@ void** KeepSkiaCSymbols ()
(void*)sk_surface_new_raster,
(void*)sk_colortype_get_default_8888,
(void*)sk_bitmap_new,
(void*)sk_pixmap_new,
(void*)sk_canvas_clear,
(void*)sk_colorfilter_unref,
(void*)sk_data_new_from_file,

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

@ -39,5 +39,25 @@ namespace SkiaSharp.Tests
Marshal.FreeCoTaskMem(pixels);
}
[Test]
public void BitmapAndPixmapAreValid()
{
var info = new SKImageInfo(10, 10);
using (var bitmap = new SKBitmap(info)) {
Assert.AreEqual(10, bitmap.Width);
Assert.AreEqual(10, bitmap.Height);
var pixmap = bitmap.PeekPixels();
Assert.IsNotNull(pixmap);
Assert.AreEqual(10, pixmap.Width);
Assert.AreEqual(10, pixmap.Height);
Assert.IsTrue(bitmap.GetPixels() != IntPtr.Zero);
Assert.IsTrue(pixmap.GetPixels() != IntPtr.Zero);
Assert.AreEqual(bitmap.GetPixels(), pixmap.GetPixels());
}
}
}
}