1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkDescriptor_DEFINED 11 #define SkDescriptor_DEFINED 12 13 #include "SkChecksum.h" 14 #include "SkTypes.h" 15 16 class SkDescriptor : SkNoncopyable { 17 public: ComputeOverhead(int entryCount)18 static size_t ComputeOverhead(int entryCount) { 19 SkASSERT(entryCount >= 0); 20 return sizeof(SkDescriptor) + entryCount * sizeof(Entry); 21 } 22 Alloc(size_t length)23 static SkDescriptor* Alloc(size_t length) { 24 SkASSERT(SkAlign4(length) == length); 25 SkDescriptor* desc = (SkDescriptor*)sk_malloc_throw(length); 26 return desc; 27 } 28 Free(SkDescriptor * desc)29 static void Free(SkDescriptor* desc) { 30 sk_free(desc); 31 } 32 init()33 void init() { 34 fLength = sizeof(SkDescriptor); 35 fCount = 0; 36 } 37 getLength()38 uint32_t getLength() const { return fLength; } 39 40 void* addEntry(uint32_t tag, size_t length, const void* data = nullptr) { 41 SkASSERT(tag); 42 SkASSERT(SkAlign4(length) == length); 43 SkASSERT(this->findEntry(tag, nullptr) == nullptr); 44 45 Entry* entry = (Entry*)((char*)this + fLength); 46 entry->fTag = tag; 47 entry->fLen = SkToU32(length); 48 if (data) { 49 memcpy(entry + 1, data, length); 50 } 51 52 fCount += 1; 53 fLength = SkToU32(fLength + sizeof(Entry) + length); 54 return (entry + 1); // return its data 55 } 56 computeChecksum()57 void computeChecksum() { 58 fChecksum = SkDescriptor::ComputeChecksum(this); 59 } 60 61 #ifdef SK_DEBUG assertChecksum()62 void assertChecksum() const { 63 SkASSERT(SkDescriptor::ComputeChecksum(this) == fChecksum); 64 } 65 #endif 66 findEntry(uint32_t tag,uint32_t * length)67 const void* findEntry(uint32_t tag, uint32_t* length) const { 68 const Entry* entry = (const Entry*)(this + 1); 69 int count = fCount; 70 71 while (--count >= 0) { 72 if (entry->fTag == tag) { 73 if (length) { 74 *length = entry->fLen; 75 } 76 return entry + 1; 77 } 78 entry = (const Entry*)((const char*)(entry + 1) + entry->fLen); 79 } 80 return nullptr; 81 } 82 copy()83 SkDescriptor* copy() const { 84 SkDescriptor* desc = SkDescriptor::Alloc(fLength); 85 memcpy(desc, this, fLength); 86 return desc; 87 } 88 equals(const SkDescriptor & other)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); 92 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); 100 do { 101 if (*aa++ != *bb++) 102 return false; 103 } while (aa < stop); 104 return true; 105 } 106 getChecksum()107 uint32_t getChecksum() const { return fChecksum; } 108 109 struct Entry { 110 uint32_t fTag; 111 uint32_t fLen; 112 }; 113 114 #ifdef SK_DEBUG getCount()115 uint32_t getCount() const { return fCount; } 116 #endif 117 118 private: 119 uint32_t fChecksum; // must be first 120 uint32_t fLength; // must be second 121 uint32_t fCount; 122 ComputeChecksum(const SkDescriptor * desc)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::Murmur3(ptr, len); 127 } 128 129 // private so no one can create one except our factories SkDescriptor()130 SkDescriptor() {} 131 }; 132 133 #include "SkScalerContext.h" 134 135 class SkAutoDescriptor : SkNoncopyable { 136 public: SkAutoDescriptor()137 SkAutoDescriptor() : fDesc(nullptr) {} SkAutoDescriptor(size_t size)138 SkAutoDescriptor(size_t size) : fDesc(nullptr) { this->reset(size); } SkAutoDescriptor(const SkDescriptor & desc)139 SkAutoDescriptor(const SkDescriptor& desc) : fDesc(nullptr) { 140 size_t size = desc.getLength(); 141 this->reset(size); 142 memcpy(fDesc, &desc, size); 143 } 144 ~SkAutoDescriptor()145 ~SkAutoDescriptor() { this->free(); } 146 reset(size_t size)147 void reset(size_t size) { 148 this->free(); 149 if (size <= sizeof(fStorage)) { 150 fDesc = (SkDescriptor*)(void*)fStorage; 151 } else { 152 fDesc = SkDescriptor::Alloc(size); 153 } 154 } 155 getDesc()156 SkDescriptor* getDesc() const { SkASSERT(fDesc); return fDesc; } 157 private: free()158 void free() { 159 if (fDesc != (SkDescriptor*)(void*)fStorage) { 160 SkDescriptor::Free(fDesc); 161 } 162 } 163 164 enum { 165 kStorageSize = sizeof(SkDescriptor) 166 + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContext::Rec) // for rec 167 + sizeof(SkDescriptor::Entry) + sizeof(void*) // for typeface 168 + 32 // slop for occational small extras 169 }; 170 SkDescriptor* fDesc; 171 uint32_t fStorage[(kStorageSize + 3) >> 2]; 172 }; 173 #define SkAutoDescriptor(...) SK_REQUIRE_LOCAL_VAR(SkAutoDescriptor) 174 175 176 #endif 177