Line data Source code
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 :
8 : #include "GrDrawVerticesOp.h"
9 : #include "GrDefaultGeoProcFactory.h"
10 : #include "GrOpFlushState.h"
11 : #include "SkGr.h"
12 :
13 0 : std::unique_ptr<GrLegacyMeshDrawOp> GrDrawVerticesOp::Make(
14 : GrColor color, GrPrimitiveType primitiveType, const SkMatrix& viewMatrix,
15 : const SkPoint* positions, int vertexCount, const uint16_t* indices, int indexCount,
16 : const uint32_t* colors, const SkPoint* localCoords, const SkRect& bounds,
17 : GrRenderTargetContext::ColorArrayType colorArrayType) {
18 : static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
19 0 : SkASSERT(positions);
20 0 : if (!colors) {
21 : // When we tessellate we will fill a color array with the GrColor value passed above as
22 : // 'color'.
23 0 : colorArrayType = GrRenderTargetContext::ColorArrayType::kPremulGrColor;
24 : }
25 : sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions,
26 0 : localCoords, colors, indexCount, indices);
27 0 : if (!vertices) {
28 0 : return nullptr;
29 : }
30 : return std::unique_ptr<GrLegacyMeshDrawOp>(new GrDrawVerticesOp(
31 0 : std::move(vertices), primitiveType, color, colorArrayType, viewMatrix));
32 : }
33 :
34 0 : std::unique_ptr<GrLegacyMeshDrawOp> GrDrawVerticesOp::Make(GrColor color,
35 : sk_sp<SkVertices> vertices,
36 : const SkMatrix& viewMatrix) {
37 0 : SkASSERT(vertices);
38 0 : GrPrimitiveType primType = SkVertexModeToGrPrimitiveType(vertices->mode());
39 : return std::unique_ptr<GrLegacyMeshDrawOp>(
40 0 : new GrDrawVerticesOp(std::move(vertices), primType, color,
41 0 : GrRenderTargetContext::ColorArrayType::kSkColor, viewMatrix));
42 : }
43 :
44 0 : GrDrawVerticesOp::GrDrawVerticesOp(sk_sp<SkVertices> vertices, GrPrimitiveType primitiveType,
45 : GrColor color,
46 : GrRenderTargetContext::ColorArrayType colorArrayType,
47 0 : const SkMatrix& viewMatrix, uint32_t flags)
48 0 : : INHERITED(ClassID()), fColorArrayType(colorArrayType) {
49 0 : SkASSERT(vertices);
50 :
51 0 : fVertexCount = vertices->vertexCount();
52 0 : fIndexCount = vertices->indexCount();
53 0 : fPrimitiveType = primitiveType;
54 :
55 0 : Mesh& mesh = fMeshes.push_back();
56 0 : mesh.fColor = color;
57 0 : mesh.fViewMatrix = viewMatrix;
58 0 : mesh.fVertices = std::move(vertices);
59 0 : mesh.fFlags = flags;
60 :
61 0 : fFlags = 0;
62 0 : if (mesh.hasPerVertexColors()) {
63 0 : fFlags |= kRequiresPerVertexColors_Flag;
64 : }
65 0 : if (mesh.hasExplicitLocalCoords()) {
66 0 : fFlags |= kAnyMeshHasExplicitLocalCoords;
67 : }
68 :
69 : IsZeroArea zeroArea;
70 0 : if (GrIsPrimTypeLines(primitiveType) || kPoints_GrPrimitiveType == primitiveType) {
71 0 : zeroArea = IsZeroArea::kYes;
72 : } else {
73 0 : zeroArea = IsZeroArea::kNo;
74 : }
75 0 : this->setTransformedBounds(mesh.fVertices->bounds(), viewMatrix, HasAABloat::kNo, zeroArea);
76 0 : }
77 :
78 0 : void GrDrawVerticesOp::getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
79 : GrProcessorAnalysisCoverage* coverage) const {
80 0 : if (this->requiresPerVertexColors()) {
81 0 : color->setToUnknown();
82 : } else {
83 0 : color->setToConstant(fMeshes[0].fColor);
84 : }
85 0 : *coverage = GrProcessorAnalysisCoverage::kNone;
86 0 : }
87 :
88 0 : void GrDrawVerticesOp::applyPipelineOptimizations(const PipelineOptimizations& optimizations) {
89 0 : SkASSERT(fMeshes.count() == 1);
90 : GrColor overrideColor;
91 0 : if (optimizations.getOverrideColorIfSet(&overrideColor)) {
92 0 : fMeshes[0].fColor = overrideColor;
93 0 : fMeshes[0].fFlags |= kIgnoreColors_VerticesFlag;
94 0 : fFlags &= ~kRequiresPerVertexColors_Flag;
95 0 : fColorArrayType = GrRenderTargetContext::ColorArrayType::kPremulGrColor;
96 : }
97 0 : if (optimizations.readsLocalCoords()) {
98 0 : fFlags |= kPipelineRequiresLocalCoords_Flag;
99 : } else {
100 0 : fFlags |= kIgnoreTexCoords_VerticesFlag;
101 0 : fFlags &= ~kAnyMeshHasExplicitLocalCoords;
102 : }
103 0 : }
104 :
105 0 : sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(bool* hasColorAttribute,
106 : bool* hasLocalCoordAttribute) const {
107 : using namespace GrDefaultGeoProcFactory;
108 : LocalCoords::Type localCoordsType;
109 0 : if (this->pipelineRequiresLocalCoords()) {
110 : // If we have multiple view matrices we will transform the positions into device space. We
111 : // must then also provide untransformed positions as local coords.
112 0 : if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
113 0 : *hasLocalCoordAttribute = true;
114 0 : localCoordsType = LocalCoords::kHasExplicit_Type;
115 : } else {
116 0 : *hasLocalCoordAttribute = false;
117 0 : localCoordsType = LocalCoords::kUsePosition_Type;
118 : }
119 : } else {
120 0 : localCoordsType = LocalCoords::kUnused_Type;
121 0 : *hasLocalCoordAttribute = false;
122 : }
123 :
124 0 : Color color(fMeshes[0].fColor);
125 0 : if (this->requiresPerVertexColors()) {
126 0 : color.fType = (fColorArrayType == GrRenderTargetContext::ColorArrayType::kPremulGrColor)
127 0 : ? Color::kPremulGrColorAttribute_Type
128 : : Color::kUnpremulSkColorAttribute_Type;
129 0 : *hasColorAttribute = true;
130 : } else {
131 0 : *hasColorAttribute = false;
132 : };
133 0 : const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
134 0 : return GrDefaultGeoProcFactory::Make(color, Coverage::kSolid_Type, localCoordsType, vm);
135 : }
136 :
137 0 : void GrDrawVerticesOp::onPrepareDraws(Target* target) const {
138 : bool hasColorAttribute;
139 : bool hasLocalCoordsAttribute;
140 0 : sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, &hasLocalCoordsAttribute);
141 0 : size_t vertexStride = gp->getVertexStride();
142 :
143 0 : SkASSERT(vertexStride == sizeof(SkPoint) + (hasColorAttribute ? sizeof(uint32_t) : 0) +
144 : (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0));
145 :
146 0 : int instanceCount = fMeshes.count();
147 :
148 : const GrBuffer* vertexBuffer;
149 : int firstVertex;
150 :
151 0 : void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
152 :
153 0 : if (!verts) {
154 0 : SkDebugf("Could not allocate vertices\n");
155 0 : return;
156 : }
157 :
158 0 : const GrBuffer* indexBuffer = nullptr;
159 0 : int firstIndex = 0;
160 :
161 0 : uint16_t* indices = nullptr;
162 0 : if (this->isIndexed()) {
163 0 : indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
164 :
165 0 : if (!indices) {
166 0 : SkDebugf("Could not allocate indices\n");
167 0 : return;
168 : }
169 : }
170 :
171 0 : int vertexOffset = 0;
172 : // We have a fast case below for uploading the vertex data when the matrix is translate
173 : // only and there are colors but not local coords.
174 0 : bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
175 0 : for (int i = 0; i < instanceCount; i++) {
176 0 : const Mesh& mesh = fMeshes[i];
177 0 : if (indices) {
178 0 : int indexCount = mesh.fVertices->indexCount();
179 0 : for (int j = 0; j < indexCount; ++j) {
180 0 : *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
181 : }
182 : }
183 0 : int vertexCount = mesh.fVertices->vertexCount();
184 0 : const SkPoint* positions = mesh.fVertices->positions();
185 0 : const SkColor* colors = mesh.fVertices->colors();
186 0 : const SkPoint* localCoords = mesh.fVertices->texCoords();
187 0 : bool fastMesh = (!this->hasMultipleViewMatrices() ||
188 0 : mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
189 0 : mesh.hasPerVertexColors();
190 0 : if (fastAttrs && fastMesh) {
191 : struct V {
192 : SkPoint fPos;
193 : uint32_t fColor;
194 : };
195 0 : SkASSERT(sizeof(V) == vertexStride);
196 0 : V* v = (V*)verts;
197 : Sk2f t(0, 0);
198 0 : if (this->hasMultipleViewMatrices()) {
199 0 : t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
200 : }
201 0 : for (int j = 0; j < vertexCount; ++j) {
202 0 : Sk2f p = Sk2f::Load(positions++) + t;
203 0 : p.store(&v[j].fPos);
204 0 : v[j].fColor = colors[j];
205 : }
206 0 : verts = v + vertexCount;
207 : } else {
208 : static constexpr size_t kColorOffset = sizeof(SkPoint);
209 : size_t localCoordOffset =
210 0 : hasColorAttribute ? kColorOffset + sizeof(uint32_t) : kColorOffset;
211 :
212 0 : for (int j = 0; j < vertexCount; ++j) {
213 0 : if (this->hasMultipleViewMatrices()) {
214 0 : mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
215 : } else {
216 0 : *((SkPoint*)verts) = positions[j];
217 : }
218 0 : if (hasColorAttribute) {
219 0 : if (mesh.hasPerVertexColors()) {
220 0 : *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
221 : } else {
222 0 : *(uint32_t*)((intptr_t)verts + kColorOffset) = mesh.fColor;
223 : }
224 : }
225 0 : if (hasLocalCoordsAttribute) {
226 0 : if (mesh.hasExplicitLocalCoords()) {
227 0 : *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
228 : } else {
229 0 : *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
230 : }
231 : }
232 0 : verts = (void*)((intptr_t)verts + vertexStride);
233 : }
234 : }
235 0 : vertexOffset += vertexCount;
236 : }
237 :
238 0 : GrMesh mesh;
239 0 : if (indices) {
240 0 : mesh.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex, firstIndex,
241 0 : fVertexCount, fIndexCount);
242 :
243 : } else {
244 0 : mesh.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexCount);
245 : }
246 0 : target->draw(gp.get(), this->pipeline(), mesh);
247 : }
248 :
249 0 : bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
250 0 : GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>();
251 :
252 0 : if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
253 : that->bounds(), caps)) {
254 0 : return false;
255 : }
256 :
257 0 : if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
258 0 : return false;
259 : }
260 :
261 0 : if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
262 0 : return false;
263 : }
264 :
265 0 : if (fColorArrayType != that->fColorArrayType) {
266 0 : return false;
267 : }
268 :
269 0 : if (fVertexCount + that->fVertexCount > SK_MaxU16) {
270 0 : return false;
271 : }
272 :
273 : // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
274 : // with multiple view matrices.
275 0 : fFlags |= that->fFlags;
276 :
277 0 : if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
278 0 : fFlags |= kRequiresPerVertexColors_Flag;
279 : }
280 : // Check whether we are about to acquire a mesh with a different view matrix.
281 0 : if (!this->hasMultipleViewMatrices() &&
282 0 : !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
283 0 : fFlags |= kHasMultipleViewMatrices_Flag;
284 : }
285 :
286 0 : fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
287 0 : fVertexCount += that->fVertexCount;
288 0 : fIndexCount += that->fIndexCount;
289 :
290 0 : this->joinBounds(*that);
291 0 : return true;
292 : }
293 :
294 : ///////////////////////////////////////////////////////////////////////////////////////////////////
295 :
296 : #if GR_TEST_UTILS
297 :
298 : #include "GrDrawOpTest.h"
299 :
300 0 : static uint32_t seed_vertices(GrPrimitiveType type) {
301 0 : switch (type) {
302 : case kTriangles_GrPrimitiveType:
303 : case kTriangleStrip_GrPrimitiveType:
304 : case kTriangleFan_GrPrimitiveType:
305 0 : return 3;
306 : case kPoints_GrPrimitiveType:
307 0 : return 1;
308 : case kLines_GrPrimitiveType:
309 : case kLineStrip_GrPrimitiveType:
310 0 : return 2;
311 : }
312 0 : SkFAIL("Incomplete switch\n");
313 0 : return 0;
314 : }
315 :
316 0 : static uint32_t primitive_vertices(GrPrimitiveType type) {
317 0 : switch (type) {
318 : case kTriangles_GrPrimitiveType:
319 0 : return 3;
320 : case kLines_GrPrimitiveType:
321 0 : return 2;
322 : case kTriangleStrip_GrPrimitiveType:
323 : case kTriangleFan_GrPrimitiveType:
324 : case kPoints_GrPrimitiveType:
325 : case kLineStrip_GrPrimitiveType:
326 0 : return 1;
327 : }
328 0 : SkFAIL("Incomplete switch\n");
329 0 : return 0;
330 : }
331 :
332 0 : static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
333 : SkPoint p;
334 0 : p.fX = random->nextRangeScalar(min, max);
335 0 : p.fY = random->nextRangeScalar(min, max);
336 0 : return p;
337 : }
338 :
339 0 : static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
340 : SkRandom* random, SkTArray<SkPoint>* positions,
341 : SkTArray<SkPoint>* texCoords, bool hasTexCoords,
342 : SkTArray<uint32_t>* colors, bool hasColors,
343 : SkTArray<uint16_t>* indices, bool hasIndices) {
344 0 : for (uint32_t v = 0; v < count; v++) {
345 0 : positions->push_back(random_point(random, min, max));
346 0 : if (hasTexCoords) {
347 0 : texCoords->push_back(random_point(random, min, max));
348 : }
349 0 : if (hasColors) {
350 0 : colors->push_back(GrRandomColor(random));
351 : }
352 0 : if (hasIndices) {
353 0 : SkASSERT(maxVertex <= SK_MaxU16);
354 0 : indices->push_back(random->nextULessThan((uint16_t)maxVertex));
355 : }
356 : }
357 0 : }
358 :
359 0 : DRAW_OP_TEST_DEFINE(VerticesOp) {
360 0 : GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1));
361 0 : uint32_t primitiveCount = random->nextRangeU(1, 100);
362 :
363 : // TODO make 'sensible' indexbuffers
364 0 : SkTArray<SkPoint> positions;
365 0 : SkTArray<SkPoint> texCoords;
366 0 : SkTArray<uint32_t> colors;
367 0 : SkTArray<uint16_t> indices;
368 :
369 0 : bool hasTexCoords = random->nextBool();
370 0 : bool hasIndices = random->nextBool();
371 0 : bool hasColors = random->nextBool();
372 :
373 0 : uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
374 :
375 : static const SkScalar kMinVertExtent = -100.f;
376 : static const SkScalar kMaxVertExtent = 100.f;
377 0 : randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
378 : &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
379 0 : hasIndices);
380 :
381 0 : for (uint32_t i = 1; i < primitiveCount; i++) {
382 0 : randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
383 : random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
384 0 : hasIndices);
385 : }
386 :
387 : GrRenderTargetContext::ColorArrayType colorArrayType =
388 0 : random->nextBool() ? GrRenderTargetContext::ColorArrayType::kPremulGrColor
389 0 : : GrRenderTargetContext::ColorArrayType::kSkColor;
390 0 : SkMatrix viewMatrix = GrTest::TestMatrix(random);
391 : SkRect bounds;
392 0 : SkDEBUGCODE(bool result =) bounds.setBoundsCheck(positions.begin(), vertexCount);
393 0 : SkASSERT(result);
394 :
395 0 : GrColor color = GrRandomColor(random);
396 0 : return GrDrawVerticesOp::Make(color, type, viewMatrix, positions.begin(), vertexCount,
397 0 : indices.begin(), hasIndices ? indices.count() : 0, colors.begin(),
398 0 : texCoords.begin(), bounds, colorArrayType);
399 : }
400 :
401 : #endif
|