1 /* 2 * Copyright 2021 Google LLC 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/SkCustomMeshPriv.h" 9 10 #ifdef SK_ENABLE_SKSL 11 min_vcount_for_mode(SkCustomMesh::Mode mode)12static int min_vcount_for_mode(SkCustomMesh::Mode mode) { 13 switch (mode) { 14 case SkCustomMesh::Mode::kTriangles: return 3; 15 case SkCustomMesh::Mode::kTriangleStrip: return 3; 16 } 17 SkUNREACHABLE; 18 } 19 SkValidateCustomMesh(const SkCustomMesh & cm)20bool SkValidateCustomMesh(const SkCustomMesh& cm) { 21 if (!cm.spec) { 22 return false; 23 } 24 25 if (!cm.vb) { 26 return false; 27 } 28 29 if (cm.vcount <= 0) { 30 return false; 31 } 32 33 if (cm.indices) { 34 if (cm.icount < min_vcount_for_mode(cm.mode)) { 35 return false; 36 } 37 } else { 38 if (cm.icount > 0) { 39 return false; 40 } 41 } 42 43 return true; 44 } 45 SkCopyCustomMeshVB(const SkCustomMesh & cm)46std::unique_ptr<const char[]> SkCopyCustomMeshVB(const SkCustomMesh& cm) { 47 SkASSERT(cm.spec); 48 size_t size = cm.spec->stride()*cm.vcount; 49 50 auto vb = std::make_unique<char[]>(size); 51 std::memcpy(vb.get(), cm.vb, size); 52 53 return std::move(vb); 54 } 55 SkCopyCustomMeshIB(const SkCustomMesh & cm)56std::unique_ptr<const uint16_t[]> SkCopyCustomMeshIB(const SkCustomMesh& cm) { 57 SkASSERT(cm.spec); 58 SkASSERT(SkToBool(cm.indices) == SkToBool(cm.icount)); 59 if (!cm.indices) { 60 return nullptr; 61 } 62 auto ib = std::make_unique<uint16_t[]>(cm.icount); 63 std::copy_n(cm.indices, cm.icount, ib.get()); 64 65 return std::move(ib); 66 } 67 68 #endif 69