OLD | NEW |
---|---|
(Empty) | |
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 GrTBatchTesselator_DEFINED | |
9 #define GrTBatchTesselator_DEFINED | |
10 | |
11 #include "GrVertexBatch.h" | |
12 | |
13 #include "GrBatchFlushState.h" | |
14 | |
15 /* | |
bsalomon
2015/08/18 15:01:46
/** (I think the double * is needed for doxygen to
| |
16 * GrTBatchTesselator is an optional template to help with writing batches | |
17 * To use this template, an implementation must define the following static func tions: | |
bsalomon
2015/08/18 15:01:46
referring to Base?
Base is a confusing name... it
| |
18 * A Geometry struct | |
19 * | |
20 * const char* Name() | |
21 * | |
22 * bool CanCombine(const Geometry& mine, const Geometry& theirs, | |
23 * const GrPipelineOptimizations&) | |
24 * | |
25 * const GrGeometryProcessor* CreateGP(const Geometry& seedGeometry, | |
26 * const GrPipelineOptimizations& opts) | |
27 * | |
28 * const GrIndexBuffer* GetIndexBuffer(GrResourceProvider*) | |
29 * | |
30 * Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, | |
31 * const GrPipelineOptimizations& opts) | |
32 */ | |
33 template <typename Base, int kVertsPerInstance, int kIndicesPerInstance> | |
34 class GrTBatchTesselator : public GrVertexBatch { | |
35 public: | |
36 typedef typename Base::Geometry Geometry; | |
37 | |
38 static GrTBatchTesselator* Create() { | |
39 return SkNEW(GrTBatchTesselator); | |
40 } | |
41 | |
42 const char* name() const override { return Base::Name(); } | |
43 | |
44 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { | |
45 // When this is called on a batch, there is only one geometry bundle | |
46 out->setKnownFourComponents(fGeoData[0].fColor); | |
47 } | |
48 | |
49 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { | |
50 out->setUnknownSingleComponent(); | |
51 } | |
52 | |
53 void initBatchTracker(const GrPipelineOptimizations& opt) override { | |
54 opt.getOverrideColorIfSet(&fGeoData[0].fColor); | |
55 fOpts = opt; | |
56 } | |
57 | |
58 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | |
59 | |
60 // to avoid even the initial copy of the struct, we have a getter for the fi rst item which | |
61 // is used to seed the batch with its initial geometry. After seeding, the client should call | |
62 // init() so the Batch can initialize itself | |
63 Geometry* geometry() { return &fGeoData[0]; } | |
64 void init() { | |
65 const Geometry& geo = fGeoData[0]; | |
66 this->setBounds(geo.fDevRect); | |
67 } | |
68 | |
69 private: | |
70 GrTBatchTesselator() { | |
71 this->initClassID<GrTBatchTesselator<Base, kVertsPerInstance, kIndicesPe rInstance>>(); | |
72 | |
73 // Push back an initial geometry | |
74 fGeoData.push_back(); | |
75 } | |
76 | |
77 void onPrepareDraws(Target* target) override { | |
78 SkAutoTUnref<const GrGeometryProcessor> gp(Base::CreateGP(this->seedGeom etry(), fOpts)); | |
79 if (!gp) { | |
80 SkDebugf("Couldn't create GrGeometryProcessor\n"); | |
81 return; | |
82 } | |
83 | |
84 target->initDraw(gp, this->pipeline()); | |
85 | |
86 size_t vertexStride = gp->getVertexStride(); | |
87 int instanceCount = fGeoData.count(); | |
88 | |
89 SkAutoTUnref<const GrIndexBuffer> indexBuffer( | |
90 Base::GetIndexBuffer(target->resourceProvider())); | |
91 InstancedHelper helper; | |
92 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS tride, | |
93 indexBuffer, kVertsPerInstance, kIndicesPer Instance, | |
94 instanceCount); | |
95 if (!vertices || !indexBuffer) { | |
96 SkDebugf("Could not allocate vertices\n"); | |
97 return; | |
98 } | |
99 | |
100 for (int i = 0; i < instanceCount; i++) { | |
101 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + | |
102 i * kVertsPerInstance * vertexStride; | |
103 Base::Tesselate(verts, vertexStride, fGeoData[i], fOpts); | |
104 } | |
105 helper.recordDraw(target); | |
106 } | |
107 | |
108 const Geometry& seedGeometry() const { return fGeoData[0]; } | |
109 | |
110 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | |
111 GrTBatchTesselator* that = t->cast<GrTBatchTesselator>(); | |
112 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi peline(), | |
113 that->bounds(), caps)) { | |
114 return false; | |
115 } | |
116 | |
117 if (!Base::CanCombine(this->seedGeometry(), that->seedGeometry(), fOpts) ) { | |
118 return false; | |
119 } | |
120 | |
121 // In the event of two batches, one who can tweak, one who cannot, we ju st fall back to | |
122 // not tweaking | |
123 if (fOpts.canTweakAlphaForCoverage() && !that->fOpts.canTweakAlphaForCov erage()) { | |
124 fOpts = that->fOpts; | |
125 } | |
126 | |
127 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()) ; | |
128 this->joinBounds(that->bounds()); | |
129 return true; | |
130 } | |
131 | |
132 GrPipelineOptimizations fOpts; | |
133 SkSTArray<1, Geometry, true> fGeoData; | |
134 }; | |
135 | |
136 #endif | |
OLD | NEW |