3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkDescriptor_DEFINED
11 #define SkDescriptor_DEFINED
13 #include "SkChecksum.h"
16 class SkDescriptor : SkNoncopyable {
18 static size_t ComputeOverhead(int entryCount) {
19 SkASSERT(entryCount >= 0);
20 return sizeof(SkDescriptor) + entryCount * sizeof(Entry);
23 static SkDescriptor* Alloc(size_t length) {
24 SkASSERT(SkAlign4(length) == length);
25 SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length);
29 static void Free(SkDescriptor* desc) {
34 fLength = sizeof(SkDescriptor);
38 uint32_t getLength() const { return fLength; }
40 void* addEntry(uint32_t tag, size_t length, const void* data = NULL) {
42 SkASSERT(SkAlign4(length) == length);
43 SkASSERT(this->findEntry(tag, NULL) == NULL);
45 Entry* entry = (Entry*)((char*)this + fLength);
47 entry->fLen = SkToU32(length);
49 memcpy(entry + 1, data, length);
53 fLength += sizeof(Entry) + length;
54 return (entry + 1); // return its data
57 void computeChecksum() {
58 fChecksum = SkDescriptor::ComputeChecksum(this);
62 void assertChecksum() const {
63 SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum);
67 const void* findEntry(uint32_t tag, uint32_t* length) const {
68 const Entry* entry = (const Entry*)(this + 1);
71 while (--count >= 0) {
72 if (entry->fTag == tag) {
74 *length = entry->fLen;
78 entry = (const Entry*)((const char*)(entry + 1) + entry->fLen);
83 SkDescriptor* copy() const {
84 SkDescriptor* desc = SkDescriptor::Alloc(fLength);
85 memcpy(desc, this, fLength);
89 bool equals(const SkDescriptor& other) const {
90 // probe to see if we have a good checksum algo
91 // SkASSERT(a.fChecksum != b.fChecksum || memcmp(&a, &b, a.fLength) == 0);
93 // the first value we should look at is the checksum, so this loop
94 // should terminate early if they descriptors are different.
95 // NOTE: if we wrote a sentinel value at the end of each, we chould
96 // remove the aa < stop test in the loop...
97 const uint32_t* aa = (const uint32_t*)this;
98 const uint32_t* bb = (const uint32_t*)&other;
99 const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength);
107 uint32_t getChecksum() const { return fChecksum; }
115 uint32_t getCount() const { return fCount; }
119 uint32_t fChecksum; // must be first
120 uint32_t fLength; // must be second
123 static uint32_t ComputeChecksum(const SkDescriptor* desc) {
124 const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field
125 size_t len = desc->fLength - sizeof(uint32_t);
126 return SkChecksum::Compute(ptr, len);
129 // private so no one can create one except our factories
133 #include "SkScalerContext.h"
135 class SkAutoDescriptor : SkNoncopyable {
137 SkAutoDescriptor(size_t size) {
138 if (size <= sizeof(fStorage)) {
139 fDesc = (SkDescriptor*)(void*)fStorage;
141 fDesc = SkDescriptor::Alloc(size);
145 ~SkAutoDescriptor() {
146 if (fDesc != (SkDescriptor*)(void*)fStorage) {
147 SkDescriptor::Free(fDesc);
151 SkDescriptor* getDesc() const { return fDesc; }
154 kStorageSize = sizeof(SkDescriptor)
155 + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext::Rec) // for rec
156 + sizeof(SkDescriptor::Entry) + sizeof(void*) // for typeface
157 + 32 // slop for occational small extras
160 uint32_t fStorage[(kStorageSize + 3) >> 2];
162 #define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor)