Skip to content

Commit

Permalink
Improve managed interop (#61)
Browse files Browse the repository at this point in the history
* Re-work the managed types
   - pass a context around
   - clean up the API
* Add hooks for working with native types directly
* Split the duplicate and fork for SkManagedStream
* Add unref for sk_vertices_t
  • Loading branch information
mattleibow committed Jul 29, 2019
1 parent 54589eb commit 45d2598
Show file tree
Hide file tree
Showing 21 changed files with 470 additions and 414 deletions.
2 changes: 2 additions & 0 deletions gn/core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
33 changes: 33 additions & 0 deletions include/c/sk_general.h
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions include/c/sk_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

////////////////////////////////////////////////////////////////////////////////

Expand Down
2 changes: 0 additions & 2 deletions include/c/sk_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions include/c/sk_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions include/c/sk_vertices.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions include/core/SkRefCnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<int32_t> fRefCnt;
int32_t getRefCnt() const {
Expand Down
26 changes: 19 additions & 7 deletions include/xamarin/SkManagedDrawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
124 changes: 68 additions & 56 deletions include/xamarin/SkManagedStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 15 additions & 12 deletions include/xamarin/sk_manageddrawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 45d2598

Please sign in to comment.