blob: d27192f461694dbe9a21b3e7b54c849e28af5637 [file] [log] [blame]
joshualitt4973d9d2014-11-08 09:24:25 -08001/*
2 * Copyright 2014 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrDefaultGeoProcFactory.h"
joshualitt4973d9d2014-11-08 09:24:25 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkRefCnt.h"
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050011#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
15#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/glsl/GrGLSLVarying.h"
17#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
joshualitt4973d9d2014-11-08 09:24:25 -080018
19/*
20 * The default Geometry Processor simply takes position and multiplies it by the uniform view
21 * matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or
22 * local coords.
23 */
joshualittb2aa7cb2015-08-05 11:05:22 -070024
25enum GPFlag {
Chris Dalton97f85902021-07-14 03:16:22 -060026 kColorAttribute_GPFlag = 0x1,
27 kColorAttributeIsWide_GPFlag = 0x2,
28 kLocalCoordAttribute_GPFlag = 0x4,
29 kCoverageAttribute_GPFlag = 0x8,
30 kCoverageAttributeTweak_GPFlag = 0x10,
31 kCoverageAttributeUnclamped_GPFlag = 0x20,
joshualittb2aa7cb2015-08-05 11:05:22 -070032};
33
joshualitt4973d9d2014-11-08 09:24:25 -080034class DefaultGeoProc : public GrGeometryProcessor {
35public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050036 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050037 uint32_t gpTypeFlags,
38 const SkPMColor4f& color,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050039 const SkMatrix& viewMatrix,
40 const SkMatrix& localMatrix,
41 bool localCoordsWillBeRead,
42 uint8_t coverage) {
Mike Kleinf1241082020-12-14 15:59:09 -060043 return arena->make([&](void* ptr) {
44 return new (ptr) DefaultGeoProc(gpTypeFlags, color, viewMatrix, localMatrix, coverage,
45 localCoordsWillBeRead);
46 });
joshualitt4973d9d2014-11-08 09:24:25 -080047 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 const char* name() const override { return "DefaultGeometryProcessor"; }
joshualitt4973d9d2014-11-08 09:24:25 -080050
Brian Osmancf860852018-10-31 14:04:39 -040051 const SkPMColor4f& color() const { return fColor; }
Brian Salomon92be2f72018-06-19 14:33:47 -040052 bool hasVertexColor() const { return fInColor.isInitialized(); }
joshualitte578a952015-05-14 10:09:13 -070053 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -070054 const SkMatrix& localMatrix() const { return fLocalMatrix; }
bsalomon7765a472015-07-08 11:26:37 -070055 bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; }
joshualitt9b989322014-12-15 14:16:27 -080056 uint8_t coverage() const { return fCoverage; }
Brian Salomon92be2f72018-06-19 14:33:47 -040057 bool hasVertexCoverage() const { return fInCoverage.isInitialized(); }
joshualitt9b989322014-12-15 14:16:27 -080058
egdaniel57d3b032015-11-13 11:57:27 -080059 class GLSLProcessor : public GrGLSLGeometryProcessor {
joshualitt4973d9d2014-11-08 09:24:25 -080060 public:
egdaniel57d3b032015-11-13 11:57:27 -080061 GLSLProcessor()
Robert Phillipscd11e272021-01-06 11:31:38 -050062 : fViewMatrixPrev(SkMatrix::InvalidMatrix())
63 , fLocalMatrixPrev(SkMatrix::InvalidMatrix())
Brian Osmancf860852018-10-31 14:04:39 -040064 , fColor(SK_PMColor4fILLEGAL)
65 , fCoverage(0xff) {}
joshualitt4973d9d2014-11-08 09:24:25 -080066
mtklein36352bf2015-03-25 18:17:31 -070067 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -040068 const DefaultGeoProc& gp = args.fGeomProc.cast<DefaultGeoProc>();
egdaniel4ca2e602015-11-18 08:01:26 -080069 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
Chris Dalton60283612018-02-14 13:38:14 -070070 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel0eafe792015-11-20 14:01:22 -080071 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -080072 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt4973d9d2014-11-08 09:24:25 -080073
joshualittabb52a12015-01-13 15:02:10 -080074 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -080075 varyingHandler->emitAttributes(gp);
joshualitt2dd1ae02014-12-03 06:24:10 -080076
Brian Osman80879d42019-01-07 16:15:27 -050077 bool tweakAlpha = SkToBool(gp.fFlags & kCoverageAttributeTweak_GPFlag);
Chris Dalton97f85902021-07-14 03:16:22 -060078 bool coverageNeedsSaturate = SkToBool(gp.fFlags & kCoverageAttributeUnclamped_GPFlag);
Brian Osman80879d42019-01-07 16:15:27 -050079 SkASSERT(!tweakAlpha || gp.hasVertexCoverage());
Chris Dalton97f85902021-07-14 03:16:22 -060080 SkASSERT(!tweakAlpha || !coverageNeedsSaturate);
Brian Osman80879d42019-01-07 16:15:27 -050081
joshualitt2dd1ae02014-12-03 06:24:10 -080082 // Setup pass through color
John Stiles4d7ac492021-03-09 20:16:43 -050083 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Brian Osman80879d42019-01-07 16:15:27 -050084 if (gp.hasVertexColor() || tweakAlpha) {
Chris Dalton27372882017-12-08 13:34:21 -070085 GrGLSLVarying varying(kHalf4_GrSLType);
Brian Salomon3de0aee2017-01-29 09:34:17 -050086 varyingHandler->addVarying("color", &varying);
Brian Osmanfa6d8652017-05-31 09:37:27 -040087
Brian Osman8370fd12020-03-18 14:25:45 -040088 // Start with the attribute or with uniform color
Brian Osman80879d42019-01-07 16:15:27 -050089 if (gp.hasVertexColor()) {
90 vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name());
91 } else {
92 const char* colorUniformName;
Ethan Nicholas16464c32020-04-06 13:53:05 -040093 fColorUniform = uniformHandler->addUniform(nullptr,
94 kVertex_GrShaderFlag,
Brian Osman80879d42019-01-07 16:15:27 -050095 kHalf4_GrSLType,
96 "Color",
97 &colorUniformName);
98 vertBuilder->codeAppendf("half4 color = %s;", colorUniformName);
99 }
Brian Osmanfa6d8652017-05-31 09:37:27 -0400100
Brian Osman80879d42019-01-07 16:15:27 -0500101 // Optionally fold coverage into alpha (color).
102 if (tweakAlpha) {
103 vertBuilder->codeAppendf("color = color * %s;", gp.fInCoverage.name());
104 }
Brian Osmanfa6d8652017-05-31 09:37:27 -0400105 vertBuilder->codeAppendf("%s = color;\n", varying.vsOut());
Brian Salomon3de0aee2017-01-29 09:34:17 -0500106 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn());
Brian Salomonbfd51832017-01-04 13:22:08 -0500107 } else {
108 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
109 &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -0700110 }
111
joshualittabb52a12015-01-13 15:02:10 -0800112 // Setup position
Brian Salomon5a328282021-04-14 10:32:25 -0400113 WriteOutputPosition(vertBuilder,
114 uniformHandler,
115 *args.fShaderCaps,
116 gpArgs,
117 gp.fInPosition.name(),
118 gp.viewMatrix(),
119 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -0800120
Brian Osman8370fd12020-03-18 14:25:45 -0400121 // emit transforms using either explicit local coords or positions
Michael Ludwig553db622020-06-19 10:47:30 -0400122 if (gp.fInLocalCoords.isInitialized()) {
123 SkASSERT(gp.localMatrix().isIdentity());
124 gpArgs->fLocalCoordVar = gp.fInLocalCoords.asShaderVar();
125 } else if (gp.fLocalCoordsWillBeRead) {
Brian Salomon5a328282021-04-14 10:32:25 -0400126 WriteLocalCoord(vertBuilder,
127 uniformHandler,
128 *args.fShaderCaps,
129 gpArgs,
130 gp.fInPosition.asShaderVar(),
131 gp.localMatrix(),
132 &fLocalMatrixUniform);
Michael Ludwig553db622020-06-19 10:47:30 -0400133 }
joshualittabb52a12015-01-13 15:02:10 -0800134
joshualitt2dd1ae02014-12-03 06:24:10 -0800135 // Setup coverage as pass through
Brian Osman80879d42019-01-07 16:15:27 -0500136 if (gp.hasVertexCoverage() && !tweakAlpha) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400137 fragBuilder->codeAppendf("half alpha = 1.0;");
Brian Salomon92be2f72018-06-19 14:33:47 -0400138 varyingHandler->addPassThroughAttribute(gp.fInCoverage, "alpha");
Chris Dalton97f85902021-07-14 03:16:22 -0600139 if (coverageNeedsSaturate) {
140 fragBuilder->codeAppendf("half4 %s = half4(saturate(alpha));",
141 args.fOutputCoverage);
142 } else {
143 fragBuilder->codeAppendf("half4 %s = half4(alpha);", args.fOutputCoverage);
144 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500145 } else if (gp.coverage() == 0xff) {
John Stiles4d7ac492021-03-09 20:16:43 -0500146 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
Brian Salomon8c852be2017-01-04 10:44:42 -0500147 } else {
148 const char* fragCoverage;
Ethan Nicholas16464c32020-04-06 13:53:05 -0400149 fCoverageUniform = uniformHandler->addUniform(nullptr,
150 kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 kHalf_GrSLType,
Brian Salomon8c852be2017-01-04 10:44:42 -0500152 "Coverage",
153 &fragCoverage);
John Stiles4d7ac492021-03-09 20:16:43 -0500154 fragBuilder->codeAppendf("half4 %s = half4(%s);",
155 args.fOutputCoverage, fragCoverage);
joshualitt2dd1ae02014-12-03 06:24:10 -0800156 }
joshualitt4973d9d2014-11-08 09:24:25 -0800157 }
158
joshualitt87f48d92014-12-04 10:41:40 -0800159 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon5a328282021-04-14 10:32:25 -0400160 const GrShaderCaps& shaderCaps,
joshualitt87f48d92014-12-04 10:41:40 -0800161 GrProcessorKeyBuilder* b) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800162 const DefaultGeoProc& def = gp.cast<DefaultGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800163 uint32_t key = def.fFlags;
Brian Osman80879d42019-01-07 16:15:27 -0500164 key |= (def.coverage() == 0xff) ? 0x80 : 0;
Michael Ludwig553db622020-06-19 10:47:30 -0400165 key |= def.localCoordsWillBeRead() ? 0x100 : 0;
166
167 bool usesLocalMatrix = def.localCoordsWillBeRead() &&
168 !def.fInLocalCoords.isInitialized();
Brian Salomon5a328282021-04-14 10:32:25 -0400169 key = AddMatrixKeys(shaderCaps,
170 key,
171 def.viewMatrix(),
Michael Ludwig553db622020-06-19 10:47:30 -0400172 usesLocalMatrix ? def.localMatrix() : SkMatrix::I());
joshualitt8fc6c2d2014-12-22 15:27:05 -0800173 b->add32(key);
joshualitt2dd1ae02014-12-03 06:24:10 -0800174 }
joshualitt4973d9d2014-11-08 09:24:25 -0800175
egdaniel018fb622015-10-28 07:26:40 -0700176 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400177 const GrShaderCaps& shaderCaps,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400178 const GrGeometryProcessor& geomProc) override {
179 const DefaultGeoProc& dgp = geomProc.cast<DefaultGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700180
Brian Salomon5a328282021-04-14 10:32:25 -0400181 SetTransform(pdman, shaderCaps, fViewMatrixUniform, dgp.viewMatrix(), &fViewMatrixPrev);
182 SetTransform(pdman,
183 shaderCaps,
184 fLocalMatrixUniform,
185 dgp.localMatrix(),
186 &fLocalMatrixPrev);
joshualittee2af952014-12-30 09:04:15 -0800187
Brian Osman1be2b7c2018-10-29 16:07:15 -0400188 if (!dgp.hasVertexColor() && dgp.color() != fColor) {
Brian Osmancf860852018-10-31 14:04:39 -0400189 pdman.set4fv(fColorUniform, 1, dgp.color().vec());
joshualittb8c241a2015-05-19 08:23:30 -0700190 fColor = dgp.color();
joshualitt9b989322014-12-15 14:16:27 -0800191 }
joshualittb8c241a2015-05-19 08:23:30 -0700192
Brian Salomon8c852be2017-01-04 10:44:42 -0500193 if (dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) {
joshualittb8c241a2015-05-19 08:23:30 -0700194 pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverage()));
195 fCoverage = dgp.coverage();
joshualitt9b989322014-12-15 14:16:27 -0800196 }
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400197 }
198
199 private:
Robert Phillipscd11e272021-01-06 11:31:38 -0500200 SkMatrix fViewMatrixPrev;
201 SkMatrix fLocalMatrixPrev;
Brian Osmancf860852018-10-31 14:04:39 -0400202 SkPMColor4f fColor;
joshualitt9b989322014-12-15 14:16:27 -0800203 uint8_t fCoverage;
joshualitt5559ca22015-05-21 15:50:36 -0700204 UniformHandle fViewMatrixUniform;
Michael Ludwig553db622020-06-19 10:47:30 -0400205 UniformHandle fLocalMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800206 UniformHandle fColorUniform;
207 UniformHandle fCoverageUniform;
208
John Stiles7571f9e2020-09-02 22:42:33 -0400209 using INHERITED = GrGLSLGeometryProcessor;
joshualitt4973d9d2014-11-08 09:24:25 -0800210 };
211
Brian Salomon94efbf52016-11-29 13:43:05 -0500212 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800213 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800214 }
215
Robert Phillipsf10535f2021-03-23 09:30:45 -0400216 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800217 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800218 }
219
joshualitt4973d9d2014-11-08 09:24:25 -0800220private:
Brian Osmanf0aee742020-03-12 09:28:44 -0400221 DefaultGeoProc(uint32_t gpTypeFlags,
Brian Osmancf860852018-10-31 14:04:39 -0400222 const SkPMColor4f& color,
joshualitt8059eb92014-12-29 15:10:07 -0800223 const SkMatrix& viewMatrix,
224 const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -0700225 uint8_t coverage,
Brian Osman37064c12019-02-08 10:53:07 -0500226 bool localCoordsWillBeRead)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400227 : INHERITED(kDefaultGeoProc_ClassID)
228 , fColor(color)
Brian Salomon8c852be2017-01-04 10:44:42 -0500229 , fViewMatrix(viewMatrix)
230 , fLocalMatrix(localMatrix)
231 , fCoverage(coverage)
232 , fFlags(gpTypeFlags)
Brian Osman8370fd12020-03-18 14:25:45 -0400233 , fLocalCoordsWillBeRead(localCoordsWillBeRead) {
Brian Osmand4c29702018-09-14 16:16:55 -0400234 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Salomon3de0aee2017-01-29 09:34:17 -0500235 if (fFlags & kColorAttribute_GPFlag) {
Brian Osman2a4c4df2018-12-20 14:06:54 -0500236 fInColor = MakeColorAttribute("inColor",
237 SkToBool(fFlags & kColorAttributeIsWide_GPFlag));
joshualitt2dd1ae02014-12-03 06:24:10 -0800238 }
Brian Salomon3de0aee2017-01-29 09:34:17 -0500239 if (fFlags & kLocalCoordAttribute_GPFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400240 fInLocalCoords = {"inLocalCoord", kFloat2_GrVertexAttribType,
241 kFloat2_GrSLType};
joshualitt2dd1ae02014-12-03 06:24:10 -0800242 }
Brian Salomon3de0aee2017-01-29 09:34:17 -0500243 if (fFlags & kCoverageAttribute_GPFlag) {
Brian Osmand4c29702018-09-14 16:16:55 -0400244 fInCoverage = {"inCoverage", kFloat_GrVertexAttribType, kHalf_GrSLType};
joshualitt2dd1ae02014-12-03 06:24:10 -0800245 }
Brian Osman37064c12019-02-08 10:53:07 -0500246 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400247 }
248
249 Attribute fInPosition;
250 Attribute fInColor;
251 Attribute fInLocalCoords;
252 Attribute fInCoverage;
Brian Osmancf860852018-10-31 14:04:39 -0400253 SkPMColor4f fColor;
joshualitte578a952015-05-14 10:09:13 -0700254 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700255 SkMatrix fLocalMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800256 uint8_t fCoverage;
joshualitt2dd1ae02014-12-03 06:24:10 -0800257 uint32_t fFlags;
bsalomon7765a472015-07-08 11:26:37 -0700258 bool fLocalCoordsWillBeRead;
joshualitt4973d9d2014-11-08 09:24:25 -0800259
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400260 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
joshualitt5478d422014-11-14 16:00:38 -0800261
John Stiles7571f9e2020-09-02 22:42:33 -0400262 using INHERITED = GrGeometryProcessor;
joshualitt4973d9d2014-11-08 09:24:25 -0800263};
264
265GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc);
266
Hal Canary6f6961e2017-01-31 13:50:44 -0500267#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500268GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800269 uint32_t flags = 0;
joshualitt0067ff52015-07-08 14:26:19 -0700270 if (d->fRandom->nextBool()) {
Brian Salomon3de0aee2017-01-29 09:34:17 -0500271 flags |= kColorAttribute_GPFlag;
joshualitt4973d9d2014-11-08 09:24:25 -0800272 }
joshualitt0067ff52015-07-08 14:26:19 -0700273 if (d->fRandom->nextBool()) {
Brian Osman2a4c4df2018-12-20 14:06:54 -0500274 flags |= kColorAttributeIsWide_GPFlag;
275 }
276 if (d->fRandom->nextBool()) {
Brian Salomon3de0aee2017-01-29 09:34:17 -0500277 flags |= kCoverageAttribute_GPFlag;
Brian Osman80879d42019-01-07 16:15:27 -0500278 if (d->fRandom->nextBool()) {
Chris Dalton97f85902021-07-14 03:16:22 -0600279 flags |= (d->fRandom->nextBool()) ? kCoverageAttributeTweak_GPFlag
280 : kCoverageAttributeUnclamped_GPFlag;
Brian Osman80879d42019-01-07 16:15:27 -0500281 }
Brian Salomon3de0aee2017-01-29 09:34:17 -0500282 }
283 if (d->fRandom->nextBool()) {
284 flags |= kLocalCoordAttribute_GPFlag;
joshualittb2aa7cb2015-08-05 11:05:22 -0700285 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800286
John Stiles391a9f62021-04-29 23:17:41 -0400287 GrColor color = GrRandomColor(d->fRandom);
288 SkMatrix viewMtx = GrTest::TestMatrix(d->fRandom);
289 SkMatrix localMtx = GrTest::TestMatrix(d->fRandom);
290 bool readsLocalCoords = d->fRandom->nextBool();
291 uint8_t coverage = GrRandomCoverage(d->fRandom);
Brian Osmanf0aee742020-03-12 09:28:44 -0400292 return DefaultGeoProc::Make(d->allocator(),
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400293 flags,
John Stiles391a9f62021-04-29 23:17:41 -0400294 SkPMColor4f::FromBytes_RGBA(color),
295 viewMtx,
296 localMtx,
297 readsLocalCoords,
298 coverage);
joshualitt4973d9d2014-11-08 09:24:25 -0800299}
Hal Canary6f6961e2017-01-31 13:50:44 -0500300#endif
joshualitt4973d9d2014-11-08 09:24:25 -0800301
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500302GrGeometryProcessor* GrDefaultGeoProcFactory::Make(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500303 const Color& color,
304 const Coverage& coverage,
305 const LocalCoords& localCoords,
306 const SkMatrix& viewMatrix) {
joshualitte9d60952015-07-27 12:13:14 -0700307 uint32_t flags = 0;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500308 if (Color::kPremulGrColorAttribute_Type == color.fType) {
309 flags |= kColorAttribute_GPFlag;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500310 } else if (Color::kPremulWideColorAttribute_Type == color.fType) {
311 flags |= kColorAttribute_GPFlag | kColorAttributeIsWide_GPFlag;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500312 }
Brian Osman80879d42019-01-07 16:15:27 -0500313 if (Coverage::kAttribute_Type == coverage.fType) {
314 flags |= kCoverageAttribute_GPFlag;
315 } else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) {
316 flags |= kCoverageAttribute_GPFlag | kCoverageAttributeTweak_GPFlag;
Chris Dalton97f85902021-07-14 03:16:22 -0600317 } else if (Coverage::kAttributeUnclamped_Type == coverage.fType) {
318 flags |= kCoverageAttribute_GPFlag | kCoverageAttributeUnclamped_GPFlag;
Brian Osman80879d42019-01-07 16:15:27 -0500319 }
Brian Salomon3de0aee2017-01-29 09:34:17 -0500320 flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0;
joshualitte9d60952015-07-27 12:13:14 -0700321
322 uint8_t inCoverage = coverage.fCoverage;
joshualitt0d986d82015-07-28 10:01:18 -0700323 bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type;
joshualitte9d60952015-07-27 12:13:14 -0700324
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500325 return DefaultGeoProc::Make(arena,
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400326 flags,
Brian Osmancf860852018-10-31 14:04:39 -0400327 color.fColor,
bungeman06ca8ec2016-06-09 08:01:03 -0700328 viewMatrix,
329 localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(),
330 localCoordsWillBeRead,
Brian Osman37064c12019-02-08 10:53:07 -0500331 inCoverage);
joshualitte9d60952015-07-27 12:13:14 -0700332}
joshualitt0d986d82015-07-28 10:01:18 -0700333
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500334GrGeometryProcessor* GrDefaultGeoProcFactory::MakeForDeviceSpace(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500335 const Color& color,
336 const Coverage& coverage,
337 const LocalCoords& localCoords,
338 const SkMatrix& viewMatrix) {
joshualitt0d986d82015-07-28 10:01:18 -0700339 SkMatrix invert = SkMatrix::I();
joshualittdf0c5572015-08-03 11:35:28 -0700340 if (LocalCoords::kUnused_Type != localCoords.fType) {
341 SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType);
342 if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700343 return nullptr;
joshualittdf0c5572015-08-03 11:35:28 -0700344 }
joshualitt0d986d82015-07-28 10:01:18 -0700345
joshualittdf0c5572015-08-03 11:35:28 -0700346 if (localCoords.hasLocalMatrix()) {
Michael Ludwigef776042018-11-01 11:07:51 -0400347 invert.postConcat(*localCoords.fMatrix);
joshualittdf0c5572015-08-03 11:35:28 -0700348 }
joshualitt0d986d82015-07-28 10:01:18 -0700349 }
350
351 LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert);
Brian Osmanf0aee742020-03-12 09:28:44 -0400352 return Make(arena, color, coverage, inverted, SkMatrix::I());
joshualitt0d986d82015-07-28 10:01:18 -0700353}