• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrCCCoverageProcessor_DEFINED
9 #define GrCCCoverageProcessor_DEFINED
10 
11 #include "include/private/SkNx.h"
12 #include "src/gpu/GrCaps.h"
13 #include "src/gpu/GrGeometryProcessor.h"
14 #include "src/gpu/GrPipeline.h"
15 #include "src/gpu/GrShaderCaps.h"
16 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
17 #include "src/gpu/glsl/GrGLSLShaderBuilder.h"
18 #include "src/gpu/glsl/GrGLSLVarying.h"
19 
20 class GrGLSLFPFragmentBuilder;
21 class GrGLSLVertexGeoBuilder;
22 class GrMesh;
23 class GrOpFlushState;
24 
25 /**
26  * This is the geometry processor for the simple convex primitive shapes (triangles and closed,
27  * convex bezier curves) from which ccpr paths are composed. The output is a single-channel alpha
28  * value, positive for clockwise shapes and negative for counter-clockwise, that indicates coverage.
29  *
30  * The caller is responsible to draw all primitives as produced by GrCCGeometry into a cleared,
31  * floating point, alpha-only render target using SkBlendMode::kPlus. Once all of a path's
32  * primitives have been drawn, the render target contains a composite coverage count that can then
33  * be used to draw the path (see GrCCPathProcessor).
34  *
35  * To draw primitives, use appendMesh() and draw() (defined below).
36  */
37 class GrCCCoverageProcessor : public GrGeometryProcessor {
38 public:
39     enum class PrimitiveType {
40         kTriangles,
41         kWeightedTriangles,  // Triangles (from the tessellator) whose winding magnitude > 1.
42         kQuadratics,
43         kCubics,
44         kConics
45     };
46     static const char* PrimitiveTypeName(PrimitiveType);
47 
48     // Defines a single primitive shape with 3 input points (i.e. Triangles and Quadratics).
49     // X,Y point values are transposed.
50     struct TriPointInstance {
51         float fValues[6];
52 
53         enum class Ordering : bool {
54             kXYTransposed,
55             kXYInterleaved,
56         };
57 
58         void set(const SkPoint[3], const Sk2f& translate, Ordering);
59         void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& translate, Ordering);
60         void set(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering);
61     };
62 
63     // Defines a single primitive shape with 4 input points, or 3 input points plus a "weight"
64     // parameter duplicated in both lanes of the 4th input (i.e. Cubics, Conics, and Triangles with
65     // a weighted winding number). X,Y point values are transposed.
66     struct QuadPointInstance {
67         float fX[4];
68         float fY[4];
69 
70         void set(const SkPoint[4], float dx, float dy);
71         void setW(const SkPoint[3], const Sk2f& trans, float w);
72         void setW(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans, float w);
73         void setW(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& trans, float w);
74     };
75 
76     virtual void reset(PrimitiveType, GrResourceProvider*) = 0;
77 
primitiveType()78     PrimitiveType primitiveType() const { return fPrimitiveType; }
79 
80     // Number of bezier points for curves, or 3 for triangles.
numInputPoints()81     int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; }
82 
isTriangles()83     bool isTriangles() const {
84         return PrimitiveType::kTriangles == fPrimitiveType ||
85                PrimitiveType::kWeightedTriangles == fPrimitiveType;
86     }
87 
hasInputWeight()88     int hasInputWeight() const {
89         return PrimitiveType::kWeightedTriangles == fPrimitiveType ||
90                PrimitiveType::kConics == fPrimitiveType;
91     }
92 
93     // GrPrimitiveProcessor overrides.
name()94     const char* name() const override { return PrimitiveTypeName(fPrimitiveType); }
95 #ifdef SK_DEBUG
dumpInfo()96     SkString dumpInfo() const override {
97         return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
98     }
99 #endif
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b)100     void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
101         SkDEBUGCODE(this->getDebugBloatKey(b));
102         b->add32((int)fPrimitiveType);
103     }
104     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
105 
106 #ifdef SK_DEBUG
107     // Increases the 1/2 pixel AA bloat by a factor of debugBloat.
enableDebugBloat(float debugBloat)108     void enableDebugBloat(float debugBloat) { fDebugBloat = debugBloat; }
debugBloatEnabled()109     bool debugBloatEnabled() const { return fDebugBloat > 0; }
debugBloat()110     float debugBloat() const { SkASSERT(this->debugBloatEnabled()); return fDebugBloat; }
getDebugBloatKey(GrProcessorKeyBuilder * b)111     void getDebugBloatKey(GrProcessorKeyBuilder* b) const {
112         uint32_t bloatBits;
113         memcpy(&bloatBits, &fDebugBloat, 4);
114         b->add32(bloatBits);
115     }
116 #endif
117 
118     // Appends a GrMesh that will draw the provided instances. The instanceBuffer must be an array
119     // of either TriPointInstance or QuadPointInstance, depending on this processor's RendererPass,
120     // with coordinates in the desired shape's final atlas-space position.
121     virtual void appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
122                             int baseInstance, SkTArray<GrMesh>* out) const = 0;
123 
124     virtual void draw(GrOpFlushState*, const GrPipeline&, const SkIRect scissorRects[],
125                       const GrMesh[], int meshCount, const SkRect& drawBounds) const;
126 
127     virtual GrPrimitiveType primType() const = 0;
128 
129     // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
130     // provides details about shape-specific geometry.
131     class Shader {
132     public:
133         // Returns true if the Impl should not calculate the coverage argument for emitVaryings().
134         // If true, then "coverage" will have a signed magnitude of 1.
calculatesOwnEdgeCoverage()135         virtual bool calculatesOwnEdgeCoverage() const { return false; }
136 
137         // Called before generating geometry. Subclasses may set up internal member variables during
138         // this time that will be needed during onEmitVaryings (e.g. transformation matrices).
139         //
140         // If the 'outHull4' parameter is provided, and there are not 4 input points, the subclass
141         // is required to fill it with the name of a 4-point hull around which the Impl can generate
142         // its geometry. If it is left unchanged, the Impl will use the regular input points.
143         virtual void emitSetupCode(
144                 GrGLSLVertexGeoBuilder*, const char* pts, const char** outHull4 = nullptr) const {
145             SkASSERT(!outHull4);
146         }
147 
emitVaryings(GrGLSLVaryingHandler * varyingHandler,GrGLSLVarying::Scope scope,SkString * code,const char * position,const char * coverage,const char * cornerCoverage,const char * wind)148         void emitVaryings(
149                 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
150                 const char* position, const char* coverage, const char* cornerCoverage,
151                 const char* wind) {
152             SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
153             this->onEmitVaryings(
154                     varyingHandler, scope, code, position, coverage, cornerCoverage, wind);
155         }
156 
157         // Writes the signed coverage value at the current pixel to "outputCoverage".
158         virtual void emitFragmentCoverageCode(
159                 GrGLSLFPFragmentBuilder*, const char* outputCoverage) const = 0;
160 
161         // Assigns the built-in sample mask at the current pixel.
162         virtual void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const = 0;
163 
164         // Calculates the winding direction of the input points (+1, -1, or 0). Wind for extremely
165         // thin triangles gets rounded to zero.
166         static void CalcWind(const GrCCCoverageProcessor&, GrGLSLVertexGeoBuilder*, const char* pts,
167                              const char* outputWind);
168 
169         // Calculates an edge's coverage at a conservative raster vertex. The edge is defined by two
170         // clockwise-ordered points, 'leftPt' and 'rightPt'. 'rasterVertexDir' is a pair of +/-1
171         // values that point in the direction of conservative raster bloat, starting from an
172         // endpoint.
173         //
174         // Coverage values ramp from -1 (completely outside the edge) to 0 (completely inside).
175         static void CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder*, const char* leftPt,
176                                                   const char* rightPt, const char* rasterVertexDir,
177                                                   const char* outputCoverage);
178 
179         // Calculates an edge's coverage at two conservative raster vertices.
180         // (See CalcEdgeCoverageAtBloatVertex).
181         static void CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder*, const char* leftPt,
182                                                      const char* rightPt, const char* bloatDir1,
183                                                      const char* bloatDir2,
184                                                      const char* outputCoverages);
185 
186         // Corner boxes require an additional "attenuation" varying that is multiplied by the
187         // regular (linearly-interpolated) coverage. This function calculates the attenuation value
188         // to use in the single, outermost vertex. The remaining three vertices of the corner box
189         // all use an attenuation value of 1.
190         static void CalcCornerAttenuation(GrGLSLVertexGeoBuilder*, const char* leftDir,
191                                           const char* rightDir, const char* outputAttenuation);
192 
~Shader()193         virtual ~Shader() {}
194 
195     protected:
196         // Here the subclass adds its internal varyings to the handler and produces code to
197         // initialize those varyings from a given position and coverage values.
198         //
199         // NOTE: the coverage values are signed appropriately for wind.
200         //       'coverage' will only be +1 or -1 on curves.
201         virtual void onEmitVaryings(
202                 GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code, const char* position,
203                 const char* coverage, const char* cornerCoverage, const char* wind) = 0;
204 
205         // Returns the name of a Shader's internal varying at the point where where its value is
206         // assigned. This is intended to work whether called for a vertex or a geometry shader.
OutName(const GrGLSLVarying & varying)207         const char* OutName(const GrGLSLVarying& varying) const {
208             using Scope = GrGLSLVarying::Scope;
209             SkASSERT(Scope::kVertToGeo != varying.scope());
210             return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
211         }
212 
213         // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
AccessCodeString(GrGLSLShaderBuilder * s)214         inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
215     };
216 
217 protected:
218     // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
219     // accidentally bleed into neighbor pixels.
220     static constexpr float kAABloatRadius = 0.491111f;
221 
GrCCCoverageProcessor(ClassID classID)222     GrCCCoverageProcessor(ClassID classID) : INHERITED(classID) {}
223 
224     virtual GrGLSLPrimitiveProcessor* onCreateGLSLInstance(std::unique_ptr<Shader>) const = 0;
225 
226     // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
AccessCodeString(GrGLSLShaderBuilder * s)227     inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
228 
229     PrimitiveType fPrimitiveType;
230     SkDEBUGCODE(float fDebugBloat = 0);
231 
232     class TriangleShader;
233 
234     typedef GrGeometryProcessor INHERITED;
235 };
236 
PrimitiveTypeName(PrimitiveType type)237 inline const char* GrCCCoverageProcessor::PrimitiveTypeName(PrimitiveType type) {
238     switch (type) {
239         case PrimitiveType::kTriangles: return "kTriangles";
240         case PrimitiveType::kWeightedTriangles: return "kWeightedTriangles";
241         case PrimitiveType::kQuadratics: return "kQuadratics";
242         case PrimitiveType::kCubics: return "kCubics";
243         case PrimitiveType::kConics: return "kConics";
244     }
245     SK_ABORT("Invalid PrimitiveType");
246 }
247 
set(const SkPoint p[3],const Sk2f & translate,Ordering ordering)248 inline void GrCCCoverageProcessor::TriPointInstance::set(
249         const SkPoint p[3], const Sk2f& translate, Ordering ordering) {
250     this->set(p[0], p[1], p[2], translate, ordering);
251 }
252 
set(const SkPoint & p0,const SkPoint & p1,const SkPoint & p2,const Sk2f & translate,Ordering ordering)253 inline void GrCCCoverageProcessor::TriPointInstance::set(
254         const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, const Sk2f& translate,
255         Ordering ordering) {
256     Sk2f P0 = Sk2f::Load(&p0);
257     Sk2f P1 = Sk2f::Load(&p1);
258     Sk2f P2 = Sk2f::Load(&p2);
259     this->set(P0, P1, P2, translate, ordering);
260 }
261 
set(const Sk2f & P0,const Sk2f & P1,const Sk2f & P2,const Sk2f & translate,Ordering ordering)262 inline void GrCCCoverageProcessor::TriPointInstance::set(
263         const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering ordering) {
264     if (Ordering::kXYTransposed == ordering) {
265         Sk2f::Store3(fValues, P0 + translate, P1 + translate, P2 + translate);
266     } else {
267         (P0 + translate).store(fValues);
268         (P1 + translate).store(fValues + 2);
269         (P2 + translate).store(fValues + 4);
270     }
271 }
272 
set(const SkPoint p[4],float dx,float dy)273 inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
274     Sk4f X,Y;
275     Sk4f::Load2(p, &X, &Y);
276     (X + dx).store(&fX);
277     (Y + dy).store(&fY);
278 }
279 
setW(const SkPoint p[3],const Sk2f & trans,float w)280 inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint p[3], const Sk2f& trans,
281                                                            float w) {
282     this->setW(p[0], p[1], p[2], trans, w);
283 }
284 
setW(const SkPoint & p0,const SkPoint & p1,const SkPoint & p2,const Sk2f & trans,float w)285 inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint& p0, const SkPoint& p1,
286                                                            const SkPoint& p2, const Sk2f& trans,
287                                                            float w) {
288     Sk2f P0 = Sk2f::Load(&p0);
289     Sk2f P1 = Sk2f::Load(&p1);
290     Sk2f P2 = Sk2f::Load(&p2);
291     this->setW(P0, P1, P2, trans, w);
292 }
293 
setW(const Sk2f & P0,const Sk2f & P1,const Sk2f & P2,const Sk2f & trans,float w)294 inline void GrCCCoverageProcessor::QuadPointInstance::setW(const Sk2f& P0, const Sk2f& P1,
295                                                            const Sk2f& P2, const Sk2f& trans,
296                                                            float w) {
297     Sk2f W = Sk2f(w);
298     Sk2f::Store4(this, P0 + trans, P1 + trans, P2 + trans, W);
299 }
300 
301 #endif
302