diff --git a/BUILD.gn b/BUILD.gn index 61facb7a1957..c2664deef249 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -3089,13 +3089,21 @@ skiasharp_build("SkiaSharp") { sources = [ "src/xamarin/sk_compatpaint.cpp", + "src/xamarin/sk_managedidchangelistener.cpp", + "src/xamarin/sk_managedidchangelistenerlist.cpp", "src/xamarin/sk_manageddrawable.cpp", + "src/xamarin/sk_managedallocator.cpp", + "src/xamarin/sk_managedpixelref.cpp", "src/xamarin/sk_managedstream.cpp", "src/xamarin/sk_managedtracememorydump.cpp", "src/xamarin/sk_xamarin.cpp", "src/xamarin/SkiaKeeper.c", "src/xamarin/SkCompatPaint.cpp", + "src/xamarin/SkManagedIDChangeListener.cpp", + "src/xamarin/SkManagedIDChangeListenerList.cpp", "src/xamarin/SkManagedDrawable.cpp", + "src/xamarin/SkManagedAllocator.cpp", + "src/xamarin/SkManagedPixelRef.cpp", "src/xamarin/SkManagedStream.cpp", "src/xamarin/SkManagedTraceMemoryDump.cpp", "src/xamarin/WinRTCompat.cpp", diff --git a/include/c/sk_bitmap.h b/include/c/sk_bitmap.h index bf019a05e8ad..26015523509d 100644 --- a/include/c/sk_bitmap.h +++ b/include/c/sk_bitmap.h @@ -36,16 +36,21 @@ SK_C_API void sk_bitmap_get_pixel_colors(sk_bitmap_t* cbitmap, sk_color_t* color SK_C_API bool sk_bitmap_install_pixels(sk_bitmap_t* cbitmap, const sk_imageinfo_t* cinfo, void* pixels, size_t rowBytes, const sk_bitmap_release_proc releaseProc, void* context); SK_C_API bool sk_bitmap_install_pixels_with_pixmap(sk_bitmap_t* cbitmap, const sk_pixmap_t* cpixmap); SK_C_API bool sk_bitmap_install_mask_pixels(sk_bitmap_t* cbitmap, const sk_mask_t* cmask); +SK_C_API bool sk_bitmap_try_alloc_pixels_with_allocator(sk_bitmap_t* cbitmap, sk_bitmapallocator_t* allocator); SK_C_API bool sk_bitmap_try_alloc_pixels(sk_bitmap_t* cbitmap, const sk_imageinfo_t* requestedInfo, size_t rowBytes); SK_C_API bool sk_bitmap_try_alloc_pixels_with_flags(sk_bitmap_t* cbitmap, const sk_imageinfo_t* requestedInfo, uint32_t flags); SK_C_API void sk_bitmap_set_pixels(sk_bitmap_t* cbitmap, void* pixels); SK_C_API bool sk_bitmap_peek_pixels(sk_bitmap_t* cbitmap, sk_pixmap_t* cpixmap); +SK_C_API void* sk_bitmap_get_pixelref(sk_bitmap_t* cbitmap); +SK_C_API void sk_bitmap_set_pixelref(sk_bitmap_t* cbitmap, void* cpixelref, int x, int y); SK_C_API bool sk_bitmap_extract_subset(sk_bitmap_t* cbitmap, sk_bitmap_t* dst, sk_irect_t* subset); SK_C_API bool sk_bitmap_extract_alpha(sk_bitmap_t* cbitmap, sk_bitmap_t* dst, const sk_paint_t* paint, sk_ipoint_t* offset); SK_C_API void sk_bitmap_notify_pixels_changed(sk_bitmap_t* cbitmap); SK_C_API void sk_bitmap_swap(sk_bitmap_t* cbitmap, sk_bitmap_t* cother); SK_C_API sk_shader_t* sk_bitmap_make_shader(sk_bitmap_t* cbitmap, sk_shader_tilemode_t tmx, sk_shader_tilemode_t tmy, const sk_matrix_t* cmatrix); +SK_C_API bool sk_bitmap_heapalloc(sk_bitmap_t* cbitmap); + SK_C_PLUS_PLUS_END_GUARD #endif diff --git a/include/c/sk_types.h b/include/c/sk_types.h index 289ecaee92ee..bc4b2694bd5e 100644 --- a/include/c/sk_types.h +++ b/include/c/sk_types.h @@ -343,11 +343,17 @@ typedef struct sk_string_t sk_string_t; A sk_bitmap_t is an abstraction that specifies a raster bitmap. */ typedef struct sk_bitmap_t sk_bitmap_t; +typedef struct sk_bitmapallocator_t sk_bitmapallocator_t; typedef struct sk_pixmap_t sk_pixmap_t; typedef struct sk_colorfilter_t sk_colorfilter_t; +// placed here to help seperate in PR +typedef struct sk_pixelref_t sk_pixelref_t; typedef struct sk_imagefilter_t sk_imagefilter_t; typedef struct sk_imagefilter_croprect_t sk_imagefilter_croprect_t; +typedef struct sk_idchangelistener_t sk_idchangelistener_t; +typedef struct sk_idchangelistenerlist_t sk_idchangelistenerlist_t; + /** A sk_typeface_t pecifies the typeface and intrinsic style of a font. This is used in the paint, along with optionally algorithmic settings like diff --git a/include/xamarin/SkManagedAllocator.h b/include/xamarin/SkManagedAllocator.h new file mode 100644 index 000000000000..50e26ce384c7 --- /dev/null +++ b/include/xamarin/SkManagedAllocator.h @@ -0,0 +1,49 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkManagedAllocator_h +#define SkManagedAllocator_h + +#include "include/core/SkBitmap.h" +#include "include/core/SkTypes.h" + +class SkBitmap; + +class SK_API SkManagedAllocator; + +// delegate declarations + +// managed Allocator +class SkManagedAllocator : public SkBitmap::Allocator { +public: + SkManagedAllocator(void* context); + + ~SkManagedAllocator() override; + +public: + typedef bool (*AllocProc) (SkManagedAllocator* d, void* context, SkBitmap* bitmap); + typedef void (*DestroyProc) (SkManagedAllocator* d, void* context); + + struct Procs { + AllocProc fAllocPixelRef = nullptr; + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(Procs procs); + +protected: + bool allocPixelRef(SkBitmap* bitmap) override; + +private: + void* fContext; + static Procs fProcs; + + typedef SkBitmap::Allocator INHERITED; +}; + + +#endif diff --git a/include/xamarin/SkManagedIDChangeListener.h b/include/xamarin/SkManagedIDChangeListener.h new file mode 100644 index 000000000000..49e5c6c4587d --- /dev/null +++ b/include/xamarin/SkManagedIDChangeListener.h @@ -0,0 +1,49 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkManagedIDChangeListener_h +#define SkManagedIDChangeListener_h + +#include "include/private/SkIDChangeListener.h" +#include "include/core/SkTypes.h" + +class SkIDChangeListener; + +class SK_API SkManagedIDChangeListener; + +// delegate declarations + +// managed Allocator +class SkManagedIDChangeListener : public SkIDChangeListener { +public: + SkManagedIDChangeListener(void* context); + + ~SkManagedIDChangeListener() override; + +public: + typedef void (*ChangedProc) (SkManagedIDChangeListener* d, void* context); + typedef void (*DestroyProc) (SkManagedIDChangeListener* d, void* context); + + struct Procs { + ChangedProc fChanged = nullptr; + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(Procs procs); + +protected: + void changed() override; + +private: + void* fContext; + static Procs fProcs; + + typedef SkIDChangeListener INHERITED; +}; + + +#endif diff --git a/include/xamarin/SkManagedIDChangeListenerList.h b/include/xamarin/SkManagedIDChangeListenerList.h new file mode 100644 index 000000000000..cff5b92ec478 --- /dev/null +++ b/include/xamarin/SkManagedIDChangeListenerList.h @@ -0,0 +1,41 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkManagedIDChangeListenerList_h +#define SkManagedIDChangeListenerList_h + +#include "include/private/SkIDChangeListener.h" +#include "include/core/SkTypes.h" + +class SK_API SkManagedIDChangeListenerList; + +// delegate declarations + +// managed Allocator +class SkManagedIDChangeListenerList : public SkIDChangeListener::List { +public: + SkManagedIDChangeListenerList(void* context); + + ~SkManagedIDChangeListenerList(); + + typedef void (*DestroyProc)(SkManagedIDChangeListenerList* d, void* context); + + struct Procs { + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(Procs procs); + +private: + void* fContext; + static Procs fProcs; + + typedef SkIDChangeListener::List INHERITED; +}; + + +#endif diff --git a/include/xamarin/SkManagedPixelRef.h b/include/xamarin/SkManagedPixelRef.h new file mode 100644 index 000000000000..29f22f654015 --- /dev/null +++ b/include/xamarin/SkManagedPixelRef.h @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkManagedPixelRef_h +#define SkManagedPixelRef_h + +#include "include/core/SkPixelRef.h" +#include "include/core/SkTypes.h" + +class SkPixelRef; + +class SK_API SkManagedPixelRef; + +// delegate declarations + +// managed Allocator +class SkManagedPixelRef { +public: + + sk_sp pixelRef; + + SkManagedPixelRef(void* context, SkPixelRef * pixelRef); + + SkManagedPixelRef(void* context, int32_t, int32_t, void*, size_t); + + virtual ~SkManagedPixelRef(); + + typedef void (*DestroyProc)(SkManagedPixelRef* d, void* context); + + struct Procs { + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(Procs procs); + +private: + void* fContext; + static Procs fProcs; +}; + + +#endif diff --git a/include/xamarin/sk_managedallocator.h b/include/xamarin/sk_managedallocator.h new file mode 100644 index 000000000000..152b1b85e133 --- /dev/null +++ b/include/xamarin/sk_managedallocator.h @@ -0,0 +1,33 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef sk_managedallocator_DEFINED +#define sk_managedallocator_DEFINED + +#include "sk_xamarin.h" + +#include "include/c/sk_types.h" + +SK_C_PLUS_PLUS_BEGIN_GUARD + +typedef struct sk_managedallocator_t sk_managedallocator_t; + +typedef bool (*sk_managedallocator_allocpixelref_proc) (sk_managedallocator_t* d, void* context, sk_bitmap_t* bitmap); +typedef void (*sk_managedallocator_destroy_proc) (sk_managedallocator_t* d, void* context); + +typedef struct { + sk_managedallocator_allocpixelref_proc fAllocPixelRef; + sk_managedallocator_destroy_proc fDestroy; +} sk_managedallocator_procs_t; + +SK_X_API sk_managedallocator_t* sk_managedallocator_new(void* context); +SK_X_API void sk_managedallocator_delete(sk_managedallocator_t*); +SK_X_API void sk_managedallocator_set_procs(sk_managedallocator_procs_t procs); + +SK_C_PLUS_PLUS_END_GUARD + +#endif diff --git a/include/xamarin/sk_managedidchangelistener.h b/include/xamarin/sk_managedidchangelistener.h new file mode 100644 index 000000000000..0ba4f0f2a752 --- /dev/null +++ b/include/xamarin/sk_managedidchangelistener.h @@ -0,0 +1,35 @@ +/* + * Copyright 2014 Google Inc. + * Copyright 2015 Xamarin Inc. + * Copyright 2017 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef sk_managedidchangelistener_DEFINED +#define sk_managedidchangelistener_DEFINED + +#include "sk_xamarin.h" + +#include "include/c/sk_types.h" + +SK_C_PLUS_PLUS_BEGIN_GUARD + +typedef void (*sk_idchangelistener_changed_proc)(sk_idchangelistener_t* d, void* context); +typedef void (*sk_idchangelistener_destroy_proc)(sk_idchangelistener_t* d, void* context); + +typedef struct { + sk_idchangelistener_changed_proc fChanged; + sk_idchangelistener_destroy_proc fDestroy; +} sk_idchangelistener_procs_t; + +SK_X_API sk_idchangelistener_t* sk_managedidchangelistener_new(void* context); +SK_X_API void sk_managedidchangelistener_delete(sk_idchangelistener_t*); +SK_X_API void sk_managedidchangelistener_mark_should_deregister(sk_idchangelistener_t*); +SK_X_API bool sk_managedidchangelistener_should_deregister(sk_idchangelistener_t*); +SK_X_API void sk_managedidchangelistener_set_procs(sk_idchangelistener_procs_t procs); + +SK_C_PLUS_PLUS_END_GUARD + +#endif diff --git a/include/xamarin/sk_managedidchangelistenerlist.h b/include/xamarin/sk_managedidchangelistenerlist.h new file mode 100644 index 000000000000..30eb35008343 --- /dev/null +++ b/include/xamarin/sk_managedidchangelistenerlist.h @@ -0,0 +1,35 @@ +/* + * Copyright 2014 Google Inc. + * Copyright 2015 Xamarin Inc. + * Copyright 2017 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef sk_managedidchangelistenerlist_DEFINED +#define sk_managedidchangelistenerlist_DEFINED + +#include "sk_xamarin.h" + +#include "include/c/sk_types.h" + +SK_C_PLUS_PLUS_BEGIN_GUARD + +typedef void (*sk_idchangelistenerlist_destroy_proc)(sk_idchangelistenerlist_t* d, void* context); + +typedef struct { + sk_idchangelistenerlist_destroy_proc fDestroy; +} sk_idchangelistenerlist_procs_t; + +SK_X_API sk_idchangelistenerlist_t* sk_managedidchangelistenerlist_new(void* context); +SK_X_API void sk_managedidchangelistenerlist_delete(sk_idchangelistenerlist_t*); +SK_X_API void sk_managedidchangelistenerlist_add(sk_idchangelistenerlist_t*, sk_idchangelistener_t*, bool single_threaded); +SK_X_API int32_t sk_managedidchangelistenerlist_count(sk_idchangelistenerlist_t*); +SK_X_API void sk_managedidchangelistenerlist_changed(sk_idchangelistenerlist_t*, bool single_threaded); +SK_X_API void sk_managedidchangelistenerlist_reset(sk_idchangelistenerlist_t*, bool single_threaded); +SK_X_API void sk_managedidchangelistenerlist_set_procs(sk_idchangelistenerlist_procs_t procs); + +SK_C_PLUS_PLUS_END_GUARD + +#endif diff --git a/include/xamarin/sk_managedpixelref.h b/include/xamarin/sk_managedpixelref.h new file mode 100644 index 000000000000..76f6dc329713 --- /dev/null +++ b/include/xamarin/sk_managedpixelref.h @@ -0,0 +1,44 @@ +/* + * Copyright 2014 Google Inc. + * Copyright 2015 Xamarin Inc. + * Copyright 2017 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef sk_managedpixelref_DEFINED +#define sk_managedpixelref_DEFINED + +#include "sk_xamarin.h" + +#include "include/c/sk_types.h" + +SK_C_PLUS_PLUS_BEGIN_GUARD + +typedef void (*sk_pixelref_destroy_proc)(sk_pixelref_t* d, void* context); + +typedef struct { + sk_pixelref_destroy_proc fDestroy; +} sk_pixelref_procs_t; + +SK_X_API sk_pixelref_t* sk_managedpixelref_new_from_existing(void* context, void* pixelRef); +SK_X_API sk_pixelref_t* sk_managedpixelref_new(void* context, int32_t, int32_t, void*, size_t); +SK_X_API void sk_managedpixelref_delete(sk_pixelref_t*); +SK_X_API sk_isize_t sk_managedpixelref_dimensions(sk_pixelref_t*); +SK_X_API int32_t sk_managedpixelref_width(sk_pixelref_t*); +SK_X_API int32_t sk_managedpixelref_height(sk_pixelref_t*); +SK_X_API size_t sk_managedpixelref_rowBytes(sk_pixelref_t*); +SK_X_API void* sk_managedpixelref_pixels(sk_pixelref_t*); +SK_X_API void* sk_managedpixelref_pixelref(sk_pixelref_t* d); +SK_X_API uint32_t sk_managedpixelref_generation_id(sk_pixelref_t*); +SK_X_API void sk_managedpixelref_notify_pixels_changed(sk_pixelref_t*); +SK_X_API bool sk_managedpixelref_is_immutable(sk_pixelref_t*); +SK_X_API void sk_managedpixelref_set_immutable(sk_pixelref_t*); +SK_X_API void sk_managedpixelref_add_generation_id_listener(sk_pixelref_t*, sk_idchangelistener_t*); +SK_X_API void sk_managedpixelref_notify_added_to_cache(sk_pixelref_t*); +SK_X_API void sk_managedpixelref_set_procs(sk_pixelref_procs_t procs); + +SK_C_PLUS_PLUS_END_GUARD + +#endif diff --git a/src/c/sk_bitmap.cpp b/src/c/sk_bitmap.cpp index 7e6d84270a4f..272e39be3c02 100644 --- a/src/c/sk_bitmap.cpp +++ b/src/c/sk_bitmap.cpp @@ -12,6 +12,7 @@ #include "include/core/SkColorPriv.h" #include "include/core/SkImageInfo.h" #include "include/core/SkMath.h" +#include "include/core/SkPixelRef.h" #include "include/core/SkShader.h" #include "include/core/SkUnPreMultiply.h" @@ -117,6 +118,10 @@ bool sk_bitmap_install_mask_pixels(sk_bitmap_t* cbitmap, const sk_mask_t* cmask) return AsBitmap(cbitmap)->installMaskPixels(*AsMask(cmask)); } +bool sk_bitmap_try_alloc_pixels_with_allocator(sk_bitmap_t* cbitmap, sk_bitmapallocator_t* allocator) { + return AsBitmap(cbitmap)->tryAllocPixels(AsBitmapAllocator(allocator)); +} + bool sk_bitmap_try_alloc_pixels(sk_bitmap_t* cbitmap, const sk_imageinfo_t* requestedInfo, size_t rowBytes) { return AsBitmap(cbitmap)->tryAllocPixels(AsImageInfo(requestedInfo), rowBytes); } @@ -133,6 +138,16 @@ bool sk_bitmap_peek_pixels(sk_bitmap_t* cbitmap, sk_pixmap_t* cpixmap) { return AsBitmap(cbitmap)->peekPixels(AsPixmap(cpixmap)); } +SK_C_API void* sk_bitmap_get_pixelref(sk_bitmap_t* cbitmap) { + return AsBitmap(cbitmap)->pixelRef(); +} + +SK_C_API void sk_bitmap_set_pixelref(sk_bitmap_t* cbitmap, void* cpixelref, int x, int y) { + SkPixelRef* r = (SkPixelRef*)cpixelref; + AsBitmap(cbitmap)->setPixelRef(sk_ref_sp(r), x, y); +} + + bool sk_bitmap_extract_subset(sk_bitmap_t* cbitmap, sk_bitmap_t* cdst, sk_irect_t* subset) { return AsBitmap(cbitmap)->extractSubset(AsBitmap(cdst), *AsIRect(subset)); } @@ -156,3 +171,8 @@ sk_shader_t* sk_bitmap_make_shader(sk_bitmap_t* cbitmap, sk_shader_tilemode_t tm } return ToShader(AsBitmap(cbitmap)->makeShader((SkTileMode)tmx, (SkTileMode)tmy, cmatrix ? &m : nullptr).release()); } + +bool sk_bitmap_heapalloc(sk_bitmap_t* cbitmap) { + SkBitmap::HeapAllocator stdalloc; + return stdalloc.allocPixelRef(AsBitmap(cbitmap)); +} diff --git a/src/c/sk_id_change_listener.cpp b/src/c/sk_id_change_listener.cpp new file mode 100644 index 000000000000..a36fefbb7f64 --- /dev/null +++ b/src/c/sk_id_change_listener.cpp @@ -0,0 +1,43 @@ +/* + * Copyright 2014 Google Inc. + * Copyright 2015 Xamarin Inc. + * Copyright 2017 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef sk_idchangelistener_DEFINED +#define sk_idchangelistener_DEFINED + +#include "include/c/sk_idchangelistener.h" +#include "src/c/sk_types_priv.h" + +static inline SkIDChangeListener* AsSkIDChangeListener(sk_idchangelistener_t* d) { + return reinterpret_cast(d); +} + +static sk_idchangelistener_procs_t gProcs; + +void changed(SkIDChangeListener* d, void* context) { + if (gProcs.fChanged) { + gProcs.fChanged(ToSkIDChangeListener(d), context); + } +} + +sk_idchangelistener_t* sk_idchangelistener_new(void* context) { + return ToSKIDChangeListener(new SkIDChangeListener(context)); +} + +void sk_idchangelistener_delete(sk_idchangelistener_t* d) { delete AsManagedAllocator(d); } + +void sk_idchangelistener_set_procs(sk_idchangelistener_procs_t procs) { + gProcs = procs; + + SkManagedAllocator::Procs p; + p.fAllocPixelRef = allocPixelRef; + + SkManagedAllocator::setProcs(p); +} + +#endif diff --git a/src/c/sk_types_priv.h b/src/c/sk_types_priv.h index af460d8994d0..adea0e07856a 100644 --- a/src/c/sk_types_priv.h +++ b/src/c/sk_types_priv.h @@ -175,9 +175,18 @@ DEF_STRUCT_MAP(GrGLInterface, gr_glinterface_t, GrGLInterface) DEF_STRUCT_MAP(GrVkYcbcrConversionInfo, gr_vk_ycbcrconversioninfo_t, GrVkYcbcrConversionInfo) DEF_STRUCT_MAP(GrVkImageInfo, gr_vk_imageinfo_t, GrVkImageInfo) +// placed here to help seperate in PR +#include "include/core/SkBitmap.h" +DEF_MAP(SkBitmap::Allocator, sk_bitmapallocator_t, BitmapAllocator) + #include "include/effects/SkRuntimeEffect.h" DEF_MAP(SkRuntimeEffect::Uniform, sk_runtimeeffect_uniform_t, RuntimeEffectUniform) + +#include "include/private/SkIDChangeListener.h" +DEF_CLASS_MAP(SkIDChangeListener, sk_idchangelistener_t, SKIDChangeListener) +DEF_MAP(SkIDChangeListener::List, sk_idchangelistenerlist_t, SKIDChangeListenerList) + #include "include/core/SkCanvas.h" DEF_MAP(SkCanvas::Lattice, sk_lattice_t, Lattice) diff --git a/src/xamarin/SkManagedAllocator.cpp b/src/xamarin/SkManagedAllocator.cpp new file mode 100644 index 000000000000..2db03cd3c35d --- /dev/null +++ b/src/xamarin/SkManagedAllocator.cpp @@ -0,0 +1,29 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "include/xamarin/SkManagedAllocator.h" + +SkManagedAllocator::Procs SkManagedAllocator::fProcs; + +void SkManagedAllocator::setProcs(SkManagedAllocator::Procs procs) { + fProcs = procs; +} + +SkManagedAllocator::SkManagedAllocator(void* context) { + fContext = context; +} + +SkManagedAllocator::~SkManagedAllocator() { + if (fProcs.fDestroy) { + fProcs.fDestroy(this, fContext); + } +} + +bool SkManagedAllocator::allocPixelRef(SkBitmap* bitmap) { + if (!fProcs.fAllocPixelRef) return false; + return fProcs.fAllocPixelRef(this, fContext, bitmap); +} diff --git a/src/xamarin/SkManagedIDChangeListener.cpp b/src/xamarin/SkManagedIDChangeListener.cpp new file mode 100644 index 000000000000..98e754a48c20 --- /dev/null +++ b/src/xamarin/SkManagedIDChangeListener.cpp @@ -0,0 +1,30 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "include/xamarin/SkManagedIDChangeListener.h" + +SkManagedIDChangeListener::Procs SkManagedIDChangeListener::fProcs; + +void SkManagedIDChangeListener::setProcs(SkManagedIDChangeListener::Procs procs) { + fProcs = procs; +} + +SkManagedIDChangeListener::SkManagedIDChangeListener(void* context) { + fContext = context; +} + +SkManagedIDChangeListener::~SkManagedIDChangeListener() { + if (fProcs.fDestroy) { + fProcs.fDestroy(this, fContext); + } +} + +void SkManagedIDChangeListener::changed() { + if (fProcs.fChanged) { + fProcs.fChanged(this, fContext); + } +} diff --git a/src/xamarin/SkManagedIDChangeListenerList.cpp b/src/xamarin/SkManagedIDChangeListenerList.cpp new file mode 100644 index 000000000000..531814fe25ed --- /dev/null +++ b/src/xamarin/SkManagedIDChangeListenerList.cpp @@ -0,0 +1,24 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "include/xamarin/SkManagedIDChangeListenerList.h" + +SkManagedIDChangeListenerList::Procs SkManagedIDChangeListenerList::fProcs; + +void SkManagedIDChangeListenerList::setProcs(SkManagedIDChangeListenerList::Procs procs) { + fProcs = procs; +} + +SkManagedIDChangeListenerList::SkManagedIDChangeListenerList(void* context) { + fContext = context; +} + +SkManagedIDChangeListenerList::~SkManagedIDChangeListenerList() { + if (fProcs.fDestroy) { + fProcs.fDestroy(this, fContext); + } +} diff --git a/src/xamarin/SkManagedPixelRef.cpp b/src/xamarin/SkManagedPixelRef.cpp new file mode 100644 index 000000000000..9d1c756d5344 --- /dev/null +++ b/src/xamarin/SkManagedPixelRef.cpp @@ -0,0 +1,33 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +// TODO: make this more like SKDrawable +// - inherit directly instead of using obj ptr + +#include "include/xamarin/SkManagedPixelRef.h" + +SkManagedPixelRef::Procs SkManagedPixelRef::fProcs; + +void SkManagedPixelRef::setProcs(SkManagedPixelRef::Procs procs) { + fProcs = procs; +} + +SkManagedPixelRef::SkManagedPixelRef(void* context, SkPixelRef * pixelRef) { + fContext = context; + this->pixelRef = sk_ref_sp(pixelRef); +} + +SkManagedPixelRef::SkManagedPixelRef(void* context, int width, int height, void* addr, size_t rowBytes) { + fContext = context; + this->pixelRef = sk_ref_sp(new SkPixelRef(width, height, addr, rowBytes)); +} + +SkManagedPixelRef::~SkManagedPixelRef() { + if (fProcs.fDestroy) { + fProcs.fDestroy(this, fContext); + } +} diff --git a/src/xamarin/SkiaKeeper.c b/src/xamarin/SkiaKeeper.c index 67f482881e68..c1e0c02f0f16 100644 --- a/src/xamarin/SkiaKeeper.c +++ b/src/xamarin/SkiaKeeper.c @@ -52,8 +52,13 @@ // Xamarin #include "include/xamarin/sk_managedstream.h" +// placed here to help seperate in PR +#include "include/xamarin/sk_managedallocator.h" +#include "include/xamarin/sk_managedpixelref.h" #include "include/xamarin/sk_manageddrawable.h" #include "include/xamarin/sk_managedtracememorydump.h" +#include "include/xamarin/sk_managedidchangelistener.h" +#include "include/xamarin/sk_managedidchangelistenerlist.h" #include "include/xamarin/sk_compatpaint.h" SK_X_API void** KeepSkiaCSymbols (void); @@ -107,8 +112,14 @@ void** KeepSkiaCSymbols (void) // Xamarin (void*)sk_compatpaint_new, (void*)sk_managedstream_new, + // placed here to help seperate in PR + (void*)sk_managedallocator_new, + (void*)sk_managedpixelref_new, + (void*)sk_managedpixelref_new_from_existing, (void*)sk_manageddrawable_new, (void*)sk_managedtracememorydump_new, + (void*)sk_managedidchangelistener_new, + (void*)sk_managedidchangelistenerlist_new, }; return ret; } diff --git a/src/xamarin/sk_managedallocator.cpp b/src/xamarin/sk_managedallocator.cpp new file mode 100644 index 000000000000..dc83499a4d0f --- /dev/null +++ b/src/xamarin/sk_managedallocator.cpp @@ -0,0 +1,49 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "include/xamarin/SkManagedAllocator.h" + +#include "include/xamarin/sk_managedallocator.h" +#include "src/c/sk_types_priv.h" + +static inline SkManagedAllocator* AsManagedAllocator(sk_managedallocator_t* d) { + return reinterpret_cast(d); +} +static inline sk_managedallocator_t* ToManagedAllocator(SkManagedAllocator* d) { + return reinterpret_cast(d); +} + +static sk_managedallocator_procs_t gProcs; + +bool allocPixelRef(SkManagedAllocator* d, void* context, SkBitmap* bitmap) { + if (!gProcs.fAllocPixelRef) return false; + return gProcs.fAllocPixelRef(ToManagedAllocator(d), context, ToBitmap(bitmap)); +} + +void destroy(SkManagedAllocator* d, void* context) { + if (gProcs.fDestroy) { + gProcs.fDestroy(ToManagedAllocator(d), context); + } +} + +sk_managedallocator_t* sk_managedallocator_new(void* context) { + return ToManagedAllocator(new SkManagedAllocator(context)); +} + +void sk_managedallocator_delete(sk_managedallocator_t* d) { + delete AsManagedAllocator(d); +} + +void sk_managedallocator_set_procs(sk_managedallocator_procs_t procs) { + gProcs = procs; + + SkManagedAllocator::Procs p; + p.fAllocPixelRef = allocPixelRef; + p.fDestroy = destroy; + + SkManagedAllocator::setProcs(p); +} diff --git a/src/xamarin/sk_managedidchangelistener.cpp b/src/xamarin/sk_managedidchangelistener.cpp new file mode 100644 index 000000000000..782da4cf5549 --- /dev/null +++ b/src/xamarin/sk_managedidchangelistener.cpp @@ -0,0 +1,59 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#include "include/xamarin/sk_managedidchangelistener.h" +#include "include/xamarin/SkManagedIDChangeListener.h" + +#include "src/c/sk_types_priv.h" + +static inline SkManagedIDChangeListener* AsSkManagedIDChangeListener(sk_idchangelistener_t* d) { + return reinterpret_cast(d); +} +static inline sk_idchangelistener_t* ToSkManagedIDChangeListener(SkManagedIDChangeListener* d) { + return reinterpret_cast(d); +} + +static sk_idchangelistener_procs_t gProcs; + +void changed(SkManagedIDChangeListener* d, void* context) { + if (gProcs.fChanged) { + gProcs.fChanged(ToSkManagedIDChangeListener(d), context); + } +} + +void destroy(SkManagedIDChangeListener* d, void* context) { + if (gProcs.fDestroy) { + gProcs.fDestroy(ToSkManagedIDChangeListener(d), context); + } +} + +sk_idchangelistener_t* sk_managedidchangelistener_new(void* context) { + return ToSkManagedIDChangeListener(new SkManagedIDChangeListener(context)); +} + +void sk_managedidchangelistener_delete(sk_idchangelistener_t* d) { + delete AsSkManagedIDChangeListener(d); +} + +void sk_managedidchangelistener_mark_should_deregister(sk_idchangelistener_t* d) { + AsSkManagedIDChangeListener(d)->markShouldDeregister(); +} + +bool sk_managedidchangelistener_should_deregister(sk_idchangelistener_t* d) { + return AsSkManagedIDChangeListener(d)->shouldDeregister(); +} + +void sk_managedidchangelistener_set_procs(sk_idchangelistener_procs_t procs) { + gProcs = procs; + + SkManagedIDChangeListener::Procs p; + p.fChanged = changed; + p.fDestroy = destroy; + + SkManagedIDChangeListener::setProcs(p); +} diff --git a/src/xamarin/sk_managedidchangelistenerlist.cpp b/src/xamarin/sk_managedidchangelistenerlist.cpp new file mode 100644 index 000000000000..59f2d190096c --- /dev/null +++ b/src/xamarin/sk_managedidchangelistenerlist.cpp @@ -0,0 +1,66 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "include/xamarin/sk_managedidchangelistener.h" +#include "include/xamarin/SkManagedIDChangeListener.h" + +#include "include/xamarin/sk_managedidchangelistenerlist.h" +#include "include/xamarin/SkManagedIDChangeListenerList.h" + +#include "src/c/sk_types_priv.h" + +static inline SkManagedIDChangeListener* AsSkManagedIDChangeListener(sk_idchangelistener_t* d) { + return reinterpret_cast(d); +} +static inline SkManagedIDChangeListenerList* AsSkManagedIDChangeListenerList(sk_idchangelistenerlist_t* d) { + return reinterpret_cast(d); +} +static inline sk_idchangelistenerlist_t* ToSkManagedIDChangeListenerList(SkManagedIDChangeListenerList* d) { + return reinterpret_cast(d); +} + +static sk_idchangelistenerlist_procs_t gProcs; + +void destroy_List(SkManagedIDChangeListenerList* d, void* context) { + if (gProcs.fDestroy) { + gProcs.fDestroy(ToSkManagedIDChangeListenerList(d), context); + } +} + +sk_idchangelistenerlist_t* sk_managedidchangelistenerlist_new(void* context) { + return ToSkManagedIDChangeListenerList(new SkManagedIDChangeListenerList(context)); +} + +void sk_managedidchangelistenerlist_delete(sk_idchangelistenerlist_t* d) { + delete AsSkManagedIDChangeListenerList(d); +} + +void sk_managedidchangelistenerlist_add(sk_idchangelistenerlist_t* d, sk_idchangelistener_t* listener, bool single_threaded) { + AsSkManagedIDChangeListenerList(d)->add(sk_sp(AsSkManagedIDChangeListener(listener)), + single_threaded); +} + +int32_t sk_managedidchangelistenerlist_count(sk_idchangelistenerlist_t* d) { + return AsSkManagedIDChangeListenerList(d)->count(); +} + +void sk_managedidchangelistenerlist_changed(sk_idchangelistenerlist_t* d, bool single_threaded) { + AsSkManagedIDChangeListenerList(d)->changed(single_threaded); +} + +void sk_managedidchangelistenerlist_reset(sk_idchangelistenerlist_t* d, bool single_threaded) { + AsSkManagedIDChangeListenerList(d)->reset(single_threaded); +} + +void sk_managedidchangelistenerlist_set_procs(sk_idchangelistenerlist_procs_t procs) { + gProcs = procs; + + SkManagedIDChangeListenerList::Procs p; + p.fDestroy = destroy_List; + + SkManagedIDChangeListenerList::setProcs(p); +} diff --git a/src/xamarin/sk_managedpixelref.cpp b/src/xamarin/sk_managedpixelref.cpp new file mode 100644 index 000000000000..a81a0c00325f --- /dev/null +++ b/src/xamarin/sk_managedpixelref.cpp @@ -0,0 +1,93 @@ +/* + * Copyright 2022 Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "include/xamarin/SkManagedPixelRef.h" +#include "include/xamarin/SkManagedIDChangeListener.h" +#include "include/xamarin/sk_managedpixelref.h" +#include "src/c/sk_types_priv.h" + +static inline SkManagedPixelRef* AsSkManagedPixelRef(sk_pixelref_t* d) { + return reinterpret_cast(d); +} +static inline sk_pixelref_t* ToSkPixelRef(SkManagedPixelRef* d) { + return reinterpret_cast(d); +} + +static inline SkManagedIDChangeListener* AsSkManagedIDChangeListener(sk_idchangelistener_t* d) { + return reinterpret_cast(d); +} + +static sk_pixelref_procs_t gProcs; + +void destroy(SkManagedPixelRef* d, void* context) { + if (gProcs.fDestroy) { + gProcs.fDestroy(ToSkPixelRef(d), context); + } +} + +sk_pixelref_t* sk_managedpixelref_new_from_existing(void* context, void* pixelRef) { + return ToSkPixelRef(new SkManagedPixelRef(context, (SkPixelRef*)pixelRef)); +} + +sk_pixelref_t* sk_managedpixelref_new( + void* context, int32_t width, int32_t height, void* addr, size_t rowBytes) { + return ToSkPixelRef(new SkManagedPixelRef(context, width, height, addr, rowBytes)); +} + +void sk_managedpixelref_delete(sk_pixelref_t* d) { + delete AsSkManagedPixelRef(d); +} + +sk_isize_t sk_managedpixelref_dimensions(sk_pixelref_t* d) { + return ToISize(AsSkManagedPixelRef(d)->pixelRef->dimensions()); +} +int32_t sk_managedpixelref_width(sk_pixelref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->width(); +} + +int32_t sk_managedpixelref_height(sk_pixelref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->height(); +} +size_t sk_managedpixelref_rowBytes(sk_pixelref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->rowBytes(); +} +void* sk_managedpixelref_pixels(sk_pixelref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->pixels(); +} +void* sk_managedpixelref_pixelref(sk_pixelref_t* d) { + // IMPORTANT!!! + // we must keep our pixel ref in order to keep functioning + // so we do not call release() nor unref() on it to prevent it pointing to garbage + return AsSkManagedPixelRef(d)->pixelRef.get(); +} +uint32_t sk_managedpixelref_generation_id(sk_pixelref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->getGenerationID(); +} +void sk_managedpixelref_notify_pixels_changed(sk_pixelref_t* d) { + AsSkManagedPixelRef(d)->pixelRef->notifyPixelsChanged(); +} +bool sk_managedpixelref_is_immutable(sk_pixelref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->isImmutable(); +} +void sk_managedpixelref_set_immutable(sk_pixelref_t* d) { + AsSkManagedPixelRef(d)->pixelRef->setImmutable(); +} +void sk_managedpixelref_add_generation_id_listener(sk_pixelref_t* d, sk_idchangelistener_t* listener) { + AsSkManagedPixelRef(d)->pixelRef->addGenIDChangeListener(sk_ref_sp(AsSkManagedIDChangeListener(listener))); +} +void sk_managedpixelref_notify_added_to_cache(sk_pixelref_t* d) { + AsSkManagedPixelRef(d)->pixelRef->notifyAddedToCache(); +} + +void sk_managedpixelref_set_procs(sk_pixelref_procs_t procs) { + gProcs = procs; + + SkManagedPixelRef::Procs p; + p.fDestroy = destroy; + + SkManagedPixelRef::setProcs(p); +}