blob: 1d788ab15cbe2c772922add92bfecc5e8cb19f45 [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 Salomonfc527d22016-12-14 21:07:01 -050014#include "GrTypes.h"
15#include "SkMatrix.h"
16#include "SkRect.h"
17#include "SkTDArray.h"
Brian Salomon199fb872017-02-06 09:41:10 -050018#include "SkVertices.h"
Brian Salomonfc527d22016-12-14 21:07:01 -050019
Brian Osmanae0c50c2017-05-25 16:56:34 -040020#if GR_TEST_UTILS
21#include "GrDrawOpTest.h"
22#endif
23
Brian Salomonfc527d22016-12-14 21:07:01 -050024class GrOpFlushState;
Brian Salomon199fb872017-02-06 09:41:10 -050025class SkVertices;
Brian Salomonfc527d22016-12-14 21:07:01 -050026struct GrInitInvariantOutput;
27
Brian Salomond3ccb0a2017-04-03 10:38:00 -040028class GrDrawVerticesOp final : public GrLegacyMeshDrawOp {
Brian Salomonfc527d22016-12-14 21:07:01 -050029public:
30 DEFINE_OP_CLASS_ID
31
Brian Salomon199fb872017-02-06 09:41:10 -050032 /**
Brian Osmanae0c50c2017-05-25 16:56:34 -040033 * 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 Osmanfa6d8652017-05-31 09:37:27 -040036 * If gammaCorrect is true, the vertex colors will be linearized in the shader to get correct
37 * rendering.
Brian Salomon199fb872017-02-06 09:41:10 -050038 */
Brian Salomond3ccb0a2017-04-03 10:38:00 -040039 static std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, sk_sp<SkVertices>,
Brian Osmanfa6d8652017-05-31 09:37:27 -040040 const SkMatrix& viewMatrix, bool gammaCorrect,
41 sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Osmanae0c50c2017-05-25 16:56:34 -040042 GrPrimitiveType* overridePrimType = nullptr);
Brian Salomonfc527d22016-12-14 21:07:01 -050043
44 const char* name() const override { return "DrawVerticesOp"; }
45
46 SkString dumpInfo() const override {
47 SkString string;
Brian Salomon199fb872017-02-06 09:41:10 -050048 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", fPrimitiveType,
49 fMeshes.count(), fVertexCount, fIndexCount);
Brian Salomonfc527d22016-12-14 21:07:01 -050050 string.append(DumpPipelineInfo(*this->pipeline()));
51 string.append(INHERITED::dumpInfo());
52 return string;
53 }
54
Brian Salomonfc527d22016-12-14 21:07:01 -050055private:
Brian Osmanae0c50c2017-05-25 16:56:34 -040056 enum class ColorArrayType {
57 kPremulGrColor,
58 kSkColor,
59 };
60
Brian Osmanfa6d8652017-05-31 09:37:27 -040061 GrDrawVerticesOp(sk_sp<SkVertices>, GrPrimitiveType, GrColor, bool gammaCorrect,
62 sk_sp<GrColorSpaceXform>, const SkMatrix& viewMatrix);
Brian Salomonfc527d22016-12-14 21:07:01 -050063
Brian Salomona811b122017-03-30 08:21:32 -040064 void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
65 GrProcessorAnalysisCoverage* coverage) const override;
Brian Salomone7d30482017-03-29 12:09:15 -040066 void applyPipelineOptimizations(const PipelineOptimizations&) override;
Brian Salomonfc527d22016-12-14 21:07:01 -050067 void onPrepareDraws(Target*) const override;
Brian Salomonfc527d22016-12-14 21:07:01 -050068
Brian Salomon199fb872017-02-06 09:41:10 -050069 sk_sp<GrGeometryProcessor> makeGP(bool* hasColorAttribute, bool* hasLocalCoordAttribute) const;
70
Brian Salomonfc527d22016-12-14 21:07:01 -050071 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
Brian Salomon53e4c3c2016-12-21 11:38:53 -050072 bool combinablePrimitive() const {
Brian Salomonfc527d22016-12-14 21:07:01 -050073 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 Salomon199fb872017-02-06 09:41:10 -050081 GrColor fColor; // Used if this->hasPerVertexColors() is false.
82 sk_sp<SkVertices> fVertices;
Brian Salomon3f363692017-02-02 21:05:19 -050083 SkMatrix fViewMatrix;
Brian Osman8a030552017-05-23 15:03:18 -040084 bool fIgnoreTexCoords;
85 bool fIgnoreColors;
Brian Salomon199fb872017-02-06 09:41:10 -050086
87 bool hasExplicitLocalCoords() const {
Brian Osman8a030552017-05-23 15:03:18 -040088 return fVertices->hasTexCoords() && !fIgnoreTexCoords;
Brian Salomon199fb872017-02-06 09:41:10 -050089 }
90
91 bool hasPerVertexColors() const {
Brian Osman8a030552017-05-23 15:03:18 -040092 return fVertices->hasColors() && !fIgnoreColors;
Brian Salomon199fb872017-02-06 09:41:10 -050093 }
Brian Salomonfc527d22016-12-14 21:07:01 -050094 };
95
Brian Salomon199fb872017-02-06 09:41:10 -050096 bool isIndexed() const {
97 // Consistency enforced in onCombineIfPossible.
Mike Reedaa9e3322017-03-16 14:38:48 -040098 return fMeshes[0].fVertices->hasIndices();
Brian Salomon199fb872017-02-06 09:41:10 -050099 }
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 Salomonfc527d22016-12-14 21:07:01 -0500127 GrPrimitiveType fPrimitiveType;
Brian Salomon199fb872017-02-06 09:41:10 -0500128 uint32_t fFlags;
Brian Salomonfc527d22016-12-14 21:07:01 -0500129 int fVertexCount;
130 int fIndexCount;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400131 ColorArrayType fColorArrayType;
Brian Osmanfa6d8652017-05-31 09:37:27 -0400132 bool fLinearizeColors;
133 sk_sp<GrColorSpaceXform> fColorSpaceXform;
Brian Salomonfc527d22016-12-14 21:07:01 -0500134 SkSTArray<1, Mesh, true> fMeshes;
135
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400136 typedef GrLegacyMeshDrawOp INHERITED;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400137
138#if GR_TEST_UTILS
139 GR_LEGACY_MESH_DRAW_OP_TEST_FRIEND(VerticesOp);
140#endif
Brian Salomonfc527d22016-12-14 21:07:01 -0500141};
142
143#endif