| 1 |
/* |
| 2 |
* Copyright 2017 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 |
#include "include/core/SkVertices.h" |
| 8 |
|
| 9 |
#include "include/core/SkTypes.h" |
| 10 |
#include "include/private/base/SkDebug.h" |
| 11 |
#include "include/private/base/SkMalloc.h" |
| 12 |
#include "include/private/base/SkTo.h" |
| 13 |
#include "src/base/SkSafeMath.h" |
| 14 |
#include "src/base/SkVx.h" |
| 15 |
#include "src/core/SkPicturePriv.h" |
| 16 |
#include "src/core/SkReadBuffer.h" |
| 17 |
#include "src/core/SkSafeRange.h" |
| 18 |
#include "src/core/SkVerticesPriv.h" |
| 19 |
#include "src/core/SkWriteBuffer.h" |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <atomic> |
| 23 |
#include <cstdint> |
| 24 |
#include <new> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
static uint32_t next_id() { |
| 28 |
static std::atomic<uint32_t> nextID{1}; |
| 29 |
|
| 30 |
uint32_t id; |
| 31 |
do { |
| 32 |
id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 33 |
} while (id == SK_InvalidGenID); |
| 34 |
return id; |
| 35 |
} |
| 36 |
|
| 37 |
struct SkVertices::Desc { |
| 38 |
VertexMode fMode; |
| 39 |
int fVertexCount, |
| 40 |
fIndexCount; |
| 41 |
bool fHasTexs, |
| 42 |
fHasColors; |
| 43 |
}; |
| 44 |
|
| 45 |
struct SkVertices::Sizes { |
| 46 |
Sizes(const Desc& desc) { |
| 47 |
SkSafeMath safe; |
| 48 |
|
| 49 |
fVSize = safe.mul(desc.fVertexCount, sizeof(SkPoint)); |
| 50 |
fTSize = desc.fHasTexs ? safe.mul(desc.fVertexCount, sizeof(SkPoint)) : 0; |
| 51 |
fCSize = desc.fHasColors ? safe.mul(desc.fVertexCount, sizeof(SkColor)) : 0; |
| 52 |
|
| 53 |
fBuilderTriFanISize = 0; |
| 54 |
fISize = safe.mul(desc.fIndexCount, sizeof(uint16_t)); |
| 55 |
if (kTriangleFan_VertexMode == desc.fMode) { |
| 56 |
int numFanTris = 0; |
| 57 |
if (desc.fIndexCount) { |
| 58 |
fBuilderTriFanISize = fISize; |
| 59 |
numFanTris = desc.fIndexCount - 2; |
| 60 |
} else { |
| 61 |
numFanTris = desc.fVertexCount - 2; |
| 62 |
// By forcing this to become indexed we are adding a constraint to the maximum |
| 63 |
// number of vertices. |
| 64 |
if (desc.fVertexCount > (SkTo<int>(UINT16_MAX) + 1)) { |
| 65 |
sk_bzero(this, sizeof(*this)); |
| 66 |
return; |
| 67 |
} |
| 68 |
} |
| 69 |
if (numFanTris <= 0) { |
| 70 |
sk_bzero(this, sizeof(*this)); |
| 71 |
return; |
| 72 |
} |
| 73 |
fISize = safe.mul(numFanTris, 3 * sizeof(uint16_t)); |
| 74 |
} |
| 75 |
|
| 76 |
fTotal = safe.add(sizeof(SkVertices), |
| 77 |
safe.add(fVSize, |
| 78 |
safe.add(fTSize, |
| 79 |
safe.add(fCSize, |
| 80 |
fISize)))); |
| 81 |
|
| 82 |
if (safe.ok()) { |
| 83 |
fArrays = fVSize + fTSize + fCSize + fISize; // just the sum of the arrays |
| 84 |
} else { |
| 85 |
sk_bzero(this, sizeof(*this)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
bool isValid() const { |
| 90 |
SkASSERT(fTotal >= fVSize); |
| 91 |
return fVSize > 0; |
| 92 |
} |
| 93 |
|
| 94 |
size_t fTotal = 0; // size of entire SkVertices allocation (obj + arrays) |
| 95 |
size_t fArrays; // size of all the data arrays (V + D + T + C + I) |
| 96 |
size_t fVSize; |
| 97 |
size_t fTSize; |
| 98 |
size_t fCSize; |
| 99 |
size_t fISize; |
| 100 |
|
| 101 |
// For indexed tri-fans this is the number of amount of space fo indices needed in the builder |
| 102 |
// before conversion to indexed triangles (or zero if not indexed or not a triangle fan). |
| 103 |
size_t fBuilderTriFanISize; |
| 104 |
}; |
| 105 |
|
| 106 |
SkVertices::Builder::Builder(VertexMode mode, int vertexCount, int indexCount, |
| 107 |
uint32_t builderFlags) { |
| 108 |
bool hasTexs = SkToBool(builderFlags & SkVertices::kHasTexCoords_BuilderFlag); |
| 109 |
bool hasColors = SkToBool(builderFlags & SkVertices::kHasColors_BuilderFlag); |
| 110 |
this->init({mode, vertexCount, indexCount, hasTexs, hasColors}); |
| 111 |
} |
| 112 |
|
| 113 |
SkVertices::Builder::Builder(const Desc& desc) { |
| 114 |
this->init(desc); |
| 115 |
} |
| 116 |
|
| 117 |
void SkVertices::Builder::init(const Desc& desc) { |
| 118 |
Sizes sizes(desc); |
| 119 |
if (!sizes.isValid()) { |
| 120 |
SkASSERT(!this->isValid()); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
void* storage = ::operator new (sizes.fTotal); |
| 125 |
if (sizes.fBuilderTriFanISize) { |
| 126 |
fIntermediateFanIndices.reset(new uint8_t[sizes.fBuilderTriFanISize]); |
| 127 |
} |
| 128 |
|
| 129 |
fVertices.reset(new (storage) SkVertices); |
| 130 |
|
| 131 |
// need to point past the object to store the arrays |
| 132 |
char* ptr = (char*)storage + sizeof(SkVertices); |
| 133 |
|
| 134 |
// return the original ptr (or null), but then advance it by size |
| 135 |
auto advance = [&ptr](size_t size) { |
| 136 |
char* new_ptr = size ? ptr : nullptr; |
| 137 |
ptr += size; |
| 138 |
return new_ptr; |
| 139 |
}; |
| 140 |
|
| 141 |
fVertices->fPositions = (SkPoint*) advance(sizes.fVSize); |
| 142 |
fVertices->fTexs = (SkPoint*) advance(sizes.fTSize); |
| 143 |
fVertices->fColors = (SkColor*) advance(sizes.fCSize); |
| 144 |
fVertices->fIndices = (uint16_t*)advance(sizes.fISize); |
| 145 |
|
| 146 |
fVertices->fVertexCount = desc.fVertexCount; |
| 147 |
fVertices->fIndexCount = desc.fIndexCount; |
| 148 |
fVertices->fMode = desc.fMode; |
| 149 |
|
| 150 |
// We defer assigning fBounds and fUniqueID until detach() is called |
| 151 |
} |
| 152 |
|
| 153 |
sk_sp<SkVertices> SkVertices::Builder::detach() { |
| 154 |
if (fVertices) { |
| 155 |
fVertices->fBounds = SkRect::BoundsOrEmpty({fVertices->fPositions, |
| 156 |
(size_t)fVertices->fVertexCount}); |
| 157 |
if (fVertices->fMode == kTriangleFan_VertexMode) { |
| 158 |
if (fIntermediateFanIndices) { |
| 159 |
SkASSERT(fVertices->fIndexCount); |
| 160 |
auto tempIndices = this->indices(); |
| 161 |
for (int t = 0; t < fVertices->fIndexCount - 2; ++t) { |
| 162 |
fVertices->fIndices[3 * t + 0] = tempIndices[0]; |
| 163 |
fVertices->fIndices[3 * t + 1] = tempIndices[t + 1]; |
| 164 |
fVertices->fIndices[3 * t + 2] = tempIndices[t + 2]; |
| 165 |
} |
| 166 |
fVertices->fIndexCount = 3 * (fVertices->fIndexCount - 2); |
| 167 |
} else { |
| 168 |
SkASSERT(!fVertices->fIndexCount); |
| 169 |
for (int t = 0; t < fVertices->fVertexCount - 2; ++t) { |
| 170 |
fVertices->fIndices[3 * t + 0] = 0; |
| 171 |
fVertices->fIndices[3 * t + 1] = SkToU16(t + 1); |
| 172 |
fVertices->fIndices[3 * t + 2] = SkToU16(t + 2); |
| 173 |
} |
| 174 |
fVertices->fIndexCount = 3 * (fVertices->fVertexCount - 2); |
| 175 |
} |
| 176 |
fVertices->fMode = kTriangles_VertexMode; |
| 177 |
} |
| 178 |
fVertices->fUniqueID = next_id(); |
| 179 |
return std::move(fVertices); // this will null fVertices after the return |
| 180 |
} |
| 181 |
return nullptr; |
| 182 |
} |
| 183 |
|
| 184 |
SkPoint* SkVertices::Builder::positions() { |
| 185 |
return fVertices ? const_cast<SkPoint*>(fVertices->fPositions) : nullptr; |
| 186 |
} |
| 187 |
|
| 188 |
SkPoint* SkVertices::Builder::texCoords() { |
| 189 |
return fVertices ? const_cast<SkPoint*>(fVertices->fTexs) : nullptr; |
| 190 |
} |
| 191 |
|
| 192 |
SkColor* SkVertices::Builder::colors() { |
| 193 |
return fVertices ? const_cast<SkColor*>(fVertices->fColors) : nullptr; |
| 194 |
} |
| 195 |
|
| 196 |
uint16_t* SkVertices::Builder::indices() { |
| 197 |
if (!fVertices) { |
| 198 |
return nullptr; |
| 199 |
} |
| 200 |
if (fIntermediateFanIndices) { |
| 201 |
return reinterpret_cast<uint16_t*>(fIntermediateFanIndices.get()); |
| 202 |
} |
| 203 |
return const_cast<uint16_t*>(fVertices->fIndices); |
| 204 |
} |
| 205 |
|
| 206 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 207 |
|
| 208 |
sk_sp<SkVertices> SkVertices::MakeCopy(VertexMode mode, int vertexCount, |
| 209 |
const SkPoint pos[], const SkPoint texs[], |
| 210 |
const SkColor colors[], |
| 211 |
int indexCount, const uint16_t indices[]) { |
| 212 |
auto desc = Desc{mode, vertexCount, indexCount, !!texs, !!colors}; |
| 213 |
Builder builder(desc); |
| 214 |
if (!builder.isValid()) { |
| 215 |
return nullptr; |
| 216 |
} |
| 217 |
|
| 218 |
Sizes sizes(desc); |
| 219 |
SkASSERT(sizes.isValid()); |
| 220 |
sk_careful_memcpy(builder.positions(), pos, sizes.fVSize); |
| 221 |
sk_careful_memcpy(builder.texCoords(), texs, sizes.fTSize); |
| 222 |
sk_careful_memcpy(builder.colors(), colors, sizes.fCSize); |
| 223 |
|
| 224 |
// The builder can update the number of indices. |
| 225 |
const size_t isize = ((mode == kTriangleFan_VertexMode) ? sizes.fBuilderTriFanISize |
| 226 |
: sizes.fISize), |
| 227 |
icount = isize / sizeof(uint16_t); |
| 228 |
|
| 229 |
// Ensure that indices are valid for the given vertex count. |
| 230 |
SkASSERT(vertexCount > 0); |
| 231 |
const uint16_t max_index = SkToU16(vertexCount - 1); |
| 232 |
|
| 233 |
size_t i = 0; |
| 234 |
for (; i + 8 <= icount; i += 8) { |
| 235 |
const skvx::ushort8 ind8 = skvx::ushort8::Load(indices + i), |
| 236 |
clamped_ind8 = skvx::min(ind8, max_index); |
| 237 |
clamped_ind8.store(builder.indices() + i); |
| 238 |
} |
| 239 |
if (i + 4 <= icount) { |
| 240 |
const skvx::ushort4 ind4 = skvx::ushort4::Load(indices + i), |
| 241 |
clamped_ind4 = skvx::min(ind4, max_index); |
| 242 |
clamped_ind4.store(builder.indices() + i); |
| 243 |
|
| 244 |
i += 4; |
| 245 |
} |
| 246 |
if (i + 2 <= icount) { |
| 247 |
const skvx::ushort2 ind2 = skvx::ushort2::Load(indices + i), |
| 248 |
clamped_ind2 = skvx::min(ind2, max_index); |
| 249 |
clamped_ind2.store(builder.indices() + i); |
| 250 |
|
| 251 |
i += 2; |
| 252 |
} |
| 253 |
if (i < icount) { |
| 254 |
builder.indices()[i] = std::min(indices[i], max_index); |
| 255 |
SkDEBUGCODE(i += 1); |
| 256 |
} |
| 257 |
SkASSERT(i == icount); |
| 258 |
|
| 259 |
return builder.detach(); |
| 260 |
} |
| 261 |
|
| 262 |
size_t SkVertices::approximateSize() const { |
| 263 |
return this->getSizes().fTotal; |
| 264 |
} |
| 265 |
|
| 266 |
SkVertices::Sizes SkVertices::getSizes() const { |
| 267 |
Sizes sizes({fMode, fVertexCount, fIndexCount, !!fTexs, !!fColors}); |
| 268 |
SkASSERT(sizes.isValid()); |
| 269 |
return sizes; |
| 270 |
} |
| 271 |
|
| 272 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 273 |
|
| 274 |
// storage = packed | vertex_count | index_count | attr_count |
| 275 |
// | pos[] | custom[] | texs[] | colors[] | indices[] |
| 276 |
|
| 277 |
#define kMode_Mask 0x0FF |
| 278 |
#define kHasTexs_Mask 0x100 |
| 279 |
#define kHasColors_Mask 0x200 |
| 280 |
|
| 281 |
void SkVerticesPriv::encode(SkWriteBuffer& buffer) const { |
| 282 |
// packed has room for additional flags in the future |
| 283 |
uint32_t packed = static_cast<uint32_t>(fVertices->fMode); |
| 284 |
SkASSERT((packed & ~kMode_Mask) == 0); // our mode fits in the mask bits |
| 285 |
if (fVertices->fTexs) { |
| 286 |
packed |= kHasTexs_Mask; |
| 287 |
} |
| 288 |
if (fVertices->fColors) { |
| 289 |
packed |= kHasColors_Mask; |
| 290 |
} |
| 291 |
|
| 292 |
SkVertices::Sizes sizes = fVertices->getSizes(); |
| 293 |
SkASSERT(!sizes.fBuilderTriFanISize); |
| 294 |
|
| 295 |
// Header |
| 296 |
buffer.writeUInt(packed); |
| 297 |
buffer.writeInt(fVertices->fVertexCount); |
| 298 |
buffer.writeInt(fVertices->fIndexCount); |
| 299 |
|
| 300 |
// Data arrays |
| 301 |
buffer.writeByteArray(fVertices->fPositions, sizes.fVSize); |
| 302 |
buffer.writeByteArray(fVertices->fTexs, sizes.fTSize); |
| 303 |
buffer.writeByteArray(fVertices->fColors, sizes.fCSize); |
| 304 |
// if index-count is odd, we won't be 4-bytes aligned, so we call the pad version |
| 305 |
buffer.writeByteArray(fVertices->fIndices, sizes.fISize); |
| 306 |
} |
| 307 |
|
| 308 |
sk_sp<SkVertices> SkVerticesPriv::Decode(SkReadBuffer& buffer) { |
| 309 |
auto decode = [](SkReadBuffer& buffer) -> sk_sp<SkVertices> { |
| 310 |
SkSafeRange safe; |
| 311 |
bool hasCustomData = buffer.isVersionLT(SkPicturePriv::kVerticesRemoveCustomData_Version); |
| 312 |
|
| 313 |
const uint32_t packed = buffer.readUInt(); |
| 314 |
const int vertexCount = safe.checkGE(buffer.readInt(), 0); |
| 315 |
const int indexCount = safe.checkGE(buffer.readInt(), 0); |
| 316 |
const int attrCount = hasCustomData ? safe.checkGE(buffer.readInt(), 0) : 0; |
| 317 |
const SkVertices::VertexMode mode = safe.checkLE<SkVertices::VertexMode>( |
| 318 |
packed & kMode_Mask, SkVertices::kLast_VertexMode); |
| 319 |
const bool hasTexs = SkToBool(packed & kHasTexs_Mask); |
| 320 |
const bool hasColors = SkToBool(packed & kHasColors_Mask); |
| 321 |
|
| 322 |
// Check that the header fields and buffer are valid. If this is data with the experimental |
| 323 |
// custom attributes feature - we don't support that any more. |
| 324 |
// We also don't support serialized triangle-fan data. We stopped writing that long ago, |
| 325 |
// so it should never appear in valid encoded data. |
| 326 |
if (!safe || !buffer.isValid() || attrCount || |
| 327 |
mode == SkVertices::kTriangleFan_VertexMode) { |
| 328 |
return nullptr; |
| 329 |
} |
| 330 |
|
| 331 |
const SkVertices::Desc desc{mode, vertexCount, indexCount, hasTexs, hasColors}; |
| 332 |
SkVertices::Sizes sizes(desc); |
| 333 |
if (!sizes.isValid() || sizes.fArrays > buffer.available()) { |
| 334 |
return nullptr; |
| 335 |
} |
| 336 |
|
| 337 |
SkVertices::Builder builder(desc); |
| 338 |
if (!builder.isValid()) { |
| 339 |
return nullptr; |
| 340 |
} |
| 341 |
|
| 342 |
buffer.readByteArray(builder.positions(), sizes.fVSize); |
| 343 |
if (hasCustomData) { |
| 344 |
size_t customDataSize = 0; |
| 345 |
buffer.skipByteArray(&customDataSize); |
| 346 |
if (customDataSize != 0) { |
| 347 |
return nullptr; |
| 348 |
} |
| 349 |
} |
| 350 |
buffer.readByteArray(builder.texCoords(), sizes.fTSize); |
| 351 |
buffer.readByteArray(builder.colors(), sizes.fCSize); |
| 352 |
buffer.readByteArray(builder.indices(), sizes.fISize); |
| 353 |
|
| 354 |
if (!buffer.isValid()) { |
| 355 |
return nullptr; |
| 356 |
} |
| 357 |
|
| 358 |
if (indexCount > 0) { |
| 359 |
// validate that the indices are in range |
| 360 |
const uint16_t* indices = builder.indices(); |
| 361 |
for (int i = 0; i < indexCount; ++i) { |
| 362 |
if (indices[i] >= (unsigned)vertexCount) { |
| 363 |
return nullptr; |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
return builder.detach(); |
| 369 |
}; |
| 370 |
|
| 371 |
if (auto verts = decode(buffer)) { |
| 372 |
return verts; |
| 373 |
} |
| 374 |
buffer.validate(false); |
| 375 |
return nullptr; |
| 376 |
} |
| 377 |
|
| 378 |
void SkVertices::operator delete(void* p) { |
| 379 |
::operator delete(p); |
| 380 |
} |