1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "Test.h"
9 #include "SkColor.h"
10 #include "SkColorPriv.h"
11 #include "SkTaskGroup.h"
12 #include "SkXfermode.h"
13 #include <functional>
14
15 struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
16
acceptable(const Results & r)17 static bool acceptable(const Results& r) {
18 #if 0
19 SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
20 r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
21 #endif
22 return r.diffs_by_1 == r.diffs // never off by more than 1
23 && r.diffs_0x00 == 0 // transparent must stay transparent
24 && r.diffs_0xff == 0; // opaque must stay opaque
25 }
26
27 template <typename Fn>
test(Fn && multiply)28 static Results test(Fn&& multiply) {
29 Results r = { 0,0,0,0 };
30 for (int x = 0; x < 256; x++) {
31 for (int y = 0; y < 256; y++) {
32 int p = multiply(x, y),
33 ideal = (x*y+127)/255;
34 if (p != ideal) {
35 r.diffs++;
36 if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
37 if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
38 if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
39 }
40 }}
41 return r;
42 }
43
DEF_TEST(Blend_byte_multiply,r)44 DEF_TEST(Blend_byte_multiply, r) {
45 // These are all temptingly close but fundamentally broken.
46 int (*broken[])(int, int) = {
47 [](int x, int y) { return (x*y)>>8; },
48 [](int x, int y) { return (x*y+128)>>8; },
49 [](int x, int y) { y += y>>7; return (x*y)>>8; },
50 };
51 for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
52
53 // These are fine to use, but not perfect.
54 int (*fine[])(int, int) = {
55 [](int x, int y) { return (x*y+x)>>8; },
56 [](int x, int y) { return (x*y+y)>>8; },
57 [](int x, int y) { return (x*y+255)>>8; },
58 [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
59 };
60 for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
61
62 // These are pefect.
63 int (*perfect[])(int, int) = {
64 [](int x, int y) { return (x*y+127)/255; }, // Duh.
65 [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
66 [](int x, int y) { return ((x*y+128)*257)>>16; },
67 };
68 for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
69 }
70
DEF_TEST(Blend_premul_begets_premul,r)71 DEF_TEST(Blend_premul_begets_premul, r) {
72 // This test is quite slow, even if you have enough cores to run each mode in parallel.
73 if (!r->allowExtendedTest()) {
74 return;
75 }
76
77 // No matter what xfermode we use, premul inputs should create premul outputs.
78 auto test_mode = [&](int m) {
79 SkXfermode::Mode mode = (SkXfermode::Mode)m;
80 if (mode == SkXfermode::kSrcOver_Mode) {
81 return; // TODO: can't create a SrcOver xfermode.
82 }
83 SkAutoTUnref<SkXfermode> xfermode(SkXfermode::Create(mode));
84 SkASSERT(xfermode);
85 // We'll test all alphas and legal color values, assuming all colors work the same.
86 // This is not true for non-separable blend modes, but this test still can't hurt.
87 for (int sa = 0; sa <= 255; sa++) {
88 for (int da = 0; da <= 255; da++) {
89 for (int s = 0; s <= sa; s++) {
90 for (int d = 0; d <= da; d++) {
91 SkPMColor src = SkPackARGB32(sa, s, s, s),
92 dst = SkPackARGB32(da, d, d, d);
93 xfermode->xfer32(&dst, &src, 1, nullptr); // To keep it simple, no AA.
94 if (!SkPMColorValid(dst)) {
95 ERRORF(r, "%08x is not premul using %s", dst, SkXfermode::ModeName(mode));
96 }
97 }}}}
98 };
99
100 // Parallelism helps speed things up on my desktop from ~725s to ~50s.
101 SkTaskGroup().batch(SkXfermode::kLastMode, test_mode);
102 }
103