GrQuadPerEdgeAA.h 5.5 KB

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