• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = fCount;
90     size_t lengthRemaining = this->fLength;
91     if (lengthRemaining < sizeof(SkDescriptor)) {
92         return false;
93     }
94     lengthRemaining -= sizeof(SkDescriptor);
95     size_t offset = sizeof(SkDescriptor);
96 
97     while (lengthRemaining > 0 && count > 0) {
98         if (lengthRemaining < sizeof(Entry)) {
99             return false;
100         }
101         lengthRemaining -= sizeof(Entry);
102 
103         const Entry* entry = (const Entry*)(reinterpret_cast<const char*>(this) + offset);
104 
105         if (lengthRemaining < entry->fLen) {
106             return false;
107         }
108         lengthRemaining -= entry->fLen;
109 
110         // rec tags are always a known size.
111         if (entry->fTag == kRec_SkDescriptorTag && entry->fLen != sizeof(SkScalerContextRec)) {
112             return false;
113         }
114 
115         offset += sizeof(Entry) + entry->fLen;
116         count--;
117     }
118     return lengthRemaining == 0 && count == 0;
119 }
120 
121 SkAutoDescriptor::SkAutoDescriptor() = default;
SkAutoDescriptor(size_t size)122 SkAutoDescriptor::SkAutoDescriptor(size_t size) { this->reset(size); }
SkAutoDescriptor(const SkDescriptor & desc)123 SkAutoDescriptor::SkAutoDescriptor(const SkDescriptor& desc) { this->reset(desc); }
SkAutoDescriptor(const SkAutoDescriptor & ad)124 SkAutoDescriptor::SkAutoDescriptor(const SkAutoDescriptor& ad) {
125     this->reset(*ad.getDesc());
126 }
operator =(const SkAutoDescriptor & ad)127 SkAutoDescriptor& SkAutoDescriptor::operator=(const SkAutoDescriptor& ad) {
128     this->reset(*ad.getDesc());
129     return *this;
130 }
131 
~SkAutoDescriptor()132 SkAutoDescriptor::~SkAutoDescriptor() { this->free(); }
133 
reset(size_t size)134 void SkAutoDescriptor::reset(size_t size) {
135     this->free();
136     if (size <= sizeof(fStorage)) {
137         fDesc = reinterpret_cast<SkDescriptor*>(&fStorage);
138     } else {
139         fDesc = SkDescriptor::Alloc(size).release();
140     }
141 }
142 
reset(const SkDescriptor & desc)143 void SkAutoDescriptor::reset(const SkDescriptor& desc) {
144     size_t size = desc.getLength();
145     this->reset(size);
146     memcpy(fDesc, &desc, size);
147 }
148 
free()149 void SkAutoDescriptor::free() {
150     if (fDesc != (SkDescriptor*)&fStorage) {
151         delete fDesc;
152     }
153 }
154 
155 
156