mtklein | 767d273 | 2015-07-16 07:01:39 -0700 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkColor.h" |
mtklein | 767d273 | 2015-07-16 07:01:39 -0700 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| 11 | #include "SkTaskGroup.h" |
| 12 | #include "SkXfermode.h" |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 13 | #include <functional> |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 14 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 15 | struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; }; |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 16 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 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 |
commit-bot@chromium.org | 40ac5e5 | 2014-02-26 21:40:07 +0000 | [diff] [blame] | 25 | } |
| 26 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 27 | template <typename Fn> |
| 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++; } |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 39 | } |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 40 | }} |
| 41 | return r; |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 42 | } |
| 43 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 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))); } |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 52 | |
mtklein | 195fe08 | 2015-11-17 08:39:01 -0800 | [diff] [blame] | 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); } |
commit-bot@chromium.org | 7f428a9 | 2014-02-25 21:31:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
mtklein | 767d273 | 2015-07-16 07:01:39 -0700 | [diff] [blame] | 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 | } |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 83 | auto xfermode(SkXfermode::Make(mode)); |
mtklein | 767d273 | 2015-07-16 07:01:39 -0700 | [diff] [blame] | 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. |
mtklein | 279c786 | 2016-01-04 19:13:19 -0800 | [diff] [blame] | 101 | SkTaskGroup().batch(SkXfermode::kLastMode, test_mode); |
mtklein | 767d273 | 2015-07-16 07:01:39 -0700 | [diff] [blame] | 102 | } |