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 GrCCPathProcessor_DEFINED
9 #define GrCCPathProcessor_DEFINED
10 
11 #include <array>
12 #include "GrCaps.h"
13 #include "GrGeometryProcessor.h"
14 #include "GrPipeline.h"
15 #include "SkPath.h"
16 
17 class GrCCPathCacheEntry;
18 class GrCCPerFlushResources;
19 class GrOnFlushResourceProvider;
20 class GrOpFlushState;
21 
22 /**
23  * This class draws AA paths using the coverage count masks produced by GrCCCoverageProcessor.
24  *
25  * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and
26  * fill rule.
27  *
28  * To draw paths, the caller must set up an instance buffer as detailed below, then call drawPaths()
29  * providing its own instance buffer alongside the buffers found by calling FindIndexBuffer/
30  * FindVertexBuffer.
31  */
32 class GrCCPathProcessor : public GrGeometryProcessor {
33 public:
34     enum class InstanceAttribs {
35         kDevBounds,
36         kDevBounds45,
37         kDevToAtlasOffset,
38         kColor
39     };
40     static constexpr int kNumInstanceAttribs = 1 + (int)InstanceAttribs::kColor;
41 
42     // Helper to offset the 45-degree bounding box returned by GrCCPathParser::parsePath().
MakeOffset45(const SkRect & devBounds45,float dx,float dy)43     static SkRect MakeOffset45(const SkRect& devBounds45, float dx, float dy) {
44         // devBounds45 is in "| 1  -1 | * devCoords" space.
45         //                    | 1   1 |
46         return devBounds45.makeOffset(dx - dy, dx + dy);
47     }
48 
49     enum class DoEvenOddFill : bool {
50         kNo = false,
51         kYes = true
52     };
53 
54     struct Instance {
55         SkRect fDevBounds;  // "right < left" indicates even-odd fill type.
56         SkRect fDevBounds45;  // Bounding box in "| 1  -1 | * devCoords" space.
57                               //                  | 1   1 |
58         SkIVector fDevToAtlasOffset;  // Translation from device space to location in atlas.
59         uint64_t fColor;  // Color always stored as 4 x fp16
60 
61         void set(const SkRect& devBounds, const SkRect& devBounds45,
62                  const SkIVector& devToAtlasOffset, uint64_t, DoEvenOddFill = DoEvenOddFill::kNo);
63         void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t,
64                  DoEvenOddFill = DoEvenOddFill::kNo);
65     };
66 
67     GR_STATIC_ASSERT(4 * 12 == sizeof(Instance));
68 
69     static sk_sp<const GrBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
70     static sk_sp<const GrBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
71 
72     GrCCPathProcessor(const GrTextureProxy* atlas,
73                       const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I());
74 
name()75     const char* name() const override { return "GrCCPathProcessor"; }
atlasSize()76     const SkISize& atlasSize() const { return fAtlasSize; }
atlasOrigin()77     GrSurfaceOrigin atlasOrigin() const { return fAtlasOrigin; }
localMatrix()78     const SkMatrix& localMatrix() const { return fLocalMatrix; }
getInstanceAttrib(InstanceAttribs attribID)79     const Attribute& getInstanceAttrib(InstanceAttribs attribID) const {
80         int idx = static_cast<int>(attribID);
81         SkASSERT(idx >= 0 && idx < static_cast<int>(SK_ARRAY_COUNT(kInstanceAttribs)));
82         return kInstanceAttribs[idx];
83     }
getEdgeNormsAttrib()84     const Attribute& getEdgeNormsAttrib() const { return kEdgeNormsAttrib; }
85 
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *)86     void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
87     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
88 
89     void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
90                    const GrCCPerFlushResources&, int baseInstance, int endInstance,
91                    const SkRect& bounds) const;
92 
93 private:
onTextureSampler(int)94     const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
95 
96     const TextureSampler fAtlasAccess;
97     SkISize fAtlasSize;
98     GrSurfaceOrigin fAtlasOrigin;
99 
100     SkMatrix fLocalMatrix;
101     static constexpr Attribute kInstanceAttribs[kNumInstanceAttribs] = {
102             {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
103             {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
104             {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
105             {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
106     };
107     static constexpr Attribute kEdgeNormsAttrib = {"edge_norms", kFloat4_GrVertexAttribType,
108                                                                  kFloat4_GrSLType};
109 
110     typedef GrGeometryProcessor INHERITED;
111 };
112 
set(const SkRect & devBounds,const SkRect & devBounds45,const SkIVector & devToAtlasOffset,uint64_t color,DoEvenOddFill doEvenOddFill)113 inline void GrCCPathProcessor::Instance::set(const SkRect& devBounds, const SkRect& devBounds45,
114                                              const SkIVector& devToAtlasOffset, uint64_t color,
115                                              DoEvenOddFill doEvenOddFill) {
116     if (DoEvenOddFill::kYes == doEvenOddFill) {
117         // "right < left" indicates even-odd fill type.
118         fDevBounds.setLTRB(devBounds.fRight, devBounds.fTop, devBounds.fLeft, devBounds.fBottom);
119     } else {
120         fDevBounds = devBounds;
121     }
122     fDevBounds45 = devBounds45;
123     fDevToAtlasOffset = devToAtlasOffset;
124     fColor = color;
125 }
126 
127 #endif
128