Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [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 | |
| 8 | #ifndef GrDrawVerticesOp_DEFINED |
| 9 | #define GrDrawVerticesOp_DEFINED |
| 10 | |
| 11 | #include "GrColor.h" |
| 12 | #include "GrMeshDrawOp.h" |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 13 | #include "GrRenderTargetContext.h" |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 14 | #include "GrTypes.h" |
| 15 | #include "SkMatrix.h" |
| 16 | #include "SkRect.h" |
| 17 | #include "SkTDArray.h" |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 18 | #include "SkVertices.h" |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 19 | |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 20 | #if GR_TEST_UTILS |
| 21 | #include "GrDrawOpTest.h" |
| 22 | #endif |
| 23 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 24 | class GrOpFlushState; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 25 | class SkVertices; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 26 | struct GrInitInvariantOutput; |
| 27 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 28 | class GrDrawVerticesOp final : public GrLegacyMeshDrawOp { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 29 | public: |
| 30 | DEFINE_OP_CLASS_ID |
| 31 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 32 | /** |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 33 | * Draw a SkVertices. The GrColor param is used if the vertices lack per-vertex color. If the |
| 34 | * vertices lack local coords then the vertex positions are used as local coords. The primitive |
| 35 | * type drawn is derived from the SkVertices object, unless overridePrimType is specified. |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 36 | * If gammaCorrect is true, the vertex colors will be linearized in the shader to get correct |
| 37 | * rendering. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 38 | */ |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 39 | static std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, sk_sp<SkVertices>, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 40 | const SkMatrix& viewMatrix, bool gammaCorrect, |
| 41 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 42 | GrPrimitiveType* overridePrimType = nullptr); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 43 | |
| 44 | const char* name() const override { return "DrawVerticesOp"; } |
| 45 | |
| 46 | SkString dumpInfo() const override { |
| 47 | SkString string; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 48 | string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", fPrimitiveType, |
| 49 | fMeshes.count(), fVertexCount, fIndexCount); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 50 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 51 | string.append(INHERITED::dumpInfo()); |
| 52 | return string; |
| 53 | } |
| 54 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 55 | private: |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 56 | enum class ColorArrayType { |
| 57 | kPremulGrColor, |
| 58 | kSkColor, |
| 59 | }; |
| 60 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 61 | GrDrawVerticesOp(sk_sp<SkVertices>, GrPrimitiveType, GrColor, bool gammaCorrect, |
| 62 | sk_sp<GrColorSpaceXform>, const SkMatrix& viewMatrix); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 63 | |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 64 | void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color, |
| 65 | GrProcessorAnalysisCoverage* coverage) const override; |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 66 | void applyPipelineOptimizations(const PipelineOptimizations&) override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 67 | void onPrepareDraws(Target*) const override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 68 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 69 | sk_sp<GrGeometryProcessor> makeGP(bool* hasColorAttribute, bool* hasLocalCoordAttribute) const; |
| 70 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 71 | GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 72 | bool combinablePrimitive() const { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 73 | return kTriangles_GrPrimitiveType == fPrimitiveType || |
| 74 | kLines_GrPrimitiveType == fPrimitiveType || |
| 75 | kPoints_GrPrimitiveType == fPrimitiveType; |
| 76 | } |
| 77 | |
| 78 | bool onCombineIfPossible(GrOp* t, const GrCaps&) override; |
| 79 | |
| 80 | struct Mesh { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 81 | GrColor fColor; // Used if this->hasPerVertexColors() is false. |
| 82 | sk_sp<SkVertices> fVertices; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 83 | SkMatrix fViewMatrix; |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 84 | bool fIgnoreTexCoords; |
| 85 | bool fIgnoreColors; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 86 | |
| 87 | bool hasExplicitLocalCoords() const { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 88 | return fVertices->hasTexCoords() && !fIgnoreTexCoords; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool hasPerVertexColors() const { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 92 | return fVertices->hasColors() && !fIgnoreColors; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 93 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 96 | bool isIndexed() const { |
| 97 | // Consistency enforced in onCombineIfPossible. |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 98 | return fMeshes[0].fVertices->hasIndices(); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool requiresPerVertexColors() const { |
| 102 | return SkToBool(kRequiresPerVertexColors_Flag & fFlags); |
| 103 | } |
| 104 | |
| 105 | bool anyMeshHasExplicitLocalCoords() const { |
| 106 | return SkToBool(kAnyMeshHasExplicitLocalCoords & fFlags); |
| 107 | } |
| 108 | |
| 109 | bool pipelineRequiresLocalCoords() const { |
| 110 | return SkToBool(kPipelineRequiresLocalCoords_Flag & fFlags); |
| 111 | } |
| 112 | |
| 113 | bool hasMultipleViewMatrices() const { |
| 114 | return SkToBool(kHasMultipleViewMatrices_Flag & fFlags); |
| 115 | } |
| 116 | |
| 117 | enum Flags { |
| 118 | kRequiresPerVertexColors_Flag = 0x1, |
| 119 | kAnyMeshHasExplicitLocalCoords = 0x2, |
| 120 | kPipelineRequiresLocalCoords_Flag = 0x4, |
| 121 | kHasMultipleViewMatrices_Flag = 0x8 |
| 122 | |
| 123 | }; |
| 124 | |
| 125 | // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore |
| 126 | // the SkVertices mode (though fPrimitiveType may have been inferred from it). |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 127 | GrPrimitiveType fPrimitiveType; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 128 | uint32_t fFlags; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 129 | int fVertexCount; |
| 130 | int fIndexCount; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 131 | ColorArrayType fColorArrayType; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 132 | bool fLinearizeColors; |
| 133 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 134 | SkSTArray<1, Mesh, true> fMeshes; |
| 135 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 136 | typedef GrLegacyMeshDrawOp INHERITED; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 137 | |
| 138 | #if GR_TEST_UTILS |
| 139 | GR_LEGACY_MESH_DRAW_OP_TEST_FRIEND(VerticesOp); |
| 140 | #endif |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #endif |