From 5ae97c7f1adc7f6239e65921df4a89184d4f778e Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 17 Jul 2025 11:49:29 +0900 Subject: [PATCH] api: Add Composition blend mode The composition mode enables intermediate blending. When a scene uses Composition mode, it generates an intermediate buffer to render the scene image first. This image can then be blended with the canvas afterward. Please avoid to use this, unless you really need to composite the precomposite scene. This feature is relatively expensive at performance. C++ API: +BlendMethod::Composition C API: +Tvg_Blend_Method::TVG_BLEND_METHOD_COMPOSITION --- inc/thorvg.h | 3 ++- src/bindings/capi/thorvg_capi.h | 3 ++- src/renderer/sw_engine/tvgSwRenderer.cpp | 1 + src/renderer/tvgPaint.cpp | 9 ++++++--- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index 638481f0..8c5e3807 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -214,7 +214,8 @@ enum class BlendMethod : uint8_t Color, ///< Reserved. Not supported. Luminosity, ///< Reserved. Not supported. Add, ///< Simply adds pixel values of one layer with the other. (S + D) - HardMix ///< Reserved. Not supported. + HardMix, ///< Reserved. Not supported. + Composition = 255 ///< Used for intermediate composition. @since 1.0 }; diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index 4fc32a68..57b4b4fd 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -164,7 +164,8 @@ typedef enum { TVG_BLEND_METHOD_COLOR, ///< Reserved. Not supported. TVG_BLEND_METHOD_LUMINOSITY, ///< Reserved. Not supported. TVG_BLEND_METHOD_ADD, ///< Simply adds pixel values of one layer with the other. (S + D) - TVG_BLEND_METHOD_HARDMIX ///< Reserved. Not supported. + TVG_BLEND_METHOD_HARDMIX, ///< Reserved. Not supported. + TVG_BLEND_METHOD_COMPOSITION = 255 ///< Used for intermediate composition. @since 1.0 } Tvg_Blend_Method; diff --git a/src/renderer/sw_engine/tvgSwRenderer.cpp b/src/renderer/sw_engine/tvgSwRenderer.cpp index 162770b6..7e375b04 100644 --- a/src/renderer/sw_engine/tvgSwRenderer.cpp +++ b/src/renderer/sw_engine/tvgSwRenderer.cpp @@ -528,6 +528,7 @@ bool SwRenderer::blend(BlendMethod method) switch (method) { case BlendMethod::Normal: + case BlendMethod::Composition: surface->blender = nullptr; break; case BlendMethod::Multiply: diff --git a/src/renderer/tvgPaint.cpp b/src/renderer/tvgPaint.cpp index 23cd05b3..a3d5c6b7 100644 --- a/src/renderer/tvgPaint.cpp +++ b/src/renderer/tvgPaint.cpp @@ -429,11 +429,14 @@ uint8_t Paint::opacity() const noexcept Result Paint::blend(BlendMethod method) noexcept { - if (method > BlendMethod::HardMix) return Result::InvalidArguments; //TODO: Remove later if (method == BlendMethod::Hue || method == BlendMethod::Saturation || method == BlendMethod::Color || method == BlendMethod::Luminosity || method == BlendMethod::HardMix) return Result::NonSupport; - pImpl->blend(method); - return Result::Success; + + if (method == BlendMethod::Composition || method <= BlendMethod::HardMix) { + pImpl->blend(method); + return Result::Success; + } + return Result::InvalidArguments; }