epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame^] | 8 | #include "SkAutoMalloc.h" |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 9 | #include "SkColor.h" |
| 10 | #include "SkColorFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 12 | #include "SkLumaColorFilter.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame^] | 13 | #include "SkRandom.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
| 15 | #include "SkWriteBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 16 | #include "SkRandom.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 17 | #include "Test.h" |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 18 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 19 | static sk_sp<SkColorFilter> reincarnate_colorfilter(SkFlattenable* obj) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 20 | SkBinaryWriteBuffer wb; |
djsollen@google.com | cefc865 | 2012-03-26 15:52:10 +0000 | [diff] [blame] | 21 | wb.writeFlattenable(obj); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 23 | size_t size = wb.bytesWritten(); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 24 | SkAutoSMalloc<1024> storage(size); |
| 25 | // make a copy into storage |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 26 | wb.writeToMemory(storage.get()); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 28 | SkReadBuffer rb(storage.get(), size); |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 29 | return rb.readColorFilter(); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 34 | static sk_sp<SkColorFilter> make_filter() { |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 35 | // pick a filter that cannot compose with itself via newComposed() |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 36 | return SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kColorBurn); |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static void test_composecolorfilter_limit(skiatest::Reporter* reporter) { |
| 40 | // Test that CreateComposeFilter() has some finite limit (i.e. that the factory can return null) |
| 41 | const int way_too_many = 100; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 42 | auto parent(make_filter()); |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 43 | for (int i = 2; i < way_too_many; ++i) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 44 | auto filter(make_filter()); |
| 45 | parent = SkColorFilter::MakeComposeFilter(parent, filter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | if (nullptr == parent) { |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, i > 2); // we need to have succeeded at least once! |
| 48 | return; |
| 49 | } |
| 50 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, false); // we never saw a nullptr :( |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 54 | #define ILLEGAL_MODE ((SkBlendMode)-1) |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 55 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 56 | DEF_TEST(ColorFilter, reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 57 | SkRandom rand; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 58 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 59 | for (int mode = 0; mode <= (int)SkBlendMode::kLastMode; mode++) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 60 | SkColor color = rand.nextU(); |
| 61 | |
| 62 | // ensure we always get a filter, by avoiding the possibility of a |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | // special case that would return nullptr (if color's alpha is 0 or 0xFF) |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 64 | color = SkColorSetA(color, 0x7F); |
| 65 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 66 | auto cf = SkColorFilter::MakeModeFilter(color, (SkBlendMode)mode); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 67 | |
| 68 | // allow for no filter if we're in Dst mode (its a no op) |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 69 | if (SkBlendMode::kDst == (SkBlendMode)mode && nullptr == cf) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 70 | continue; |
| 71 | } |
| 72 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, cf); |
| 74 | |
| 75 | SkColor c = ~color; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 76 | SkBlendMode m = ILLEGAL_MODE; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 77 | |
| 78 | SkColor expectedColor = color; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 79 | SkBlendMode expectedMode = (SkBlendMode)mode; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 80 | |
| 81 | // SkDebugf("--- mc [%d %x] ", mode, color); |
| 82 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, cf->asColorMode(&c, (SkBlendMode*)&m)); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 84 | // handle special-case folding by the factory |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 85 | if (SkBlendMode::kClear == (SkBlendMode)mode) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 86 | if (c != expectedColor) { |
| 87 | expectedColor = 0; |
| 88 | } |
| 89 | if (m != expectedMode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 90 | expectedMode = SkBlendMode::kSrc; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 91 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 92 | } |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 93 | |
| 94 | // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, expectedColor); |
| 95 | |
| 96 | REPORTER_ASSERT(reporter, c == expectedColor); |
| 97 | REPORTER_ASSERT(reporter, m == expectedMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 99 | { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 100 | auto cf2 = reincarnate_colorfilter(cf.get()); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, cf2); |
| 102 | |
| 103 | SkColor c2 = ~color; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 104 | SkBlendMode m2 = ILLEGAL_MODE; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, (SkBlendMode*)&m2)); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, c2 == expectedColor); |
| 107 | REPORTER_ASSERT(reporter, m2 == expectedMode); |
| 108 | } |
| 109 | } |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 110 | |
| 111 | test_composecolorfilter_limit(reporter); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 116 | DEF_TEST(LumaColorFilter, reporter) { |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 117 | SkPMColor in, out; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 118 | auto lf(SkLumaColorFilter::Make()); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 119 | |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 120 | // Applying luma to white produces black with the same transparency. |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 121 | for (unsigned i = 0; i < 256; ++i) { |
| 122 | in = SkPackARGB32(i, i, i, i); |
| 123 | lf->filterSpan(&in, 1, &out); |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, SkGetPackedA32(out) == i); |
| 125 | REPORTER_ASSERT(reporter, SkGetPackedR32(out) == 0); |
| 126 | REPORTER_ASSERT(reporter, SkGetPackedG32(out) == 0); |
| 127 | REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Applying luma to black yields transparent black (luminance(black) == 0) |
| 131 | for (unsigned i = 0; i < 256; ++i) { |
| 132 | in = SkPackARGB32(i, 0, 0, 0); |
| 133 | lf->filterSpan(&in, 1, &out); |
| 134 | REPORTER_ASSERT(reporter, out == SK_ColorTRANSPARENT); |
| 135 | } |
| 136 | |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 137 | // For general colors, a luma filter generates black with an attenuated alpha channel. |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 138 | for (unsigned i = 1; i < 256; ++i) { |
| 139 | in = SkPackARGB32(i, i, i / 2, i / 3); |
| 140 | lf->filterSpan(&in, 1, &out); |
| 141 | REPORTER_ASSERT(reporter, out != in); |
| 142 | REPORTER_ASSERT(reporter, SkGetPackedA32(out) <= i); |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, SkGetPackedR32(out) == 0); |
| 144 | REPORTER_ASSERT(reporter, SkGetPackedG32(out) == 0); |
| 145 | REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | #include "SkColorMatrixFilter.h" |
| 152 | |
| 153 | static void get_brightness_matrix(float amount, float matrix[20]) { |
| 154 | // Spec implementation |
| 155 | // (http://dvcs.w3.org/hg/FXTF/raw-file/tip/filters/index.html#brightnessEquivalent) |
| 156 | // <feFunc[R|G|B] type="linear" slope="[amount]"> |
| 157 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 158 | matrix[0] = matrix[6] = matrix[12] = amount; |
| 159 | matrix[18] = 1.f; |
| 160 | } |
| 161 | |
| 162 | static void get_grayscale_matrix(float amount, float matrix[20]) { |
| 163 | // Note, these values are computed to ensure MatrixNeedsClamping is false |
| 164 | // for amount in [0..1] |
| 165 | matrix[0] = 0.2126f + 0.7874f * amount; |
| 166 | matrix[1] = 0.7152f - 0.7152f * amount; |
| 167 | matrix[2] = 1.f - (matrix[0] + matrix[1]); |
| 168 | matrix[3] = matrix[4] = 0.f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 169 | |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 170 | matrix[5] = 0.2126f - 0.2126f * amount; |
| 171 | matrix[6] = 0.7152f + 0.2848f * amount; |
| 172 | matrix[7] = 1.f - (matrix[5] + matrix[6]); |
| 173 | matrix[8] = matrix[9] = 0.f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 174 | |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 175 | matrix[10] = 0.2126f - 0.2126f * amount; |
| 176 | matrix[11] = 0.7152f - 0.7152f * amount; |
| 177 | matrix[12] = 1.f - (matrix[10] + matrix[11]); |
| 178 | matrix[13] = matrix[14] = 0.f; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 179 | |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 180 | matrix[15] = matrix[16] = matrix[17] = matrix[19] = 0.f; |
| 181 | matrix[18] = 1.f; |
| 182 | } |
| 183 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 184 | static sk_sp<SkColorFilter> make_cf0() { |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 185 | SkScalar matrix[20]; |
| 186 | get_brightness_matrix(0.5f, matrix); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 187 | return SkColorFilter::MakeMatrixFilterRowMajor255(matrix); |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 188 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 189 | static sk_sp<SkColorFilter> make_cf1() { |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 190 | SkScalar matrix[20]; |
| 191 | get_grayscale_matrix(1, matrix); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 192 | return SkColorFilter::MakeMatrixFilterRowMajor255(matrix); |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 193 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 194 | static sk_sp<SkColorFilter> make_cf2() { |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 195 | SkColorMatrix m0, m1; |
| 196 | get_brightness_matrix(0.5f, m0.fMat); |
| 197 | get_grayscale_matrix(1, m1.fMat); |
| 198 | m0.preConcat(m1); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 199 | return SkColorFilter::MakeMatrixFilterRowMajor255(m0.fMat); |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 200 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 201 | static sk_sp<SkColorFilter> make_cf3() { |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 202 | SkColorMatrix m0, m1; |
| 203 | get_brightness_matrix(0.5f, m0.fMat); |
| 204 | get_grayscale_matrix(1, m1.fMat); |
| 205 | m0.postConcat(m1); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 206 | return SkColorFilter::MakeMatrixFilterRowMajor255(m0.fMat); |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 207 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 208 | typedef sk_sp<SkColorFilter> (*CFProc)(); |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 209 | |
| 210 | // Test that a colormatrix that "should" preserve opaquness actually does. |
| 211 | DEF_TEST(ColorMatrixFilter, reporter) { |
| 212 | const CFProc procs[] = { |
| 213 | make_cf0, make_cf1, make_cf2, make_cf3, |
| 214 | }; |
| 215 | |
| 216 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 217 | auto cf(procs[i]()); |
reed | 0b18c35 | 2015-04-09 08:27:27 -0700 | [diff] [blame] | 218 | |
| 219 | // generate all possible r,g,b triples |
| 220 | for (int r = 0; r < 256; ++r) { |
| 221 | for (int g = 0; g < 256; ++g) { |
| 222 | SkPMColor storage[256]; |
| 223 | for (int b = 0; b < 256; ++b) { |
| 224 | storage[b] = SkPackARGB32(0xFF, r, g, b); |
| 225 | } |
| 226 | cf->filterSpan(storage, 256, storage); |
| 227 | for (int b = 0; b < 256; ++b) { |
| 228 | REPORTER_ASSERT(reporter, 0xFF == SkGetPackedA32(storage[b])); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |