From 0aae65a1712602de0dd57f67176997fa928b76a8 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 18 May 2023 15:07:40 +0900 Subject: [PATCH] sw_engine code refactoring. Introduced a compositor table to maintain clean and organized code. --- src/lib/sw_engine/tvgSwCommon.h | 3 +- src/lib/sw_engine/tvgSwRaster.cpp | 208 ++++++------------------------ 2 files changed, 39 insertions(+), 172 deletions(-) diff --git a/src/lib/sw_engine/tvgSwCommon.h b/src/lib/sw_engine/tvgSwCommon.h index b488eb4d..e3142b4b 100644 --- a/src/lib/sw_engine/tvgSwCommon.h +++ b/src/lib/sw_engine/tvgSwCommon.h @@ -241,8 +241,7 @@ struct SwImage struct SwBlender { uint32_t (*join)(uint8_t r, uint8_t g, uint8_t b, uint8_t a); - uint8_t (*luma)(uint8_t* c); - uint8_t (*iluma)(uint8_t* c); + uint8_t (*alpha[(int)CompositeMethod::InvLumaMask + 1])(uint8_t*); }; struct SwCompositor; diff --git a/src/lib/sw_engine/tvgSwRaster.cpp b/src/lib/sw_engine/tvgSwRaster.cpp index f158fa5c..b881fdc9 100644 --- a/src/lib/sw_engine/tvgSwRaster.cpp +++ b/src/lib/sw_engine/tvgSwRaster.cpp @@ -108,7 +108,7 @@ static inline uint32_t _argbJoin(uint8_t r, uint8_t g, uint8_t b, uint8_t a) static inline bool _compositing(const SwSurface* surface) { - if (!surface->compositor || surface->compositor->method == CompositeMethod::None) return false; + if (!surface->compositor || (int)surface->compositor->method <= (int)CompositeMethod::ClipPath) return false; return true; } @@ -240,15 +240,8 @@ static bool _rasterSolidRect(SwSurface* surface, const SwBBox& region, uint8_t r static bool _rasterRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a) { if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterMaskedRect(surface, region, r, g, b, a, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterMaskedRect(surface, region, r, g, b, a, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterMaskedRect(surface, region, r, g, b, a, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterMaskedRect(surface, region, r, g, b, a, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterMaskedRect(surface, region, r, g, b, a, alpha); } else { if (a == 255) { return _rasterSolidRect(surface, region, r, g, b); @@ -350,15 +343,8 @@ static bool _rasterRle(SwSurface* surface, SwRleData* rle, uint8_t r, uint8_t g, if (!rle) return false; if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterMaskedRle(surface, rle, r, g, b, a, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterMaskedRle(surface, rle, r, g, b, a, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterMaskedRle(surface, rle, r, g, b, a, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterMaskedRle(surface, rle, r, g, b, a, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterMaskedRle(surface, rle, r, g, b, a, alpha); } else { if (a == 255) { return _rasterSolidRle(surface, rle, r, g, b); @@ -383,15 +369,8 @@ static bool _rasterRle(SwSurface* surface, SwRleData* rle, uint8_t r, uint8_t g, static bool _transformedRleRGBAImage(SwSurface* surface, const SwImage* image, const Matrix* transform, uint32_t opacity) { if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity, alpha); } else { return _rasterTexmapPolygon(surface, image, transform, nullptr, opacity, nullptr); } @@ -607,27 +586,9 @@ static bool _scaledRleRGBAImage(SwSurface* surface, const SwImage* image, const auto halfScale = _halfScale(image->scale); if (_compositing(surface)) { - if (opacity == 255) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterScaledMaskedRleRGBAImage(surface, image, &itransform, region, halfScale, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterScaledMaskedRleRGBAImage(surface, image, &itransform, region, halfScale, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterScaledMaskedRleRGBAImage(surface, image, &itransform, region, halfScale, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterScaledMaskedRleRGBAImage(surface, image, &itransform, region, halfScale, surface->blender.iluma); - } - } else { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterScaledMaskedTranslucentRleRGBAImage(surface, image, &itransform, region, opacity, halfScale, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterScaledMaskedTranslucentRleRGBAImage(surface, image, &itransform, region, opacity, halfScale, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterScaledMaskedTranslucentRleRGBAImage(surface, image, &itransform, region, opacity, halfScale, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterScaledMaskedTranslucentRleRGBAImage(surface, image, &itransform, region, opacity, halfScale, surface->blender.iluma); - } - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + if (opacity == 255) return _rasterScaledMaskedRleRGBAImage(surface, image, &itransform, region, halfScale, alpha); + else return _rasterScaledMaskedTranslucentRleRGBAImage(surface, image, &itransform, region, opacity, halfScale, alpha); } else { if (opacity == 255) return _rasterScaledRleRGBAImage(surface, image, &itransform, region, opacity, halfScale); else return _rasterScaledTranslucentRleRGBAImage(surface, image, &itransform, region, opacity, halfScale); @@ -739,27 +700,9 @@ static bool _rasterDirectRleRGBAImage(SwSurface* surface, const SwImage* image) static bool _directRleRGBAImage(SwSurface* surface, const SwImage* image, uint32_t opacity) { if (_compositing(surface)) { - if (opacity == 255) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterDirectMaskedRleRGBAImage(surface, image, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterDirectMaskedRleRGBAImage(surface, image, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterDirectMaskedRleRGBAImage(surface, image, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterDirectMaskedRleRGBAImage(surface, image, surface->blender.iluma); - } - } else { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterDirectMaskedTranslucentRleRGBAImage(surface, image, opacity, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterDirectMaskedTranslucentRleRGBAImage(surface, image, opacity, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterDirectMaskedTranslucentRleRGBAImage(surface, image, opacity, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterDirectMaskedTranslucentRleRGBAImage(surface, image, opacity, surface->blender.iluma); - } - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + if (opacity == 255) return _rasterDirectMaskedRleRGBAImage(surface, image, alpha); + else return _rasterDirectMaskedTranslucentRleRGBAImage(surface, image, opacity, alpha); } else { if (opacity == 255) return _rasterDirectRleRGBAImage(surface, image); else return _rasterDirectTranslucentRleRGBAImage(surface, image, opacity); @@ -775,15 +718,8 @@ static bool _directRleRGBAImage(SwSurface* surface, const SwImage* image, uint32 static bool _transformedRGBAImage(SwSurface* surface, const SwImage* image, const Matrix* transform, const SwBBox& region, uint32_t opacity) { if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity, alpha); } else { return _rasterTexmapPolygon(surface, image, transform, ®ion, opacity, nullptr); } @@ -793,15 +729,8 @@ static bool _transformedRGBAImage(SwSurface* surface, const SwImage* image, cons static bool _transformedRGBAImageMesh(SwSurface* surface, const SwImage* image, const RenderMesh* mesh, const Matrix* transform, const SwBBox* region, uint32_t opacity) { if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterTexmapPolygonMesh(surface, image, mesh, transform, region, opacity, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterTexmapPolygonMesh(surface, image, mesh, transform, region, opacity, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterTexmapPolygonMesh(surface, image, mesh, transform, region, opacity, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterTexmapPolygonMesh(surface, image, mesh, transform, region, opacity, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterTexmapPolygonMesh(surface, image, mesh, transform, region, opacity, alpha); } else { return _rasterTexmapPolygonMesh(surface, image, mesh, transform, region, opacity, nullptr); } @@ -986,27 +915,9 @@ static bool _scaledRGBAImage(SwSurface* surface, const SwImage* image, const Mat auto halfScale = _halfScale(image->scale); if (_compositing(surface)) { - if (opacity == 255) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterScaledMaskedRGBAImage(surface, image, &itransform, region, halfScale, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterScaledMaskedRGBAImage(surface, image, &itransform, region, halfScale, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterScaledMaskedRGBAImage(surface, image, &itransform, region, halfScale, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterScaledMaskedRGBAImage(surface, image, &itransform, region, halfScale, surface->blender.iluma); - } - } else { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterScaledMaskedTranslucentRGBAImage(surface, image, &itransform, region, opacity, halfScale, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterScaledMaskedTranslucentRGBAImage(surface, image, &itransform, region, opacity, halfScale, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterScaledMaskedTranslucentRGBAImage(surface, image, &itransform, region, opacity, halfScale, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterScaledMaskedTranslucentRGBAImage(surface, image, &itransform, region, opacity, halfScale, surface->blender.iluma); - } - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + if (opacity == 255) return _rasterScaledMaskedRGBAImage(surface, image, &itransform, region, halfScale, alpha); + else return _rasterScaledMaskedTranslucentRGBAImage(surface, image, &itransform, region, opacity, halfScale, alpha); } else { if (opacity == 255) return _rasterScaledRGBAImage(surface, image, &itransform, region, halfScale); else return _rasterScaledTranslucentRGBAImage(surface, image, &itransform, region, opacity, halfScale); @@ -1116,27 +1027,9 @@ static bool _rasterDirectRGBAImage(SwSurface* surface, const SwImage* image, con static bool _directRGBAImage(SwSurface* surface, const SwImage* image, const SwBBox& region, uint32_t opacity) { if (_compositing(surface)) { - if (opacity == 255) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterDirectMaskedRGBAImage(surface, image, region, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterDirectMaskedRGBAImage(surface, image, region, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterDirectMaskedRGBAImage(surface, image, region, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterDirectMaskedRGBAImage(surface, image, region, surface->blender.iluma); - } - } else { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterDirectMaskedTranslucentRGBAImage(surface, image, region, opacity, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterDirectMaskedTranslucentRGBAImage(surface, image, region, opacity, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterDirectMaskedTranslucentRGBAImage(surface, image, region, opacity, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterDirectMaskedTranslucentRGBAImage(surface, image, region, opacity, surface->blender.iluma); - } - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + if (opacity == 255) return _rasterDirectMaskedRGBAImage(surface, image, region, alpha); + else return _rasterDirectMaskedTranslucentRGBAImage(surface, image, region, opacity, alpha); } else { if (opacity == 255) return _rasterDirectRGBAImage(surface, image, region); else return _rasterDirectTranslucentRGBAImage(surface, image, region, opacity); @@ -1236,15 +1129,8 @@ static bool _rasterSolidLinearGradientRect(SwSurface* surface, const SwBBox& reg static bool _rasterLinearGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) { if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterLinearGradientMaskedRect(surface, region, fill, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterLinearGradientMaskedRect(surface, region, fill, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterLinearGradientMaskedRect(surface, region, fill, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterLinearGradientMaskedRect(surface, region, fill, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterLinearGradientMaskedRect(surface, region, fill, alpha); } else { if (fill->translucent) return _rasterTranslucentLinearGradientRect(surface, region, fill); else _rasterSolidLinearGradientRect(surface, region, fill); @@ -1345,15 +1231,8 @@ static bool _rasterLinearGradientRle(SwSurface* surface, const SwRleData* rle, c if (!rle) return false; if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterLinearGradientMaskedRle(surface, rle, fill, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterLinearGradientMaskedRle(surface, rle, fill, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterLinearGradientMaskedRle(surface, rle, fill, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterLinearGradientMaskedRle(surface, rle, fill, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterLinearGradientMaskedRle(surface, rle, fill, alpha); } else { if (fill->translucent) return _rasterTranslucentLinearGradientRle(surface, rle, fill); else return _rasterSolidLinearGradientRle(surface, rle, fill); @@ -1437,15 +1316,8 @@ static bool _rasterSolidRadialGradientRect(SwSurface* surface, const SwBBox& reg static bool _rasterRadialGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill) { if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterRadialGradientMaskedRect(surface, region, fill, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterRadialGradientMaskedRect(surface, region, fill, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterRadialGradientMaskedRect(surface, region, fill, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterRadialGradientMaskedRect(surface, region, fill, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterRadialGradientMaskedRect(surface, region, fill, alpha); } else { if (fill->translucent) return _rasterTranslucentRadialGradientRect(surface, region, fill); else return _rasterSolidRadialGradientRect(surface, region, fill); @@ -1545,15 +1417,8 @@ static bool _rasterRadialGradientRle(SwSurface* surface, const SwRleData* rle, c if (!rle) return false; if (_compositing(surface)) { - if (surface->compositor->method == CompositeMethod::AlphaMask) { - return _rasterRadialGradientMaskedRle(surface, rle, fill, _alpha); - } else if (surface->compositor->method == CompositeMethod::InvAlphaMask) { - return _rasterRadialGradientMaskedRle(surface, rle, fill, _ialpha); - } else if (surface->compositor->method == CompositeMethod::LumaMask) { - return _rasterRadialGradientMaskedRle(surface, rle, fill, surface->blender.luma); - } else if (surface->compositor->method == CompositeMethod::InvLumaMask) { - return _rasterRadialGradientMaskedRle(surface, rle, fill, surface->blender.iluma); - } + auto alpha = surface->blender.alpha[(int)surface->compositor->method]; + return _rasterRadialGradientMaskedRle(surface, rle, fill, alpha); } else { if (fill->translucent) _rasterTranslucentRadialGradientRle(surface, rle, fill); else return _rasterSolidRadialGradientRle(surface, rle, fill); @@ -1579,14 +1444,17 @@ void rasterRGBA32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len) bool rasterCompositor(SwSurface* surface) { + surface->blender.alpha[(int)CompositeMethod::AlphaMask] = _alpha; + surface->blender.alpha[(int)CompositeMethod::InvAlphaMask] = _ialpha; + if (surface->cs == ColorSpace::ABGR8888 || surface->cs == ColorSpace::ABGR8888S) { surface->blender.join = _abgrJoin; - surface->blender.luma = _abgrLuma; - surface->blender.iluma = _abgrInvLuma; + surface->blender.alpha[(int)CompositeMethod::LumaMask] = _abgrLuma; + surface->blender.alpha[(int)CompositeMethod::InvLumaMask] = _abgrInvLuma; } else if (surface->cs == ColorSpace::ARGB8888 || surface->cs == ColorSpace::ARGB8888S) { surface->blender.join = _argbJoin; - surface->blender.luma = _argbLuma; - surface->blender.iluma = _argbInvLuma; + surface->blender.alpha[(int)CompositeMethod::LumaMask] = _argbLuma; + surface->blender.alpha[(int)CompositeMethod::InvLumaMask] = _argbInvLuma; } else { TVGERR("SW_ENGINE", "Unsupported Colorspace(%d) is expected!", surface->cs); return false;