summaryrefslogtreecommitdiffstatsci
diff options
context:
space:
mode:
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>2026-10-15 15:38:05 +0200
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>2026-10-15 15:38:05 +0200
commit44a2e03dd1a3d98b215d0a1cba71d16574cc2b6b (patch)
tree2fa1d24fbd44dc6ca488f71095d29ab51a4d4c6a
parent909ef3c352e947517b3cab6ecdb8c64ea9677705 (diff)
atomic references for pattern and surface
-rw-r--r--src/vkvg_context.c6
-rw-r--r--src/vkvg_pattern.c19
-rw-r--r--src/vkvg_pattern.h2
-rw-r--r--src/vkvg_surface.c16
-rw-r--r--src/vkvg_surface_internal.c2
-rw-r--r--src/vkvg_surface_internal.h2
6 files changed, 22 insertions, 25 deletions
diff --git a/src/vkvg_context.c b/src/vkvg_context.c
index 16e31d5..c7bc897 100644
--- a/src/vkvg_context.c
+++ b/src/vkvg_context.c
@@ -216,6 +216,7 @@ void vkvg_flush(VkvgContext ctx) {
void _clear_context(VkvgContext ctx) {
VkvgPattern pat = NULL;
+ VkvgFont font = NULL;
// free saved context stack elmt
while (ctx->ctxSaveCount > 0) {
@@ -223,6 +224,9 @@ void _clear_context(VkvgContext ctx) {
pat = ctx->pSavedCtxs[ctx->ctxSaveCount].pattern;
if (pat)
vkvg_pattern_destroy(pat);
+ font = ctx->pSavedCtxs[ctx->ctxSaveCount].currentFont;
+ if (font)
+ vkvg_font_destroy(font);
}
// free additional stencil use in save/restore process
@@ -1239,7 +1243,7 @@ void vkvg_set_text_direction(vkvg_context* ctx, vkvg_direction_t direction) {
ctx->textDirection = direction;
}
void vkvg_text_extents(VkvgContext ctx, const char* text, vkvg_text_extents_t* extents) {
- if (vkvg_status(ctx))
+ if (vkvg_status(ctx) || vkvg_font_status(ctx->currentFont))
return;
_font_cache_text_extents(ctx, text, -1, extents);
}
diff --git a/src/vkvg_pattern.c b/src/vkvg_pattern.c
index cac17cb..1eaccd9 100644
--- a/src/vkvg_pattern.c
+++ b/src/vkvg_pattern.c
@@ -47,7 +47,7 @@ VkvgPattern vkvg_pattern_create_for_surface(VkvgSurface surf) {
pat->type = VKVG_PATTERN_TYPE_SURFACE;
pat->extend = VKVG_EXTEND_NONE;
pat->data = surf;
- pat->references = 1;
+ atomic_init(&pat->references, 1);
vkvg_surface_reference(surf);
if (vkvg_surface_status(surf))
@@ -92,7 +92,7 @@ VkvgPattern vkvg_pattern_create_linear(float x0, float y0, float x1, float y1) {
if (pat->data) {
vkvg_pattern_edit_linear(pat, x0, y0, x1, y1);
- pat->references = 1;
+ atomic_init(&pat->references, 1);
} else {
pat->status = VKVG_STATUS_PATTERN_INVALID_GRADIENT;
}
@@ -139,7 +139,7 @@ VkvgPattern vkvg_pattern_create_radial(float cx0, float cy0, float radius0, floa
if (pat->data) {
vkvg_pattern_edit_radial(pat, cx0, cy0, radius0, cx1, cy1, radius1);
- pat->references = 1;
+ atomic_init(&pat->references, 1);
} else
pat->status = VKVG_STATUS_NO_MEMORY;
@@ -147,13 +147,13 @@ VkvgPattern vkvg_pattern_create_radial(float cx0, float cy0, float radius0, floa
}
VkvgPattern vkvg_pattern_reference(VkvgPattern pat) {
if (!vkvg_pattern_status(pat))
- pat->references++;
+ atomic_fetch_add_explicit(&pat->references, 1, memory_order_relaxed);
return pat;
}
uint32_t vkvg_pattern_get_reference_count(VkvgPattern pat) {
if (vkvg_pattern_status(pat))
return 0;
- return pat->references;
+ return atomic_load_explicit(&pat->references, memory_order_relaxed);
}
vkvg_status_t vkvg_pattern_add_color_stop(VkvgPattern pat, float offset, float r, float g, float b, float a) {
if (vkvg_pattern_status(pat))
@@ -246,13 +246,14 @@ void vkvg_pattern_get_matrix(VkvgPattern pat, vkvg_matrix_t* matrix) {
else
*matrix = VKVG_IDENTITY_MATRIX;
}
-vkvg_status_t vkvg_pattern_status(VkvgPattern pat) { return !pat ? VKVG_STATUS_NULL_POINTER : pat->status; }
-
+vkvg_status_t vkvg_pattern_status(VkvgPattern pat) {
+ return !pat ? VKVG_STATUS_NULL_POINTER : pat->status;
+}
void vkvg_pattern_destroy(VkvgPattern pat) {
if (vkvg_pattern_status(pat))
return;
- pat->references--;
- if (pat->references > 0)
+
+ if (atomic_fetch_sub_explicit(&pat->references, 1, memory_order_acq_rel) != 1)
return;
if (pat->type == VKVG_PATTERN_TYPE_SURFACE) {
diff --git a/src/vkvg_pattern.h b/src/vkvg_pattern.h
index e34648b..1c1975e 100644
--- a/src/vkvg_pattern.h
+++ b/src/vkvg_pattern.h
@@ -26,7 +26,7 @@
typedef struct _vkvg_pattern_t {
vkvg_status_t status;
- uint32_t references;
+ atomic_int references;
vkvg_pattern_type_t type;
vkvg_extend_t extend;
vkvg_filter_t filter;
diff --git a/src/vkvg_surface.c b/src/vkvg_surface.c
index 8a7c728..b52eea2 100644
--- a/src/vkvg_surface.c
+++ b/src/vkvg_surface.c
@@ -207,13 +207,8 @@ void vkvg_surface_destroy(VkvgSurface surf) {
return;
}
- LOCK_SURFACE(surf)
- surf->references--;
- if (surf->references > 0) {
- UNLOCK_SURFACE(surf)
+ if (atomic_fetch_sub_explicit(&surf->references, 1, memory_order_acq_rel) != 1)
return;
- }
- UNLOCK_SURFACE(surf)
LOG(VKVG_LOG_INFO, "DESTROY Surface\n");
@@ -243,17 +238,14 @@ void vkvg_surface_destroy(VkvgSurface surf) {
vkvg_status_t vkvg_surface_status(VkvgSurface surf) { return !surf ? VKVG_STATUS_NULL_POINTER : surf->status; }
VkvgSurface vkvg_surface_reference(VkvgSurface surf) {
- if (!vkvg_surface_status(surf)) {
- LOCK_SURFACE(surf)
- surf->references++;
- UNLOCK_SURFACE(surf)
- }
+ if (!vkvg_surface_status(surf))
+ atomic_fetch_add_explicit(&surf->references, 1, memory_order_relaxed);
return surf;
}
uint32_t vkvg_surface_get_reference_count(VkvgSurface surf) {
if (vkvg_surface_status(surf))
return 0;
- return surf->references;
+ return atomic_load_explicit(&surf->references, memory_order_relaxed);
}
VkImage vkvg_surface_get_vk_image(VkvgSurface surf) {
diff --git a/src/vkvg_surface_internal.c b/src/vkvg_surface_internal.c
index 8dcddac..8d15d2b 100644
--- a/src/vkvg_surface_internal.c
+++ b/src/vkvg_surface_internal.c
@@ -224,7 +224,7 @@ VkvgSurface _create_surface(VkvgDevice dev, VkFormat format) {
return (VkvgSurface)&_vkvg_status_no_memory;
}
- surf->references = 1;
+ atomic_init(&surf->references, 1);
surf->dev = dev;
surf->format = format;
diff --git a/src/vkvg_surface_internal.h b/src/vkvg_surface_internal.h
index ef9c685..ee0be34 100644
--- a/src/vkvg_surface_internal.h
+++ b/src/vkvg_surface_internal.h
@@ -28,7 +28,7 @@
typedef struct _vkvg_surface_t {
vkvg_status_t status; /**< Current status of surface, affected by last operation */
- uint32_t references;
+ atomic_int references;
VkvgDevice dev;
uint32_t width;
uint32_t height;