Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwRaster.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the 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 #ifdef _WIN32
24 #include <malloc.h>
25 #elif defined(__linux__) || defined(__ZEPHYR__)
26 #include <alloca.h>
27 #else
28 #include <stdlib.h>
29 #endif
30
31 #include "tvgMath.h"
32 #include "tvgRender.h"
33 #include "tvgSwCommon.h"
34
35 /************************************************************************/
36 /* Internal Class Implementation */
37 /************************************************************************/
38 constexpr auto DOWN_SCALE_TOLERANCE = 0.5f;
39
40 struct FillLinear
41 {
42 void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask op, uint8_t a)
43 {
44 fillLinear(fill, dst, y, x, len, op, a);
45 }
46
47 void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask op, uint8_t a)
48 {
49 fillLinear(fill, dst, y, x, len, cmp, op, a);
50 }
51
52 void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a)
53 {
54 fillLinear(fill, dst, y, x, len, op, a);
55 }
56
57 void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity)
58 {
59 fillLinear(fill, dst, y, x, len, cmp, alpha, csize, opacity);
60 }
61
62 void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a)
63 {
64 fillLinear(fill, dst, y, x, len, op, op2, a);
65 }
66
67 };
68
69 struct FillRadial
70 {
71 void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, SwMask op, uint8_t a)
72 {
73 fillRadial(fill, dst, y, x, len, op, a);
74 }
75
76 void operator()(const SwFill* fill, uint8_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwMask op, uint8_t a)
77 {
78 fillRadial(fill, dst, y, x, len, cmp, op, a);
79 }
80
81 void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, uint8_t a)
82 {
83 fillRadial(fill, dst, y, x, len, op, a);
84 }
85
86 void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, uint8_t* cmp, SwAlpha alpha, uint8_t csize, uint8_t opacity)
87 {
88 fillRadial(fill, dst, y, x, len, cmp, alpha, csize, opacity);
89 }
90
91 void operator()(const SwFill* fill, uint32_t* dst, uint32_t y, uint32_t x, uint32_t len, SwBlender op, SwBlender op2, uint8_t a)
92 {
93 fillRadial(fill, dst, y, x, len, op, op2, a);
94 }
95 };
96
97
98 static inline uint8_t _alpha(uint8_t* a)
99 {
100 return *a;
101 }
102
103
104 static inline uint8_t _ialpha(uint8_t* a)
105 {
106 return ~(*a);
107 }
108
109
110 static inline uint8_t _abgrLuma(uint8_t* c)
111 {
112 auto v = *(uint32_t*)c;
113 return ((((v&0xff)*54) + (((v>>8)&0xff)*183) + (((v>>16)&0xff)*19))) >> 8; //0.2125*R + 0.7154*G + 0.0721*B
114 }
115
116
117 static inline uint8_t _argbLuma(uint8_t* c)
118 {
119 auto v = *(uint32_t*)c;
120 return ((((v&0xff)*19) + (((v>>8)&0xff)*183) + (((v>>16)&0xff)*54))) >> 8; //0.0721*B + 0.7154*G + 0.2125*R
121 }
122
123
124 static inline uint8_t _abgrInvLuma(uint8_t* c)
125 {
126 return ~_abgrLuma(c);
127 }
128
129
130 static inline uint8_t _argbInvLuma(uint8_t* c)
131 {
132 return ~_argbLuma(c);
133 }
134
135
136 static inline uint32_t _abgrJoin(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
137 {
138 return (a << 24 | b << 16 | g << 8 | r);
139 }
140
141
142 static inline uint32_t _argbJoin(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
143 {
144 return (a << 24 | r << 16 | g << 8 | b);
145 }
146
147 static inline bool _blending(const SwSurface* surface)
148 {
149 return (surface->blender) ? true : false;
150 }
151
152
153 /* OPTIMIZE_ME: Probably, we can separate masking(8bits) / composition(32bits)
154 This would help to enhance the performance by avoiding the unnecessary matting from the composition */
155 static inline bool _compositing(const SwSurface* surface)
156 {
157 if (!surface->compositor || (int)surface->compositor->method <= (int)CompositeMethod::ClipPath) return false;
158 return true;
159 }
160
161
162 static inline bool _matting(const SwSurface* surface)
163 {
164 if ((int)surface->compositor->method < (int)CompositeMethod::AddMask) return true;
165 else return false;
166 }
167
168 static inline uint8_t _opMaskNone(uint8_t s, TVG_UNUSED uint8_t d, TVG_UNUSED uint8_t a)
169 {
170 return s;
171 }
172
173 static inline uint8_t _opMaskAdd(uint8_t s, uint8_t d, uint8_t a)
174 {
175 return s + MULTIPLY(d, a);
176 }
177
178
179 static inline uint8_t _opMaskSubtract(uint8_t s, uint8_t d, TVG_UNUSED uint8_t a)
180 {
181 return MULTIPLY(s, 255 - d);
182 }
183
184
185 static inline uint8_t _opMaskIntersect(uint8_t s, uint8_t d, TVG_UNUSED uint8_t a)
186 {
187 return MULTIPLY(s, d);
188 }
189
190
191 static inline uint8_t _opMaskDifference(uint8_t s, uint8_t d, uint8_t a)
192 {
193 return MULTIPLY(s, 255 - d) + MULTIPLY(d, a);
194 }
195
196
197 static inline uint8_t _opMaskLighten(uint8_t s, uint8_t d, uint8_t a)
198 {
199 return (s > d) ? s : d;
200 }
201
202
203 static inline uint8_t _opMaskDarken(uint8_t s, uint8_t d, uint8_t a)
204 {
205 return (s < d) ? s : d;
206 }
207
208
209 static inline bool _direct(CompositeMethod method)
210 {
211 if (method == CompositeMethod::SubtractMask || method == CompositeMethod::IntersectMask || method == CompositeMethod::DarkenMask) return true;
212 return false;
213 }
214
215
216 static inline SwMask _getMaskOp(CompositeMethod method)
217 {
218 switch (method) {
219 case CompositeMethod::AddMask: return _opMaskAdd;
220 case CompositeMethod::SubtractMask: return _opMaskSubtract;
221 case CompositeMethod::DifferenceMask: return _opMaskDifference;
222 case CompositeMethod::IntersectMask: return _opMaskIntersect;
223 case CompositeMethod::LightenMask: return _opMaskLighten;
224 case CompositeMethod::DarkenMask: return _opMaskDarken;
225 default: return nullptr;
226 }
227 }
228
229
230 static bool _compositeMaskImage(SwSurface* surface, const SwImage& image, const SwBBox& region)
231 {
232 auto dbuffer = &surface->buf8[region.min.y * surface->stride + region.min.x];
233 auto sbuffer = image.buf8 + (region.min.y + image.oy) * image.stride + (region.min.x + image.ox);
234
235 for (auto y = region.min.y; y < region.max.y; ++y) {
236 auto dst = dbuffer;
237 auto src = sbuffer;
238 for (auto x = region.min.x; x < region.max.x; x++, dst++, src++) {
239 *dst = *src + MULTIPLY(*dst, ~*src);
240 }
241 dbuffer += surface->stride;
242 sbuffer += image.stride;
243 }
244 return true;
245 }
246
247
248 #include "tvgSwRasterTexmap.h"
249 #include "tvgSwRasterC.h"
250 #include "tvgSwRasterAvx.h"
251 #include "tvgSwRasterNeon.h"
252
253
254 static inline uint32_t _sampleSize(float scale)
255 {
256 auto sampleSize = static_cast<uint32_t>(0.5f / scale);
257 if (sampleSize == 0) sampleSize = 1;
258 return sampleSize;
259 }
260
261
262 //Bilinear Interpolation
263 //OPTIMIZE_ME: Skip the function pointer access
264 static uint32_t _interpUpScaler(const uint32_t *img, TVG_UNUSED uint32_t stride, uint32_t w, uint32_t h, float sx, float sy, TVG_UNUSED int32_t miny, TVG_UNUSED int32_t maxy, TVG_UNUSED int32_t n)
265 {
266 auto rx = (size_t)(sx);
267 auto ry = (size_t)(sy);
268 auto rx2 = rx + 1;
269 if (rx2 >= w) rx2 = w - 1;
270 auto ry2 = ry + 1;
271 if (ry2 >= h) ry2 = h - 1;
272
273 auto dx = (sx > 0.0f) ? static_cast<uint8_t>((sx - rx) * 255.0f) : 0;
274 auto dy = (sy > 0.0f) ? static_cast<uint8_t>((sy - ry) * 255.0f) : 0;
275
276 auto c1 = img[rx + ry * w];
277 auto c2 = img[rx2 + ry * w];
278 auto c3 = img[rx + ry2 * w];
279 auto c4 = img[rx2 + ry2 * w];
280
281 return INTERPOLATE(INTERPOLATE(c4, c3, dx), INTERPOLATE(c2, c1, dx), dy);
282 }
283
284
285 //2n x 2n Mean Kernel
286 //OPTIMIZE_ME: Skip the function pointer access
287 static uint32_t _interpDownScaler(const uint32_t *img, uint32_t stride, uint32_t w, uint32_t h, float sx, TVG_UNUSED float sy, int32_t miny, int32_t maxy, int32_t n)
288 {
289 size_t c[4] = {0, 0, 0, 0};
290
291 int32_t minx = (int32_t)sx - n;
292 if (minx < 0) minx = 0;
293
294 int32_t maxx = (int32_t)sx + n;
295 if (maxx >= (int32_t)w) maxx = w;
296
297 int32_t inc = (n / 2) + 1;
298 n = 0;
299
300 auto src = img + minx + miny * stride;
301
302 for (auto y = miny; y < maxy; y += inc) {
303 auto p = src;
304 for (auto x = minx; x < maxx; x += inc, p += inc) {
305 c[0] += A(*p);
306 c[1] += C1(*p);
307 c[2] += C2(*p);
308 c[3] += C3(*p);
309 ++n;
310 }
311 src += (stride * inc);
312 }
313
314 c[0] /= n;
315 c[1] /= n;
316 c[2] /= n;
317 c[3] /= n;
318
319 return (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
320 }
321
322
323 /************************************************************************/
324 /* Rect */
325 /************************************************************************/
326
327 static bool _rasterCompositeMaskedRect(SwSurface* surface, const SwBBox& region, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
328 {
329 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
330 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
331 auto cstride = surface->compositor->image.stride;
332 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x); //compositor buffer
333 auto ialpha = 255 - a;
334
335 for (uint32_t y = 0; y < h; ++y) {
336 auto cmp = cbuffer;
337 for (uint32_t x = 0; x < w; ++x, ++cmp) {
338 *cmp = maskOp(a, *cmp, ialpha);
339 }
340 cbuffer += cstride;
341 }
342 return _compositeMaskImage(surface, surface->compositor->image, surface->compositor->bbox);
343 }
344
345
346 static bool _rasterDirectMaskedRect(SwSurface* surface, const SwBBox& region, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
347 {
348 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
349 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
350 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x); //compositor buffer
351 auto dbuffer = surface->buf8 + (region.min.y * surface->stride + region.min.x); //destination buffer
352
353 for (uint32_t y = 0; y < h; ++y) {
354 auto cmp = cbuffer;
355 auto dst = dbuffer;
356 for (uint32_t x = 0; x < w; ++x, ++cmp, ++dst) {
357 auto tmp = maskOp(a, *cmp, 0); //not use alpha.
358 *dst = tmp + MULTIPLY(*dst, ~tmp);
359 }
360 cbuffer += surface->compositor->image.stride;
361 dbuffer += surface->stride;
362 }
363 return true;
364 }
365
366
367 static bool _rasterMaskedRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
368 {
369 //8bit masking channels composition
370 if (surface->channelSize != sizeof(uint8_t)) return false;
371
372 TVGLOG("SW_ENGINE", "Masked(%d) Rect [Region: %lu %lu %lu %lu]", (int)surface->compositor->method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y);
373
374 auto maskOp = _getMaskOp(surface->compositor->method);
375 if (_direct(surface->compositor->method)) return _rasterDirectMaskedRect(surface, region, maskOp, r, g, b, a);
376 else return _rasterCompositeMaskedRect(surface, region, maskOp, r, g, b, a);
377 return false;
378 }
379
380
381 static bool _rasterMattedRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
382 {
383 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
384 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
385 auto csize = surface->compositor->image.channelSize;
386 auto cbuffer = surface->compositor->image.buf8 + ((region.min.y * surface->compositor->image.stride + region.min.x) * csize); //compositor buffer
387 auto alpha = surface->alpha(surface->compositor->method);
388
389 TVGLOG("SW_ENGINE", "Matted(%d) Rect [Region: %lu %lu %u %u]", (int)surface->compositor->method, region.min.x, region.min.y, w, h);
390
391 //32bits channels
392 if (surface->channelSize == sizeof(uint32_t)) {
393 auto color = surface->join(r, g, b, a);
394 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
395 for (uint32_t y = 0; y < h; ++y) {
396 auto dst = &buffer[y * surface->stride];
397 auto cmp = &cbuffer[y * surface->compositor->image.stride * csize];
398 for (uint32_t x = 0; x < w; ++x, ++dst, cmp += csize) {
399 auto tmp = ALPHA_BLEND(color, alpha(cmp));
400 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
401 }
402 }
403 //8bits grayscale
404 } else if (surface->channelSize == sizeof(uint8_t)) {
405 auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x;
406 for (uint32_t y = 0; y < h; ++y) {
407 auto dst = &buffer[y * surface->stride];
408 auto cmp = &cbuffer[y * surface->compositor->image.stride * csize];
409 for (uint32_t x = 0; x < w; ++x, ++dst, cmp += csize) {
410 *dst = INTERPOLATE8(a, *dst, alpha(cmp));
411 }
412 }
413 }
414 return true;
415 }
416
417
418 static bool _rasterBlendingRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
419 {
420 if (surface->channelSize != sizeof(uint32_t)) return false;
421
422 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
423 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
424 auto color = surface->join(r, g, b, a);
425 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
426
427 for (uint32_t y = 0; y < h; ++y) {
428 auto dst = &buffer[y * surface->stride];
429 for (uint32_t x = 0; x < w; ++x, ++dst) {
430 *dst = surface->blender(color, *dst, 255);
431 }
432 }
433 return true;
434 }
435
436
437 static bool _rasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
438 {
439 #if defined(THORVG_AVX_VECTOR_SUPPORT)
440 return avxRasterTranslucentRect(surface, region, r, g, b, a);
441 #elif defined(THORVG_NEON_VECTOR_SUPPORT)
442 return neonRasterTranslucentRect(surface, region, r, g, b, a);
443 #else
444 return cRasterTranslucentRect(surface, region, r, g, b, a);
445 #endif
446 }
447
448
449 static bool _rasterSolidRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b)
450 {
451 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
452 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
453
454 //32bits channels
455 if (surface->channelSize == sizeof(uint32_t)) {
456 auto color = surface->join(r, g, b, 255);
457 auto buffer = surface->buf32 + (region.min.y * surface->stride);
458 for (uint32_t y = 0; y < h; ++y) {
459 rasterPixel32(buffer + y * surface->stride, color, region.min.x, w);
460 }
461 return true;
462 }
463 //8bits grayscale
464 if (surface->channelSize == sizeof(uint8_t)) {
465 for (uint32_t y = 0; y < h; ++y) {
466 rasterGrayscale8(surface->buf8, 255, (y + region.min.y) * surface->stride + region.min.x, w);
467 }
468 return true;
469 }
470 return false;
471 }
472
473
474 static bool _rasterRect(SwSurface* surface, const SwBBox& region, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
475 {
476 if (_compositing(surface)) {
477 if (_matting(surface)) return _rasterMattedRect(surface, region, r, g, b, a);
478 else return _rasterMaskedRect(surface, region, r, g, b, a);
479 } else if (_blending(surface)) {
480 return _rasterBlendingRect(surface, region, r, g, b, a);
481 } else {
482 if (a == 255) return _rasterSolidRect(surface, region, r, g, b);
483 else return _rasterTranslucentRect(surface, region, r, g, b, a);
484 }
485 return false;
486 }
487
488
489 /************************************************************************/
490 /* Rle */
491 /************************************************************************/
492
493 static bool _rasterCompositeMaskedRle(SwSurface* surface, SwRle* rle, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
494 {
495 auto span = rle->spans;
496 auto cbuffer = surface->compositor->image.buf8;
497 auto cstride = surface->compositor->image.stride;
498 uint8_t src;
499
500 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
501 auto cmp = &cbuffer[span->y * cstride + span->x];
502 if (span->coverage == 255) src = a;
503 else src = MULTIPLY(a, span->coverage);
504 auto ialpha = 255 - src;
505 for (auto x = 0; x < span->len; ++x, ++cmp) {
506 *cmp = maskOp(src, *cmp, ialpha);
507 }
508 }
509 return _compositeMaskImage(surface, surface->compositor->image, surface->compositor->bbox);
510 }
511
512
513 static bool _rasterDirectMaskedRle(SwSurface* surface, SwRle* rle, SwMask maskOp, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
514 {
515 auto span = rle->spans;
516 auto cbuffer = surface->compositor->image.buf8;
517 auto cstride = surface->compositor->image.stride;
518 uint8_t src;
519
520 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
521 auto cmp = &cbuffer[span->y * cstride + span->x];
522 auto dst = &surface->buf8[span->y * surface->stride + span->x];
523 if (span->coverage == 255) src = a;
524 else src = MULTIPLY(a, span->coverage);
525 for (auto x = 0; x < span->len; ++x, ++cmp, ++dst) {
526 auto tmp = maskOp(src, *cmp, 0); //not use alpha
527 *dst = tmp + MULTIPLY(*dst, ~tmp);
528 }
529 }
530 return true;
531 }
532
533
534 static bool _rasterMaskedRle(SwSurface* surface, SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
535 {
536 TVGLOG("SW_ENGINE", "Masked(%d) Rle", (int)surface->compositor->method);
537
538 //8bit masking channels composition
539 if (surface->channelSize != sizeof(uint8_t)) return false;
540
541 auto maskOp = _getMaskOp(surface->compositor->method);
542 if (_direct(surface->compositor->method)) return _rasterDirectMaskedRle(surface, rle, maskOp, r, g, b, a);
543 else return _rasterCompositeMaskedRle(surface, rle, maskOp, r, g, b, a);
544 return false;
545 }
546
547
548 static bool _rasterMattedRle(SwSurface* surface, SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
549 {
550 TVGLOG("SW_ENGINE", "Matted(%d) Rle", (int)surface->compositor->method);
551
552 auto span = rle->spans;
553 auto cbuffer = surface->compositor->image.buf8;
554 auto csize = surface->compositor->image.channelSize;
555 auto alpha = surface->alpha(surface->compositor->method);
556
557 //32bit channels
558 if (surface->channelSize == sizeof(uint32_t)) {
559 uint32_t src;
560 auto color = surface->join(r, g, b, a);
561 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
562 auto dst = &surface->buf32[span->y * surface->stride + span->x];
563 auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize];
564 if (span->coverage == 255) src = color;
565 else src = ALPHA_BLEND(color, span->coverage);
566 for (uint32_t x = 0; x < span->len; ++x, ++dst, cmp += csize) {
567 auto tmp = ALPHA_BLEND(src, alpha(cmp));
568 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
569 }
570 }
571 //8bit grayscale
572 } else if (surface->channelSize == sizeof(uint8_t)) {
573 uint8_t src;
574 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
575 auto dst = &surface->buf8[span->y * surface->stride + span->x];
576 auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize];
577 if (span->coverage == 255) src = a;
578 else src = MULTIPLY(a, span->coverage);
579 for (uint32_t x = 0; x < span->len; ++x, ++dst, cmp += csize) {
580 *dst = INTERPOLATE8(src, *dst, alpha(cmp));
581 }
582 }
583 }
584 return true;
585 }
586
587
588 static bool _rasterBlendingRle(SwSurface* surface, const SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
589 {
590 if (surface->channelSize != sizeof(uint32_t)) return false;
591
592 auto span = rle->spans;
593 auto color = surface->join(r, g, b, a);
594
595 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
596 auto dst = &surface->buf32[span->y * surface->stride + span->x];
597 if (span->coverage == 255) {
598 for (uint32_t x = 0; x < span->len; ++x, ++dst) {
599 *dst = surface->blender(color, *dst, 255);
600 }
601 } else {
602 for (uint32_t x = 0; x < span->len; ++x, ++dst) {
603 auto tmp = surface->blender(color, *dst, 255);
604 *dst = INTERPOLATE(tmp, *dst, span->coverage);
605 }
606 }
607 }
608 return true;
609 }
610
611
612 static bool _rasterTranslucentRle(SwSurface* surface, const SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
613 {
614 #if defined(THORVG_AVX_VECTOR_SUPPORT)
615 return avxRasterTranslucentRle(surface, rle, r, g, b, a);
616 #elif defined(THORVG_NEON_VECTOR_SUPPORT)
617 return neonRasterTranslucentRle(surface, rle, r, g, b, a);
618 #else
619 return cRasterTranslucentRle(surface, rle, r, g, b, a);
620 #endif
621 }
622
623
624 static bool _rasterSolidRle(SwSurface* surface, const SwRle* rle, uint8_t r, uint8_t g, uint8_t b)
625 {
626 auto span = rle->spans;
627
628 //32bit channels
629 if (surface->channelSize == sizeof(uint32_t)) {
630 auto color = surface->join(r, g, b, 255);
631 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
632 if (span->coverage == 255) {
633 rasterPixel32(surface->buf32 + span->y * surface->stride, color, span->x, span->len);
634 } else {
635 auto dst = &surface->buf32[span->y * surface->stride + span->x];
636 auto src = ALPHA_BLEND(color, span->coverage);
637 auto ialpha = 255 - span->coverage;
638 for (uint32_t x = 0; x < span->len; ++x, ++dst) {
639 *dst = src + ALPHA_BLEND(*dst, ialpha);
640 }
641 }
642 }
643 //8bit grayscale
644 } else if (surface->channelSize == sizeof(uint8_t)) {
645 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
646 if (span->coverage == 255) {
647 rasterGrayscale8(surface->buf8, span->coverage, span->y * surface->stride + span->x, span->len);
648 } else {
649 auto dst = &surface->buf8[span->y * surface->stride + span->x];
650 auto ialpha = 255 - span->coverage;
651 for (uint32_t x = 0; x < span->len; ++x, ++dst) {
652 *dst = span->coverage + MULTIPLY(*dst, ialpha);
653 }
654 }
655 }
656 }
657 return true;
658 }
659
660
661 static bool _rasterRle(SwSurface* surface, SwRle* rle, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
662 {
663 if (!rle) return false;
664
665 if (_compositing(surface)) {
666 if (_matting(surface)) return _rasterMattedRle(surface, rle, r, g, b, a);
667 else return _rasterMaskedRle(surface, rle, r, g, b, a);
668 } else if (_blending(surface)) {
669 return _rasterBlendingRle(surface, rle, r, g, b, a);
670 } else {
671 if (a == 255) return _rasterSolidRle(surface, rle, r, g, b);
672 else return _rasterTranslucentRle(surface, rle, r, g, b, a);
673 }
674 return false;
675 }
676
677
678 /************************************************************************/
679 /* RLE Scaled Image */
680 /************************************************************************/
681
682 #define SCALED_IMAGE_RANGE_Y(y) \
683 auto sy = (y) * itransform->e22 + itransform->e23 - 0.49f; \
684 if (sy <= -0.5f || (uint32_t)(sy + 0.5f) >= image.h) continue; \
685 if (scaleMethod == _interpDownScaler) { \
686 auto my = (int32_t)nearbyint(sy); \
687 miny = my - (int32_t)sampleSize; \
688 if (miny < 0) miny = 0; \
689 maxy = my + (int32_t)sampleSize; \
690 if (maxy >= (int32_t)image.h) maxy = (int32_t)image.h; \
691 }
692
693 #define SCALED_IMAGE_RANGE_X \
694 auto sx = (x) * itransform->e11 + itransform->e13 - 0.49f; \
695 if (sx <= -0.5f || (uint32_t)(sx + 0.5f) >= image.w) continue; \
696
697 static bool _rasterScaledMaskedRleImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
698 {
699 TVGERR("SW_ENGINE", "Not Supported Scaled Masked(%d) Rle Image", (int)surface->compositor->method);
700 return false;
701 }
702
703
704 static bool _rasterScaledMattedRleImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
705 {
706 TVGLOG("SW_ENGINE", "Scaled Matted(%d) Rle Image", (int)surface->compositor->method);
707
708 auto span = image.rle->spans;
709 auto csize = surface->compositor->image.channelSize;
710 auto alpha = surface->alpha(surface->compositor->method);
711 auto scaleMethod = image.scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler;
712 auto sampleSize = _sampleSize(image.scale);
713 int32_t miny = 0, maxy = 0;
714
715 for (uint32_t i = 0; i < image.rle->size; ++i, ++span) {
716 SCALED_IMAGE_RANGE_Y(span->y)
717 auto dst = &surface->buf32[span->y * surface->stride + span->x];
718 auto cmp = &surface->compositor->image.buf8[(span->y * surface->compositor->image.stride + span->x) * csize];
719 auto a = MULTIPLY(span->coverage, opacity);
720 for (uint32_t x = static_cast<uint32_t>(span->x); x < static_cast<uint32_t>(span->x) + span->len; ++x, ++dst, cmp += csize) {
721 SCALED_IMAGE_RANGE_X
722 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
723 src = ALPHA_BLEND(src, (a == 255) ? alpha(cmp) : MULTIPLY(alpha(cmp), a));
724 *dst = src + ALPHA_BLEND(*dst, IA(src));
725 }
726 }
727 return true;
728 }
729
730
731 static bool _rasterScaledBlendingRleImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
732 {
733 auto span = image.rle->spans;
734 auto scaleMethod = image.scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler;
735 auto sampleSize = _sampleSize(image.scale);
736 int32_t miny = 0, maxy = 0;
737
738 for (uint32_t i = 0; i < image.rle->size; ++i, ++span) {
739 SCALED_IMAGE_RANGE_Y(span->y)
740 auto dst = &surface->buf32[span->y * surface->stride + span->x];
741 auto alpha = MULTIPLY(span->coverage, opacity);
742 if (alpha == 255) {
743 for (uint32_t x = static_cast<uint32_t>(span->x); x < static_cast<uint32_t>(span->x) + span->len; ++x, ++dst) {
744 SCALED_IMAGE_RANGE_X
745 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
746 auto tmp = surface->blender(src, *dst, 255);
747 *dst = INTERPOLATE(tmp, *dst, A(src));
748 }
749 } else {
750 for (uint32_t x = static_cast<uint32_t>(span->x); x < static_cast<uint32_t>(span->x) + span->len; ++x, ++dst) {
751 SCALED_IMAGE_RANGE_X
752 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
753 auto tmp = surface->blender(src, *dst, 255);
754 *dst = INTERPOLATE(tmp, *dst, MULTIPLY(alpha, A(src)));
755 }
756 }
757 }
758 return true;
759 }
760
761
762 static bool _rasterScaledRleImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
763 {
764 auto span = image.rle->spans;
765 auto scaleMethod = image.scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler;
766 auto sampleSize = _sampleSize(image.scale);
767 int32_t miny = 0, maxy = 0;
768
769 for (uint32_t i = 0; i < image.rle->size; ++i, ++span) {
770 SCALED_IMAGE_RANGE_Y(span->y)
771 auto dst = &surface->buf32[span->y * surface->stride + span->x];
772 auto alpha = MULTIPLY(span->coverage, opacity);
773 for (uint32_t x = static_cast<uint32_t>(span->x); x < static_cast<uint32_t>(span->x) + span->len; ++x, ++dst) {
774 SCALED_IMAGE_RANGE_X
775 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
776 if (alpha < 255) src = ALPHA_BLEND(src, alpha);
777 *dst = src + ALPHA_BLEND(*dst, IA(src));
778 }
779 }
780 return true;
781 }
782
783
784 /************************************************************************/
785 /* RLE Direct Image */
786 /************************************************************************/
787
788 static bool _rasterDirectMattedRleImage(SwSurface* surface, const SwImage& image, uint8_t opacity)
789 {
790 TVGLOG("SW_ENGINE", "Direct Matted(%d) Rle Image", (int)surface->compositor->method);
791
792 auto span = image.rle->spans;
793 auto csize = surface->compositor->image.channelSize;
794 auto cbuffer = surface->compositor->image.buf8;
795 auto alpha = surface->alpha(surface->compositor->method);
796
797 for (uint32_t i = 0; i < image.rle->size; ++i, ++span) {
798 auto dst = &surface->buf32[span->y * surface->stride + span->x];
799 auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize];
800 auto img = image.buf32 + (span->y + image.oy) * image.stride + (span->x + image.ox);
801 auto a = MULTIPLY(span->coverage, opacity);
802 if (a == 255) {
803 for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img, cmp += csize) {
804 auto tmp = ALPHA_BLEND(*img, alpha(cmp));
805 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
806 }
807 } else {
808 for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img, cmp += csize) {
809 auto tmp = ALPHA_BLEND(*img, MULTIPLY(a, alpha(cmp)));
810 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
811 }
812 }
813 }
814 return true;
815 }
816
817
818 static bool _rasterDirectBlendingRleImage(SwSurface* surface, const SwImage& image, uint8_t opacity)
819 {
820 auto span = image.rle->spans;
821
822 for (uint32_t i = 0; i < image.rle->size; ++i, ++span) {
823 auto dst = &surface->buf32[span->y * surface->stride + span->x];
824 auto img = image.buf32 + (span->y + image.oy) * image.stride + (span->x + image.ox);
825 auto alpha = MULTIPLY(span->coverage, opacity);
826 if (alpha == 255) {
827 for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) {
828 *dst = surface->blender(*img, *dst, 255);
829 }
830 } else {
831 for (uint32_t x = 0; x < span->len; ++x, ++dst, ++img) {
832 auto tmp = surface->blender(*img, *dst, 255);
833 *dst = INTERPOLATE(tmp, *dst, MULTIPLY(alpha, A(*img)));
834 }
835 }
836 }
837 return true;
838 }
839
840
841 static bool _rasterDirectRleImage(SwSurface* surface, const SwImage& image, uint8_t opacity)
842 {
843 auto span = image.rle->spans;
844
845 for (uint32_t i = 0; i < image.rle->size; ++i, ++span) {
846 auto dst = &surface->buf32[span->y * surface->stride + span->x];
847 auto img = image.buf32 + (span->y + image.oy) * image.stride + (span->x + image.ox);
848 auto alpha = MULTIPLY(span->coverage, opacity);
849 rasterTranslucentPixel32(dst, img, span->len, alpha);
850 }
851 return true;
852 }
853
854
855 static bool _rasterDirectMaskedRleImage(SwSurface* surface, const SwImage& image, uint8_t opacity)
856 {
857 TVGERR("SW_ENGINE", "Not Supported Direct Masked(%d) Rle Image", (int)surface->compositor->method);
858 return false;
859 }
860
861
862 /************************************************************************/
863 /*Scaled Image */
864 /************************************************************************/
865
866 static bool _rasterScaledMaskedImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
867 {
868 TVGERR("SW_ENGINE", "Not Supported Scaled Masked Image!");
869 return false;
870 }
871
872
873 static bool _rasterScaledMattedImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
874 {
875 if (surface->channelSize == sizeof(uint8_t)) {
876 TVGERR("SW_ENGINE", "Not supported grayscale scaled matted image!");
877 return false;
878 }
879
880 auto dbuffer = surface->buf32 + (region.min.y * surface->stride + region.min.x);
881 auto csize = surface->compositor->image.channelSize;
882 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize;
883 auto alpha = surface->alpha(surface->compositor->method);
884
885 TVGLOG("SW_ENGINE", "Scaled Matted(%d) Image [Region: %lu %lu %lu %lu]", (int)surface->compositor->method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y);
886
887 auto scaleMethod = image.scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler;
888 auto sampleSize = _sampleSize(image.scale);
889 int32_t miny = 0, maxy = 0;
890
891 for (auto y = region.min.y; y < region.max.y; ++y) {
892 SCALED_IMAGE_RANGE_Y(y)
893 auto dst = dbuffer;
894 auto cmp = cbuffer;
895 for (auto x = region.min.x; x < region.max.x; ++x, ++dst, cmp += csize) {
896 SCALED_IMAGE_RANGE_X
897 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
898 auto tmp = ALPHA_BLEND(src, opacity == 255 ? alpha(cmp) : MULTIPLY(opacity, alpha(cmp)));
899 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
900 }
901 dbuffer += surface->stride;
902 cbuffer += surface->compositor->image.stride * csize;
903 }
904 return true;
905 }
906
907
908 static bool _rasterScaledBlendingImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
909 {
910 if (surface->channelSize == sizeof(uint8_t)) {
911 TVGERR("SW_ENGINE", "Not supported grayscale scaled blending image!");
912 return false;
913 }
914
915 auto dbuffer = surface->buf32 + (region.min.y * surface->stride + region.min.x);
916 auto scaleMethod = image.scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler;
917 auto sampleSize = _sampleSize(image.scale);
918 int32_t miny = 0, maxy = 0;
919
920 for (auto y = region.min.y; y < region.max.y; ++y, dbuffer += surface->stride) {
921 SCALED_IMAGE_RANGE_Y(y)
922 auto dst = dbuffer;
923 for (auto x = region.min.x; x < region.max.x; ++x, ++dst) {
924 SCALED_IMAGE_RANGE_X
925 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
926 auto tmp = surface->blender(src, *dst, 255);
927 *dst = INTERPOLATE(tmp, *dst, MULTIPLY(opacity, A(src)));
928 }
929 }
930 return true;
931 }
932
933
934 static bool _rasterScaledImage(SwSurface* surface, const SwImage& image, const Matrix* itransform, const SwBBox& region, uint8_t opacity)
935 {
936 auto scaleMethod = image.scale < DOWN_SCALE_TOLERANCE ? _interpDownScaler : _interpUpScaler;
937 auto sampleSize = _sampleSize(image.scale);
938 int32_t miny = 0, maxy = 0;
939
940 //32bits channels
941 if (surface->channelSize == sizeof(uint32_t)) {
942 auto buffer = surface->buf32 + (region.min.y * surface->stride + region.min.x);
943 for (auto y = region.min.y; y < region.max.y; ++y, buffer += surface->stride) {
944 SCALED_IMAGE_RANGE_Y(y)
945 auto dst = buffer;
946 for (auto x = region.min.x; x < region.max.x; ++x, ++dst) {
947 SCALED_IMAGE_RANGE_X
948 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
949 if (opacity < 255) src = ALPHA_BLEND(src, opacity);
950 *dst = src + ALPHA_BLEND(*dst, IA(src));
951 }
952 }
953 } else if (surface->channelSize == sizeof(uint8_t)) {
954 auto buffer = surface->buf8 + (region.min.y * surface->stride + region.min.x);
955 for (auto y = region.min.y; y < region.max.y; ++y, buffer += surface->stride) {
956 SCALED_IMAGE_RANGE_Y(y)
957 auto dst = buffer;
958 for (auto x = region.min.x; x < region.max.x; ++x, ++dst) {
959 SCALED_IMAGE_RANGE_X
960 auto src = scaleMethod(image.buf32, image.stride, image.w, image.h, sx, sy, miny, maxy, sampleSize);
961 *dst = MULTIPLY(A(src), opacity);
962 }
963 }
964 }
965 return true;
966 }
967
968
969 /************************************************************************/
970 /* Direct Image */
971 /************************************************************************/
972
973 static bool _rasterDirectMaskedImage(SwSurface* surface, const SwImage& image, const SwBBox& region, uint8_t opacity)
974 {
975 TVGERR("SW_ENGINE", "Not Supported: Direct Masked Image");
976 return false;
977 }
978
979
980 static bool _rasterDirectMattedImage(SwSurface* surface, const SwImage& image, const SwBBox& region, uint8_t opacity)
981 {
982 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
983 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
984 auto csize = surface->compositor->image.channelSize;
985 auto alpha = surface->alpha(surface->compositor->method);
986 auto sbuffer = image.buf32 + (region.min.y + image.oy) * image.stride + (region.min.x + image.ox);
987 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize; //compositor buffer
988
989 TVGLOG("SW_ENGINE", "Direct Matted(%d) Image [Region: %lu %lu %u %u]", (int)surface->compositor->method, region.min.x, region.min.y, w, h);
990
991 //32 bits
992 if (surface->channelSize == sizeof(uint32_t)) {
993 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
994 for (uint32_t y = 0; y < h; ++y) {
995 auto dst = buffer;
996 auto cmp = cbuffer;
997 auto src = sbuffer;
998 if (opacity == 255) {
999 for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) {
1000 auto tmp = ALPHA_BLEND(*src, alpha(cmp));
1001 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
1002 }
1003 } else {
1004 for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) {
1005 auto tmp = ALPHA_BLEND(*src, MULTIPLY(opacity, alpha(cmp)));
1006 *dst = tmp + ALPHA_BLEND(*dst, IA(tmp));
1007 }
1008 }
1009 buffer += surface->stride;
1010 cbuffer += surface->compositor->image.stride * csize;
1011 sbuffer += image.stride;
1012 }
1013 //8 bits
1014 } else if (surface->channelSize == sizeof(uint8_t)) {
1015 auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x;
1016 for (uint32_t y = 0; y < h; ++y) {
1017 auto dst = buffer;
1018 auto cmp = cbuffer;
1019 auto src = sbuffer;
1020 if (opacity == 255) {
1021 for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) {
1022 auto tmp = MULTIPLY(A(*src), alpha(cmp));
1023 *dst = tmp + MULTIPLY(*dst, 255 - tmp);
1024 }
1025 } else {
1026 for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) {
1027 auto tmp = MULTIPLY(A(*src), MULTIPLY(opacity, alpha(cmp)));
1028 *dst = tmp + MULTIPLY(*dst, 255 - tmp);
1029 }
1030 }
1031 buffer += surface->stride;
1032 cbuffer += surface->compositor->image.stride * csize;
1033 sbuffer += image.stride;
1034 }
1035 }
1036 return true;
1037 }
1038
1039
1040 static bool _rasterDirectBlendingImage(SwSurface* surface, const SwImage& image, const SwBBox& region, uint8_t opacity)
1041 {
1042 if (surface->channelSize == sizeof(uint8_t)) {
1043 TVGERR("SW_ENGINE", "Not supported grayscale image!");
1044 return false;
1045 }
1046
1047 auto dbuffer = &surface->buf32[region.min.y * surface->stride + region.min.x];
1048 auto sbuffer = image.buf32 + (region.min.y + image.oy) * image.stride + (region.min.x + image.ox);
1049
1050 for (auto y = region.min.y; y < region.max.y; ++y) {
1051 auto dst = dbuffer;
1052 auto src = sbuffer;
1053 if (opacity == 255) {
1054 for (auto x = region.min.x; x < region.max.x; x++, dst++, src++) {
1055 auto tmp = surface->blender(*src, *dst, 255);
1056 *dst = INTERPOLATE(tmp, *dst, A(*src));
1057 }
1058 } else {
1059 for (auto x = region.min.x; x < region.max.x; x++, dst++, src++) {
1060 auto tmp = surface->blender(*src, *dst, 255);
1061 *dst = INTERPOLATE(tmp, *dst, MULTIPLY(opacity, A(*src)));
1062 }
1063 }
1064 dbuffer += surface->stride;
1065 sbuffer += image.stride;
1066 }
1067 return true;
1068 }
1069
1070
1071 static bool _rasterDirectImage(SwSurface* surface, const SwImage& image, const SwBBox& region, uint8_t opacity)
1072 {
1073 auto sbuffer = image.buf32 + (region.min.y + image.oy) * image.stride + (region.min.x + image.ox);
1074
1075 //32bits channels
1076 if (surface->channelSize == sizeof(uint32_t)) {
1077 auto dbuffer = &surface->buf32[region.min.y * surface->stride + region.min.x];
1078 for (auto y = region.min.y; y < region.max.y; ++y) {
1079 rasterTranslucentPixel32(dbuffer, sbuffer, region.max.x - region.min.x, opacity);
1080 dbuffer += surface->stride;
1081 sbuffer += image.stride;
1082 }
1083 //8bits grayscale
1084 //32 -> 8 direct converting seems an avoidable stage. maybe draw to a masking image after an intermediate scene. Can get rid of this?
1085 } else if (surface->channelSize == sizeof(uint8_t)) {
1086 auto dbuffer = &surface->buf8[region.min.y * surface->stride + region.min.x];
1087 for (auto y = region.min.y; y < region.max.y; ++y, dbuffer += surface->stride, sbuffer += image.stride) {
1088 auto dst = dbuffer;
1089 auto src = sbuffer;
1090 if (opacity == 255) {
1091 for (auto x = region.min.x; x < region.max.x; ++x, ++dst, ++src) {
1092 *dst = A(*src) + MULTIPLY(*dst, IA(*src));
1093 }
1094 } else {
1095 for (auto x = region.min.x; x < region.max.x; ++x, ++dst, ++src) {
1096 *dst = INTERPOLATE8(A(*src), *dst, opacity);
1097 }
1098 }
1099 }
1100 }
1101 return true;
1102 }
1103
1104
1105 static bool _rasterDirectMattedBlendingImage(SwSurface* surface, const SwImage& image, const SwBBox& region, uint8_t opacity)
1106 {
1107 if (surface->channelSize == sizeof(uint8_t)) {
1108 TVGERR("SW_ENGINE", "Not supported grayscale image!");
1109 return false;
1110 }
1111
1112 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1113 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1114 auto csize = surface->compositor->image.channelSize;
1115 auto alpha = surface->alpha(surface->compositor->method);
1116 auto sbuffer = image.buf32 + (region.min.y + image.oy) * image.stride + (region.min.x + image.ox);
1117 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize; //compositor buffer
1118 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
1119
1120 for (uint32_t y = 0; y < h; ++y) {
1121 auto dst = buffer;
1122 auto cmp = cbuffer;
1123 auto src = sbuffer;
1124 if (opacity == 255) {
1125 for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) {
1126 auto tmp = ALPHA_BLEND(*src, alpha(cmp));
1127 *dst = INTERPOLATE(surface->blender(tmp, *dst, 255), *dst, A(tmp));
1128 }
1129 } else {
1130 for (uint32_t x = 0; x < w; ++x, ++dst, ++src, cmp += csize) {
1131 auto tmp = ALPHA_BLEND(*src, alpha(cmp));
1132 *dst = INTERPOLATE(surface->blender(tmp, *dst, 255), *dst, MULTIPLY(opacity, A(tmp)));
1133 }
1134 }
1135 buffer += surface->stride;
1136 cbuffer += surface->compositor->image.stride * csize;
1137 sbuffer += image.stride;
1138 }
1139 return true;
1140 }
1141
1142
1143 /************************************************************************/
1144 /* Rect Gradient */
1145 /************************************************************************/
1146
1147 template<typename fillMethod>
1148 static bool _rasterCompositeGradientMaskedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill, SwMask maskOp)
1149 {
1150 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1151 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1152 auto cstride = surface->compositor->image.stride;
1153 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x);
1154
1155 for (uint32_t y = 0; y < h; ++y) {
1156 fillMethod()(fill, cbuffer, region.min.y + y, region.min.x, w, maskOp, 255);
1157 cbuffer += surface->stride;
1158 }
1159 return _compositeMaskImage(surface, surface->compositor->image, surface->compositor->bbox);
1160 }
1161
1162
1163 template<typename fillMethod>
1164 static bool _rasterDirectGradientMaskedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill, SwMask maskOp)
1165 {
1166 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1167 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1168 auto cstride = surface->compositor->image.stride;
1169 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * cstride + region.min.x);
1170 auto dbuffer = surface->buf8 + (region.min.y * surface->stride + region.min.x);
1171
1172 for (uint32_t y = 0; y < h; ++y) {
1173 fillMethod()(fill, dbuffer, region.min.y + y, region.min.x, w, cbuffer, maskOp, 255);
1174 cbuffer += cstride;
1175 dbuffer += surface->stride;
1176 }
1177 return true;
1178 }
1179
1180
1181 template<typename fillMethod>
1182 static bool _rasterGradientMaskedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1183 {
1184 auto method = surface->compositor->method;
1185
1186 TVGLOG("SW_ENGINE", "Masked(%d) Gradient [Region: %lu %lu %lu %lu]", (int)method, region.min.x, region.min.y, region.max.x - region.min.x, region.max.y - region.min.y);
1187
1188 auto maskOp = _getMaskOp(method);
1189
1190 if (_direct(method)) return _rasterDirectGradientMaskedRect<fillMethod>(surface, region, fill, maskOp);
1191 else return _rasterCompositeGradientMaskedRect<fillMethod>(surface, region, fill, maskOp);
1192
1193 return false;
1194 }
1195
1196
1197 template<typename fillMethod>
1198 static bool _rasterGradientMattedRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1199 {
1200 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
1201 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1202 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1203 auto csize = surface->compositor->image.channelSize;
1204 auto cbuffer = surface->compositor->image.buf8 + (region.min.y * surface->compositor->image.stride + region.min.x) * csize;
1205 auto alpha = surface->alpha(surface->compositor->method);
1206
1207 TVGLOG("SW_ENGINE", "Matted(%d) Gradient [Region: %lu %lu %u %u]", (int)surface->compositor->method, region.min.x, region.min.y, w, h);
1208
1209 for (uint32_t y = 0; y < h; ++y) {
1210 fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, cbuffer, alpha, csize, 255);
1211 buffer += surface->stride;
1212 cbuffer += surface->stride * csize;
1213 }
1214 return true;
1215 }
1216
1217
1218 template<typename fillMethod>
1219 static bool _rasterBlendingGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1220 {
1221 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
1222 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1223 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1224
1225 if (fill->translucent) {
1226 for (uint32_t y = 0; y < h; ++y) {
1227 fillMethod()(fill, buffer + y * surface->stride, region.min.y + y, region.min.x, w, opBlendPreNormal, surface->blender, 255);
1228 }
1229 } else {
1230 for (uint32_t y = 0; y < h; ++y) {
1231 fillMethod()(fill, buffer + y * surface->stride, region.min.y + y, region.min.x, w, opBlendSrcOver, surface->blender, 255);
1232 }
1233 }
1234 return true;
1235 }
1236
1237 template<typename fillMethod>
1238 static bool _rasterTranslucentGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1239 {
1240 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1241 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1242
1243 //32 bits
1244 if (surface->channelSize == sizeof(uint32_t)) {
1245 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
1246 for (uint32_t y = 0; y < h; ++y) {
1247 fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, opBlendPreNormal, 255);
1248 buffer += surface->stride;
1249 }
1250 //8 bits
1251 } else if (surface->channelSize == sizeof(uint8_t)) {
1252 auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x;
1253 for (uint32_t y = 0; y < h; ++y) {
1254 fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, _opMaskAdd, 255);
1255 buffer += surface->stride;
1256 }
1257 }
1258 return true;
1259 }
1260
1261
1262 template<typename fillMethod>
1263 static bool _rasterSolidGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1264 {
1265 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
1266 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
1267
1268 //32 bits
1269 if (surface->channelSize == sizeof(uint32_t)) {
1270 auto buffer = surface->buf32 + (region.min.y * surface->stride) + region.min.x;
1271 for (uint32_t y = 0; y < h; ++y) {
1272 fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, opBlendSrcOver, 255);
1273 buffer += surface->stride;
1274 }
1275 //8 bits
1276 } else if (surface->channelSize == sizeof(uint8_t)) {
1277 auto buffer = surface->buf8 + (region.min.y * surface->stride) + region.min.x;
1278 for (uint32_t y = 0; y < h; ++y) {
1279 fillMethod()(fill, buffer, region.min.y + y, region.min.x, w, _opMaskNone, 255);
1280 buffer += surface->stride;
1281 }
1282 }
1283 return true;
1284 }
1285
1286
1287 static bool _rasterLinearGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1288 {
1289 if (_compositing(surface)) {
1290 if (_matting(surface)) return _rasterGradientMattedRect<FillLinear>(surface, region, fill);
1291 else return _rasterGradientMaskedRect<FillLinear>(surface, region, fill);
1292 } else if (_blending(surface)) {
1293 return _rasterBlendingGradientRect<FillLinear>(surface, region, fill);
1294 } else {
1295 if (fill->translucent) return _rasterTranslucentGradientRect<FillLinear>(surface, region, fill);
1296 else _rasterSolidGradientRect<FillLinear>(surface, region, fill);
1297 }
1298 return false;
1299 }
1300
1301
1302 static bool _rasterRadialGradientRect(SwSurface* surface, const SwBBox& region, const SwFill* fill)
1303 {
1304 if (_compositing(surface)) {
1305 if (_matting(surface)) return _rasterGradientMattedRect<FillRadial>(surface, region, fill);
1306 else return _rasterGradientMaskedRect<FillRadial>(surface, region, fill);
1307 } else if (_blending(surface)) {
1308 return _rasterBlendingGradientRect<FillRadial>(surface, region, fill);
1309 } else {
1310 if (fill->translucent) return _rasterTranslucentGradientRect<FillRadial>(surface, region, fill);
1311 else _rasterSolidGradientRect<FillRadial>(surface, region, fill);
1312 }
1313 return false;
1314 }
1315
1316
1317 /************************************************************************/
1318 /* Rle Gradient */
1319 /************************************************************************/
1320
1321 template<typename fillMethod>
1322 static bool _rasterCompositeGradientMaskedRle(SwSurface* surface, const SwRle* rle, const SwFill* fill, SwMask maskOp)
1323 {
1324 auto span = rle->spans;
1325 auto cstride = surface->compositor->image.stride;
1326 auto cbuffer = surface->compositor->image.buf8;
1327
1328 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1329 auto cmp = &cbuffer[span->y * cstride + span->x];
1330 fillMethod()(fill, cmp, span->y, span->x, span->len, maskOp, span->coverage);
1331 }
1332 return _compositeMaskImage(surface, surface->compositor->image, surface->compositor->bbox);
1333 }
1334
1335
1336 template<typename fillMethod>
1337 static bool _rasterDirectGradientMaskedRle(SwSurface* surface, const SwRle* rle, const SwFill* fill, SwMask maskOp)
1338 {
1339 auto span = rle->spans;
1340 auto cstride = surface->compositor->image.stride;
1341 auto cbuffer = surface->compositor->image.buf8;
1342 auto dbuffer = surface->buf8;
1343
1344 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1345 auto cmp = &cbuffer[span->y * cstride + span->x];
1346 auto dst = &dbuffer[span->y * surface->stride + span->x];
1347 fillMethod()(fill, dst, span->y, span->x, span->len, cmp, maskOp, span->coverage);
1348 }
1349 return true;
1350 }
1351
1352
1353 template<typename fillMethod>
1354 static bool _rasterGradientMaskedRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1355 {
1356 auto method = surface->compositor->method;
1357
1358 TVGLOG("SW_ENGINE", "Masked(%d) Rle Linear Gradient", (int)method);
1359
1360 auto maskOp = _getMaskOp(method);
1361
1362 if (_direct(method)) return _rasterDirectGradientMaskedRle<fillMethod>(surface, rle, fill, maskOp);
1363 else return _rasterCompositeGradientMaskedRle<fillMethod>(surface, rle, fill, maskOp);
1364 return false;
1365 }
1366
1367
1368 template<typename fillMethod>
1369 static bool _rasterGradientMattedRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1370 {
1371 TVGLOG("SW_ENGINE", "Matted(%d) Rle Linear Gradient", (int)surface->compositor->method);
1372
1373 auto span = rle->spans;
1374 auto csize = surface->compositor->image.channelSize;
1375 auto cbuffer = surface->compositor->image.buf8;
1376 auto alpha = surface->alpha(surface->compositor->method);
1377
1378 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1379 auto dst = &surface->buf32[span->y * surface->stride + span->x];
1380 auto cmp = &cbuffer[(span->y * surface->compositor->image.stride + span->x) * csize];
1381 fillMethod()(fill, dst, span->y, span->x, span->len, cmp, alpha, csize, span->coverage);
1382 }
1383 return true;
1384 }
1385
1386
1387 template<typename fillMethod>
1388 static bool _rasterBlendingGradientRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1389 {
1390 auto span = rle->spans;
1391
1392 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1393 auto dst = &surface->buf32[span->y * surface->stride + span->x];
1394 fillMethod()(fill, dst, span->y, span->x, span->len, opBlendPreNormal, surface->blender, span->coverage);
1395 }
1396 return true;
1397 }
1398
1399
1400 template<typename fillMethod>
1401 static bool _rasterTranslucentGradientRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1402 {
1403 auto span = rle->spans;
1404
1405 //32 bits
1406 if (surface->channelSize == sizeof(uint32_t)) {
1407 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1408 auto dst = &surface->buf32[span->y * surface->stride + span->x];
1409 if (span->coverage == 255) fillMethod()(fill, dst, span->y, span->x, span->len, opBlendPreNormal, 255);
1410 else fillMethod()(fill, dst, span->y, span->x, span->len, opBlendNormal, span->coverage);
1411 }
1412 //8 bits
1413 } else if (surface->channelSize == sizeof(uint8_t)) {
1414 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1415 auto dst = &surface->buf8[span->y * surface->stride + span->x];
1416 fillMethod()(fill, dst, span->y, span->x, span->len, _opMaskAdd, span->coverage);
1417 }
1418 }
1419 return true;
1420 }
1421
1422
1423 template<typename fillMethod>
1424 static bool _rasterSolidGradientRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1425 {
1426 auto span = rle->spans;
1427
1428 //32 bits
1429 if (surface->channelSize == sizeof(uint32_t)) {
1430 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1431 auto dst = &surface->buf32[span->y * surface->stride + span->x];
1432 if (span->coverage == 255) fillMethod()(fill, dst, span->y, span->x, span->len, opBlendSrcOver, 255);
1433 else fillMethod()(fill, dst, span->y, span->x, span->len, opBlendInterp, span->coverage);
1434 }
1435 //8 bits
1436 } else if (surface->channelSize == sizeof(uint8_t)) {
1437 for (uint32_t i = 0; i < rle->size; ++i, ++span) {
1438 auto dst = &surface->buf8[span->y * surface->stride + span->x];
1439 if (span->coverage == 255) fillMethod()(fill, dst, span->y, span->x, span->len, _opMaskNone, 255);
1440 else fillMethod()(fill, dst, span->y, span->x, span->len, _opMaskAdd, span->coverage);
1441 }
1442 }
1443
1444 return true;
1445 }
1446
1447
1448 static bool _rasterLinearGradientRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1449 {
1450 if (!rle) return false;
1451
1452 if (_compositing(surface)) {
1453 if (_matting(surface)) return _rasterGradientMattedRle<FillLinear>(surface, rle, fill);
1454 else return _rasterGradientMaskedRle<FillLinear>(surface, rle, fill);
1455 } else if (_blending(surface)) {
1456 return _rasterBlendingGradientRle<FillLinear>(surface, rle, fill);
1457 } else {
1458 if (fill->translucent) return _rasterTranslucentGradientRle<FillLinear>(surface, rle, fill);
1459 else return _rasterSolidGradientRle<FillLinear>(surface, rle, fill);
1460 }
1461 return false;
1462 }
1463
1464
1465 static bool _rasterRadialGradientRle(SwSurface* surface, const SwRle* rle, const SwFill* fill)
1466 {
1467 if (!rle) return false;
1468
1469 if (_compositing(surface)) {
1470 if (_matting(surface)) return _rasterGradientMattedRle<FillRadial>(surface, rle, fill);
1471 else return _rasterGradientMaskedRle<FillRadial>(surface, rle, fill);
1472 } else if (_blending(surface)) {
1473 return _rasterBlendingGradientRle<FillRadial>(surface, rle, fill);
1474 } else {
1475 if (fill->translucent) return _rasterTranslucentGradientRle<FillRadial>(surface, rle, fill);
1476 else return _rasterSolidGradientRle<FillRadial>(surface, rle, fill);
1477 }
1478 return false;
1479 }
1480
1481
1482 /************************************************************************/
1483 /* External Class Implementation */
1484 /************************************************************************/
1485
1486 void rasterTranslucentPixel32(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity)
1487 {
1488 //TODO: Support SIMD accelerations
1489 cRasterTranslucentPixels(dst, src, len, opacity);
1490 }
1491
1492
1493 void rasterPixel32(uint32_t* dst, uint32_t* src, uint32_t len, uint8_t opacity)
1494 {
1495 //TODO: Support SIMD accelerations
1496 cRasterPixels(dst, src, len, opacity);
1497 }
1498
1499
1500 void rasterGrayscale8(uint8_t *dst, uint8_t val, uint32_t offset, int32_t len)
1501 {
1502 #if defined(THORVG_AVX_VECTOR_SUPPORT)
1503 avxRasterGrayscale8(dst, val, offset, len);
1504 #elif defined(THORVG_NEON_VECTOR_SUPPORT)
1505 neonRasterGrayscale8(dst, val, offset, len);
1506 #else
1507 cRasterPixels(dst, val, offset, len);
1508 #endif
1509 }
1510
1511
1512 void rasterPixel32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len)
1513 {
1514 #if defined(THORVG_AVX_VECTOR_SUPPORT)
1515 avxRasterPixel32(dst, val, offset, len);
1516 #elif defined(THORVG_NEON_VECTOR_SUPPORT)
1517 neonRasterPixel32(dst, val, offset, len);
1518 #else
1519 cRasterPixels(dst, val, offset, len);
1520 #endif
1521 }
1522
1523
1524 bool rasterCompositor(SwSurface* surface)
1525 {
1526 //See CompositeMethod, Alpha:3, InvAlpha:4, Luma:5, InvLuma:6
1527 surface->alphas[0] = _alpha;
1528 surface->alphas[1] = _ialpha;
1529
1530 if (surface->cs == ColorSpace::ABGR8888 || surface->cs == ColorSpace::ABGR8888S) {
1531 surface->join = _abgrJoin;
1532 surface->alphas[2] = _abgrLuma;
1533 surface->alphas[3] = _abgrInvLuma;
1534 } else if (surface->cs == ColorSpace::ARGB8888 || surface->cs == ColorSpace::ARGB8888S) {
1535 surface->join = _argbJoin;
1536 surface->alphas[2] = _argbLuma;
1537 surface->alphas[3] = _argbInvLuma;
1538 } else {
1539 TVGERR("SW_ENGINE", "Unsupported Colorspace(%d) is expected!", surface->cs);
1540 return false;
1541 }
1542 return true;
1543 }
1544
1545
1546 bool rasterClear(SwSurface* surface, uint32_t x, uint32_t y, uint32_t w, uint32_t h, pixel_t val)
1547 {
1548 if (!surface || !surface->buf32 || surface->stride == 0 || surface->w == 0 || surface->h == 0) return false;
1549
1550 //32 bits
1551 if (surface->channelSize == sizeof(uint32_t)) {
1552 //full clear
1553 if (w == surface->stride) {
1554 rasterPixel32(surface->buf32, val, surface->stride * y, w * h);
1555 //partial clear
1556 } else {
1557 for (uint32_t i = 0; i < h; i++) {
1558 rasterPixel32(surface->buf32, val, (surface->stride * y + x) + (surface->stride * i), w);
1559 }
1560 }
1561 //8 bits
1562 } else if (surface->channelSize == sizeof(uint8_t)) {
1563 //full clear
1564 if (w == surface->stride) {
1565 rasterGrayscale8(surface->buf8, 0x00, surface->stride * y, w * h);
1566 //partial clear
1567 } else {
1568 for (uint32_t i = 0; i < h; i++) {
1569 rasterGrayscale8(surface->buf8, 0x00, (surface->stride * y + x) + (surface->stride * i), w);
1570 }
1571 }
1572 }
1573 return true;
1574 }
1575
1576
1577 uint32_t rasterUnpremultiply(uint32_t data)
1578 {
1579 uint8_t a = data >> 24;
1580 if (a == 255 || a == 0) return data;
1581 uint16_t r = ((data >> 8) & 0xff00) / a;
1582 uint16_t g = ((data) & 0xff00) / a;
1583 uint16_t b = ((data << 8) & 0xff00) / a;
1584 if (r > 0xff) r = 0xff;
1585 if (g > 0xff) g = 0xff;
1586 if (b > 0xff) b = 0xff;
1587 return (a << 24) | (r << 16) | (g << 8) | (b);
1588 }
1589
1590
1591 void rasterUnpremultiply(RenderSurface* surface)
1592 {
1593 if (surface->channelSize != sizeof(uint32_t)) return;
1594
1595 TVGLOG("SW_ENGINE", "Unpremultiply [Size: %d x %d]", surface->w, surface->h);
1596
1597 //OPTIMIZE_ME: +SIMD
1598 for (uint32_t y = 0; y < surface->h; y++) {
1599 auto buffer = surface->buf32 + surface->stride * y;
1600 for (uint32_t x = 0; x < surface->w; ++x) {
1601 buffer[x] = rasterUnpremultiply(buffer[x]);
1602 }
1603 }
1604 surface->premultiplied = false;
1605 }
1606
1607
1608 void rasterPremultiply(RenderSurface* surface)
1609 {
1610 ScopedLock lock(surface->key);
1611 if (surface->premultiplied || (surface->channelSize != sizeof(uint32_t))) return;
1612 surface->premultiplied = true;
1613
1614 TVGLOG("SW_ENGINE", "Premultiply [Size: %d x %d]", surface->w, surface->h);
1615
1616 //OPTIMIZE_ME: +SIMD
1617 auto buffer = surface->buf32;
1618 for (uint32_t y = 0; y < surface->h; ++y, buffer += surface->stride) {
1619 auto dst = buffer;
1620 for (uint32_t x = 0; x < surface->w; ++x, ++dst) {
1621 auto c = *dst;
1622 auto a = (c >> 24);
1623 *dst = (c & 0xff000000) + ((((c >> 8) & 0xff) * a) & 0xff00) + ((((c & 0x00ff00ff) * a) >> 8) & 0x00ff00ff);
1624 }
1625 }
1626 }
1627
1628
1629 bool rasterScaledImage(SwSurface* surface, const SwImage& image, const Matrix& transform, const SwBBox& bbox, uint8_t opacity)
1630 {
1631 Matrix itransform;
1632
1633 if (!inverse(&transform, &itransform)) return true;
1634
1635 if (_compositing(surface)) {
1636 if (_matting(surface)) return _rasterScaledMattedImage(surface, image, &itransform, bbox, opacity);
1637 else return _rasterScaledMaskedImage(surface, image, &itransform, bbox, opacity);
1638 } else if (_blending(surface)) {
1639 return _rasterScaledBlendingImage(surface, image, &itransform, bbox, opacity);
1640 } else {
1641 return _rasterScaledImage(surface, image, &itransform, bbox, opacity);
1642 }
1643 return false;
1644 }
1645
1646
1647 bool rasterDirectImage(SwSurface* surface, const SwImage& image, const SwBBox& bbox, uint8_t opacity)
1648 {
1649 if (_compositing(surface)) {
1650 if (_matting(surface)) {
1651 if (_blending(surface)) return _rasterDirectMattedBlendingImage(surface, image, bbox, opacity);
1652 else return _rasterDirectMattedImage(surface, image, bbox, opacity);
1653 } else return _rasterDirectMaskedImage(surface, image, bbox, opacity);
1654 } else if (_blending(surface)) {
1655 return _rasterDirectBlendingImage(surface, image, bbox, opacity);
1656 } else {
1657 return _rasterDirectImage(surface, image, bbox, opacity);
1658 }
1659 return false;
1660 }
1661
1662
1663 bool rasterScaledRleImage(SwSurface* surface, const SwImage& image, const Matrix& transform, const SwBBox& bbox, uint8_t opacity)
1664 {
1665 if (surface->channelSize == sizeof(uint8_t)) {
1666 TVGERR("SW_ENGINE", "Not supported scaled rle image!");
1667 return false;
1668 }
1669
1670 Matrix itransform;
1671
1672 if (!inverse(&transform, &itransform)) return true;
1673
1674 if (_compositing(surface)) {
1675 if (_matting(surface)) return _rasterScaledMattedRleImage(surface, image, &itransform, bbox, opacity);
1676 else return _rasterScaledMaskedRleImage(surface, image, &itransform, bbox, opacity);
1677 } else if (_blending(surface)) {
1678 return _rasterScaledBlendingRleImage(surface, image, &itransform, bbox, opacity);
1679 } else {
1680 return _rasterScaledRleImage(surface, image, &itransform, bbox, opacity);
1681 }
1682 return false;
1683 }
1684
1685
1686 bool rasterDirectRleImage(SwSurface* surface, const SwImage& image, uint8_t opacity)
1687 {
1688 if (surface->channelSize == sizeof(uint8_t)) {
1689 TVGERR("SW_ENGINE", "Not supported grayscale rle image!");
1690 return false;
1691 }
1692
1693 if (_compositing(surface)) {
1694 if (_matting(surface)) return _rasterDirectMattedRleImage(surface, image, opacity);
1695 else return _rasterDirectMaskedRleImage(surface, image, opacity);
1696 } else if (_blending(surface)) {
1697 return _rasterDirectBlendingRleImage(surface, image, opacity);
1698 } else {
1699 return _rasterDirectRleImage(surface, image, opacity);
1700 }
1701 return false;
1702 }
1703
1704
1705 bool rasterGradientShape(SwSurface* surface, SwShape* shape, const Fill* fdata, uint8_t opacity)
1706 {
1707 if (!shape->fill) return false;
1708
1709 if (auto color = fillFetchSolid(shape->fill, fdata)) {
1710 auto a = MULTIPLY(color->a, opacity);
1711 return a > 0 ? rasterShape(surface, shape, color->r, color->g, color->b, a) : true;
1712 }
1713
1714 auto type = fdata->type();
1715 if (shape->fastTrack) {
1716 if (type == Type::LinearGradient) return _rasterLinearGradientRect(surface, shape->bbox, shape->fill);
1717 else if (type == Type::RadialGradient)return _rasterRadialGradientRect(surface, shape->bbox, shape->fill);
1718 } else {
1719 if (type == Type::LinearGradient) return _rasterLinearGradientRle(surface, shape->rle, shape->fill);
1720 else if (type == Type::RadialGradient) return _rasterRadialGradientRle(surface, shape->rle, shape->fill);
1721 }
1722 return false;
1723 }
1724
1725
1726 bool rasterGradientStroke(SwSurface* surface, SwShape* shape, const Fill* fdata, uint8_t opacity)
1727 {
1728 if (!shape->stroke || !shape->stroke->fill || !shape->strokeRle) return false;
1729
1730 if (auto color = fillFetchSolid(shape->stroke->fill, fdata)) {
1731 auto a = MULTIPLY(color->a, opacity);
1732 return a > 0 ? rasterStroke(surface, shape, color->r, color->g, color->b, a) : true;
1733 }
1734
1735 auto type = fdata->type();
1736 if (type == Type::LinearGradient) return _rasterLinearGradientRle(surface, shape->strokeRle, shape->stroke->fill);
1737 else if (type == Type::RadialGradient) return _rasterRadialGradientRle(surface, shape->strokeRle, shape->stroke->fill);
1738
1739 return false;
1740 }
1741
1742
1743 bool rasterShape(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
1744 {
1745 if (a < 255) {
1746 r = MULTIPLY(r, a);
1747 g = MULTIPLY(g, a);
1748 b = MULTIPLY(b, a);
1749 }
1750 if (shape->fastTrack) return _rasterRect(surface, shape->bbox, r, g, b, a);
1751 else return _rasterRle(surface, shape->rle, r, g, b, a);
1752 }
1753
1754
1755 bool rasterStroke(SwSurface* surface, SwShape* shape, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
1756 {
1757 if (a < 255) {
1758 r = MULTIPLY(r, a);
1759 g = MULTIPLY(g, a);
1760 b = MULTIPLY(b, a);
1761 }
1762
1763 return _rasterRle(surface, shape->strokeRle, r, g, b, a);
1764 }
1765
1766
1767 bool rasterConvertCS(RenderSurface* surface, ColorSpace to)
1768 {
1769 ScopedLock lock(surface->key);
1770 if (surface->cs == to) return true;
1771
1772 //TODO: Support SIMD accelerations
1773 auto from = surface->cs;
1774
1775 if (((from == ColorSpace::ABGR8888) || (from == ColorSpace::ABGR8888S)) && ((to == ColorSpace::ARGB8888) || (to == ColorSpace::ARGB8888S))) {
1776 surface->cs = to;
1777 return cRasterABGRtoARGB(surface);
1778 }
1779 if (((from == ColorSpace::ARGB8888) || (from == ColorSpace::ARGB8888S)) && ((to == ColorSpace::ABGR8888) || (to == ColorSpace::ABGR8888S))) {
1780 surface->cs = to;
1781 return cRasterARGBtoABGR(surface);
1782 }
1783 return false;
1784 }
1785
1786
1787 //TODO: SIMD OPTIMIZATION?
1788 void rasterXYFlip(uint32_t* src, uint32_t* dst, int32_t stride, int32_t w, int32_t h, const SwBBox& bbox, bool flipped)
1789 {
1790 constexpr int32_t BLOCK = 8; //experimental decision
1791
1792 if (flipped) {
1793 src += ((bbox.min.x * stride) + bbox.min.y);
1794 dst += ((bbox.min.y * stride) + bbox.min.x);
1795 } else {
1796 src += ((bbox.min.y * stride) + bbox.min.x);
1797 dst += ((bbox.min.x * stride) + bbox.min.y);
1798 }
1799
1800 #pragma omp parallel for
1801 for (int32_t x = 0; x < w; x += BLOCK) {
1802 auto bx = std::min(w, x + BLOCK) - x;
1803 auto in = &src[x];
1804 auto out = &dst[x * stride];
1805 for (int32_t y = 0; y < h; y += BLOCK) {
1806 auto p = &in[y * stride];
1807 auto q = &out[y];
1808 auto by = std::min(h, y + BLOCK) - y;
1809 for (int32_t xx = 0; xx < bx; ++xx) {
1810 for (int32_t yy = 0; yy < by; ++yy) {
1811 *q = *p;
1812 p += stride;
1813 ++q;
1814 }
1815 p += 1 - by * stride;
1816 q += stride - by;
1817 }
1818 }
1819 }
1820 }