aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects/SkPorterDuff.cpp
blob: 4cdcf798a1c527b869502ea44ffa163fb2621f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkPorterDuff.h"
#include "SkXfermode.h"

/*  This file just exists as a compatibility layer, gluing the PorterDuff API
    into the (extended) SkXfermode API
 */

#define MAKE_PAIR(mode) { SkPorterDuff::k##mode##_Mode, SkXfermode::k##mode##_Mode }

// this table must be in SkPorterDuff::Mode order, so it can be indexed directly
// with a porterduff mode.
static const struct Pair {
    SkPorterDuff::Mode  fPD;
    SkXfermode::Mode    fXF;
} gPairs[] = {
    MAKE_PAIR(Clear),
    MAKE_PAIR(Src),
    MAKE_PAIR(Dst),
    MAKE_PAIR(SrcOver),
    MAKE_PAIR(DstOver),
    MAKE_PAIR(SrcIn),
    MAKE_PAIR(DstIn),
    MAKE_PAIR(SrcOut),
    MAKE_PAIR(DstOut),
    MAKE_PAIR(SrcATop),
    MAKE_PAIR(DstATop),
    MAKE_PAIR(Xor),
    MAKE_PAIR(Darken),
    MAKE_PAIR(Lighten),
    MAKE_PAIR(Modulate),
    MAKE_PAIR(Screen),
    { SkPorterDuff::kAdd_Mode, SkXfermode::kPlus_Mode },
#ifdef SK_BUILD_FOR_ANDROID
    MAKE_PAIR(Overlay),
#endif
};

static bool find_pdmode(SkXfermode::Mode src, SkPorterDuff::Mode* dst) {
    const Pair* pairs = gPairs;
    for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
        if (pairs[i].fXF == src) {
            if (dst) {
                *dst = pairs[i].fPD;
            }
            return true;
        }
    }
    return false;
}

SkXfermode::Mode SkPorterDuff::ToXfermodeMode(Mode mode) {
    SkASSERT((unsigned)mode < SkPorterDuff::kModeCount);
    const Pair& pair = gPairs[mode];
    SkASSERT(pair.fPD == mode);
    return pair.fXF;
}

SkXfermode* SkPorterDuff::CreateXfermode(SkPorterDuff::Mode mode) {
    const Pair& pair = gPairs[mode];
    SkASSERT(pair.fPD == mode);
    return SkXfermode::Create(pair.fXF);
}

bool SkPorterDuff::IsMode(SkXfermode* xfer, Mode* pdmode) {
    SkXfermode::Mode xfmode;
    if (!SkXfermode::AsMode(xfer, &xfmode)) {
        return false;
    }
    return find_pdmode(xfmode, pdmode);
}

SkXfermodeProc SkPorterDuff::GetXfermodeProc(Mode mode) {
    return SkXfermode::GetProc(gPairs[mode].fXF);
}

SkXfermodeProc16 SkPorterDuff::GetXfermodeProc16(Mode mode, SkColor srcColor) {
    return SkXfermode::GetProc16(gPairs[mode].fXF, srcColor);
}