Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/core/SkDescriptor.cpp
Line
Count
Source (jump to first uncovered line)
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
16
32.8k
std::unique_ptr<SkDescriptor> SkDescriptor::Alloc(size_t length) {
17
32.8k
    SkASSERT(SkAlign4(length) == length);
18
32.8k
    void* allocation = ::operator new (length);
19
32.8k
    return std::unique_ptr<SkDescriptor>(new (allocation) SkDescriptor{});
20
32.8k
}
21
22
30.7k
void SkDescriptor::operator delete(void* p) { ::operator delete(p); }
23
0
void* SkDescriptor::operator new(size_t) {
24
0
    SK_ABORT("Descriptors are created with placement new.");
25
0
}
26
27
9.92M
void* SkDescriptor::addEntry(uint32_t tag, size_t length, const void* data) {
28
9.92M
    SkASSERT(tag);
29
9.92M
    SkASSERT(SkAlign4(length) == length);
30
9.92M
    SkASSERT(this->findEntry(tag, nullptr) == nullptr);
31
32
9.92M
    Entry* entry = (Entry*)((char*)this + fLength);
33
9.92M
    entry->fTag = tag;
34
9.92M
    entry->fLen = SkToU32(length);
35
9.92M
    if (data) {
36
9.90M
        memcpy(entry + 1, data, length);
37
9.90M
    }
38
39
9.92M
    fCount += 1;
40
9.92M
    fLength = SkToU32(fLength + sizeof(Entry) + length);
41
9.92M
    return (entry + 1); // return its data
42
9.92M
}
43
44
9.90M
void SkDescriptor::computeChecksum() {
45
9.90M
    fChecksum = SkDescriptor::ComputeChecksum(this);
46
9.90M
}
47
48
17.6k
const void* SkDescriptor::findEntry(uint32_t tag, uint32_t* length) const {
49
17.6k
    const Entry* entry = (const Entry*)(this + 1);
50
17.6k
    int          count = fCount;
51
52
18.0k
    while (--count >= 0) {
53
17.9k
        if (entry->fTag == tag) {
54
17.5k
            if (length) {
55
6
                *length = entry->fLen;
56
6
            }
57
17.5k
            return entry + 1;
58
17.5k
        }
59
400
        entry = (const Entry*)((const char*)(entry + 1) + entry->fLen);
60
400
    }
61
86
    return nullptr;
62
17.6k
}
63
64
0
std::unique_ptr<SkDescriptor> SkDescriptor::copy() const {
65
0
    std::unique_ptr<SkDescriptor> desc = SkDescriptor::Alloc(fLength);
66
0
    memcpy(desc.get(), this, fLength);
67
0
    return desc;
68
0
}
69
70
10.2M
bool SkDescriptor::operator==(const SkDescriptor& other) const {
71
72
    // the first value we should look at is the checksum, so this loop
73
    // should terminate early if they descriptors are different.
74
    // NOTE: if we wrote a sentinel value at the end of each, we could
75
    //       remove the aa < stop test in the loop...
76
10.2M
    const uint32_t* aa = (const uint32_t*)this;
77
10.2M
    const uint32_t* bb = (const uint32_t*)&other;
78
10.2M
    const uint32_t* stop = (const uint32_t*)((const char*)aa + fLength);
79
180M
    do {
80
180M
        if (*aa++ != *bb++)
81
369k
            return false;
82
180M
    } while (aa < stop);
83
9.89M
    return true;
84
10.2M
}
85
86
0
SkString SkDescriptor::dumpRec() const {
87
0
    const SkScalerContextRec* rec = static_cast<const SkScalerContextRec*>(
88
0
            this->findEntry(kRec_SkDescriptorTag, nullptr));
89
90
0
    SkString result;
91
0
    result.appendf("    Checksum: %x\n", fChecksum);
92
0
    if (rec != nullptr) {
93
0
        result.append(rec->dump());
94
0
    }
95
0
    return result;
96
0
}
97
98
9.90M
uint32_t SkDescriptor::ComputeChecksum(const SkDescriptor* desc) {
99
9.90M
    const uint32_t* ptr = (const uint32_t*)desc + 1; // skip the checksum field
100
9.90M
    size_t len = desc->fLength - sizeof(uint32_t);
101
9.90M
    return SkOpts::hash(ptr, len);
102
9.90M
}
103
104
391
bool SkDescriptor::isValid() const {
105
391
    uint32_t count = fCount;
106
391
    size_t lengthRemaining = this->fLength;
107
391
    if (lengthRemaining < sizeof(SkDescriptor)) {
108
8
        return false;
109
8
    }
110
383
    lengthRemaining -= sizeof(SkDescriptor);
111
383
    size_t offset = sizeof(SkDescriptor);
112
113
1.40k
    while (lengthRemaining > 0 && count > 0) {
114
1.13k
        if (lengthRemaining < sizeof(Entry)) {
115
50
            return false;
116
50
        }
117
1.08k
        lengthRemaining -= sizeof(Entry);
118
119
1.08k
        const Entry* entry = (const Entry*)(reinterpret_cast<const char*>(this) + offset);
120
121
1.08k
        if (lengthRemaining < entry->fLen) {
122
46
            return false;
123
46
        }
124
1.04k
        lengthRemaining -= entry->fLen;
125
126
        // rec tags are always a known size.
127
1.04k
        if (entry->fTag == kRec_SkDescriptorTag && entry->fLen != sizeof(SkScalerContextRec)) {
128
17
            return false;
129
17
        }
130
131
1.02k
        offset += sizeof(Entry) + entry->fLen;
132
1.02k
        count--;
133
1.02k
    }
134
270
    return lengthRemaining == 0 && count == 0;
135
383
}
136
137
9.90M
SkAutoDescriptor::SkAutoDescriptor() = default;
138
0
SkAutoDescriptor::SkAutoDescriptor(size_t size) { this->reset(size); }
139
16.2k
SkAutoDescriptor::SkAutoDescriptor(const SkDescriptor& desc) { this->reset(desc); }
140
4.74k
SkAutoDescriptor::SkAutoDescriptor(const SkAutoDescriptor& that) {
141
4.74k
    this->reset(*that.getDesc());
142
4.74k
}
143
0
SkAutoDescriptor& SkAutoDescriptor::operator=(const SkAutoDescriptor& that) {
144
0
    this->reset(*that.getDesc());
145
0
    return *this;
146
0
}
147
132
SkAutoDescriptor::SkAutoDescriptor(SkAutoDescriptor&& that) {
148
132
    if (that.fDesc == (SkDescriptor*)&that.fStorage) {
149
132
        this->reset(*that.getDesc());
150
0
    } else {
151
0
        fDesc = that.fDesc;
152
0
        that.fDesc = nullptr;
153
0
    }
154
132
}
155
0
SkAutoDescriptor& SkAutoDescriptor::operator=(SkAutoDescriptor&& that) {
156
0
    if (that.fDesc == (SkDescriptor*)&that.fStorage) {
157
0
        this->reset(*that.getDesc());
158
0
    } else {
159
0
        this->free();
160
0
        fDesc = that.fDesc;
161
0
        that.fDesc = nullptr;
162
0
    }
163
0
    return *this;
164
0
}
165
166
9.91M
SkAutoDescriptor::~SkAutoDescriptor() { this->free(); }
167
168
9.92M
void SkAutoDescriptor::reset(size_t size) {
169
9.92M
    this->free();
170
9.92M
    if (size <= sizeof(fStorage)) {
171
9.88M
        fDesc = new (&fStorage) SkDescriptor{};
172
32.8k
    } else {
173
32.8k
        fDesc = SkDescriptor::Alloc(size).release();
174
32.8k
    }
175
9.92M
}
176
177
21.1k
void SkAutoDescriptor::reset(const SkDescriptor& desc) {
178
21.1k
    size_t size = desc.getLength();
179
21.1k
    this->reset(size);
180
21.1k
    memcpy(fDesc, &desc, size);
181
21.1k
}
182
183
19.8M
void SkAutoDescriptor::free() {
184
19.8M
    if (fDesc == (SkDescriptor*)&fStorage) {
185
9.88M
        fDesc->~SkDescriptor();
186
9.95M
    } else {
187
9.95M
        delete fDesc;
188
9.95M
    }
189
19.8M
}
190
191