1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <cmath>
#include "SkBuffer.h"
#include "SkData.h"
#include "SkMath.h"
#include "SkPathPriv.h"
#include "SkPathRef.h"
#include "SkRRect.h"
#include "SkSafeMath.h"
enum SerializationOffsets {
kType_SerializationShift = 28, // requires 4 bits
kDirection_SerializationShift = 26, // requires 2 bits
kFillType_SerializationShift = 8, // requires 8 bits
// low-8-bits are version
kVersion_SerializationMask = 0xFF,
};
enum SerializationVersions {
// kPathPrivFirstDirection_Version = 1,
kPathPrivLastMoveToIndex_Version = 2,
kPathPrivTypeEnumVersion = 3,
kJustPublicData_Version = 4, // introduced Feb/2018
kCurrent_Version = kJustPublicData_Version
};
enum SerializationType {
kGeneral = 0,
kRRect = 1
};
static unsigned extract_version(uint32_t packed) {
return packed & kVersion_SerializationMask;
}
static SkPath::FillType extract_filltype(uint32_t packed) {
return static_cast<SkPath::FillType>((packed >> kFillType_SerializationShift) & 0x3);
}
static SerializationType extract_serializationtype(uint32_t packed) {
return static_cast<SerializationType>((packed >> kType_SerializationShift) & 0xF);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
size_t SkPath::writeToMemoryAsRRect(void* storage) const {
SkRect oval;
SkRRect rrect;
bool isCCW;
unsigned start;
if (fPathRef->isOval(&oval, &isCCW, &start)) {
rrect.setOval(oval);
// Convert to rrect start indices.
start *= 2;
} else if (!fPathRef->isRRect(&rrect, &isCCW, &start)) {
return 0;
}
// packed header, rrect, start index.
const size_t sizeNeeded = sizeof(int32_t) + SkRRect::kSizeInMemory + sizeof(int32_t);
if (!storage) {
return sizeNeeded;
}
int firstDir = isCCW ? SkPathPriv::kCCW_FirstDirection : SkPathPriv::kCW_FirstDirection;
int32_t packed = (fFillType << kFillType_SerializationShift) |
(firstDir << kDirection_SerializationShift) |
(SerializationType::kRRect << kType_SerializationShift) |
kCurrent_Version;
SkWBuffer buffer(storage);
buffer.write32(packed);
rrect.writeToBuffer(&buffer);
buffer.write32(SkToS32(start));
buffer.padToAlign4();
SkASSERT(sizeNeeded == buffer.pos());
return buffer.pos();
}
size_t SkPath::writeToMemory(void* storage) const {
SkDEBUGCODE(this->validate();)
if (size_t bytes = this->writeToMemoryAsRRect(storage)) {
return bytes;
}
int32_t packed = (fFillType << kFillType_SerializationShift) |
(SerializationType::kGeneral << kType_SerializationShift) |
kCurrent_Version;
int32_t pts = fPathRef->countPoints();
int32_t cnx = fPathRef->countWeights();
int32_t vbs = fPathRef->countVerbs();
SkSafeMath safe;
size_t size = 4 * sizeof(int32_t);
size = safe.add(size, safe.mul(pts, sizeof(SkPoint)));
size = safe.add(size, safe.mul(cnx, sizeof(SkScalar)));
size = safe.add(size, safe.mul(vbs, sizeof(uint8_t)));
size = safe.alignUp(size, 4);
if (!safe) {
return 0;
}
if (!storage) {
return size;
}
SkWBuffer buffer(storage);
buffer.write32(packed);
buffer.write32(pts);
buffer.write32(cnx);
buffer.write32(vbs);
buffer.write(fPathRef->points(), pts * sizeof(SkPoint));
buffer.write(fPathRef->conicWeights(), cnx * sizeof(SkScalar));
buffer.write(fPathRef->verbsMemBegin(), vbs * sizeof(uint8_t));
buffer.padToAlign4();
SkASSERT(buffer.pos() == size);
return size;
}
sk_sp<SkData> SkPath::serialize() const {
size_t size = this->writeToMemory(nullptr);
sk_sp<SkData> data = SkData::MakeUninitialized(size);
this->writeToMemory(data->writable_data());
return data;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// reading
size_t SkPath::readFromMemory(const void* storage, size_t length) {
SkRBuffer buffer(storage, length);
uint32_t packed;
if (!buffer.readU32(&packed)) {
return 0;
}
unsigned version = extract_version(packed);
if (version <= kPathPrivTypeEnumVersion) {
return this->readFromMemory_LE3(storage, length);
}
if (version == kJustPublicData_Version) {
return this->readFromMemory_EQ4(storage, length);
}
return 0;
}
size_t SkPath::readAsRRect(const void* storage, size_t length) {
SkRBuffer buffer(storage, length);
uint32_t packed;
if (!buffer.readU32(&packed)) {
return 0;
}
SkASSERT(extract_serializationtype(packed) == SerializationType::kRRect);
uint8_t dir = (packed >> kDirection_SerializationShift) & 0x3;
FillType fillType = extract_filltype(packed);
Direction rrectDir;
SkRRect rrect;
int32_t start;
switch (dir) {
case SkPathPriv::kCW_FirstDirection:
rrectDir = kCW_Direction;
break;
case SkPathPriv::kCCW_FirstDirection:
rrectDir = kCCW_Direction;
break;
default:
return 0;
}
if (!rrect.readFromBuffer(&buffer)) {
return 0;
}
if (!buffer.readS32(&start) || start != SkTPin(start, 0, 7)) {
return 0;
}
this->reset();
this->addRRect(rrect, rrectDir, SkToUInt(start));
this->setFillType(fillType);
buffer.skipToAlign4();
return buffer.pos();
}
size_t SkPath::readFromMemory_EQ4(const void* storage, size_t length) {
SkRBuffer buffer(storage, length);
uint32_t packed;
if (!buffer.readU32(&packed)) {
return 0;
}
SkASSERT(extract_version(packed) == 4);
switch (extract_serializationtype(packed)) {
case SerializationType::kRRect:
return this->readAsRRect(storage, length);
case SerializationType::kGeneral:
break; // fall through
default:
return 0;
}
int32_t pts, cnx, vbs;
if (!buffer.readS32(&pts) || !buffer.readS32(&cnx) || !buffer.readS32(&vbs)) {
return 0;
}
const SkPoint* points = buffer.skipCount<SkPoint>(pts);
const SkScalar* conics = buffer.skipCount<SkScalar>(cnx);
const uint8_t* verbs = buffer.skipCount<uint8_t>(vbs);
buffer.skipToAlign4();
if (!buffer.isValid()) {
return 0;
}
SkASSERT(buffer.pos() <= length);
#define CHECK_POINTS_CONICS(p, c) \
do { \
if (p && ((pts -= p) < 0)) { \
return 0; \
} \
if (c && ((cnx -= c) < 0)) { \
return 0; \
} \
} while (0)
SkPath tmp;
tmp.setFillType(extract_filltype(packed));
tmp.incReserve(pts);
for (int i = vbs - 1; i >= 0; --i) {
switch (verbs[i]) {
case kMove_Verb:
CHECK_POINTS_CONICS(1, 0);
tmp.moveTo(*points++);
break;
case kLine_Verb:
CHECK_POINTS_CONICS(1, 0);
tmp.lineTo(*points++);
break;
case kQuad_Verb:
CHECK_POINTS_CONICS(2, 0);
tmp.quadTo(points[0], points[1]);
points += 2;
break;
case kConic_Verb:
CHECK_POINTS_CONICS(2, 1);
tmp.conicTo(points[0], points[1], *conics++);
points += 2;
break;
case kCubic_Verb:
CHECK_POINTS_CONICS(3, 0);
tmp.cubicTo(points[0], points[1], points[2]);
points += 3;
break;
case kClose_Verb:
tmp.close();
break;
default:
return 0; // bad verb
}
}
#undef CHECK_POINTS_CONICS
if (pts || cnx) {
return 0; // leftover points and/or conics
}
*this = std::move(tmp);
return buffer.pos();
}
size_t SkPath::readFromMemory_LE3(const void* storage, size_t length) {
SkRBuffer buffer(storage, length);
int32_t packed;
if (!buffer.readS32(&packed)) {
return 0;
}
unsigned version = extract_version(packed);
SkASSERT(version <= 3);
FillType fillType = extract_filltype(packed);
if (version >= kPathPrivTypeEnumVersion) {
switch (extract_serializationtype(packed)) {
case SerializationType::kRRect:
return this->readAsRRect(storage, length);
case SerializationType::kGeneral:
// Fall through to general path deserialization
break;
default:
return 0;
}
}
if (version >= kPathPrivLastMoveToIndex_Version && !buffer.readS32(&fLastMoveToIndex)) {
return 0;
}
// These are written into the serialized data but we no longer use them in the deserialized
// path. If convexity is corrupted it may cause the GPU backend to make incorrect
// rendering choices, possibly crashing. We set them to unknown so that they'll be recomputed if
// requested.
fConvexity = kUnknown_Convexity;
fFirstDirection = SkPathPriv::kUnknown_FirstDirection;
fFillType = fillType;
fIsVolatile = 0;
SkPathRef* pathRef = SkPathRef::CreateFromBuffer(&buffer);
if (!pathRef) {
return 0;
}
fPathRef.reset(pathRef);
SkDEBUGCODE(this->validate();)
buffer.skipToAlign4();
return buffer.pos();
}
|