1 /*
2  * Copyright 2018 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 GrQuadPerEdgeAA_DEFINED
9 #define GrQuadPerEdgeAA_DEFINED
10 
11 #include "GrColor.h"
12 #include "GrGeometryProcessor.h"
13 #include "GrMeshDrawOp.h"
14 #include "GrQuad.h"
15 #include "GrSamplerState.h"
16 #include "GrTypesPriv.h"
17 #include "SkPoint.h"
18 #include "SkPoint3.h"
19 
20 class GrColorSpaceXform;
21 class GrShaderCaps;
22 
23 namespace GrQuadPerEdgeAA {
24 
25     enum class Domain : bool { kNo = false, kYes = true };
26     enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
27     static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
28 
29     // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
30     // order (when enabled) is device position, color, local position, domain, aa edge equations.
31     // This order matches the constructor argument order of VertexSpec and is the order that
32     // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored.
33     struct VertexSpec {
34     public:
VertexSpecVertexSpec35         VertexSpec(GrQuadType deviceQuadType, ColorType colorType, GrQuadType localQuadType,
36                    bool hasLocalCoords, Domain domain, GrAAType aa, bool alphaAsCoverage)
37                 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
38                 , fLocalQuadType(static_cast<unsigned>(localQuadType))
39                 , fHasLocalCoords(hasLocalCoords)
40                 , fColorType(static_cast<unsigned>(colorType))
41                 , fHasDomain(static_cast<unsigned>(domain))
42                 , fUsesCoverageAA(aa == GrAAType::kCoverage)
43                 , fCompatibleWithAlphaAsCoverage(alphaAsCoverage) { }
44 
deviceQuadTypeVertexSpec45         GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); }
localQuadTypeVertexSpec46         GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); }
hasLocalCoordsVertexSpec47         bool hasLocalCoords() const { return fHasLocalCoords; }
colorTypeVertexSpec48         ColorType colorType() const { return static_cast<ColorType>(fColorType); }
hasVertexColorsVertexSpec49         bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
hasDomainVertexSpec50         bool hasDomain() const { return fHasDomain; }
usesCoverageAAVertexSpec51         bool usesCoverageAA() const { return fUsesCoverageAA; }
compatibleWithAlphaAsCoverageVertexSpec52         bool compatibleWithAlphaAsCoverage() const { return fCompatibleWithAlphaAsCoverage; }
53 
54         // Will always be 2 or 3
55         int deviceDimensionality() const;
56         // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
57         int localDimensionality() const;
58 
verticesPerQuadVertexSpec59         int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
60     private:
61         static_assert(kGrQuadTypeCount <= 4, "GrQuadType doesn't fit in 2 bits");
62         static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
63 
64         unsigned fDeviceQuadType: 2;
65         unsigned fLocalQuadType: 2;
66         unsigned fHasLocalCoords: 1;
67         unsigned fColorType : 2;
68         unsigned fHasDomain: 1;
69         unsigned fUsesCoverageAA: 1;
70         unsigned fCompatibleWithAlphaAsCoverage: 1;
71     };
72 
73     sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
74 
75     sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec,
76             const GrShaderCaps& caps, GrTextureType textureType, GrPixelConfig textureConfig,
77             const GrSamplerState& samplerState, uint32_t extraSamplerKey,
78             sk_sp<GrColorSpaceXform> textureColorSpaceXform);
79 
80     // Fill vertices with the vertex data needed to represent the given quad. The device position,
81     // local coords, vertex color, domain, and edge coefficients will be written and/or computed
82     // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
83     // then its corresponding function argument is ignored.
84     //
85     // Returns the advanced pointer in vertices.
86     void* Tessellate(void* vertices, const VertexSpec& spec, const GrPerspQuad& deviceQuad,
87                      const SkPMColor4f& color, const GrPerspQuad& localQuad, const SkRect& domain,
88                      GrQuadAAFlags aa);
89 
90     // The mesh will have its index data configured to meet the expectations of the Tessellate()
91     // function, but it the calling code must handle filling a vertex buffer via Tessellate() and
92     // then assigning it to the returned mesh.
93     //
94     // Returns false if the index data could not be allocated.
95     bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec,
96                               int quadCount);
97 
98     static constexpr int kNumAAQuadsInIndexBuffer = 512;
99 
100 } // namespace GrQuadPerEdgeAA
101 
102 #endif // GrQuadPerEdgeAA_DEFINED
103