From bbb6dc80fdfe6ff08cc1abb1a401bd958ced9602 Mon Sep 17 00:00:00 2001 From: mtklein Date: Wed, 27 Jan 2016 13:08:33 -0800 Subject: [PATCH] kill SkValue This is clearly not what we're going to do. TBR=reed@google.com BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1643753002 Review URL: https://codereview.chromium.org/1643753002 --- gyp/core.gypi | 1 - gyp/effects.gypi | 1 - include/core/SkXfermode.h | 4 - include/effects/SkPixelXorXfermode.h | 2 - src/core/SkValue.cpp | 185 --------------------------- src/core/SkValue.h | 116 ----------------- src/core/SkValueKeys.h | 20 --- src/core/SkXfermode.cpp | 10 -- src/core/SkXfermode_proccoeff.h | 2 - src/effects/SkArithmeticMode.cpp | 15 +-- src/effects/SkPixelXorXfermode.cpp | 15 +-- src/effects/SkToFromValue.cpp | 121 ------------------ src/effects/SkToFromValue.h | 21 --- tests/ValueTest.cpp | 111 ---------------- 14 files changed, 4 insertions(+), 620 deletions(-) delete mode 100644 src/core/SkValue.cpp delete mode 100644 src/core/SkValue.h delete mode 100644 src/core/SkValueKeys.h delete mode 100644 src/effects/SkToFromValue.cpp delete mode 100644 src/effects/SkToFromValue.h delete mode 100644 tests/ValueTest.cpp diff --git a/gyp/core.gypi b/gyp/core.gypi index ff1282f4b4..1ecaa3ed22 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -289,7 +289,6 @@ '<(skia_src_path)/core/SkValidatingReadBuffer.cpp', '<(skia_src_path)/core/SkValidatingReadBuffer.h', '<(skia_src_path)/core/SkValidationUtils.h', - '<(skia_src_path)/core/SkValue.cpp', '<(skia_src_path)/core/SkVarAlloc.cpp', '<(skia_src_path)/core/SkVertState.cpp', '<(skia_src_path)/core/SkWriteBuffer.cpp', diff --git a/gyp/effects.gypi b/gyp/effects.gypi index 0ab3cf5fc1..af0935700c 100644 --- a/gyp/effects.gypi +++ b/gyp/effects.gypi @@ -61,7 +61,6 @@ '<(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', diff --git a/include/core/SkXfermode.h b/include/core/SkXfermode.h index 9568b96630..91268ab5f1 100644 --- a/include/core/SkXfermode.h +++ b/include/core/SkXfermode.h @@ -17,7 +17,6 @@ class GrFragmentProcessor; class GrTexture; class GrXPFactory; class SkString; -class SkValue; /** \class SkXfermode * @@ -247,9 +246,6 @@ private: kModeCount = kLastMode + 1 }; - template friend SkValue SkToValue(const T*); - virtual SkValue asValue() const; - typedef SkFlattenable INHERITED; }; diff --git a/include/effects/SkPixelXorXfermode.h b/include/effects/SkPixelXorXfermode.h index b6a160b581..c248de6cc1 100644 --- a/include/effects/SkPixelXorXfermode.h +++ b/include/effects/SkPixelXorXfermode.h @@ -40,8 +40,6 @@ private: SkColor fOpColor; - SkValue asValue() const override; - typedef SkXfermode INHERITED; }; diff --git a/src/core/SkValue.cpp b/src/core/SkValue.cpp deleted file mode 100644 index 2db2796b0c..0000000000 --- a/src/core/SkValue.cpp +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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 -#include - -#include "SkData.h" -#include "SkValue.h" - -class SkValue::Obj { -public: - void set(SkValue::Key k, SkValue&& v) { fMap[k] = std::move(v); } - const SkValue* get(SkValue::Key k) const { - auto it = fMap.find(k); - return it != fMap.end() ? &it->second : nullptr; - } - void foreach(std::function fn) const { - for (const auto& pair : fMap) { - fn(pair.first, pair.second); - } - } - -private: - std::unordered_map fMap; -}; - -class SkValue::Arr { -public: - size_t length() const { return fVec.size(); } - void append(SkValue&& val) { fVec.emplace_back(std::move(val)); } - const SkValue& at(size_t index) const { - SkASSERT(index < fVec.size()); - return fVec[index]; - } - -private: - std::vector fVec; -}; - -SkValue::SkValue() : fType(Null) {} - -SkValue::SkValue(Type type) : fType(type) {} - -SkValue::SkValue(const SkValue& o) { - memcpy(this, &o, sizeof(o)); - if (this->isData()) { - fBytes->ref(); - } else if (this->isObject()) { - fObject = new Obj(*fObject); - } else if (Array == fType) { - fArray = new Arr(*fArray); - } -} - -SkValue::SkValue(SkValue&& o) { - memcpy(this, &o, sizeof(o)); - new (&o) SkValue(); -} - -SkValue& SkValue::operator=(const SkValue& o) { - if (this != &o) { - this->~SkValue(); - new (this) SkValue(o); - } - return *this; -} - -SkValue& SkValue::operator=(SkValue&& o) { - if (this != &o) { - this->~SkValue(); - new (this) SkValue(std::move(o)); - } - return *this; -} - -SkValue::~SkValue() { - if (this->isData()) { - fBytes->unref(); - } else if (this->isObject()) { - delete fObject; - } else if (Array == fType) { - delete fArray; - } -} - -template -SkValue SkValue::FromT(SkValue::Type type, T SkValue::*mp, T t) { - SkValue v(type); - v.*mp = t; - return v; -} - -SkValue SkValue::FromS32(int32_t x) { return FromT(S32, &SkValue::fS32, x); } -SkValue SkValue::FromU32(uint32_t x) { return FromT(U32, &SkValue::fU32, x); } -SkValue SkValue::FromF32(float x) { return FromT(F32, &SkValue::fF32, x); } - -int32_t SkValue::s32() const { SkASSERT(S32 == fType); return fS32; } -uint32_t SkValue::u32() const { SkASSERT(U32 == fType); return fU32; } -float SkValue::f32() const { SkASSERT(F32 == fType); return fF32; } - -SkValue SkValue::FromBytes(SkData* data) { - if (!data) { - return SkValue(); - } - SkValue v(Bytes); - v.fBytes = SkRef(data); - return v; -} - -SkValue SkValue::Object(SkValue::Type t) { - SkValue v(t); - SkASSERT(v.isObject()); - v.fObject = new Obj; - return v; -} - -SkValue SkValue::ValueArray() { - SkValue v(Array); - v.fArray = new Arr; - return v; -} - -SkData* SkValue::bytes() const { - SkASSERT(this->isData()); - return fBytes; -} - -void SkValue::set(SkValue::Key k, SkValue v) { - SkASSERT(this->isObject()); - fObject->set(k, std::move(v)); -} - -const SkValue* SkValue::get(Key k) const { - SkASSERT(this->isObject()); - return fObject->get(k); -} - -void SkValue::foreach(std::function fn) const { - SkASSERT(this->isObject()); - fObject->foreach(fn); -} - -size_t SkValue::length() const { - SkASSERT(Array == fType); - return fArray->length(); -} - -const SkValue& SkValue::at(size_t index) const { - SkASSERT(Array == fType); - return fArray->at(index); -} - -void SkValue::append(SkValue val) { - SkASSERT(Array == fType); - fArray->append(std::move(val)); -} - -template -const T* SkValue::asTs(SkValue::Type t, int* count) const { - SkASSERT(t == fType && this->isData()); - SkASSERT(count); - *count = fBytes->size() / sizeof(T); - return static_cast(fBytes->data()); -} - -const uint16_t* SkValue::u16s(int* c) const { return this->asTs(U16s, c); } -const uint32_t* SkValue::u32s(int* c) const { return this->asTs(U32s, c); } -const float* SkValue::f32s(int* c) const { return this->asTs(F32s, c); } - -template -SkValue SkValue::FromTs(SkValue::Type type, SkData* data) { - SkValue val(type); - val.fBytes = SkRef(data); - SkASSERT(val.isData()); - SkASSERT(0 == (reinterpret_cast(data->bytes()) & (sizeof(T)-1))); - return val; -} - -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); } diff --git a/src/core/SkValue.h b/src/core/SkValue.h deleted file mode 100644 index 5200842228..0000000000 --- a/src/core/SkValue.h +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 SkValue_DEFINED -#define SkValue_DEFINED - -#include "SkTypes.h" -#include - -class SkData; - -class SkValue { -public: - enum Type : uint32_t { - // 0-255 are reserved for built-in SkValue types. - Null, - Byte , S16 , U16 , S32 , U32 , S64 , U64 , F32 , F64 , - Bytes, S16s, U16s, S32s, U32s, S64s, U64s, F32s, F64s, - Array, - - kMaxBuiltin = 0xFF, - // 256-2147483647 may be used by Skia for public Object types. - - Matrix, - - ArithmeticXfermode, - DefaultXfermode, - PixelXorXfermode, - ProcCoeffXfermode, - - kMaxPublicObject = 0x7FFFFFFF, - // 2147483648+ won't be used by Skia. They're open for - // client-specific use, testing, etc. - }; - - // Each Object type may define its own namespace of Key values, - // so there are no pre-defined Keys here. - // - // This is just a reminder that they must fit in a uint32_t, - // and that their namespace is distinct from other uint32_ts (e.g. Type). - typedef uint32_t Key; - - SkValue(); - SkValue(const SkValue&); - SkValue(SkValue&&); - - SkValue& operator=(const SkValue&); - SkValue& operator=(SkValue&&); - - ~SkValue(); - - static SkValue FromS32(int32_t); - static SkValue FromU32(uint32_t); - static SkValue FromF32(float); - static SkValue FromBytes(SkData*); - static SkValue FromU16s(SkData*); - static SkValue FromU32s(SkData*); - static SkValue FromF32s(SkData*); - static SkValue ValueArray(); - static SkValue Object(Type); - - Type type() const { return fType; } - - // These remaining methods may assert they're called on a value of the appropriate type. - - int32_t s32() const; - uint32_t u32() const; - float f32() const; - SkData* bytes() const; - - const uint16_t* u16s(int* count) const; - const uint32_t* u32s(int* count) const; - const float* f32s(int* count) const; - - // Object - void set(Key, SkValue); - const SkValue* get(Key) const; - void foreach(std::function) const; - - // Array - size_t length() const; - const SkValue& at(size_t) const; - void append(SkValue); - -private: - class Obj; - class Arr; - - Type fType; - union { - int32_t fS32; - uint32_t fU32; - float fF32; - SkData* fBytes; - Obj* fObject; - Arr* fArray; - }; - - SkValue(Type); - bool isObject() const { return fType > kMaxBuiltin; } - bool isData() const { - return Bytes == fType - || U16s == fType - || U32s == fType - || F32s == fType; - } - template static SkValue FromT(SkValue::Type, T SkValue::*, T); - template static SkValue FromTs(SkValue::Type, SkData*); - template const T* asTs(SkValue::Type, int*) const; -}; - -#endif // SkValue_DEFINED diff --git a/src/core/SkValueKeys.h b/src/core/SkValueKeys.h deleted file mode 100644 index 3ec3b5a306..0000000000 --- a/src/core/SkValueKeys.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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 ad477e1215..79de322c2d 100644 --- a/src/core/SkXfermode.cpp +++ b/src/core/SkXfermode.cpp @@ -15,8 +15,6 @@ #include "SkReadBuffer.h" #include "SkString.h" #include "SkWriteBuffer.h" -#include "SkValue.h" -#include "SkValueKeys.h" #define SkAlphaMulAlpha(a, b) SkMulDiv255Round(a, b) @@ -765,12 +763,6 @@ 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; @@ -1079,5 +1071,3 @@ 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 c81957c7fc..f86af2df01 100644 --- a/src/core/SkXfermode_proccoeff.h +++ b/src/core/SkXfermode_proccoeff.h @@ -67,8 +67,6 @@ private: friend class SkXfermode; - SkValue asValue() const override; - typedef SkXfermode INHERITED; }; diff --git a/src/effects/SkArithmeticMode.cpp b/src/effects/SkArithmeticMode.cpp index 7d22076558..ff062c3966 100644 --- a/src/effects/SkArithmeticMode.cpp +++ b/src/effects/SkArithmeticMode.cpp @@ -11,8 +11,6 @@ #include "SkWriteBuffer.h" #include "SkString.h" #include "SkUnPreMultiply.h" -#include "SkValue.h" -#include "SkValueKeys.h" #if SK_SUPPORT_GPU #include "SkArithmeticMode_gpu.h" #endif @@ -26,7 +24,7 @@ public: if (SkScalarNearlyZero(k1) && SkScalarNearlyEqual(k2, SK_Scalar1) && SkScalarNearlyZero(k3) && SkScalarNearlyZero(k4)) { return SkXfermode::Create(SkXfermode::kSrc_Mode); - } else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) && + } else if (SkScalarNearlyZero(k1) && SkScalarNearlyZero(k2) && SkScalarNearlyEqual(k3, SK_Scalar1) && SkScalarNearlyZero(k4)) { return SkXfermode::Create(SkXfermode::kDst_Mode); } @@ -64,17 +62,6 @@ private: 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/SkPixelXorXfermode.cpp b/src/effects/SkPixelXorXfermode.cpp index 5ea82e6309..e2ecb0e365 100644 --- a/src/effects/SkPixelXorXfermode.cpp +++ b/src/effects/SkPixelXorXfermode.cpp @@ -10,8 +10,6 @@ #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. @@ -66,8 +64,8 @@ static void add_pixelxor_code(GrGLSLFragmentBuilder* fragBuilder, SkString xorFuncName; // The xor function checks if the three passed in floats (f1, f2, f3) would - // have a bit in the log2(fPowerOf2Divisor)-th position if they were - // represented by an int. It then performs an xor of the 3 bits (using + // have a bit in the log2(fPowerOf2Divisor)-th position if they were + // represented by an int. It then performs an xor of the 3 bits (using // the property that serial xors can be treated as a sum of 0s & 1s mod 2). fragBuilder->emitFunction(kFloat_GrSLType, "xor", @@ -298,7 +296,7 @@ private: void onSetData(const GrGLSLProgramDataManager& pdman, const GrXferProcessor& processor) override { const PixelXorXP& pixelXor = processor.cast(); - pdman.set3f(fOpColorUni, + pdman.set3f(fOpColorUni, SkColorGetR(pixelXor.opColor())/255.0f, SkColorGetG(pixelXor.opColor())/255.0f, SkColorGetB(pixelXor.opColor())/255.0f); @@ -388,10 +386,3 @@ bool SkPixelXorXfermode::asXPFactory(GrXPFactory** xpf) const { } #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 deleted file mode 100644 index cbb265b33f..0000000000 --- a/src/effects/SkToFromValue.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 "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_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::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 deleted file mode 100644 index 1ead4bd3fb..0000000000 --- a/src/effects/SkToFromValue.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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 diff --git a/tests/ValueTest.cpp b/tests/ValueTest.cpp deleted file mode 100644 index 0569a94bb4..0000000000 --- a/tests/ValueTest.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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 "SkData.h" -#include "SkMatrix.h" -#include "SkToFromValue.h" -#include "Test.h" - -static const SkValue::Type example_type = - SkValue::Type(SkValue::kMaxPublicObject + 1); - -enum { kExampleS32, kExampleF32, kExampleU32, kExampleObject, kExampleBytes, - kExampleF32s, kExampleU32s, kExampleU16s, kExampleArray }; - -static const uint16_t aU16[] = { 1, 2, 3, 4 }; -static const uint32_t aU32[] = { 5, 6, 7, 8 }; -static const float aF32[] = { 9.0f, 9.125f, 9.25f }; -static const char hello[] = "HELLO"; - -static SkValue make_example(skiatest::Reporter* r, int level = 4) { - auto value = SkValue::Object(example_type); - value.set(kExampleU32, SkValue::FromU32(1000)); - value.set(kExampleS32, SkValue::FromS32(-123)); - value.set(kExampleF32, SkValue::FromF32(0.5f)); - value.set(kExampleU32, SkValue::FromU32(1234)); - if (level > 0) { - value.set(kExampleObject, make_example(r, 0)); - value.set(kExampleObject, make_example(r, level - 1)); // replace - } - SkAutoTUnref data(SkData::NewWithCString(hello)); - value.set(kExampleBytes, SkValue::FromBytes(data)); - - SkAutoTUnref dataU16(SkData::NewWithCopy(aU16, sizeof(aU16))); - SkAutoTUnref dataU32(SkData::NewWithCopy(aU32, sizeof(aU32))); - SkAutoTUnref dataF32(SkData::NewWithCopy(aF32, sizeof(aF32))); - value.set(kExampleU16s, SkValue::FromU16s(dataU16)); - value.set(kExampleU32s, SkValue::FromU32s(dataU32)); - value.set(kExampleF32s, SkValue::FromF32s(dataF32)); - - auto varray = SkValue::ValueArray(); - varray.append(SkValue::FromU32(99)); - varray.append(SkValue::FromS32(-99)); - value.set(kExampleArray, std::move(varray)); - return value; -} - -DEF_TEST(Value, r) { - SkValue val = make_example(r); - REPORTER_ASSERT(r, example_type == val.type()); - SkValue valCopy = val; - REPORTER_ASSERT(r, example_type == valCopy.type()); - valCopy.set(4321, SkValue()); - auto fn = [&](SkValue::Key k, const SkValue& v){ - int count; - switch (k) { - case kExampleS32: - REPORTER_ASSERT(r, -123 == v.s32()); - break; - case kExampleF32: - REPORTER_ASSERT(r, 0.5f == v.f32()); - break; - case kExampleU32: - REPORTER_ASSERT(r, 1234 == v.u32()); - break; - case kExampleObject: - REPORTER_ASSERT(r, example_type == v.type()); - break; - case kExampleBytes: - REPORTER_ASSERT(r, v.type() == SkValue::Bytes && v.bytes() - && v.bytes()->size() == sizeof(hello)); - break; - case kExampleF32s: - REPORTER_ASSERT(r, v.type() == SkValue::F32s && v.bytes() - && v.bytes()->size() == sizeof(aF32) - && v.f32s(&count) - && count == SK_ARRAY_COUNT(aF32)); - break; - case kExampleU32s: - REPORTER_ASSERT(r, v.type() == SkValue::U32s && v.bytes() - && v.bytes()->size() == sizeof(aU32) - && v.u32s(&count) - && count == SK_ARRAY_COUNT(aU32)); - break; - case kExampleU16s: - REPORTER_ASSERT(r, v.type() == SkValue::U16s && v.bytes() - && v.bytes()->size() == sizeof(aU16) - && v.u16s(&count) - && count == SK_ARRAY_COUNT(aU16)); - break; - case kExampleArray: - REPORTER_ASSERT(r, v.type() == SkValue::Array - && v.length() == 2); - break; - default: - ERRORF(r, "unexpected key"); - } - }; - val.foreach(fn); -} - -DEF_TEST(Value_Matrix, r) { - auto m = SkMatrix::MakeTrans(900.0f, 1000.0f); - auto val = SkToValue(m); - SkMatrix dst; - REPORTER_ASSERT(r, SkFromValue(val, &dst)); - REPORTER_ASSERT(r, dst == m); -}