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 GrMeshDrawOp_DEFINED 9 #define GrMeshDrawOp_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/private/gpu/ganesh/GrTypesPriv.h" 13 #include "src/gpu/ganesh/GrAppliedClip.h" 14 #include "src/gpu/ganesh/ops/GrDrawOp.h" 15 16 #include <cstddef> 17 #include <cstdint> 18 #include <utility> 19 20 class GrBuffer; 21 class GrCaps; 22 class GrDstProxyView; 23 class GrGeometryProcessor; 24 class GrMeshDrawTarget; 25 class GrOpFlushState; 26 class GrProgramInfo; 27 class GrRecordingContext; 28 class GrSurfaceProxy; 29 class GrSurfaceProxyView; 30 class SkArenaAlloc; 31 enum class GrXferBarrierFlags; 32 struct GrSimpleMesh; 33 34 /** 35 * Base class for mesh-drawing GrDrawOps. 36 */ 37 class GrMeshDrawOp : public GrDrawOp { 38 public: CanUpgradeAAOnMerge(GrAAType aa1,GrAAType aa2)39 static bool CanUpgradeAAOnMerge(GrAAType aa1, GrAAType aa2) { 40 return (aa1 == GrAAType::kNone && aa2 == GrAAType::kCoverage) || 41 (aa1 == GrAAType::kCoverage && aa2 == GrAAType::kNone); 42 } 43 44 protected: 45 GrMeshDrawOp(uint32_t classID); 46 createProgramInfo(const GrCaps * caps,SkArenaAlloc * arena,const GrSurfaceProxyView & writeView,bool usesMSAASurface,GrAppliedClip && appliedClip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)47 void createProgramInfo(const GrCaps* caps, 48 SkArenaAlloc* arena, 49 const GrSurfaceProxyView& writeView, 50 bool usesMSAASurface, 51 GrAppliedClip&& appliedClip, 52 const GrDstProxyView& dstProxyView, 53 GrXferBarrierFlags renderPassXferBarriers, 54 GrLoadOp colorLoadOp) { 55 this->onCreateProgramInfo(caps, arena, writeView, usesMSAASurface, std::move(appliedClip), 56 dstProxyView, renderPassXferBarriers, colorLoadOp); 57 } 58 59 void createProgramInfo(GrMeshDrawTarget*); 60 61 /** Helper for rendering repeating meshes using a patterned index buffer. This class creates the 62 space for the vertices and flushes the draws to the GrMeshDrawTarget. */ 63 class PatternHelper { 64 public: 65 PatternHelper(GrMeshDrawTarget*, GrPrimitiveType, size_t vertexStride, 66 sk_sp<const GrBuffer> indexBuffer, int verticesPerRepetition, 67 int indicesPerRepetition, int repeatCount, int maxRepetitions); 68 69 /** Called to issue draws to the GrMeshDrawTarget.*/ 70 void recordDraw(GrMeshDrawTarget*, const GrGeometryProcessor*) const; 71 void recordDraw(GrMeshDrawTarget*, const GrGeometryProcessor*, 72 const GrSurfaceProxy* const primProcProxies[]) const; 73 vertices()74 void* vertices() const { return fVertices; } mesh()75 GrSimpleMesh* mesh() { return fMesh; } 76 77 protected: 78 PatternHelper() = default; 79 void init(GrMeshDrawTarget*, GrPrimitiveType, size_t vertexStride, 80 sk_sp<const GrBuffer> indexBuffer, int verticesPerRepetition, 81 int indicesPerRepetition, int repeatCount, int maxRepetitions); 82 83 private: 84 void* fVertices = nullptr; 85 GrSimpleMesh* fMesh = nullptr; 86 GrPrimitiveType fPrimitiveType; 87 }; 88 89 /** A specialization of InstanceHelper for quad rendering. 90 * It only draws non-antialiased indexed quads. 91 */ 92 class QuadHelper : private PatternHelper { 93 public: 94 QuadHelper() = delete; 95 QuadHelper(GrMeshDrawTarget*, size_t vertexStride, int quadsToDraw); 96 97 using PatternHelper::mesh; 98 using PatternHelper::recordDraw; 99 using PatternHelper::vertices; 100 101 private: 102 using INHERITED = PatternHelper; 103 }; 104 105 static bool CombinedQuadCountWillOverflow(GrAAType aaType, 106 bool willBeUpgradedToAA, 107 int combinedQuadCount); 108 109 virtual void onPrePrepareDraws(GrRecordingContext*, 110 const GrSurfaceProxyView& writeView, 111 GrAppliedClip*, 112 const GrDstProxyView&, 113 GrXferBarrierFlags renderPassXferBarriers, 114 GrLoadOp colorLoadOp); 115 116 private: 117 virtual GrProgramInfo* programInfo() = 0; 118 // This method is responsible for creating all the programInfos required 119 // by this op. 120 virtual void onCreateProgramInfo(const GrCaps*, 121 SkArenaAlloc*, 122 const GrSurfaceProxyView& writeView, 123 bool usesMSAASurface, 124 GrAppliedClip&&, 125 const GrDstProxyView&, 126 GrXferBarrierFlags renderPassXferBarriers, 127 GrLoadOp colorLoadOp) = 0; 128 onPrePrepare(GrRecordingContext * context,const GrSurfaceProxyView & writeView,GrAppliedClip * clip,const GrDstProxyView & dstProxyView,GrXferBarrierFlags renderPassXferBarriers,GrLoadOp colorLoadOp)129 void onPrePrepare(GrRecordingContext* context, 130 const GrSurfaceProxyView& writeView, 131 GrAppliedClip* clip, 132 const GrDstProxyView& dstProxyView, 133 GrXferBarrierFlags renderPassXferBarriers, 134 GrLoadOp colorLoadOp) final { 135 this->onPrePrepareDraws(context, writeView, clip, dstProxyView, renderPassXferBarriers, 136 colorLoadOp); 137 } 138 void onPrepare(GrOpFlushState* state) final; 139 140 virtual void onPrepareDraws(GrMeshDrawTarget*) = 0; 141 using INHERITED = GrDrawOp; 142 }; 143 144 #endif 145