blob: 12c2a0bdbb2d416c2f70fd9b4b44e2bd22c08eac [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"
joshualitt2771b562015-08-07 12:46:26 -07009
joshualitt2771b562015-08-07 12:46:26 -070010#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrInvariantOutput.h"
12#include "GrOpFlushState.h"
joshualitt2771b562015-08-07 12:46:26 -070013
bungeman06ca8ec2016-06-09 08:01:03 -070014static sk_sp<GrGeometryProcessor> set_vertex_attributes(bool hasLocalCoords,
joshualitt2771b562015-08-07 12:46:26 -070015 int* colorOffset,
16 int* texOffset,
joshualitt2771b562015-08-07 12:46:26 -070017 const SkMatrix& viewMatrix,
18 bool coverageIgnored) {
19 using namespace GrDefaultGeoProcFactory;
20 *texOffset = -1;
21 *colorOffset = -1;
joshualitt2771b562015-08-07 12:46:26 -070022
23 Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_Type);
Brian Salomonfc527d22016-12-14 21:07:01 -050024 LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type
25 : LocalCoords::kUsePosition_Type);
bsalomon14eaaa62015-09-24 07:01:26 -070026 *colorOffset = sizeof(SkPoint);
27 if (hasLocalCoords) {
joshualitt2771b562015-08-07 12:46:26 -070028 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
joshualitt2771b562015-08-07 12:46:26 -070029 }
Brian Salomonfc527d22016-12-14 21:07:01 -050030 return GrDefaultGeoProcFactory::Make(Color(Color::kAttribute_Type), coverage, localCoords,
31 viewMatrix);
joshualitt2771b562015-08-07 12:46:26 -070032}
33
Brian Salomonfc527d22016-12-14 21:07:01 -050034GrDrawVerticesOp::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()) {
joshualitt2771b562015-08-07 12:46:26 -070040 SkASSERT(positions);
41
bsalomon14eaaa62015-09-24 07:01:26 -070042 fViewMatrix = viewMatrix;
bsalomond92b4192016-06-30 07:59:23 -070043 Mesh& mesh = fMeshes.push_back();
44 mesh.fColor = color;
joshualitt2771b562015-08-07 12:46:26 -070045
bsalomond92b4192016-06-30 07:59:23 -070046 mesh.fPositions.append(vertexCount, positions);
joshualitt2771b562015-08-07 12:46:26 -070047 if (indices) {
bsalomond92b4192016-06-30 07:59:23 -070048 mesh.fIndices.append(indexCount, indices);
joshualitt2771b562015-08-07 12:46:26 -070049 }
50
51 if (colors) {
bsalomon14eaaa62015-09-24 07:01:26 -070052 fVariableColor = true;
bsalomond92b4192016-06-30 07:59:23 -070053 mesh.fColors.append(vertexCount, colors);
joshualitt2771b562015-08-07 12:46:26 -070054 } else {
bsalomon14eaaa62015-09-24 07:01:26 -070055 fVariableColor = false;
joshualitt2771b562015-08-07 12:46:26 -070056 }
57
58 if (localCoords) {
bsalomond92b4192016-06-30 07:59:23 -070059 mesh.fLocalCoords.append(vertexCount, localCoords);
joshualitt2771b562015-08-07 12:46:26 -070060 }
bsalomon14eaaa62015-09-24 07:01:26 -070061 fVertexCount = vertexCount;
62 fIndexCount = indexCount;
63 fPrimitiveType = primitiveType;
joshualitt2771b562015-08-07 12:46:26 -070064
bsalomon88cf17d2016-07-08 06:40:56 -070065 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);
joshualitt2771b562015-08-07 12:46:26 -070072}
73
Brian Salomon92aee3d2016-12-21 09:20:25 -050074void GrDrawVerticesOp::getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const {
bsalomon14eaaa62015-09-24 07:01:26 -070075 if (fVariableColor) {
Brian Salomon92aee3d2016-12-21 09:20:25 -050076 input->pipelineColorInput()->setUnknownFourComponents();
joshualitt2771b562015-08-07 12:46:26 -070077 } else {
Brian Salomon92aee3d2016-12-21 09:20:25 -050078 input->pipelineColorInput()->setKnownFourComponents(fMeshes[0].fColor);
joshualitt2771b562015-08-07 12:46:26 -070079 }
Brian Salomon92aee3d2016-12-21 09:20:25 -050080 input->pipelineCoverageInput()->setKnownSingleComponent(0xff);
joshualitt2771b562015-08-07 12:46:26 -070081}
82
Brian Salomon92aee3d2016-12-21 09:20:25 -050083void GrDrawVerticesOp::applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) {
bsalomond92b4192016-06-30 07:59:23 -070084 SkASSERT(fMeshes.count() == 1);
bsalomon14eaaa62015-09-24 07:01:26 -070085 GrColor overrideColor;
Brian Salomon92aee3d2016-12-21 09:20:25 -050086 if (optimizations.getOverrideColorIfSet(&overrideColor)) {
bsalomond92b4192016-06-30 07:59:23 -070087 fMeshes[0].fColor = overrideColor;
88 fMeshes[0].fColors.reset();
bsalomon14eaaa62015-09-24 07:01:26 -070089 fVariableColor = false;
joshualitt2771b562015-08-07 12:46:26 -070090 }
Brian Salomon92aee3d2016-12-21 09:20:25 -050091 fCoverageIgnored = !optimizations.readsCoverage();
92 if (!optimizations.readsLocalCoords()) {
bsalomond92b4192016-06-30 07:59:23 -070093 fMeshes[0].fLocalCoords.reset();
bsalomon14eaaa62015-09-24 07:01:26 -070094 }
joshualitt2771b562015-08-07 12:46:26 -070095}
96
Brian Salomonfc527d22016-12-14 21:07:01 -050097void GrDrawVerticesOp::onPrepareDraws(Target* target) const {
bsalomond92b4192016-06-30 07:59:23 -070098 bool hasLocalCoords = !fMeshes[0].fLocalCoords.isEmpty();
joshualitt2771b562015-08-07 12:46:26 -070099 int colorOffset = -1, texOffset = -1;
bungeman06ca8ec2016-06-09 08:01:03 -0700100 sk_sp<GrGeometryProcessor> gp(set_vertex_attributes(hasLocalCoords, &colorOffset, &texOffset,
101 fViewMatrix, fCoverageIgnored));
joshualitt2771b562015-08-07 12:46:26 -0700102 size_t vertexStride = gp->getVertexStride();
103
Brian Salomonfc527d22016-12-14 21:07:01 -0500104 SkASSERT(vertexStride ==
105 sizeof(SkPoint) + (hasLocalCoords ? sizeof(SkPoint) : 0) + sizeof(GrColor));
joshualitt2771b562015-08-07 12:46:26 -0700106
bsalomond92b4192016-06-30 07:59:23 -0700107 int instanceCount = fMeshes.count();
joshualitt2771b562015-08-07 12:46:26 -0700108
cdalton397536c2016-03-25 12:15:03 -0700109 const GrBuffer* vertexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700110 int firstVertex;
111
bsalomon14eaaa62015-09-24 07:01:26 -0700112 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700113
114 if (!verts) {
115 SkDebugf("Could not allocate vertices\n");
116 return;
117 }
118
cdalton397536c2016-03-25 12:15:03 -0700119 const GrBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700120 int firstIndex = 0;
121
halcanary96fcdcc2015-08-27 07:41:13 -0700122 uint16_t* indices = nullptr;
bsalomond92b4192016-06-30 07:59:23 -0700123 if (!fMeshes[0].fIndices.isEmpty()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700124 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700125
126 if (!indices) {
127 SkDebugf("Could not allocate indices\n");
128 return;
129 }
130 }
131
132 int indexOffset = 0;
133 int vertexOffset = 0;
134 for (int i = 0; i < instanceCount; i++) {
bsalomond92b4192016-06-30 07:59:23 -0700135 const Mesh& mesh = fMeshes[i];
joshualitt2771b562015-08-07 12:46:26 -0700136
137 // TODO we can actually cache this interleaved and then just memcopy
bsalomon14eaaa62015-09-24 07:01:26 -0700138 if (indices) {
bsalomond92b4192016-06-30 07:59:23 -0700139 for (int j = 0; j < mesh.fIndices.count(); ++j, ++indexOffset) {
140 *(indices + indexOffset) = mesh.fIndices[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700141 }
142 }
143
bsalomond92b4192016-06-30 07:59:23 -0700144 for (int j = 0; j < mesh.fPositions.count(); ++j) {
145 *((SkPoint*)verts) = mesh.fPositions[j];
146 if (mesh.fColors.isEmpty()) {
147 *(GrColor*)((intptr_t)verts + colorOffset) = mesh.fColor;
bsalomon14eaaa62015-09-24 07:01:26 -0700148 } else {
bsalomond92b4192016-06-30 07:59:23 -0700149 *(GrColor*)((intptr_t)verts + colorOffset) = mesh.fColors[j];
joshualitt2771b562015-08-07 12:46:26 -0700150 }
bsalomon14eaaa62015-09-24 07:01:26 -0700151 if (hasLocalCoords) {
bsalomond92b4192016-06-30 07:59:23 -0700152 *(SkPoint*)((intptr_t)verts + texOffset) = mesh.fLocalCoords[j];
joshualitt2771b562015-08-07 12:46:26 -0700153 }
154 verts = (void*)((intptr_t)verts + vertexStride);
155 vertexOffset++;
156 }
157 }
158
egdaniel0e1853c2016-03-17 11:35:45 -0700159 GrMesh mesh;
bsalomon14eaaa62015-09-24 07:01:26 -0700160 if (indices) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500161 mesh.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex, firstIndex,
162 fVertexCount, fIndexCount);
joshualitt2771b562015-08-07 12:46:26 -0700163
164 } else {
egdaniel0e1853c2016-03-17 11:35:45 -0700165 mesh.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700166 }
bungeman06ca8ec2016-06-09 08:01:03 -0700167 target->draw(gp.get(), mesh);
joshualitt2771b562015-08-07 12:46:26 -0700168}
169
Brian Salomonfc527d22016-12-14 21:07:01 -0500170bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
171 GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700172
173 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
174 that->bounds(), caps)) {
joshualitt2771b562015-08-07 12:46:26 -0700175 return false;
176 }
177
joshualitt2771b562015-08-07 12:46:26 -0700178 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
179 return false;
180 }
181
Brian Salomonfc527d22016-12-14 21:07:01 -0500182 // We currently use a uniform viewmatrix for this op.
bsalomon14eaaa62015-09-24 07:01:26 -0700183 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
joshualitt2771b562015-08-07 12:46:26 -0700184 return false;
185 }
186
bsalomond92b4192016-06-30 07:59:23 -0700187 if (fMeshes[0].fIndices.isEmpty() != that->fMeshes[0].fIndices.isEmpty()) {
joshualitt2771b562015-08-07 12:46:26 -0700188 return false;
189 }
190
bsalomond92b4192016-06-30 07:59:23 -0700191 if (fMeshes[0].fLocalCoords.isEmpty() != that->fMeshes[0].fLocalCoords.isEmpty()) {
joshualitt2771b562015-08-07 12:46:26 -0700192 return false;
193 }
194
bsalomon14eaaa62015-09-24 07:01:26 -0700195 if (!fVariableColor) {
bsalomond92b4192016-06-30 07:59:23 -0700196 if (that->fVariableColor || that->fMeshes[0].fColor != fMeshes[0].fColor) {
bsalomon14eaaa62015-09-24 07:01:26 -0700197 fVariableColor = true;
198 }
joshualitt2771b562015-08-07 12:46:26 -0700199 }
200
bsalomond92b4192016-06-30 07:59:23 -0700201 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700202 fVertexCount += that->fVertexCount;
203 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700204
bsalomon88cf17d2016-07-08 06:40:56 -0700205 this->joinBounds(*that);
joshualitt2771b562015-08-07 12:46:26 -0700206 return true;
207}
208
209///////////////////////////////////////////////////////////////////////////////////////////////////
210
211#ifdef GR_TEST_UTILS
212
Brian Salomon5ec9def2016-12-20 15:34:05 -0500213#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700214
215static uint32_t seed_vertices(GrPrimitiveType type) {
216 switch (type) {
217 case kTriangles_GrPrimitiveType:
218 case kTriangleStrip_GrPrimitiveType:
219 case kTriangleFan_GrPrimitiveType:
220 return 3;
221 case kPoints_GrPrimitiveType:
222 return 1;
223 case kLines_GrPrimitiveType:
224 case kLineStrip_GrPrimitiveType:
225 return 2;
226 }
227 SkFAIL("Incomplete switch\n");
228 return 0;
229}
230
231static uint32_t primitive_vertices(GrPrimitiveType type) {
232 switch (type) {
233 case kTriangles_GrPrimitiveType:
234 return 3;
235 case kLines_GrPrimitiveType:
236 return 2;
237 case kTriangleStrip_GrPrimitiveType:
238 case kTriangleFan_GrPrimitiveType:
239 case kPoints_GrPrimitiveType:
240 case kLineStrip_GrPrimitiveType:
241 return 1;
242 }
243 SkFAIL("Incomplete switch\n");
244 return 0;
245}
246
247static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
248 SkPoint p;
249 p.fX = random->nextRangeScalar(min, max);
250 p.fY = random->nextRangeScalar(min, max);
251 return p;
252}
253
254static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500255 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700256 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomonfc527d22016-12-14 21:07:01 -0500257 SkTArray<GrColor>* colors, bool hasColors, SkTArray<uint16_t>* indices,
258 bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700259 for (uint32_t v = 0; v < count; v++) {
260 positions->push_back(random_point(random, min, max));
261 if (hasTexCoords) {
262 texCoords->push_back(random_point(random, min, max));
263 }
264 if (hasColors) {
265 colors->push_back(GrRandomColor(random));
266 }
267 if (hasIndices) {
268 SkASSERT(maxVertex <= SK_MaxU16);
269 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
270 }
271 }
272}
273
Brian Salomon5ec9def2016-12-20 15:34:05 -0500274DRAW_OP_TEST_DEFINE(VerticesOp) {
joshualitt2771b562015-08-07 12:46:26 -0700275 GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1));
276 uint32_t primitiveCount = random->nextRangeU(1, 100);
277
278 // TODO make 'sensible' indexbuffers
279 SkTArray<SkPoint> positions;
280 SkTArray<SkPoint> texCoords;
281 SkTArray<GrColor> colors;
282 SkTArray<uint16_t> indices;
283
284 bool hasTexCoords = random->nextBool();
285 bool hasIndices = random->nextBool();
286 bool hasColors = random->nextBool();
287
288 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
289
290 static const SkScalar kMinVertExtent = -100.f;
291 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500292 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
293 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
294 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700295
296 for (uint32_t i = 1; i < primitiveCount; i++) {
297 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500298 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
299 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700300 }
301
302 SkMatrix viewMatrix = GrTest::TestMatrix(random);
303 SkRect bounds;
Brian Salomonfc527d22016-12-14 21:07:01 -0500304 SkDEBUGCODE(bool result =) bounds.setBoundsCheck(positions.begin(), vertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700305 SkASSERT(result);
306
307 viewMatrix.mapRect(&bounds);
308
bsalomond92b4192016-06-30 07:59:23 -0700309 GrColor color = GrRandomColor(random);
Brian Salomonfc527d22016-12-14 21:07:01 -0500310 return GrDrawVerticesOp::Make(color, type, viewMatrix, positions.begin(), vertexCount,
311 indices.begin(), hasIndices ? vertexCount : 0, colors.begin(),
Brian Salomon5ec9def2016-12-20 15:34:05 -0500312 texCoords.begin(), bounds);
joshualitt2771b562015-08-07 12:46:26 -0700313}
314
315#endif