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" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame^] | 9 | #include "SkBlendMode.h" |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 10 | #include "SkColor.h" |
| 11 | #include "SkColorFilter.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 12 | #include "SkRandom.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame^] | 14 | #include "SkRefCnt.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame^] | 16 | #include "SkTypes.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 | |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame^] | 19 | class SkFlattenable; |
| 20 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 21 | static sk_sp<SkColorFilter> reincarnate_colorfilter(SkFlattenable* obj) { |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 22 | SkBinaryWriteBuffer wb; |
djsollen@google.com | cefc865 | 2012-03-26 15:52:10 +0000 | [diff] [blame] | 23 | wb.writeFlattenable(obj); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 25 | size_t size = wb.bytesWritten(); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 26 | SkAutoSMalloc<1024> storage(size); |
| 27 | // make a copy into storage |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 28 | wb.writeToMemory(storage.get()); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 29 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 30 | SkReadBuffer rb(storage.get(), size); |
reed@google.com | 3534822 | 2013-10-16 13:05:06 +0000 | [diff] [blame] | 31 | return rb.readColorFilter(); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 36 | static sk_sp<SkColorFilter> make_filter() { |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 37 | // pick a filter that cannot compose with itself via newComposed() |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 38 | return SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kColorBurn); |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void test_composecolorfilter_limit(skiatest::Reporter* reporter) { |
| 42 | // Test that CreateComposeFilter() has some finite limit (i.e. that the factory can return null) |
| 43 | const int way_too_many = 100; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 44 | auto parent(make_filter()); |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 45 | for (int i = 2; i < way_too_many; ++i) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 46 | auto filter(make_filter()); |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 47 | parent = parent->makeComposed(filter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | if (nullptr == parent) { |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, i > 2); // we need to have succeeded at least once! |
| 50 | return; |
| 51 | } |
| 52 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, false); // we never saw a nullptr :( |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 56 | #define ILLEGAL_MODE ((SkBlendMode)-1) |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 57 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 58 | DEF_TEST(ColorFilter, reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 59 | SkRandom rand; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 60 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 61 | for (int mode = 0; mode <= (int)SkBlendMode::kLastMode; mode++) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 62 | SkColor color = rand.nextU(); |
| 63 | |
| 64 | // ensure we always get a filter, by avoiding the possibility of a |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | // 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] | 66 | color = SkColorSetA(color, 0x7F); |
| 67 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 68 | auto cf = SkColorFilter::MakeModeFilter(color, (SkBlendMode)mode); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 69 | |
| 70 | // 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] | 71 | if (SkBlendMode::kDst == (SkBlendMode)mode && nullptr == cf) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 72 | continue; |
| 73 | } |
| 74 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, cf); |
| 76 | |
| 77 | SkColor c = ~color; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 78 | SkBlendMode m = ILLEGAL_MODE; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 79 | |
| 80 | SkColor expectedColor = color; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 81 | SkBlendMode expectedMode = (SkBlendMode)mode; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 82 | |
| 83 | // SkDebugf("--- mc [%d %x] ", mode, color); |
| 84 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, cf->asColorMode(&c, (SkBlendMode*)&m)); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 86 | // handle special-case folding by the factory |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 87 | if (SkBlendMode::kClear == (SkBlendMode)mode) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 88 | if (c != expectedColor) { |
| 89 | expectedColor = 0; |
| 90 | } |
| 91 | if (m != expectedMode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 92 | expectedMode = SkBlendMode::kSrc; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 93 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 94 | } |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 95 | |
| 96 | // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, expectedColor); |
| 97 | |
| 98 | REPORTER_ASSERT(reporter, c == expectedColor); |
| 99 | REPORTER_ASSERT(reporter, m == expectedMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 100 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 101 | { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 102 | auto cf2 = reincarnate_colorfilter(cf.get()); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, cf2); |
| 104 | |
| 105 | SkColor c2 = ~color; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 106 | SkBlendMode m2 = ILLEGAL_MODE; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, (SkBlendMode*)&m2)); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 108 | REPORTER_ASSERT(reporter, c2 == expectedColor); |
| 109 | REPORTER_ASSERT(reporter, m2 == expectedMode); |
| 110 | } |
| 111 | } |
reed | dc81222 | 2015-03-05 07:21:02 -0800 | [diff] [blame] | 112 | |
| 113 | test_composecolorfilter_limit(reporter); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 114 | } |