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" |
| 13 | |
| 14 | static SkFlattenable* reincarnate_flattenable(SkFlattenable* obj) { |
| 15 | SkFlattenable::Factory fact = obj->getFactory(); |
| 16 | if (NULL == fact) { |
| 17 | return NULL; |
| 18 | } |
| 19 | |
| 20 | SkFlattenableWriteBuffer wb(1024); |
| 21 | obj->flatten(wb); |
| 22 | |
| 23 | size_t size = wb.size(); |
| 24 | SkAutoSMalloc<1024> storage(size); |
| 25 | // make a copy into storage |
| 26 | wb.flatten(storage.get()); |
| 27 | |
| 28 | SkFlattenableReadBuffer rb(storage.get(), size); |
| 29 | return fact(rb); |
| 30 | } |
| 31 | |
| 32 | template <typename T> T* reincarnate(T* obj) { |
| 33 | return (T*)reincarnate_flattenable(obj); |
| 34 | } |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | #define ILLEGAL_MODE ((SkXfermode::Mode)-1) |
| 39 | |
| 40 | static void test_asColorMode(skiatest::Reporter* reporter) { |
| 41 | SkRandom rand; |
| 42 | |
| 43 | for (int mode = 0; mode <= SkXfermode::kLastMode; mode++) { |
| 44 | SkColor color = rand.nextU(); |
| 45 | |
| 46 | // ensure we always get a filter, by avoiding the possibility of a |
| 47 | // special case that would return NULL (if color's alpha is 0 or 0xFF) |
| 48 | color = SkColorSetA(color, 0x7F); |
| 49 | |
| 50 | SkColorFilter* cf = SkColorFilter::CreateModeFilter(color, |
| 51 | (SkXfermode::Mode)mode); |
| 52 | |
| 53 | // allow for no filter if we're in Dst mode (its a no op) |
| 54 | if (SkXfermode::kDst_Mode == mode && NULL == cf) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | SkAutoUnref aur(cf); |
| 59 | REPORTER_ASSERT(reporter, cf); |
| 60 | |
| 61 | SkColor c = ~color; |
| 62 | SkXfermode::Mode m = ILLEGAL_MODE; |
| 63 | |
| 64 | SkColor expectedColor = color; |
| 65 | SkXfermode::Mode expectedMode = (SkXfermode::Mode)mode; |
| 66 | |
| 67 | // SkDebugf("--- mc [%d %x] ", mode, color); |
| 68 | |
| 69 | REPORTER_ASSERT(reporter, cf->asColorMode(&c, &m)); |
| 70 | // handle special-case folding by the factory |
| 71 | if (SkXfermode::kClear_Mode == mode) { |
| 72 | if (c != expectedColor) { |
| 73 | expectedColor = 0; |
| 74 | } |
| 75 | if (m != expectedMode) { |
| 76 | expectedMode = SkXfermode::kSrc_Mode; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // SkDebugf("--- got [%d %x] expected [%d %x]\n", m, c, expectedMode, expectedColor); |
| 81 | |
| 82 | REPORTER_ASSERT(reporter, c == expectedColor); |
| 83 | REPORTER_ASSERT(reporter, m == expectedMode); |
| 84 | |
| 85 | { |
| 86 | SkColorFilter* cf2 = reincarnate(cf); |
| 87 | SkAutoUnref aur2(cf2); |
| 88 | REPORTER_ASSERT(reporter, cf2); |
| 89 | |
| 90 | SkColor c2 = ~color; |
| 91 | SkXfermode::Mode m2 = ILLEGAL_MODE; |
| 92 | REPORTER_ASSERT(reporter, cf2->asColorMode(&c2, &m2)); |
| 93 | REPORTER_ASSERT(reporter, c2 == expectedColor); |
| 94 | REPORTER_ASSERT(reporter, m2 == expectedMode); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #include "TestClassDef.h" |
| 100 | DEFINE_TESTCLASS("ColorFilter", ColorFilterTestClass, test_asColorMode) |