blob: ca9e46efd305f0e572bb960232594ad25b7dc1b6 [file] [log] [blame]
mtklein767d2732015-07-16 07:01:39 -07001/*
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
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +00008#include "Test.h"
9#include "SkColor.h"
mtklein767d2732015-07-16 07:01:39 -070010#include "SkColorPriv.h"
11#include "SkTaskGroup.h"
12#include "SkXfermode.h"
mtklein195fe082015-11-17 08:39:01 -080013#include <functional>
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000014
mtklein195fe082015-11-17 08:39:01 -080015struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000016
mtklein195fe082015-11-17 08:39:01 -080017static 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
commit-bot@chromium.org40ac5e52014-02-26 21:40:07 +000025}
26
mtklein195fe082015-11-17 08:39:01 -080027template <typename Fn>
28static 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++; }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000039 }
mtklein195fe082015-11-17 08:39:01 -080040 }}
41 return r;
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000042}
43
mtklein195fe082015-11-17 08:39:01 -080044DEF_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))); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000052
mtklein195fe082015-11-17 08:39:01 -080053 // 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); }
commit-bot@chromium.org7f428a92014-02-25 21:31:03 +000069}
70
mtklein767d2732015-07-16 07:01:39 -070071DEF_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 }
reedcfb6bdf2016-03-29 11:32:50 -070083 auto xfermode(SkXfermode::Make(mode));
mtklein767d2732015-07-16 07:01:39 -070084 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.
mtklein279c7862016-01-04 19:13:19 -0800101 SkTaskGroup().batch(SkXfermode::kLastMode, test_mode);
mtklein767d2732015-07-16 07:01:39 -0700102}