Home | History | Annotate | Download | only in ops
      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 "GrColor.h"
     12 #include "GrGeometryProcessor.h"
     13 #include "GrMeshDrawOp.h"
     14 #include "GrQuad.h"
     15 #include "GrSamplerState.h"
     16 #include "GrTypesPriv.h"
     17 #include "SkPoint.h"
     18 #include "SkPoint3.h"
     19 
     20 class GrColorSpaceXform;
     21 class GrShaderCaps;
     22 
     23 namespace GrQuadPerEdgeAA {
     24 
     25     enum class Domain : bool { kNo = false, kYes = true };
     26     enum class ColorType { kNone, kByte, kHalf, kLast = kHalf };
     27     static const int kColorTypeCount = static_cast<int>(ColorType::kLast) + 1;
     28 
     29     // Gets the minimum ColorType that can represent a color.
     30     ColorType MinColorType(SkPMColor4f);
     31 
     32     // 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:
     38         VertexSpec(GrQuadType deviceQuadType, ColorType colorType, GrQuadType localQuadType,
     39                    bool hasLocalCoords, Domain domain, GrAAType aa, bool alphaAsCoverage)
     40                 : fDeviceQuadType(static_cast<unsigned>(deviceQuadType))
     41                 , fLocalQuadType(static_cast<unsigned>(localQuadType))
     42                 , fHasLocalCoords(hasLocalCoords)
     43                 , fColorType(static_cast<unsigned>(colorType))
     44                 , fHasDomain(static_cast<unsigned>(domain))
     45                 , fUsesCoverageAA(aa == GrAAType::kCoverage)
     46                 , fCompatibleWithAlphaAsCoverage(alphaAsCoverage) { }
     47 
     48         GrQuadType deviceQuadType() const { return static_cast<GrQuadType>(fDeviceQuadType); }
     49         GrQuadType localQuadType() const { return static_cast<GrQuadType>(fLocalQuadType); }
     50         bool hasLocalCoords() const { return fHasLocalCoords; }
     51         ColorType colorType() const { return static_cast<ColorType>(fColorType); }
     52         bool hasVertexColors() const { return ColorType::kNone != this->colorType(); }
     53         bool hasDomain() const { return fHasDomain; }
     54         bool usesCoverageAA() const { return fUsesCoverageAA; }
     55         bool compatibleWithAlphaAsCoverage() const { return fCompatibleWithAlphaAsCoverage; }
     56 
     57         // 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;
     61 
     62         int verticesPerQuad() const { return fUsesCoverageAA ? 8 : 4; }
     63     private:
     64         static_assert(kGrQuadTypeCount <= 4, "GrQuadType doesn't fit in 2 bits");
     65         static_assert(kColorTypeCount <= 4, "Color doesn't fit in 2 bits");
     66 
     67         unsigned fDeviceQuadType: 2;
     68         unsigned fLocalQuadType: 2;
     69         unsigned fHasLocalCoords: 1;
     70         unsigned fColorType : 2;
     71         unsigned fHasDomain: 1;
     72         unsigned fUsesCoverageAA: 1;
     73         unsigned fCompatibleWithAlphaAsCoverage: 1;
     74     };
     75 
     76     sk_sp<GrGeometryProcessor> MakeProcessor(const VertexSpec& spec);
     77 
     78     sk_sp<GrGeometryProcessor> MakeTexturedProcessor(const VertexSpec& spec,
     79             const GrShaderCaps& caps, GrTextureType textureType, GrPixelConfig textureConfig,
     80             const GrSamplerState& samplerState, uint32_t extraSamplerKey,
     81             sk_sp<GrColorSpaceXform> textureColorSpaceXform);
     82 
     83     // 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.
     89     void* Tessellate(void* vertices, const VertexSpec& spec, const GrPerspQuad& deviceQuad,
     90                      const SkPMColor4f& color, const GrPerspQuad& localQuad, const SkRect& domain,
     91                      GrQuadAAFlags aa);
     92 
     93     // 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 
    103 } // namespace GrQuadPerEdgeAA
    104 
    105 #endif // GrQuadPerEdgeAA_DEFINED
    106