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 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 8 | #include "SkColor.h" |
| 9 | #include "SkColorFilter.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame^] | 10 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 11 | #include "SkLumaColorFilter.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 12 | #include "SkOrderedReadBuffer.h" |
| 13 | #include "SkOrderedWriteBuffer.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame^] | 14 | #include "SkRandom.h" |
| 15 | #include "SkXfermode.h" |
| 16 | #include "Test.h" |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 17 | |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 18 | static SkColorFilter* reincarnate_colorfilter(SkFlattenable* obj) { |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 19 | SkOrderedWriteBuffer wb; |
djsollen@google.com | cefc865 | 2012-03-26 15:52:10 +0000 | [diff] [blame] | 20 | wb.writeFlattenable(obj); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 21 | |
| 22 | size_t size = wb.size(); |
| 23 | SkAutoSMalloc<1024> storage(size); |
| 24 | // make a copy into storage |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 25 | wb.writeToMemory(storage.get()); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 26 | |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 27 | SkOrderedReadBuffer rb(storage.get(), size); |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 28 | return rb.readColorFilter(); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | #define ILLEGAL_MODE ((SkXfermode::Mode)-1) |
| 34 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 35 | DEF_TEST(ColorFilter, reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 36 | SkRandom rand; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 37 | |
| 38 | for (int mode = 0; mode <= SkXfermode::kLastMode; mode++) { |
| 39 | SkColor color = rand.nextU(); |
| 40 | |
| 41 | // ensure we always get a filter, by avoiding the possibility of a |
| 42 | // special case that would return NULL (if color's alpha is 0 or 0xFF) |
| 43 | color = SkColorSetA(color, 0x7F); |
| 44 | |
| 45 | SkColorFilter* cf = SkColorFilter::CreateModeFilter(color, |
| 46 | (SkXfermode::Mode)mode); |
| 47 | |
| 48 | // allow for no filter if we're in Dst mode (its a no op) |
| 49 | if (SkXfermode::kDst_Mode == mode && NULL == cf) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | SkAutoUnref aur(cf); |
| 54 | REPORTER_ASSERT(reporter, cf); |
| 55 | |
| 56 | SkColor c = ~color; |
| 57 | SkXfermode::Mode m = ILLEGAL_MODE; |
| 58 | |
| 59 | SkColor expectedColor = color; |
| 60 | SkXfermode::Mode expectedMode = (SkXfermode::Mode)mode; |
| 61 | |
| 62 | // SkDebugf("--- mc [%d %x] ", mode, color); |
| 63 | |
| 64 | REPORTER_ASSERT(reporter, cf->asColorMode(&c, &m)); |
| 65 | // handle special-case folding by the factory |
| 66 | if (SkXfermode::kClear_Mode == mode) { |
| 67 | if (c != expectedColor) { |
| 68 | expectedColor = 0; |
| 69 | } |
| 70 | if (m != expectedMode) { |
| 71 | expectedMode = SkXfermode::kSrc_Mode; |
| 72 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | } |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 74 | |
| 75 | // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, expectedColor); |
| 76 | |
| 77 | REPORTER_ASSERT(reporter, c == expectedColor); |
| 78 | REPORTER_ASSERT(reporter, m == expectedMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 80 | { |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 81 | SkColorFilter* cf2 = reincarnate_colorfilter(cf); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 82 | SkAutoUnref aur2(cf2); |
| 83 | REPORTER_ASSERT(reporter, cf2); |
| 84 | |
| 85 | SkColor c2 = ~color; |
| 86 | SkXfermode::Mode m2 = ILLEGAL_MODE; |
| 87 | REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, &m2)); |
| 88 | REPORTER_ASSERT(reporter, c2 == expectedColor); |
| 89 | REPORTER_ASSERT(reporter, m2 == expectedMode); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 94 | /////////////////////////////////////////////////////////////////////////////// |
| 95 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 96 | DEF_TEST(LumaColorFilter, reporter) { |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 97 | SkPMColor in, out; |
| 98 | SkAutoTUnref<SkColorFilter> lf(SkLumaColorFilter::Create()); |
| 99 | |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 100 | // Applying luma to white produces black with the same transparency. |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 101 | for (unsigned i = 0; i < 256; ++i) { |
| 102 | in = SkPackARGB32(i, i, i, i); |
| 103 | lf->filterSpan(&in, 1, &out); |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, SkGetPackedA32(out) == i); |
| 105 | REPORTER_ASSERT(reporter, SkGetPackedR32(out) == 0); |
| 106 | REPORTER_ASSERT(reporter, SkGetPackedG32(out) == 0); |
| 107 | REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Applying luma to black yields transparent black (luminance(black) == 0) |
| 111 | for (unsigned i = 0; i < 256; ++i) { |
| 112 | in = SkPackARGB32(i, 0, 0, 0); |
| 113 | lf->filterSpan(&in, 1, &out); |
| 114 | REPORTER_ASSERT(reporter, out == SK_ColorTRANSPARENT); |
| 115 | } |
| 116 | |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 117 | // 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] | 118 | for (unsigned i = 1; i < 256; ++i) { |
| 119 | in = SkPackARGB32(i, i, i / 2, i / 3); |
| 120 | lf->filterSpan(&in, 1, &out); |
| 121 | REPORTER_ASSERT(reporter, out != in); |
| 122 | REPORTER_ASSERT(reporter, SkGetPackedA32(out) <= i); |
commit-bot@chromium.org | d494b09 | 2013-10-10 20:13:51 +0000 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, SkGetPackedR32(out) == 0); |
| 124 | REPORTER_ASSERT(reporter, SkGetPackedG32(out) == 0); |
| 125 | REPORTER_ASSERT(reporter, SkGetPackedB32(out) == 0); |
commit-bot@chromium.org | 6c1ee2d | 2013-10-07 18:00:17 +0000 | [diff] [blame] | 126 | } |
| 127 | } |