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 | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 14 | #include "GrSimpleMeshDrawOpHelper.h" |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 15 | #include "GrTypes.h" |
| 16 | #include "SkMatrix.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "SkTDArray.h" |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 19 | #include "SkVertices.h" |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 20 | |
| 21 | class GrOpFlushState; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 22 | class SkVertices; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 23 | struct GrInitInvariantOutput; |
| 24 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 25 | class GrDrawVerticesOp final : public GrMeshDrawOp { |
| 26 | private: |
| 27 | using Helper = GrSimpleMeshDrawOpHelper; |
| 28 | |
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 Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 33 | * Draw a SkVertices. The GrPaint param's color is used if the vertices lack per-vertex color. |
| 34 | * If the vertices lack local coords then the vertex positions are used as local coords. The |
| 35 | * primitive type drawn is derived from the SkVertices object, unless overridePrimType is |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 36 | * specified. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 37 | */ |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 38 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 39 | GrPaint&&, |
| 40 | sk_sp<SkVertices>, |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 41 | const SkVertices::Bone bones[], |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 42 | int boneCount, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 43 | const SkMatrix& viewMatrix, |
| 44 | GrAAType, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 45 | sk_sp<GrColorSpaceXform>, |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 46 | GrPrimitiveType* overridePrimType = nullptr); |
| 47 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 48 | GrDrawVerticesOp(const Helper::MakeArgs&, const SkPMColor4f&, sk_sp<SkVertices>, |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 49 | const SkVertices::Bone bones[], int boneCount, GrPrimitiveType, GrAAType, |
| 50 | sk_sp<GrColorSpaceXform>, const SkMatrix& viewMatrix); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 51 | |
| 52 | const char* name() const override { return "DrawVerticesOp"; } |
| 53 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 54 | void visitProxies(const VisitProxyFunc& func, VisitorType) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 55 | fHelper.visitProxies(func); |
| 56 | } |
| 57 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 58 | #ifdef SK_DEBUG |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 59 | SkString dumpInfo() const override; |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 60 | #endif |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 61 | |
| 62 | FixedFunctionFlags fixedFunctionFlags() const override; |
| 63 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 64 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 65 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 66 | private: |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 67 | enum class ColorArrayType { |
| 68 | kPremulGrColor, |
| 69 | kSkColor, |
| 70 | }; |
| 71 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 72 | void onPrepareDraws(Target*) override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 73 | |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 74 | void drawVolatile(Target*); |
| 75 | void drawNonVolatile(Target*); |
| 76 | |
| 77 | void fillBuffers(bool hasColorAttribute, |
| 78 | bool hasLocalCoordsAttribute, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 79 | bool hasBoneAttribute, |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 80 | size_t vertexStride, |
| 81 | void* verts, |
| 82 | uint16_t* indices) const; |
| 83 | |
| 84 | void drawVertices(Target*, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 85 | sk_sp<const GrGeometryProcessor>, |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 86 | const GrBuffer* vertexBuffer, |
| 87 | int firstVertex, |
| 88 | const GrBuffer* indexBuffer, |
| 89 | int firstIndex); |
| 90 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 91 | sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps, |
| 92 | bool* hasColorAttribute, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 93 | bool* hasLocalCoordAttribute, |
| 94 | bool* hasBoneAttribute) const; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 95 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 96 | GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 97 | bool combinablePrimitive() const { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 98 | return GrPrimitiveType::kTriangles == fPrimitiveType || |
| 99 | GrPrimitiveType::kLines == fPrimitiveType || |
| 100 | GrPrimitiveType::kPoints == fPrimitiveType; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 103 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 104 | |
| 105 | struct Mesh { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 106 | SkPMColor4f fColor; // Used if this->hasPerVertexColors() is false. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 107 | sk_sp<SkVertices> fVertices; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 108 | SkMatrix fViewMatrix; |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 109 | bool fIgnoreTexCoords; |
| 110 | bool fIgnoreColors; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 111 | bool fIgnoreBones; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 112 | |
| 113 | bool hasExplicitLocalCoords() const { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 114 | return fVertices->hasTexCoords() && !fIgnoreTexCoords; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool hasPerVertexColors() const { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 118 | return fVertices->hasColors() && !fIgnoreColors; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 119 | } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 120 | |
| 121 | bool hasBones() const { |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 122 | return fVertices->hasBones() && !fIgnoreBones; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 123 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 124 | }; |
| 125 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 126 | bool isIndexed() const { |
| 127 | // Consistency enforced in onCombineIfPossible. |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 128 | return fMeshes[0].fVertices->hasIndices(); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool requiresPerVertexColors() const { |
| 132 | return SkToBool(kRequiresPerVertexColors_Flag & fFlags); |
| 133 | } |
| 134 | |
| 135 | bool anyMeshHasExplicitLocalCoords() const { |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 136 | return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 139 | bool hasMultipleViewMatrices() const { |
| 140 | return SkToBool(kHasMultipleViewMatrices_Flag & fFlags); |
| 141 | } |
| 142 | |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 143 | bool hasBones() const { |
| 144 | return SkToBool(kHasBones_Flag & fFlags); |
| 145 | } |
| 146 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 147 | enum Flags { |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 148 | kRequiresPerVertexColors_Flag = 0x1, |
| 149 | kAnyMeshHasExplicitLocalCoords_Flag = 0x2, |
| 150 | kHasMultipleViewMatrices_Flag = 0x4, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 151 | kHasBones_Flag = 0x8, |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 152 | }; |
| 153 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 154 | Helper fHelper; |
| 155 | SkSTArray<1, Mesh, true> fMeshes; |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 156 | std::vector<SkVertices::Bone> fBones; // Bone transformation matrices. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 157 | // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore |
| 158 | // the SkVertices mode (though fPrimitiveType may have been inferred from it). |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 159 | GrPrimitiveType fPrimitiveType; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 160 | uint32_t fFlags; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 161 | int fVertexCount; |
| 162 | int fIndexCount; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 163 | ColorArrayType fColorArrayType; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 164 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 165 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 166 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | #endif |