blob: 314253cb6952660958f3d27ebfad7d9dd5735d8b [file] [log] [blame]
Michael Ludwig460eb5e2018-10-29 11:09:29 -04001/*
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"
Michael Ludwig467994d2018-12-03 14:58:31 +000012#include "GrGeometryProcessor.h"
Michael Ludwig93aeba02018-12-21 09:50:31 -050013#include "GrMeshDrawOp.h"
Michael Ludwigc182b942018-11-16 10:27:51 -050014#include "GrQuad.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040015#include "GrSamplerState.h"
16#include "GrTypesPriv.h"
17#include "SkPoint.h"
18#include "SkPoint3.h"
19
Brian Osman8fa7ab42019-03-18 10:22:42 -040020class GrCaps;
Michael Ludwig467994d2018-12-03 14:58:31 +000021class GrColorSpaceXform;
22class GrShaderCaps;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040023
Michael Ludwigc182b942018-11-16 10:27:51 -050024namespace GrQuadPerEdgeAA {
25
Michael Ludwig460eb5e2018-10-29 11:09:29 -040026 enum class Domain : bool { kNo = false, kYes = true };
Brian Osman3d139a42018-11-19 10:42:10 -050027 enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
28 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040029
Brian Salomon1d835422019-03-13 16:11:44 -040030 // Gets the minimum ColorType that can represent a color.
Brian Osman8fa7ab42019-03-18 10:22:42 -040031 ColorType MinColorType(SkPMColor4f, GrClampType, const GrCaps&);
Brian Salomon1d835422019-03-13 16:11:44 -040032
Michael Ludwigc182b942018-11-16 10:27:51 -050033 // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
34 // order (when enabled) is device position, color, local position, domain, aa edge equations.
35 // This order matches the constructor argument order of VertexSpec and is the order that
36 // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored.
37 struct VertexSpec {
38 public:
Brian Osman3d139a42018-11-19 10:42:10 -050039 VertexSpec(GrQuadType deviceQuadType, ColorType colorType, GrQuadType localQuadType,
Brian Osman605c6d52019-03-15 12:10:35 -040040 bool hasLocalCoords, Domain domain, GrAAType aa, bool coverageAsAlpha)
Michael Ludwigc182b942018-11-16 10:27:51 -050041 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
42 , fLocalQuadType(static_cast<unsigned>(localQuadType))
43 , fHasLocalCoords(hasLocalCoords)
Brian Osman3d139a42018-11-19 10:42:10 -050044 , fColorType(static_cast<unsigned>(colorType))
Michael Ludwigc182b942018-11-16 10:27:51 -050045 , fHasDomain(static_cast<unsigned>(domain))
Michael Ludwig93aeba02018-12-21 09:50:31 -050046 , fUsesCoverageAA(aa == GrAAType::kCoverage)
Brian Osman605c6d52019-03-15 12:10:35 -040047 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha) { }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040048
Michael Ludwigc182b942018-11-16 10:27:51 -050049 GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); }
50 GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); }
51 bool hasLocalCoords() const { return fHasLocalCoords; }
Brian Osman3d139a42018-11-19 10:42:10 -050052 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
53 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
Michael Ludwigc182b942018-11-16 10:27:51 -050054 bool hasDomain() const { return fHasDomain; }
55 bool usesCoverageAA() const { return fUsesCoverageAA; }
Brian Osman605c6d52019-03-15 12:10:35 -040056 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040057
Michael Ludwigc182b942018-11-16 10:27:51 -050058 // Will always be 2 or 3
59 int deviceDimensionality() const;
60 // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
61 int localDimensionality() const;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040062
Michael Ludwig93aeba02018-12-21 09:50:31 -050063 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
Michael Ludwigc182b942018-11-16 10:27:51 -050064 private:
65 static_assert(kGrQuadTypeCount <= 4, "GrQuadType doesn't fit in 2 bits");
Brian Osman3d139a42018-11-19 10:42:10 -050066 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
Michael Ludwig460eb5e2018-10-29 11:09:29 -040067
Michael Ludwigc182b942018-11-16 10:27:51 -050068 unsigned fDeviceQuadType: 2;
69 unsigned fLocalQuadType: 2;
70 unsigned fHasLocalCoords: 1;
Brian Osman3d139a42018-11-19 10:42:10 -050071 unsigned fColorType : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -050072 unsigned fHasDomain: 1;
73 unsigned fUsesCoverageAA: 1;
Brian Osman605c6d52019-03-15 12:10:35 -040074 unsigned fCompatibleWithCoverageAsAlpha: 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040075 };
76
Michael Ludwig467994d2018-12-03 14:58:31 +000077 sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -040078
Michael Ludwig467994d2018-12-03 14:58:31 +000079 sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec,
80 const GrShaderCaps& caps, GrTextureType textureType, GrPixelConfig textureConfig,
Greg Daniel7a82edf2018-12-04 10:54:34 -050081 const GrSamplerState& samplerState, uint32_t extraSamplerKey,
82 sk_sp<GrColorSpaceXform> textureColorSpaceXform);
Michael Ludwig20e909e2018-10-30 10:43:57 -040083
Michael Ludwigc182b942018-11-16 10:27:51 -050084 // Fill vertices with the vertex data needed to represent the given quad. The device position,
85 // local coords, vertex color, domain, and edge coefficients will be written and/or computed
86 // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
87 // then its corresponding function argument is ignored.
88 //
89 // Returns the advanced pointer in vertices.
Michael Ludwigc182b942018-11-16 10:27:51 -050090 void* Tessellate(void* vertices, const VertexSpec& spec, const GrPerspQuad& deviceQuad,
Brian Osman3d139a42018-11-19 10:42:10 -050091 const SkPMColor4f& color, const GrPerspQuad& localQuad, const SkRect& domain,
Michael Ludwigc182b942018-11-16 10:27:51 -050092 GrQuadAAFlags aa);
Michael Ludwig460eb5e2018-10-29 11:09:29 -040093
Michael Ludwig93aeba02018-12-21 09:50:31 -050094 // The mesh will have its index data configured to meet the expectations of the Tessellate()
95 // function, but it the calling code must handle filling a vertex buffer via Tessellate() and
96 // then assigning it to the returned mesh.
97 //
98 // Returns false if the index data could not be allocated.
99 bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec,
100 int quadCount);
101
102 static constexpr int kNumAAQuadsInIndexBuffer = 512;
103
Michael Ludwigc182b942018-11-16 10:27:51 -0500104} // namespace GrQuadPerEdgeAA
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400105
106#endif // GrQuadPerEdgeAA_DEFINED