blob: dc09e0c765c5c0004a5d2d7a4be33072ad6e85e8 [file] [log] [blame]
bsalomon16b99132015-08-13 14:55:50 -07001/*
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
Brian Salomon53e4c3c2016-12-21 11:38:53 -05008#ifndef GrMeshDrawOp_DEFINED
9#define GrMeshDrawOp_DEFINED
bsalomon16b99132015-08-13 14:55:50 -070010
Brian Salomon9afd3712016-12-01 10:59:09 -050011#include "GrDrawOp.h"
bsalomon342bfc22016-04-01 06:06:20 -070012#include "GrGeometryProcessor.h"
egdaniel0e1853c2016-03-17 11:35:45 -070013#include "GrMesh.h"
bsalomon75398562015-08-17 12:55:38 -070014#include "GrPendingProgramElement.h"
bsalomon75398562015-08-17 12:55:38 -070015
16#include "SkTLList.h"
17
Brian Salomon54d212e2017-03-21 14:22:38 -040018class GrCaps;
Brian Salomon742e31d2016-12-07 17:06:19 -050019class GrOpFlushState;
bsalomon16b99132015-08-13 14:55:50 -070020
21/**
Brian Salomondad29232016-12-01 16:40:24 -050022 * Base class for mesh-drawing GrDrawOps.
bsalomon16b99132015-08-13 14:55:50 -070023 */
Brian Salomondad29232016-12-01 16:40:24 -050024class GrMeshDrawOp : public GrDrawOp {
bsalomon16b99132015-08-13 14:55:50 -070025public:
bsalomon75398562015-08-17 12:55:38 -070026 class Target;
27
Brian Salomond3ccb0a2017-04-03 10:38:00 -040028protected:
29 GrMeshDrawOp(uint32_t classID);
30
Chris Daltonff926502017-05-03 14:36:54 -040031 /** Helper for rendering repeating meshes using a patterned index buffer. This class creates the
32 space for the vertices and flushes the draws to the GrMeshDrawOp::Target. */
33 class PatternHelper {
Brian Salomond3ccb0a2017-04-03 10:38:00 -040034 public:
Chris Daltonbca46e22017-05-15 11:03:26 -060035 PatternHelper(GrPrimitiveType primitiveType) : fMesh(primitiveType) {}
Brian Salomond3ccb0a2017-04-03 10:38:00 -040036 /** Returns the allocated storage for the vertices. The caller should populate the vertices
37 before calling recordDraws(). */
Chris Daltonbca46e22017-05-15 11:03:26 -060038 void* init(Target*, size_t vertexStride, const GrBuffer*, int verticesPerRepetition,
39 int indicesPerRepetition, int repeatCount);
Brian Salomond3ccb0a2017-04-03 10:38:00 -040040
41 /** Call after init() to issue draws to the GrMeshDrawOp::Target.*/
42 void recordDraw(Target*, const GrGeometryProcessor*, const GrPipeline*);
43
44 private:
45 GrMesh fMesh;
46 };
47
48 static const int kVerticesPerQuad = 4;
49 static const int kIndicesPerQuad = 6;
50
51 /** A specialization of InstanceHelper for quad rendering. */
Chris Daltonff926502017-05-03 14:36:54 -040052 class QuadHelper : private PatternHelper {
Brian Salomond3ccb0a2017-04-03 10:38:00 -040053 public:
Chris Dalton3809bab2017-06-13 10:55:06 -060054 QuadHelper() : INHERITED(GrPrimitiveType::kTriangles) {}
Brian Salomond3ccb0a2017-04-03 10:38:00 -040055 /** Finds the cached quad index buffer and reserves vertex space. Returns nullptr on failure
56 and on success a pointer to the vertex data that the caller should populate before
57 calling recordDraws(). */
58 void* init(Target*, size_t vertexStride, int quadsToDraw);
59
Chris Daltonff926502017-05-03 14:36:54 -040060 using PatternHelper::recordDraw;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040061
62 private:
Chris Daltonff926502017-05-03 14:36:54 -040063 typedef PatternHelper INHERITED;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040064 };
65
66private:
67 void onPrepare(GrOpFlushState* state) final;
68 void onExecute(GrOpFlushState* state) final;
69
Brian Salomon91326c32017-08-09 16:02:19 -040070 virtual void onPrepareDraws(Target*) = 0;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040071
72 // A set of contiguous draws that share a draw token and primitive processor. The draws all use
73 // the op's pipeline. The meshes for the draw are stored in the fMeshes array and each
74 // Queued draw uses fMeshCnt meshes from the fMeshes array. The reason for coallescing meshes
75 // that share a primitive processor into a QueuedDraw is that it allows the Gpu object to setup
76 // the shared state once and then issue draws for each mesh.
77 struct QueuedDraw {
78 int fMeshCnt = 0;
79 GrPendingProgramElement<const GrGeometryProcessor> fGeometryProcessor;
80 const GrPipeline* fPipeline;
81 };
82
83 // All draws in all the GrMeshDrawOps have implicit tokens based on the order they are enqueued
84 // globally across all ops. This is the offset of the first entry in fQueuedDraws.
85 // fQueuedDraws[i]'s token is fBaseDrawToken + i.
86 GrDrawOpUploadToken fBaseDrawToken;
87 SkSTArray<4, GrMesh> fMeshes;
88 SkSTArray<4, QueuedDraw, true> fQueuedDraws;
89
90 typedef GrDrawOp INHERITED;
91};
92
bsalomon16b99132015-08-13 14:55:50 -070093#endif