blob: 20e55c9d8a749df2368917d2ec02c41a3e7a5e89 [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 Salomon742e31d2016-12-07 17:06:19 -050018class GrOpFlushState;
bsalomon16b99132015-08-13 14:55:50 -070019
20/**
Brian Salomondad29232016-12-01 16:40:24 -050021 * Base class for mesh-drawing GrDrawOps.
bsalomon16b99132015-08-13 14:55:50 -070022 */
Brian Salomondad29232016-12-01 16:40:24 -050023class GrMeshDrawOp : public GrDrawOp {
bsalomon16b99132015-08-13 14:55:50 -070024public:
bsalomon75398562015-08-17 12:55:38 -070025 class Target;
26
Brian Salomondad29232016-12-01 16:40:24 -050027 GrMeshDrawOp(uint32_t classID);
bsalomon16b99132015-08-13 14:55:50 -070028
bsalomon16b99132015-08-13 14:55:50 -070029protected:
30 /** Helper for rendering instances using an instanced index index buffer. This class creates the
Brian Salomon53e4c3c2016-12-21 11:38:53 -050031 space for the vertices and flushes the draws to the GrMeshDrawOp::Target. */
Brian Salomondad29232016-12-01 16:40:24 -050032 class InstancedHelper {
33 public:
bsalomon16b99132015-08-13 14:55:50 -070034 InstancedHelper() {}
bsalomon342bfc22016-04-01 06:06:20 -070035 /** Returns the allocated storage for the vertices. The caller should populate the vertices
36 before calling recordDraws(). */
Brian Salomondad29232016-12-01 16:40:24 -050037 void* init(Target*, GrPrimitiveType, size_t vertexStride, const GrBuffer*,
38 int verticesPerInstance, int indicesPerInstance, int instancesToDraw);
bsalomon16b99132015-08-13 14:55:50 -070039
Brian Salomon53e4c3c2016-12-21 11:38:53 -050040 /** Call after init() to issue draws to the GrMeshDrawOp::Target.*/
bsalomon342bfc22016-04-01 06:06:20 -070041 void recordDraw(Target*, const GrGeometryProcessor*);
Brian Salomondad29232016-12-01 16:40:24 -050042
bsalomon16b99132015-08-13 14:55:50 -070043 private:
bsalomon342bfc22016-04-01 06:06:20 -070044 GrMesh fMesh;
bsalomon16b99132015-08-13 14:55:50 -070045 };
46
47 static const int kVerticesPerQuad = 4;
48 static const int kIndicesPerQuad = 6;
49
50 /** A specialization of InstanceHelper for quad rendering. */
51 class QuadHelper : private InstancedHelper {
52 public:
53 QuadHelper() : INHERITED() {}
halcanary96fcdcc2015-08-27 07:41:13 -070054 /** Finds the cached quad index buffer and reserves vertex space. Returns nullptr on failure
bsalomon342bfc22016-04-01 06:06:20 -070055 and on success a pointer to the vertex data that the caller should populate before
56 calling recordDraws(). */
57 void* init(Target*, size_t vertexStride, int quadsToDraw);
bsalomon16b99132015-08-13 14:55:50 -070058
bsalomon75398562015-08-17 12:55:38 -070059 using InstancedHelper::recordDraw;
Brian Salomondad29232016-12-01 16:40:24 -050060
bsalomon16b99132015-08-13 14:55:50 -070061 private:
62 typedef InstancedHelper INHERITED;
63 };
64
65private:
Brian Salomon742e31d2016-12-07 17:06:19 -050066 void onPrepare(GrOpFlushState* state) final;
67 void onDraw(GrOpFlushState* state, const SkRect& bounds) final;
bsalomon53469832015-08-18 09:20:09 -070068
joshualitt144c3c82015-11-30 12:30:13 -080069 virtual void onPrepareDraws(Target*) const = 0;
bsalomon75398562015-08-17 12:55:38 -070070
bsalomon342bfc22016-04-01 06:06:20 -070071 // A set of contiguous draws that share a draw token and primitive processor. The draws all use
Brian Salomon53e4c3c2016-12-21 11:38:53 -050072 // the op's pipeline. The meshes for the draw are stored in the fMeshes array and each
bsalomon342bfc22016-04-01 06:06:20 -070073 // Queued draw uses fMeshCnt meshes from the fMeshes array. The reason for coallescing meshes
74 // that share a primitive processor into a QueuedDraw is that it allows the Gpu object to setup
75 // the shared state once and then issue draws for each mesh.
76 struct QueuedDraw {
77 int fMeshCnt = 0;
78 GrPendingProgramElement<const GrGeometryProcessor> fGeometryProcessor;
bsalomon75398562015-08-17 12:55:38 -070079 };
80
Brian Salomon53e4c3c2016-12-21 11:38:53 -050081 // All draws in all the GrMeshDrawOps have implicit tokens based on the order they are enqueued
82 // globally across all ops. This is the offset of the first entry in fQueuedDraws.
bsalomon342bfc22016-04-01 06:06:20 -070083 // fQueuedDraws[i]'s token is fBaseDrawToken + i.
Brian Salomon9afd3712016-12-01 10:59:09 -050084 GrDrawOpUploadToken fBaseDrawToken;
bsalomon342bfc22016-04-01 06:06:20 -070085
Brian Salomondad29232016-12-01 16:40:24 -050086 SkSTArray<4, GrMesh> fMeshes;
bsalomon342bfc22016-04-01 06:06:20 -070087 SkSTArray<4, QueuedDraw, true> fQueuedDraws;
bsalomon75398562015-08-17 12:55:38 -070088
Brian Salomon9afd3712016-12-01 10:59:09 -050089 typedef GrDrawOp INHERITED;
bsalomon16b99132015-08-13 14:55:50 -070090};
91
92#endif