Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Unified Diff: src/core/SkValue.cpp

Issue 1585813004: SkValue: SkXfermode (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-01-15 (Friday) 11:08:28 EST Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkValue.cpp
diff --git a/src/core/SkValue.cpp b/src/core/SkValue.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..befce70d1e733b74401fd6c709c05795a90ee37d
--- /dev/null
+++ b/src/core/SkValue.cpp
@@ -0,0 +1,132 @@
+/*
+ * 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 <unordered_map>
+
+#include "SkData.h"
+#include "SkValue.h"
+
+// WARNING: this implementation is for testing purposes only.
+// TODO(mtklein): replace with something less fragile later.
mtklein 2016/01/15 17:26:20 Goodness no. Copy. Not going to ask again.
hal.canary 2016/01/15 19:44:31 done
+class SkValue::Obj : public SkRefCnt {
+public:
+ std::unordered_map<SkValue::Key, SkValue> fMap;
+ std::atomic<bool> fReadOnly;
+ Obj() : fReadOnly(false) {}
+};
+
+SkValue::SkValue() : fType(Null) {}
+
+SkValue::SkValue(const SkValue& o) {
+ memcpy(this, &o, sizeof(o));
+ if (Bytes == fType) {
+ fBytes->ref();
+ } else if (this->isObject()) {
+ fObject->ref();
+ fObject->fReadOnly = true;
+ }
+}
+
+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 (Bytes == fType) {
+ fBytes->unref();
+ } else if (this->isObject()) {
+ fObject->unref();
+ }
+}
+
+#define FN(NAME, LNAME, TYPE) \
+ SkValue SkValue::From##NAME(TYPE x) { \
+ SkValue v; \
+ v.fType = NAME; \
+ v.f##NAME = x; \
+ return v; \
+ } \
+ TYPE SkValue::LNAME() const { \
+ SkASSERT(NAME == fType); \
+ return f##NAME; \
+ }
+FN(S32, s32, int32_t)
+FN(U32, u32, uint32_t)
+FN(F32, f32, float)
+#undef FN
+
+SkValue SkValue::FromBytes(const void* data, size_t length) {
+ SkValue v;
+ v.fType = Bytes;
+ reinterpret_cast<SkData*&>(v.fBytes) = SkData::NewWithCopy(data, length);
+ return v;
+}
+
+SkValue SkValue::Object(SkValue::Type t) {
+ SkValue v;
+ v.fType = t;
+ SkASSERT(v.isObject());
+ v.fObject = new Obj;
+ return v;
+}
+
+SkValue::Type SkValue::type() const { return fType; }
+
+const void* SkValue::bytes() const {
+ return Bytes == fType ? fBytes->data() : nullptr;
+}
+
+size_t SkValue::count() const {
+ if (Bytes == fType) {
+ return fBytes->size();
+ } else if (this->isObject()) {
+ return fObject->fMap.size();
+ }
+ return 0;
+}
+
+bool SkValue::set(SkValue::Key k, SkValue v) {
+ if (!this->isObject() || fObject->fReadOnly) {
+ return false;
+ }
+ fObject->fMap[k] = std::move(v);
+ return true;
+}
+
+const SkValue* SkValue::get(SkValue::Key k) const {
+ if (!this->isObject()) {
+ return nullptr;
+ }
+ auto it = fObject->fMap.find(k);
+ return it != fObject->fMap.end() ? &it->second : nullptr;
+}
+
+void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
+ if (!this->isObject()) {
+ return;
+ }
+ for (const auto& pair : fObject->fMap) {
+ fn(pair.first, pair.second);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698