From f54c3279685abb4973ba3cfeeae1f2014f0350e2 Mon Sep 17 00:00:00 2001 From: mgood7123 Date: Thu, 30 Jun 2022 22:46:38 +1000 Subject: [PATCH 1/3] 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/3] 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/3] 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;