blob: a491f2603bbf8d66c33970bb95b0b61324f93189 [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
Michael Ludwig467994d2018-12-03 14:58:31 +000020class GrColorSpaceXform;
21class GrShaderCaps;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040022
Michael Ludwigc182b942018-11-16 10:27:51 -050023namespace GrQuadPerEdgeAA {
24
Michael Ludwig460eb5e2018-10-29 11:09:29 -040025 enum class Domain : bool { kNo = false, kYes = true };
Brian Osman3d139a42018-11-19 10:42:10 -050026 enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
27 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040028
Brian Salomon1d835422019-03-13 16:11:44 -040029 // Gets the minimum ColorType that can represent a color.
30 ColorType MinColorType(SkPMColor4f);
31
Michael Ludwigc182b942018-11-16 10:27:51 -050032 // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex
33 // order (when enabled) is device position, color, local position, domain, aa edge equations.
34 // This order matches the constructor argument order of VertexSpec and is the order that
35 // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored.
36 struct VertexSpec {
37 public:
Brian Osman3d139a42018-11-19 10:42:10 -050038 VertexSpec(GrQuadType deviceQuadType, ColorType colorType, GrQuadType localQuadType,
Michael Ludwig93aeba02018-12-21 09:50:31 -050039 bool hasLocalCoords, Domain domain, GrAAType aa, bool alphaAsCoverage)
Michael Ludwigc182b942018-11-16 10:27:51 -050040 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
41 , fLocalQuadType(static_cast<unsigned>(localQuadType))
42 , fHasLocalCoords(hasLocalCoords)
Brian Osman3d139a42018-11-19 10:42:10 -050043 , fColorType(static_cast<unsigned>(colorType))
Michael Ludwigc182b942018-11-16 10:27:51 -050044 , fHasDomain(static_cast<unsigned>(domain))
Michael Ludwig93aeba02018-12-21 09:50:31 -050045 , fUsesCoverageAA(aa == GrAAType::kCoverage)
46 , fCompatibleWithAlphaAsCoverage(alphaAsCoverage) { }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040047
Michael Ludwigc182b942018-11-16 10:27:51 -050048 GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); }
49 GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); }
50 bool hasLocalCoords() const { return fHasLocalCoords; }
Brian Osman3d139a42018-11-19 10:42:10 -050051 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
52 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
Michael Ludwigc182b942018-11-16 10:27:51 -050053 bool hasDomain() const { return fHasDomain; }
54 bool usesCoverageAA() const { return fUsesCoverageAA; }
Michael Ludwig93aeba02018-12-21 09:50:31 -050055 bool compatibleWithAlphaAsCoverage() const { return fCompatibleWithAlphaAsCoverage; }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040056
Michael Ludwigc182b942018-11-16 10:27:51 -050057 // Will always be 2 or 3
58 int deviceDimensionality() const;
59 // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
60 int localDimensionality() const;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040061
Michael Ludwig93aeba02018-12-21 09:50:31 -050062 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
Michael Ludwigc182b942018-11-16 10:27:51 -050063 private:
64 static_assert(kGrQuadTypeCount <= 4, "GrQuadType doesn't fit in 2 bits");
Brian Osman3d139a42018-11-19 10:42:10 -050065 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
Michael Ludwig460eb5e2018-10-29 11:09:29 -040066
Michael Ludwigc182b942018-11-16 10:27:51 -050067 unsigned fDeviceQuadType: 2;
68 unsigned fLocalQuadType: 2;
69 unsigned fHasLocalCoords: 1;
Brian Osman3d139a42018-11-19 10:42:10 -050070 unsigned fColorType : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -050071 unsigned fHasDomain: 1;
72 unsigned fUsesCoverageAA: 1;
Michael Ludwig93aeba02018-12-21 09:50:31 -050073 unsigned fCompatibleWithAlphaAsCoverage: 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040074 };
75
Michael Ludwig467994d2018-12-03 14:58:31 +000076 sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -040077
Michael Ludwig467994d2018-12-03 14:58:31 +000078 sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec,
79 const GrShaderCaps& caps, GrTextureType textureType, GrPixelConfig textureConfig,
Greg Daniel7a82edf2018-12-04 10:54:34 -050080 const GrSamplerState& samplerState, uint32_t extraSamplerKey,
81 sk_sp<GrColorSpaceXform> textureColorSpaceXform);
Michael Ludwig20e909e2018-10-30 10:43:57 -040082
Michael Ludwigc182b942018-11-16 10:27:51 -050083 // Fill vertices with the vertex data needed to represent the given quad. The device position,
84 // local coords, vertex color, domain, and edge coefficients will be written and/or computed
85 // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
86 // then its corresponding function argument is ignored.
87 //
88 // Returns the advanced pointer in vertices.
Michael Ludwigc182b942018-11-16 10:27:51 -050089 void* Tessellate(void* vertices, const VertexSpec& spec, const GrPerspQuad& deviceQuad,
Brian Osman3d139a42018-11-19 10:42:10 -050090 const SkPMColor4f& color, const GrPerspQuad& localQuad, const SkRect& domain,
Michael Ludwigc182b942018-11-16 10:27:51 -050091 GrQuadAAFlags aa);
Michael Ludwig460eb5e2018-10-29 11:09:29 -040092
Michael Ludwig93aeba02018-12-21 09:50:31 -050093 // The mesh will have its index data configured to meet the expectations of the Tessellate()
94 // function, but it the calling code must handle filling a vertex buffer via Tessellate() and
95 // then assigning it to the returned mesh.
96 //
97 // Returns false if the index data could not be allocated.
98 bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec,
99 int quadCount);
100
101 static constexpr int kNumAAQuadsInIndexBuffer = 512;
102
Michael Ludwigc182b942018-11-16 10:27:51 -0500103} // namespace GrQuadPerEdgeAA
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400104
105#endif // GrQuadPerEdgeAA_DEFINED