From 11e070d1672d6ff88f47ee365d27b296c674cf09 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 1 May 2020 17:15:31 +0900 Subject: [PATCH] sw_engine: support anti-aliasing Change-Id: I9b79c8b4022ddf2ae4fe980f480ba3ec140750d3 --- src/lib/sw_engine/tvgSwRaster.cpp | 35 ++++++++++++++++++++++++++--------- src/lib/sw_engine/tvgSwRle.cpp | 4 ++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/lib/sw_engine/tvgSwRaster.cpp b/src/lib/sw_engine/tvgSwRaster.cpp index 29350db..0b813fd 100644 --- a/src/lib/sw_engine/tvgSwRaster.cpp +++ b/src/lib/sw_engine/tvgSwRaster.cpp @@ -24,6 +24,12 @@ /* Internal Class Implementation */ /************************************************************************/ +static inline size_t COLOR_ALPHA(size_t color) +{ + return (color >> 24) & 0xff; +} + + static inline size_t COLOR_ALPHA_BLEND(size_t color, size_t alpha) { return (((((color >> 8) & 0x00ff00ff) * alpha) & 0xff00ff00) + @@ -33,15 +39,18 @@ static inline size_t COLOR_ALPHA_BLEND(size_t color, size_t alpha) static inline size_t COLOR_ARGB_JOIN(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { - return (a << 24 | r << 16 | g << 8 | b); + return (a << 24 | r << 16 | g << 8 | b); } static void -_drawTranslucentSpan(uint32_t* dst, size_t len, size_t color, size_t alpha) +_rasterTranslucent(uint32_t* dst, size_t len, size_t color, size_t cov) { //OPTIMIZE ME: SIMD - auto ialpha = 255 - alpha; + + if (cov < 255) color = COLOR_ALPHA_BLEND(color, cov); + auto ialpha = 255 - COLOR_ALPHA(color); + for (size_t i = 0; i < len; ++i) { dst[i] = color + COLOR_ALPHA_BLEND(dst[i], ialpha); } @@ -49,11 +58,20 @@ _drawTranslucentSpan(uint32_t* dst, size_t len, size_t color, size_t alpha) static void -_drawSolidSpan(uint32_t* dst, size_t len, size_t color) +_rasterSolid(uint32_t* dst, size_t len, size_t color, size_t cov) { //OPTIMIZE ME: SIMD - for (size_t i = 0; i < len; ++i) { - dst[i] = color; + + //Fully Opaque + if (cov == 255) { + for (size_t i = 0; i < len; ++i) { + dst[i] = color; + } + } else { + auto ialpha = 255 - cov; + for (size_t i = 0; i < len; ++i) { + dst[i] = COLOR_ALPHA_BLEND(color, cov) + COLOR_ALPHA_BLEND(dst[i], ialpha); + } } } @@ -75,10 +93,9 @@ bool rasterShape(Surface& surface, SwShape& sdata, uint8_t r, uint8_t g, uint8_t assert(span); auto dst = &surface.buffer[span->y * stride + span->x]; - assert(dst); - if (a == 255) _drawSolidSpan(dst, span->len, color); - else _drawTranslucentSpan(dst, span->len, color, a); + if (a == 255) _rasterSolid(dst, span->len, color, span->coverage); + else _rasterTranslucent(dst, span->len, color, span->coverage); ++span; } diff --git a/src/lib/sw_engine/tvgSwRle.cpp b/src/lib/sw_engine/tvgSwRle.cpp index 5166de8..f915a5d 100644 --- a/src/lib/sw_engine/tvgSwRle.cpp +++ b/src/lib/sw_engine/tvgSwRle.cpp @@ -434,8 +434,8 @@ static void _lineTo(RleWorker& rw, const SwPoint& to) /* These macros speed up repetitive divisions by replacing them with multiplications and right shifts. */ - auto dx_r = (ULONG_MAX >> PIXEL_BITS) / (diff.x); - auto dy_r = (ULONG_MAX >> PIXEL_BITS) / (diff.y); + auto dx_r = static_cast(ULONG_MAX >> PIXEL_BITS) / (diff.x); + auto dy_r = static_cast(ULONG_MAX >> PIXEL_BITS) / (diff.y); /* The fundamental value `prod' determines which side and the */ /* exact coordinate where the line exits current cell. It is */ -- 2.7.4