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