blob: dded70a7f7d5fc4fc72a3d0f0b686031e18d2778 [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
Brian Salomonc2f42542017-07-12 14:11:22 -040014std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrPaint&& paint,
15 sk_sp<SkVertices> vertices,
16 const SkMatrix& viewMatrix,
17 GrAAType aaType,
18 bool gammaCorrect,
19 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());
Brian Salomonc2f42542017-07-12 14:11:22 -040024 return Helper::FactoryHelper<GrDrawVerticesOp>(std::move(paint), std::move(vertices), primType,
25 aaType, gammaCorrect, 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,
31 GrAAType aaType, bool gammaCorrect,
Brian Osmanfa6d8652017-05-31 09:37:27 -040032 sk_sp<GrColorSpaceXform> colorSpaceXform,
33 const SkMatrix& viewMatrix)
34 : INHERITED(ClassID())
Brian Salomonc2f42542017-07-12 14:11:22 -040035 , fHelper(helperArgs, aaType)
Brian Osmanfa6d8652017-05-31 09:37:27 -040036 , fPrimitiveType(primitiveType)
37 , fColorSpaceXform(std::move(colorSpaceXform)) {
Brian Salomon199fb872017-02-06 09:41:10 -050038 SkASSERT(vertices);
39
40 fVertexCount = vertices->vertexCount();
41 fIndexCount = vertices->indexCount();
Brian Osmanae0c50c2017-05-25 16:56:34 -040042 fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor
43 : ColorArrayType::kPremulGrColor;
Brian Osmanfa6d8652017-05-31 09:37:27 -040044 // GrColor is linearized (and gamut converted) during paint conversion, but SkColors need to be
45 // handled in the shader
46 fLinearizeColors = gammaCorrect && vertices->hasColors();
joshualitt2771b562015-08-07 12:46:26 -070047
bsalomond92b4192016-06-30 07:59:23 -070048 Mesh& mesh = fMeshes.push_back();
49 mesh.fColor = color;
Brian Salomon3f363692017-02-02 21:05:19 -050050 mesh.fViewMatrix = viewMatrix;
Brian Salomon199fb872017-02-06 09:41:10 -050051 mesh.fVertices = std::move(vertices);
Brian Osman8a030552017-05-23 15:03:18 -040052 mesh.fIgnoreTexCoords = false;
53 mesh.fIgnoreColors = false;
joshualitt2771b562015-08-07 12:46:26 -070054
Brian Salomon199fb872017-02-06 09:41:10 -050055 fFlags = 0;
56 if (mesh.hasPerVertexColors()) {
57 fFlags |= kRequiresPerVertexColors_Flag;
joshualitt2771b562015-08-07 12:46:26 -070058 }
Brian Salomon199fb872017-02-06 09:41:10 -050059 if (mesh.hasExplicitLocalCoords()) {
60 fFlags |= kAnyMeshHasExplicitLocalCoords;
joshualitt2771b562015-08-07 12:46:26 -070061 }
62
bsalomon88cf17d2016-07-08 06:40:56 -070063 IsZeroArea zeroArea;
Chris Dalton3809bab2017-06-13 10:55:06 -060064 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon88cf17d2016-07-08 06:40:56 -070065 zeroArea = IsZeroArea::kYes;
66 } else {
67 zeroArea = IsZeroArea::kNo;
68 }
Brian Salomon199fb872017-02-06 09:41:10 -050069 this->setTransformedBounds(mesh.fVertices->bounds(), viewMatrix, HasAABloat::kNo, zeroArea);
joshualitt2771b562015-08-07 12:46:26 -070070}
71
Brian Salomonc2f42542017-07-12 14:11:22 -040072SkString GrDrawVerticesOp::dumpInfo() const {
73 SkString string;
74 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
75 fMeshes.count(), fVertexCount, fIndexCount);
76 string += fHelper.dumpInfo();
77 string += INHERITED::dumpInfo();
78 return string;
joshualitt2771b562015-08-07 12:46:26 -070079}
80
Brian Salomonc2f42542017-07-12 14:11:22 -040081GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const {
82 return fHelper.fixedFunctionFlags();
83}
84
85GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040086 const GrAppliedClip* clip,
87 GrPixelConfigIsClamped dstIsClamped) {
Brian Salomonc2f42542017-07-12 14:11:22 -040088 GrProcessorAnalysisColor gpColor;
89 if (this->requiresPerVertexColors()) {
90 gpColor.setToUnknown();
91 } else {
92 gpColor.setToConstant(fMeshes.front().fColor);
93 }
Brian Osman9a725dd2017-09-20 09:53:22 -040094 auto result = fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
95 GrProcessorAnalysisCoverage::kNone, &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -040096 if (gpColor.isConstant(&fMeshes.front().fColor)) {
97 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -050098 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -040099 fColorArrayType = ColorArrayType::kPremulGrColor;
Brian Osmanfa6d8652017-05-31 09:37:27 -0400100 fLinearizeColors = false;
joshualitt2771b562015-08-07 12:46:26 -0700101 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400102 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400103 fMeshes[0].fIgnoreTexCoords = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500104 fFlags &= ~kAnyMeshHasExplicitLocalCoords;
bsalomon14eaaa62015-09-24 07:01:26 -0700105 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400106 return result;
joshualitt2771b562015-08-07 12:46:26 -0700107}
108
Brian Salomon199fb872017-02-06 09:41:10 -0500109sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(bool* hasColorAttribute,
110 bool* hasLocalCoordAttribute) const {
111 using namespace GrDefaultGeoProcFactory;
112 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400113 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500114 // If we have multiple view matrices we will transform the positions into device space. We
115 // must then also provide untransformed positions as local coords.
116 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
117 *hasLocalCoordAttribute = true;
118 localCoordsType = LocalCoords::kHasExplicit_Type;
119 } else {
120 *hasLocalCoordAttribute = false;
121 localCoordsType = LocalCoords::kUsePosition_Type;
122 }
123 } else {
124 localCoordsType = LocalCoords::kUnused_Type;
125 *hasLocalCoordAttribute = false;
126 }
127
128 Color color(fMeshes[0].fColor);
129 if (this->requiresPerVertexColors()) {
Brian Osmanae0c50c2017-05-25 16:56:34 -0400130 color.fType = (fColorArrayType == ColorArrayType::kPremulGrColor)
Brian Salomon199fb872017-02-06 09:41:10 -0500131 ? Color::kPremulGrColorAttribute_Type
132 : Color::kUnpremulSkColorAttribute_Type;
Brian Osmanfa6d8652017-05-31 09:37:27 -0400133 color.fLinearize = fLinearizeColors;
134 color.fColorSpaceXform = fColorSpaceXform;
Brian Salomon199fb872017-02-06 09:41:10 -0500135 *hasColorAttribute = true;
136 } else {
137 *hasColorAttribute = false;
138 };
139 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
140 return GrDefaultGeoProcFactory::Make(color, Coverage::kSolid_Type, localCoordsType, vm);
141}
142
Brian Salomon91326c32017-08-09 16:02:19 -0400143void GrDrawVerticesOp::onPrepareDraws(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500144 bool hasColorAttribute;
145 bool hasLocalCoordsAttribute;
146 sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700147 size_t vertexStride = gp->getVertexStride();
148
Brian Salomon199fb872017-02-06 09:41:10 -0500149 SkASSERT(vertexStride == sizeof(SkPoint) + (hasColorAttribute ? sizeof(uint32_t) : 0) +
150 (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0));
joshualitt2771b562015-08-07 12:46:26 -0700151
bsalomond92b4192016-06-30 07:59:23 -0700152 int instanceCount = fMeshes.count();
joshualitt2771b562015-08-07 12:46:26 -0700153
cdalton397536c2016-03-25 12:15:03 -0700154 const GrBuffer* vertexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700155 int firstVertex;
156
bsalomon14eaaa62015-09-24 07:01:26 -0700157 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700158
159 if (!verts) {
160 SkDebugf("Could not allocate vertices\n");
161 return;
162 }
163
cdalton397536c2016-03-25 12:15:03 -0700164 const GrBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700165 int firstIndex = 0;
166
halcanary96fcdcc2015-08-27 07:41:13 -0700167 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500168 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700169 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700170
171 if (!indices) {
172 SkDebugf("Could not allocate indices\n");
173 return;
174 }
175 }
176
joshualitt2771b562015-08-07 12:46:26 -0700177 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500178 // We have a fast case below for uploading the vertex data when the matrix is translate
179 // only and there are colors but not local coords.
180 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700181 for (int i = 0; i < instanceCount; i++) {
bsalomond92b4192016-06-30 07:59:23 -0700182 const Mesh& mesh = fMeshes[i];
bsalomon14eaaa62015-09-24 07:01:26 -0700183 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500184 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500185 for (int j = 0; j < indexCount; ++j) {
186 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700187 }
188 }
Brian Salomon199fb872017-02-06 09:41:10 -0500189 int vertexCount = mesh.fVertices->vertexCount();
190 const SkPoint* positions = mesh.fVertices->positions();
191 const SkColor* colors = mesh.fVertices->colors();
192 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500193 bool fastMesh = (!this->hasMultipleViewMatrices() ||
194 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
195 mesh.hasPerVertexColors();
196 if (fastAttrs && fastMesh) {
197 struct V {
198 SkPoint fPos;
199 uint32_t fColor;
200 };
201 SkASSERT(sizeof(V) == vertexStride);
202 V* v = (V*)verts;
203 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500204 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500205 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700206 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500207 for (int j = 0; j < vertexCount; ++j) {
208 Sk2f p = Sk2f::Load(positions++) + t;
209 p.store(&v[j].fPos);
210 v[j].fColor = colors[j];
211 }
212 verts = v + vertexCount;
213 } else {
214 static constexpr size_t kColorOffset = sizeof(SkPoint);
215 size_t localCoordOffset =
216 hasColorAttribute ? kColorOffset + sizeof(uint32_t) : kColorOffset;
217
218 for (int j = 0; j < vertexCount; ++j) {
219 if (this->hasMultipleViewMatrices()) {
220 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500221 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500222 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500223 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500224 if (hasColorAttribute) {
225 if (mesh.hasPerVertexColors()) {
226 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
227 } else {
228 *(uint32_t*)((intptr_t)verts + kColorOffset) = mesh.fColor;
229 }
Brian Salomon3f363692017-02-02 21:05:19 -0500230 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500231 if (hasLocalCoordsAttribute) {
232 if (mesh.hasExplicitLocalCoords()) {
233 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
234 } else {
235 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
236 }
237 }
238 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700239 }
joshualitt2771b562015-08-07 12:46:26 -0700240 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500241 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700242 }
243
Chris Daltonbca46e22017-05-15 11:03:26 -0600244 GrMesh mesh(this->primitiveType());
Chris Dalton114a3c02017-05-26 15:17:19 -0600245 if (!indices) {
Chris Dalton1d616352017-05-31 12:51:23 -0600246 mesh.setNonIndexedNonInstanced(fVertexCount);
Chris Dalton114a3c02017-05-26 15:17:19 -0600247 } else {
248 mesh.setIndexed(indexBuffer, fIndexCount, firstIndex, 0, fVertexCount - 1);
joshualitt2771b562015-08-07 12:46:26 -0700249 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600250 mesh.setVertexData(vertexBuffer, firstVertex);
Brian Salomonc2f42542017-07-12 14:11:22 -0400251 target->draw(gp.get(), fHelper.makePipeline(target), mesh);
joshualitt2771b562015-08-07 12:46:26 -0700252}
253
Brian Salomonfc527d22016-12-14 21:07:01 -0500254bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
255 GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700256
Brian Salomonc2f42542017-07-12 14:11:22 -0400257 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt2771b562015-08-07 12:46:26 -0700258 return false;
259 }
260
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500261 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
joshualitt2771b562015-08-07 12:46:26 -0700262 return false;
263 }
264
Mike Reedaa9e3322017-03-16 14:38:48 -0400265 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
joshualitt2771b562015-08-07 12:46:26 -0700266 return false;
267 }
268
Brian Salomon3de0aee2017-01-29 09:34:17 -0500269 if (fColorArrayType != that->fColorArrayType) {
270 return false;
271 }
272
Brian Osmanfa6d8652017-05-31 09:37:27 -0400273 if (fLinearizeColors != that->fLinearizeColors) {
274 return false;
275 }
276
Brian Salomon199fb872017-02-06 09:41:10 -0500277 if (fVertexCount + that->fVertexCount > SK_MaxU16) {
Brian Salomon3f363692017-02-02 21:05:19 -0500278 return false;
279 }
280
Brian Osmanfa6d8652017-05-31 09:37:27 -0400281 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
282 // gamut is determined by the render target context. A mis-match should be impossible.
283 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
284
Brian Salomon199fb872017-02-06 09:41:10 -0500285 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
286 // with multiple view matrices.
287 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700288
Brian Salomon199fb872017-02-06 09:41:10 -0500289 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
290 fFlags |= kRequiresPerVertexColors_Flag;
291 }
Brian Salomon3f363692017-02-02 21:05:19 -0500292 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500293 if (!this->hasMultipleViewMatrices() &&
294 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
295 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500296 }
297
bsalomond92b4192016-06-30 07:59:23 -0700298 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700299 fVertexCount += that->fVertexCount;
300 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700301
bsalomon88cf17d2016-07-08 06:40:56 -0700302 this->joinBounds(*that);
joshualitt2771b562015-08-07 12:46:26 -0700303 return true;
304}
305
306///////////////////////////////////////////////////////////////////////////////////////////////////
307
Hal Canary6f6961e2017-01-31 13:50:44 -0500308#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700309
Brian Salomon5ec9def2016-12-20 15:34:05 -0500310#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700311
312static uint32_t seed_vertices(GrPrimitiveType type) {
313 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600314 case GrPrimitiveType::kTriangles:
315 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700316 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600317 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700318 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600319 case GrPrimitiveType::kLines:
320 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700321 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600322 case GrPrimitiveType::kLinesAdjacency:
323 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700324 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400325 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700326 return 0;
327}
328
329static uint32_t primitive_vertices(GrPrimitiveType type) {
330 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600331 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700332 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600333 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700334 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600335 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600336 case GrPrimitiveType::kPoints:
337 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700338 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600339 case GrPrimitiveType::kLinesAdjacency:
340 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700341 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400342 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700343 return 0;
344}
345
346static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
347 SkPoint p;
348 p.fX = random->nextRangeScalar(min, max);
349 p.fY = random->nextRangeScalar(min, max);
350 return p;
351}
352
353static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500354 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700355 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500356 SkTArray<uint32_t>* colors, bool hasColors,
357 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700358 for (uint32_t v = 0; v < count; v++) {
359 positions->push_back(random_point(random, min, max));
360 if (hasTexCoords) {
361 texCoords->push_back(random_point(random, min, max));
362 }
363 if (hasColors) {
364 colors->push_back(GrRandomColor(random));
365 }
366 if (hasIndices) {
367 SkASSERT(maxVertex <= SK_MaxU16);
368 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
369 }
370 }
371}
372
Brian Salomonc2f42542017-07-12 14:11:22 -0400373GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600374 GrPrimitiveType type;
375 do {
376 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
377 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400378 !context->contextPriv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600379
joshualitt2771b562015-08-07 12:46:26 -0700380 uint32_t primitiveCount = random->nextRangeU(1, 100);
381
382 // TODO make 'sensible' indexbuffers
383 SkTArray<SkPoint> positions;
384 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500385 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700386 SkTArray<uint16_t> indices;
387
388 bool hasTexCoords = random->nextBool();
389 bool hasIndices = random->nextBool();
390 bool hasColors = random->nextBool();
Brian Osmanfa6d8652017-05-31 09:37:27 -0400391 bool linearizeColors = random->nextBool();
joshualitt2771b562015-08-07 12:46:26 -0700392
393 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
394
395 static const SkScalar kMinVertExtent = -100.f;
396 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500397 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
398 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
399 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700400
401 for (uint32_t i = 1; i < primitiveCount; i++) {
402 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500403 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
404 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700405 }
406
407 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700408
Brian Osmanfa6d8652017-05-31 09:37:27 -0400409 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400410
411 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
412 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
413 texCoords.begin(), colors.begin(),
414 hasIndices ? indices.count() : 0,
415 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400416 GrAAType aaType = GrAAType::kNone;
417 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
418 aaType = GrAAType::kMSAA;
419 }
420 return GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType,
421 linearizeColors, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700422}
423
424#endif