2 * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
23 #ifdef THORVG_NEON_VECTOR_SUPPORT
27 static inline uint8x8_t ALPHA_BLEND(uint8x8_t c, uint8x8_t a)
29 uint16x8_t t = vmull_u8(c, a);
30 return vshrn_n_u16(t, 8);
34 static void neonRasterRGBA32(uint32_t *dst, uint32_t val, uint32_t offset, int32_t len)
36 uint32_t iterations = len / 4;
37 uint32_t neonFilled = iterations * 4;
40 uint32x4_t vectorVal = {val, val, val, val};
42 for (uint32_t i = 0; i < iterations; ++i) {
43 vst1q_u32(dst, vectorVal);
47 int32_t leftovers = len - neonFilled;
48 while (leftovers--) *dst++ = val;
52 static bool neonRasterTranslucentRle(SwSurface* surface, const SwRleData* rle, uint32_t color)
54 auto span = rle->spans;
56 uint8x8_t *vDst = nullptr;
59 for (uint32_t i = 0; i < rle->size; ++i) {
60 if (span->coverage < 255) src = ALPHA_BLEND(color, span->coverage);
63 auto dst = &surface->buffer[span->y * surface->stride + span->x];
64 auto ialpha = 255 - _alpha(src);
66 if ((((uint32_t) dst) & 0x7) != 0) {
67 //fill not aligned byte
68 *dst = src + ALPHA_BLEND(*dst, ialpha);
69 vDst = (uint8x8_t*)(dst + 1);
72 vDst = (uint8x8_t*) dst;
76 uint8x8_t vSrc = (uint8x8_t) vdup_n_u32(src);
77 uint8x8_t vIalpha = vdup_n_u8((uint8_t) ialpha);
79 for (uint32_t x = 0; x < (span->len - align) / 2; ++x)
80 vDst[x] = vadd_u8(vSrc, ALPHA_BLEND(vDst[x], vIalpha));
82 auto leftovers = (span->len - align) % 2;
83 if (leftovers > 0) dst[span->len - 1] = src + ALPHA_BLEND(dst[span->len - 1], ialpha);
91 static bool neonRasterTranslucentRect(SwSurface* surface, const SwBBox& region, uint32_t color)
93 auto buffer = surface->buffer + (region.min.y * surface->stride) + region.min.x;
94 auto h = static_cast<uint32_t>(region.max.y - region.min.y);
95 auto w = static_cast<uint32_t>(region.max.x - region.min.x);
96 auto ialpha = 255 - _alpha(color);
98 auto vColor = vdup_n_u32(color);
99 auto vIalpha = vdup_n_u8((uint8_t) ialpha);
101 uint8x8_t* vDst = nullptr;
104 for (uint32_t y = 0; y < h; ++y) {
105 auto dst = &buffer[y * surface->stride];
107 if ((((uint32_t) dst) & 0x7) != 0) {
108 //fill not aligned byte
109 *dst = color + ALPHA_BLEND(*dst, ialpha);
110 vDst = (uint8x8_t*) (dst + 1);
113 vDst = (uint8x8_t*) dst;
117 for (uint32_t x = 0; x < (w - align) / 2; ++x)
118 vDst[x] = vadd_u8((uint8x8_t)vColor, ALPHA_BLEND(vDst[x], vIalpha));
120 auto leftovers = (w - align) % 2;
121 if (leftovers > 0) dst[w - 1] = color + ALPHA_BLEND(dst[w - 1], ialpha);