Skip to content
0.2.0-dev Download ZIP
321 lines ·13.4 KB Raw
1/*
2 * Copyright (c) 2021 - 2026 ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "tvgSwRasterCore.h"
24
25/************************************************************************/
26/* See tvgSwRasterSimd.cpp */
27/************************************************************************/
28
29#ifdef THORVG_AVX_SUPPORT
30 #if defined(_MSC_VER)
31 #include <intrin.h>
32 #endif
33 uint32_t avxRasterDownScaler(const uint32_t* img, uint32_t stride, uint32_t w, TVG_UNUSED uint32_t h, float sx, TVG_UNUSED float sy, int32_t miny, int32_t maxy, int32_t n);
34 void avxRasterTranslucentPixels(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity);
35 void avxRasterSolidPixels(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity);
36 void avxRasterSolidPixel32(uint32_t* dst, uint32_t val, uint32_t offset, int32_t len);
37 bool avxRasterTranslucentRect(SwSurface* surface, const RenderRegion& bbox, const RenderColor& c);
38 bool avxRasterTranslucentRle(SwSurface* surface, const SwRle* rle, const RenderRegion& bbox, const RenderColor& c);
39 void avxRasterUnpremultiply(uint32_t* buffer, uint32_t width);
40 void avxRasterPremultiply(uint32_t* buffer, uint32_t width);
41 bool avxRasterABGRtoARGB(RenderSurface* surface);
42 void avxRasterGaussianFilterRow(uint8_t* dst, const uint8_t* src, int32_t w, int32_t dimension, float iarr);
43 void avxRasterGaussianXYFlipBlock(const uint32_t* src, uint32_t* dst, int32_t stride);
44#elif defined(THORVG_NEON_SUPPORT)
45 #if defined(__linux__) && defined(__arm__)
46 #include <sys/auxv.h>
47 #include <asm/hwcap.h>
48 #endif
49 void neonRasterUnpremultiply(uint32_t* buffer, uint32_t width);
50 void neonRasterPremultiply(uint32_t* buffer, uint32_t width);
51 uint32_t neonRasterDownScaler(const uint32_t* img, uint32_t stride, uint32_t w, TVG_UNUSED uint32_t h, float sx, TVG_UNUSED float sy, int32_t miny, int32_t maxy, int32_t n);
52 void neonRasterTranslucentPixels(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity);
53 void neonRasterSolidPixels(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity);
54 void neonRasterSolidPixel32(uint32_t* dst, uint32_t val, uint32_t offset, int32_t len);
55 bool neonRasterTranslucentRle(SwSurface* surface, const SwRle* rle, const RenderRegion& bbox, const RenderColor& c);
56 bool neonRasterTranslucentRect(SwSurface* surface, const RenderRegion& bbox, const RenderColor& c);
57 bool neonRasterABGRtoARGB(RenderSurface* surface);
58 void neonRasterGaussianFilterRow(uint8_t* dst, const uint8_t* src, int32_t w, int32_t dimension, float iarr);
59 void neonRasterGaussianXYFlipBlock(const uint32_t* src, uint32_t* dst, int32_t stride);
60#endif
61
62/************************************************************************/
63/* Scalar */
64/************************************************************************/
65
66void cRasterGaussianFilterRow(uint8_t* dst, const uint8_t* src, int32_t w, int32_t dimension, float iarr)
67{
68 auto end = w - 1;
69 auto i = 0; // current index
70 auto l = -(dimension + 1); // left index
71 auto r = dimension; // right index
72 int acc[4] = {0, 0, 0, 0}; // sliding accumulator
73
74 // initial accumulation
75 for (int x = l; x < r; ++x) {
76 auto id = tvg::clamp(x, 0, end) * 4;
77 acc[0] += src[id++];
78 acc[1] += src[id++];
79 acc[2] += src[id++];
80 acc[3] += src[id];
81 }
82 // perform filtering
83 for (int x = 0; x < w; ++x, ++r, ++l) {
84 auto rid = tvg::clamp(r, 0, end) * 4;
85 auto lid = tvg::clamp(l, 0, end) * 4;
86 acc[0] += src[rid++] - src[lid++];
87 acc[1] += src[rid++] - src[lid++];
88 acc[2] += src[rid++] - src[lid++];
89 acc[3] += src[rid] - src[lid];
90 // ignored rounding for the performance. It should be originally: acc[idx] * iarr + 0.5f
91 dst[i++] = static_cast<uint8_t>(acc[0] * iarr);
92 dst[i++] = static_cast<uint8_t>(acc[1] * iarr);
93 dst[i++] = static_cast<uint8_t>(acc[2] * iarr);
94 dst[i++] = static_cast<uint8_t>(acc[3] * iarr);
95 }
96}
97
98void cRasterUnpremultiply(uint32_t* buffer, uint32_t width)
99{
100 for (uint32_t x = 0; x < width; ++x) {
101 buffer[x] = UNPREMULTIPLY(buffer[x]);
102 }
103}
104
105void cRasterPremultiply(uint32_t* buffer, uint32_t width)
106{
107 for (uint32_t x = 0; x < width; ++x) {
108 auto c = buffer[x];
109 if (A(c) != 255) buffer[x] = PREMULTIPLY(c, A(c));
110 }
111}
112
113uint32_t cRasterDownScaler(const uint32_t* img, uint32_t stride, uint32_t w, TVG_UNUSED uint32_t h, float sx, TVG_UNUSED float sy, int32_t miny, int32_t maxy, int32_t n)
114{
115 size_t c[4] = {0, 0, 0, 0};
116
117 auto minx = static_cast<int32_t>(sx) - n;
118 if (minx < 0) minx = 0;
119
120 auto maxx = static_cast<int32_t>(sx) + n;
121 if (maxx >= static_cast<int32_t>(w)) maxx = w;
122
123 auto inc = (n / 2) + 1;
124 n = 0;
125
126 auto src = img + minx + miny * stride;
127
128 for (auto y = miny; y < maxy; y += inc) {
129 auto p = src;
130 for (auto x = minx; x < maxx; x += inc, p += inc) {
131 c[0] += A(*p);
132 c[1] += C1(*p);
133 c[2] += C2(*p);
134 c[3] += C3(*p);
135 ++n;
136 }
137 src += (stride * inc);
138 }
139
140 c[0] /= n;
141 c[1] /= n;
142 c[2] /= n;
143 c[3] /= n;
144
145 return (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
146}
147
148void cRasterTranslucentPixels(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity)
149{
150 if (opacity == 255) {
151 for (uint32_t x = 0; x < len; ++x, ++dst, ++src) {
152 *dst = *src + ALPHA_BLEND(*dst, IA(*src));
153 }
154 } else {
155 for (uint32_t x = 0; x < len; ++x, ++dst, ++src) {
156 auto tmp = ALPHA_BLEND(*src, opacity);
157 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
158 }
159 }
160}
161
162void cRasterSolidPixels(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity)
163{
164 if (opacity == 255) memcpy(dst, src, size_t(len) * sizeof(uint32_t));
165 else cRasterTranslucentPixels(dst, src, len, opacity);
166}
167
168void cRasterSolidPixel32(uint32_t* dst, uint32_t val, uint32_t offset, int32_t len)
169{
170 dst += offset;
171 for (int32_t x = 0; x < len; ++x) *dst++ = val;
172}
173
174bool cRasterTranslucentRle(SwSurface* surface, const SwRle* rle, const RenderRegion& bbox, const RenderColor& c)
175{
176 const SwSpan* end;
177 int32_t x, len;
178
179 //32bit channels
180 if (surface->channelSize == sizeof(uint32_t)) {
181 auto color = surface->join(c.r, c.g, c.b, c.a);
182 uint32_t src;
183 for (auto span = rle->fetch(bbox, &end); span < end; ++span) {
184 if (!span->fetch(bbox, x, len)) continue;
185 auto dst = &surface->buf32[span->y * surface->stride + x];
186 if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage);
187 else src = color;
188 auto ialpha = IA(src);
189 for (auto x = 0; x < len; ++x, ++dst) {
190 *dst = src + ALPHA_BLEND(*dst, ialpha);
191 }
192 }
193 //8bit grayscale
194 } else if (surface->channelSize == sizeof(uint8_t)) {
195 uint8_t src;
196 for (auto span = rle->fetch(bbox, &end); span < end; ++span) {
197 if (!span->fetch(bbox, x, len)) continue;
198 auto dst = &surface->buf8[span->y * surface->stride + x];
199 if (span->coverage < 255) src = MULTIPLY(span->coverage, c.a);
200 else src = c.a;
201 auto ialpha = ~c.a;
202 for (auto x = 0; x < len; ++x, ++dst) {
203 *dst = src + MULTIPLY(*dst, ialpha);
204 }
205 }
206 }
207 return true;
208}
209
210bool cRasterTranslucentRect(SwSurface* surface, const RenderRegion& bbox, const RenderColor& c)
211{
212 //32bits channels
213 if (surface->channelSize == sizeof(uint32_t)) {
214 auto color = surface->join(c.r, c.g, c.b, c.a);
215 auto buffer = surface->buf32 + (bbox.yMin * surface->stride) + bbox.xMin;
216 auto ialpha = 255 - c.a;
217 for (uint32_t y = 0; y < bbox.h(); ++y) {
218 auto dst = &buffer[y * surface->stride];
219 for (uint32_t x = 0; x < bbox.w(); ++x, ++dst) {
220 *dst = color + ALPHA_BLEND(*dst, ialpha);
221 }
222 }
223 //8bit grayscale
224 } else if (surface->channelSize == sizeof(uint8_t)) {
225 auto buffer = surface->buf8 + (bbox.yMin * surface->stride) + bbox.xMin;
226 auto ialpha = ~c.a;
227 for (uint32_t y = 0; y < bbox.h(); ++y) {
228 auto dst = &buffer[y * surface->stride];
229 for (uint32_t x = 0; x < bbox.w(); ++x, ++dst) {
230 *dst = c.a + MULTIPLY(*dst, ialpha);
231 }
232 }
233 }
234 return true;
235}
236
237bool cRasterABGRtoARGB(RenderSurface* surface)
238{
239 TVGLOG("SW_ENGINE", "Convert ColorSpace ABGR - ARGB [Size: %d x %d]", surface->w, surface->h);
240
241 auto buffer = surface->buf32;
242 #pragma omp parallel for
243 for (int32_t y = 0; y < (int32_t)surface->h; ++y) {
244 auto dst = buffer + uint32_t(y) * surface->stride;
245 for (uint32_t x = 0; x < surface->w; ++x, ++dst) {
246 auto c = *dst;
247 //flip Blue, Red channels
248 *dst = (c & 0xff000000) + ((c & 0x00ff0000) >> 16) + (c & 0x0000ff00) + ((c & 0x000000ff) << 16);
249 }
250 }
251
252 return true;
253}
254
255/************************************************************************/
256/* Runtime Dispatch */
257/************************************************************************/
258
259#if defined(THORVG_AVX_SUPPORT) || defined(THORVG_NEON_SUPPORT)
260 uint32_t (*rasterDownScaler)(const uint32_t*, uint32_t, uint32_t, uint32_t, float, float, int32_t, int32_t, int32_t) = cRasterDownScaler;
261 void (*rasterUnpremultiply)(uint32_t*, uint32_t) = cRasterUnpremultiply;
262 void (*rasterPremultiply)(uint32_t*, uint32_t) = cRasterPremultiply;
263 void (*rasterTranslucentPixels)(uint32_t*, uint32_t*, uint32_t, uint8_t) = cRasterTranslucentPixels;
264 void (*rasterSolidPixels)(uint32_t*, uint32_t*, uint32_t, uint8_t) = cRasterSolidPixels;
265 void (*rasterSolidPixel32)(uint32_t*, uint32_t, uint32_t, int32_t) = cRasterSolidPixel32;
266 bool (*rasterTranslucentRle)(SwSurface*, const SwRle*, const RenderRegion&, const RenderColor&) = cRasterTranslucentRle;
267 bool (*rasterTranslucentRect)(SwSurface*, const RenderRegion&, const RenderColor&) = cRasterTranslucentRect;
268 bool (*rasterABGRtoARGB)(RenderSurface*) = cRasterABGRtoARGB;
269 void (*rasterGaussianFilterRow)(uint8_t*, const uint8_t*, int32_t, int32_t, float) = cRasterGaussianFilterRow;
270 void (*rasterGaussianXYFlipBlock)(const uint32_t*, uint32_t*, int32_t) = nullptr; // A null keeps the scalar path in the caller.
271#endif
272
273void rasterInit()
274{
275#if defined(THORVG_AVX_SUPPORT)
276 // AVX2 also requires OS support for saving XMM/YMM state.
277 #if defined(_MSC_VER)
278 int info[4];
279 __cpuid(info, 0);
280 if (info[0] < 7) return;
281 __cpuidex(info, 1, 0);
282 if ((info[2] & 0x18000000) != 0x18000000) return;
283 if ((_xgetbv(0) & 6) != 6) return;
284 __cpuidex(info, 7, 0);
285 if (!(info[1] & (1 << 5))) return;
286 #elif defined(__GNUC__) || defined(__clang__)
287 __builtin_cpu_init();
288 if (!__builtin_cpu_supports("avx2")) return;
289 #else
290 return;
291 #endif
292 rasterDownScaler = avxRasterDownScaler;
293 rasterUnpremultiply = avxRasterUnpremultiply;
294 rasterPremultiply = avxRasterPremultiply;
295 rasterTranslucentPixels = avxRasterTranslucentPixels;
296 rasterSolidPixels = avxRasterSolidPixels;
297 rasterSolidPixel32 = avxRasterSolidPixel32;
298 rasterTranslucentRle = avxRasterTranslucentRle;
299 rasterTranslucentRect = avxRasterTranslucentRect;
300 rasterABGRtoARGB = avxRasterABGRtoARGB;
301 rasterGaussianFilterRow = avxRasterGaussianFilterRow;
302 rasterGaussianXYFlipBlock = avxRasterGaussianXYFlipBlock;
303 TVGLOG("SW_ENGINE", "Run AVX Vectorization.");
304#elif defined(THORVG_NEON_SUPPORT)
305 #if defined(__linux__) && defined(__arm__)
306 if (!(getauxval(AT_HWCAP) & HWCAP_NEON)) return;
307 #endif
308 rasterDownScaler = neonRasterDownScaler;
309 rasterUnpremultiply = neonRasterUnpremultiply;
310 rasterPremultiply = neonRasterPremultiply;
311 rasterTranslucentPixels = neonRasterTranslucentPixels;
312 rasterSolidPixels = neonRasterSolidPixels;
313 rasterSolidPixel32 = neonRasterSolidPixel32;
314 rasterTranslucentRle = neonRasterTranslucentRle;
315 rasterTranslucentRect = neonRasterTranslucentRect;
316 rasterABGRtoARGB = neonRasterABGRtoARGB;
317 rasterGaussianFilterRow = neonRasterGaussianFilterRow;
318 rasterGaussianXYFlipBlock = neonRasterGaussianXYFlipBlock;
319 TVGLOG("SW_ENGINE", "Run NEON Vectorization.");
320#endif
321}