blob: 7133a1b8ca5089bba5991aec71a9e5675867cacf [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPoint.h"
12#include "include/core/SkPoint3.h"
13#include "include/gpu/GrSamplerState.h"
14#include "include/private/GrColor.h"
15#include "include/private/GrTypesPriv.h"
16#include "src/gpu/GrGeometryProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040017#include "src/gpu/geometry/GrQuad.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/ops/GrMeshDrawOp.h"
Michael Ludwig460eb5e2018-10-29 11:09:29 -040019
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:
Michael Ludwigde4c58c2019-06-04 09:12:59 -040039 VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type 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)
Michael Ludwigdcfbe322019-04-01 14:55:54 -040047 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha)
48 , fRequiresGeometryDomain(aa == GrAAType::kCoverage &&
Michael Ludwigde4c58c2019-06-04 09:12:59 -040049 deviceQuadType > GrQuad::Type::kRectilinear) { }
Michael Ludwig460eb5e2018-10-29 11:09:29 -040050
Michael Ludwigde4c58c2019-06-04 09:12:59 -040051 GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); }
52 GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); }
Michael Ludwigc182b942018-11-16 10:27:51 -050053 bool hasLocalCoords() const { return fHasLocalCoords; }
Brian Osman3d139a42018-11-19 10:42:10 -050054 ColorType colorType() const { return static_cast<ColorType>(fColorType); }
55 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
Michael Ludwigc182b942018-11-16 10:27:51 -050056 bool hasDomain() const { return fHasDomain; }
57 bool usesCoverageAA() const { return fUsesCoverageAA; }
Brian Osman605c6d52019-03-15 12:10:35 -040058 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; }
Michael Ludwigdcfbe322019-04-01 14:55:54 -040059 bool requiresGeometryDomain() const { return fRequiresGeometryDomain; }
Michael Ludwigc182b942018-11-16 10:27:51 -050060 // Will always be 2 or 3
61 int deviceDimensionality() const;
62 // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3
63 int localDimensionality() const;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040064
Michael Ludwig93aeba02018-12-21 09:50:31 -050065 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
Michael Ludwigc182b942018-11-16 10:27:51 -050066 private:
Michael Ludwigde4c58c2019-06-04 09:12:59 -040067 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits");
Brian Osman3d139a42018-11-19 10:42:10 -050068 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
Michael Ludwig460eb5e2018-10-29 11:09:29 -040069
Michael Ludwigc182b942018-11-16 10:27:51 -050070 unsigned fDeviceQuadType: 2;
71 unsigned fLocalQuadType: 2;
72 unsigned fHasLocalCoords: 1;
Brian Osman3d139a42018-11-19 10:42:10 -050073 unsigned fColorType : 2;
Michael Ludwigc182b942018-11-16 10:27:51 -050074 unsigned fHasDomain: 1;
75 unsigned fUsesCoverageAA: 1;
Brian Osman605c6d52019-03-15 12:10:35 -040076 unsigned fCompatibleWithCoverageAsAlpha: 1;
Michael Ludwigdcfbe322019-04-01 14:55:54 -040077 // The geometry domain serves to clip off pixels touched by quads with sharp corners that
78 // would otherwise exceed the miter limit for the AA-outset geometry.
79 unsigned fRequiresGeometryDomain: 1;
Michael Ludwig460eb5e2018-10-29 11:09:29 -040080 };
81
Michael Ludwig467994d2018-12-03 14:58:31 +000082 sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
Michael Ludwig20e909e2018-10-30 10:43:57 -040083
Michael Ludwig467994d2018-12-03 14:58:31 +000084 sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec,
85 const GrShaderCaps& caps, GrTextureType textureType, GrPixelConfig textureConfig,
Greg Daniel7a82edf2018-12-04 10:54:34 -050086 const GrSamplerState& samplerState, uint32_t extraSamplerKey,
87 sk_sp<GrColorSpaceXform> textureColorSpaceXform);
Michael Ludwig20e909e2018-10-30 10:43:57 -040088
Michael Ludwigc182b942018-11-16 10:27:51 -050089 // Fill vertices with the vertex data needed to represent the given quad. The device position,
90 // local coords, vertex color, domain, and edge coefficients will be written and/or computed
91 // based on the configuration in the vertex spec; if that attribute is disabled in the spec,
92 // then its corresponding function argument is ignored.
93 //
Michael Ludwigde4c58c2019-06-04 09:12:59 -040094 // Tessellation is based on the quad type of the vertex spec, not the provided GrQuad's
Michael Ludwig41f395d2019-05-23 13:59:45 -040095 // so that all quads in a batch are tessellated the same.
96 //
Michael Ludwigc182b942018-11-16 10:27:51 -050097 // Returns the advanced pointer in vertices.
Michael Ludwigde4c58c2019-06-04 09:12:59 -040098 void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad,
99 const SkPMColor4f& color, const GrQuad& localQuad, const SkRect& domain,
Michael Ludwigc182b942018-11-16 10:27:51 -0500100 GrQuadAAFlags aa);
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400101
Michael Ludwig93aeba02018-12-21 09:50:31 -0500102 // The mesh will have its index data configured to meet the expectations of the Tessellate()
103 // function, but it the calling code must handle filling a vertex buffer via Tessellate() and
104 // then assigning it to the returned mesh.
105 //
106 // Returns false if the index data could not be allocated.
107 bool ConfigureMeshIndices(GrMeshDrawOp::Target* target, GrMesh* mesh, const VertexSpec& spec,
108 int quadCount);
109
110 static constexpr int kNumAAQuadsInIndexBuffer = 512;
111
Michael Ludwigc182b942018-11-16 10:27:51 -0500112} // namespace GrQuadPerEdgeAA
Michael Ludwig460eb5e2018-10-29 11:09:29 -0400113
114#endif // GrQuadPerEdgeAA_DEFINED