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 20 class GrCaps; 21 class GrColorSpaceXform; 22 class GrShaderCaps; 23 24 namespace GrQuadPerEdgeAA { 25 26 enum class Domain : bool { kNo = false, kYes = true }; 27 enum class ColorType { kNone, kByte, kHalf, kLast = kHalf }; 28 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1; 29 30 // Gets the minimum ColorType that can represent a color. 31 ColorType MinColorType(SkPMColor4f, GrClampType, const GrCaps&); 32 33 // 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: VertexSpecVertexSpec39 VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType, 40 bool hasLocalCoords, Domain domain, GrAAType aa, bool coverageAsAlpha) 41 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType)) 42 , fLocalQuadType(static_cast<unsigned>(localQuadType)) 43 , fHasLocalCoords(hasLocalCoords) 44 , fColorType(static_cast<unsigned>(colorType)) 45 , fHasDomain(static_cast<unsigned>(domain)) 46 , fUsesCoverageAA(aa == GrAAType::kCoverage) 47 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha) 48 , fRequiresGeometryDomain(aa == GrAAType::kCoverage && 49 deviceQuadType > GrQuad::Type::kRectilinear) { } 50 deviceQuadTypeVertexSpec51 GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); } localQuadTypeVertexSpec52 GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); } hasLocalCoordsVertexSpec53 bool hasLocalCoords() const { return fHasLocalCoords; } colorTypeVertexSpec54 ColorType colorType() const { return static_cast<ColorType>(fColorType); } hasVertexColorsVertexSpec55 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); } hasDomainVertexSpec56 bool hasDomain() const { return fHasDomain; } usesCoverageAAVertexSpec57 bool usesCoverageAA() const { return fUsesCoverageAA; } compatibleWithCoverageAsAlphaVertexSpec58 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } requiresGeometryDomainVertexSpec59 bool requiresGeometryDomain() const { return fRequiresGeometryDomain; } 60 // 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; 64 verticesPerQuadVertexSpec65 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; } 66 private: 67 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits"); 68 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits"); 69 70 unsigned fDeviceQuadType: 2; 71 unsigned fLocalQuadType: 2; 72 unsigned fHasLocalCoords: 1; 73 unsigned fColorType : 2; 74 unsigned fHasDomain: 1; 75 unsigned fUsesCoverageAA: 1; 76 unsigned fCompatibleWithCoverageAsAlpha: 1; 77 // 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; 80 }; 81 82 sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec); 83 84 sk_sp<GrGeometryProcessor> MakeTexturedProcessor( 85 const VertexSpec& spec, const GrShaderCaps& caps, GrTextureType textureType, 86 const GrSamplerState& samplerState, const GrSwizzle& swizzle, uint32_t extraSamplerKey, 87 sk_sp<GrColorSpaceXform> textureColorSpaceXform); 88 89 // 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 // 94 // Tessellation is based on the quad type of the vertex spec, not the provided GrQuad's 95 // so that all quads in a batch are tessellated the same. 96 // 97 // Returns the advanced pointer in vertices. 98 void* Tessellate(void* vertices, const VertexSpec& spec, const GrQuad& deviceQuad, 99 const SkPMColor4f& color, const GrQuad& localQuad, const SkRect& domain, 100 GrQuadAAFlags aa); 101 102 // 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 112 } // namespace GrQuadPerEdgeAA 113 114 #endif // GrQuadPerEdgeAA_DEFINED 115