diff --git a/gn/core.gni b/gn/core.gni index 90e4ac954318..1a36a92fea2f 100644 --- a/gn/core.gni +++ b/gn/core.gni @@ -16,6 +16,7 @@ skia_core_sources = [ "$_include/c/sk_data.h", "$_include/c/sk_document.h", "$_include/c/sk_drawable.h", + "$_include/c/sk_general.h", "$_include/c/sk_image.h", "$_include/c/sk_imagefilter.h", "$_include/c/sk_mask.h", @@ -49,6 +50,7 @@ skia_core_sources = [ "$_src/c/sk_document.cpp", "$_src/c/sk_drawable.cpp", "$_src/c/sk_enums.cpp", + "$_src/c/sk_general.cpp", "$_src/c/sk_image.cpp", "$_src/c/sk_imagefilter.cpp", "$_src/c/sk_mask.cpp", diff --git a/include/c/sk_general.h b/include/c/sk_general.h new file mode 100644 index 000000000000..2c493dab01cc --- /dev/null +++ b/include/c/sk_general.h @@ -0,0 +1,33 @@ +/* + * Copyright 2019 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_general_DEFINED +#define sk_general_DEFINED + +#include "sk_types.h" + +SK_C_PLUS_PLUS_BEGIN_GUARD + +// ref counting + +SK_C_API bool sk_refcnt_unique(const sk_refcnt_t* refcnt); +SK_C_API int sk_refcnt_get_ref_count(const sk_refcnt_t* refcnt); +SK_C_API void sk_refcnt_safe_ref(sk_refcnt_t* refcnt); +SK_C_API void sk_refcnt_safe_unref(sk_refcnt_t* refcnt); + +SK_C_API bool sk_nvrefcnt_unique(const sk_nvrefcnt_t* refcnt); +SK_C_API int sk_nvrefcnt_get_ref_count(const sk_nvrefcnt_t* refcnt); +SK_C_API void sk_nvrefcnt_safe_ref(sk_nvrefcnt_t* refcnt); +SK_C_API void sk_nvrefcnt_safe_unref(sk_nvrefcnt_t* refcnt); + +// color type + +SK_C_API sk_colortype_t sk_colortype_get_default_8888(void); + +SK_C_PLUS_PLUS_END_GUARD + +#endif diff --git a/include/c/sk_stream.h b/include/c/sk_stream.h index 8dbdf8e04ae9..3a258b05b8bc 100644 --- a/include/c/sk_stream.h +++ b/include/c/sk_stream.h @@ -52,6 +52,9 @@ SK_C_API bool sk_stream_move(sk_stream_t* cstream, long offset); SK_C_API bool sk_stream_has_length(sk_stream_t* cstream); SK_C_API size_t sk_stream_get_length(sk_stream_t* cstream); SK_C_API const void* sk_stream_get_memory_base(sk_stream_t* cstream); +SK_C_API sk_stream_t* sk_stream_fork(sk_stream_t* cstream); +SK_C_API sk_stream_t* sk_stream_duplicate(sk_stream_t* cstream); +SK_C_API void sk_stream_destroy(sk_stream_t* cstream); //////////////////////////////////////////////////////////////////////////////// diff --git a/include/c/sk_surface.h b/include/c/sk_surface.h index bb7c26f0f3a3..3d8766ea0494 100644 --- a/include/c/sk_surface.h +++ b/include/c/sk_surface.h @@ -14,8 +14,6 @@ SK_C_PLUS_PLUS_BEGIN_GUARD -SK_C_API sk_colortype_t sk_colortype_get_default_8888(void); - // surface SK_C_API sk_surface_t* sk_surface_new_null(int width, int height); diff --git a/include/c/sk_types.h b/include/c/sk_types.h index 2ebc13872975..1a36269a8a9f 100644 --- a/include/c/sk_types.h +++ b/include/c/sk_types.h @@ -42,6 +42,9 @@ SK_C_PLUS_PLUS_BEGIN_GUARD +typedef struct sk_refcnt_t sk_refcnt_t; +typedef struct sk_nvrefcnt_t sk_nvrefcnt_t; + typedef uint32_t sk_color_t; typedef uint32_t sk_pmcolor_t; diff --git a/include/c/sk_vertices.h b/include/c/sk_vertices.h index 6225bdb15df2..277c39015137 100644 --- a/include/c/sk_vertices.h +++ b/include/c/sk_vertices.h @@ -15,6 +15,7 @@ SK_C_PLUS_PLUS_BEGIN_GUARD SK_C_API void sk_vertices_unref(sk_vertices_t* cvertices); +SK_C_API void sk_vertices_ref(sk_vertices_t* cvertices); SK_C_API sk_vertices_t* sk_vertices_make_copy(sk_vertices_vertex_mode_t vmode, int vertexCount, const sk_point_t* positions, const sk_point_t* texs, const sk_color_t* colors, int indexCount, const uint16_t* indices); SK_C_PLUS_PLUS_END_GUARD diff --git a/include/core/SkRefCnt.h b/include/core/SkRefCnt.h index 0c3b23a7d1fc..134bc7dc88de 100644 --- a/include/core/SkRefCnt.h +++ b/include/core/SkRefCnt.h @@ -88,6 +88,8 @@ class SK_API SkRefCntBase : SkNoncopyable { } } + int32_t getRefCount() const { return fRefCnt.load(std::memory_order_relaxed); } + protected: /** * Allow subclasses to call this if they've overridden internal_dispose @@ -227,6 +229,8 @@ class SkNVRefCnt : SkNoncopyable { } void deref() const { this->unref(); } + int32_t getRefCount() const { return fRefCnt.load(std::memory_order_relaxed); } + private: mutable std::atomic fRefCnt; int32_t getRefCnt() const { diff --git a/include/xamarin/SkManagedDrawable.h b/include/xamarin/SkManagedDrawable.h index 0204897bcb49..ea10891cbcb3 100644 --- a/include/xamarin/SkManagedDrawable.h +++ b/include/xamarin/SkManagedDrawable.h @@ -19,27 +19,39 @@ struct SkRect; class SK_API SkManagedDrawable; // delegate declarations -typedef void (*draw_delegate) (SkManagedDrawable* managedDrawable, SkCanvas* canvas); -typedef void (*getBounds_delegate) (SkManagedDrawable* managedDrawable, SkRect* rect); -typedef SkPicture* (*newPictureSnapshot_delegate) (SkManagedDrawable* managedDrawable); // managed drawable class SkManagedDrawable : public SkDrawable { public: - SkManagedDrawable(); + SkManagedDrawable(void* context); virtual ~SkManagedDrawable(); - static void setDelegates(const draw_delegate pDraw, - const getBounds_delegate pgetBounds, - const newPictureSnapshot_delegate pNewPictureSnapshot); +public: + typedef void (*DrawProc) (SkManagedDrawable* d, void* context, SkCanvas* canvas); + typedef void (*GetBoundsProc) (SkManagedDrawable* d, void* context, SkRect* rect); + typedef SkPicture* (*NewPictureSnapshotProc) (SkManagedDrawable* d, void* context); + typedef void (*DestroyProc) (SkManagedDrawable* d, void* context); + + struct Procs { + DrawProc fDraw = nullptr; + GetBoundsProc fGetBounds = nullptr; + NewPictureSnapshotProc fNewPictureSnapshot = nullptr; + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(Procs procs); protected: void onDraw(SkCanvas*) override; SkRect onGetBounds() override; SkPicture* onNewPictureSnapshot() override; +private: + void* fContext; + static Procs fProcs; + typedef SkDrawable INHERITED; }; diff --git a/include/xamarin/SkManagedStream.h b/include/xamarin/SkManagedStream.h index 3bbb0bb64f21..e0d9c078e056 100644 --- a/include/xamarin/SkManagedStream.h +++ b/include/xamarin/SkManagedStream.h @@ -12,97 +12,109 @@ #include "SkTypes.h" #include "SkStream.h" - class SkManagedWStream; class SkManagedStream; -// delegate declarations -typedef size_t (*read_delegate) (SkManagedStream* managedStream, void* buffer, size_t size); -typedef size_t (*peek_delegate) (SkManagedStream* managedStream, void* buffer, size_t size); -typedef bool (*isAtEnd_delegate) (const SkManagedStream* managedStream); -typedef bool (*hasPosition_delegate) (const SkManagedStream* managedStream); -typedef bool (*hasLength_delegate) (const SkManagedStream* managedStream); -typedef bool (*rewind_delegate) (SkManagedStream* managedStream); -typedef size_t (*getPosition_delegate) (const SkManagedStream* managedStream); -typedef bool (*seek_delegate) (SkManagedStream* managedStream, size_t position); -typedef bool (*move_delegate) (SkManagedStream* managedStream, long offset); -typedef size_t (*getLength_delegate) (const SkManagedStream* managedStream); -typedef SkManagedStream* (*createNew_delegate) (const SkManagedStream* managedStream); -typedef void (*destroy_delegate) (size_t managedStream); - -// delegate declarations -typedef bool (*write_delegate) (SkManagedWStream* managedStream, const void* buffer, size_t size); -typedef void (*flush_delegate) (SkManagedWStream* managedStream); -typedef size_t (*bytesWritten_delegate) (const SkManagedWStream* managedStream); -typedef void (*wdestroy_delegate) (size_t managedStream); - - -// managed stream wrapper +// READ-ONLY MANAGED STREAM + class SkManagedStream : public SkStreamAsset { public: - SkManagedStream(); - + SkManagedStream(void* context); + virtual ~SkManagedStream(); - static void setDelegates(const read_delegate pRead, - const peek_delegate pPeek, - const isAtEnd_delegate pIsAtEnd, - const hasPosition_delegate pHasPosition, - const hasLength_delegate pHasLength, - const rewind_delegate pRewind, - const getPosition_delegate pGetPosition, - const seek_delegate pSeek, - const move_delegate pMove, - const getLength_delegate pGetLength, - const createNew_delegate pCreateNew, - const destroy_delegate pDestroy); - size_t read(void* buffer, size_t size) override; bool isAtEnd() const override; bool hasPosition() const override; bool hasLength() const override; - + size_t peek(void* buffer, size_t size) const override; - + bool rewind() override; - + size_t getPosition() const override; bool seek(size_t position) override; bool move(long offset) override; - + size_t getLength() const override; - -private: - size_t address; +public: + typedef size_t (*ReadProc) ( SkManagedStream* s, void* context, void* buffer, size_t size); + typedef size_t (*PeekProc) (const SkManagedStream* s, void* context, void* buffer, size_t size); + typedef bool (*IsAtEndProc) (const SkManagedStream* s, void* context); + typedef bool (*HasPositionProc) (const SkManagedStream* s, void* context); + typedef bool (*HasLengthProc) (const SkManagedStream* s, void* context); + typedef bool (*RewindProc) ( SkManagedStream* s, void* context); + typedef size_t (*GetPositionProc) (const SkManagedStream* s, void* context); + typedef bool (*SeekProc) ( SkManagedStream* s, void* context, size_t position); + typedef bool (*MoveProc) ( SkManagedStream* s, void* context, long offset); + typedef size_t (*GetLengthProc) (const SkManagedStream* s, void* context); + typedef SkManagedStream* (*DuplicateProc) (const SkManagedStream* s, void* context); + typedef SkManagedStream* (*ForkProc) (const SkManagedStream* s, void* context); + typedef void (*DestroyProc) ( SkManagedStream* s, void* context); + + struct Procs { + ReadProc fRead = nullptr; + PeekProc fPeek = nullptr; + IsAtEndProc fIsAtEnd = nullptr; + HasPositionProc fHasPosition = nullptr; + HasLengthProc fHasLength = nullptr; + RewindProc fRewind = nullptr; + GetPositionProc fGetPosition = nullptr; + SeekProc fSeek = nullptr; + MoveProc fMove = nullptr; + GetLengthProc fGetLength = nullptr; + DuplicateProc fDuplicate = nullptr; + ForkProc fFork = nullptr; + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(SkManagedStream::Procs procs); + +private: SkStreamAsset* onDuplicate() const override; SkStreamAsset* onFork() const override; +private: + void* fContext; + static Procs fProcs; + typedef SkStreamAsset INHERITED; }; -// managed wstream wrapper +// WRITEABLE MANAGED STREAM + class SkManagedWStream : public SkWStream { public: - SkManagedWStream(); + SkManagedWStream(void* context); virtual ~SkManagedWStream(); - static void setDelegates(const write_delegate pWrite, - const flush_delegate pFlush, - const bytesWritten_delegate pBytesWritten, - const wdestroy_delegate pDestroy); - bool write(const void* buffer, size_t size) override; void flush() override; size_t bytesWritten() const override; - + +public: + typedef bool (*WriteProc) ( SkManagedWStream* s, void* context, const void* buffer, size_t size); + typedef void (*FlushProc) ( SkManagedWStream* s, void* context); + typedef size_t (*BytesWrittenProc) (const SkManagedWStream* s, void* context); + typedef void (*DestroyProc) ( SkManagedWStream* s, void* context); + + struct Procs { + WriteProc fWrite = nullptr; + FlushProc fFlush = nullptr; + BytesWrittenProc fBytesWritten = nullptr; + DestroyProc fDestroy = nullptr; + }; + + static void setProcs(SkManagedWStream::Procs procs); + private: - size_t address; - + void* fContext; + static Procs fProcs; + typedef SkWStream INHERITED; }; - #endif diff --git a/include/xamarin/sk_manageddrawable.h b/include/xamarin/sk_manageddrawable.h index f603c05835f2..cb1de3ce49ad 100644 --- a/include/xamarin/sk_manageddrawable.h +++ b/include/xamarin/sk_manageddrawable.h @@ -15,20 +15,23 @@ SK_C_PLUS_PLUS_BEGIN_GUARD - typedef struct sk_manageddrawable_t sk_manageddrawable_t; - -typedef void (*sk_manageddrawable_draw_delegate) (sk_manageddrawable_t* cmanagedDrawable, sk_canvas_t* ccanvas); -typedef void (*sk_manageddrawable_getBounds_delegate) (sk_manageddrawable_t* cmanagedDrawable, sk_rect_t* rect); -typedef sk_picture_t* (*sk_manageddrawable_newPictureSnapshot_delegate) (sk_manageddrawable_t* cmanagedDrawable); - - -SK_X_API sk_manageddrawable_t* sk_manageddrawable_new (void); -SK_X_API void sk_manageddrawable_destroy (sk_manageddrawable_t*); -SK_X_API void sk_manageddrawable_set_delegates (const sk_manageddrawable_draw_delegate pDraw, - const sk_manageddrawable_getBounds_delegate pGetBounds, - const sk_manageddrawable_newPictureSnapshot_delegate pNewPictureSnapshot); +typedef void (*sk_manageddrawable_draw_proc) (sk_manageddrawable_t* d, void* context, sk_canvas_t* ccanvas); +typedef void (*sk_manageddrawable_getBounds_proc) (sk_manageddrawable_t* d, void* context, sk_rect_t* rect); +typedef sk_picture_t* (*sk_manageddrawable_newPictureSnapshot_proc) (sk_manageddrawable_t* d, void* context); +typedef void (*sk_manageddrawable_destroy_proc) (sk_manageddrawable_t* d, void* context); + +typedef struct { + sk_manageddrawable_draw_proc fDraw; + sk_manageddrawable_getBounds_proc fGetBounds; + sk_manageddrawable_newPictureSnapshot_proc fNewPictureSnapshot; + sk_manageddrawable_destroy_proc fDestroy; +} sk_manageddrawable_procs_t; + +SK_X_API sk_manageddrawable_t* sk_manageddrawable_new(void* context); +SK_X_API void sk_manageddrawable_unref(sk_manageddrawable_t*); +SK_X_API void sk_manageddrawable_set_procs(sk_manageddrawable_procs_t procs); SK_C_PLUS_PLUS_END_GUARD diff --git a/include/xamarin/sk_managedstream.h b/include/xamarin/sk_managedstream.h index 2f8983d28da2..29cefd51d9e8 100644 --- a/include/xamarin/sk_managedstream.h +++ b/include/xamarin/sk_managedstream.h @@ -15,59 +15,66 @@ SK_C_PLUS_PLUS_BEGIN_GUARD +// WRITEABLE MANAGED STREAM typedef struct sk_wstream_managedstream_t sk_wstream_managedstream_t; +typedef bool (*sk_managedwstream_write_proc) ( sk_wstream_managedstream_t* s, void* context, const void* buffer, size_t size); +typedef void (*sk_managedwstream_flush_proc) ( sk_wstream_managedstream_t* s, void* context); +typedef size_t (*sk_managedwstream_bytesWritten_proc) (const sk_wstream_managedstream_t* s, void* context); +typedef void (*sk_managedwstream_destroy_proc) ( sk_wstream_managedstream_t* s, void* context); -typedef bool (*sk_managedwstream_write_delegate) (sk_wstream_managedstream_t* cmanagedStream, const void* buffer, size_t size); -typedef void (*sk_managedwstream_flush_delegate) (sk_wstream_managedstream_t* cmanagedStream); -typedef size_t (*sk_managedwstream_bytesWritten_delegate) (const sk_wstream_managedstream_t* cmanagedStream); -typedef void (*sk_managedwstream_destroy_delegate) (size_t cmanagedStream); +typedef struct { + sk_managedwstream_write_proc fWrite; + sk_managedwstream_flush_proc fFlush; + sk_managedwstream_bytesWritten_proc fBytesWritten; + sk_managedwstream_destroy_proc fDestroy; +} sk_managedwstream_procs_t; +SK_X_API void sk_managedwstream_set_procs(sk_managedwstream_procs_t procs); -SK_X_API sk_wstream_managedstream_t* sk_managedwstream_new (void); -SK_X_API void sk_managedwstream_destroy (sk_wstream_managedstream_t*); +SK_X_API sk_wstream_managedstream_t* sk_managedwstream_new(void* context); +SK_X_API void sk_managedwstream_destroy(sk_wstream_managedstream_t* s); -SK_X_API void sk_managedwstream_set_delegates (const sk_managedwstream_write_delegate pWrite, - const sk_managedwstream_flush_delegate pFlush, - const sk_managedwstream_bytesWritten_delegate pBytesWritten, - const sk_managedwstream_destroy_delegate pDestroy); +// READ-ONLY MANAGED STREAM typedef struct sk_stream_managedstream_t sk_stream_managedstream_t; - -typedef size_t (*sk_managedstream_read_delegate) (sk_stream_managedstream_t* cmanagedStream, void* buffer, size_t size); -typedef size_t (*sk_managedstream_peek_delegate) (sk_stream_managedstream_t* cmanagedStream, void* buffer, size_t size); -typedef bool (*sk_managedstream_isAtEnd_delegate) (const sk_stream_managedstream_t* cmanagedStream); -typedef bool (*sk_managedstream_hasPosition_delegate) (const sk_stream_managedstream_t* cmanagedStream); -typedef bool (*sk_managedstream_hasLength_delegate) (const sk_stream_managedstream_t* cmanagedStream); -typedef bool (*sk_managedstream_rewind_delegate) (sk_stream_managedstream_t* cmanagedStream); -typedef size_t (*sk_managedstream_getPosition_delegate) (const sk_stream_managedstream_t* cmanagedStream); -typedef bool (*sk_managedstream_seek_delegate) (sk_stream_managedstream_t* cmanagedStream, size_t position); -typedef bool (*sk_managedstream_move_delegate) (sk_stream_managedstream_t* cmanagedStream, long offset); -typedef size_t (*sk_managedstream_getLength_delegate) (const sk_stream_managedstream_t* cmanagedStream); -typedef sk_stream_managedstream_t* (*sk_managedstream_createNew_delegate) (const sk_stream_managedstream_t* cmanagedStream); -typedef void (*sk_managedstream_destroy_delegate) (size_t cmanagedStream); - - -// c API -SK_X_API sk_stream_managedstream_t* sk_managedstream_new (void); -SK_X_API void sk_managedstream_destroy (sk_stream_managedstream_t*); - -SK_X_API void sk_managedstream_set_delegates (const sk_managedstream_read_delegate pRead, - const sk_managedstream_peek_delegate pPeek, - const sk_managedstream_isAtEnd_delegate pIsAtEnd, - const sk_managedstream_hasPosition_delegate pHasPosition, - const sk_managedstream_hasLength_delegate pHasLength, - const sk_managedstream_rewind_delegate pRewind, - const sk_managedstream_getPosition_delegate pGetPosition, - const sk_managedstream_seek_delegate pSeek, - const sk_managedstream_move_delegate pMove, - const sk_managedstream_getLength_delegate pGetLength, - const sk_managedstream_createNew_delegate pCreateNew, - const sk_managedstream_destroy_delegate pDestroy); - +typedef size_t (*sk_managedstream_read_proc) ( sk_stream_managedstream_t* s, void* context, void* buffer, size_t size); +typedef size_t (*sk_managedstream_peek_proc) (const sk_stream_managedstream_t* s, void* context, void* buffer, size_t size); +typedef bool (*sk_managedstream_isAtEnd_proc) (const sk_stream_managedstream_t* s, void* context); +typedef bool (*sk_managedstream_hasPosition_proc) (const sk_stream_managedstream_t* s, void* context); +typedef bool (*sk_managedstream_hasLength_proc) (const sk_stream_managedstream_t* s, void* context); +typedef bool (*sk_managedstream_rewind_proc) ( sk_stream_managedstream_t* s, void* context); +typedef size_t (*sk_managedstream_getPosition_proc) (const sk_stream_managedstream_t* s, void* context); +typedef bool (*sk_managedstream_seek_proc) ( sk_stream_managedstream_t* s, void* context, size_t position); +typedef bool (*sk_managedstream_move_proc) ( sk_stream_managedstream_t* s, void* context, long offset); +typedef size_t (*sk_managedstream_getLength_proc) (const sk_stream_managedstream_t* s, void* context); +typedef sk_stream_managedstream_t* (*sk_managedstream_duplicate_proc) (const sk_stream_managedstream_t* s, void* context); +typedef sk_stream_managedstream_t* (*sk_managedstream_fork_proc) (const sk_stream_managedstream_t* s, void* context); +typedef void (*sk_managedstream_destroy_proc) ( sk_stream_managedstream_t* s, void* context); + +typedef struct { + sk_managedstream_read_proc fRead; + sk_managedstream_peek_proc fPeek; + sk_managedstream_isAtEnd_proc fIsAtEnd; + sk_managedstream_hasPosition_proc fHasPosition; + sk_managedstream_hasLength_proc fHasLength; + sk_managedstream_rewind_proc fRewind; + sk_managedstream_getPosition_proc fGetPosition; + sk_managedstream_seek_proc fSeek; + sk_managedstream_move_proc fMove; + sk_managedstream_getLength_proc fGetLength; + sk_managedstream_duplicate_proc fDuplicate; + sk_managedstream_fork_proc fFork; + sk_managedstream_destroy_proc fDestroy; +} sk_managedstream_procs_t; + +SK_X_API void sk_managedstream_set_procs(sk_managedstream_procs_t procs); + +SK_X_API sk_stream_managedstream_t* sk_managedstream_new(void* context); +SK_X_API void sk_managedstream_destroy(sk_stream_managedstream_t* s); SK_C_PLUS_PLUS_END_GUARD diff --git a/src/c/sk_general.cpp b/src/c/sk_general.cpp new file mode 100644 index 000000000000..007faa81ed8c --- /dev/null +++ b/src/c/sk_general.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2019 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 "SkTypes.h" +#include "SkRefCnt.h" + +#include "sk_types.h" +#include "sk_general.h" + +#include "sk_types_priv.h" + +static inline const SkRefCnt* AsRefCnt(const sk_refcnt_t* t) { + return reinterpret_cast(t); +} +static inline const SkNVRefCnt* AsNVRefCnt(const sk_nvrefcnt_t* t) { + return reinterpret_cast*>(t); +} + +// ref counting + +bool sk_refcnt_unique(const sk_refcnt_t* refcnt) { + return AsRefCnt(refcnt)->unique(); +} +int sk_refcnt_get_ref_count(const sk_refcnt_t* refcnt) { + return AsRefCnt(refcnt)->getRefCount(); +} +void sk_refcnt_safe_ref(sk_refcnt_t* refcnt) { + SkSafeRef(AsRefCnt(refcnt)); +} +void sk_refcnt_safe_unref(sk_refcnt_t* refcnt) { + SkSafeUnref(AsRefCnt(refcnt)); +} + +bool sk_nvrefcnt_unique(const sk_nvrefcnt_t* refcnt) { + return AsNVRefCnt(refcnt)->unique(); +} +int sk_nvrefcnt_get_ref_count(const sk_nvrefcnt_t* refcnt) { + return AsNVRefCnt(refcnt)->getRefCount(); +} +void sk_nvrefcnt_safe_ref(sk_nvrefcnt_t* refcnt) { + SkSafeRef(AsNVRefCnt(refcnt)); +} +void sk_nvrefcnt_safe_unref(sk_nvrefcnt_t* refcnt) { + SkSafeUnref(AsNVRefCnt(refcnt)); +} + +// color type + +sk_colortype_t sk_colortype_get_default_8888() { + return (sk_colortype_t)SkColorType::kN32_SkColorType; +} diff --git a/src/c/sk_paint.cpp b/src/c/sk_paint.cpp index 03a3dfa6b896..056f37d4314d 100644 --- a/src/c/sk_paint.cpp +++ b/src/c/sk_paint.cpp @@ -119,11 +119,11 @@ void sk_paint_set_verticaltext(sk_paint_t* cpaint, bool vt) { } sk_shader_t* sk_paint_get_shader(sk_paint_t* cpaint) { - return ToShader(AsPaint(cpaint)->getShader()); + return ToShader(AsPaint(cpaint)->refShader().release()); } sk_maskfilter_t* sk_paint_get_maskfilter(sk_paint_t* cpaint) { - return ToMaskFilter(AsPaint(cpaint)->getMaskFilter()); + return ToMaskFilter(AsPaint(cpaint)->refMaskFilter().release()); } void sk_paint_set_colorfilter(sk_paint_t* cpaint, sk_colorfilter_t* cfilter) { @@ -131,7 +131,7 @@ void sk_paint_set_colorfilter(sk_paint_t* cpaint, sk_colorfilter_t* cfilter) { } sk_colorfilter_t* sk_paint_get_colorfilter(sk_paint_t* cpaint) { - return ToColorFilter(AsPaint(cpaint)->getColorFilter()); + return ToColorFilter(AsPaint(cpaint)->refColorFilter().release()); } void sk_paint_set_imagefilter(sk_paint_t* cpaint, sk_imagefilter_t* cfilter) { @@ -139,7 +139,7 @@ void sk_paint_set_imagefilter(sk_paint_t* cpaint, sk_imagefilter_t* cfilter) { } sk_imagefilter_t* sk_paint_get_imagefilter(sk_paint_t* cpaint) { - return ToImageFilter(AsPaint(cpaint)->getImageFilter()); + return ToImageFilter(AsPaint(cpaint)->refImageFilter().release()); } sk_blendmode_t sk_paint_get_blendmode(sk_paint_t* paint) { @@ -155,7 +155,7 @@ sk_filter_quality_t sk_paint_get_filter_quality(sk_paint_t* cpaint) { } sk_typeface_t* sk_paint_get_typeface(sk_paint_t* paint) { - return ToTypeface(AsPaint(paint)->getTypeface()); + return ToTypeface(AsPaint(paint)->refTypeface().release()); } void sk_paint_set_typeface(sk_paint_t* paint, sk_typeface_t* typeface) { @@ -227,7 +227,7 @@ float sk_paint_get_fontmetrics(sk_paint_t* cpaint, sk_fontmetrics_t* cfontmetric } sk_path_effect_t* sk_paint_get_path_effect(sk_paint_t* cpaint) { - return ToPathEffect(AsPaint(cpaint)->getPathEffect()); + return ToPathEffect(AsPaint(cpaint)->refPathEffect().release()); } void sk_paint_set_path_effect(sk_paint_t* cpaint, sk_path_effect_t* effect) { diff --git a/src/c/sk_stream.cpp b/src/c/sk_stream.cpp index b3d94f2d4d71..924d939ce27d 100644 --- a/src/c/sk_stream.cpp +++ b/src/c/sk_stream.cpp @@ -141,6 +141,18 @@ const void* sk_stream_get_memory_base(sk_stream_t* cstream) { return AsStream(cstream)->getMemoryBase(); } +sk_stream_t* sk_stream_fork(sk_stream_t* cstream) { + return ToStream(AsStream(cstream)->fork().release()); +} + +sk_stream_t* sk_stream_duplicate(sk_stream_t* cstream) { + return ToStream(AsStream(cstream)->duplicate().release()); +} + +void sk_stream_destroy(sk_stream_t* cstream) { + delete AsStream(cstream); +} + // file W stream diff --git a/src/c/sk_surface.cpp b/src/c/sk_surface.cpp index 41be3b302cd7..7b0aca60a8de 100644 --- a/src/c/sk_surface.cpp +++ b/src/c/sk_surface.cpp @@ -27,10 +27,6 @@ #include "sk_types_priv.h" -sk_colortype_t sk_colortype_get_default_8888() { - return (sk_colortype_t)SkColorType::kN32_SkColorType; -} - // surface sk_surface_t* sk_surface_new_null(int width, int height) { diff --git a/src/c/sk_vertices.cpp b/src/c/sk_vertices.cpp index f9fa6e6b396d..698126ae3b04 100644 --- a/src/c/sk_vertices.cpp +++ b/src/c/sk_vertices.cpp @@ -17,6 +17,10 @@ void sk_vertices_unref(sk_vertices_t* cvertices) { SkSafeUnref(AsVertices(cvertices)); } +void sk_vertices_ref(sk_vertices_t* cvertices) { + SkSafeRef(AsVertices(cvertices)); +} + sk_vertices_t* sk_vertices_make_copy(sk_vertices_vertex_mode_t vmode, int vertexCount, const sk_point_t* positions, const sk_point_t* texs, const sk_color_t* colors, int indexCount, const uint16_t* indices) { return ToVertices(SkVertices::MakeCopy( (SkVertices::VertexMode)vmode, vertexCount, AsPoint(positions), AsPoint(texs), colors, indexCount, indices).release()); diff --git a/src/xamarin/SkManagedDrawable.cpp b/src/xamarin/SkManagedDrawable.cpp index 084d87a91821..91f158db8478 100644 --- a/src/xamarin/SkManagedDrawable.cpp +++ b/src/xamarin/SkManagedDrawable.cpp @@ -11,38 +11,31 @@ #include "SkPicture.h" #include "SkManagedDrawable.h" +SkManagedDrawable::Procs SkManagedDrawable::fProcs; -// delegates -static draw_delegate fDraw = nullptr; -static getBounds_delegate fGetBounds = nullptr; -static newPictureSnapshot_delegate fNewPictureSnapshot = nullptr; - - -SkManagedDrawable::SkManagedDrawable() { +void SkManagedDrawable::setProcs(SkManagedDrawable::Procs procs) { + fProcs = procs; } -SkManagedDrawable::~SkManagedDrawable() { +SkManagedDrawable::SkManagedDrawable(void* context) { + fContext = context; } - -void SkManagedDrawable::setDelegates(const draw_delegate pDraw, - const getBounds_delegate pGetBounds, - const newPictureSnapshot_delegate pNewPictureSnapshot) -{ - ::fDraw = (pDraw); - ::fGetBounds = (pGetBounds); - ::fNewPictureSnapshot = (pNewPictureSnapshot); +SkManagedDrawable::~SkManagedDrawable() { + if (!fProcs.fDestroy) return; + fProcs.fDestroy(this, fContext); } void SkManagedDrawable::onDraw(SkCanvas* canvas) { - ::fDraw(this, canvas); + if (!fProcs.fDraw) return; + fProcs.fDraw(this, fContext, canvas); } - SkRect SkManagedDrawable::onGetBounds() { SkRect rect; - ::fGetBounds(this, &rect); + if (fProcs.fGetBounds) + fProcs.fGetBounds(this, fContext, &rect); return rect; } - SkPicture* SkManagedDrawable::onNewPictureSnapshot() { - return ::fNewPictureSnapshot(this); + if (!fProcs.fNewPictureSnapshot) return nullptr; + return fProcs.fNewPictureSnapshot(this, fContext); } diff --git a/src/xamarin/SkManagedStream.cpp b/src/xamarin/SkManagedStream.cpp index f81aa1892a12..8a91997ce52c 100644 --- a/src/xamarin/SkManagedStream.cpp +++ b/src/xamarin/SkManagedStream.cpp @@ -8,147 +8,97 @@ #include "SkManagedStream.h" +// READ-ONLY MANAGED STREAM -// read stream -static read_delegate fRead = nullptr; -static peek_delegate fPeek = nullptr; -static isAtEnd_delegate fIsAtEnd = nullptr; -static hasPosition_delegate fHasPosition = nullptr; -static hasLength_delegate fHasLength = nullptr; -static rewind_delegate fRewind = nullptr; -static getPosition_delegate fGetPosition = nullptr; -static seek_delegate fSeek = nullptr; -static move_delegate fMove = nullptr; -static getLength_delegate fGetLength = nullptr; -static createNew_delegate fCreateNew = nullptr; -static destroy_delegate fDestroy = nullptr; +SkManagedStream::Procs SkManagedStream::fProcs; -// write stream -static write_delegate fWrite = nullptr; -static flush_delegate fFlush = nullptr; -static bytesWritten_delegate fBytesWritten = nullptr; -static wdestroy_delegate fWDestroy = nullptr; - - -// the read stream - -SkManagedStream::SkManagedStream() { - this->address = (size_t)this; +void SkManagedStream::setProcs(SkManagedStream::Procs procs) { + fProcs = procs; } -SkManagedStream::~SkManagedStream() { - ::fDestroy(address); +SkManagedStream::SkManagedStream(void* context) { + fContext = context; } - -void SkManagedStream::setDelegates(const read_delegate pRead, - const peek_delegate pPeek, - const isAtEnd_delegate pIsAtEnd, - const hasPosition_delegate pHasPosition, - const hasLength_delegate pHasLength, - const rewind_delegate pRewind, - const getPosition_delegate pGetPosition, - const seek_delegate pSeek, - const move_delegate pMove, - const getLength_delegate pGetLength, - const createNew_delegate pCreateNew, - const destroy_delegate pDestroy) -{ - ::fRead = (pRead); - ::fPeek = (pPeek); - ::fIsAtEnd = (pIsAtEnd); - ::fHasPosition = (pHasPosition); - ::fHasLength = (pHasLength); - ::fRewind = (pRewind); - ::fGetPosition = (pGetPosition); - ::fSeek = (pSeek); - ::fMove = (pMove); - ::fGetLength = (pGetLength); - ::fCreateNew = (pCreateNew); - ::fDestroy = (pDestroy); +SkManagedStream::~SkManagedStream() { + if (!fProcs.fDestroy) return; + fProcs.fDestroy(this, fContext); } - size_t SkManagedStream::read(void* buffer, size_t size) { - return ::fRead(this, buffer, size); + if (!fProcs.fRead) return 0; + return fProcs.fRead(this, fContext, buffer, size); } - size_t SkManagedStream::peek(void *buffer, size_t size) const { - SkManagedStream* nonConstThis = const_cast(this); - return ::fPeek(nonConstThis, buffer, size); + if (!fProcs.fPeek) return 0; + return fProcs.fPeek(this, fContext, buffer, size); } - bool SkManagedStream::isAtEnd() const { - return ::fIsAtEnd(this); + if (!fProcs.fIsAtEnd) return false; + return fProcs.fIsAtEnd(this, fContext); } - bool SkManagedStream::hasPosition() const { - return ::fHasPosition(this); + if (!fProcs.fHasPosition) return false; + return fProcs.fHasPosition(this, fContext); } - bool SkManagedStream::hasLength() const { - return ::fHasLength(this); + if (!fProcs.fHasLength) return false; + return fProcs.fHasLength(this, fContext); } - bool SkManagedStream::rewind() { - return ::fRewind(this); + if (!fProcs.fRewind) return false; + return fProcs.fRewind(this, fContext); } - size_t SkManagedStream::getPosition() const { - return ::fGetPosition(this); + if (!fProcs.fGetPosition) return 0; + return fProcs.fGetPosition(this, fContext); } - bool SkManagedStream::seek(size_t position) { - return ::fSeek(this, position); + if (!fProcs.fSeek) return false; + return fProcs.fSeek(this, fContext, position); } - bool SkManagedStream::move(long offset) { - return ::fMove(this, offset); + if (!fProcs.fMove) return false; + return fProcs.fMove(this, fContext, offset); } - size_t SkManagedStream::getLength() const { - return ::fGetLength(this); + if (!fProcs.fGetLength) return 0; + return fProcs.fGetLength(this, fContext); } - SkStreamAsset* SkManagedStream::onDuplicate() const { - return ::fCreateNew(this); + if (!fProcs.fDuplicate) return nullptr; + return fProcs.fDuplicate(this, fContext); } - SkStreamAsset* SkManagedStream::onFork() const { - std::unique_ptr that(::fCreateNew(this)); - that->seek(getPosition()); - return that.release(); + if (!fProcs.fFork) return nullptr; + return fProcs.fFork(this, fContext); } -// the write stream +// WRITEABLE MANAGED STREAM -SkManagedWStream::SkManagedWStream() { - this->address = (size_t)this; -} +SkManagedWStream::Procs SkManagedWStream::fProcs; -SkManagedWStream::~SkManagedWStream() { - ::fWDestroy(address); +void SkManagedWStream::setProcs(SkManagedWStream::Procs procs) { + fProcs = procs; } -void SkManagedWStream::setDelegates(const write_delegate pWrite, - const flush_delegate pFlush, - const bytesWritten_delegate pBytesWritten, - const wdestroy_delegate pDestroy) -{ - ::fWrite = (pWrite); - ::fFlush = (pFlush); - ::fBytesWritten = (pBytesWritten); - ::fWDestroy = (pDestroy); +SkManagedWStream::SkManagedWStream(void* context) { + fContext = context; +} +SkManagedWStream::~SkManagedWStream() { + if (!fProcs.fDestroy) return; + fProcs.fDestroy(this, fContext); } bool SkManagedWStream::write(const void* buffer, size_t size) { - return ::fWrite(this, buffer, size); + if (!fProcs.fWrite) return false; + return fProcs.fWrite(this, fContext, buffer, size); } - void SkManagedWStream::flush() { - ::fFlush(this); + if (!fProcs.fFlush) return; + fProcs.fFlush(this, fContext); } - size_t SkManagedWStream::bytesWritten() const { - return ::fBytesWritten(this); + if (!fProcs.fBytesWritten) return 0; + return fProcs.fBytesWritten(this, fContext); } diff --git a/src/xamarin/SkiaKeeper.c b/src/xamarin/SkiaKeeper.c index 40aa52781723..f9bd48454113 100644 --- a/src/xamarin/SkiaKeeper.c +++ b/src/xamarin/SkiaKeeper.c @@ -19,6 +19,7 @@ #include "sk_data.h" #include "sk_document.h" #include "sk_drawable.h" +#include "sk_general.h" #include "sk_image.h" #include "sk_imagefilter.h" #include "sk_mask.h" @@ -51,6 +52,7 @@ void** KeepSkiaCSymbols (void) { static void* ret[] = { // Skia + (void*)sk_colortype_get_default_8888, (void*)gr_context_unref, (void*)gr_glinterface_create_native_interface, (void*)sk_bitmap_new, @@ -77,7 +79,7 @@ void** KeepSkiaCSymbols (void) (void*)sk_shader_ref, (void*)sk_stream_asset_destroy, (void*)sk_string_new_empty, - (void*)sk_colortype_get_default_8888, + (void*)sk_surface_new_null, (void*)sk_svgcanvas_create, (void*)sk_typeface_unref, (void*)sk_textblob_ref, diff --git a/src/xamarin/sk_manageddrawable.cpp b/src/xamarin/sk_manageddrawable.cpp index b5dae666858f..1547dfa7bd5b 100644 --- a/src/xamarin/sk_manageddrawable.cpp +++ b/src/xamarin/sk_manageddrawable.cpp @@ -12,54 +12,46 @@ #include "sk_picture.h" #include "sk_types_priv.h" -/* This is a C wrapper around the C++ class, so that it is possible - to access the C++ from C#. */ - -static sk_manageddrawable_draw_delegate gDraw; -static sk_manageddrawable_getBounds_delegate gGetBounds; -static sk_manageddrawable_newPictureSnapshot_delegate gNewPictureSnapshot; - -static inline SkManagedDrawable* AsManagedDrawable(sk_manageddrawable_t* cdrawable) -{ - return reinterpret_cast(cdrawable); +static inline SkManagedDrawable* AsManagedDrawable(sk_manageddrawable_t* d) { + return reinterpret_cast(d); } - -static inline sk_manageddrawable_t* ToManagedDrawable(SkManagedDrawable* drawable) -{ - return reinterpret_cast(drawable); +static inline sk_manageddrawable_t* ToManagedDrawable(SkManagedDrawable* d) { + return reinterpret_cast(d); } -void dDraw(SkManagedDrawable* managedDrawable, SkCanvas* canvas) -{ - gDraw(ToManagedDrawable(managedDrawable), ToCanvas(canvas)); -} +static sk_manageddrawable_procs_t gProcs; -void dGetBounds(SkManagedDrawable* managedDrawable, SkRect* rect) -{ - gGetBounds(ToManagedDrawable(managedDrawable), ToRect(rect)); +void dDraw(SkManagedDrawable* d, void* context, SkCanvas* canvas) { + if (!gProcs.fDraw) return; + gProcs.fDraw(ToManagedDrawable(d), context, ToCanvas(canvas)); } - -SkPicture* dNewPictureSnapshot(SkManagedDrawable* managedDrawable) -{ - return AsPicture(gNewPictureSnapshot(ToManagedDrawable(managedDrawable))); +void dGetBounds(SkManagedDrawable* d, void* context, SkRect* rect) { + if (!gProcs.fGetBounds) return; + gProcs.fGetBounds(ToManagedDrawable(d), context, ToRect(rect)); } - -sk_manageddrawable_t* sk_manageddrawable_new() { - return ToManagedDrawable(new SkManagedDrawable()); +SkPicture* dNewPictureSnapshot(SkManagedDrawable* d, void* context) { + if (!gProcs.fNewPictureSnapshot) return nullptr; + return AsPicture(gProcs.fNewPictureSnapshot(ToManagedDrawable(d), context)); +} +void dDestroy(SkManagedDrawable* d, void* context) { + if (!gProcs.fDestroy) return; + gProcs.fDestroy(ToManagedDrawable(d), context); } -void sk_manageddrawable_destroy(sk_manageddrawable_t* drawable) -{ +sk_manageddrawable_t* sk_manageddrawable_new(void* context) { + return ToManagedDrawable(new SkManagedDrawable(context)); +} +void sk_manageddrawable_unref(sk_manageddrawable_t* drawable) { SkSafeUnref(AsManagedDrawable(drawable)); } +void sk_manageddrawable_set_procs(sk_manageddrawable_procs_t procs) { + gProcs = procs; -void sk_manageddrawable_set_delegates(const sk_manageddrawable_draw_delegate pDraw, - const sk_manageddrawable_getBounds_delegate pGetBounds, - const sk_manageddrawable_newPictureSnapshot_delegate pNewPictureSnapshot) -{ - gDraw = pDraw; - gGetBounds = pGetBounds; - gNewPictureSnapshot = pNewPictureSnapshot; + SkManagedDrawable::Procs p; + p.fDraw = dDraw; + p.fGetBounds = dGetBounds; + p.fNewPictureSnapshot = dNewPictureSnapshot; + p.fDestroy = dDestroy; - SkManagedDrawable::setDelegates(dDraw, dGetBounds, dNewPictureSnapshot); + SkManagedDrawable::setProcs(p); } diff --git a/src/xamarin/sk_managedstream.cpp b/src/xamarin/sk_managedstream.cpp index 730b907a61c2..365d7a3d65a8 100644 --- a/src/xamarin/sk_managedstream.cpp +++ b/src/xamarin/sk_managedstream.cpp @@ -11,14 +11,10 @@ #include "sk_managedstream.h" #include "sk_types_priv.h" +// WRITEABLE MANAGED STREAM -static sk_managedwstream_write_delegate gWrite; -static sk_managedwstream_flush_delegate gFlush; -static sk_managedwstream_bytesWritten_delegate gBytesWritten; -static sk_managedwstream_destroy_delegate gWDestroy; - -static inline SkManagedWStream* AsManagedWStream(sk_wstream_managedstream_t* cstream) { - return reinterpret_cast(cstream); +static inline SkManagedWStream* AsManagedWStream(sk_wstream_managedstream_t* stream) { + return reinterpret_cast(stream); } static inline sk_wstream_managedstream_t* ToManagedWStream(SkManagedWStream* stream) { return reinterpret_cast(stream); @@ -27,158 +23,136 @@ static inline const sk_wstream_managedstream_t* ToManagedWStream(const SkManaged return reinterpret_cast(stream); } -bool dWrite(SkManagedWStream* managedStream, const void* buffer, size_t size) -{ - return gWrite(ToManagedWStream(managedStream), buffer, size); +static sk_managedwstream_procs_t gWProcs; + +bool dWrite(SkManagedWStream* stream, void* context, const void* buffer, size_t size) { + if (!gWProcs.fWrite) return false; + return gWProcs.fWrite(ToManagedWStream(stream), context, buffer, size); } -void dFlush(SkManagedWStream* managedStream) -{ - gFlush(ToManagedWStream(managedStream)); +void dFlush(SkManagedWStream* stream, void* context) { + if (!gWProcs.fFlush) return; + gWProcs.fFlush(ToManagedWStream(stream), context); } -size_t dBytesWritten(const SkManagedWStream* managedStream) -{ - return gBytesWritten(ToManagedWStream(managedStream)); +size_t dBytesWritten(const SkManagedWStream* stream, void* context) { + if (!gWProcs.fBytesWritten) return 0; + return gWProcs.fBytesWritten(ToManagedWStream(stream), context); } -void dWDestroy(size_t managedStream) -{ - gWDestroy(managedStream); +void dWDestroy(SkManagedWStream* stream, void* context) { + if (!gWProcs.fDestroy) return; + gWProcs.fDestroy(ToManagedWStream(stream), context); } -sk_wstream_managedstream_t* sk_managedwstream_new() -{ - return ToManagedWStream(new SkManagedWStream()); +sk_wstream_managedstream_t* sk_managedwstream_new(void* context) { + return ToManagedWStream(new SkManagedWStream(context)); } - -void sk_managedwstream_destroy(sk_wstream_managedstream_t* stream) -{ - delete AsManagedWStream(stream); +void sk_managedwstream_destroy(sk_wstream_managedstream_t* stream) { + if (stream) + delete AsManagedWStream(stream); } +void sk_managedwstream_set_procs(sk_managedwstream_procs_t procs) { + gWProcs = procs; -void sk_managedwstream_set_delegates(const sk_managedwstream_write_delegate pWrite, - const sk_managedwstream_flush_delegate pFlush, - const sk_managedwstream_bytesWritten_delegate pBytesWritten, - const sk_managedwstream_destroy_delegate pDestroy) -{ - gWrite = pWrite; - gFlush = pFlush; - gBytesWritten = pBytesWritten; - gWDestroy = pDestroy; + SkManagedWStream::Procs p; + p.fWrite = dWrite; + p.fFlush = dFlush; + p.fBytesWritten = dBytesWritten; + p.fDestroy = dWDestroy; - SkManagedWStream::setDelegates(dWrite, dFlush, dBytesWritten, dWDestroy); + SkManagedWStream::setProcs(p); } -static sk_managedstream_read_delegate gRead; -static sk_managedstream_peek_delegate gPeek; -static sk_managedstream_isAtEnd_delegate gIsAtEnd; -static sk_managedstream_hasPosition_delegate gHasPosition; -static sk_managedstream_hasLength_delegate gHasLength; -static sk_managedstream_rewind_delegate gRewind; -static sk_managedstream_getPosition_delegate gGetPosition; -static sk_managedstream_seek_delegate gSeek; -static sk_managedstream_move_delegate gMove; -static sk_managedstream_getLength_delegate gGetLength; -static sk_managedstream_createNew_delegate gCreateNew; -static sk_managedstream_destroy_delegate gDestroy; - +// READ-ONLY MANAGED STREAM -static inline SkManagedStream* AsManagedStream(sk_stream_managedstream_t* cstream) { - return reinterpret_cast(cstream); +static inline SkManagedStream* AsManagedStream(sk_stream_managedstream_t* s) { + return reinterpret_cast(s); } -static inline sk_stream_managedstream_t* ToManagedStream(SkManagedStream* stream) { - return reinterpret_cast(stream); +static inline sk_stream_managedstream_t* ToManagedStream(SkManagedStream* s) { + return reinterpret_cast(s); } -static inline const sk_stream_managedstream_t* ToManagedStream(const SkManagedStream* stream) { - return reinterpret_cast(stream); +static inline const sk_stream_managedstream_t* ToManagedStream(const SkManagedStream* s) { + return reinterpret_cast(s); } +static sk_managedstream_procs_t gProcs; -size_t dRead(SkManagedStream* managedStream, void* buffer, size_t size) -{ - return gRead(ToManagedStream(managedStream), buffer, size); +size_t dRead(SkManagedStream* stream, void* context, void* buffer, size_t size) { + if (!gProcs.fRead) return 0; + return gProcs.fRead(ToManagedStream(stream), context, buffer, size); } -size_t dPeek(SkManagedStream* managedStream, void* buffer, size_t size) -{ - return gPeek(ToManagedStream(managedStream), buffer, size); +size_t dPeek(const SkManagedStream* stream, void* context, void* buffer, size_t size) { + if (!gProcs.fPeek) return 0; + return gProcs.fPeek(ToManagedStream(stream), context, buffer, size); } -bool dIsAtEnd(const SkManagedStream* managedStream) -{ - return gIsAtEnd(ToManagedStream(managedStream)); +bool dIsAtEnd(const SkManagedStream* stream, void* context) { + if (!gProcs.fIsAtEnd) return false; + return gProcs.fIsAtEnd(ToManagedStream(stream), context); } -bool dHasPosition(const SkManagedStream* managedStream) -{ - return gHasPosition(ToManagedStream(managedStream)); +bool dHasPosition(const SkManagedStream* stream, void* context) { + if (!gProcs.fIsAtEnd) return false; + return gProcs.fHasPosition(ToManagedStream(stream), context); } -bool dHasLength(const SkManagedStream* managedStream) -{ - return gHasLength(ToManagedStream(managedStream)); +bool dHasLength(const SkManagedStream* stream, void* context) { + if (!gProcs.fHasLength) return false; + return gProcs.fHasLength(ToManagedStream(stream), context); } -bool dRewind(SkManagedStream* managedStream) -{ - return gRewind(ToManagedStream(managedStream)); +bool dRewind(SkManagedStream* stream, void* context) { + if (!gProcs.fRewind) return false; + return gProcs.fRewind(ToManagedStream(stream), context); } -size_t dGetPosition(const SkManagedStream* managedStream) -{ - return gGetPosition(ToManagedStream(managedStream)); +size_t dGetPosition(const SkManagedStream* stream, void* context) { + if (!gProcs.fGetPosition) return 0; + return gProcs.fGetPosition(ToManagedStream(stream), context); } -bool dSeek(SkManagedStream* managedStream, size_t position) -{ - return gSeek(ToManagedStream(managedStream), position); +bool dSeek(SkManagedStream* stream, void* context, size_t position) { + if (!gProcs.fSeek) return false; + return gProcs.fSeek(ToManagedStream(stream), context, position); } -bool dMove(SkManagedStream* managedStream, long offset) -{ - return gMove(ToManagedStream(managedStream), offset); +bool dMove(SkManagedStream* stream, void* context, long offset) { + if (!gProcs.fMove) return false; + return gProcs.fMove(ToManagedStream(stream), context, offset); } -size_t dGetLength(const SkManagedStream* managedStream) -{ - return gGetLength(ToManagedStream(managedStream)); +size_t dGetLength(const SkManagedStream* stream, void* context) { + if (!gProcs.fGetLength) return 0; + return gProcs.fGetLength(ToManagedStream(stream), context); } -SkManagedStream* dCreateNew(const SkManagedStream* managedStream) -{ - return AsManagedStream(gCreateNew(ToManagedStream(managedStream))); +SkManagedStream* dDuplicate(const SkManagedStream* stream, void* context) { + if (!gProcs.fDuplicate) return nullptr; + return AsManagedStream(gProcs.fDuplicate(ToManagedStream(stream), context)); } -void dDestroy(size_t managedStream) -{ - gDestroy(managedStream); +SkManagedStream* dFork(const SkManagedStream* stream, void* context) { + if (!gProcs.fFork) return nullptr; + return AsManagedStream(gProcs.fFork(ToManagedStream(stream), context)); } - - -sk_stream_managedstream_t* sk_managedstream_new () -{ - return ToManagedStream (new SkManagedStream ()); +void dDestroy(SkManagedStream* stream, void* context) { + if (!gProcs.fDestroy) return; + gProcs.fDestroy(ToManagedStream(stream), context); } -void sk_managedstream_destroy (sk_stream_managedstream_t* stream) -{ - delete AsManagedStream (stream); +sk_stream_managedstream_t* sk_managedstream_new(void* context) { + return ToManagedStream(new SkManagedStream(context)); } +void sk_managedstream_destroy(sk_stream_managedstream_t* stream) { + if (stream) + delete AsManagedStream(stream); +} +void sk_managedstream_set_procs(const sk_managedstream_procs_t procs) { + gProcs = procs; -void sk_managedstream_set_delegates (const sk_managedstream_read_delegate pRead, - const sk_managedstream_peek_delegate pPeek, - const sk_managedstream_isAtEnd_delegate pIsAtEnd, - const sk_managedstream_hasPosition_delegate pHasPosition, - const sk_managedstream_hasLength_delegate pHasLength, - const sk_managedstream_rewind_delegate pRewind, - const sk_managedstream_getPosition_delegate pGetPosition, - const sk_managedstream_seek_delegate pSeek, - const sk_managedstream_move_delegate pMove, - const sk_managedstream_getLength_delegate pGetLength, - const sk_managedstream_createNew_delegate pCreateNew, - const sk_managedstream_destroy_delegate pDestroy) -{ - gRead = pRead; - gPeek = pPeek; - gIsAtEnd = pIsAtEnd; - gHasPosition = pHasPosition; - gHasLength = pHasLength; - gRewind = pRewind; - gGetPosition = pGetPosition; - gSeek = pSeek; - gMove = pMove; - gGetLength = pGetLength; - gCreateNew = pCreateNew; - gDestroy = pDestroy; + SkManagedStream::Procs p; + p.fRead = dRead; + p.fPeek = dPeek; + p.fIsAtEnd = dIsAtEnd; + p.fHasPosition = dHasPosition; + p.fHasLength = dHasLength; + p.fRewind = dRewind; + p.fGetPosition = dGetPosition; + p.fSeek = dSeek; + p.fMove = dMove; + p.fGetLength = dGetLength; + p.fDuplicate = dDuplicate; + p.fFork = dFork; + p.fDestroy = dDestroy; - SkManagedStream::setDelegates(dRead, dPeek, dIsAtEnd, dHasPosition, dHasLength, dRewind, dGetPosition, dSeek, dMove, dGetLength, dCreateNew, dDestroy); + SkManagedStream::setProcs(p); } -