Add SkPaint::getHash().
BUG=skia:
Review URL: https://codereview.chromium.org/637583002
diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h
index 66e217a..9f477d8 100644
--- a/include/core/SkPaint.h
+++ b/include/core/SkPaint.h
@@ -62,6 +62,11 @@
return !(a == b);
}
+ /** getHash() is a shallow hash, with the same limitations as operator==.
+ * If operator== returns true for two paints, getHash() returns the same value for each.
+ */
+ uint32_t getHash() const;
+
void flatten(SkWriteBuffer&) const;
void unflatten(SkReadBuffer&);
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index 7ce7fc9..a25ec1d 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -8,6 +8,7 @@
#include "SkPaint.h"
#include "SkAnnotation.h"
#include "SkAutoKern.h"
+#include "SkChecksum.h"
#include "SkColorFilter.h"
#include "SkData.h"
#include "SkDeviceProperties.h"
@@ -2460,3 +2461,11 @@
return false;
}
+uint32_t SkPaint::getHash() const {
+ // We're going to hash 10 pointers and 7 32-bit values, finishing up with fBitfields,
+ // so fBitfields should be 10 pointers and 6 32-bit values from the start.
+ SK_COMPILE_ASSERT(offsetof(SkPaint, fBitfields) == 10 * sizeof(void*) + 6 * sizeof(uint32_t),
+ SkPaint_notPackedTightly);
+ return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(this),
+ offsetof(SkPaint, fBitfields) + sizeof(fBitfields));
+}
diff --git a/tests/PaintTest.cpp b/tests/PaintTest.cpp
index cbb5dad..9b49ec1 100644
--- a/tests/PaintTest.cpp
+++ b/tests/PaintTest.cpp
@@ -343,3 +343,29 @@
ASSERT(paint.getXfermode()->asMode(&paintMode));
ASSERT(otherMode == paintMode);
}
+
+DEF_TEST(Paint_getHash, r) {
+ // Try not to inspect the actual hash values in here.
+ // We might want to change the hash function.
+
+ SkPaint paint;
+ const uint32_t defaultHash = paint.getHash();
+
+ // Check that some arbitrary field affects the hash.
+ paint.setColor(0xFF00FF00);
+ REPORTER_ASSERT(r, paint.getHash() != defaultHash);
+ paint.setColor(SK_ColorBLACK); // Reset to default value.
+ REPORTER_ASSERT(r, paint.getHash() == defaultHash);
+
+ // SkTypeface is the first field we hash, so test it specially.
+ paint.setTypeface(SkTypeface::RefDefault())->unref();
+ REPORTER_ASSERT(r, paint.getHash() != defaultHash);
+ paint.setTypeface(NULL);
+ REPORTER_ASSERT(r, paint.getHash() == defaultHash);
+
+ // This is part of fBitfields, the last field we hash.
+ paint.setHinting(SkPaint::kSlight_Hinting);
+ REPORTER_ASSERT(r, paint.getHash() != defaultHash);
+ paint.setHinting(SkPaint::kNormal_Hinting);
+ REPORTER_ASSERT(r, paint.getHash() == defaultHash);
+}