aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/core/SkPaint.h5
-rw-r--r--src/core/SkPaint.cpp9
-rw-r--r--tests/PaintTest.cpp26
3 files changed, 40 insertions, 0 deletions
diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h
index 66e217a570..9f477d867b 100644
--- a/include/core/SkPaint.h
+++ b/include/core/SkPaint.h
@@ -62,6 +62,11 @@ public:
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 7ce7fc9ccd..a25ec1d333 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 @@ bool SkPaint::nothingToDraw() const {
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 cbb5dadcc5..9b49ec1e85 100644
--- a/tests/PaintTest.cpp
+++ b/tests/PaintTest.cpp
@@ -343,3 +343,29 @@ DEF_TEST(Paint_MoreFlattening, r) {
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);
+}