17f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org#include "Test.h"
27f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org#include "SkColor.h"
37f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
47f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org#define ASSERT(x) REPORTER_ASSERT(r, x)
57f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
640ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.orgstatic uint8_t double_to_u8(double d) {
740ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    SkASSERT(d >= 0);
840ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    SkASSERT(d < 256);
940ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    return uint8_t(d);
1040ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org}
1140ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org
127f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// All algorithms we're testing have this interface.
137f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We want a single channel blend, src over dst, assuming src is premultiplied by srcAlpha.
147f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgtypedef uint8_t(*Blend)(uint8_t dst, uint8_t src, uint8_t srcAlpha);
157f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
167f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// This is our golden algorithm.
177f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_double_round(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
187f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    SkASSERT(src <= srcAlpha);
1940ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    return double_to_u8(0.5 + src + dst * (255.0 - srcAlpha) / 255.0);
207f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
217f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
227f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t abs_diff(uint8_t a, uint8_t b) {
237f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const int diff = a - b;
247f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return diff > 0 ? diff : -diff;
257f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
267f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
277f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic void test(skiatest::Reporter* r, int maxDiff, Blend algorithm,
287f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org                 uint8_t dst, uint8_t src, uint8_t alpha) {
297f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t golden = blend_double_round(dst, src, alpha);
307f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t  blend =          algorithm(dst, src, alpha);
317f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    if (abs_diff(blend, golden) > maxDiff) {
327f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        SkDebugf("dst %02x, src %02x, alpha %02x, |%02x - %02x| > %d\n",
337f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org                 dst, src, alpha, blend, golden, maxDiff);
347f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        ASSERT(abs_diff(blend, golden) <= maxDiff);
357f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    }
367f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
377f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
387f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// Exhaustively compare an algorithm against our golden, for a given alpha.
397f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic void test_alpha(skiatest::Reporter* r, uint8_t alpha, int maxDiff, Blend algorithm) {
407f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    SkASSERT(maxDiff >= 0);
417f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
427f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    for (unsigned src = 0; src <= alpha; src++) {
437f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        for (unsigned dst = 0; dst < 256; dst++) {
447f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org            test(r, maxDiff, algorithm, dst, src, alpha);
457f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        }
467f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    }
477f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
487f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
497f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// Exhaustively compare an algorithm against our golden, for a given dst.
507f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic void test_dst(skiatest::Reporter* r, uint8_t dst, int maxDiff, Blend algorithm) {
517f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    SkASSERT(maxDiff >= 0);
527f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
537f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    for (unsigned alpha = 0; alpha < 256; alpha++) {
547f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        for (unsigned src = 0; src <= alpha; src++) {
557f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org            test(r, maxDiff, algorithm, dst, src, alpha);
567f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        }
577f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    }
587f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
597f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
607f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_double_trunc(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
6140ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    return double_to_u8(src + dst * (255.0 - srcAlpha) / 255.0);
627f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
637f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
647f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_float_trunc(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
6540ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    return double_to_u8(src + dst * (255.0f - srcAlpha) / 255.0f);
667f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
677f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
687f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_float_round(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
6940ac5e52b8d08e85c269c93076fbd4208650d17fcommit-bot@chromium.org    return double_to_u8(0.5f + src + dst * (255.0f - srcAlpha) / 255.0f);
707f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
717f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
727f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_255_trunc(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
737f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = 255 - srcAlpha;
747f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha;
757f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
767f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
777f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
787f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_255_round(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
797f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = 255 - srcAlpha;
807f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha + 128;
817f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
827f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
837f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
847f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_256_trunc(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
857f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = 256 - (srcAlpha + (srcAlpha >> 7));
867f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha;
877f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
887f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
897f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
907f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_256_round(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
917f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = 256 - (srcAlpha + (srcAlpha >> 7));
927f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha + 128;
937f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
947f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
957f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
967f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_256_round_alt(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
977f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t invAlpha8 = 255 - srcAlpha;
987f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = invAlpha8 + (invAlpha8 >> 7);
997f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha + 128;
1007f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
1017f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1027f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1037f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_256_plus1_trunc(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
1047f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = 256 - (srcAlpha + 1);
1057f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha;
1067f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
1077f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1087f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1097f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_256_plus1_round(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
1107f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t invAlpha = 256 - (srcAlpha + 1);
1117f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha + 128;
1127f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + (product >> 8);
1137f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1147f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1157f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgstatic uint8_t blend_perfect(uint8_t dst, uint8_t src, uint8_t srcAlpha) {
1167f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t invAlpha = 255 - srcAlpha;
1177f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint16_t product = dst * invAlpha + 128;
1187f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    return src + ((product + (product >> 8)) >> 8);
1197f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1207f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1217f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1227f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We want 0 diff whenever src is fully transparent.
1237f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgDEF_TEST(Blend_alpha_0x00, r) {
1247f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t alpha = 0x00;
1257f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1267f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // GOOD
1277f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_round);
1287f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_round_alt);
1297f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_trunc);
1307f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_double_trunc);
1317f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_float_round);
1327f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_float_trunc);
1337f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_perfect);
1347f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1357f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // BAD
1367f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 1, blend_255_round);
1377f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 1, blend_255_trunc);
1387f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 1, blend_256_plus1_round);
1397f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 1, blend_256_plus1_trunc);
1407f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1417f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1427f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We want 0 diff whenever dst is 0.
1437f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgDEF_TEST(Blend_dst_0x00, r) {
1447f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t dst = 0x00;
1457f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1467f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // GOOD
1477f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_255_round);
1487f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_255_trunc);
1497f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_plus1_round);
1507f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_plus1_trunc);
1517f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_round);
1527f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_round_alt);
1537f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_trunc);
1547f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_double_trunc);
1557f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_float_round);
1567f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_float_trunc);
1577f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_perfect);
1587f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1597f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // BAD
1607f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1617f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1627f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We want 0 diff whenever src is fully opaque.
1637f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgDEF_TEST(Blend_alpha_0xFF, r) {
1647f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t alpha = 0xFF;
1657f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1667f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // GOOD
1677f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_255_round);
1687f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_255_trunc);
1697f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_plus1_round);
1707f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_plus1_trunc);
1717f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_round);
1727f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_round_alt);
1737f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_256_trunc);
1747f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_double_trunc);
1757f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_float_round);
1767f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_float_trunc);
1777f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_alpha(r, alpha, 0, blend_perfect);
1787f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1797f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // BAD
1807f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
1817f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1827f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We want 0 diff whenever dst is 0xFF.
1837f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgDEF_TEST(Blend_dst_0xFF, r) {
1847f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    const uint8_t dst = 0xFF;
1857f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1867f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // GOOD
1877f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_round);
1887f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_256_round_alt);
1897f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_double_trunc);
1907f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_float_round);
1917f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_float_trunc);
1927f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 0, blend_perfect);
1937f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
1947f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    // BAD
1957f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 1, blend_255_round);
1967f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 1, blend_255_trunc);
1977f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 1, blend_256_plus1_round);
1987f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 1, blend_256_plus1_trunc);
1997f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    test_dst(r, dst, 1, blend_256_trunc);
2007f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
2017f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
2027f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We'd like diff <= 1 everywhere.
2037f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgDEF_TEST(Blend_alpha_Exhaustive, r) {
2047f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    for (unsigned alpha = 0; alpha < 256; alpha++) {
2057f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        // PERFECT
2067f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 0, blend_float_round);
2077f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 0, blend_perfect);
2087f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
2097f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        // GOOD
2107f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_255_round);
2117f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_256_plus1_round);
2127f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_256_round);
2137f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_256_round_alt);
2147f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_256_trunc);
2157f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_double_trunc);
2167f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 1, blend_float_trunc);
2177f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
2187f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        // BAD
2197f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 2, blend_255_trunc);
2207f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_alpha(r, alpha, 2, blend_256_plus1_trunc);
2217f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    }
2227f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
2237f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
2247f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// We'd like diff <= 1 everywhere.
2257f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.orgDEF_TEST(Blend_dst_Exhaustive, r) {
2267f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    for (unsigned dst = 0; dst < 256; dst++) {
2277f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        // PERFECT
2287f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 0, blend_float_round);
2297f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 0, blend_perfect);
2307f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
2317f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        // GOOD
2327f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_255_round);
2337f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_256_plus1_round);
2347f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_256_round);
2357f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_256_round_alt);
2367f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_256_trunc);
2377f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_double_trunc);
2387f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 1, blend_float_trunc);
2397f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org
2407f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        // BAD
2417f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 2, blend_255_trunc);
2427f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org        test_dst(r, dst, 2, blend_256_plus1_trunc);
2437f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org    }
2447f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org}
2457f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// Overall summary:
2467f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// PERFECT
2477f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_double_round
2487f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_float_round
2497f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_perfect
2507f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// GOOD ENOUGH
2517f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_double_trunc
2527f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_float_trunc
2537f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_256_round
2547f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  blend_256_round_alt
2557f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org// NOT GOOD ENOUGH
2567f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  all others
2577f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//
2587f428a941b47c88d84da7adb656924245a55f050commit-bot@chromium.org//  Algorithms that make sense to use in Skia: blend_256_round, blend_256_round_alt, blend_perfect
259