From 2dc5a5782affb57f74093146070a80e5ff80ea8c Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 19 Nov 2021 13:22:32 +0900 Subject: [PATCH] sw_engine image: code refactoring +++ --- src/lib/sw_engine/tvgSwCommon.h | 6 +++--- src/lib/sw_engine/tvgSwImage.cpp | 34 ++++++++++++++-------------------- src/lib/sw_engine/tvgSwRaster.cpp | 28 ++++++++++++++-------------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/lib/sw_engine/tvgSwCommon.h b/src/lib/sw_engine/tvgSwCommon.h index 85974f5..e46eee7 100644 --- a/src/lib/sw_engine/tvgSwCommon.h +++ b/src/lib/sw_engine/tvgSwCommon.h @@ -224,11 +224,11 @@ struct SwImage SwRleData* rle = nullptr; uint32_t* data = nullptr; uint32_t w, h, stride; - int32_t x = 0; //shift x - int32_t y = 0; //shift y + int32_t ox = 0; //offset x + int32_t oy = 0; //offset y float scale; - bool transformed = false; + bool direct = false; //draw image directly (with offset) }; struct SwBlender diff --git a/src/lib/sw_engine/tvgSwImage.cpp b/src/lib/sw_engine/tvgSwImage.cpp index 096e734..76631cc 100644 --- a/src/lib/sw_engine/tvgSwImage.cpp +++ b/src/lib/sw_engine/tvgSwImage.cpp @@ -26,6 +26,13 @@ /* Internal Class Implementation */ /************************************************************************/ +static inline bool _onlyShifted(const Matrix* m) +{ + if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true; + return false; +} + + static bool _genOutline(SwImage* image, const Matrix* transform, SwMpool* mpool, unsigned tid) { image->outline = mpoolReqOutline(mpool, tid); @@ -67,40 +74,27 @@ static bool _genOutline(SwImage* image, const Matrix* transform, SwMpool* mpool, } -static inline bool _onlyShifted(const Matrix* m) -{ - if (mathEqual(m->e11, 1.0f) && mathEqual(m->e22, 1.0f) && mathZero(m->e12) && mathZero(m->e21)) return true; - return false; -} - - /************************************************************************/ /* External Class Implementation */ /************************************************************************/ - bool imagePrepare(SwImage* image, const Matrix* transform, const SwBBox& clipRegion, SwBBox& renderRegion, SwMpool* mpool, unsigned tid) { - image->transformed = !_onlyShifted(transform); - bool fastTrack; + image->direct = _onlyShifted(transform); + //Fast track: Non-transformed image but just shifted. + if (image->direct) { + image->ox = -static_cast(round(transform->e13)); + image->oy = -static_cast(round(transform->e23)); //Figure out the scale factor by transform matrix - if (image->transformed) { + } else { auto scaleX = sqrtf((transform->e11 * transform->e11) + (transform->e21 * transform->e21)); auto scaleY = sqrtf((transform->e22 * transform->e22) + (transform->e12 * transform->e12)); - //TODO:If the x and y axis scale is different, a separate interpolation algorithm for each axis should be applied. image->scale = (fabsf(scaleX - scaleY) > 0.01f) ? 1.0f : scaleX; - fastTrack = false; - //Fast track: Non-transformed image but just shifted. - } else { - image->scale = 1.0f; - image->x = -static_cast(round(transform->e13)); - image->y = -static_cast(round(transform->e23)); - fastTrack = true; } if (!_genOutline(image, transform, mpool, tid)) return false; - return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, fastTrack); + return mathUpdateOutlineBBox(image->outline, clipRegion, renderRegion, image->direct); } diff --git a/src/lib/sw_engine/tvgSwRaster.cpp b/src/lib/sw_engine/tvgSwRaster.cpp index ede7155..efecb63 100644 --- a/src/lib/sw_engine/tvgSwRaster.cpp +++ b/src/lib/sw_engine/tvgSwRaster.cpp @@ -597,7 +597,7 @@ static bool _rasterDirectMaskedRleImage(SwSurface* surface, const SwImage* image for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { auto dst = &surface->buffer[span->y * surface->stride + span->x]; auto cmp = &cbuffer[span->y * surface->stride + span->x]; - auto img = image->data + (span->y + image->y) * image->stride + (span->x + image->x); + auto img = image->data + (span->y + image->oy) * image->stride + (span->x + image->ox); auto alpha = _multiplyAlpha(span->coverage, opacity); if (alpha == 255) { for (uint32_t x = 0; x < span->len; ++x, ++dst, ++cmp, ++img) { @@ -621,7 +621,7 @@ static bool __rasterDirectTranslucentRleImage(SwSurface* surface, const SwImage* for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { auto dst = &surface->buffer[span->y * surface->stride + span->x]; - auto img = image->data + (span->y + image->y) * image->stride + (span->x + image->x); + auto img = image->data + (span->y + image->oy) * image->stride + (span->x + image->ox); auto alpha = _multiplyAlpha(span->coverage, opacity); for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { auto src = ALPHA_BLEND(*img, alpha); @@ -651,7 +651,7 @@ static bool _rasterDirectSolidRleImage(SwSurface* surface, const SwImage* image) for (uint32_t i = 0; i < image->rle->size; ++i, ++span) { auto dst = &surface->buffer[span->y * surface->stride + span->x]; - auto img = image->data + (span->y + image->y) * image->stride + (span->x + image->x); + auto img = image->data + (span->y + image->oy) * image->stride + (span->x + image->ox); if (span->coverage == 255) { for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) { *dst = *img; @@ -978,7 +978,7 @@ static bool _rasterDirectMaskedImage(SwSurface* surface, const SwImage* image, u TVGLOG("SW_ENGINE", "Direct Masked Image"); - auto sbuffer = image->data + (region.min.y + image->y) * image->stride + (region.min.x + image->x); + auto sbuffer = image->data + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); auto cbuffer = surface->compositor->image.data + (region.min.y * surface->stride) + region.min.x; //compositor buffer for (uint32_t y = 0; y < h2; ++y) { @@ -1000,7 +1000,7 @@ static bool _rasterDirectMaskedImage(SwSurface* surface, const SwImage* image, u static bool __rasterDirectTranslucentImage(SwSurface* surface, const SwImage* image, uint32_t opacity, const SwBBox& region) { auto dbuffer = &surface->buffer[region.min.y * surface->stride + region.min.x]; - auto sbuffer = image->data + (region.min.y + image->y) * image->stride + (region.min.x + image->x); + auto sbuffer = image->data + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); for (auto y = region.min.y; y < region.max.y; ++y) { auto dst = dbuffer; @@ -1032,7 +1032,7 @@ static bool _rasterDirectTranslucentImage(SwSurface* surface, const SwImage* ima static bool _rasterDirectSolidImage(SwSurface* surface, const SwImage* image, const SwBBox& region) { auto dbuffer = &surface->buffer[region.min.y * surface->stride + region.min.x]; - auto sbuffer = image->data + (region.min.y + image->y) * image->stride + (region.min.x + image->x); + auto sbuffer = image->data + (region.min.y + image->oy) * image->stride + (region.min.x + image->ox); for (auto y = region.min.y; y < region.max.y; ++y) { auto dst = dbuffer; @@ -1592,21 +1592,21 @@ bool rasterImage(SwSurface* surface, SwImage* image, const Matrix* transform, co //Clipped Image if (image->rle) { - if (image->transformed) { - if (translucent) return _rasterTransformedTranslucentRleImage(surface, image, opacity, &itransform, halfScale); - else return _rasterTransformedSolidRleImage(surface, image, &itransform, halfScale); - } else { + if (image->direct) { if (translucent) return _rasterDirectTranslucentRleImage(surface, image, opacity); else return _rasterDirectSolidRleImage(surface, image); + } else { + if (translucent) return _rasterTransformedTranslucentRleImage(surface, image, opacity, &itransform, halfScale); + else return _rasterTransformedSolidRleImage(surface, image, &itransform, halfScale); } //Whole Image } else { - if (image->transformed) { - if (translucent) return _rasterTransformedTranslucentImage(surface, image, opacity, bbox, &itransform, halfScale); - else return _rasterTransformedSolidImage(surface, image, bbox, &itransform, halfScale); - } else { + if (image->direct) { if (translucent) return _rasterDirectTranslucentImage(surface, image, opacity, bbox); else return _rasterDirectSolidImage(surface, image, bbox); + } else { + if (translucent) return _rasterTransformedTranslucentImage(surface, image, opacity, bbox, &itransform, halfScale); + else return _rasterTransformedSolidImage(surface, image, bbox, &itransform, halfScale); } } } -- 2.7.4