aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects/SkToFromValue.cpp
blob: b981d1715ee4d475f9b90363cdb10a301e0e74cb (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkArithmeticMode.h"
#include "SkLerpXfermode.h"
#include "SkMatrix.h"
#include "SkPixelXorXfermode.h"
#include "SkToFromValue.h"
#include "SkValueKeys.h"
#include "SkXfermode.h"

////////////////////////////////////////////////////////////////////////////////

#define REQUIRE(cond) do { if (!(cond)) { SkASSERT(false); return false; } } while (false)

template <typename T>
bool getT(const SkValue& obj, SkValue::Key key, T* ptr) {
    auto v = obj.get(key);
    return v ? SkFromValue(*v, ptr) : false;
}

template<> bool SkFromValue<float>(const SkValue& val, float* f) {
    REQUIRE(val.type() == SkValue::F32);
    *f = val.f32();
    return true;
}

template<> bool SkFromValue<int32_t>(const SkValue& val, int32_t* x) {
    REQUIRE(val.type() == SkValue::S32);
    *x = val.s32();
    return true;
}

template<> bool SkFromValue<uint32_t>(const SkValue& val, uint32_t* x) {
    REQUIRE(val.type() == SkValue::U32);
    *x = val.u32();
    return true;
}

////////////////////////////////////////////////////////////////////////////////

template<> SkValue SkToValue<SkMatrix>(const SkMatrix& mat) {
    auto val = SkValue::Object(SkValue::Matrix);
    for (int i = 0; i < 9; ++i) {
        if (mat[i] != SkMatrix::I()[i]) {
            val.set(i, SkValue::FromF32(mat[i]));
        }
    }
    return val;
}

template<> bool SkFromValue<SkMatrix>(const SkValue& val, SkMatrix* m) {
    REQUIRE(val.type() == SkValue::Matrix);
    *m = SkMatrix::I();
    for (int i = 0; i < 9; i++) {
        getT(val, i, &(*m)[i]);
    }
    return true;
}

////////////////////////////////////////////////////////////////////////////////

template<> SkValue SkToValue<SkXfermode>(const SkXfermode* x) {
    return x ? x->asValue() : SkValue::Object(SkValue::DefaultXfermode);
}

static bool from_value_DefaultXfermode(const SkValue& val,
                                       SkAutoTUnref<SkXfermode>* dst) {
    dst->reset(nullptr);
    return true;
}

static bool from_value_ArithmeticXfermode(const SkValue& val,
                                          SkAutoTUnref<SkXfermode>* dst) {
    using namespace SkValueKeys::ArithmeticXfermode;
    float k[4];
    REQUIRE(getT(val, kK0, &k[0]));
    REQUIRE(getT(val, kK1, &k[1]));
    REQUIRE(getT(val, kK2, &k[2]));
    REQUIRE(getT(val, kK3, &k[3]));
    int32_t enforce = true;
    getT(val, kEnforcePMColor, &enforce);
    dst->reset(SkArithmeticMode::Create(
                       k[0], k[1], k[2], k[3], SkToBool(enforce)));
    return true;
}

static bool from_value_LerpXfermode(const SkValue& val,
                                    SkAutoTUnref<SkXfermode>* dst) {
    float scale;
    REQUIRE(getT(val, SkValueKeys::LerpXfermode::kScale, &scale));
    dst->reset(SkLerpXfermode::Create(scale));
    return true;
}

static bool from_value_PixelXorXfermode(const SkValue& val,
                                        SkAutoTUnref<SkXfermode>* dst) {
    uint32_t opColor;
    REQUIRE(getT(val, SkValueKeys::PixelXorXfermode::kOpColor, &opColor));
    dst->reset(SkPixelXorXfermode::Create(opColor));
    return true;
}

static bool from_value_ProcCoeffXfermode(const SkValue& val,
                                         SkAutoTUnref<SkXfermode>* dst) {
    uint32_t mode;
    REQUIRE(getT(val, SkValueKeys::ProcCoeffXfermode::kMode, &mode));
    dst->reset(SkXfermode::Create((SkXfermode::Mode)mode));
    return true;
}

template<> bool SkFromValue< SkAutoTUnref<SkXfermode> >(
        const SkValue& val, SkAutoTUnref<SkXfermode>* dst) {
    switch (val.type()) {
        case SkValue::DefaultXfermode:    return from_value_DefaultXfermode(val, dst);
        case SkValue::ArithmeticXfermode: return from_value_ArithmeticXfermode(val, dst);
        case SkValue::LerpXfermode:       return from_value_LerpXfermode(val, dst);
        case SkValue::PixelXorXfermode:   return from_value_PixelXorXfermode(val, dst);
        case SkValue::ProcCoeffXfermode:  return from_value_ProcCoeffXfermode(val, dst);
        default:                          REQUIRE(false);
    }
}

////////////////////////////////////////////////////////////////////////////////

#undef REQUIRE