epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkXfermode.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 13 | #include "SkOrderedReadBuffer.h" |
| 14 | #include "SkOrderedWriteBuffer.h" |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 15 | |
| 16 | static SkFlattenable* reincarnate_flattenable(SkFlattenable* obj) { |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 17 | SkOrderedWriteBuffer wb(1024); |
djsollen@google.com | cefc865 | 2012-03-26 15:52:10 +0000 | [diff] [blame] | 18 | wb.writeFlattenable(obj); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 19 | |
| 20 | size_t size = wb.size(); |
| 21 | SkAutoSMalloc<1024> storage(size); |
| 22 | // make a copy into storage |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 23 | wb.writeToMemory(storage.get()); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 24 | |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 25 | SkOrderedReadBuffer rb(storage.get(), size); |
djsollen@google.com | cefc865 | 2012-03-26 15:52:10 +0000 | [diff] [blame] | 26 | return rb.readFlattenable(); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | template <typename T> T* reincarnate(T* obj) { |
| 30 | return (T*)reincarnate_flattenable(obj); |
| 31 | } |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | #define ILLEGAL_MODE ((SkXfermode::Mode)-1) |
| 36 | |
| 37 | static void test_asColorMode(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame^] | 38 | SkRandom rand; |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 39 | |
| 40 | for (int mode = 0; mode <= SkXfermode::kLastMode; mode++) { |
| 41 | SkColor color = rand.nextU(); |
| 42 | |
| 43 | // ensure we always get a filter, by avoiding the possibility of a |
| 44 | // special case that would return NULL (if color's alpha is 0 or 0xFF) |
| 45 | color = SkColorSetA(color, 0x7F); |
| 46 | |
| 47 | SkColorFilter* cf = SkColorFilter::CreateModeFilter(color, |
| 48 | (SkXfermode::Mode)mode); |
| 49 | |
| 50 | // allow for no filter if we're in Dst mode (its a no op) |
| 51 | if (SkXfermode::kDst_Mode == mode && NULL == cf) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | SkAutoUnref aur(cf); |
| 56 | REPORTER_ASSERT(reporter, cf); |
| 57 | |
| 58 | SkColor c = ~color; |
| 59 | SkXfermode::Mode m = ILLEGAL_MODE; |
| 60 | |
| 61 | SkColor expectedColor = color; |
| 62 | SkXfermode::Mode expectedMode = (SkXfermode::Mode)mode; |
| 63 | |
| 64 | // SkDebugf("--- mc [%d %x] ", mode, color); |
| 65 | |
| 66 | REPORTER_ASSERT(reporter, cf->asColorMode(&c, &m)); |
| 67 | // handle special-case folding by the factory |
| 68 | if (SkXfermode::kClear_Mode == mode) { |
| 69 | if (c != expectedColor) { |
| 70 | expectedColor = 0; |
| 71 | } |
| 72 | if (m != expectedMode) { |
| 73 | expectedMode = SkXfermode::kSrc_Mode; |
| 74 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 75 | } |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 76 | |
| 77 | // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, expectedColor); |
| 78 | |
| 79 | REPORTER_ASSERT(reporter, c == expectedColor); |
| 80 | REPORTER_ASSERT(reporter, m == expectedMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 81 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 82 | { |
| 83 | SkColorFilter* cf2 = reincarnate(cf); |
| 84 | SkAutoUnref aur2(cf2); |
| 85 | REPORTER_ASSERT(reporter, cf2); |
| 86 | |
| 87 | SkColor c2 = ~color; |
| 88 | SkXfermode::Mode m2 = ILLEGAL_MODE; |
| 89 | REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, &m2)); |
| 90 | REPORTER_ASSERT(reporter, c2 == expectedColor); |
| 91 | REPORTER_ASSERT(reporter, m2 == expectedMode); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #include "TestClassDef.h" |
| 97 | DEFINE_TESTCLASS("ColorFilter", ColorFilterTestClass, test_asColorMode) |