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" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 9 | #include "GrCaps.h" |
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 "GrOpFlushState.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 12 | #include "SkGr.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 13 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 14 | std::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 Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 21 | SkASSERT(vertices); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 22 | GrPrimitiveType primType = overridePrimType ? *overridePrimType |
| 23 | : SkVertexModeToGrPrimitiveType(vertices->mode()); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 24 | return Helper::FactoryHelper<GrDrawVerticesOp>(std::move(paint), std::move(vertices), primType, |
| 25 | aaType, gammaCorrect, std::move(colorSpaceXform), |
| 26 | viewMatrix); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 27 | } |
| 28 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 29 | GrDrawVerticesOp::GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, GrColor color, |
| 30 | sk_sp<SkVertices> vertices, GrPrimitiveType primitiveType, |
| 31 | GrAAType aaType, bool gammaCorrect, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 32 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 33 | const SkMatrix& viewMatrix) |
| 34 | : INHERITED(ClassID()) |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 35 | , fHelper(helperArgs, aaType) |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 36 | , fPrimitiveType(primitiveType) |
| 37 | , fColorSpaceXform(std::move(colorSpaceXform)) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 38 | SkASSERT(vertices); |
| 39 | |
| 40 | fVertexCount = vertices->vertexCount(); |
| 41 | fIndexCount = vertices->indexCount(); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 42 | fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor |
| 43 | : ColorArrayType::kPremulGrColor; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 44 | // GrColor is linearized (and gamut converted) during paint conversion, but SkColors need to be |
| 45 | // handled in the shader |
| 46 | fLinearizeColors = gammaCorrect && vertices->hasColors(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 47 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 48 | Mesh& mesh = fMeshes.push_back(); |
| 49 | mesh.fColor = color; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 50 | mesh.fViewMatrix = viewMatrix; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 51 | mesh.fVertices = std::move(vertices); |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 52 | mesh.fIgnoreTexCoords = false; |
| 53 | mesh.fIgnoreColors = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 54 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 55 | fFlags = 0; |
| 56 | if (mesh.hasPerVertexColors()) { |
| 57 | fFlags |= kRequiresPerVertexColors_Flag; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 58 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 59 | if (mesh.hasExplicitLocalCoords()) { |
| 60 | fFlags |= kAnyMeshHasExplicitLocalCoords; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 61 | } |
| 62 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 63 | IsZeroArea zeroArea; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 64 | if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 65 | zeroArea = IsZeroArea::kYes; |
| 66 | } else { |
| 67 | zeroArea = IsZeroArea::kNo; |
| 68 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 69 | this->setTransformedBounds(mesh.fVertices->bounds(), viewMatrix, HasAABloat::kNo, zeroArea); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 72 | SkString 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; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 81 | GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const { |
| 82 | return fHelper.fixedFunctionFlags(); |
| 83 | } |
| 84 | |
| 85 | GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps, |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 86 | const GrAppliedClip* clip, |
| 87 | GrPixelConfigIsClamped dstIsClamped) { |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 88 | GrProcessorAnalysisColor gpColor; |
| 89 | if (this->requiresPerVertexColors()) { |
| 90 | gpColor.setToUnknown(); |
| 91 | } else { |
| 92 | gpColor.setToConstant(fMeshes.front().fColor); |
| 93 | } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 94 | auto result = fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped, |
| 95 | GrProcessorAnalysisCoverage::kNone, &gpColor); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 96 | if (gpColor.isConstant(&fMeshes.front().fColor)) { |
| 97 | fMeshes.front().fIgnoreColors = true; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 98 | fFlags &= ~kRequiresPerVertexColors_Flag; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 99 | fColorArrayType = ColorArrayType::kPremulGrColor; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 100 | fLinearizeColors = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 101 | } |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 102 | if (!fHelper.usesLocalCoords()) { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 103 | fMeshes[0].fIgnoreTexCoords = true; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 104 | fFlags &= ~kAnyMeshHasExplicitLocalCoords; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 105 | } |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 106 | return result; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 109 | sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(bool* hasColorAttribute, |
| 110 | bool* hasLocalCoordAttribute) const { |
| 111 | using namespace GrDefaultGeoProcFactory; |
| 112 | LocalCoords::Type localCoordsType; |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 113 | if (fHelper.usesLocalCoords()) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 114 | // 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 Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 130 | color.fType = (fColorArrayType == ColorArrayType::kPremulGrColor) |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 131 | ? Color::kPremulGrColorAttribute_Type |
| 132 | : Color::kUnpremulSkColorAttribute_Type; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 133 | color.fLinearize = fLinearizeColors; |
| 134 | color.fColorSpaceXform = fColorSpaceXform; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 135 | *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 Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 143 | void GrDrawVerticesOp::onPrepareDraws(Target* target) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 144 | bool hasColorAttribute; |
| 145 | bool hasLocalCoordsAttribute; |
| 146 | sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, &hasLocalCoordsAttribute); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 147 | size_t vertexStride = gp->getVertexStride(); |
| 148 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 149 | SkASSERT(vertexStride == sizeof(SkPoint) + (hasColorAttribute ? sizeof(uint32_t) : 0) + |
| 150 | (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0)); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 151 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 152 | int instanceCount = fMeshes.count(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 153 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 154 | const GrBuffer* vertexBuffer; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 155 | int firstVertex; |
| 156 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 157 | void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 158 | |
| 159 | if (!verts) { |
| 160 | SkDebugf("Could not allocate vertices\n"); |
| 161 | return; |
| 162 | } |
| 163 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 164 | const GrBuffer* indexBuffer = nullptr; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 165 | int firstIndex = 0; |
| 166 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | uint16_t* indices = nullptr; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 168 | if (this->isIndexed()) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 169 | indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 170 | |
| 171 | if (!indices) { |
| 172 | SkDebugf("Could not allocate indices\n"); |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 177 | int vertexOffset = 0; |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 178 | // 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; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 181 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 182 | const Mesh& mesh = fMeshes[i]; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 183 | if (indices) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 184 | int indexCount = mesh.fVertices->indexCount(); |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 185 | for (int j = 0; j < indexCount; ++j) { |
| 186 | *indices++ = mesh.fVertices->indices()[j] + vertexOffset; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 189 | 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 Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 193 | 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 Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 204 | if (this->hasMultipleViewMatrices()) { |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 205 | t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY()); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 206 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 207 | 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 Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 221 | } else { |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 222 | *((SkPoint*)verts) = positions[j]; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 223 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 224 | 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 Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 230 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 231 | 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); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 239 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 240 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 241 | vertexOffset += vertexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 244 | GrMesh mesh(this->primitiveType()); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 245 | if (!indices) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 246 | mesh.setNonIndexedNonInstanced(fVertexCount); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 247 | } else { |
| 248 | mesh.setIndexed(indexBuffer, fIndexCount, firstIndex, 0, fVertexCount - 1); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 249 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 250 | mesh.setVertexData(vertexBuffer, firstVertex); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 251 | target->draw(gp.get(), fHelper.makePipeline(target), mesh); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 254 | bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
| 255 | GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 256 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 257 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 261 | if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 265 | if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 269 | if (fColorArrayType != that->fColorArrayType) { |
| 270 | return false; |
| 271 | } |
| 272 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 273 | if (fLinearizeColors != that->fLinearizeColors) { |
| 274 | return false; |
| 275 | } |
| 276 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 277 | if (fVertexCount + that->fVertexCount > SK_MaxU16) { |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 281 | // 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 Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 285 | // 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; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 288 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 289 | if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) { |
| 290 | fFlags |= kRequiresPerVertexColors_Flag; |
| 291 | } |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 292 | // Check whether we are about to acquire a mesh with a different view matrix. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 293 | if (!this->hasMultipleViewMatrices() && |
| 294 | !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) { |
| 295 | fFlags |= kHasMultipleViewMatrices_Flag; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 296 | } |
| 297 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 298 | fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin()); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 299 | fVertexCount += that->fVertexCount; |
| 300 | fIndexCount += that->fIndexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 301 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 302 | this->joinBounds(*that); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | |
| 306 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 307 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 308 | #if GR_TEST_UTILS |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 309 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 310 | #include "GrDrawOpTest.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 311 | |
| 312 | static uint32_t seed_vertices(GrPrimitiveType type) { |
| 313 | switch (type) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 314 | case GrPrimitiveType::kTriangles: |
| 315 | case GrPrimitiveType::kTriangleStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 316 | return 3; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 317 | case GrPrimitiveType::kPoints: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 318 | return 1; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 319 | case GrPrimitiveType::kLines: |
| 320 | case GrPrimitiveType::kLineStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 321 | return 2; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 322 | case GrPrimitiveType::kLinesAdjacency: |
| 323 | return 4; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 324 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 325 | SK_ABORT("Incomplete switch\n"); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static uint32_t primitive_vertices(GrPrimitiveType type) { |
| 330 | switch (type) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 331 | case GrPrimitiveType::kTriangles: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 332 | return 3; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 333 | case GrPrimitiveType::kLines: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 334 | return 2; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 335 | case GrPrimitiveType::kTriangleStrip: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 336 | case GrPrimitiveType::kPoints: |
| 337 | case GrPrimitiveType::kLineStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 338 | return 1; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 339 | case GrPrimitiveType::kLinesAdjacency: |
| 340 | return 4; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 341 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 342 | SK_ABORT("Incomplete switch\n"); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static 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 | |
| 353 | 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] | 354 | SkRandom* random, SkTArray<SkPoint>* positions, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 355 | SkTArray<SkPoint>* texCoords, bool hasTexCoords, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 356 | SkTArray<uint32_t>* colors, bool hasColors, |
| 357 | SkTArray<uint16_t>* indices, bool hasIndices) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 358 | 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 Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 373 | GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) { |
Chris Dalton | b894c2b | 2017-06-14 12:39:19 -0600 | [diff] [blame] | 374 | GrPrimitiveType type; |
| 375 | do { |
| 376 | type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes)); |
| 377 | } while (GrPrimTypeRequiresGeometryShaderSupport(type) && |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame^] | 378 | !context->contextPriv().caps()->shaderCaps()->geometryShaderSupport()); |
Chris Dalton | b894c2b | 2017-06-14 12:39:19 -0600 | [diff] [blame] | 379 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 380 | uint32_t primitiveCount = random->nextRangeU(1, 100); |
| 381 | |
| 382 | // TODO make 'sensible' indexbuffers |
| 383 | SkTArray<SkPoint> positions; |
| 384 | SkTArray<SkPoint> texCoords; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 385 | SkTArray<uint32_t> colors; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 386 | SkTArray<uint16_t> indices; |
| 387 | |
| 388 | bool hasTexCoords = random->nextBool(); |
| 389 | bool hasIndices = random->nextBool(); |
| 390 | bool hasColors = random->nextBool(); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 391 | bool linearizeColors = random->nextBool(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 392 | |
| 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 Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 397 | randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random, |
| 398 | &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 399 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 400 | |
| 401 | for (uint32_t i = 1; i < primitiveCount; i++) { |
| 402 | randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 403 | random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 404 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 408 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 409 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 410 | |
| 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 Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 416 | 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); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | #endif |