OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkArithmeticMode.h" | |
9 #include "SkMatrix.h" | |
10 #include "SkPixelXorXfermode.h" | |
11 #include "SkToFromValue.h" | |
12 #include "SkValueKeys.h" | |
13 #include "SkXfermode.h" | |
14 | |
15 //////////////////////////////////////////////////////////////////////////////// | |
16 | |
17 #define REQUIRE(cond) do { if (!(cond)) { SkASSERT(false); return false; } } whi
le (false) | |
18 | |
19 template <typename T> | |
20 bool getT(const SkValue& obj, SkValue::Key key, T* ptr) { | |
21 auto v = obj.get(key); | |
22 return v ? SkFromValue(*v, ptr) : false; | |
23 } | |
24 | |
25 template<> bool SkFromValue<float>(const SkValue& val, float* f) { | |
26 REQUIRE(val.type() == SkValue::F32); | |
27 *f = val.f32(); | |
28 return true; | |
29 } | |
30 | |
31 template<> bool SkFromValue<int32_t>(const SkValue& val, int32_t* x) { | |
32 REQUIRE(val.type() == SkValue::S32); | |
33 *x = val.s32(); | |
34 return true; | |
35 } | |
36 | |
37 template<> bool SkFromValue<uint32_t>(const SkValue& val, uint32_t* x) { | |
38 REQUIRE(val.type() == SkValue::U32); | |
39 *x = val.u32(); | |
40 return true; | |
41 } | |
42 | |
43 //////////////////////////////////////////////////////////////////////////////// | |
44 | |
45 template<> SkValue SkToValue<SkMatrix>(const SkMatrix& mat) { | |
46 auto val = SkValue::Object(SkValue::Matrix); | |
47 for (int i = 0; i < 9; ++i) { | |
48 if (mat[i] != SkMatrix::I()[i]) { | |
49 val.set(i, SkValue::FromF32(mat[i])); | |
50 } | |
51 } | |
52 return val; | |
53 } | |
54 | |
55 template<> bool SkFromValue<SkMatrix>(const SkValue& val, SkMatrix* m) { | |
56 REQUIRE(val.type() == SkValue::Matrix); | |
57 *m = SkMatrix::I(); | |
58 for (int i = 0; i < 9; i++) { | |
59 getT(val, i, &(*m)[i]); | |
60 } | |
61 return true; | |
62 } | |
63 | |
64 //////////////////////////////////////////////////////////////////////////////// | |
65 | |
66 template<> SkValue SkToValue<SkXfermode>(const SkXfermode* x) { | |
67 return x ? x->asValue() : SkValue::Object(SkValue::DefaultXfermode); | |
68 } | |
69 | |
70 static bool from_value_DefaultXfermode(const SkValue& val, | |
71 SkAutoTUnref<SkXfermode>* dst) { | |
72 dst->reset(nullptr); | |
73 return true; | |
74 } | |
75 | |
76 static bool from_value_ArithmeticXfermode(const SkValue& val, | |
77 SkAutoTUnref<SkXfermode>* dst) { | |
78 using namespace SkValueKeys::ArithmeticXfermode; | |
79 float k[4]; | |
80 REQUIRE(getT(val, kK0, &k[0])); | |
81 REQUIRE(getT(val, kK1, &k[1])); | |
82 REQUIRE(getT(val, kK2, &k[2])); | |
83 REQUIRE(getT(val, kK3, &k[3])); | |
84 int32_t enforce = true; | |
85 getT(val, kEnforcePMColor, &enforce); | |
86 dst->reset(SkArithmeticMode::Create( | |
87 k[0], k[1], k[2], k[3], SkToBool(enforce))); | |
88 return true; | |
89 } | |
90 | |
91 static bool from_value_PixelXorXfermode(const SkValue& val, | |
92 SkAutoTUnref<SkXfermode>* dst) { | |
93 uint32_t opColor; | |
94 REQUIRE(getT(val, SkValueKeys::PixelXorXfermode::kOpColor, &opColor)); | |
95 dst->reset(SkPixelXorXfermode::Create(opColor)); | |
96 return true; | |
97 } | |
98 | |
99 static bool from_value_ProcCoeffXfermode(const SkValue& val, | |
100 SkAutoTUnref<SkXfermode>* dst) { | |
101 uint32_t mode; | |
102 REQUIRE(getT(val, SkValueKeys::ProcCoeffXfermode::kMode, &mode)); | |
103 dst->reset(SkXfermode::Create((SkXfermode::Mode)mode)); | |
104 return true; | |
105 } | |
106 | |
107 template<> bool SkFromValue< SkAutoTUnref<SkXfermode> >( | |
108 const SkValue& val, SkAutoTUnref<SkXfermode>* dst) { | |
109 switch (val.type()) { | |
110 case SkValue::DefaultXfermode: return from_value_DefaultXfermode(val,
dst); | |
111 case SkValue::ArithmeticXfermode: return from_value_ArithmeticXfermode(v
al, dst); | |
112 case SkValue::PixelXorXfermode: return from_value_PixelXorXfermode(val
, dst); | |
113 case SkValue::ProcCoeffXfermode: return from_value_ProcCoeffXfermode(va
l, dst); | |
114 default: REQUIRE(false); | |
115 } | |
116 } | |
117 | |
118 //////////////////////////////////////////////////////////////////////////////// | |
119 | |
120 #undef REQUIRE | |
121 | |
OLD | NEW |