blob: 816ddae04b521a70df44ade1c73f299ccde520b5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.org3334c3a2011-04-20 11:39:28 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.coma0f5d152009-06-22 17:38:10 +000010#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.
21static 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.com8d3cd7a2013-01-30 21:36:11 +000039 MAKE_PAIR(Modulate),
reed@android.coma0f5d152009-06-22 17:38:10 +000040 MAKE_PAIR(Screen),
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000041 { SkPorterDuff::kAdd_Mode, SkXfermode::kPlus_Mode },
djsollen@google.com56c69772011-11-08 19:00:26 +000042#ifdef SK_BUILD_FOR_ANDROID
djsollen@google.comcd9d69b2011-03-14 20:30:14 +000043 MAKE_PAIR(Overlay),
44#endif
reed@android.coma0f5d152009-06-22 17:38:10 +000045};
46
47static 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.com845fdac2009-06-23 03:01:32 +000060SkXfermode::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.coma0f5d152009-06-22 17:38:10 +000067SkXfermode* SkPorterDuff::CreateXfermode(SkPorterDuff::Mode mode) {
68 const Pair& pair = gPairs[mode];
69 SkASSERT(pair.fPD == mode);
70 return SkXfermode::Create(pair.fXF);
71}
72
73bool 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
81SkXfermodeProc SkPorterDuff::GetXfermodeProc(Mode mode) {
82 return SkXfermode::GetProc(gPairs[mode].fXF);
83}
84
85SkXfermodeProc16 SkPorterDuff::GetXfermodeProc16(Mode mode, SkColor srcColor) {
86 return SkXfermode::GetProc16(gPairs[mode].fXF, srcColor);
87}