blob: c968611152bec936f738ae517ef274a791ffe679 [file] [log] [blame]
joshualitt2771b562015-08-07 12:46:26 -07001/*
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 Salomonfc527d22016-12-14 21:07:01 -05008#include "GrDrawVerticesOp.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -04009#include "GrCaps.h"
joshualitt2771b562015-08-07 12:46:26 -070010#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
Brian Osman3b655982017-03-07 16:58:08 -050012#include "SkGr.h"
joshualitt2771b562015-08-07 12:46:26 -070013
Robert Phillips7c525e62018-06-12 10:11:12 -040014std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrContext* context,
15 GrPaint&& paint,
Brian Salomonc2f42542017-07-12 14:11:22 -040016 sk_sp<SkVertices> vertices,
17 const SkMatrix& viewMatrix,
18 GrAAType aaType,
Brian Salomonc2f42542017-07-12 14:11:22 -040019 sk_sp<GrColorSpaceXform> colorSpaceXform,
20 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -050021 SkASSERT(vertices);
Brian Osmanae0c50c2017-05-25 16:56:34 -040022 GrPrimitiveType primType = overridePrimType ? *overridePrimType
23 : SkVertexModeToGrPrimitiveType(vertices->mode());
Robert Phillips7c525e62018-06-12 10:11:12 -040024 return Helper::FactoryHelper<GrDrawVerticesOp>(context, std::move(paint), std::move(vertices),
Brian Osman08a50e02018-06-15 15:06:48 -040025 primType, aaType, std::move(colorSpaceXform),
26 viewMatrix);
Brian Salomon199fb872017-02-06 09:41:10 -050027}
28
Brian Salomonc2f42542017-07-12 14:11:22 -040029GrDrawVerticesOp::GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, GrColor color,
30 sk_sp<SkVertices> vertices, GrPrimitiveType primitiveType,
Brian Osman08a50e02018-06-15 15:06:48 -040031 GrAAType aaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Osmanfa6d8652017-05-31 09:37:27 -040032 const SkMatrix& viewMatrix)
33 : INHERITED(ClassID())
Brian Salomonc2f42542017-07-12 14:11:22 -040034 , fHelper(helperArgs, aaType)
Brian Osmanfa6d8652017-05-31 09:37:27 -040035 , fPrimitiveType(primitiveType)
36 , fColorSpaceXform(std::move(colorSpaceXform)) {
Brian Salomon199fb872017-02-06 09:41:10 -050037 SkASSERT(vertices);
38
39 fVertexCount = vertices->vertexCount();
40 fIndexCount = vertices->indexCount();
Brian Osmanae0c50c2017-05-25 16:56:34 -040041 fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor
42 : ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -070043
bsalomond92b4192016-06-30 07:59:23 -070044 Mesh& mesh = fMeshes.push_back();
45 mesh.fColor = color;
Brian Salomon3f363692017-02-02 21:05:19 -050046 mesh.fViewMatrix = viewMatrix;
Brian Salomon199fb872017-02-06 09:41:10 -050047 mesh.fVertices = std::move(vertices);
Brian Osman8a030552017-05-23 15:03:18 -040048 mesh.fIgnoreTexCoords = false;
49 mesh.fIgnoreColors = false;
joshualitt2771b562015-08-07 12:46:26 -070050
Brian Salomon199fb872017-02-06 09:41:10 -050051 fFlags = 0;
52 if (mesh.hasPerVertexColors()) {
53 fFlags |= kRequiresPerVertexColors_Flag;
joshualitt2771b562015-08-07 12:46:26 -070054 }
Brian Salomon199fb872017-02-06 09:41:10 -050055 if (mesh.hasExplicitLocalCoords()) {
56 fFlags |= kAnyMeshHasExplicitLocalCoords;
joshualitt2771b562015-08-07 12:46:26 -070057 }
58
bsalomon88cf17d2016-07-08 06:40:56 -070059 IsZeroArea zeroArea;
Chris Dalton3809bab2017-06-13 10:55:06 -060060 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon88cf17d2016-07-08 06:40:56 -070061 zeroArea = IsZeroArea::kYes;
62 } else {
63 zeroArea = IsZeroArea::kNo;
64 }
Brian Salomon199fb872017-02-06 09:41:10 -050065 this->setTransformedBounds(mesh.fVertices->bounds(), viewMatrix, HasAABloat::kNo, zeroArea);
joshualitt2771b562015-08-07 12:46:26 -070066}
67
Brian Salomonc2f42542017-07-12 14:11:22 -040068SkString GrDrawVerticesOp::dumpInfo() const {
69 SkString string;
70 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
71 fMeshes.count(), fVertexCount, fIndexCount);
72 string += fHelper.dumpInfo();
73 string += INHERITED::dumpInfo();
74 return string;
joshualitt2771b562015-08-07 12:46:26 -070075}
76
Brian Salomonc2f42542017-07-12 14:11:22 -040077GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const {
78 return fHelper.fixedFunctionFlags();
79}
80
81GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040082 const GrAppliedClip* clip,
83 GrPixelConfigIsClamped dstIsClamped) {
Brian Salomonc2f42542017-07-12 14:11:22 -040084 GrProcessorAnalysisColor gpColor;
85 if (this->requiresPerVertexColors()) {
86 gpColor.setToUnknown();
87 } else {
88 gpColor.setToConstant(fMeshes.front().fColor);
89 }
Brian Osman9a725dd2017-09-20 09:53:22 -040090 auto result = fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
91 GrProcessorAnalysisCoverage::kNone, &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -040092 if (gpColor.isConstant(&fMeshes.front().fColor)) {
93 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -050094 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -040095 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -070096 }
Brian Salomonc2f42542017-07-12 14:11:22 -040097 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -040098 fMeshes[0].fIgnoreTexCoords = true;
Brian Salomon199fb872017-02-06 09:41:10 -050099 fFlags &= ~kAnyMeshHasExplicitLocalCoords;
bsalomon14eaaa62015-09-24 07:01:26 -0700100 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400101 return result;
joshualitt2771b562015-08-07 12:46:26 -0700102}
103
Brian Salomon199fb872017-02-06 09:41:10 -0500104sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(bool* hasColorAttribute,
105 bool* hasLocalCoordAttribute) const {
106 using namespace GrDefaultGeoProcFactory;
107 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400108 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500109 // If we have multiple view matrices we will transform the positions into device space. We
110 // must then also provide untransformed positions as local coords.
111 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
112 *hasLocalCoordAttribute = true;
113 localCoordsType = LocalCoords::kHasExplicit_Type;
114 } else {
115 *hasLocalCoordAttribute = false;
116 localCoordsType = LocalCoords::kUsePosition_Type;
117 }
118 } else {
119 localCoordsType = LocalCoords::kUnused_Type;
120 *hasLocalCoordAttribute = false;
121 }
122
123 Color color(fMeshes[0].fColor);
124 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400125 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
126 color.fType = Color::kPremulGrColorAttribute_Type;
127 } else {
128 color.fType = Color::kUnpremulSkColorAttribute_Type;
129 color.fColorSpaceXform = fColorSpaceXform;
130 }
Brian Salomon199fb872017-02-06 09:41:10 -0500131 *hasColorAttribute = true;
132 } else {
133 *hasColorAttribute = false;
134 };
135 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
136 return GrDefaultGeoProcFactory::Make(color, Coverage::kSolid_Type, localCoordsType, vm);
137}
138
Brian Salomon91326c32017-08-09 16:02:19 -0400139void GrDrawVerticesOp::onPrepareDraws(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500140 bool hasColorAttribute;
141 bool hasLocalCoordsAttribute;
142 sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700143 size_t vertexStride = gp->getVertexStride();
144
Brian Salomon199fb872017-02-06 09:41:10 -0500145 SkASSERT(vertexStride == sizeof(SkPoint) + (hasColorAttribute ? sizeof(uint32_t) : 0) +
146 (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0));
joshualitt2771b562015-08-07 12:46:26 -0700147
bsalomond92b4192016-06-30 07:59:23 -0700148 int instanceCount = fMeshes.count();
joshualitt2771b562015-08-07 12:46:26 -0700149
cdalton397536c2016-03-25 12:15:03 -0700150 const GrBuffer* vertexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700151 int firstVertex;
152
bsalomon14eaaa62015-09-24 07:01:26 -0700153 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700154
155 if (!verts) {
156 SkDebugf("Could not allocate vertices\n");
157 return;
158 }
159
cdalton397536c2016-03-25 12:15:03 -0700160 const GrBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700161 int firstIndex = 0;
162
halcanary96fcdcc2015-08-27 07:41:13 -0700163 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500164 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700165 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700166
167 if (!indices) {
168 SkDebugf("Could not allocate indices\n");
169 return;
170 }
171 }
172
joshualitt2771b562015-08-07 12:46:26 -0700173 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500174 // We have a fast case below for uploading the vertex data when the matrix is translate
175 // only and there are colors but not local coords.
176 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700177 for (int i = 0; i < instanceCount; i++) {
bsalomond92b4192016-06-30 07:59:23 -0700178 const Mesh& mesh = fMeshes[i];
bsalomon14eaaa62015-09-24 07:01:26 -0700179 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500180 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500181 for (int j = 0; j < indexCount; ++j) {
182 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700183 }
184 }
Brian Salomon199fb872017-02-06 09:41:10 -0500185 int vertexCount = mesh.fVertices->vertexCount();
186 const SkPoint* positions = mesh.fVertices->positions();
187 const SkColor* colors = mesh.fVertices->colors();
188 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500189 bool fastMesh = (!this->hasMultipleViewMatrices() ||
190 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
191 mesh.hasPerVertexColors();
192 if (fastAttrs && fastMesh) {
193 struct V {
194 SkPoint fPos;
195 uint32_t fColor;
196 };
197 SkASSERT(sizeof(V) == vertexStride);
198 V* v = (V*)verts;
199 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500200 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500201 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700202 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500203 for (int j = 0; j < vertexCount; ++j) {
204 Sk2f p = Sk2f::Load(positions++) + t;
205 p.store(&v[j].fPos);
206 v[j].fColor = colors[j];
207 }
208 verts = v + vertexCount;
209 } else {
210 static constexpr size_t kColorOffset = sizeof(SkPoint);
211 size_t localCoordOffset =
212 hasColorAttribute ? kColorOffset + sizeof(uint32_t) : kColorOffset;
213
214 for (int j = 0; j < vertexCount; ++j) {
215 if (this->hasMultipleViewMatrices()) {
216 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500217 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500218 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500219 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500220 if (hasColorAttribute) {
221 if (mesh.hasPerVertexColors()) {
222 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
223 } else {
224 *(uint32_t*)((intptr_t)verts + kColorOffset) = mesh.fColor;
225 }
Brian Salomon3f363692017-02-02 21:05:19 -0500226 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500227 if (hasLocalCoordsAttribute) {
228 if (mesh.hasExplicitLocalCoords()) {
229 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
230 } else {
231 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
232 }
233 }
234 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700235 }
joshualitt2771b562015-08-07 12:46:26 -0700236 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500237 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700238 }
239
Chris Daltonbca46e22017-05-15 11:03:26 -0600240 GrMesh mesh(this->primitiveType());
Chris Dalton114a3c02017-05-26 15:17:19 -0600241 if (!indices) {
Chris Dalton1d616352017-05-31 12:51:23 -0600242 mesh.setNonIndexedNonInstanced(fVertexCount);
Chris Dalton114a3c02017-05-26 15:17:19 -0600243 } else {
Brian Salomon802cb312018-06-08 18:05:20 -0400244 mesh.setIndexed(indexBuffer, fIndexCount, firstIndex, 0, fVertexCount - 1,
245 GrPrimitiveRestart::kNo);
joshualitt2771b562015-08-07 12:46:26 -0700246 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600247 mesh.setVertexData(vertexBuffer, firstVertex);
Brian Salomonc2f42542017-07-12 14:11:22 -0400248 target->draw(gp.get(), fHelper.makePipeline(target), mesh);
joshualitt2771b562015-08-07 12:46:26 -0700249}
250
Brian Salomonfc527d22016-12-14 21:07:01 -0500251bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
252 GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700253
Brian Salomonc2f42542017-07-12 14:11:22 -0400254 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt2771b562015-08-07 12:46:26 -0700255 return false;
256 }
257
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500258 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
joshualitt2771b562015-08-07 12:46:26 -0700259 return false;
260 }
261
Mike Reedaa9e3322017-03-16 14:38:48 -0400262 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
joshualitt2771b562015-08-07 12:46:26 -0700263 return false;
264 }
265
Brian Salomon3de0aee2017-01-29 09:34:17 -0500266 if (fColorArrayType != that->fColorArrayType) {
267 return false;
268 }
269
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400270 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon3f363692017-02-02 21:05:19 -0500271 return false;
272 }
273
Brian Osmanfa6d8652017-05-31 09:37:27 -0400274 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
275 // gamut is determined by the render target context. A mis-match should be impossible.
276 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
277
Brian Salomon199fb872017-02-06 09:41:10 -0500278 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
279 // with multiple view matrices.
280 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700281
Brian Salomon199fb872017-02-06 09:41:10 -0500282 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
283 fFlags |= kRequiresPerVertexColors_Flag;
284 }
Brian Salomon3f363692017-02-02 21:05:19 -0500285 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500286 if (!this->hasMultipleViewMatrices() &&
287 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
288 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500289 }
290
bsalomond92b4192016-06-30 07:59:23 -0700291 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700292 fVertexCount += that->fVertexCount;
293 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700294
bsalomon88cf17d2016-07-08 06:40:56 -0700295 this->joinBounds(*that);
joshualitt2771b562015-08-07 12:46:26 -0700296 return true;
297}
298
299///////////////////////////////////////////////////////////////////////////////////////////////////
300
Hal Canary6f6961e2017-01-31 13:50:44 -0500301#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700302
Brian Salomon5ec9def2016-12-20 15:34:05 -0500303#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700304
305static uint32_t seed_vertices(GrPrimitiveType type) {
306 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600307 case GrPrimitiveType::kTriangles:
308 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700309 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600310 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700311 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600312 case GrPrimitiveType::kLines:
313 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700314 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600315 case GrPrimitiveType::kLinesAdjacency:
316 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700317 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400318 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700319 return 0;
320}
321
322static uint32_t primitive_vertices(GrPrimitiveType type) {
323 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600324 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700325 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600326 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700327 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600328 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600329 case GrPrimitiveType::kPoints:
330 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700331 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600332 case GrPrimitiveType::kLinesAdjacency:
333 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700334 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400335 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700336 return 0;
337}
338
339static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
340 SkPoint p;
341 p.fX = random->nextRangeScalar(min, max);
342 p.fY = random->nextRangeScalar(min, max);
343 return p;
344}
345
346static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500347 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700348 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500349 SkTArray<uint32_t>* colors, bool hasColors,
350 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700351 for (uint32_t v = 0; v < count; v++) {
352 positions->push_back(random_point(random, min, max));
353 if (hasTexCoords) {
354 texCoords->push_back(random_point(random, min, max));
355 }
356 if (hasColors) {
357 colors->push_back(GrRandomColor(random));
358 }
359 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000360 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700361 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
362 }
363 }
364}
365
Brian Salomonc2f42542017-07-12 14:11:22 -0400366GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600367 GrPrimitiveType type;
368 do {
369 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
370 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400371 !context->contextPriv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600372
joshualitt2771b562015-08-07 12:46:26 -0700373 uint32_t primitiveCount = random->nextRangeU(1, 100);
374
375 // TODO make 'sensible' indexbuffers
376 SkTArray<SkPoint> positions;
377 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500378 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700379 SkTArray<uint16_t> indices;
380
381 bool hasTexCoords = random->nextBool();
382 bool hasIndices = random->nextBool();
383 bool hasColors = random->nextBool();
384
385 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
386
387 static const SkScalar kMinVertExtent = -100.f;
388 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500389 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
390 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
391 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700392
393 for (uint32_t i = 1; i < primitiveCount; i++) {
394 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500395 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
396 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700397 }
398
399 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700400
Brian Osmanfa6d8652017-05-31 09:37:27 -0400401 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400402
403 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
404 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
405 texCoords.begin(), colors.begin(),
406 hasIndices ? indices.count() : 0,
407 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400408 GrAAType aaType = GrAAType::kNone;
409 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
410 aaType = GrAAType::kMSAA;
411 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400412 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), viewMatrix,
Brian Osman08a50e02018-06-15 15:06:48 -0400413 aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700414}
415
416#endif