From a36e25e1781b33988d4761e44a65fe83e10037d4 Mon Sep 17 00:00:00 2001 From: Michal Szczecinski Date: Thu, 26 Nov 2020 09:31:32 +0100 Subject: [PATCH] common capi: Added scene clear API Scene::clear() API allows users to remove shapes on their own, without a crash in paint->dispose() or tvg_paint_del() methods. This case is needed especially when thorvg is used to draw frames, when in one frame we have scene with shape, and in next frames (future time stamps) user deletes shapes @API additions Result Scene::clear(); Tvg_Result tvg_scene_clear(Tvg_Paint *scene); Example: ```c Tvg_Paint *scene = tvg_scene_new(); Tvg_Paint *shape = tvg_shape_new(); tvg_scene_push(scene, shape); tvg_scene_clear(); //Now we can safelly free resources manually tvg_paint_del(scene); //Without tvg_scene_clear() memory allocatad for shape was double released tvg_paint_del(shape); ``` --- inc/thorvg.h | 1 + inc/thorvg_capi.h | 1 + src/bindings/capi/tvgCapi.cpp | 6 ++++++ src/lib/tvgScene.cpp | 10 +++++++++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 4d4c18bd..1b37ab1d 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -303,6 +303,7 @@ public: Result push(std::unique_ptr paint) noexcept; Result reserve(uint32_t size) noexcept; + Result clear() noexcept; static std::unique_ptr gen() noexcept; diff --git a/inc/thorvg_capi.h b/inc/thorvg_capi.h index e1602014..22df235f 100644 --- a/inc/thorvg_capi.h +++ b/inc/thorvg_capi.h @@ -193,6 +193,7 @@ TVG_EXPORT Tvg_Result tvg_picture_get_viewbox(const Tvg_Paint* paint, float* x, TVG_EXPORT Tvg_Paint* tvg_scene_new(); TVG_EXPORT Tvg_Result tvg_scene_reserve(Tvg_Paint* scene, uint32_t size); TVG_EXPORT Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint* paint); +TVG_EXPORT Tvg_Result tvg_scene_clear(Tvg_Paint* scene); #ifdef __cplusplus diff --git a/src/bindings/capi/tvgCapi.cpp b/src/bindings/capi/tvgCapi.cpp index 6f47e97c..8d3fbf2e 100644 --- a/src/bindings/capi/tvgCapi.cpp +++ b/src/bindings/capi/tvgCapi.cpp @@ -514,6 +514,12 @@ TVG_EXPORT Tvg_Result tvg_scene_push(Tvg_Paint* scene, Tvg_Paint* paint) return (Tvg_Result) reinterpret_cast(scene)->push(unique_ptr((Paint*)paint)); } +TVG_EXPORT Tvg_Result tvg_scene_clear(Tvg_Paint* scene) +{ + if (!scene) return TVG_RESULT_INVALID_ARGUMENT; + return (Tvg_Result) reinterpret_cast(scene)->clear(); +} + #ifdef __cplusplus } diff --git a/src/lib/tvgScene.cpp b/src/lib/tvgScene.cpp index cf982a58..fae2ff34 100644 --- a/src/lib/tvgScene.cpp +++ b/src/lib/tvgScene.cpp @@ -58,4 +58,12 @@ Result Scene::reserve(uint32_t size) noexcept pImpl->paints.reserve(size); return Result::Success; -} \ No newline at end of file +} + + +Result Scene::clear() noexcept +{ + pImpl->paints.clear(); + + return Result::Success; +}