joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkRefCnt.h" |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 11 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 14 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 15 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 17 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 18 | |
| 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 | */ |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 24 | |
| 25 | enum GPFlag { |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 26 | kColorAttribute_GPFlag = 0x1, |
| 27 | kColorAttributeIsWide_GPFlag = 0x2, |
| 28 | kLocalCoordAttribute_GPFlag = 0x4, |
| 29 | kCoverageAttribute_GPFlag = 0x8, |
| 30 | kCoverageAttributeTweak_GPFlag = 0x10, |
| 31 | kCoverageAttributeUnclamped_GPFlag = 0x20, |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 34 | class DefaultGeoProc : public GrGeometryProcessor { |
| 35 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 36 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 37 | uint32_t gpTypeFlags, |
| 38 | const SkPMColor4f& color, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 39 | const SkMatrix& viewMatrix, |
| 40 | const SkMatrix& localMatrix, |
| 41 | bool localCoordsWillBeRead, |
| 42 | uint8_t coverage) { |
Mike Klein | f124108 | 2020-12-14 15:59:09 -0600 | [diff] [blame] | 43 | return arena->make([&](void* ptr) { |
| 44 | return new (ptr) DefaultGeoProc(gpTypeFlags, color, viewMatrix, localMatrix, coverage, |
| 45 | localCoordsWillBeRead); |
| 46 | }); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 47 | } |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | const char* name() const override { return "DefaultGeometryProcessor"; } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 50 | |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 51 | void addToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 52 | uint32_t key = fFlags; |
| 53 | key |= fCoverage == 0xff ? 0x80 : 0; |
| 54 | key |= fLocalCoordsWillBeRead ? 0x100 : 0; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 55 | |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 56 | bool usesLocalMatrix = fLocalCoordsWillBeRead && !fInLocalCoords.isInitialized(); |
| 57 | key = ProgramImpl::AddMatrixKeys(caps, |
| 58 | key, |
| 59 | fViewMatrix, |
| 60 | usesLocalMatrix ? fLocalMatrix : SkMatrix::I()); |
| 61 | b->add32(key); |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override { |
| 65 | return std::make_unique<Impl>(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | class Impl : public ProgramImpl { |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 70 | public: |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 71 | void setData(const GrGLSLProgramDataManager& pdman, |
| 72 | const GrShaderCaps& shaderCaps, |
| 73 | const GrGeometryProcessor& geomProc) override { |
| 74 | const DefaultGeoProc& dgp = geomProc.cast<DefaultGeoProc>(); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 75 | |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 76 | SetTransform(pdman, shaderCaps, fViewMatrixUniform, dgp.fViewMatrix, &fViewMatrixPrev); |
| 77 | SetTransform(pdman, |
| 78 | shaderCaps, |
| 79 | fLocalMatrixUniform, |
| 80 | dgp.fLocalMatrix, |
| 81 | &fLocalMatrixPrev); |
| 82 | |
| 83 | if (!dgp.hasVertexColor() && dgp.fColor != fColor) { |
| 84 | pdman.set4fv(fColorUniform, 1, dgp.fColor.vec()); |
| 85 | fColor = dgp.fColor; |
| 86 | } |
| 87 | |
| 88 | if (dgp.fCoverage != fCoverage && !dgp.hasVertexCoverage()) { |
| 89 | pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.fCoverage)); |
| 90 | fCoverage = dgp.fCoverage; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 96 | const DefaultGeoProc& gp = args.fGeomProc.cast<DefaultGeoProc>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 97 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 98 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 99 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 100 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 101 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 102 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 103 | varyingHandler->emitAttributes(gp); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 104 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 105 | bool tweakAlpha = SkToBool(gp.fFlags & kCoverageAttributeTweak_GPFlag); |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 106 | bool coverageNeedsSaturate = SkToBool(gp.fFlags & kCoverageAttributeUnclamped_GPFlag); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 107 | SkASSERT(!tweakAlpha || gp.hasVertexCoverage()); |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 108 | SkASSERT(!tweakAlpha || !coverageNeedsSaturate); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 109 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 110 | // Setup pass through color |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 111 | fragBuilder->codeAppendf("half4 %s;", args.fOutputColor); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 112 | if (gp.hasVertexColor() || tweakAlpha) { |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 113 | GrGLSLVarying varying(kHalf4_GrSLType); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 114 | varyingHandler->addVarying("color", &varying); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 115 | |
Brian Osman | 8370fd1 | 2020-03-18 14:25:45 -0400 | [diff] [blame] | 116 | // Start with the attribute or with uniform color |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 117 | if (gp.hasVertexColor()) { |
| 118 | vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name()); |
| 119 | } else { |
| 120 | const char* colorUniformName; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 121 | fColorUniform = uniformHandler->addUniform(nullptr, |
| 122 | kVertex_GrShaderFlag, |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 123 | kHalf4_GrSLType, |
| 124 | "Color", |
| 125 | &colorUniformName); |
| 126 | vertBuilder->codeAppendf("half4 color = %s;", colorUniformName); |
| 127 | } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 128 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 129 | // Optionally fold coverage into alpha (color). |
| 130 | if (tweakAlpha) { |
| 131 | vertBuilder->codeAppendf("color = color * %s;", gp.fInCoverage.name()); |
| 132 | } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 133 | vertBuilder->codeAppendf("%s = color;\n", varying.vsOut()); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 134 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn()); |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 135 | } else { |
| 136 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, |
| 137 | &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 138 | } |
| 139 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 140 | // Setup position |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 141 | WriteOutputPosition(vertBuilder, |
| 142 | uniformHandler, |
| 143 | *args.fShaderCaps, |
| 144 | gpArgs, |
| 145 | gp.fInPosition.name(), |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 146 | gp.fViewMatrix, |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 147 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 148 | |
Brian Osman | 8370fd1 | 2020-03-18 14:25:45 -0400 | [diff] [blame] | 149 | // emit transforms using either explicit local coords or positions |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 150 | if (gp.fInLocalCoords.isInitialized()) { |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 151 | SkASSERT(gp.fLocalMatrix.isIdentity()); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 152 | gpArgs->fLocalCoordVar = gp.fInLocalCoords.asShaderVar(); |
| 153 | } else if (gp.fLocalCoordsWillBeRead) { |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 154 | WriteLocalCoord(vertBuilder, |
| 155 | uniformHandler, |
| 156 | *args.fShaderCaps, |
| 157 | gpArgs, |
| 158 | gp.fInPosition.asShaderVar(), |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 159 | gp.fLocalMatrix, |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 160 | &fLocalMatrixUniform); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 161 | } |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 162 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 163 | // Setup coverage as pass through |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 164 | if (gp.hasVertexCoverage() && !tweakAlpha) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 165 | fragBuilder->codeAppendf("half alpha = 1.0;"); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 166 | varyingHandler->addPassThroughAttribute(gp.fInCoverage, "alpha"); |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 167 | if (coverageNeedsSaturate) { |
| 168 | fragBuilder->codeAppendf("half4 %s = half4(saturate(alpha));", |
| 169 | args.fOutputCoverage); |
| 170 | } else { |
| 171 | fragBuilder->codeAppendf("half4 %s = half4(alpha);", args.fOutputCoverage); |
| 172 | } |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 173 | } else if (gp.fCoverage == 0xff) { |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 174 | fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 175 | } else { |
| 176 | const char* fragCoverage; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 177 | fCoverageUniform = uniformHandler->addUniform(nullptr, |
| 178 | kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 179 | kHalf_GrSLType, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 180 | "Coverage", |
| 181 | &fragCoverage); |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 182 | fragBuilder->codeAppendf("half4 %s = half4(%s);", |
| 183 | args.fOutputCoverage, fragCoverage); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 184 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 187 | SkMatrix fViewMatrixPrev = SkMatrix::InvalidMatrix(); |
| 188 | SkMatrix fLocalMatrixPrev = SkMatrix::InvalidMatrix(); |
| 189 | SkPMColor4f fColor = SK_PMColor4fILLEGAL; |
| 190 | uint8_t fCoverage = 0xFF; |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 191 | |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 192 | UniformHandle fViewMatrixUniform; |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 193 | UniformHandle fLocalMatrixUniform; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 194 | UniformHandle fColorUniform; |
| 195 | UniformHandle fCoverageUniform; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 198 | bool hasVertexColor() const { return fInColor.isInitialized(); } |
| 199 | bool hasVertexCoverage() const { return fInCoverage.isInitialized(); } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 200 | |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 201 | DefaultGeoProc(uint32_t gpTypeFlags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 202 | const SkPMColor4f& color, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 203 | const SkMatrix& viewMatrix, |
| 204 | const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 205 | uint8_t coverage, |
Brian Osman | 37064c1 | 2019-02-08 10:53:07 -0500 | [diff] [blame] | 206 | bool localCoordsWillBeRead) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 207 | : INHERITED(kDefaultGeoProc_ClassID) |
| 208 | , fColor(color) |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 209 | , fViewMatrix(viewMatrix) |
| 210 | , fLocalMatrix(localMatrix) |
| 211 | , fCoverage(coverage) |
| 212 | , fFlags(gpTypeFlags) |
Brian Osman | 8370fd1 | 2020-03-18 14:25:45 -0400 | [diff] [blame] | 213 | , fLocalCoordsWillBeRead(localCoordsWillBeRead) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 214 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 215 | if (fFlags & kColorAttribute_GPFlag) { |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 216 | fInColor = MakeColorAttribute("inColor", |
| 217 | SkToBool(fFlags & kColorAttributeIsWide_GPFlag)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 218 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 219 | if (fFlags & kLocalCoordAttribute_GPFlag) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 220 | fInLocalCoords = {"inLocalCoord", kFloat2_GrVertexAttribType, |
| 221 | kFloat2_GrSLType}; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 222 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 223 | if (fFlags & kCoverageAttribute_GPFlag) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 224 | fInCoverage = {"inCoverage", kFloat_GrVertexAttribType, kHalf_GrSLType}; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 225 | } |
Brian Osman | 37064c1 | 2019-02-08 10:53:07 -0500 | [diff] [blame] | 226 | this->setVertexAttributes(&fInPosition, 4); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | Attribute fInPosition; |
| 230 | Attribute fInColor; |
| 231 | Attribute fInLocalCoords; |
| 232 | Attribute fInCoverage; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 233 | SkPMColor4f fColor; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 234 | SkMatrix fViewMatrix; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 235 | SkMatrix fLocalMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 236 | uint8_t fCoverage; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 237 | uint32_t fFlags; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 238 | bool fLocalCoordsWillBeRead; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 239 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 240 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 241 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 242 | using INHERITED = GrGeometryProcessor; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); |
| 246 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 247 | #if GR_TEST_UTILS |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 248 | GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 249 | uint32_t flags = 0; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 250 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 251 | flags |= kColorAttribute_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 252 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 253 | if (d->fRandom->nextBool()) { |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 254 | flags |= kColorAttributeIsWide_GPFlag; |
| 255 | } |
| 256 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 257 | flags |= kCoverageAttribute_GPFlag; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 258 | if (d->fRandom->nextBool()) { |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 259 | flags |= (d->fRandom->nextBool()) ? kCoverageAttributeTweak_GPFlag |
| 260 | : kCoverageAttributeUnclamped_GPFlag; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 261 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 262 | } |
| 263 | if (d->fRandom->nextBool()) { |
| 264 | flags |= kLocalCoordAttribute_GPFlag; |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 265 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 266 | |
John Stiles | 391a9f6 | 2021-04-29 23:17:41 -0400 | [diff] [blame] | 267 | GrColor color = GrRandomColor(d->fRandom); |
| 268 | SkMatrix viewMtx = GrTest::TestMatrix(d->fRandom); |
| 269 | SkMatrix localMtx = GrTest::TestMatrix(d->fRandom); |
| 270 | bool readsLocalCoords = d->fRandom->nextBool(); |
| 271 | uint8_t coverage = GrRandomCoverage(d->fRandom); |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 272 | return DefaultGeoProc::Make(d->allocator(), |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 273 | flags, |
John Stiles | 391a9f6 | 2021-04-29 23:17:41 -0400 | [diff] [blame] | 274 | SkPMColor4f::FromBytes_RGBA(color), |
| 275 | viewMtx, |
| 276 | localMtx, |
| 277 | readsLocalCoords, |
| 278 | coverage); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 279 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 280 | #endif |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 281 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 282 | GrGeometryProcessor* GrDefaultGeoProcFactory::Make(SkArenaAlloc* arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 283 | const Color& color, |
| 284 | const Coverage& coverage, |
| 285 | const LocalCoords& localCoords, |
| 286 | const SkMatrix& viewMatrix) { |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 287 | uint32_t flags = 0; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 288 | if (Color::kPremulGrColorAttribute_Type == color.fType) { |
| 289 | flags |= kColorAttribute_GPFlag; |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 290 | } else if (Color::kPremulWideColorAttribute_Type == color.fType) { |
| 291 | flags |= kColorAttribute_GPFlag | kColorAttributeIsWide_GPFlag; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 292 | } |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 293 | if (Coverage::kAttribute_Type == coverage.fType) { |
| 294 | flags |= kCoverageAttribute_GPFlag; |
| 295 | } else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) { |
| 296 | flags |= kCoverageAttribute_GPFlag | kCoverageAttributeTweak_GPFlag; |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 297 | } else if (Coverage::kAttributeUnclamped_Type == coverage.fType) { |
| 298 | flags |= kCoverageAttribute_GPFlag | kCoverageAttributeUnclamped_GPFlag; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 299 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 300 | flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 301 | |
| 302 | uint8_t inCoverage = coverage.fCoverage; |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 303 | bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 304 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 305 | return DefaultGeoProc::Make(arena, |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 306 | flags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 307 | color.fColor, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 308 | viewMatrix, |
| 309 | localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(), |
| 310 | localCoordsWillBeRead, |
Brian Osman | 37064c1 | 2019-02-08 10:53:07 -0500 | [diff] [blame] | 311 | inCoverage); |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 312 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 313 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 314 | GrGeometryProcessor* GrDefaultGeoProcFactory::MakeForDeviceSpace(SkArenaAlloc* arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 315 | const Color& color, |
| 316 | const Coverage& coverage, |
| 317 | const LocalCoords& localCoords, |
| 318 | const SkMatrix& viewMatrix) { |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 319 | SkMatrix invert = SkMatrix::I(); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 320 | if (LocalCoords::kUnused_Type != localCoords.fType) { |
| 321 | SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType); |
| 322 | if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 323 | return nullptr; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 324 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 325 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 326 | if (localCoords.hasLocalMatrix()) { |
Michael Ludwig | ef77604 | 2018-11-01 11:07:51 -0400 | [diff] [blame] | 327 | invert.postConcat(*localCoords.fMatrix); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 328 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert); |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 332 | return Make(arena, color, coverage, inverted, SkMatrix::I()); |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 333 | } |