1 /*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/core/SkDescriptor.h"
9
10 #include <new>
11
12 #include "include/core/SkTypes.h"
13 #include "include/private/SkTo.h"
14 #include "src/core/SkOpts.h"
15
Alloc(size_t length)16 std::unique_ptr<SkDescriptor> SkDescriptor::Alloc(size_t length) {
17 SkASSERT(SkAlign4(length) == length);
18 return std::unique_ptr<SkDescriptor>(static_cast<SkDescriptor*>(::operator new (length)));
19 }
20
operator delete(void * p)21 void SkDescriptor::operator delete(void* p) { ::operator delete(p); }
22
addEntry(uint32_t tag,size_t length,const void * data)23 void* SkDescriptor::addEntry(uint32_t tag, size_t length, const void* data) {
24 SkASSERT(tag);
25 SkASSERT(SkAlign4(length) == length);
26 SkASSERT(this->findEntry(tag, nullptr) == nullptr);
27
28 Entry* entry = (Entry*)((char*)this + fLength);
29 entry->fTag = tag;
30 entry->fLen = SkToU32(length);
31 if (data) {
32 memcpy(entry + 1, data, length);
33 }
34
35 fCount += 1;
36 fLength = SkToU32(fLength + sizeof(Entry) + length);
37 return (entry + 1); // return its data
38 }
39
computeChecksum()40 void SkDescriptor::computeChecksum() {
41 fChecksum = SkDescriptor::ComputeChecksum(this);
42 }
43
findEntry(uint32_t tag,uint32_t * length) const44 const void* SkDescriptor::findEntry(uint32_t tag, uint32_t* length) const {
45 const Entry* entry = (const Entry*)(this + 1);
46 int count = fCount;
47
48 while (--count >= 0) {
49 if (entry->fTag == tag) {
50 if (length) {
51 *length = entry->fLen;
52 }
53 return entry + 1;
54 }
55 entry = (const Entry*)((const char*)(entry + 1) + entry->fLen);
56 }
57 return nullptr;
58 }
59
copy() const60 std::unique_ptr<SkDescriptor> SkDescriptor::copy() const {
61 std::unique_ptr<SkDescriptor> desc = SkDescriptor::Alloc(fLength);
62 memcpy(desc.get(), this, fLength);
63 return desc;
64 }
65
operator ==(const SkDescriptor & other) const66 bool SkDescriptor::operator==(const SkDescriptor& other) const {
67
68 // the first value we should look at is the checksum, so this loop
69 // should terminate early if they descriptors are different.
70 // NOTE: if we wrote a sentinel value at the end of each, we could
71 // remove the aa < stop test in the loop...
72 const uint32_t* aa = (const uint32_t*)this;
73 const uint32_t* bb = (const uint32_t*)&other;
74 const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength);
75 do {
76 if (*aa++ != *bb++)
77 return false;
78 } while (aa < stop);
79 return true;
80 }
81
ComputeChecksum(const SkDescriptor * desc)82 uint32_t SkDescriptor::ComputeChecksum(const SkDescriptor* desc) {
83 const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field
84 size_t len = desc->fLength - sizeof(uint32_t);
85 return SkOpts::hash(ptr, len);
86 }
87
isValid() const88 bool SkDescriptor::isValid() const {
89 uint32_t count = 0;
90 size_t offset = sizeof(SkDescriptor);
91
92 while (offset < fLength) {
93 const Entry* entry = (const Entry*)(reinterpret_cast<const char*>(this) + offset);
94 // rec tags are always a known size.
95 if (entry->fTag == kRec_SkDescriptorTag && entry->fLen != sizeof(SkScalerContextRec)) {
96 return false;
97 }
98 offset += sizeof(Entry) + entry->fLen;
99 count++;
100 }
101 return offset <= fLength && count == fCount;
102 }
103
104 SkAutoDescriptor::SkAutoDescriptor() = default;
SkAutoDescriptor(size_t size)105 SkAutoDescriptor::SkAutoDescriptor(size_t size) { this->reset(size); }
SkAutoDescriptor(const SkDescriptor & desc)106 SkAutoDescriptor::SkAutoDescriptor(const SkDescriptor& desc) { this->reset(desc); }
SkAutoDescriptor(const SkAutoDescriptor & ad)107 SkAutoDescriptor::SkAutoDescriptor(const SkAutoDescriptor& ad) {
108 this->reset(*ad.getDesc());
109 }
operator =(const SkAutoDescriptor & ad)110 SkAutoDescriptor& SkAutoDescriptor::operator=(const SkAutoDescriptor& ad) {
111 this->reset(*ad.getDesc());
112 return *this;
113 }
114
~SkAutoDescriptor()115 SkAutoDescriptor::~SkAutoDescriptor() { this->free(); }
116
reset(size_t size)117 void SkAutoDescriptor::reset(size_t size) {
118 this->free();
119 if (size <= sizeof(fStorage)) {
120 fDesc = reinterpret_cast<SkDescriptor*>(&fStorage);
121 } else {
122 fDesc = SkDescriptor::Alloc(size).release();
123 }
124 }
125
reset(const SkDescriptor & desc)126 void SkAutoDescriptor::reset(const SkDescriptor& desc) {
127 size_t size = desc.getLength();
128 this->reset(size);
129 memcpy(fDesc, &desc, size);
130 }
131
free()132 void SkAutoDescriptor::free() {
133 if (fDesc != (SkDescriptor*)&fStorage) {
134 delete fDesc;
135 }
136 }
137
138
139