michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkDescriptor_DEFINED michael@0: #define SkDescriptor_DEFINED michael@0: michael@0: #include "SkChecksum.h" michael@0: #include "SkTypes.h" michael@0: michael@0: class SkDescriptor : SkNoncopyable { michael@0: public: michael@0: static size_t ComputeOverhead(int entryCount) { michael@0: SkASSERT(entryCount >= 0); michael@0: return sizeof(SkDescriptor) + entryCount * sizeof(Entry); michael@0: } michael@0: michael@0: static SkDescriptor* Alloc(size_t length) { michael@0: SkASSERT(SkAlign4(length) == length); michael@0: SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length); michael@0: return desc; michael@0: } michael@0: michael@0: static void Free(SkDescriptor* desc) { michael@0: sk_free(desc); michael@0: } michael@0: michael@0: void init() { michael@0: fLength = sizeof(SkDescriptor); michael@0: fCount = 0; michael@0: } michael@0: michael@0: uint32_t getLength() const { return fLength; } michael@0: michael@0: void* addEntry(uint32_t tag, uint32_t length, const void* data = NULL) { michael@0: SkASSERT(tag); michael@0: SkASSERT(SkAlign4(length) == length); michael@0: SkASSERT(this->findEntry(tag, NULL) == NULL); michael@0: michael@0: Entry* entry = (Entry*)((char*)this + fLength); michael@0: entry->fTag = tag; michael@0: entry->fLen = length; michael@0: if (data) { michael@0: memcpy(entry + 1, data, length); michael@0: } michael@0: michael@0: fCount += 1; michael@0: fLength += sizeof(Entry) + length; michael@0: return (entry + 1); // return its data michael@0: } michael@0: michael@0: void computeChecksum() { michael@0: fChecksum = SkDescriptor::ComputeChecksum(this); michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void assertChecksum() const { michael@0: SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum); michael@0: } michael@0: #endif michael@0: michael@0: const void* findEntry(uint32_t tag, uint32_t* length) const { michael@0: const Entry* entry = (const Entry*)(this + 1); michael@0: int count = fCount; michael@0: michael@0: while (--count >= 0) { michael@0: if (entry->fTag == tag) { michael@0: if (length) { michael@0: *length = entry->fLen; michael@0: } michael@0: return entry + 1; michael@0: } michael@0: entry = (const Entry*)((const char*)(entry + 1) + entry->fLen); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: SkDescriptor* copy() const { michael@0: SkDescriptor* desc = SkDescriptor::Alloc(fLength); michael@0: memcpy(desc, this, fLength); michael@0: return desc; michael@0: } michael@0: michael@0: bool equals(const SkDescriptor& other) const { michael@0: // probe to see if we have a good checksum algo michael@0: // SkASSERT(a.fChecksum != b.fChecksum || memcmp(&a, &b, a.fLength) == 0); michael@0: michael@0: // the first value we should look at is the checksum, so this loop michael@0: // should terminate early if they descriptors are different. michael@0: // NOTE: if we wrote a sentinel value at the end of each, we chould michael@0: // remove the aa < stop test in the loop... michael@0: const uint32_t* aa = (const uint32_t*)this; michael@0: const uint32_t* bb = (const uint32_t*)&other; michael@0: const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength); michael@0: do { michael@0: if (*aa++ != *bb++) michael@0: return false; michael@0: } while (aa < stop); michael@0: return true; michael@0: } michael@0: michael@0: uint32_t getChecksum() const { return fChecksum; } michael@0: michael@0: struct Entry { michael@0: uint32_t fTag; michael@0: uint32_t fLen; michael@0: }; michael@0: michael@0: #ifdef SK_DEBUG michael@0: uint32_t getCount() const { return fCount; } michael@0: #endif michael@0: michael@0: private: michael@0: uint32_t fChecksum; // must be first michael@0: uint32_t fLength; // must be second michael@0: uint32_t fCount; michael@0: michael@0: static uint32_t ComputeChecksum(const SkDescriptor* desc) { michael@0: const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field michael@0: size_t len = desc->fLength - sizeof(uint32_t); michael@0: return SkChecksum::Compute(ptr, len); michael@0: } michael@0: michael@0: // private so no one can create one except our factories michael@0: SkDescriptor() {} michael@0: }; michael@0: michael@0: #include "SkScalerContext.h" michael@0: michael@0: class SkAutoDescriptor : SkNoncopyable { michael@0: public: michael@0: SkAutoDescriptor(size_t size) { michael@0: if (size <= sizeof(fStorage)) { michael@0: fDesc = (SkDescriptor*)(void*)fStorage; michael@0: } else { michael@0: fDesc = SkDescriptor::Alloc(size); michael@0: } michael@0: } michael@0: michael@0: ~SkAutoDescriptor() { michael@0: if (fDesc != (SkDescriptor*)(void*)fStorage) { michael@0: SkDescriptor::Free(fDesc); michael@0: } michael@0: } michael@0: michael@0: SkDescriptor* getDesc() const { return fDesc; } michael@0: private: michael@0: enum { michael@0: kStorageSize = sizeof(SkDescriptor) michael@0: + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext::Rec) // for rec michael@0: + sizeof(SkDescriptor::Entry) + sizeof(void*) // for typeface michael@0: + 32 // slop for occational small extras michael@0: }; michael@0: SkDescriptor* fDesc; michael@0: uint32_t fStorage[(kStorageSize + 3) >> 2]; michael@0: }; michael@0: #define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor) michael@0: michael@0: michael@0: #endif