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" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 9 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 10 | #include "GrOpFlushState.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 11 | #include "SkGr.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 12 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 13 | std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrPaint&& paint, |
| 14 | sk_sp<SkVertices> vertices, |
| 15 | const SkMatrix& viewMatrix, |
| 16 | GrAAType aaType, |
| 17 | bool gammaCorrect, |
| 18 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 19 | GrPrimitiveType* overridePrimType) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 20 | SkASSERT(vertices); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 21 | GrPrimitiveType primType = overridePrimType ? *overridePrimType |
| 22 | : SkVertexModeToGrPrimitiveType(vertices->mode()); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 23 | return Helper::FactoryHelper<GrDrawVerticesOp>(std::move(paint), std::move(vertices), primType, |
| 24 | aaType, gammaCorrect, std::move(colorSpaceXform), |
| 25 | viewMatrix); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 26 | } |
| 27 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 28 | GrDrawVerticesOp::GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, GrColor color, |
| 29 | sk_sp<SkVertices> vertices, GrPrimitiveType primitiveType, |
| 30 | GrAAType aaType, bool gammaCorrect, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 31 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 32 | const SkMatrix& viewMatrix) |
| 33 | : INHERITED(ClassID()) |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 34 | , fHelper(helperArgs, aaType) |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 35 | , fPrimitiveType(primitiveType) |
| 36 | , fColorSpaceXform(std::move(colorSpaceXform)) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 37 | SkASSERT(vertices); |
| 38 | |
| 39 | fVertexCount = vertices->vertexCount(); |
| 40 | fIndexCount = vertices->indexCount(); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 41 | fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor |
| 42 | : ColorArrayType::kPremulGrColor; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 43 | // GrColor is linearized (and gamut converted) during paint conversion, but SkColors need to be |
| 44 | // handled in the shader |
| 45 | fLinearizeColors = gammaCorrect && vertices->hasColors(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 46 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 47 | Mesh& mesh = fMeshes.push_back(); |
| 48 | mesh.fColor = color; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 49 | mesh.fViewMatrix = viewMatrix; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 50 | mesh.fVertices = std::move(vertices); |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 51 | mesh.fIgnoreTexCoords = false; |
| 52 | mesh.fIgnoreColors = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 53 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 54 | fFlags = 0; |
| 55 | if (mesh.hasPerVertexColors()) { |
| 56 | fFlags |= kRequiresPerVertexColors_Flag; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 57 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 58 | if (mesh.hasExplicitLocalCoords()) { |
| 59 | fFlags |= kAnyMeshHasExplicitLocalCoords; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 62 | IsZeroArea zeroArea; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 63 | if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 64 | zeroArea = IsZeroArea::kYes; |
| 65 | } else { |
| 66 | zeroArea = IsZeroArea::kNo; |
| 67 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 68 | this->setTransformedBounds(mesh.fVertices->bounds(), viewMatrix, HasAABloat::kNo, zeroArea); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 71 | SkString GrDrawVerticesOp::dumpInfo() const { |
| 72 | SkString string; |
| 73 | string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType, |
| 74 | fMeshes.count(), fVertexCount, fIndexCount); |
| 75 | string += fHelper.dumpInfo(); |
| 76 | string += INHERITED::dumpInfo(); |
| 77 | return string; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 80 | GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const { |
| 81 | return fHelper.fixedFunctionFlags(); |
| 82 | } |
| 83 | |
| 84 | GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps, |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 85 | const GrAppliedClip* clip, |
| 86 | GrPixelConfigIsClamped dstIsClamped) { |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 87 | GrProcessorAnalysisColor gpColor; |
| 88 | if (this->requiresPerVertexColors()) { |
| 89 | gpColor.setToUnknown(); |
| 90 | } else { |
| 91 | gpColor.setToConstant(fMeshes.front().fColor); |
| 92 | } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 93 | auto result = fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped, |
| 94 | GrProcessorAnalysisCoverage::kNone, &gpColor); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 95 | if (gpColor.isConstant(&fMeshes.front().fColor)) { |
| 96 | fMeshes.front().fIgnoreColors = true; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 97 | fFlags &= ~kRequiresPerVertexColors_Flag; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 98 | fColorArrayType = ColorArrayType::kPremulGrColor; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 99 | fLinearizeColors = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 100 | } |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 101 | if (!fHelper.usesLocalCoords()) { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 102 | fMeshes[0].fIgnoreTexCoords = true; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 103 | fFlags &= ~kAnyMeshHasExplicitLocalCoords; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 104 | } |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 105 | return result; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 108 | sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(bool* hasColorAttribute, |
| 109 | bool* hasLocalCoordAttribute) const { |
| 110 | using namespace GrDefaultGeoProcFactory; |
| 111 | LocalCoords::Type localCoordsType; |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 112 | if (fHelper.usesLocalCoords()) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 113 | // If we have multiple view matrices we will transform the positions into device space. We |
| 114 | // must then also provide untransformed positions as local coords. |
| 115 | if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) { |
| 116 | *hasLocalCoordAttribute = true; |
| 117 | localCoordsType = LocalCoords::kHasExplicit_Type; |
| 118 | } else { |
| 119 | *hasLocalCoordAttribute = false; |
| 120 | localCoordsType = LocalCoords::kUsePosition_Type; |
| 121 | } |
| 122 | } else { |
| 123 | localCoordsType = LocalCoords::kUnused_Type; |
| 124 | *hasLocalCoordAttribute = false; |
| 125 | } |
| 126 | |
| 127 | Color color(fMeshes[0].fColor); |
| 128 | if (this->requiresPerVertexColors()) { |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 129 | color.fType = (fColorArrayType == ColorArrayType::kPremulGrColor) |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 130 | ? Color::kPremulGrColorAttribute_Type |
| 131 | : Color::kUnpremulSkColorAttribute_Type; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 132 | color.fLinearize = fLinearizeColors; |
| 133 | color.fColorSpaceXform = fColorSpaceXform; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 134 | *hasColorAttribute = true; |
| 135 | } else { |
| 136 | *hasColorAttribute = false; |
| 137 | }; |
| 138 | const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix; |
| 139 | return GrDefaultGeoProcFactory::Make(color, Coverage::kSolid_Type, localCoordsType, vm); |
| 140 | } |
| 141 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 142 | void GrDrawVerticesOp::onPrepareDraws(Target* target) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 143 | bool hasColorAttribute; |
| 144 | bool hasLocalCoordsAttribute; |
| 145 | sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, &hasLocalCoordsAttribute); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 146 | size_t vertexStride = gp->getVertexStride(); |
| 147 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 148 | SkASSERT(vertexStride == sizeof(SkPoint) + (hasColorAttribute ? sizeof(uint32_t) : 0) + |
| 149 | (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0)); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 150 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 151 | int instanceCount = fMeshes.count(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 152 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 153 | const GrBuffer* vertexBuffer; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 154 | int firstVertex; |
| 155 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 156 | void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 157 | |
| 158 | if (!verts) { |
| 159 | SkDebugf("Could not allocate vertices\n"); |
| 160 | return; |
| 161 | } |
| 162 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 163 | const GrBuffer* indexBuffer = nullptr; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 164 | int firstIndex = 0; |
| 165 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | uint16_t* indices = nullptr; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 167 | if (this->isIndexed()) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 168 | indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 169 | |
| 170 | if (!indices) { |
| 171 | SkDebugf("Could not allocate indices\n"); |
| 172 | return; |
| 173 | } |
| 174 | } |
| 175 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 176 | int vertexOffset = 0; |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 177 | // We have a fast case below for uploading the vertex data when the matrix is translate |
| 178 | // only and there are colors but not local coords. |
| 179 | bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 180 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 181 | const Mesh& mesh = fMeshes[i]; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 182 | if (indices) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 183 | int indexCount = mesh.fVertices->indexCount(); |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 184 | for (int j = 0; j < indexCount; ++j) { |
| 185 | *indices++ = mesh.fVertices->indices()[j] + vertexOffset; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 188 | int vertexCount = mesh.fVertices->vertexCount(); |
| 189 | const SkPoint* positions = mesh.fVertices->positions(); |
| 190 | const SkColor* colors = mesh.fVertices->colors(); |
| 191 | const SkPoint* localCoords = mesh.fVertices->texCoords(); |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 192 | bool fastMesh = (!this->hasMultipleViewMatrices() || |
| 193 | mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) && |
| 194 | mesh.hasPerVertexColors(); |
| 195 | if (fastAttrs && fastMesh) { |
| 196 | struct V { |
| 197 | SkPoint fPos; |
| 198 | uint32_t fColor; |
| 199 | }; |
| 200 | SkASSERT(sizeof(V) == vertexStride); |
| 201 | V* v = (V*)verts; |
| 202 | Sk2f t(0, 0); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 203 | if (this->hasMultipleViewMatrices()) { |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 204 | t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY()); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 205 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 206 | for (int j = 0; j < vertexCount; ++j) { |
| 207 | Sk2f p = Sk2f::Load(positions++) + t; |
| 208 | p.store(&v[j].fPos); |
| 209 | v[j].fColor = colors[j]; |
| 210 | } |
| 211 | verts = v + vertexCount; |
| 212 | } else { |
| 213 | static constexpr size_t kColorOffset = sizeof(SkPoint); |
| 214 | size_t localCoordOffset = |
| 215 | hasColorAttribute ? kColorOffset + sizeof(uint32_t) : kColorOffset; |
| 216 | |
| 217 | for (int j = 0; j < vertexCount; ++j) { |
| 218 | if (this->hasMultipleViewMatrices()) { |
| 219 | mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 220 | } else { |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 221 | *((SkPoint*)verts) = positions[j]; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 222 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 223 | if (hasColorAttribute) { |
| 224 | if (mesh.hasPerVertexColors()) { |
| 225 | *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j]; |
| 226 | } else { |
| 227 | *(uint32_t*)((intptr_t)verts + kColorOffset) = mesh.fColor; |
| 228 | } |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 229 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 230 | if (hasLocalCoordsAttribute) { |
| 231 | if (mesh.hasExplicitLocalCoords()) { |
| 232 | *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j]; |
| 233 | } else { |
| 234 | *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j]; |
| 235 | } |
| 236 | } |
| 237 | verts = (void*)((intptr_t)verts + vertexStride); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 238 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 239 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 240 | vertexOffset += vertexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 243 | GrMesh mesh(this->primitiveType()); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 244 | if (!indices) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 245 | mesh.setNonIndexedNonInstanced(fVertexCount); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 246 | } else { |
| 247 | mesh.setIndexed(indexBuffer, fIndexCount, firstIndex, 0, fVertexCount - 1); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 248 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 249 | mesh.setVertexData(vertexBuffer, firstVertex); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 250 | target->draw(gp.get(), fHelper.makePipeline(target), mesh); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 253 | bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
| 254 | GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 255 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 256 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 260 | if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 264 | if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 268 | if (fColorArrayType != that->fColorArrayType) { |
| 269 | return false; |
| 270 | } |
| 271 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 272 | if (fLinearizeColors != that->fLinearizeColors) { |
| 273 | return false; |
| 274 | } |
| 275 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 276 | if (fVertexCount + that->fVertexCount > SK_MaxU16) { |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 280 | // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination |
| 281 | // gamut is determined by the render target context. A mis-match should be impossible. |
| 282 | SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())); |
| 283 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 284 | // If either op required explicit local coords or per-vertex colors the combined mesh does. Same |
| 285 | // with multiple view matrices. |
| 286 | fFlags |= that->fFlags; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 287 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 288 | if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) { |
| 289 | fFlags |= kRequiresPerVertexColors_Flag; |
| 290 | } |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 291 | // 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] | 292 | if (!this->hasMultipleViewMatrices() && |
| 293 | !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) { |
| 294 | fFlags |= kHasMultipleViewMatrices_Flag; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 295 | } |
| 296 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 297 | fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin()); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 298 | fVertexCount += that->fVertexCount; |
| 299 | fIndexCount += that->fIndexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 300 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 301 | this->joinBounds(*that); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 302 | return true; |
| 303 | } |
| 304 | |
| 305 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 306 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 307 | #if GR_TEST_UTILS |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 308 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 309 | #include "GrDrawOpTest.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 310 | |
| 311 | static uint32_t seed_vertices(GrPrimitiveType type) { |
| 312 | switch (type) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 313 | case GrPrimitiveType::kTriangles: |
| 314 | case GrPrimitiveType::kTriangleStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 315 | return 3; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 316 | case GrPrimitiveType::kPoints: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 317 | return 1; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 318 | case GrPrimitiveType::kLines: |
| 319 | case GrPrimitiveType::kLineStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 320 | return 2; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 321 | case GrPrimitiveType::kLinesAdjacency: |
| 322 | return 4; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 323 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 324 | SK_ABORT("Incomplete switch\n"); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static uint32_t primitive_vertices(GrPrimitiveType type) { |
| 329 | switch (type) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 330 | case GrPrimitiveType::kTriangles: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 331 | return 3; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 332 | case GrPrimitiveType::kLines: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 333 | return 2; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 334 | case GrPrimitiveType::kTriangleStrip: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 335 | case GrPrimitiveType::kPoints: |
| 336 | case GrPrimitiveType::kLineStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 337 | return 1; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 338 | case GrPrimitiveType::kLinesAdjacency: |
| 339 | return 4; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 340 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 341 | SK_ABORT("Incomplete switch\n"); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) { |
| 346 | SkPoint p; |
| 347 | p.fX = random->nextRangeScalar(min, max); |
| 348 | p.fY = random->nextRangeScalar(min, max); |
| 349 | return p; |
| 350 | } |
| 351 | |
| 352 | 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] | 353 | SkRandom* random, SkTArray<SkPoint>* positions, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 354 | SkTArray<SkPoint>* texCoords, bool hasTexCoords, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 355 | SkTArray<uint32_t>* colors, bool hasColors, |
| 356 | SkTArray<uint16_t>* indices, bool hasIndices) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 357 | for (uint32_t v = 0; v < count; v++) { |
| 358 | positions->push_back(random_point(random, min, max)); |
| 359 | if (hasTexCoords) { |
| 360 | texCoords->push_back(random_point(random, min, max)); |
| 361 | } |
| 362 | if (hasColors) { |
| 363 | colors->push_back(GrRandomColor(random)); |
| 364 | } |
| 365 | if (hasIndices) { |
| 366 | SkASSERT(maxVertex <= SK_MaxU16); |
| 367 | indices->push_back(random->nextULessThan((uint16_t)maxVertex)); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 372 | GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) { |
Chris Dalton | b894c2b | 2017-06-14 12:39:19 -0600 | [diff] [blame] | 373 | GrPrimitiveType type; |
| 374 | do { |
| 375 | type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes)); |
| 376 | } while (GrPrimTypeRequiresGeometryShaderSupport(type) && |
| 377 | !context->caps()->shaderCaps()->geometryShaderSupport()); |
| 378 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 379 | uint32_t primitiveCount = random->nextRangeU(1, 100); |
| 380 | |
| 381 | // TODO make 'sensible' indexbuffers |
| 382 | SkTArray<SkPoint> positions; |
| 383 | SkTArray<SkPoint> texCoords; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 384 | SkTArray<uint32_t> colors; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 385 | SkTArray<uint16_t> indices; |
| 386 | |
| 387 | bool hasTexCoords = random->nextBool(); |
| 388 | bool hasIndices = random->nextBool(); |
| 389 | bool hasColors = random->nextBool(); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 390 | bool linearizeColors = random->nextBool(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 391 | |
| 392 | uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type); |
| 393 | |
| 394 | static const SkScalar kMinVertExtent = -100.f; |
| 395 | static const SkScalar kMaxVertExtent = 100.f; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 396 | randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random, |
| 397 | &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 398 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 399 | |
| 400 | for (uint32_t i = 1; i < primitiveCount; i++) { |
| 401 | randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 402 | random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 403 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 407 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 408 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 409 | |
| 410 | static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode; |
| 411 | sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(), |
| 412 | texCoords.begin(), colors.begin(), |
| 413 | hasIndices ? indices.count() : 0, |
| 414 | indices.begin()); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 415 | GrAAType aaType = GrAAType::kNone; |
| 416 | if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) { |
| 417 | aaType = GrAAType::kMSAA; |
| 418 | } |
| 419 | return GrDrawVerticesOp::Make(std::move(paint), std::move(vertices), viewMatrix, aaType, |
| 420 | linearizeColors, std::move(colorSpaceXform), &type); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | #endif |