Home | History | Annotate | Download | only in ops
      1 /*
      2  * Copyright 2015 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 GrDrawVerticesOp_DEFINED
      9 #define GrDrawVerticesOp_DEFINED
     10 
     11 #include "GrColor.h"
     12 #include "GrMeshDrawOp.h"
     13 #include "GrRenderTargetContext.h"
     14 #include "GrSimpleMeshDrawOpHelper.h"
     15 #include "GrTypes.h"
     16 #include "SkMatrix.h"
     17 #include "SkRect.h"
     18 #include "SkTDArray.h"
     19 #include "SkVertices.h"
     20 
     21 class GrOpFlushState;
     22 class SkVertices;
     23 struct GrInitInvariantOutput;
     24 
     25 class GrDrawVerticesOp final : public GrMeshDrawOp {
     26 private:
     27     using Helper = GrSimpleMeshDrawOpHelper;
     28 
     29 public:
     30     DEFINE_OP_CLASS_ID
     31 
     32     /**
     33      * Draw a SkVertices. The GrPaint param's color is used if the vertices lack per-vertex color.
     34      * If the vertices lack local coords then the vertex positions are used as local coords. The
     35      * primitive type drawn is derived from the SkVertices object, unless overridePrimType is
     36      * specified. If gammaCorrect is true, the vertex colors will be linearized in the shader to get
     37      * correct rendering.
     38      */
     39     static std::unique_ptr<GrDrawOp> Make(GrPaint&&, sk_sp<SkVertices>, const SkMatrix& viewMatrix,
     40                                           GrAAType, bool gammaCorrect, sk_sp<GrColorSpaceXform>,
     41                                           GrPrimitiveType* overridePrimType = nullptr);
     42 
     43     GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, GrColor, sk_sp<SkVertices>,
     44                      GrPrimitiveType, GrAAType, bool gammaCorrect, sk_sp<GrColorSpaceXform>,
     45                      const SkMatrix& viewMatrix);
     46 
     47     const char* name() const override { return "DrawVerticesOp"; }
     48 
     49     SkString dumpInfo() const override;
     50 
     51     FixedFunctionFlags fixedFunctionFlags() const override;
     52 
     53     RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override;
     54 
     55 private:
     56     enum class ColorArrayType {
     57         kPremulGrColor,
     58         kSkColor,
     59     };
     60 
     61     void onPrepareDraws(Target*) const override;
     62 
     63     sk_sp<GrGeometryProcessor> makeGP(bool* hasColorAttribute, bool* hasLocalCoordAttribute) const;
     64 
     65     GrPrimitiveType primitiveType() const { return fPrimitiveType; }
     66     bool combinablePrimitive() const {
     67         return GrPrimitiveType::kTriangles == fPrimitiveType ||
     68                GrPrimitiveType::kLines == fPrimitiveType ||
     69                GrPrimitiveType::kPoints == fPrimitiveType;
     70     }
     71 
     72     bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
     73 
     74     struct Mesh {
     75         GrColor fColor;  // Used if this->hasPerVertexColors() is false.
     76         sk_sp<SkVertices> fVertices;
     77         SkMatrix fViewMatrix;
     78         bool fIgnoreTexCoords;
     79         bool fIgnoreColors;
     80 
     81         bool hasExplicitLocalCoords() const {
     82             return fVertices->hasTexCoords() && !fIgnoreTexCoords;
     83         }
     84 
     85         bool hasPerVertexColors() const {
     86             return fVertices->hasColors() && !fIgnoreColors;
     87         }
     88     };
     89 
     90     bool isIndexed() const {
     91         // Consistency enforced in onCombineIfPossible.
     92         return fMeshes[0].fVertices->hasIndices();
     93     }
     94 
     95     bool requiresPerVertexColors() const {
     96         return SkToBool(kRequiresPerVertexColors_Flag & fFlags);
     97     }
     98 
     99     bool anyMeshHasExplicitLocalCoords() const {
    100         return SkToBool(kAnyMeshHasExplicitLocalCoords & fFlags);
    101     }
    102 
    103     bool hasMultipleViewMatrices() const {
    104         return SkToBool(kHasMultipleViewMatrices_Flag & fFlags);
    105     }
    106 
    107     enum Flags {
    108         kRequiresPerVertexColors_Flag = 0x1,
    109         kAnyMeshHasExplicitLocalCoords = 0x2,
    110         kHasMultipleViewMatrices_Flag = 0x4
    111 
    112     };
    113 
    114     Helper fHelper;
    115     SkSTArray<1, Mesh, true> fMeshes;
    116     // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore
    117     // the SkVertices mode (though fPrimitiveType may have been inferred from it).
    118     GrPrimitiveType fPrimitiveType;
    119     uint32_t fFlags;
    120     int fVertexCount;
    121     int fIndexCount;
    122     ColorArrayType fColorArrayType;
    123     bool fLinearizeColors;
    124     sk_sp<GrColorSpaceXform> fColorSpaceXform;
    125 
    126     typedef GrMeshDrawOp INHERITED;
    127 };
    128 
    129 #endif
    130