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()); |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame^] | 45 | parent = parent->makeComposed(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 | } |