1 /*
2  * Copyright 2017 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 GrCCQuadraticShader_DEFINED
9 #define GrCCQuadraticShader_DEFINED
10 
11 #include "ccpr/GrCCCoverageProcessor.h"
12 
13 /**
14  * This class renders the coverage of closed quadratic curves using the techniques outlined in
15  * "Resolution Independent Curve Rendering using Programmable Graphics Hardware" by Charles Loop and
16  * Jim Blinn:
17  *
18  * https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
19  *
20  * The provided curves must be monotonic with respect to the vector of their closing edge [P2 - P0].
21  * (Use GrCCGeometry.)
22  */
23 class GrCCQuadraticShader : public GrCCCoverageProcessor::Shader {
24 protected:
25     void emitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts, const char* repetitionID,
26                        const char* wind, GeometryVars*) const final;
27     virtual void onEmitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts, const char* repetitionID,
28                                  GeometryVars*) const = 0;
29 
30     void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code,
31                         const char* position, const char* inputCoverage, const char* wind) final;
onEmitVaryings(GrGLSLVaryingHandler *,GrGLSLVarying::Scope,SkString * code)32     virtual void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code) {}
33 
34     void onEmitFragmentCode(GrGLSLPPFragmentBuilder*, const char* outputCoverage) const final;
35     virtual void emitCoverage(GrGLSLPPFragmentBuilder*, const char* outputCoverage) const = 0;
36 
37     const GrShaderVar fCanonicalMatrix{"canonical_matrix", kFloat3x3_GrSLType};
38     const GrShaderVar fEdgeDistanceEquation{"edge_distance_equation", kFloat3_GrSLType};
39     GrGLSLVarying fXYDW;
40 };
41 
42 /**
43  * This pass draws a conservative raster hull around the quadratic bezier curve, computes the
44  * curve's coverage using the gradient-based AA technique outlined in the Loop/Blinn paper, and
45  * uses simple distance-to-edge to subtract out coverage for the flat closing edge [P2 -> P0]. Since
46  * the provided curves are monotonic, this will get every pixel right except the two corners.
47  */
48 class GrCCQuadraticHullShader : public GrCCQuadraticShader {
49     void onEmitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts, const char* repetitionID,
50                          GeometryVars*) const override;
51     void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code) override;
52     void emitCoverage(GrGLSLPPFragmentBuilder*, const char* outputCoverage) const override;
53 
54     GrGLSLVarying fGrad;
55 };
56 
57 /**
58  * This pass fixes the corners of a closed quadratic segment with soft MSAA.
59  */
60 class GrCCQuadraticCornerShader : public GrCCQuadraticShader {
61     void onEmitSetupCode(GrGLSLVertexGeoBuilder*, const char* pts, const char* repetitionID,
62                          GeometryVars*) const override;
63     void onEmitVaryings(GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code) override;
64     void emitCoverage(GrGLSLPPFragmentBuilder*, const char* outputCoverage) const override;
65 
66     GrGLSLVarying fdXYDdx;
67     GrGLSLVarying fdXYDdy;
68 };
69 
70 #endif
71