Skip to content

add Bitmap allocator, add Pixel ref #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: xamarin-mobile-bindings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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_managedpixelref.cpp",
"src/xamarin/sk_managedstream.cpp",
"src/xamarin/sk_managedtracememorydump.cpp",
"src/xamarin/sk_xamarin.cpp",
"src/xamarin/SkiaKeeper.c",
"src/xamarin/SkCompatPaint.cpp",
"src/xamarin/SkManagedDrawable.cpp",
"src/xamarin/SkManagedAllocator.cpp",
"src/xamarin/SkManagedPixelRef.cpp",
"src/xamarin/SkManagedStream.cpp",
"src/xamarin/SkManagedTraceMemoryDump.cpp",
"src/xamarin/WinRTCompat.cpp",
Expand Down
5 changes: 5 additions & 0 deletions include/c/sk_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ SK_C_API void sk_bitmap_get_pixel_colors(sk_bitmap_t* cbitmap, sk_color_t* color
SK_C_API bool sk_bitmap_install_pixels(sk_bitmap_t* cbitmap, const sk_imageinfo_t* cinfo, void* pixels, size_t rowBytes, const sk_bitmap_release_proc releaseProc, void* context);
SK_C_API bool sk_bitmap_install_pixels_with_pixmap(sk_bitmap_t* cbitmap, const sk_pixmap_t* cpixmap);
SK_C_API bool sk_bitmap_install_mask_pixels(sk_bitmap_t* cbitmap, const sk_mask_t* cmask);
SK_C_API bool sk_bitmap_try_alloc_pixels_with_allocator(sk_bitmap_t* cbitmap, sk_bitmapallocator_t* allocator);
SK_C_API bool sk_bitmap_try_alloc_pixels(sk_bitmap_t* cbitmap, const sk_imageinfo_t* requestedInfo, size_t rowBytes);
SK_C_API bool sk_bitmap_try_alloc_pixels_with_flags(sk_bitmap_t* cbitmap, const sk_imageinfo_t* requestedInfo, uint32_t flags);
SK_C_API void sk_bitmap_set_pixels(sk_bitmap_t* cbitmap, void* pixels);
SK_C_API bool sk_bitmap_peek_pixels(sk_bitmap_t* cbitmap, sk_pixmap_t* cpixmap);
SK_C_API void* sk_bitmap_get_pixelref(sk_bitmap_t* cbitmap);
SK_C_API void sk_bitmap_set_pixelref(sk_bitmap_t* cbitmap, void* cpixelref, int x, int y);
SK_C_API bool sk_bitmap_extract_subset(sk_bitmap_t* cbitmap, sk_bitmap_t* dst, sk_irect_t* subset);
SK_C_API bool sk_bitmap_extract_alpha(sk_bitmap_t* cbitmap, sk_bitmap_t* dst, const sk_paint_t* paint, sk_ipoint_t* offset);
SK_C_API void sk_bitmap_notify_pixels_changed(sk_bitmap_t* cbitmap);
SK_C_API void sk_bitmap_swap(sk_bitmap_t* cbitmap, sk_bitmap_t* cother);
SK_C_API sk_shader_t* sk_bitmap_make_shader(sk_bitmap_t* cbitmap, sk_shader_tilemode_t tmx, sk_shader_tilemode_t tmy, const sk_matrix_t* cmatrix);

SK_C_API bool sk_bitmap_heapalloc(sk_bitmap_t* cbitmap);

SK_C_PLUS_PLUS_END_GUARD

#endif
3 changes: 3 additions & 0 deletions include/c/sk_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,11 @@ typedef struct sk_string_t sk_string_t;
A sk_bitmap_t is an abstraction that specifies a raster bitmap.
*/
typedef struct sk_bitmap_t sk_bitmap_t;
typedef struct sk_bitmapallocator_t sk_bitmapallocator_t;
typedef struct sk_pixmap_t sk_pixmap_t;
typedef struct sk_colorfilter_t sk_colorfilter_t;
// placed here to help seperate in PR
typedef struct sk_pixelref_t sk_pixelref_t;
typedef struct sk_imagefilter_t sk_imagefilter_t;
typedef struct sk_imagefilter_croprect_t sk_imagefilter_croprect_t;

Expand Down
49 changes: 49 additions & 0 deletions include/xamarin/SkManagedAllocator.h
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions include/xamarin/SkManagedPixelRef.h
Original file line number Diff line number Diff line change
@@ -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<SkPixelRef> 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
33 changes: 33 additions & 0 deletions include/xamarin/sk_managedallocator.h
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions include/xamarin/sk_managedpixelref.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions src/c/sk_bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}
Expand All @@ -133,6 +138,16 @@ bool sk_bitmap_peek_pixels(sk_bitmap_t* cbitmap, sk_pixmap_t* cpixmap) {
return AsBitmap(cbitmap)->peekPixels(AsPixmap(cpixmap));
}

SK_C_API void* sk_bitmap_get_pixelref(sk_bitmap_t* cbitmap) {
return AsBitmap(cbitmap)->pixelRef();
}

SK_C_API void sk_bitmap_set_pixelref(sk_bitmap_t* cbitmap, void* cpixelref, int x, int y) {
SkPixelRef* r = (SkPixelRef*)cpixelref;
AsBitmap(cbitmap)->setPixelRef(sk_ref_sp(r), x, y);
}


bool sk_bitmap_extract_subset(sk_bitmap_t* cbitmap, sk_bitmap_t* cdst, sk_irect_t* subset) {
return AsBitmap(cbitmap)->extractSubset(AsBitmap(cdst), *AsIRect(subset));
}
Expand All @@ -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));
}
4 changes: 4 additions & 0 deletions src/c/sk_types_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions src/xamarin/SkManagedAllocator.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
33 changes: 33 additions & 0 deletions src/xamarin/SkManagedPixelRef.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions src/xamarin/SkiaKeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_managedpixelref.h"
#include "include/xamarin/sk_manageddrawable.h"
#include "include/xamarin/sk_managedtracememorydump.h"
#include "include/xamarin/sk_compatpaint.h"
Expand Down Expand Up @@ -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_managedpixelref_new,
(void*)sk_managedpixelref_new_from_existing,
(void*)sk_manageddrawable_new,
(void*)sk_managedtracememorydump_new,
};
Expand Down
Loading