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
This commit is contained in:
halcanary 2016-01-21 14:15:10 -08:00 committed by Commit bot
parent ae47418936
commit 27a6e86fb1
15 changed files with 232 additions and 42 deletions

View File

@ -62,6 +62,7 @@
'<(skia_src_path)/effects/SkTableMaskFilter.cpp',
'<(skia_src_path)/effects/SkTestImageFilters.cpp',
'<(skia_src_path)/effects/SkTileImageFilter.cpp',
'<(skia_src_path)/effects/SkToFromValue.cpp',
'<(skia_src_path)/effects/SkXfermodeImageFilter.cpp',
'<(skia_src_path)/effects/gradients/SkClampRange.cpp',

View File

@ -17,6 +17,7 @@ class GrFragmentProcessor;
class GrTexture;
class GrXPFactory;
class SkString;
class SkValue;
/** \class SkXfermode
*
@ -246,6 +247,9 @@ private:
kModeCount = kLastMode + 1
};
template<typename T> friend SkValue SkToValue(const T*);
virtual SkValue asValue() const;
typedef SkFlattenable INHERITED;
};

View File

@ -39,6 +39,8 @@ private:
unsigned fScale256; // 0..256
SkValue asValue() const override;
typedef SkXfermode INHERITED;
};

View File

@ -31,6 +31,8 @@ private:
SkColor fOpColor;
SkValue asValue() const override;
typedef SkXfermode INHERITED;
};

View File

@ -9,7 +9,6 @@
#include <vector>
#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<uint16_t>(U16s, d); }
SkValue SkValue::FromU32s(SkData* d) { return FromTs<uint32_t>(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<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<float>(const SkValue& val, float* f) {
REQUIRE(val.type() == SkValue::F32);
*f = val.f32();
return true;
}
template<> bool SkFromValue<SkMatrix>(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

View File

@ -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 <typename T> const T* asTs(SkValue::Type, int*) const;
};
template <typename T>
SkValue SkToValue(const T&);
template <typename T>
bool SkFromValue(const SkValue&, T*);
#endif // SkValue_DEFINED

20
src/core/SkValueKeys.h Normal file
View File

@ -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

View File

@ -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(); }

View File

@ -67,6 +67,8 @@ private:
friend class SkXfermode;
SkValue asValue() const override;
typedef SkXfermode INHERITED;
};

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -7,7 +7,7 @@
#include "SkData.h"
#include "SkMatrix.h"
#include "SkValue.h"
#include "SkToFromValue.h"
#include "Test.h"
static const SkValue::Type example_type =