blob: 7695dd9f87181946480616339757a9bd0b936d35 [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 Mao4ec72f72018-07-10 17:21:07 -040041 const SkMatrix bones[],
42 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
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040048 GrDrawVerticesOp(const Helper::MakeArgs&, GrColor, sk_sp<SkVertices>, const SkMatrix bones[],
49 int boneCount, GrPrimitiveType, GrAAType, sk_sp<GrColorSpaceXform>,
50 const SkMatrix& viewMatrix);
Brian Salomonfc527d22016-12-14 21:07:01 -050051
52 const char* name() const override { return "DrawVerticesOp"; }
53
Robert Phillipsf1748f52017-09-14 14:11:24 -040054 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040055 fHelper.visitProxies(func);
56 }
57
Brian Salomonc2f42542017-07-12 14:11:22 -040058 SkString dumpInfo() const override;
59
60 FixedFunctionFlags fixedFunctionFlags() const override;
61
Brian Osman532b3f92018-07-11 10:02:07 -040062 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override;
Brian Salomonfc527d22016-12-14 21:07:01 -050063
Brian Salomonfc527d22016-12-14 21:07:01 -050064private:
Brian Osmanae0c50c2017-05-25 16:56:34 -040065 enum class ColorArrayType {
66 kPremulGrColor,
67 kSkColor,
68 };
69
Brian Salomon91326c32017-08-09 16:02:19 -040070 void onPrepareDraws(Target*) override;
Brian Salomonfc527d22016-12-14 21:07:01 -050071
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040072 void drawVolatile(Target*);
73 void drawNonVolatile(Target*);
74
75 void fillBuffers(bool hasColorAttribute,
76 bool hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040077 bool hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040078 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 Maob609e6d2018-07-17 10:19:38 -040089 sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps,
90 bool* hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040091 bool* hasLocalCoordAttribute,
92 bool* hasBoneAttribute) const;
Brian Salomon199fb872017-02-06 09:41:10 -050093
Brian Salomonfc527d22016-12-14 21:07:01 -050094 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
Brian Salomon53e4c3c2016-12-21 11:38:53 -050095 bool combinablePrimitive() const {
Chris Dalton3809bab2017-06-13 10:55:06 -060096 return GrPrimitiveType::kTriangles == fPrimitiveType ||
97 GrPrimitiveType::kLines == fPrimitiveType ||
98 GrPrimitiveType::kPoints == fPrimitiveType;
Brian Salomonfc527d22016-12-14 21:07:01 -050099 }
100
101 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
102
103 struct Mesh {
Brian Salomon199fb872017-02-06 09:41:10 -0500104 GrColor fColor; // Used if this->hasPerVertexColors() is false.
105 sk_sp<SkVertices> fVertices;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400106 std::vector<float> fBones; // Transformation matrices stored in GPU format.
Brian Salomon3f363692017-02-02 21:05:19 -0500107 SkMatrix fViewMatrix;
Brian Osman8a030552017-05-23 15:03:18 -0400108 bool fIgnoreTexCoords;
109 bool fIgnoreColors;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400110 bool fIgnoreBones;
Brian Salomon199fb872017-02-06 09:41:10 -0500111
112 bool hasExplicitLocalCoords() const {
Brian Osman8a030552017-05-23 15:03:18 -0400113 return fVertices->hasTexCoords() && !fIgnoreTexCoords;
Brian Salomon199fb872017-02-06 09:41:10 -0500114 }
115
116 bool hasPerVertexColors() const {
Brian Osman8a030552017-05-23 15:03:18 -0400117 return fVertices->hasColors() && !fIgnoreColors;
Brian Salomon199fb872017-02-06 09:41:10 -0500118 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400119
120 bool hasBones() const {
121 return fVertices->hasBones() && fBones.size() && !fIgnoreBones;
122 }
Brian Salomonfc527d22016-12-14 21:07:01 -0500123 };
124
Brian Salomon199fb872017-02-06 09:41:10 -0500125 bool isIndexed() const {
126 // Consistency enforced in onCombineIfPossible.
Mike Reedaa9e3322017-03-16 14:38:48 -0400127 return fMeshes[0].fVertices->hasIndices();
Brian Salomon199fb872017-02-06 09:41:10 -0500128 }
129
130 bool requiresPerVertexColors() const {
131 return SkToBool(kRequiresPerVertexColors_Flag & fFlags);
132 }
133
134 bool anyMeshHasExplicitLocalCoords() const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400135 return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags);
Brian Salomon199fb872017-02-06 09:41:10 -0500136 }
137
Brian Salomon199fb872017-02-06 09:41:10 -0500138 bool hasMultipleViewMatrices() const {
139 return SkToBool(kHasMultipleViewMatrices_Flag & fFlags);
140 }
141
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400142 bool hasBones() const {
143 return SkToBool(kHasBones_Flag & fFlags);
144 }
145
Ruiqi Maob6307342018-07-03 11:38:15 -0400146 enum Flags {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400147 kRequiresPerVertexColors_Flag = 0x1,
148 kAnyMeshHasExplicitLocalCoords_Flag = 0x2,
149 kHasMultipleViewMatrices_Flag = 0x4,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400150 kHasBones_Flag = 0x8,
Brian Salomon199fb872017-02-06 09:41:10 -0500151 };
152
Brian Salomonc2f42542017-07-12 14:11:22 -0400153 Helper fHelper;
154 SkSTArray<1, Mesh, true> fMeshes;
Brian Salomon199fb872017-02-06 09:41:10 -0500155 // 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 Salomonfc527d22016-12-14 21:07:01 -0500157 GrPrimitiveType fPrimitiveType;
Brian Salomon199fb872017-02-06 09:41:10 -0500158 uint32_t fFlags;
Brian Salomonfc527d22016-12-14 21:07:01 -0500159 int fVertexCount;
160 int fIndexCount;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400161 ColorArrayType fColorArrayType;
Brian Osmanfa6d8652017-05-31 09:37:27 -0400162 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomonfc527d22016-12-14 21:07:01 -0500163
Brian Salomonc2f42542017-07-12 14:11:22 -0400164 typedef GrMeshDrawOp INHERITED;
Brian Salomonfc527d22016-12-14 21:07:01 -0500165};
166
167#endif