From f54c3279685abb4973ba3cfeeae1f2014f0350e2 Mon Sep 17 00:00:00 2001 From: mgood7123 Date: Thu, 30 Jun 2022 22:46:38 +1000 Subject: [PATCH 1/4] add Bitmap allocator, add Pixel ref --- BUILD.gn | 4 ++ include/c/sk_bitmap.h | 5 ++ include/c/sk_types.h | 2 + include/xamarin/SkManagedAllocator.h | 49 ++++++++++++++ include/xamarin/SkManagedPixelRef.h | 46 +++++++++++++ include/xamarin/sk_managed_pixel_ref.h | 44 ++++++++++++ include/xamarin/sk_managedallocator.h | 33 +++++++++ src/c/sk_bitmap.cpp | 20 ++++++ src/c/sk_types_priv.h | 4 ++ src/xamarin/SkManagedAllocator.cpp | 29 ++++++++ src/xamarin/SkManagedPixelRef.cpp | 33 +++++++++ src/xamarin/SkiaKeeper.c | 7 ++ src/xamarin/sk_managed_pixel_ref.cpp | 93 ++++++++++++++++++++++++++ src/xamarin/sk_managedallocator.cpp | 49 ++++++++++++++ 14 files changed, 418 insertions(+) create mode 100644 include/xamarin/SkManagedAllocator.h create mode 100644 include/xamarin/SkManagedPixelRef.h create mode 100644 include/xamarin/sk_managed_pixel_ref.h create mode 100644 include/xamarin/sk_managedallocator.h create mode 100644 src/xamarin/SkManagedAllocator.cpp create mode 100644 src/xamarin/SkManagedPixelRef.cpp create mode 100644 src/xamarin/sk_managed_pixel_ref.cpp create mode 100644 src/xamarin/sk_managedallocator.cpp diff --git a/BUILD.gn b/BUILD.gn index 61facb7a1957..75b9376e1fec 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -3090,12 +3090,16 @@ skiasharp_build("SkiaSharp") { sources = [ "src/xamarin/sk_compatpaint.cpp", "src/xamarin/sk_manageddrawable.cpp", + "src/xamarin/sk_managedallocator.cpp", + "src/xamarin/sk_managed_pixel_ref.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/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..8481388532c9 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_pixel_ref(sk_bitmap_t* cbitmap); +SK_C_API void sk_bitmap_set_pixel_ref(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..c1de9ecb7195 100644 --- a/include/c/sk_types.h +++ b/include/c/sk_types.h @@ -345,6 +345,8 @@ typedef struct sk_string_t sk_string_t; typedef struct sk_bitmap_t sk_bitmap_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_pixel_ref_t sk_pixel_ref_t; typedef struct sk_imagefilter_t sk_imagefilter_t; typedef struct sk_imagefilter_croprect_t sk_imagefilter_croprect_t; 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/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_managed_pixel_ref.h b/include/xamarin/sk_managed_pixel_ref.h new file mode 100644 index 000000000000..3e694082413f --- /dev/null +++ b/include/xamarin/sk_managed_pixel_ref.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_managed_pixel_ref_DEFINED +#define sk_managed_pixel_ref_DEFINED + +#include "sk_xamarin.h" + +#include "include/c/sk_types.h" + +SK_C_PLUS_PLUS_BEGIN_GUARD + +typedef void (*sk_pixel_ref_destroy_proc)(sk_pixel_ref_t* d, void* context); + +typedef struct { + sk_pixel_ref_destroy_proc fDestroy; +} sk_pixel_ref_procs_t; + +SK_X_API sk_pixel_ref_t* sk_managed_pixel_ref_new_from_existing(void* context, void* pixelRef); +SK_X_API sk_pixel_ref_t* sk_managed_pixel_ref_new(void* context, int32_t, int32_t, void*, size_t); +SK_X_API void sk_managed_pixel_ref_delete(sk_pixel_ref_t*); +SK_X_API sk_isize_t sk_managed_pixel_ref_dimensions(sk_pixel_ref_t*); +SK_X_API int32_t sk_managed_pixel_ref_width(sk_pixel_ref_t*); +SK_X_API int32_t sk_managed_pixel_ref_height(sk_pixel_ref_t*); +SK_X_API size_t sk_managed_pixel_ref_rowBytes(sk_pixel_ref_t*); +SK_X_API void* sk_managed_pixel_ref_pixels(sk_pixel_ref_t*); +SK_X_API void* sk_managed_pixel_ref_pixel_ref(sk_pixel_ref_t* d); +SK_X_API uint32_t sk_managed_pixel_ref_generation_id(sk_pixel_ref_t*); +SK_X_API void sk_managed_pixel_ref_notify_pixels_changed(sk_pixel_ref_t*); +SK_X_API bool sk_managed_pixel_ref_is_immutable(sk_pixel_ref_t*); +SK_X_API void sk_managed_pixel_ref_set_immutable(sk_pixel_ref_t*); +//SK_X_API void sk_managed_pixel_ref_add_generation_id_listener(sk_pixel_ref_t*, sk_id_change_listener_t*); +SK_X_API void sk_managed_pixel_ref_notify_added_to_cache(sk_pixel_ref_t*); +SK_X_API void sk_managed_pixel_ref_set_procs(sk_pixel_ref_procs_t procs); + +SK_C_PLUS_PLUS_END_GUARD + +#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/src/c/sk_bitmap.cpp b/src/c/sk_bitmap.cpp index 7e6d84270a4f..c3670223d7c0 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_pixel_ref(sk_bitmap_t* cbitmap) { + return AsBitmap(cbitmap)->pixelRef(); +} + +SK_C_API void sk_bitmap_set_pixel_ref(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_types_priv.h b/src/c/sk_types_priv.h index af460d8994d0..b49e81fe6159 100644 --- a/src/c/sk_types_priv.h +++ b/src/c/sk_types_priv.h @@ -175,6 +175,10 @@ 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) 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/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..c77f9cf18957 100644 --- a/src/xamarin/SkiaKeeper.c +++ b/src/xamarin/SkiaKeeper.c @@ -52,6 +52,9 @@ // Xamarin #include "include/xamarin/sk_managedstream.h" +// placed here to help seperate in PR +#include "include/xamarin/sk_managedallocator.h" +#include "include/xamarin/sk_managed_pixel_ref.h" #include "include/xamarin/sk_manageddrawable.h" #include "include/xamarin/sk_managedtracememorydump.h" #include "include/xamarin/sk_compatpaint.h" @@ -107,6 +110,10 @@ 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_managed_pixel_ref_new, + (void*)sk_managed_pixel_ref_new_from_existing, (void*)sk_manageddrawable_new, (void*)sk_managedtracememorydump_new, }; diff --git a/src/xamarin/sk_managed_pixel_ref.cpp b/src/xamarin/sk_managed_pixel_ref.cpp new file mode 100644 index 000000000000..cab98f5e93a7 --- /dev/null +++ b/src/xamarin/sk_managed_pixel_ref.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/SkManaged_ID_Change_Listener.h" +#include "include/xamarin/sk_managed_pixel_ref.h" +#include "src/c/sk_types_priv.h" + +static inline SkManagedPixelRef* AsSkManagedPixelRef(sk_pixel_ref_t* d) { + return reinterpret_cast(d); +} +static inline sk_pixel_ref_t* ToSkPixelRef(SkManagedPixelRef* d) { + return reinterpret_cast(d); +} + +static inline SkManaged_ID_Change_Listener* AsSkManaged_ID_Change_Listener(sk_id_change_listener_t* d) { + return reinterpret_cast(d); +} + +static sk_pixel_ref_procs_t gProcs; + +void destroy(SkManagedPixelRef* d, void* context) { + if (gProcs.fDestroy) { + gProcs.fDestroy(ToSkPixelRef(d), context); + } +} + +sk_pixel_ref_t* sk_managed_pixel_ref_new_from_existing(void* context, void* pixelRef) { + return ToSkPixelRef(new SkManagedPixelRef(context, (SkPixelRef*)pixelRef)); +} + +sk_pixel_ref_t* sk_managed_pixel_ref_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_managed_pixel_ref_delete(sk_pixel_ref_t* d) { + delete AsSkManagedPixelRef(d); +} + +sk_isize_t sk_managed_pixel_ref_dimensions(sk_pixel_ref_t* d) { + return ToISize(AsSkManagedPixelRef(d)->pixelRef->dimensions()); +} +int32_t sk_managed_pixel_ref_width(sk_pixel_ref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->width(); +} + +int32_t sk_managed_pixel_ref_height(sk_pixel_ref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->height(); +} +size_t sk_managed_pixel_ref_rowBytes(sk_pixel_ref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->rowBytes(); +} +void* sk_managed_pixel_ref_pixels(sk_pixel_ref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->pixels(); +} +void* sk_managed_pixel_ref_pixel_ref(sk_pixel_ref_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_managed_pixel_ref_generation_id(sk_pixel_ref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->getGenerationID(); +} +void sk_managed_pixel_ref_notify_pixels_changed(sk_pixel_ref_t* d) { + AsSkManagedPixelRef(d)->pixelRef->notifyPixelsChanged(); +} +bool sk_managed_pixel_ref_is_immutable(sk_pixel_ref_t* d) { + return AsSkManagedPixelRef(d)->pixelRef->isImmutable(); +} +void sk_managed_pixel_ref_set_immutable(sk_pixel_ref_t* d) { + AsSkManagedPixelRef(d)->pixelRef->setImmutable(); +} +//void sk_managed_pixel_ref_add_generation_id_listener(sk_pixel_ref_t* d, sk_id_change_listener_t* listener) { +// AsSkManagedPixelRef(d)->pixelRef->addGenIDChangeListener(sk_ref_sp(AsSkManaged_ID_Change_Listener(listener))); +//} +void sk_managed_pixel_ref_notify_added_to_cache(sk_pixel_ref_t* d) { + AsSkManagedPixelRef(d)->pixelRef->notifyAddedToCache(); +} + +void sk_managed_pixel_ref_set_procs(sk_pixel_ref_procs_t procs) { + gProcs = procs; + + SkManagedPixelRef::Procs p; + p.fDestroy = destroy; + + SkManagedPixelRef::setProcs(p); +} 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); +} From 864f116505c3e0ca873cf6b52453fc6b7f4f3f6b Mon Sep 17 00:00:00 2001 From: mgood7123 Date: Thu, 30 Jun 2022 23:57:38 +1000 Subject: [PATCH 2/4] add typedef struct sk_bitmapallocator_t sk_bitmapallocator_t; --- include/c/sk_types.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/c/sk_types.h b/include/c/sk_types.h index c1de9ecb7195..ccb7e80c769c 100644 --- a/include/c/sk_types.h +++ b/include/c/sk_types.h @@ -343,6 +343,7 @@ 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 From 9a7aedcc3f2d0eccae7f65fcbd8cd40cf070957a Mon Sep 17 00:00:00 2001 From: mgood7123 Date: Fri, 8 Jul 2022 13:55:08 +1000 Subject: [PATCH 3/4] rename to match --- BUILD.gn | 2 +- include/c/sk_bitmap.h | 4 +- include/c/sk_types.h | 2 +- include/xamarin/sk_managed_pixel_ref.h | 44 ------------------- include/xamarin/sk_managedpixelref.h | 44 +++++++++++++++++++ src/c/sk_bitmap.cpp | 4 +- src/xamarin/SkiaKeeper.c | 6 +-- ...d_pixel_ref.cpp => sk_managedpixelref.cpp} | 42 +++++++++--------- 8 files changed, 74 insertions(+), 74 deletions(-) delete mode 100644 include/xamarin/sk_managed_pixel_ref.h create mode 100644 include/xamarin/sk_managedpixelref.h rename src/xamarin/{sk_managed_pixel_ref.cpp => sk_managedpixelref.cpp} (61%) diff --git a/BUILD.gn b/BUILD.gn index 75b9376e1fec..c221acdded0d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -3091,7 +3091,7 @@ skiasharp_build("SkiaSharp") { "src/xamarin/sk_compatpaint.cpp", "src/xamarin/sk_manageddrawable.cpp", "src/xamarin/sk_managedallocator.cpp", - "src/xamarin/sk_managed_pixel_ref.cpp", + "src/xamarin/sk_managedpixelref.cpp", "src/xamarin/sk_managedstream.cpp", "src/xamarin/sk_managedtracememorydump.cpp", "src/xamarin/sk_xamarin.cpp", diff --git a/include/c/sk_bitmap.h b/include/c/sk_bitmap.h index 8481388532c9..26015523509d 100644 --- a/include/c/sk_bitmap.h +++ b/include/c/sk_bitmap.h @@ -41,8 +41,8 @@ SK_C_API bool sk_bitmap_try_alloc_pixels(sk_bitmap_t* cbitmap, const sk_imageinf 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_pixel_ref(sk_bitmap_t* cbitmap); -SK_C_API void sk_bitmap_set_pixel_ref(sk_bitmap_t* cbitmap, void* cpixelref, int x, int y); +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); diff --git a/include/c/sk_types.h b/include/c/sk_types.h index ccb7e80c769c..aaca718bd610 100644 --- a/include/c/sk_types.h +++ b/include/c/sk_types.h @@ -347,7 +347,7 @@ 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_pixel_ref_t sk_pixel_ref_t; +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; diff --git a/include/xamarin/sk_managed_pixel_ref.h b/include/xamarin/sk_managed_pixel_ref.h deleted file mode 100644 index 3e694082413f..000000000000 --- a/include/xamarin/sk_managed_pixel_ref.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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_managed_pixel_ref_DEFINED -#define sk_managed_pixel_ref_DEFINED - -#include "sk_xamarin.h" - -#include "include/c/sk_types.h" - -SK_C_PLUS_PLUS_BEGIN_GUARD - -typedef void (*sk_pixel_ref_destroy_proc)(sk_pixel_ref_t* d, void* context); - -typedef struct { - sk_pixel_ref_destroy_proc fDestroy; -} sk_pixel_ref_procs_t; - -SK_X_API sk_pixel_ref_t* sk_managed_pixel_ref_new_from_existing(void* context, void* pixelRef); -SK_X_API sk_pixel_ref_t* sk_managed_pixel_ref_new(void* context, int32_t, int32_t, void*, size_t); -SK_X_API void sk_managed_pixel_ref_delete(sk_pixel_ref_t*); -SK_X_API sk_isize_t sk_managed_pixel_ref_dimensions(sk_pixel_ref_t*); -SK_X_API int32_t sk_managed_pixel_ref_width(sk_pixel_ref_t*); -SK_X_API int32_t sk_managed_pixel_ref_height(sk_pixel_ref_t*); -SK_X_API size_t sk_managed_pixel_ref_rowBytes(sk_pixel_ref_t*); -SK_X_API void* sk_managed_pixel_ref_pixels(sk_pixel_ref_t*); -SK_X_API void* sk_managed_pixel_ref_pixel_ref(sk_pixel_ref_t* d); -SK_X_API uint32_t sk_managed_pixel_ref_generation_id(sk_pixel_ref_t*); -SK_X_API void sk_managed_pixel_ref_notify_pixels_changed(sk_pixel_ref_t*); -SK_X_API bool sk_managed_pixel_ref_is_immutable(sk_pixel_ref_t*); -SK_X_API void sk_managed_pixel_ref_set_immutable(sk_pixel_ref_t*); -//SK_X_API void sk_managed_pixel_ref_add_generation_id_listener(sk_pixel_ref_t*, sk_id_change_listener_t*); -SK_X_API void sk_managed_pixel_ref_notify_added_to_cache(sk_pixel_ref_t*); -SK_X_API void sk_managed_pixel_ref_set_procs(sk_pixel_ref_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..726077a9dce3 --- /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_id_change_listener_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 c3670223d7c0..272e39be3c02 100644 --- a/src/c/sk_bitmap.cpp +++ b/src/c/sk_bitmap.cpp @@ -138,11 +138,11 @@ 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_pixel_ref(sk_bitmap_t* cbitmap) { +SK_C_API void* sk_bitmap_get_pixelref(sk_bitmap_t* cbitmap) { return AsBitmap(cbitmap)->pixelRef(); } -SK_C_API void sk_bitmap_set_pixel_ref(sk_bitmap_t* cbitmap, void* cpixelref, int x, int y) { +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); } diff --git a/src/xamarin/SkiaKeeper.c b/src/xamarin/SkiaKeeper.c index c77f9cf18957..56cb5799ad54 100644 --- a/src/xamarin/SkiaKeeper.c +++ b/src/xamarin/SkiaKeeper.c @@ -54,7 +54,7 @@ #include "include/xamarin/sk_managedstream.h" // placed here to help seperate in PR #include "include/xamarin/sk_managedallocator.h" -#include "include/xamarin/sk_managed_pixel_ref.h" +#include "include/xamarin/sk_managedpixelref.h" #include "include/xamarin/sk_manageddrawable.h" #include "include/xamarin/sk_managedtracememorydump.h" #include "include/xamarin/sk_compatpaint.h" @@ -112,8 +112,8 @@ void** KeepSkiaCSymbols (void) (void*)sk_managedstream_new, // placed here to help seperate in PR (void*)sk_managedallocator_new, - (void*)sk_managed_pixel_ref_new, - (void*)sk_managed_pixel_ref_new_from_existing, + (void*)sk_managedpixelref_new, + (void*)sk_managedpixelref_new_from_existing, (void*)sk_manageddrawable_new, (void*)sk_managedtracememorydump_new, }; diff --git a/src/xamarin/sk_managed_pixel_ref.cpp b/src/xamarin/sk_managedpixelref.cpp similarity index 61% rename from src/xamarin/sk_managed_pixel_ref.cpp rename to src/xamarin/sk_managedpixelref.cpp index cab98f5e93a7..53a2f3861f80 100644 --- a/src/xamarin/sk_managed_pixel_ref.cpp +++ b/src/xamarin/sk_managedpixelref.cpp @@ -7,21 +7,21 @@ #include "include/xamarin/SkManagedPixelRef.h" #include "include/xamarin/SkManaged_ID_Change_Listener.h" -#include "include/xamarin/sk_managed_pixel_ref.h" +#include "include/xamarin/sk_managedpixelref.h" #include "src/c/sk_types_priv.h" -static inline SkManagedPixelRef* AsSkManagedPixelRef(sk_pixel_ref_t* d) { +static inline SkManagedPixelRef* AsSkManagedPixelRef(sk_pixelref_t* d) { return reinterpret_cast(d); } -static inline sk_pixel_ref_t* ToSkPixelRef(SkManagedPixelRef* d) { - return reinterpret_cast(d); +static inline sk_pixelref_t* ToSkPixelRef(SkManagedPixelRef* d) { + return reinterpret_cast(d); } static inline SkManaged_ID_Change_Listener* AsSkManaged_ID_Change_Listener(sk_id_change_listener_t* d) { return reinterpret_cast(d); } -static sk_pixel_ref_procs_t gProcs; +static sk_pixelref_procs_t gProcs; void destroy(SkManagedPixelRef* d, void* context) { if (gProcs.fDestroy) { @@ -29,61 +29,61 @@ void destroy(SkManagedPixelRef* d, void* context) { } } -sk_pixel_ref_t* sk_managed_pixel_ref_new_from_existing(void* context, void* pixelRef) { +sk_pixelref_t* sk_managedpixelref_new_from_existing(void* context, void* pixelRef) { return ToSkPixelRef(new SkManagedPixelRef(context, (SkPixelRef*)pixelRef)); } -sk_pixel_ref_t* sk_managed_pixel_ref_new( +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_managed_pixel_ref_delete(sk_pixel_ref_t* d) { +void sk_managedpixelref_delete(sk_pixelref_t* d) { delete AsSkManagedPixelRef(d); } -sk_isize_t sk_managed_pixel_ref_dimensions(sk_pixel_ref_t* d) { +sk_isize_t sk_managedpixelref_dimensions(sk_pixelref_t* d) { return ToISize(AsSkManagedPixelRef(d)->pixelRef->dimensions()); } -int32_t sk_managed_pixel_ref_width(sk_pixel_ref_t* d) { +int32_t sk_managedpixelref_width(sk_pixelref_t* d) { return AsSkManagedPixelRef(d)->pixelRef->width(); } -int32_t sk_managed_pixel_ref_height(sk_pixel_ref_t* d) { +int32_t sk_managedpixelref_height(sk_pixelref_t* d) { return AsSkManagedPixelRef(d)->pixelRef->height(); } -size_t sk_managed_pixel_ref_rowBytes(sk_pixel_ref_t* d) { +size_t sk_managedpixelref_rowBytes(sk_pixelref_t* d) { return AsSkManagedPixelRef(d)->pixelRef->rowBytes(); } -void* sk_managed_pixel_ref_pixels(sk_pixel_ref_t* d) { +void* sk_managedpixelref_pixels(sk_pixelref_t* d) { return AsSkManagedPixelRef(d)->pixelRef->pixels(); } -void* sk_managed_pixel_ref_pixel_ref(sk_pixel_ref_t* d) { +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_managed_pixel_ref_generation_id(sk_pixel_ref_t* d) { +uint32_t sk_managedpixelref_generation_id(sk_pixelref_t* d) { return AsSkManagedPixelRef(d)->pixelRef->getGenerationID(); } -void sk_managed_pixel_ref_notify_pixels_changed(sk_pixel_ref_t* d) { +void sk_managedpixelref_notify_pixels_changed(sk_pixelref_t* d) { AsSkManagedPixelRef(d)->pixelRef->notifyPixelsChanged(); } -bool sk_managed_pixel_ref_is_immutable(sk_pixel_ref_t* d) { +bool sk_managedpixelref_is_immutable(sk_pixelref_t* d) { return AsSkManagedPixelRef(d)->pixelRef->isImmutable(); } -void sk_managed_pixel_ref_set_immutable(sk_pixel_ref_t* d) { +void sk_managedpixelref_set_immutable(sk_pixelref_t* d) { AsSkManagedPixelRef(d)->pixelRef->setImmutable(); } -//void sk_managed_pixel_ref_add_generation_id_listener(sk_pixel_ref_t* d, sk_id_change_listener_t* listener) { +//void sk_managedpixelref_add_generation_id_listener(sk_pixelref_t* d, sk_id_change_listener_t* listener) { // AsSkManagedPixelRef(d)->pixelRef->addGenIDChangeListener(sk_ref_sp(AsSkManaged_ID_Change_Listener(listener))); //} -void sk_managed_pixel_ref_notify_added_to_cache(sk_pixel_ref_t* d) { +void sk_managedpixelref_notify_added_to_cache(sk_pixelref_t* d) { AsSkManagedPixelRef(d)->pixelRef->notifyAddedToCache(); } -void sk_managed_pixel_ref_set_procs(sk_pixel_ref_procs_t procs) { +void sk_managedpixelref_set_procs(sk_pixelref_procs_t procs) { gProcs = procs; SkManagedPixelRef::Procs p; From 7bd6544ce15d3bd58c0ede1bb1f9ebca6264fe3a Mon Sep 17 00:00:00 2001 From: mgood7123 Date: Sat, 9 Jul 2022 21:43:42 +1000 Subject: [PATCH 4/4] remerge --- BUILD.gn | 4 ++ include/c/sk_types.h | 3 + include/xamarin/SkManagedIDChangeListener.h | 49 ++++++++++++++ .../xamarin/SkManagedIDChangeListenerList.h | 41 ++++++++++++ include/xamarin/sk_managedidchangelistener.h | 35 ++++++++++ .../xamarin/sk_managedidchangelistenerlist.h | 35 ++++++++++ include/xamarin/sk_managedpixelref.h | 2 +- src/c/sk_id_change_listener.cpp | 43 ++++++++++++ src/c/sk_types_priv.h | 5 ++ src/xamarin/SkManagedIDChangeListener.cpp | 30 +++++++++ src/xamarin/SkManagedIDChangeListenerList.cpp | 24 +++++++ src/xamarin/SkiaKeeper.c | 4 ++ src/xamarin/sk_managedidchangelistener.cpp | 59 +++++++++++++++++ .../sk_managedidchangelistenerlist.cpp | 66 +++++++++++++++++++ src/xamarin/sk_managedpixelref.cpp | 12 ++-- 15 files changed, 405 insertions(+), 7 deletions(-) create mode 100644 include/xamarin/SkManagedIDChangeListener.h create mode 100644 include/xamarin/SkManagedIDChangeListenerList.h create mode 100644 include/xamarin/sk_managedidchangelistener.h create mode 100644 include/xamarin/sk_managedidchangelistenerlist.h create mode 100644 src/c/sk_id_change_listener.cpp create mode 100644 src/xamarin/SkManagedIDChangeListener.cpp create mode 100644 src/xamarin/SkManagedIDChangeListenerList.cpp create mode 100644 src/xamarin/sk_managedidchangelistener.cpp create mode 100644 src/xamarin/sk_managedidchangelistenerlist.cpp diff --git a/BUILD.gn b/BUILD.gn index c221acdded0d..c2664deef249 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -3089,6 +3089,8 @@ 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", @@ -3097,6 +3099,8 @@ skiasharp_build("SkiaSharp") { "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", diff --git a/include/c/sk_types.h b/include/c/sk_types.h index aaca718bd610..bc4b2694bd5e 100644 --- a/include/c/sk_types.h +++ b/include/c/sk_types.h @@ -351,6 +351,9 @@ 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/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/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 index 726077a9dce3..76f6dc329713 100644 --- a/include/xamarin/sk_managedpixelref.h +++ b/include/xamarin/sk_managedpixelref.h @@ -35,7 +35,7 @@ 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_id_change_listener_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); 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 b49e81fe6159..adea0e07856a 100644 --- a/src/c/sk_types_priv.h +++ b/src/c/sk_types_priv.h @@ -182,6 +182,11 @@ 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/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/SkiaKeeper.c b/src/xamarin/SkiaKeeper.c index 56cb5799ad54..c1e0c02f0f16 100644 --- a/src/xamarin/SkiaKeeper.c +++ b/src/xamarin/SkiaKeeper.c @@ -57,6 +57,8 @@ #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); @@ -116,6 +118,8 @@ void** KeepSkiaCSymbols (void) (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_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 index 53a2f3861f80..a81a0c00325f 100644 --- a/src/xamarin/sk_managedpixelref.cpp +++ b/src/xamarin/sk_managedpixelref.cpp @@ -6,7 +6,7 @@ */ #include "include/xamarin/SkManagedPixelRef.h" -#include "include/xamarin/SkManaged_ID_Change_Listener.h" +#include "include/xamarin/SkManagedIDChangeListener.h" #include "include/xamarin/sk_managedpixelref.h" #include "src/c/sk_types_priv.h" @@ -17,8 +17,8 @@ static inline sk_pixelref_t* ToSkPixelRef(SkManagedPixelRef* d) { return reinterpret_cast(d); } -static inline SkManaged_ID_Change_Listener* AsSkManaged_ID_Change_Listener(sk_id_change_listener_t* d) { - return reinterpret_cast(d); +static inline SkManagedIDChangeListener* AsSkManagedIDChangeListener(sk_idchangelistener_t* d) { + return reinterpret_cast(d); } static sk_pixelref_procs_t gProcs; @@ -76,9 +76,9 @@ bool sk_managedpixelref_is_immutable(sk_pixelref_t* d) { 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_id_change_listener_t* listener) { -// AsSkManagedPixelRef(d)->pixelRef->addGenIDChangeListener(sk_ref_sp(AsSkManaged_ID_Change_Listener(listener))); -//} +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(); }