joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 8 | #include "GrDrawVerticesOp.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 9 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 11 | #include "GrInvariantOutput.h" |
| 12 | #include "GrOpFlushState.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 13 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 14 | static sk_sp<GrGeometryProcessor> set_vertex_attributes(bool hasLocalCoords, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 15 | int* colorOffset, |
| 16 | int* texOffset, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 17 | const SkMatrix& viewMatrix, |
| 18 | bool coverageIgnored) { |
| 19 | using namespace GrDefaultGeoProcFactory; |
| 20 | *texOffset = -1; |
| 21 | *colorOffset = -1; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 22 | |
| 23 | Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_Type); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 24 | LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type |
| 25 | : LocalCoords::kUsePosition_Type); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 26 | *colorOffset = sizeof(SkPoint); |
| 27 | if (hasLocalCoords) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 28 | *texOffset = sizeof(SkPoint) + sizeof(GrColor); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 29 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 30 | return GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type), coverage, localCoords, |
| 31 | viewMatrix); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 34 | GrDrawVerticesOp::GrDrawVerticesOp(GrColor color, GrPrimitiveType primitiveType, |
| 35 | const SkMatrix& viewMatrix, const SkPoint* positions, |
| 36 | int vertexCount, const uint16_t* indices, int indexCount, |
| 37 | const GrColor* colors, const SkPoint* localCoords, |
| 38 | const SkRect& bounds) |
| 39 | : INHERITED(ClassID()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 40 | SkASSERT(positions); |
| 41 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 42 | fViewMatrix = viewMatrix; |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 43 | Mesh& mesh = fMeshes.push_back(); |
| 44 | mesh.fColor = color; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 45 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 46 | mesh.fPositions.append(vertexCount, positions); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 47 | if (indices) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 48 | mesh.fIndices.append(indexCount, indices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (colors) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 52 | fVariableColor = true; |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 53 | mesh.fColors.append(vertexCount, colors); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 54 | } else { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 55 | fVariableColor = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (localCoords) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 59 | mesh.fLocalCoords.append(vertexCount, localCoords); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 60 | } |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 61 | fVertexCount = vertexCount; |
| 62 | fIndexCount = indexCount; |
| 63 | fPrimitiveType = primitiveType; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 64 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 65 | IsZeroArea zeroArea; |
| 66 | if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) { |
| 67 | zeroArea = IsZeroArea::kYes; |
| 68 | } else { |
| 69 | zeroArea = IsZeroArea::kNo; |
| 70 | } |
| 71 | this->setBounds(bounds, HasAABloat::kNo, zeroArea); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 74 | void GrDrawVerticesOp::computePipelineOptimizations(GrInitInvariantOutput* color, |
| 75 | GrInitInvariantOutput* coverage, |
| 76 | GrBatchToXPOverrides* overrides) const { |
| 77 | // When this is called there is only one mesh. |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 78 | if (fVariableColor) { |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 79 | color->setUnknownFourComponents(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 80 | } else { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 81 | color->setKnownFourComponents(fMeshes[0].fColor); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 82 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 83 | coverage->setKnownSingleComponent(0xff); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 86 | void GrDrawVerticesOp::initBatchTracker(const GrXPOverridesForBatch& overrides) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 87 | SkASSERT(fMeshes.count() == 1); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 88 | GrColor overrideColor; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 89 | if (overrides.getOverrideColorIfSet(&overrideColor)) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 90 | fMeshes[0].fColor = overrideColor; |
| 91 | fMeshes[0].fColors.reset(); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 92 | fVariableColor = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 93 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 94 | fCoverageIgnored = !overrides.readsCoverage(); |
| 95 | if (!overrides.readsLocalCoords()) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 96 | fMeshes[0].fLocalCoords.reset(); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 97 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 100 | void GrDrawVerticesOp::onPrepareDraws(Target* target) const { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 101 | bool hasLocalCoords = !fMeshes[0].fLocalCoords.isEmpty(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 102 | int colorOffset = -1, texOffset = -1; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 103 | sk_sp<GrGeometryProcessor> gp(set_vertex_attributes(hasLocalCoords, &colorOffset, &texOffset, |
| 104 | fViewMatrix, fCoverageIgnored)); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 105 | size_t vertexStride = gp->getVertexStride(); |
| 106 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 107 | SkASSERT(vertexStride == |
| 108 | sizeof(SkPoint) + (hasLocalCoords ? sizeof(SkPoint) : 0) + sizeof(GrColor)); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 109 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 110 | int instanceCount = fMeshes.count(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 111 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 112 | const GrBuffer* vertexBuffer; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 113 | int firstVertex; |
| 114 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 115 | void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 116 | |
| 117 | if (!verts) { |
| 118 | SkDebugf("Could not allocate vertices\n"); |
| 119 | return; |
| 120 | } |
| 121 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 122 | const GrBuffer* indexBuffer = nullptr; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 123 | int firstIndex = 0; |
| 124 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | uint16_t* indices = nullptr; |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 126 | if (!fMeshes[0].fIndices.isEmpty()) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 127 | indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 128 | |
| 129 | if (!indices) { |
| 130 | SkDebugf("Could not allocate indices\n"); |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | int indexOffset = 0; |
| 136 | int vertexOffset = 0; |
| 137 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 138 | const Mesh& mesh = fMeshes[i]; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 139 | |
| 140 | // TODO we can actually cache this interleaved and then just memcopy |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 141 | if (indices) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 142 | for (int j = 0; j < mesh.fIndices.count(); ++j, ++indexOffset) { |
| 143 | *(indices + indexOffset) = mesh.fIndices[j] + vertexOffset; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 147 | for (int j = 0; j < mesh.fPositions.count(); ++j) { |
| 148 | *((SkPoint*)verts) = mesh.fPositions[j]; |
| 149 | if (mesh.fColors.isEmpty()) { |
| 150 | *(GrColor*)((intptr_t)verts + colorOffset) = mesh.fColor; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 151 | } else { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 152 | *(GrColor*)((intptr_t)verts + colorOffset) = mesh.fColors[j]; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 153 | } |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 154 | if (hasLocalCoords) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 155 | *(SkPoint*)((intptr_t)verts + texOffset) = mesh.fLocalCoords[j]; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 156 | } |
| 157 | verts = (void*)((intptr_t)verts + vertexStride); |
| 158 | vertexOffset++; |
| 159 | } |
| 160 | } |
| 161 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 162 | GrMesh mesh; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 163 | if (indices) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 164 | mesh.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex, firstIndex, |
| 165 | fVertexCount, fIndexCount); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 166 | |
| 167 | } else { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 168 | mesh.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexCount); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 169 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 170 | target->draw(gp.get(), mesh); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 173 | bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
| 174 | GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 175 | |
| 176 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 177 | that->bounds(), caps)) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 181 | if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) { |
| 182 | return false; |
| 183 | } |
| 184 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 185 | // We currently use a uniform viewmatrix for this op. |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 186 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 190 | if (fMeshes[0].fIndices.isEmpty() != that->fMeshes[0].fIndices.isEmpty()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 194 | if (fMeshes[0].fLocalCoords.isEmpty() != that->fMeshes[0].fLocalCoords.isEmpty()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 198 | if (!fVariableColor) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 199 | if (that->fVariableColor || that->fMeshes[0].fColor != fMeshes[0].fColor) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 200 | fVariableColor = true; |
| 201 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 202 | } |
| 203 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 204 | fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin()); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 205 | fVertexCount += that->fVertexCount; |
| 206 | fIndexCount += that->fIndexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 207 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 208 | this->joinBounds(*that); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 209 | return true; |
| 210 | } |
| 211 | |
| 212 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 213 | |
| 214 | #ifdef GR_TEST_UTILS |
| 215 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 216 | #include "GrDrawOpTest.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 217 | |
| 218 | static uint32_t seed_vertices(GrPrimitiveType type) { |
| 219 | switch (type) { |
| 220 | case kTriangles_GrPrimitiveType: |
| 221 | case kTriangleStrip_GrPrimitiveType: |
| 222 | case kTriangleFan_GrPrimitiveType: |
| 223 | return 3; |
| 224 | case kPoints_GrPrimitiveType: |
| 225 | return 1; |
| 226 | case kLines_GrPrimitiveType: |
| 227 | case kLineStrip_GrPrimitiveType: |
| 228 | return 2; |
| 229 | } |
| 230 | SkFAIL("Incomplete switch\n"); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static uint32_t primitive_vertices(GrPrimitiveType type) { |
| 235 | switch (type) { |
| 236 | case kTriangles_GrPrimitiveType: |
| 237 | return 3; |
| 238 | case kLines_GrPrimitiveType: |
| 239 | return 2; |
| 240 | case kTriangleStrip_GrPrimitiveType: |
| 241 | case kTriangleFan_GrPrimitiveType: |
| 242 | case kPoints_GrPrimitiveType: |
| 243 | case kLineStrip_GrPrimitiveType: |
| 244 | return 1; |
| 245 | } |
| 246 | SkFAIL("Incomplete switch\n"); |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) { |
| 251 | SkPoint p; |
| 252 | p.fX = random->nextRangeScalar(min, max); |
| 253 | p.fY = random->nextRangeScalar(min, max); |
| 254 | return p; |
| 255 | } |
| 256 | |
| 257 | static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 258 | SkRandom* random, SkTArray<SkPoint>* positions, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 259 | SkTArray<SkPoint>* texCoords, bool hasTexCoords, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 260 | SkTArray<GrColor>* colors, bool hasColors, SkTArray<uint16_t>* indices, |
| 261 | bool hasIndices) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 262 | for (uint32_t v = 0; v < count; v++) { |
| 263 | positions->push_back(random_point(random, min, max)); |
| 264 | if (hasTexCoords) { |
| 265 | texCoords->push_back(random_point(random, min, max)); |
| 266 | } |
| 267 | if (hasColors) { |
| 268 | colors->push_back(GrRandomColor(random)); |
| 269 | } |
| 270 | if (hasIndices) { |
| 271 | SkASSERT(maxVertex <= SK_MaxU16); |
| 272 | indices->push_back(random->nextULessThan((uint16_t)maxVertex)); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 277 | DRAW_OP_TEST_DEFINE(VerticesOp) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 278 | GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1)); |
| 279 | uint32_t primitiveCount = random->nextRangeU(1, 100); |
| 280 | |
| 281 | // TODO make 'sensible' indexbuffers |
| 282 | SkTArray<SkPoint> positions; |
| 283 | SkTArray<SkPoint> texCoords; |
| 284 | SkTArray<GrColor> colors; |
| 285 | SkTArray<uint16_t> indices; |
| 286 | |
| 287 | bool hasTexCoords = random->nextBool(); |
| 288 | bool hasIndices = random->nextBool(); |
| 289 | bool hasColors = random->nextBool(); |
| 290 | |
| 291 | uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type); |
| 292 | |
| 293 | static const SkScalar kMinVertExtent = -100.f; |
| 294 | static const SkScalar kMaxVertExtent = 100.f; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 295 | randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random, |
| 296 | &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 297 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 298 | |
| 299 | for (uint32_t i = 1; i < primitiveCount; i++) { |
| 300 | randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 301 | random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 302 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 306 | SkRect bounds; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 307 | SkDEBUGCODE(bool result =) bounds.setBoundsCheck(positions.begin(), vertexCount); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 308 | SkASSERT(result); |
| 309 | |
| 310 | viewMatrix.mapRect(&bounds); |
| 311 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 312 | GrColor color = GrRandomColor(random); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 313 | return GrDrawVerticesOp::Make(color, type, viewMatrix, positions.begin(), vertexCount, |
| 314 | indices.begin(), hasIndices ? vertexCount : 0, colors.begin(), |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 315 | texCoords.begin(), bounds); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | #endif |