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