OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrPathProcessor.h" | 8 #include "GrPathProcessor.h" |
9 | 9 |
10 #include "gl/GrGLPathProcessor.h" | |
11 #include "gl/GrGLGpu.h" | 10 #include "gl/GrGLGpu.h" |
12 | 11 |
13 #include "glsl/GrGLSLCaps.h" | 12 #include "glsl/GrGLSLCaps.h" |
14 | 13 |
| 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_Visibil
ity, |
| 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, TransformsOu
t* 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() ? kVec3
f_GrSLType : |
| 59 kVec2
f_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).toIn
dex(); |
| 66 fInstalledTransforms[i][t].fType = varyingType; |
| 67 |
| 68 SkNEW_APPEND_TO_TARRAY(&(*tout)[i], GrGLProcessor::TransformedCo
ords, |
| 69 (SkString(v.fsIn()), varyingType)); |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 74 void setData(const GrGLProgramDataManager& pd, const GrPrimitiveProcessor& p
rimProc) override { |
| 75 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
| 76 if (pathProc.opts().readsColor() && pathProc.color() != fColor) { |
| 77 GrGLfloat c[4]; |
| 78 GrColorToRGBAFloat(pathProc.color(), c); |
| 79 pd.set4fv(fColorUniform, 1, c); |
| 80 fColor = pathProc.color(); |
| 81 } |
| 82 } |
| 83 |
| 84 void setTransformData(const GrPrimitiveProcessor& primProc, |
| 85 const GrGLProgramDataManager& pdman, |
| 86 int index, |
| 87 const SkTArray<const GrCoordTransform*, true>& coordTr
ansforms) override { |
| 88 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>(); |
| 89 SkSTArray<2, Transform, true>& transforms = fInstalledTransforms[index]; |
| 90 int numTransforms = transforms.count(); |
| 91 for (int t = 0; t < numTransforms; ++t) { |
| 92 SkASSERT(transforms[t].fHandle.isValid()); |
| 93 const SkMatrix& transform = GetTransformMatrix(pathProc.localMatrix(
), |
| 94 *coordTransforms[t]); |
| 95 if (transforms[t].fCurrentValue.cheapEqualTo(transform)) { |
| 96 continue; |
| 97 } |
| 98 transforms[t].fCurrentValue = transform; |
| 99 |
| 100 SkASSERT(transforms[t].fType == kVec2f_GrSLType || |
| 101 transforms[t].fType == kVec3f_GrSLType); |
| 102 unsigned components = transforms[t].fType == kVec2f_GrSLType ? 2 : 3
; |
| 103 pdman.setPathFragmentInputTransform(transforms[t].fHandle, component
s, transform); |
| 104 } |
| 105 } |
| 106 |
| 107 private: |
| 108 UniformHandle fColorUniform; |
| 109 GrColor fColor; |
| 110 |
| 111 typedef GrGLPrimitiveProcessor INHERITED; |
| 112 }; |
| 113 |
15 GrPathProcessor::GrPathProcessor(GrColor color, | 114 GrPathProcessor::GrPathProcessor(GrColor color, |
16 const GrPipelineOptimizations& opts, | 115 const GrPipelineOptimizations& opts, |
17 const SkMatrix& viewMatrix, | 116 const SkMatrix& viewMatrix, |
18 const SkMatrix& localMatrix) | 117 const SkMatrix& localMatrix) |
19 : INHERITED(true) | 118 : INHERITED(true) |
20 , fColor(color) | 119 , fColor(color) |
21 , fViewMatrix(viewMatrix) | 120 , fViewMatrix(viewMatrix) |
22 , fLocalMatrix(localMatrix) | 121 , fLocalMatrix(localMatrix) |
23 , fOpts(opts) { | 122 , fOpts(opts) { |
24 this->initClassID<GrPathProcessor>(); | 123 this->initClassID<GrPathProcessor>(); |
25 } | 124 } |
26 | 125 |
27 void GrPathProcessor::getGLProcessorKey(const GrGLSLCaps& caps, | 126 void GrPathProcessor::getGLProcessorKey(const GrGLSLCaps& caps, |
28 GrProcessorKeyBuilder* b) const { | 127 GrProcessorKeyBuilder* b) const { |
29 GrGLPathProcessor::GenKey(*this, caps, b); | 128 GrGLPathProcessor::GenKey(*this, caps, b); |
30 } | 129 } |
31 | 130 |
32 GrGLPrimitiveProcessor* GrPathProcessor::createGLInstance(const GrGLSLCaps& caps
) const { | 131 GrGLPrimitiveProcessor* GrPathProcessor::createGLInstance(const GrGLSLCaps& caps
) const { |
33 SkASSERT(caps.pathRenderingSupport()); | 132 SkASSERT(caps.pathRenderingSupport()); |
34 return new GrGLPathProcessor(); | 133 return new GrGLPathProcessor(); |
35 } | 134 } |
OLD | NEW |