LibGfx: SIMD optimized alpha blending

This commit is contained in:
Sahan Fernando 2020-12-08 12:44:24 +11:00 committed by Andreas Kling
parent 893adbb79c
commit fe3963f3d4
Notes: sideshowbarker 2024-07-19 00:59:36 +09:00

View file

@ -29,6 +29,7 @@
#include <AK/Assertions.h>
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/SIMD.h>
#include <AK/StdLibExtras.h>
#include <LibIPC/Forward.h>
@ -131,12 +132,31 @@ public:
if (!source.alpha())
return *this;
#ifdef __SSE__
using AK::SIMD::i32x4;
const i32x4 color = {
red(),
green(),
blue()
};
const i32x4 source_color = {
source.red(),
source.green(),
source.blue()
};
const int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
const i32x4 out = (color * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source_color) / d;
return Color(out[0], out[1], out[2], d / 255);
#else
int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
u8 r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
u8 g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
u8 b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
u8 a = d / 255;
return Color(r, g, b, a);
#endif
}
Color to_grayscale() const