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/GrVertexWriter.h" 18 #include "src/gpu/geometry/GrQuad.h" 19 #include "src/gpu/geometry/GrQuadUtils.h" 20 #include "src/gpu/ops/GrMeshDrawOp.h" 21 #include "src/gpu/ops/GrTextureOp.h" 22 23 class GrCaps; 24 class GrColorSpaceXform; 25 class GrShaderCaps; 26 struct GrVertexWriter; 27 28 namespace GrQuadPerEdgeAA { 29 using Saturate = GrTextureOp::Saturate; 30 31 enum class CoverageMode { kNone, kWithPosition, kWithColor }; 32 enum class Subset : bool { kNo = false, kYes = true }; 33 enum class ColorType { kNone, kByte, kFloat, kLast = kFloat }; 34 static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1; 35 36 enum class IndexBufferOption { 37 kPictureFramed, // geometrically AA'd -> 8 verts/quad + an index buffer 38 kIndexedRects, // non-AA'd but indexed -> 4 verts/quad + an index buffer 39 kTriStrips, // non-AA'd -> 4 verts/quad but no index buffer 40 kLast = kTriStrips 41 }; 42 static const int kIndexBufferOptionCount = static_cast<int>(IndexBufferOption::kLast) + 1; 43 44 IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads); 45 46 // Gets the minimum ColorType that can represent a color. 47 ColorType MinColorType(SkPMColor4f); 48 49 // Specifies the vertex configuration for an op that renders per-edge AA quads. The vertex 50 // order (when enabled) is device position, color, local position, subset, aa edge equations. 51 // This order matches the constructor argument order of VertexSpec and is the order that 52 // GPAttributes maintains. If hasLocalCoords is false, then the local quad type can be ignored. 53 struct VertexSpec { 54 public: VertexSpecVertexSpec55 VertexSpec() 56 : fDeviceQuadType(0) // kAxisAligned 57 , fLocalQuadType(0) // kAxisAligned 58 , fIndexBufferOption(0) // kPictureFramed 59 , fHasLocalCoords(false) 60 , fColorType(0) // kNone 61 , fHasSubset(false) 62 , fUsesCoverageAA(false) 63 , fCompatibleWithCoverageAsAlpha(false) 64 , fRequiresGeometrySubset(false) {} 65 VertexSpecVertexSpec66 VertexSpec(GrQuad::Type deviceQuadType, ColorType colorType, GrQuad::Type localQuadType, 67 bool hasLocalCoords, 68 Subset subset, GrAAType aa, bool coverageAsAlpha, 69 IndexBufferOption indexBufferOption) 70 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType)) 71 , fLocalQuadType(static_cast<unsigned>(localQuadType)) 72 , fIndexBufferOption(static_cast<unsigned>(indexBufferOption)) 73 , fHasLocalCoords(hasLocalCoords) 74 , fColorType(static_cast<unsigned>(colorType)) 75 , fHasSubset(static_cast<unsigned>(subset)) 76 , fUsesCoverageAA(aa == GrAAType::kCoverage) 77 , fCompatibleWithCoverageAsAlpha(coverageAsAlpha) 78 , fRequiresGeometrySubset(aa == GrAAType::kCoverage && 79 deviceQuadType > GrQuad::Type::kRectilinear) { } 80 deviceQuadTypeVertexSpec81 GrQuad::Type deviceQuadType() const { return static_cast<GrQuad::Type>(fDeviceQuadType); } localQuadTypeVertexSpec82 GrQuad::Type localQuadType() const { return static_cast<GrQuad::Type>(fLocalQuadType); } indexBufferOptionVertexSpec83 IndexBufferOption indexBufferOption() const { 84 return static_cast<IndexBufferOption>(fIndexBufferOption); 85 } hasLocalCoordsVertexSpec86 bool hasLocalCoords() const { return fHasLocalCoords; } colorTypeVertexSpec87 ColorType colorType() const { return static_cast<ColorType>(fColorType); } hasVertexColorsVertexSpec88 bool hasVertexColors() const { return ColorType::kNone != this->colorType(); } hasSubsetVertexSpec89 bool hasSubset() const { return fHasSubset; } usesCoverageAAVertexSpec90 bool usesCoverageAA() const { return fUsesCoverageAA; } compatibleWithCoverageAsAlphaVertexSpec91 bool compatibleWithCoverageAsAlpha() const { return fCompatibleWithCoverageAsAlpha; } requiresGeometrySubsetVertexSpec92 bool requiresGeometrySubset() const { return fRequiresGeometrySubset; } 93 // Will always be 2 or 3 94 int deviceDimensionality() const; 95 // Will always be 0 if hasLocalCoords is false, otherwise will be 2 or 3 96 int localDimensionality() const; 97 verticesPerQuadVertexSpec98 int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; } 99 100 CoverageMode coverageMode() const; 101 size_t vertexSize() const; 102 needsIndexBufferVertexSpec103 bool needsIndexBuffer() const { return this->indexBufferOption() != 104 IndexBufferOption::kTriStrips; } 105 primitiveTypeVertexSpec106 GrPrimitiveType primitiveType() const { 107 switch (this->indexBufferOption()) { 108 case IndexBufferOption::kPictureFramed: return GrPrimitiveType::kTriangles; 109 case IndexBufferOption::kIndexedRects: return GrPrimitiveType::kTriangles; 110 case IndexBufferOption::kTriStrips: return GrPrimitiveType::kTriangleStrip; 111 } 112 113 SkUNREACHABLE; 114 } 115 116 private: 117 static_assert(GrQuad::kTypeCount <= 4, "GrQuad::Type doesn't fit in 2 bits"); 118 static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits"); 119 static_assert(kIndexBufferOptionCount <= 4, "IndexBufferOption doesn't fit in 2 bits"); 120 121 unsigned fDeviceQuadType: 2; 122 unsigned fLocalQuadType: 2; 123 unsigned fIndexBufferOption: 2; 124 unsigned fHasLocalCoords: 1; 125 unsigned fColorType : 2; 126 unsigned fHasSubset : 1; 127 unsigned fUsesCoverageAA: 1; 128 unsigned fCompatibleWithCoverageAsAlpha: 1; 129 // The geometry subset serves to clip off pixels touched by quads with sharp corners that 130 // would otherwise exceed the miter limit for the AA-outset geometry. 131 unsigned fRequiresGeometrySubset : 1; 132 }; 133 134 // A Tessellator is responsible for processing a series of device+local GrQuads into a VBO, 135 // as specified by a VertexSpec. This vertex data can then be processed by a GP created with 136 // MakeProcessor and/or MakeTexturedProcessor. 137 class Tessellator { 138 public: 139 explicit Tessellator(const VertexSpec& spec, char* vertices); 140 141 // Calculates (as needed) inset and outset geometry for anti-aliasing, and appends all 142 // necessary position and vertex attributes required by this Tessellator's VertexSpec into 143 // the 'vertices' the Tessellator was called with. The insetting and outsetting may 144 // damage the provided GrQuads (as this is intended to work with GrQuadBuffer::Iter). 145 // 'localQuad' can be null if the VertexSpec does not use local coords. 146 void append(GrQuad* deviceQuad, GrQuad* localQuad, 147 const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags); 148 149 SkDEBUGCODE(char* vertices() const { return (char*) fVertexWriter.fPtr; }) 150 151 private: 152 // VertexSpec defines many unique ways to write vertex attributes, which can be handled 153 // generically by branching per-quad based on the VertexSpec. However, there are several 154 // specs that appear in the wild far more frequently, so they use explicit WriteQuadProcs 155 // that have no branches. 156 typedef void (*WriteQuadProc)(GrVertexWriter* vertices, const VertexSpec& spec, 157 const GrQuad* deviceQuad, const GrQuad* localQuad, 158 const float coverage[4], const SkPMColor4f& color, 159 const SkRect& geomSubset, const SkRect& texSubset); 160 static WriteQuadProc GetWriteQuadProc(const VertexSpec& spec); 161 162 GrQuadUtils::TessellationHelper fAAHelper; 163 VertexSpec fVertexSpec; 164 GrVertexWriter fVertexWriter; 165 WriteQuadProc fWriteProc; 166 }; 167 168 GrGeometryProcessor* MakeProcessor(SkArenaAlloc*, const VertexSpec&); 169 170 GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc*, 171 const VertexSpec&, 172 const GrShaderCaps&, 173 const GrBackendFormat&, 174 GrSamplerState, 175 const GrSwizzle&, 176 sk_sp<GrColorSpaceXform> textureColorSpaceXform, 177 Saturate); 178 179 // This method will return the correct index buffer for the specified indexBufferOption. 180 // It will, correctly, return nullptr if the indexBufferOption is kTriStrips. 181 sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawOp::Target*, IndexBufferOption); 182 183 // What is the maximum number of quads allowed for the specified indexBuffer option? 184 int QuadLimit(IndexBufferOption); 185 186 // This method will issue the draw call on the provided GrOpsRenderPass, as specified by the 187 // indexing method in vertexSpec. It is up to the calling code to allocate, fill in, and bind a 188 // vertex buffer, and to acquire and bind the correct index buffer (if needed) with 189 // GrPrimitiveRestart::kNo. 190 // 191 // @param runningQuadCount the number of quads already stored in 'vertexBuffer' and 192 // 'indexBuffer' e.g., different GrMeshes have already been placed in 193 // the buffers to allow dynamic state changes. 194 // @param quadCount the number of quads that will be drawn by the provided 'mesh'. 195 // A subsequent ConfigureMesh call would the use 196 // 'runningQuadCount' + 'quadCount' for its new 'runningQuadCount'. 197 void IssueDraw(const GrCaps&, GrOpsRenderPass*, const VertexSpec&, int runningQuadCount, 198 int quadCount, int maxVerts, int absVertBufferOffset); 199 200 } // namespace GrQuadPerEdgeAA 201 202 #endif // GrQuadPerEdgeAA_DEFINED 203