jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 8 | #include "GrPathProcessor.h" |
| 9 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 10 | #include "gl/GrGLGpu.h" |
| 11 | |
jvanverth | cba99b8 | 2015-06-24 06:59:57 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLCaps.h" |
| 13 | |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 14 | class GrGLPathProcessor : public GrGLPrimitiveProcessor { |
| 15 | public: |
| 16 | GrGLPathProcessor() : fColor(GrColor_ILLEGAL) {} |
| 17 | |
| 18 | static void GenKey(const GrPathProcessor& pathProc, |
| 19 | const GrGLSLCaps&, |
| 20 | GrProcessorKeyBuilder* b) { |
| 21 | b->add32(SkToInt(pathProc.opts().readsColor()) | |
| 22 | SkToInt(pathProc.opts().readsCoverage()) << 16); |
| 23 | } |
| 24 | |
| 25 | void emitCode(EmitArgs& args) override { |
| 26 | GrGLGPBuilder* pb = args.fPB; |
| 27 | GrGLFragmentBuilder* fs = args.fPB->getFragmentShaderBuilder(); |
| 28 | const GrPathProcessor& pathProc = args.fGP.cast<GrPathProcessor>(); |
| 29 | |
| 30 | // emit transforms |
| 31 | this->emitTransforms(args.fPB, args.fTransformsIn, args.fTransformsOut); |
| 32 | |
| 33 | // Setup uniform color |
| 34 | if (pathProc.opts().readsColor()) { |
| 35 | const char* stagedLocalVarName; |
| 36 | fColorUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 37 | kVec4f_GrSLType, |
| 38 | kDefault_GrSLPrecision, |
| 39 | "Color", |
| 40 | &stagedLocalVarName); |
| 41 | fs->codeAppendf("%s = %s;", args.fOutputColor, stagedLocalVarName); |
| 42 | } |
| 43 | |
| 44 | // setup constant solid coverage |
| 45 | if (pathProc.opts().readsCoverage()) { |
| 46 | fs->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void emitTransforms(GrGLGPBuilder* pb, const TransformsIn& tin, TransformsOut* tout) { |
| 51 | tout->push_back_n(tin.count()); |
| 52 | fInstalledTransforms.push_back_n(tin.count()); |
| 53 | for (int i = 0; i < tin.count(); i++) { |
| 54 | const ProcCoords& coordTransforms = tin[i]; |
| 55 | fInstalledTransforms[i].push_back_n(coordTransforms.count()); |
| 56 | for (int t = 0; t < coordTransforms.count(); t++) { |
| 57 | GrSLType varyingType = |
| 58 | coordTransforms[t]->getMatrix().hasPerspective() ? kVec3f_GrSLType : |
| 59 | kVec2f_GrSLType; |
| 60 | |
| 61 | SkString strVaryingName("MatrixCoord"); |
| 62 | strVaryingName.appendf("_%i_%i", i, t); |
| 63 | GrGLVertToFrag v(varyingType); |
| 64 | fInstalledTransforms[i][t].fHandle = |
| 65 | pb->addSeparableVarying(strVaryingName.c_str(), &v).toIndex(); |
| 66 | fInstalledTransforms[i][t].fType = varyingType; |
| 67 | |
| 68 | SkNEW_APPEND_TO_TARRAY(&(*tout)[i], GrGLProcessor::TransformedCoords, |
| 69 | (SkString(v.fsIn()), varyingType)); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame^] | 74 | void setData(const GrGLSLProgramDataManager& pd, |
| 75 | const GrPrimitiveProcessor& primProc) override { |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 76 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
| 77 | if (pathProc.opts().readsColor() && pathProc.color() != fColor) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame^] | 78 | float c[4]; |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 79 | GrColorToRGBAFloat(pathProc.color(), c); |
| 80 | pd.set4fv(fColorUniform, 1, c); |
| 81 | fColor = pathProc.color(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void setTransformData(const GrPrimitiveProcessor& primProc, |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame^] | 86 | const GrGLSLProgramDataManager& pdman, |
joshualitt | 102081a | 2015-09-11 11:52:17 -0700 | [diff] [blame] | 87 | int index, |
| 88 | const SkTArray<const GrCoordTransform*, true>& coordTransforms) override { |
| 89 | const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
| 90 | SkSTArray<2, Transform, true>& transforms = fInstalledTransforms[index]; |
| 91 | int numTransforms = transforms.count(); |
| 92 | for (int t = 0; t < numTransforms; ++t) { |
| 93 | SkASSERT(transforms[t].fHandle.isValid()); |
| 94 | const SkMatrix& transform = GetTransformMatrix(pathProc.localMatrix(), |
| 95 | *coordTransforms[t]); |
| 96 | if (transforms[t].fCurrentValue.cheapEqualTo(transform)) { |
| 97 | continue; |
| 98 | } |
| 99 | transforms[t].fCurrentValue = transform; |
| 100 | |
| 101 | SkASSERT(transforms[t].fType == kVec2f_GrSLType || |
| 102 | transforms[t].fType == kVec3f_GrSLType); |
| 103 | unsigned components = transforms[t].fType == kVec2f_GrSLType ? 2 : 3; |
| 104 | pdman.setPathFragmentInputTransform(transforms[t].fHandle, components, transform); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | UniformHandle fColorUniform; |
| 110 | GrColor fColor; |
| 111 | |
| 112 | typedef GrGLPrimitiveProcessor INHERITED; |
| 113 | }; |
| 114 | |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 115 | GrPathProcessor::GrPathProcessor(GrColor color, |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 116 | const GrPipelineOptimizations& opts, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 117 | const SkMatrix& viewMatrix, |
| 118 | const SkMatrix& localMatrix) |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 119 | : INHERITED(true) |
| 120 | , fColor(color) |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 121 | , fViewMatrix(viewMatrix) |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 122 | , fLocalMatrix(localMatrix) |
| 123 | , fOpts(opts) { |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 124 | this->initClassID<GrPathProcessor>(); |
| 125 | } |
| 126 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 127 | void GrPathProcessor::getGLProcessorKey(const GrGLSLCaps& caps, |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 128 | GrProcessorKeyBuilder* b) const { |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 129 | GrGLPathProcessor::GenKey(*this, caps, b); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 130 | } |
| 131 | |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 132 | GrGLPrimitiveProcessor* GrPathProcessor::createGLInstance(const GrGLSLCaps& caps) const { |
jvanverth | 5053063 | 2015-04-27 10:36:27 -0700 | [diff] [blame] | 133 | SkASSERT(caps.pathRenderingSupport()); |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 134 | return new GrGLPathProcessor(); |
joshualitt | 8072caa | 2015-02-12 14:20:52 -0800 | [diff] [blame] | 135 | } |