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 "include/core/SkPoint.h"
12 #include "include/core/SkPoint3.h"
13 #include "include/private/GrTypesPriv.h"
14 #include "src/gpu/GrColor.h"
15 #include "src/gpu/GrGeometryProcessor.h"
16 #include "src/gpu/GrSamplerState.h"
17 #include "src/gpu/geometry/GrQuad.h"
18 #include "src/gpu/ops/GrMeshDrawOp.h"
19 #include "src/gpu/ops/GrTextureOp.h"
20 
21 class GrCaps;
22 class GrColorSpaceXform;
23 class GrShaderCaps;
24 
25 namespace GrQuadPerEdgeAA {
26     using Saturate = GrTextureOp::Saturate;
27 
28     enum class CoverageMode { kNone, kWithPosition, kWithColor };
29     enum class Domain : bool { kNo = false, kYes = true };
30     enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
31     static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
32 
33     // Gets the minimum ColorType that can represent a color.
34     ColorType MinColorType(SkPMColor4f, GrClampType, const GrCaps&);
35 
36     // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
37     // order (when enabled) is device position, color, local position, domain, aa edge equations.
38     // This order matches the constructor argument order of VertexSpec and is the order that
39     // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored.
40     struct VertexSpec {
41     public:
VertexSpecVertexSpec42         VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType,
43                    bool hasLocalCoords, Domain domain, GrAAType aa, bool coverageAsAlpha)
44                 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
45                 , fLocalQuadType(static_cast<unsigned>(localQuadType))
46                 , fHasLocalCoords(hasLocalCoords)
47                 , fColorType(static_cast<unsigned>(colorType))
48                 , fHasDomain(static_cast<unsigned>(domain))
49                 , fUsesCoverageAA(aa == GrAAType::kCoverage)
50                 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha)
51                 , fRequiresGeometryDomain(aa == GrAAType::kCoverage &&
52                                           deviceQuadType > GrQuad::Type::kRectilinear) { }
53 
deviceQuadTypeVertexSpec54         GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); }
localQuadTypeVertexSpec55         GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); }
hasLocalCoordsVertexSpec56         bool hasLocalCoords() const { return fHasLocalCoords; }
colorTypeVertexSpec57         ColorType colorType() const { return static_cast<ColorType>(fColorType); }
hasVertexColorsVertexSpec58         bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
hasDomainVertexSpec59         bool hasDomain() const { return fHasDomain; }
usesCoverageAAVertexSpec60         bool usesCoverageAA() const { return fUsesCoverageAA; }
compatibleWithCoverageAsAlphaVertexSpec61         bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
requiresGeometryDomainVertexSpec62         bool requiresGeometryDomain() const { return fRequiresGeometryDomain; }
63         // Will always be 2 or 3
64         int deviceDimensionality() const;
65         // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
66         int localDimensionality() const;
67 
verticesPerQuadVertexSpec68         int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
69 
70         CoverageMode coverageMode() const;
71         size_t vertexSize() const;
72 
73     private:
74         static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits");
75         static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
76 
77         unsigned fDeviceQuadType: 2;
78         unsigned fLocalQuadType: 2;
79         unsigned fHasLocalCoords: 1;
80         unsigned fColorType : 2;
81         unsigned fHasDomain: 1;
82         unsigned fUsesCoverageAA: 1;
83         unsigned fCompatibleWithCoverageAsAlpha: 1;
84         // The geometry domain serves to clip off pixels touched by quads with sharp corners that
85         // would otherwise exceed the miter limit for the AA-outset geometry.
86         unsigned fRequiresGeometryDomain: 1;
87     };
88 
89     sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
90 
91     sk_sp<GrGeometryProcessor> MakeTexturedProcessor(
92             const VertexSpec& spec, const GrShaderCaps& caps, GrTextureType textureType,
93             const GrSamplerState& samplerState, const GrSwizzle& swizzle, uint32_t extraSamplerKey,
94             sk_sp<GrColorSpaceXform> textureColorSpaceXform, Saturate saturate);
95 
96     // Fill vertices with the vertex data needed to represent the given quad. The device position,
97     // local coords, vertex color, domain, and edge coefficients will be written and/or computed
98     // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
99     // then its corresponding function argument is ignored.
100     //
101     // Tessellation is based on the quad type of the vertex spec, not the provided GrQuad's
102     // so that all quads in a batch are tessellated the same.
103     //
104     // Returns the advanced pointer in vertices.
105     void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad,
106                      const SkPMColor4f& color, const GrQuad& localQuad, const SkRect& domain,
107                      GrQuadAAFlags aa);
108 
109     // The mesh will have its index data configured to meet the expectations of the Tessellate()
110     // function, but it the calling code must handle filling a vertex buffer via Tessellate() and
111     // then assigning it to the returned mesh.
112     //
113     // Returns false if the index data could not be allocated.
114     bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec,
115                               int quadCount);
116 
117     static constexpr int kNumAAQuadsInIndexBuffer = 512;
118 
119 } // namespace GrQuadPerEdgeAA
120 
121 #endif // GrQuadPerEdgeAA_DEFINED
122