| 1 |
/* |
| 2 |
* Copyright 2018 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 "include/core/SkData.h" |
| 9 |
#include "include/core/SkPath.h" |
| 10 |
#include "include/core/SkPathTypes.h" |
| 11 |
#include "include/core/SkRRect.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkRefCnt.h" |
| 14 |
#include "include/core/SkScalar.h" |
| 15 |
#include "include/private/SkPathRef.h" |
| 16 |
#include "include/private/base/SkAssert.h" |
| 17 |
#include "include/private/base/SkDebug.h" |
| 18 |
#include "include/private/base/SkPoint_impl.h" |
| 19 |
#include "include/private/base/SkTPin.h" |
| 20 |
#include "include/private/base/SkTo.h" |
| 21 |
#include "src/base/SkAutoMalloc.h" |
| 22 |
#include "src/base/SkBuffer.h" |
| 23 |
#include "src/base/SkSafeMath.h" |
| 24 |
#include "src/core/SkPathEnums.h" |
| 25 |
#include "src/core/SkPathPriv.h" |
| 26 |
#include "src/core/SkRRectPriv.h" |
| 27 |
|
| 28 |
#include <cstddef> |
| 29 |
#include <cstdint> |
| 30 |
#include <optional> |
| 31 |
|
| 32 |
enum SerializationOffsets { |
| 33 |
kType_SerializationShift = 28, // requires 4 bits |
| 34 |
kDirection_SerializationShift = 26, // requires 2 bits |
| 35 |
kFillType_SerializationShift = 8, // requires 8 bits |
| 36 |
// low-8-bits are version |
| 37 |
kVersion_SerializationMask = 0xFF, |
| 38 |
}; |
| 39 |
|
| 40 |
enum SerializationVersions { |
| 41 |
// kPathPrivFirstDirection_Version = 1, |
| 42 |
// kPathPrivLastMoveToIndex_Version = 2, |
| 43 |
// kPathPrivTypeEnumVersion = 3, |
| 44 |
kJustPublicData_Version = 4, // introduced Feb/2018 |
| 45 |
kVerbsAreStoredForward_Version = 5, // introduced Sept/2019 |
| 46 |
|
| 47 |
kMin_Version = kJustPublicData_Version, |
| 48 |
kCurrent_Version = kVerbsAreStoredForward_Version |
| 49 |
}; |
| 50 |
|
| 51 |
enum SerializationType { |
| 52 |
kGeneral = 0, |
| 53 |
kRRect = 1 |
| 54 |
}; |
| 55 |
|
| 56 |
static unsigned extract_version(uint32_t packed) { |
| 57 |
return packed & kVersion_SerializationMask; |
| 58 |
} |
| 59 |
|
| 60 |
static SkPathFillType extract_filltype(uint32_t packed) { |
| 61 |
return static_cast<SkPathFillType>((packed >> kFillType_SerializationShift) & 0x3); |
| 62 |
} |
| 63 |
|
| 64 |
static SerializationType extract_serializationtype(uint32_t packed) { |
| 65 |
return static_cast<SerializationType>((packed >> kType_SerializationShift) & 0xF); |
| 66 |
} |
| 67 |
|
| 68 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 69 |
|
| 70 |
size_t SkPath::writeToMemoryAsRRect(void* storage) const { |
| 71 |
SkRRect rrect; |
| 72 |
SkPathDirection firstDir; |
| 73 |
unsigned start; |
| 74 |
|
| 75 |
if (auto oinfo = this->getOvalInfo()) { |
| 76 |
rrect.setOval(oinfo->fBounds); |
| 77 |
firstDir = oinfo->fDirection; |
| 78 |
// Convert to rrect start indices. |
| 79 |
start = oinfo->fStartIndex * 2; |
| 80 |
} else if (auto rinfo = this->getRRectInfo()) { |
| 81 |
rrect = rinfo->fRRect; |
| 82 |
firstDir = rinfo->fDirection; |
| 83 |
start = rinfo->fStartIndex; |
| 84 |
} else { |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
// packed header, rrect, start index. |
| 89 |
const size_t sizeNeeded = sizeof(int32_t) + SkRRect::kSizeInMemory + sizeof(int32_t); |
| 90 |
if (!storage) { |
| 91 |
return sizeNeeded; |
| 92 |
} |
| 93 |
|
| 94 |
int32_t packed = (static_cast<int>(fFillType) << kFillType_SerializationShift) | |
| 95 |
((int)firstDir << kDirection_SerializationShift) | |
| 96 |
(SerializationType::kRRect << kType_SerializationShift) | |
| 97 |
kCurrent_Version; |
| 98 |
|
| 99 |
SkWBuffer buffer(storage); |
| 100 |
buffer.write32(packed); |
| 101 |
SkRRectPriv::WriteToBuffer(rrect, &buffer); |
| 102 |
buffer.write32(SkToS32(start)); |
| 103 |
buffer.padToAlign4(); |
| 104 |
SkASSERT(sizeNeeded == buffer.pos()); |
| 105 |
return buffer.pos(); |
| 106 |
} |
| 107 |
|
| 108 |
size_t SkPath::writeToMemory(void* storage) const { |
| 109 |
SkDEBUGCODE(this->validate();) |
| 110 |
|
| 111 |
if (size_t bytes = this->writeToMemoryAsRRect(storage)) { |
| 112 |
return bytes; |
| 113 |
} |
| 114 |
|
| 115 |
int32_t packed = (static_cast<int>(fFillType) << kFillType_SerializationShift) | |
| 116 |
(SerializationType::kGeneral << kType_SerializationShift) | |
| 117 |
kCurrent_Version; |
| 118 |
|
| 119 |
SkSpan<const SkPoint> points = this->points(); |
| 120 |
SkSpan<const SkPathVerb> verbs = this->verbs(); |
| 121 |
SkSpan<const float> conics = this->conicWeights(); |
| 122 |
|
| 123 |
int32_t pts = SkToS32(points.size()); |
| 124 |
int32_t cnx = SkToS32(conics.size()); |
| 125 |
int32_t vbs = SkToS32(verbs.size()); |
| 126 |
|
| 127 |
SkSafeMath safe; |
| 128 |
size_t size = 4 * sizeof(int32_t); |
| 129 |
size = safe.add(size, safe.mul(pts, sizeof(SkPoint))); |
| 130 |
size = safe.add(size, safe.mul(cnx, sizeof(float))); |
| 131 |
size = safe.add(size, safe.mul(vbs, sizeof(uint8_t))); |
| 132 |
size = safe.alignUp(size, 4); |
| 133 |
if (!safe) { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
if (!storage) { |
| 137 |
return size; |
| 138 |
} |
| 139 |
|
| 140 |
SkWBuffer buffer(storage); |
| 141 |
buffer.write32(packed); |
| 142 |
buffer.write32(pts); |
| 143 |
buffer.write32(cnx); |
| 144 |
buffer.write32(vbs); |
| 145 |
buffer.write(points.data(), points.size_bytes()); |
| 146 |
buffer.write(conics.data(), conics.size_bytes()); |
| 147 |
buffer.write(verbs.data(), verbs.size_bytes()); |
| 148 |
buffer.padToAlign4(); |
| 149 |
|
| 150 |
SkASSERT(buffer.pos() == size); |
| 151 |
return size; |
| 152 |
} |
| 153 |
|
| 154 |
sk_sp<SkData> SkPath::serialize() const { |
| 155 |
size_t size = this->writeToMemory(nullptr); |
| 156 |
sk_sp<SkData> data = SkData::MakeUninitialized(size); |
| 157 |
this->writeToMemory(data->writable_data()); |
| 158 |
return data; |
| 159 |
} |
| 160 |
|
| 161 |
////////////////////////////////////////////////////////////////////////////////////////////////// |
| 162 |
// reading |
| 163 |
|
| 164 |
std::optional<SkPath> read_rrect_path(const void* storage, size_t length, size_t* bytesRead) { |
| 165 |
SkASSERT(bytesRead); |
| 166 |
SkRBuffer buffer(storage, length); |
| 167 |
uint32_t packed; |
| 168 |
if (!buffer.readU32(&packed)) { |
| 169 |
*bytesRead = 0; |
| 170 |
return {}; |
| 171 |
} |
| 172 |
|
| 173 |
SkASSERT(extract_serializationtype(packed) == SerializationType::kRRect); |
| 174 |
|
| 175 |
uint8_t dir = (packed >> kDirection_SerializationShift) & 0x3; |
| 176 |
SkPathFillType fillType = extract_filltype(packed); |
| 177 |
|
| 178 |
SkPathDirection rrectDir; |
| 179 |
switch (dir) { |
| 180 |
case (int)SkPathFirstDirection::kCW: |
| 181 |
rrectDir = SkPathDirection::kCW; |
| 182 |
break; |
| 183 |
case (int)SkPathFirstDirection::kCCW: |
| 184 |
rrectDir = SkPathDirection::kCCW; |
| 185 |
break; |
| 186 |
default: |
| 187 |
*bytesRead = 0; |
| 188 |
return {}; |
| 189 |
} |
| 190 |
|
| 191 |
SkRRect rrect; |
| 192 |
if (!SkRRectPriv::ReadFromBuffer(&buffer, &rrect)) { |
| 193 |
*bytesRead = 0; |
| 194 |
return {}; |
| 195 |
} |
| 196 |
|
| 197 |
int32_t start; |
| 198 |
if (!buffer.readS32(&start) || start != SkTPin(start, 0, 7)) { |
| 199 |
*bytesRead = 0; |
| 200 |
return {}; |
| 201 |
} |
| 202 |
|
| 203 |
SkPath path = SkPath::RRect(rrect, rrectDir, SkToUInt(start)); |
| 204 |
path.setFillType(fillType); |
| 205 |
buffer.skipToAlign4(); |
| 206 |
*bytesRead = buffer.pos(); |
| 207 |
return path; |
| 208 |
} |
| 209 |
|
| 210 |
std::optional<SkPath> SkPath::ReadFromMemory(const void* storage, size_t length, size_t* bytesRead) { |
| 211 |
size_t bytesStorage = 0; |
| 212 |
if (!bytesRead) { |
| 213 |
bytesRead = &bytesStorage; |
| 214 |
} |
| 215 |
SkRBuffer buffer(storage, length); |
| 216 |
uint32_t packed; |
| 217 |
if (!buffer.readU32(&packed)) { |
| 218 |
*bytesRead = 0; |
| 219 |
return {}; |
| 220 |
} |
| 221 |
unsigned version = extract_version(packed); |
| 222 |
|
| 223 |
const bool verbsAreForward = (version == kVerbsAreStoredForward_Version); |
| 224 |
if (!verbsAreForward && version != kJustPublicData_Version) SK_UNLIKELY { |
| 225 |
// Old/unsupported version. |
| 226 |
*bytesRead = 0; |
| 227 |
return {}; |
| 228 |
} |
| 229 |
|
| 230 |
switch (extract_serializationtype(packed)) { |
| 231 |
case SerializationType::kRRect: |
| 232 |
return read_rrect_path(storage, length, bytesRead); |
| 233 |
case SerializationType::kGeneral: |
| 234 |
break; // fall out |
| 235 |
default: |
| 236 |
*bytesRead = 0; |
| 237 |
return {}; |
| 238 |
} |
| 239 |
|
| 240 |
// To minimize the number of reads done a structure with the counts is used. |
| 241 |
struct { |
| 242 |
uint32_t pts, cnx, vbs; |
| 243 |
} counts; |
| 244 |
if (!buffer.read(&counts, sizeof(counts))) { |
| 245 |
*bytesRead = 0; |
| 246 |
return {}; |
| 247 |
} |
| 248 |
|
| 249 |
const SkPoint* points = buffer.skipCount<SkPoint>(counts.pts); |
| 250 |
const SkScalar* conics = buffer.skipCount<SkScalar>(counts.cnx); |
| 251 |
const SkPathVerb* verbs = buffer.skipCount<SkPathVerb>(counts.vbs); |
| 252 |
buffer.skipToAlign4(); |
| 253 |
if (!buffer.isValid()) { |
| 254 |
*bytesRead = 0; |
| 255 |
return {}; |
| 256 |
} |
| 257 |
SkASSERT(buffer.pos() <= length); |
| 258 |
|
| 259 |
if (counts.vbs == 0) { |
| 260 |
if (counts.pts == 0 && counts.cnx == 0) { |
| 261 |
SkPath path(extract_filltype(packed)); |
| 262 |
*bytesRead = buffer.pos(); |
| 263 |
return path; |
| 264 |
} |
| 265 |
// No verbs but points and/or conic weights is a not a valid path. |
| 266 |
*bytesRead = 0; |
| 267 |
return {}; |
| 268 |
} |
| 269 |
|
| 270 |
SkAutoMalloc reversedStorage; |
| 271 |
if (!verbsAreForward) SK_UNLIKELY { |
| 272 |
SkPathVerb* tmpVerbs = (SkPathVerb*)reversedStorage.reset(counts.vbs); |
| 273 |
for (unsigned i = 0; i < counts.vbs; ++i) { |
| 274 |
tmpVerbs[i] = verbs[counts.vbs - i - 1]; |
| 275 |
} |
| 276 |
verbs = tmpVerbs; |
| 277 |
} |
| 278 |
|
| 279 |
*bytesRead = buffer.pos(); |
| 280 |
return SkPath::Raw({points, counts.pts}, |
| 281 |
{verbs, counts.vbs}, |
| 282 |
{conics, counts.cnx}, |
| 283 |
extract_filltype(packed), |
| 284 |
false); |
| 285 |
} |