bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 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 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 8 | #ifndef GrMeshDrawOp_DEFINED |
| 9 | #define GrMeshDrawOp_DEFINED |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 10 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 11 | #include "GrDrawOp.h" |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 14 | #include "GrPendingProgramElement.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 15 | |
| 16 | #include "SkTLList.h" |
| 17 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 18 | class GrCaps; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 19 | class GrOpFlushState; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 22 | * Base class for mesh-drawing GrDrawOps. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 23 | */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 24 | class GrMeshDrawOp : public GrDrawOp { |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 25 | public: |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 26 | class Target; |
| 27 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 28 | protected: |
| 29 | GrMeshDrawOp(uint32_t classID); |
| 30 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 31 | /** 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 Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 34 | public: |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 35 | PatternHelper(GrPrimitiveType primitiveType) : fMesh(primitiveType) {} |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 36 | /** Returns the allocated storage for the vertices. The caller should populate the vertices |
| 37 | before calling recordDraws(). */ |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 38 | void* init(Target*, size_t vertexStride, const GrBuffer*, int verticesPerRepetition, |
| 39 | int indicesPerRepetition, int repeatCount); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 40 | |
| 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 Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 52 | class QuadHelper : private PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 53 | public: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 54 | QuadHelper() : INHERITED(GrPrimitiveType::kTriangles) {} |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 55 | /** 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 Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 60 | using PatternHelper::recordDraw; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 61 | |
| 62 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 63 | typedef PatternHelper INHERITED; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | private: |
| 67 | void onPrepare(GrOpFlushState* state) final; |
| 68 | void onExecute(GrOpFlushState* state) final; |
| 69 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 70 | virtual void onPrepareDraws(Target*) = 0; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 71 | |
| 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 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 93 | #endif |