epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 10 | #include "SkPorterDuff.h" |
| 11 | #include "SkXfermode.h" |
| 12 | |
| 13 | /* This file just exists as a compatibility layer, gluing the PorterDuff API |
| 14 | into the (extended) SkXfermode API |
| 15 | */ |
| 16 | |
| 17 | #define MAKE_PAIR(mode) { SkPorterDuff::k##mode##_Mode, SkXfermode::k##mode##_Mode } |
| 18 | |
| 19 | // this table must be in SkPorterDuff::Mode order, so it can be indexed directly |
| 20 | // with a porterduff mode. |
| 21 | static const struct Pair { |
| 22 | SkPorterDuff::Mode fPD; |
| 23 | SkXfermode::Mode fXF; |
| 24 | } gPairs[] = { |
| 25 | MAKE_PAIR(Clear), |
| 26 | MAKE_PAIR(Src), |
| 27 | MAKE_PAIR(Dst), |
| 28 | MAKE_PAIR(SrcOver), |
| 29 | MAKE_PAIR(DstOver), |
| 30 | MAKE_PAIR(SrcIn), |
| 31 | MAKE_PAIR(DstIn), |
| 32 | MAKE_PAIR(SrcOut), |
| 33 | MAKE_PAIR(DstOut), |
| 34 | MAKE_PAIR(SrcATop), |
| 35 | MAKE_PAIR(DstATop), |
| 36 | MAKE_PAIR(Xor), |
| 37 | MAKE_PAIR(Darken), |
| 38 | MAKE_PAIR(Lighten), |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 39 | MAKE_PAIR(Modulate), |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 40 | MAKE_PAIR(Screen), |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 41 | { SkPorterDuff::kAdd_Mode, SkXfermode::kPlus_Mode }, |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 42 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 43 | MAKE_PAIR(Overlay), |
| 44 | #endif |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static bool find_pdmode(SkXfermode::Mode src, SkPorterDuff::Mode* dst) { |
| 48 | const Pair* pairs = gPairs; |
| 49 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 50 | if (pairs[i].fXF == src) { |
| 51 | if (dst) { |
| 52 | *dst = pairs[i].fPD; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | return false; |
| 58 | } |
| 59 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 60 | SkXfermode::Mode SkPorterDuff::ToXfermodeMode(Mode mode) { |
| 61 | SkASSERT((unsigned)mode < SkPorterDuff::kModeCount); |
| 62 | const Pair& pair = gPairs[mode]; |
| 63 | SkASSERT(pair.fPD == mode); |
| 64 | return pair.fXF; |
| 65 | } |
| 66 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 67 | SkXfermode* SkPorterDuff::CreateXfermode(SkPorterDuff::Mode mode) { |
| 68 | const Pair& pair = gPairs[mode]; |
| 69 | SkASSERT(pair.fPD == mode); |
| 70 | return SkXfermode::Create(pair.fXF); |
| 71 | } |
| 72 | |
| 73 | bool SkPorterDuff::IsMode(SkXfermode* xfer, Mode* pdmode) { |
| 74 | SkXfermode::Mode xfmode; |
| 75 | if (!SkXfermode::IsMode(xfer, &xfmode)) { |
| 76 | return false; |
| 77 | } |
| 78 | return find_pdmode(xfmode, pdmode); |
| 79 | } |
| 80 | |
| 81 | SkXfermodeProc SkPorterDuff::GetXfermodeProc(Mode mode) { |
| 82 | return SkXfermode::GetProc(gPairs[mode].fXF); |
| 83 | } |
| 84 | |
| 85 | SkXfermodeProc16 SkPorterDuff::GetXfermodeProc16(Mode mode, SkColor srcColor) { |
| 86 | return SkXfermode::GetProc16(gPairs[mode].fXF, srcColor); |
| 87 | } |