• 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 GrCCPathProcessor_DEFINED
9 #define GrCCPathProcessor_DEFINED
10 
11 #include <array>
12 #include "include/core/SkPath.h"
13 #include "src/gpu/GrCaps.h"
14 #include "src/gpu/GrGeometryProcessor.h"
15 #include "src/gpu/GrPipeline.h"
16 #include "src/gpu/ccpr/GrCCAtlas.h"
17 #include "src/gpu/ccpr/GrOctoBounds.h"
18 
19 class GrCCPathCacheEntry;
20 class GrCCPerFlushResources;
21 class GrOnFlushResourceProvider;
22 class GrOpFlushState;
23 
24 /**
25  * This class draws AA paths using the coverage count masks produced by GrCCCoverageProcessor.
26  *
27  * Paths are drawn as bloated octagons, and coverage is derived from the coverage count mask and
28  * fill rule.
29  *
30  * To draw paths, the caller must set up an instance buffer as detailed below, then call drawPaths()
31  * providing its own instance buffer alongside the buffers found by calling FindIndexBuffer/
32  * FindVertexBuffer.
33  */
34 class GrCCPathProcessor : public GrGeometryProcessor {
35 public:
36     struct Instance {
37         SkRect fDevBounds;  // "right < left" indicates even-odd fill type.
38         SkRect fDevBounds45;  // Bounding box in "| 1  -1 | * devCoords" space. See GrOctoBounds.
39                               //                  | 1   1 |
40         SkIVector fDevToAtlasOffset;  // Translation from device space to location in atlas.
41         uint64_t fColor;  // Color always stored as 4 x fp16
42 
43         void set(const GrOctoBounds&, const SkIVector& devToAtlasOffset, uint64_t, GrFillRule);
44         void set(const GrCCPathCacheEntry&, const SkIVector& shift, uint64_t, GrFillRule);
45     };
46 
47     GR_STATIC_ASSERT(4 * 12 == sizeof(Instance));
48 
49     static sk_sp<const GrGpuBuffer> FindVertexBuffer(GrOnFlushResourceProvider*);
50     static sk_sp<const GrGpuBuffer> FindIndexBuffer(GrOnFlushResourceProvider*);
51 
52     enum class CoverageMode : bool {
53         kCoverageCount,
54         kLiteral
55     };
56 
GetCoverageMode(GrCCAtlas::CoverageType coverageType)57     static CoverageMode GetCoverageMode(GrCCAtlas::CoverageType coverageType) {
58         return (GrCCAtlas::CoverageType::kFP16_CoverageCount == coverageType)
59                 ? CoverageMode::kCoverageCount
60                 : CoverageMode::kLiteral;
61     }
62 
63     GrCCPathProcessor(
64             CoverageMode, const GrTexture* atlasTexture, const GrSwizzle&,
65             GrSurfaceOrigin atlasOrigin,
66             const SkMatrix& viewMatrixIfUsingLocalCoords = SkMatrix::I());
67 
name()68     const char* name() const override { return "GrCCPathProcessor"; }
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b)69     void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
70         b->add32((uint32_t)fCoverageMode);
71     }
72     GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
73 
74     void drawPaths(GrOpFlushState*, const GrPipeline&, const GrPipeline::FixedDynamicState*,
75                    const GrCCPerFlushResources&, int baseInstance, int endInstance,
76                    const SkRect& bounds) const;
77 
78 private:
onTextureSampler(int)79     const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
80 
81     const CoverageMode fCoverageMode;
82     const TextureSampler fAtlasAccess;
83     SkISize fAtlasSize;
84     GrSurfaceOrigin fAtlasOrigin;
85 
86     SkMatrix fLocalMatrix;
87     static constexpr Attribute kInstanceAttribs[] = {
88             {"devbounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
89             {"devbounds45", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
90             {"dev_to_atlas_offset", kInt2_GrVertexAttribType, kInt2_GrSLType},
91             {"color", kHalf4_GrVertexAttribType, kHalf4_GrSLType}
92     };
93     static constexpr int kColorAttribIdx = 3;
94     static constexpr Attribute kCornersAttrib =
95             {"corners", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
96 
97     class Impl;
98 
99     typedef GrGeometryProcessor INHERITED;
100 };
101 
set(const GrOctoBounds & octoBounds,const SkIVector & devToAtlasOffset,uint64_t color,GrFillRule fillRule)102 inline void GrCCPathProcessor::Instance::set(
103         const GrOctoBounds& octoBounds, const SkIVector& devToAtlasOffset, uint64_t color,
104         GrFillRule fillRule) {
105     if (GrFillRule::kNonzero == fillRule) {
106         // We cover "nonzero" paths with clockwise triangles, which is the default result from
107         // normal octo bounds.
108         fDevBounds = octoBounds.bounds();
109         fDevBounds45 = octoBounds.bounds45();
110     } else {
111         // We cover "even/odd" paths with counterclockwise triangles. Here we reorder the bounding
112         // box vertices so the output is flipped horizontally.
113         fDevBounds.setLTRB(
114                 octoBounds.right(), octoBounds.top(), octoBounds.left(), octoBounds.bottom());
115         fDevBounds45.setLTRB(
116                 octoBounds.bottom45(), octoBounds.right45(), octoBounds.top45(),
117                 octoBounds.left45());
118     }
119     fDevToAtlasOffset = devToAtlasOffset;
120     fColor = color;
121 }
122 
123 #endif
124