blob: d160dee4734bf65e93464f424f77653b47de54a4 [file] [log] [blame]
Brian Salomonfc527d22016-12-14 21:07:01 -05001/*
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 Salomon3de0aee2017-01-29 09:34:17 -050013#include "GrRenderTargetContext.h"
Brian Salomonc2f42542017-07-12 14:11:22 -040014#include "GrSimpleMeshDrawOpHelper.h"
Brian Salomonfc527d22016-12-14 21:07:01 -050015#include "GrTypes.h"
16#include "SkMatrix.h"
17#include "SkRect.h"
18#include "SkTDArray.h"
Brian Salomon199fb872017-02-06 09:41:10 -050019#include "SkVertices.h"
Brian Salomonfc527d22016-12-14 21:07:01 -050020
21class GrOpFlushState;
Brian Salomon199fb872017-02-06 09:41:10 -050022class SkVertices;
Brian Salomonfc527d22016-12-14 21:07:01 -050023struct GrInitInvariantOutput;
24
Brian Salomonc2f42542017-07-12 14:11:22 -040025class GrDrawVerticesOp final : public GrMeshDrawOp {
26private:
27 using Helper = GrSimpleMeshDrawOpHelper;
28
Brian Salomonfc527d22016-12-14 21:07:01 -050029public:
30 DEFINE_OP_CLASS_ID
31
Brian Salomon199fb872017-02-06 09:41:10 -050032 /**
Brian Salomonc2f42542017-07-12 14:11:22 -040033 * 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 Osman08a50e02018-06-15 15:06:48 -040036 * specified.
Brian Salomon199fb872017-02-06 09:41:10 -050037 */
Robert Phillips7c525e62018-06-12 10:11:12 -040038 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
39 GrPaint&&,
40 sk_sp<SkVertices>,
Ruiqi Maoc97a3392018-08-15 10:44:19 -040041 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040042 int boneCount,
Robert Phillips7c525e62018-06-12 10:11:12 -040043 const SkMatrix& viewMatrix,
44 GrAAType,
Robert Phillips7c525e62018-06-12 10:11:12 -040045 sk_sp<GrColorSpaceXform>,
Brian Salomonc2f42542017-07-12 14:11:22 -040046 GrPrimitiveType* overridePrimType = nullptr);
47
Brian Osmancf860852018-10-31 14:04:39 -040048 GrDrawVerticesOp(const Helper::MakeArgs&, const SkPMColor4f&, sk_sp<SkVertices>,
Ruiqi Maoc97a3392018-08-15 10:44:19 -040049 const SkVertices::Bone bones[], int boneCount, GrPrimitiveType, GrAAType,
50 sk_sp<GrColorSpaceXform>, const SkMatrix& viewMatrix);
Brian Salomonfc527d22016-12-14 21:07:01 -050051
52 const char* name() const override { return "DrawVerticesOp"; }
53
Brian Salomon7d94bb52018-10-12 14:37:19 -040054 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040055 fHelper.visitProxies(func);
56 }
57
Brian Osman9a390ac2018-11-12 09:47:48 -050058#ifdef SK_DEBUG
Brian Salomonc2f42542017-07-12 14:11:22 -040059 SkString dumpInfo() const override;
Brian Osman9a390ac2018-11-12 09:47:48 -050060#endif
Brian Salomonc2f42542017-07-12 14:11:22 -040061
62 FixedFunctionFlags fixedFunctionFlags() const override;
63
Brian Osman532b3f92018-07-11 10:02:07 -040064 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override;
Brian Salomonfc527d22016-12-14 21:07:01 -050065
Brian Salomonfc527d22016-12-14 21:07:01 -050066private:
Brian Osmanae0c50c2017-05-25 16:56:34 -040067 enum class ColorArrayType {
68 kPremulGrColor,
69 kSkColor,
70 };
71
Brian Salomon91326c32017-08-09 16:02:19 -040072 void onPrepareDraws(Target*) override;
Brian Salomonfc527d22016-12-14 21:07:01 -050073
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040074 void drawVolatile(Target*);
75 void drawNonVolatile(Target*);
76
77 void fillBuffers(bool hasColorAttribute,
78 bool hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040079 bool hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040080 size_t vertexStride,
81 void* verts,
82 uint16_t* indices) const;
83
84 void drawVertices(Target*,
Brian Salomon7eae3e02018-08-07 14:02:38 +000085 sk_sp<const GrGeometryProcessor>,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040086 const GrBuffer* vertexBuffer,
87 int firstVertex,
88 const GrBuffer* indexBuffer,
89 int firstIndex);
90
Ruiqi Maob609e6d2018-07-17 10:19:38 -040091 sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps,
92 bool* hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040093 bool* hasLocalCoordAttribute,
94 bool* hasBoneAttribute) const;
Brian Salomon199fb872017-02-06 09:41:10 -050095
Brian Salomonfc527d22016-12-14 21:07:01 -050096 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
Brian Salomon53e4c3c2016-12-21 11:38:53 -050097 bool combinablePrimitive() const {
Chris Dalton3809bab2017-06-13 10:55:06 -060098 return GrPrimitiveType::kTriangles == fPrimitiveType ||
99 GrPrimitiveType::kLines == fPrimitiveType ||
100 GrPrimitiveType::kPoints == fPrimitiveType;
Brian Salomonfc527d22016-12-14 21:07:01 -0500101 }
102
Brian Salomon7eae3e02018-08-07 14:02:38 +0000103 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
Brian Salomonfc527d22016-12-14 21:07:01 -0500104
105 struct Mesh {
Brian Osmancf860852018-10-31 14:04:39 -0400106 SkPMColor4f fColor; // Used if this->hasPerVertexColors() is false.
Brian Salomon199fb872017-02-06 09:41:10 -0500107 sk_sp<SkVertices> fVertices;
Brian Salomon3f363692017-02-02 21:05:19 -0500108 SkMatrix fViewMatrix;
Brian Osman8a030552017-05-23 15:03:18 -0400109 bool fIgnoreTexCoords;
110 bool fIgnoreColors;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400111 bool fIgnoreBones;
Brian Salomon199fb872017-02-06 09:41:10 -0500112
113 bool hasExplicitLocalCoords() const {
Brian Osman8a030552017-05-23 15:03:18 -0400114 return fVertices->hasTexCoords() && !fIgnoreTexCoords;
Brian Salomon199fb872017-02-06 09:41:10 -0500115 }
116
117 bool hasPerVertexColors() const {
Brian Osman8a030552017-05-23 15:03:18 -0400118 return fVertices->hasColors() && !fIgnoreColors;
Brian Salomon199fb872017-02-06 09:41:10 -0500119 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400120
121 bool hasBones() const {
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400122 return fVertices->hasBones() && !fIgnoreBones;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400123 }
Brian Salomonfc527d22016-12-14 21:07:01 -0500124 };
125
Brian Salomon199fb872017-02-06 09:41:10 -0500126 bool isIndexed() const {
127 // Consistency enforced in onCombineIfPossible.
Mike Reedaa9e3322017-03-16 14:38:48 -0400128 return fMeshes[0].fVertices->hasIndices();
Brian Salomon199fb872017-02-06 09:41:10 -0500129 }
130
131 bool requiresPerVertexColors() const {
132 return SkToBool(kRequiresPerVertexColors_Flag & fFlags);
133 }
134
135 bool anyMeshHasExplicitLocalCoords() const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400136 return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags);
Brian Salomon199fb872017-02-06 09:41:10 -0500137 }
138
Brian Salomon199fb872017-02-06 09:41:10 -0500139 bool hasMultipleViewMatrices() const {
140 return SkToBool(kHasMultipleViewMatrices_Flag & fFlags);
141 }
142
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400143 bool hasBones() const {
144 return SkToBool(kHasBones_Flag & fFlags);
145 }
146
Ruiqi Maob6307342018-07-03 11:38:15 -0400147 enum Flags {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400148 kRequiresPerVertexColors_Flag = 0x1,
149 kAnyMeshHasExplicitLocalCoords_Flag = 0x2,
150 kHasMultipleViewMatrices_Flag = 0x4,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400151 kHasBones_Flag = 0x8,
Brian Salomon199fb872017-02-06 09:41:10 -0500152 };
153
Brian Salomonc2f42542017-07-12 14:11:22 -0400154 Helper fHelper;
155 SkSTArray<1, Mesh, true> fMeshes;
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400156 std::vector<SkVertices::Bone> fBones; // Bone transformation matrices.
Brian Salomon199fb872017-02-06 09:41:10 -0500157 // 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 Salomonfc527d22016-12-14 21:07:01 -0500159 GrPrimitiveType fPrimitiveType;
Brian Salomon199fb872017-02-06 09:41:10 -0500160 uint32_t fFlags;
Brian Salomonfc527d22016-12-14 21:07:01 -0500161 int fVertexCount;
162 int fIndexCount;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400163 ColorArrayType fColorArrayType;
Brian Osmanfa6d8652017-05-31 09:37:27 -0400164 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomonfc527d22016-12-14 21:07:01 -0500165
Brian Salomonc2f42542017-07-12 14:11:22 -0400166 typedef GrMeshDrawOp INHERITED;
Brian Salomonfc527d22016-12-14 21:07:01 -0500167};
168
169#endif