diff options
author | 2012-07-09 17:40:48 +0000 | |
---|---|---|
committer | 2012-07-09 17:40:48 +0000 | |
commit | fc8581b2d8c63439162c701623bce096088ce6e6 (patch) | |
tree | cf63f8899846e2c94c71af0df46f2b01dc6ab316 /include/core | |
parent | f8affe5adff8e82ca6a3e5b98b0d772e204edc51 (diff) |
revert 4478
git-svn-id: http://skia.googlecode.com/svn/trunk@4479 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core')
-rw-r--r-- | include/core/SkChecksum.h | 58 | ||||
-rw-r--r-- | include/core/SkDescriptor.h | 67 |
2 files changed, 96 insertions, 29 deletions
diff --git a/include/core/SkChecksum.h b/include/core/SkChecksum.h index e66df54aaf..e767670191 100644 --- a/include/core/SkChecksum.h +++ b/include/core/SkChecksum.h @@ -10,6 +10,64 @@ #include "SkTypes.h" +#if !defined(SK_PREFER_32BIT_CHECKSUM) +#define SK_PREFER_32BIT_CHECKSUM 0 +#endif + +enum { + ChecksumRotateBits = 17 +}; + +#define SkCHECKSUM_MASH(CHECKSUM, NEW_CHUNK) \ + CHECKSUM = (((CHECKSUM) >> (sizeof(CHECKSUM)*8 - ChecksumRotateBits)) + \ + ((CHECKSUM) << ChecksumRotateBits)) ^ (NEW_CHUNK); + + +/** + * Compute a 64-bit checksum for a given data block + * + * @param data Memory address of the data block to be processed. Must be + * 32-bit aligned + * @param size Size of the data block in bytes. Must be a multiple of 8. + * @return checksum result + */ +inline uint64_t SkComputeChecksum64(const uint64_t* ptr, size_t size) { + SkASSERT(SkIsAlign8(size)); + // Strict 8-byte alignment is not required on ptr. On current + // CPUs there is no measurable performance difference between 32-bit + // and 64-bit aligned access to uint64_t data + SkASSERT(SkIsAlign4((intptr_t)ptr)); + + const uint64_t* stop = ptr + (size >> 3); + uint64_t result = 0; + while (ptr < stop) { + SkCHECKSUM_MASH(result, *ptr); + ptr++; + } + return result; +} + +/** + * Compute a 32-bit checksum for a given data block + * + * @param data Memory address of the data block to be processed. Must be + * 32-bit aligned. + * @param size Size of the data block in bytes. Must be a multiple of 4. + * @return checksum result + */ +inline uint32_t SkComputeChecksum32(const uint32_t* ptr, size_t size) { + SkASSERT(SkIsAlign4(size)); + SkASSERT(SkIsAlign4((intptr_t)ptr)); + + const uint32_t* stop = ptr + (size >> 2); + uint32_t result = 0; + while (ptr < stop) { + SkCHECKSUM_MASH(result, *ptr); + ptr++; + } + return result; +} + class SkChecksum : SkNoncopyable { private: /* diff --git a/include/core/SkDescriptor.h b/include/core/SkDescriptor.h index 8675fa1e20..00bc9aa924 100644 --- a/include/core/SkDescriptor.h +++ b/include/core/SkDescriptor.h @@ -15,29 +15,34 @@ class SkDescriptor : SkNoncopyable { public: - static size_t ComputeOverhead(int entryCount) { + static size_t ComputeOverhead(int entryCount) + { SkASSERT(entryCount >= 0); return sizeof(SkDescriptor) + entryCount * sizeof(Entry); } - static SkDescriptor* Alloc(size_t length) { + static SkDescriptor* Alloc(size_t length) + { SkASSERT(SkAlign4(length) == length); SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length); return desc; } - static void Free(SkDescriptor* desc) { + static void Free(SkDescriptor* desc) + { sk_free(desc); } - void init() { + void init() + { fLength = sizeof(SkDescriptor); fCount = 0; } uint32_t getLength() const { return fLength; } - void* addEntry(uint32_t tag, uint32_t length, const void* data = NULL) { + void* addEntry(uint32_t tag, uint32_t length, const void* data = NULL) + { SkASSERT(tag); SkASSERT(SkAlign4(length) == length); SkASSERT(this->findEntry(tag, NULL) == NULL); @@ -45,34 +50,37 @@ public: Entry* entry = (Entry*)((char*)this + fLength); entry->fTag = tag; entry->fLen = length; - if (data) { + if (data) memcpy(entry + 1, data, length); - } fCount += 1; fLength += sizeof(Entry) + length; return (entry + 1); // return its data } - void computeChecksum() { + void computeChecksum() + { fChecksum = SkDescriptor::ComputeChecksum(this); } #ifdef SK_DEBUG - void assertChecksum() const { - SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum); + void assertChecksum() const + { + SkASSERT(fChecksum == SkDescriptor::ComputeChecksum(this)); } #endif - const void* findEntry(uint32_t tag, uint32_t* length) const { + const void* findEntry(uint32_t tag, uint32_t* length) const + { const Entry* entry = (const Entry*)(this + 1); int count = fCount; - while (--count >= 0) { - if (entry->fTag == tag) { - if (length) { + while (--count >= 0) + { + if (entry->fTag == tag) + { + if (length) *length = entry->fLen; - } return entry + 1; } entry = (const Entry*)((const char*)(entry + 1) + entry->fLen); @@ -80,13 +88,15 @@ public: return NULL; } - SkDescriptor* copy() const { + SkDescriptor* copy() const + { SkDescriptor* desc = SkDescriptor::Alloc(fLength); memcpy(desc, this, fLength); return desc; } - bool equals(const SkDescriptor& other) const { + bool equals(const SkDescriptor& other) const + { // probe to see if we have a good checksum algo // SkASSERT(a.fChecksum != b.fChecksum || memcmp(&a, &b, a.fLength) == 0); @@ -120,10 +130,11 @@ private: uint32_t fLength; // must be second uint32_t fCount; - static uint32_t ComputeChecksum(const SkDescriptor* desc) { + static uint32_t ComputeChecksum(const SkDescriptor* desc) + { const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field - size_t len = desc->fLength - sizeof(uint32_t); - return SkChecksum::Compute(ptr, len); + const size_t len = desc->fLength-sizeof(uint32_t); + return SkComputeChecksum32(ptr, len); } // private so no one can create one except our factories @@ -134,20 +145,18 @@ private: class SkAutoDescriptor : SkNoncopyable { public: - SkAutoDescriptor(size_t size) { - if (size <= sizeof(fStorage)) { + SkAutoDescriptor(size_t size) + { + if (size <= sizeof(fStorage)) fDesc = (SkDescriptor*)(void*)fStorage; - } else { + else fDesc = SkDescriptor::Alloc(size); - } } - - ~SkAutoDescriptor() { - if (fDesc != (SkDescriptor*)(void*)fStorage) { + ~SkAutoDescriptor() + { + if (fDesc != (SkDescriptor*)(void*)fStorage) SkDescriptor::Free(fDesc); - } } - SkDescriptor* getDesc() const { return fDesc; } private: enum { |