2019-06-03 22:10:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tools/SkVMBuilders.h"
|
|
|
|
|
|
|
|
// Some parts of this builder code are written less fluently than possible,
|
|
|
|
// to avoid any ambiguity of function argument evaluation order. This lets
|
|
|
|
// our golden tests work portably. In general there's no reason to fear
|
|
|
|
// nesting calls to Builder routines.
|
|
|
|
|
|
|
|
SrcoverBuilder_F32::SrcoverBuilder_F32(Fmt srcFmt, Fmt dstFmt) {
|
2019-07-15 15:04:08 +00:00
|
|
|
auto load = [&](Fmt fmt, skvm::F32* r, skvm::F32* g, skvm::F32* b, skvm::F32* a) {
|
|
|
|
skvm::Arg ptr;
|
2019-06-03 22:10:59 +00:00
|
|
|
switch (fmt) {
|
|
|
|
case Fmt::A8: {
|
2019-07-30 14:44:30 +00:00
|
|
|
ptr = varying<uint8_t>();
|
2019-06-03 22:10:59 +00:00
|
|
|
*r = *g = *b = splat(0.0f);
|
2020-03-04 20:59:09 +00:00
|
|
|
*a = from_unorm(8, load8(ptr));
|
2019-06-03 22:10:59 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case Fmt::G8: {
|
2019-07-30 14:44:30 +00:00
|
|
|
ptr = varying<uint8_t>();
|
2020-03-04 20:59:09 +00:00
|
|
|
*r = *g = *b = from_unorm(8, load8(ptr));
|
2019-06-03 22:10:59 +00:00
|
|
|
*a = splat(1.0f);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Fmt::RGBA_8888: {
|
2019-07-30 14:44:30 +00:00
|
|
|
ptr = varying<int>();
|
2019-06-04 16:33:25 +00:00
|
|
|
skvm::I32 rgba = load32(ptr);
|
2020-03-04 20:59:09 +00:00
|
|
|
*r = from_unorm(8, extract(rgba, 0, splat(0xff)));
|
|
|
|
*g = from_unorm(8, extract(rgba, 8, splat(0xff)));
|
|
|
|
*b = from_unorm(8, extract(rgba, 16, splat(0xff)));
|
|
|
|
*a = from_unorm(8, extract(rgba, 24, splat(0xff)));
|
2019-06-03 22:10:59 +00:00
|
|
|
} break;
|
|
|
|
}
|
2019-07-15 15:04:08 +00:00
|
|
|
return ptr;
|
2019-06-03 22:10:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
skvm::F32 r,g,b,a;
|
2019-07-15 15:04:08 +00:00
|
|
|
(void)load(srcFmt, &r,&g,&b,&a);
|
2019-06-03 22:10:59 +00:00
|
|
|
|
|
|
|
skvm::F32 dr,dg,db,da;
|
2019-07-15 15:04:08 +00:00
|
|
|
skvm::Arg dst = load(dstFmt, &dr,&dg,&db,&da);
|
2019-06-03 22:10:59 +00:00
|
|
|
|
|
|
|
skvm::F32 invA = sub(splat(1.0f), a);
|
|
|
|
r = mad(dr, invA, r);
|
|
|
|
g = mad(dg, invA, g);
|
|
|
|
b = mad(db, invA, b);
|
|
|
|
a = mad(da, invA, a);
|
|
|
|
|
|
|
|
switch (dstFmt) {
|
|
|
|
case Fmt::A8: {
|
2020-03-04 20:59:09 +00:00
|
|
|
store8(dst, to_unorm(8, a));
|
2019-06-03 22:10:59 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case Fmt::G8: {
|
|
|
|
skvm::F32 _2126 = splat(0.2126f),
|
|
|
|
_7152 = splat(0.7152f),
|
|
|
|
_0722 = splat(0.0722f);
|
2020-03-04 20:59:09 +00:00
|
|
|
store8(dst, to_unorm(8, mad(r, _2126,
|
2019-06-03 22:10:59 +00:00
|
|
|
mad(g, _7152,
|
|
|
|
mul(b, _0722)))));
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Fmt::RGBA_8888: {
|
2020-03-04 20:59:09 +00:00
|
|
|
skvm::I32 R = to_unorm(8, r),
|
|
|
|
G = to_unorm(8, g),
|
|
|
|
B = to_unorm(8, b),
|
|
|
|
A = to_unorm(8, a);
|
2019-06-03 22:10:59 +00:00
|
|
|
|
2019-06-04 15:41:49 +00:00
|
|
|
R = pack(R, G, 8);
|
|
|
|
B = pack(B, A, 8);
|
|
|
|
R = pack(R, B, 16);
|
2019-06-03 22:10:59 +00:00
|
|
|
|
|
|
|
store32(dst, R);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:37:10 +00:00
|
|
|
SrcoverBuilder_I32_Naive::SrcoverBuilder_I32_Naive() {
|
2019-07-30 14:44:30 +00:00
|
|
|
skvm::Arg src = varying<int>(),
|
|
|
|
dst = varying<int>();
|
2019-06-20 16:37:10 +00:00
|
|
|
|
|
|
|
auto load = [&](skvm::Arg ptr,
|
|
|
|
skvm::I32* r, skvm::I32* g, skvm::I32* b, skvm::I32* a) {
|
|
|
|
skvm::I32 rgba = load32(ptr);
|
|
|
|
*r = extract(rgba, 0, splat(0xff));
|
|
|
|
*g = extract(rgba, 8, splat(0xff));
|
|
|
|
*b = extract(rgba, 16, splat(0xff));
|
|
|
|
*a = extract(rgba, 24, splat(0xff));
|
|
|
|
};
|
|
|
|
|
|
|
|
skvm::I32 r,g,b,a;
|
|
|
|
load(src, &r,&g,&b,&a);
|
|
|
|
|
|
|
|
skvm::I32 dr,dg,db,da;
|
|
|
|
load(dst, &dr,&dg,&db,&da);
|
|
|
|
|
|
|
|
// (xy + x)/256 is a good approximation of (xy + 127)/255
|
|
|
|
//
|
|
|
|
// == (d*(255-a) + d)/256
|
|
|
|
// == (d*(255-a+1) )/256
|
|
|
|
// == (d*(256-a ) )/256
|
|
|
|
|
|
|
|
skvm::I32 invA = sub(splat(256), a);
|
|
|
|
r = add(r, shr(mul(dr, invA), 8));
|
|
|
|
g = add(g, shr(mul(dg, invA), 8));
|
|
|
|
b = add(b, shr(mul(db, invA), 8));
|
|
|
|
a = add(a, shr(mul(da, invA), 8));
|
|
|
|
|
|
|
|
r = pack(r, g, 8);
|
|
|
|
b = pack(b, a, 8);
|
|
|
|
r = pack(r, b, 16);
|
|
|
|
store32(dst, r);
|
|
|
|
}
|