diff options
author | halcanary <halcanary@google.com> | 2016-01-21 14:15:10 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-21 14:15:10 -0800 |
commit | 27a6e86fb17fce7ce962b9080eae36926e87d568 (patch) | |
tree | dd71c19559185f2fbc00485ab6a00e0b9bab4cad /src/effects | |
parent | ae47418936933c9dd7f4be93fab7c3a42d7a7bd9 (diff) |
SkValue: SkXfermode
Implement:
template<> SkValue SkToValue<SkXfermode>(const SkXfermode*);
template<> bool SkFromValue<SkXfermode*>(const SkValue&, SkXfermode**);
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1585813004
NOTRY=true
Review URL: https://codereview.chromium.org/1585813004
Diffstat (limited to 'src/effects')
-rw-r--r-- | src/effects/SkArithmeticMode.cpp | 14 | ||||
-rw-r--r-- | src/effects/SkLerpXfermode.cpp | 9 | ||||
-rw-r--r-- | src/effects/SkPixelXorXfermode.cpp | 9 | ||||
-rw-r--r-- | src/effects/SkToFromValue.cpp | 131 | ||||
-rw-r--r-- | src/effects/SkToFromValue.h | 21 |
5 files changed, 184 insertions, 0 deletions
diff --git a/src/effects/SkArithmeticMode.cpp b/src/effects/SkArithmeticMode.cpp index 3a8c8f398e..2e68cab353 100644 --- a/src/effects/SkArithmeticMode.cpp +++ b/src/effects/SkArithmeticMode.cpp @@ -11,6 +11,8 @@ #include "SkWriteBuffer.h" #include "SkString.h" #include "SkUnPreMultiply.h" +#include "SkValue.h" +#include "SkValueKeys.h" #if SK_SUPPORT_GPU #include "SkArithmeticMode_gpu.h" #endif @@ -53,6 +55,18 @@ private: buffer.writeScalar(fK[3]); buffer.writeBool(fEnforcePMColor); } + + SkValue asValue() const override { + auto value = SkValue::Object(SkValue::ArithmeticXfermode); + using namespace SkValueKeys::ArithmeticXfermode; + value.set(kK0, SkValue::FromF32(fK[0])); + value.set(kK1, SkValue::FromF32(fK[1])); + value.set(kK2, SkValue::FromF32(fK[2])); + value.set(kK3, SkValue::FromF32(fK[3])); + value.set(kEnforcePMColor, SkValue::FromS32(fEnforcePMColor ? 1 : 0)); + return value; + } + SkScalar fK[4]; bool fEnforcePMColor; diff --git a/src/effects/SkLerpXfermode.cpp b/src/effects/SkLerpXfermode.cpp index a9b0d9e3e7..a069e8b367 100644 --- a/src/effects/SkLerpXfermode.cpp +++ b/src/effects/SkLerpXfermode.cpp @@ -10,6 +10,8 @@ #include "SkReadBuffer.h" #include "SkWriteBuffer.h" #include "SkString.h" +#include "SkValue.h" +#include "SkValueKeys.h" SkXfermode* SkLerpXfermode::Create(SkScalar scale) { int scale256 = SkScalarRoundToInt(scale * 256); @@ -107,3 +109,10 @@ void SkLerpXfermode::toString(SkString* str) const { str->printf("SkLerpXfermode: scale: %g", fScale256 / 256.0); } #endif + +SkValue SkLerpXfermode::asValue() const { + auto value = SkValue::Object(SkValue::LerpXfermode); + value.set(SkValueKeys::LerpXfermode::kScale, + SkValue::FromF32(fScale256 / 256.0f)); + return value; +} diff --git a/src/effects/SkPixelXorXfermode.cpp b/src/effects/SkPixelXorXfermode.cpp index 2a6ac47aac..8e13b2250c 100644 --- a/src/effects/SkPixelXorXfermode.cpp +++ b/src/effects/SkPixelXorXfermode.cpp @@ -12,6 +12,8 @@ #include "SkReadBuffer.h" #include "SkWriteBuffer.h" #include "SkString.h" +#include "SkValue.h" +#include "SkValueKeys.h" // we always return an opaque color, 'cause I don't know what to do with // the alpha-component and still return a valid premultiplied color. @@ -35,3 +37,10 @@ void SkPixelXorXfermode::toString(SkString* str) const { str->appendHex(fOpColor); } #endif + +SkValue SkPixelXorXfermode::asValue() const { + auto value = SkValue::Object(SkValue::PixelXorXfermode); + value.set(SkValueKeys::PixelXorXfermode::kOpColor, + SkValue::FromU32(SkToU32(fOpColor))); + return value; +} diff --git a/src/effects/SkToFromValue.cpp b/src/effects/SkToFromValue.cpp new file mode 100644 index 0000000000..b981d1715e --- /dev/null +++ b/src/effects/SkToFromValue.cpp @@ -0,0 +1,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 + diff --git a/src/effects/SkToFromValue.h b/src/effects/SkToFromValue.h new file mode 100644 index 0000000000..1ead4bd3fb --- /dev/null +++ b/src/effects/SkToFromValue.h @@ -0,0 +1,21 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#ifndef SkToFromValue_DEFINED +#define SkToFromValue_DEFINED + +#include "SkValue.h" + +template <typename T> +SkValue SkToValue(const T&); + +template <typename T> +SkValue SkToValue(const T*); + +template <typename T> +bool SkFromValue(const SkValue&, T*); + +#endif // SkToFromValue_DEFINED |