From 27a6e86fb17fce7ce962b9080eae36926e87d568 Mon Sep 17 00:00:00 2001 From: halcanary Date: Thu, 21 Jan 2016 14:15:10 -0800 Subject: SkValue: SkXfermode Implement: template<> SkValue SkToValue(const SkXfermode*); template<> bool SkFromValue(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 --- src/core/SkValue.cpp | 35 ---------- src/core/SkValue.h | 12 ++-- src/core/SkValueKeys.h | 20 ++++++ src/core/SkXfermode.cpp | 10 +++ src/core/SkXfermode_proccoeff.h | 2 + src/effects/SkArithmeticMode.cpp | 14 ++++ src/effects/SkLerpXfermode.cpp | 9 +++ src/effects/SkPixelXorXfermode.cpp | 9 +++ src/effects/SkToFromValue.cpp | 131 +++++++++++++++++++++++++++++++++++++ src/effects/SkToFromValue.h | 21 ++++++ 10 files changed, 222 insertions(+), 41 deletions(-) create mode 100644 src/core/SkValueKeys.h create mode 100644 src/effects/SkToFromValue.cpp create mode 100644 src/effects/SkToFromValue.h (limited to 'src') diff --git a/src/core/SkValue.cpp b/src/core/SkValue.cpp index 787cdf58df..2db2796b0c 100644 --- a/src/core/SkValue.cpp +++ b/src/core/SkValue.cpp @@ -9,7 +9,6 @@ #include #include "SkData.h" -#include "SkMatrix.h" #include "SkValue.h" class SkValue::Obj { @@ -184,37 +183,3 @@ SkValue SkValue::FromTs(SkValue::Type type, SkData* data) { SkValue SkValue::FromU16s(SkData* d) { return FromTs(U16s, d); } SkValue SkValue::FromU32s(SkData* d) { return FromTs(U32s, d); } SkValue SkValue::FromF32s(SkData* d) { return FromTs< float>(F32s, d); } - -//////////////////////////////////////////////////////////////////////////////// - -#define REQUIRE(cond) do { if (!(cond)) { SkASSERT(false); return false; } } while (false) - -template<> SkValue SkToValue(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(const SkValue& val, float* f) { - REQUIRE(val.type() == SkValue::F32); - *f = val.f32(); - return true; -} - -template<> bool SkFromValue(const SkValue& val, SkMatrix* m){ - REQUIRE(val.type() == SkValue::Matrix); - - *m = SkMatrix::I(); - for (int i = 0; i < 9; i++) { - if (auto v = val.get(i)) { - REQUIRE(SkFromValue(*v, &(*m)[i])); - } - } - return true; -} - -#undef REQUIRE diff --git a/src/core/SkValue.h b/src/core/SkValue.h index 1f11209056..5a6453f908 100644 --- a/src/core/SkValue.h +++ b/src/core/SkValue.h @@ -27,6 +27,12 @@ public: Matrix, + ArithmeticXfermode, + DefaultXfermode, + LerpXfermode, + PixelXorXfermode, + ProcCoeffXfermode, + kMaxPublicObject = 0x7FFFFFFF, // 2147483648+ won't be used by Skia. They're open for // client-specific use, testing, etc. @@ -108,10 +114,4 @@ private: template const T* asTs(SkValue::Type, int*) const; }; -template -SkValue SkToValue(const T&); - -template -bool SkFromValue(const SkValue&, T*); - #endif // SkValue_DEFINED diff --git a/src/core/SkValueKeys.h b/src/core/SkValueKeys.h new file mode 100644 index 0000000000..3ec3b5a306 --- /dev/null +++ b/src/core/SkValueKeys.h @@ -0,0 +1,20 @@ +/* + * 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 SkValueKeys_DEFINED +#define SkValueKeys_DEFINED + +namespace SkValueKeys { + namespace ArithmeticXfermode { + enum { kK0, kK1, kK2, kK3, kEnforcePMColor }; + } + namespace LerpXfermode { enum { kScale }; } + namespace PixelXorXfermode { enum { kOpColor }; } + namespace ProcCoeffXfermode { enum { kMode }; } +} + +#endif // SkValueKeys_DEFINED diff --git a/src/core/SkXfermode.cpp b/src/core/SkXfermode.cpp index 79de322c2d..ad477e1215 100644 --- a/src/core/SkXfermode.cpp +++ b/src/core/SkXfermode.cpp @@ -15,6 +15,8 @@ #include "SkReadBuffer.h" #include "SkString.h" #include "SkWriteBuffer.h" +#include "SkValue.h" +#include "SkValueKeys.h" #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) @@ -763,6 +765,12 @@ void SkProcCoeffXfermode::flatten(SkWriteBuffer& buffer) const { buffer.write32(fMode); } +SkValue SkProcCoeffXfermode::asValue() const { + auto value = SkValue::Object(SkValue::ProcCoeffXfermode); + value.set(SkValueKeys::ProcCoeffXfermode::kMode, SkValue::FromU32(SkToU32(fMode))); + return value; +} + bool SkProcCoeffXfermode::asMode(Mode* mode) const { if (mode) { *mode = fMode; @@ -1071,3 +1079,5 @@ bool SkXfermode::IsOpaque(const SkXfermode* xfer, SrcColorOpacity opacityType) { SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkXfermode) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkProcCoeffXfermode) SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END + +SkValue SkXfermode::asValue() const { return SkValue(); } diff --git a/src/core/SkXfermode_proccoeff.h b/src/core/SkXfermode_proccoeff.h index f86af2df01..c81957c7fc 100644 --- a/src/core/SkXfermode_proccoeff.h +++ b/src/core/SkXfermode_proccoeff.h @@ -67,6 +67,8 @@ private: friend class SkXfermode; + SkValue asValue() const override; + typedef SkXfermode INHERITED; }; 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 +bool getT(const SkValue& obj, SkValue::Key key, T* ptr) { + auto v = obj.get(key); + return v ? SkFromValue(*v, ptr) : false; +} + +template<> bool SkFromValue(const SkValue& val, float* f) { + REQUIRE(val.type() == SkValue::F32); + *f = val.f32(); + return true; +} + +template<> bool SkFromValue(const SkValue& val, int32_t* x) { + REQUIRE(val.type() == SkValue::S32); + *x = val.s32(); + return true; +} + +template<> bool SkFromValue(const SkValue& val, uint32_t* x) { + REQUIRE(val.type() == SkValue::U32); + *x = val.u32(); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// + +template<> SkValue SkToValue(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(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(const SkXfermode* x) { + return x ? x->asValue() : SkValue::Object(SkValue::DefaultXfermode); +} + +static bool from_value_DefaultXfermode(const SkValue& val, + SkAutoTUnref* dst) { + dst->reset(nullptr); + return true; +} + +static bool from_value_ArithmeticXfermode(const SkValue& val, + SkAutoTUnref* 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* 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* 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* dst) { + uint32_t mode; + REQUIRE(getT(val, SkValueKeys::ProcCoeffXfermode::kMode, &mode)); + dst->reset(SkXfermode::Create((SkXfermode::Mode)mode)); + return true; +} + +template<> bool SkFromValue< SkAutoTUnref >( + const SkValue& val, SkAutoTUnref* 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 +SkValue SkToValue(const T&); + +template +SkValue SkToValue(const T*); + +template +bool SkFromValue(const SkValue&, T*); + +#endif // SkToFromValue_DEFINED -- cgit v1.2.3