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 | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 41 | const SkMatrix bones[], |
| 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 | |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 48 | GrDrawVerticesOp(const Helper::MakeArgs&, GrColor, sk_sp<SkVertices>, const SkMatrix bones[], |
| 49 | int boneCount, GrPrimitiveType, GrAAType, sk_sp<GrColorSpaceXform>, |
| 50 | 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 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 54 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 55 | fHelper.visitProxies(func); |
| 56 | } |
| 57 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 58 | SkString dumpInfo() const override; |
| 59 | |
| 60 | FixedFunctionFlags fixedFunctionFlags() const override; |
| 61 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 62 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 63 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 64 | private: |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 65 | enum class ColorArrayType { |
| 66 | kPremulGrColor, |
| 67 | kSkColor, |
| 68 | }; |
| 69 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 70 | void onPrepareDraws(Target*) override; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 71 | |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 72 | void drawVolatile(Target*); |
| 73 | void drawNonVolatile(Target*); |
| 74 | |
| 75 | void fillBuffers(bool hasColorAttribute, |
| 76 | bool hasLocalCoordsAttribute, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 77 | bool hasBoneAttribute, |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 78 | size_t vertexStride, |
| 79 | void* verts, |
| 80 | uint16_t* indices) const; |
| 81 | |
| 82 | void drawVertices(Target*, |
| 83 | GrGeometryProcessor*, |
| 84 | const GrBuffer* vertexBuffer, |
| 85 | int firstVertex, |
| 86 | const GrBuffer* indexBuffer, |
| 87 | int firstIndex); |
| 88 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 89 | sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps, |
| 90 | bool* hasColorAttribute, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 91 | bool* hasLocalCoordAttribute, |
| 92 | bool* hasBoneAttribute) const; |
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 | GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 95 | bool combinablePrimitive() const { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 96 | return GrPrimitiveType::kTriangles == fPrimitiveType || |
| 97 | GrPrimitiveType::kLines == fPrimitiveType || |
| 98 | GrPrimitiveType::kPoints == fPrimitiveType; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | bool onCombineIfPossible(GrOp* t, const GrCaps&) override; |
| 102 | |
| 103 | struct Mesh { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 104 | GrColor fColor; // Used if this->hasPerVertexColors() is false. |
| 105 | sk_sp<SkVertices> fVertices; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 106 | std::vector<float> fBones; // Transformation matrices stored in GPU format. |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 107 | SkMatrix fViewMatrix; |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 108 | bool fIgnoreTexCoords; |
| 109 | bool fIgnoreColors; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 110 | bool fIgnoreBones; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 111 | |
| 112 | bool hasExplicitLocalCoords() const { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 113 | return fVertices->hasTexCoords() && !fIgnoreTexCoords; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool hasPerVertexColors() const { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 117 | return fVertices->hasColors() && !fIgnoreColors; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 118 | } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 119 | |
| 120 | bool hasBones() const { |
| 121 | return fVertices->hasBones() && fBones.size() && !fIgnoreBones; |
| 122 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 123 | }; |
| 124 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 125 | bool isIndexed() const { |
| 126 | // Consistency enforced in onCombineIfPossible. |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 127 | return fMeshes[0].fVertices->hasIndices(); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool requiresPerVertexColors() const { |
| 131 | return SkToBool(kRequiresPerVertexColors_Flag & fFlags); |
| 132 | } |
| 133 | |
| 134 | bool anyMeshHasExplicitLocalCoords() const { |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 135 | return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 138 | bool hasMultipleViewMatrices() const { |
| 139 | return SkToBool(kHasMultipleViewMatrices_Flag & fFlags); |
| 140 | } |
| 141 | |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 142 | bool hasBones() const { |
| 143 | return SkToBool(kHasBones_Flag & fFlags); |
| 144 | } |
| 145 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 146 | enum Flags { |
Ruiqi Mao | 9a6e42f | 2018-07-09 14:16:56 -0400 | [diff] [blame] | 147 | kRequiresPerVertexColors_Flag = 0x1, |
| 148 | kAnyMeshHasExplicitLocalCoords_Flag = 0x2, |
| 149 | kHasMultipleViewMatrices_Flag = 0x4, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 150 | kHasBones_Flag = 0x8, |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 151 | }; |
| 152 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 153 | Helper fHelper; |
| 154 | SkSTArray<1, Mesh, true> fMeshes; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 155 | // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore |
| 156 | // the SkVertices mode (though fPrimitiveType may have been inferred from it). |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 157 | GrPrimitiveType fPrimitiveType; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 158 | uint32_t fFlags; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 159 | int fVertexCount; |
| 160 | int fIndexCount; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 161 | ColorArrayType fColorArrayType; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 162 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 163 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 164 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | #endif |