Add SkPaint::getHash().

BUG=skia:

Review URL: https://codereview.chromium.org/637583002
This commit is contained in:
mtklein 2014-10-07 09:26:10 -07:00 committed by Commit bot
parent d909759832
commit fb1fe4f518
3 changed files with 40 additions and 0 deletions

View File

@ -62,6 +62,11 @@ public:
return !(a == b); 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 flatten(SkWriteBuffer&) const;
void unflatten(SkReadBuffer&); void unflatten(SkReadBuffer&);

View File

@ -8,6 +8,7 @@
#include "SkPaint.h" #include "SkPaint.h"
#include "SkAnnotation.h" #include "SkAnnotation.h"
#include "SkAutoKern.h" #include "SkAutoKern.h"
#include "SkChecksum.h"
#include "SkColorFilter.h" #include "SkColorFilter.h"
#include "SkData.h" #include "SkData.h"
#include "SkDeviceProperties.h" #include "SkDeviceProperties.h"
@ -2460,3 +2461,11 @@ bool SkPaint::nothingToDraw() const {
return false; 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));
}

View File

@ -343,3 +343,29 @@ DEF_TEST(Paint_MoreFlattening, r) {
ASSERT(paint.getXfermode()->asMode(&paintMode)); ASSERT(paint.getXfermode()->asMode(&paintMode));
ASSERT(otherMode == 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);
}