gdkcairo: use the SIMD kernels for pixbuf conversion

This commit is contained in:
Daemonratte 2026-10-16 13:46:48 +02:00
commit 4455848b3b
6 changed files with 143 additions and 2 deletions

View file

@ -18,6 +18,7 @@
*/
#include "gdkcairo.h"
#include "gdkrgbsimd.h"
#include "gdkdrawable.h"
#include "gdkinternals.h"
#include "gdkregion-generic.h"
@ -202,6 +203,7 @@ gdk_cairo_set_source_pixbuf (cairo_t *cr,
cairo_format_t format;
cairo_surface_t *surface;
static const cairo_user_data_key_t key;
const GdkRgbSimd *simd = _gdk_rgb_simd_get ();
cairo_status_t status;
int j;
@ -228,10 +230,19 @@ gdk_cairo_set_source_pixbuf (cairo_t *cr,
{
guchar *p = gdk_pixels;
guchar *q = cairo_pixels;
gint done = 0;
/* vector code where there is some, C for the rest of the row */
if (n_channels == 3 && simd->rgb_to_x888)
done = simd->rgb_to_x888 ((guint32 *) q, p, width);
else if (n_channels == 4 && simd->rgba_to_argb)
done = simd->rgba_to_argb ((guint32 *) q, p, width);
p += done * n_channels;
q += done * 4;
if (n_channels == 3)
{
guchar *end = p + 3 * width;
guchar *end = gdk_pixels + 3 * width;
while (p < end)
{
@ -250,7 +261,7 @@ gdk_cairo_set_source_pixbuf (cairo_t *cr,
}
else
{
guchar *end = p + 4 * width;
guchar *end = gdk_pixels + 4 * width;
guint t1,t2,t3;
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END

View file

@ -305,6 +305,40 @@ composite_565 (guint16 *pixels, const guchar *rgba, gint n)
return i;
}
static gint
rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
{
const vu8 argb = VU8 (3, 0, 1, 2, 7, 4, 5, 6, 11, 8, 9, 10, 15, 12, 13, 14);
/* a for R, G and B; 255 for the alpha, which a * 255 / 255 keeps */
const vu8 factors = VU8 (16, 3, 3, 3, 16, 7, 7, 7, 16, 11, 11, 11, 16, 15, 15, 15);
const vu8 merge = VU8 (1, 17, 3, 19, 5, 21, 7, 23, 9, 25, 11, 27, 13, 29, 15, 31);
const vu8 ones = vec_splat_u8 (-1);
Reader r;
gint i;
for (i = 0; i < n && !aligned (out + i); i++, rgba += 4)
{
guint a = rgba[3];
out[i] = (a << 24) | (blend8 (a, rgba[0], 0) << 16) |
(blend8 (a, rgba[1], 0) << 8) | blend8 (a, rgba[2], 0);
}
if (i + 4 <= n)
reader_init (&r, rgba);
for (; i + 4 <= n; i += 4)
{
vu8 s = reader_next (&r);
vu8 m = vec_perm (s, ones, factors);
s = vec_perm (s, s, argb);
vec_st (vec_perm ((vu8) blend (vec_mule (s, m)), (vu8) blend (vec_mulo (s, m)), merge),
0, (guchar *) (out + i));
}
return i;
}
void
_gdk_rgb_simd_init_altivec (GdkRgbSimd *simd)
{
@ -314,6 +348,7 @@ _gdk_rgb_simd_init_altivec (GdkRgbSimd *simd)
simd->rgb_to_565 = rgb_to_565;
simd->composite_x888 = composite_x888;
simd->composite_565 = composite_565;
simd->rgba_to_argb = rgba_to_argb;
}
#else

View file

@ -210,6 +210,39 @@ composite_565 (guint16 *pixels, const guchar *rgba, gint n)
return i;
}
/* c * a / 255 to the nearest */
static gint
rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
{
const __m64 zero = _mm_setzero_si64 ();
gint i;
for (i = 0; i + 2 <= n; i += 2, rgba += 8)
{
__m64 s = load8 (rgba);
__m64 bgra, lo, hi, t;
/* RGBA to BGRA, the bytes of 0xAARRGGBB */
bgra = _mm_or_si64 (_mm_and_si64 (s, SPLAT (0xff00ff00)),
_mm_or_si64 (_mm_slli_pi32 (_mm_and_si64 (s, SPLAT (0xff)), 16),
_mm_and_si64 (_mm_srli_pi32 (s, 16), SPLAT (0xff))));
lo = _mm_unpacklo_pi8 (bgra, zero);
hi = _mm_unpackhi_pi8 (bgra, zero);
t = _mm_add_pi16 (_mm_mullo_pi16 (lo, alpha_of (lo)), _mm_set1_pi16 (0x80));
lo = _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
t = _mm_add_pi16 (_mm_mullo_pi16 (hi, alpha_of (hi)), _mm_set1_pi16 (0x80));
hi = _mm_srli_pi16 (_mm_add_pi16 (t, _mm_srli_pi16 (t, 8)), 8);
/* the alpha byte as it was */
store8 (out + i, _mm_or_si64 (_mm_and_si64 (_mm_packs_pu16 (lo, hi), SPLAT (0x00ffffff)),
_mm_and_si64 (s, SPLAT (0xff000000))));
}
_mm_empty ();
return i;
}
void
_gdk_rgb_simd_init_mmx (GdkRgbSimd *simd)
{
@ -219,6 +252,7 @@ _gdk_rgb_simd_init_mmx (GdkRgbSimd *simd)
simd->rgb_to_565 = rgb_to_565;
simd->composite_x888 = composite_x888;
simd->composite_565 = composite_565;
simd->rgba_to_argb = rgba_to_argb;
}
#else

View file

@ -142,6 +142,31 @@ composite_565 (guint16 *pixels, const guchar *rgba, gint n)
return i;
}
/* c * a / 255 to the nearest */
static gint
rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
{
gint i;
for (i = 0; i + 8 <= n; i += 8, rgba += 32)
{
uint8x8x4_t s = vld4_u8 (rgba);
uint8x8x4_t v;
uint16x8_t t;
t = vmull_u8 (s.val[2], s.val[3]);
v.val[0] = vrshrn_n_u16 (vrsraq_n_u16 (t, t, 8), 8);
t = vmull_u8 (s.val[1], s.val[3]);
v.val[1] = vrshrn_n_u16 (vrsraq_n_u16 (t, t, 8), 8);
t = vmull_u8 (s.val[0], s.val[3]);
v.val[2] = vrshrn_n_u16 (vrsraq_n_u16 (t, t, 8), 8);
v.val[3] = s.val[3];
vst4_u8 ((guchar *) (out + i), v);
}
return i;
}
void
_gdk_rgb_simd_init_neon (GdkRgbSimd *simd)
{
@ -151,6 +176,7 @@ _gdk_rgb_simd_init_neon (GdkRgbSimd *simd)
simd->rgb_to_565 = rgb_to_565;
simd->composite_x888 = composite_x888;
simd->composite_565 = composite_565;
simd->rgba_to_argb = rgba_to_argb;
}
#else

View file

@ -187,6 +187,37 @@ composite_565 (guint16 *pixels, const guchar *rgba, gint n)
return i;
}
/* c * a / 255 to the nearest, which (c * a + 0x80) * 257 >> 16 is */
static gint
rgba_to_argb (guint32 *out, const guchar *rgba, gint n)
{
const __m128i zero = _mm_setzero_si128 ();
gint i;
for (i = 0; i + 4 <= n; i += 4, rgba += 16)
{
__m128i s = _mm_loadu_si128 ((const __m128i *) rgba);
__m128i lo = _mm_unpacklo_epi8 (s, zero);
__m128i hi = _mm_unpackhi_epi8 (s, zero);
lo = _mm_mulhi_epu16 (_mm_add_epi16 (_mm_mullo_epi16 (lo, alpha_of (lo)), _mm_set1_epi16 (0x80)),
_mm_set1_epi16 (257));
hi = _mm_mulhi_epu16 (_mm_add_epi16 (_mm_mullo_epi16 (hi, alpha_of (hi)), _mm_set1_epi16 (0x80)),
_mm_set1_epi16 (257));
/* R, G, B to B, G, R: the bytes of 0xAARRGGBB */
lo = _mm_shufflehi_epi16 (_mm_shufflelo_epi16 (lo, 0xc6), 0xc6);
hi = _mm_shufflehi_epi16 (_mm_shufflelo_epi16 (hi, 0xc6), 0xc6);
/* the alpha byte as it was */
_mm_storeu_si128 ((__m128i *) (out + i),
_mm_or_si128 (_mm_and_si128 (_mm_packus_epi16 (lo, hi), SPLAT (0x00ffffff)),
_mm_and_si128 (s, SPLAT (0xff000000))));
}
return i;
}
void
_gdk_rgb_simd_init_sse2 (GdkRgbSimd *simd)
{
@ -196,6 +227,7 @@ _gdk_rgb_simd_init_sse2 (GdkRgbSimd *simd)
simd->rgb_to_565 = rgb_to_565;
simd->composite_x888 = composite_x888;
simd->composite_565 = composite_565;
simd->rgba_to_argb = rgba_to_argb;
}
#else

View file

@ -15,6 +15,8 @@
*
* x888 0xffRRGGBB, 32 bits
* 565 RRRRRGGG GGGBBBBB, 16 bits
* argb 0xAARRGGBB, 32 bits, R, G and B multiplied by A / 255,
* rounded to the nearest: cairo's ARGB32
*
* RGB and RGBA are bytes in that order; RGBA is not premultiplied.
* Blending rounds as GdkRGB's C code does, so the results are the same.
@ -39,6 +41,7 @@ struct _GdkRgbSimd
gint (*rgb_to_565) (guint16 *out, const guchar *rgb, gint n);
gint (*composite_x888) (guint32 *pixels, const guchar *rgba, gint n);
gint (*composite_565) (guint16 *pixels, const guchar *rgba, gint n);
gint (*rgba_to_argb) (guint32 *out, const guchar *rgba, gint n);
};
/* Each fills in what its instruction set does better than C; they are