blob: 716912dc7687f88c35de9e9df2ed18c2708c7ef9 [file] [log] [blame]
joshualitt95964c62015-02-11 13:45:50 -08001/*
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 GrTestBatch_DEFINED
9#define GrTestBatch_DEFINED
10
11#include "GrBatch.h"
12
13/*
14 * A simple batch only for testing purposes which actually doesn't batch at all, but can fit into
15 * the batch pipeline and generate arbitrary geometry
16 */
17class GrTestBatch : public GrBatch {
18public:
19 struct Geometry {
20 GrColor fColor;
21 };
22
mtklein36352bf2015-03-25 18:17:31 -070023 virtual const char* name() const override = 0;
joshualitt95964c62015-02-11 13:45:50 -080024
mtklein36352bf2015-03-25 18:17:31 -070025 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt95964c62015-02-11 13:45:50 -080026 // When this is called on a batch, there is only one geometry bundle
27 if (fGeometryProcessor->hasVertexColor()) {
28 out->setUnknownFourComponents();
29 } else {
30 out->setKnownFourComponents(fGeometryProcessor->color());
31 }
32 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt95964c62015-02-11 13:45:50 -080035 out->setUnknownSingleComponent();
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt95964c62015-02-11 13:45:50 -080039 // Handle any color overrides
40 if (init.fColorIgnored) {
41 this->geoData(0)->fColor = GrColor_ILLEGAL;
42 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
43 this->geoData(0)->fColor = init.fOverrideColor;
44 }
45
46 // setup batch properties
47 fBatch.fColorIgnored = init.fColorIgnored;
48 fBatch.fColor = this->geoData(0)->fColor;
49 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
50 fBatch.fCoverageIgnored = init.fCoverageIgnored;
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt95964c62015-02-11 13:45:50 -080054 batchTarget->initDraw(fGeometryProcessor, pipeline);
55
56 // TODO this is hacky, but the only way we have to initialize the GP is to use the
57 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
58 // everywhere we can remove this nastiness
59 GrPipelineInfo init;
60 init.fColorIgnored = fBatch.fColorIgnored;
61 init.fOverrideColor = GrColor_ILLEGAL;
62 init.fCoverageIgnored = fBatch.fCoverageIgnored;
63 init.fUsesLocalCoords = fBatch.fUsesLocalCoords;
64 fGeometryProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init);
65
66 this->onGenerateGeometry(batchTarget, pipeline);
67 }
68
69protected:
70 GrTestBatch(const GrGeometryProcessor* gp) {
71 fGeometryProcessor.reset(SkRef(gp));
72 }
73
74 const GrGeometryProcessor* geometryProcessor() const { return fGeometryProcessor; }
75
76private:
77 virtual Geometry* geoData(int index) = 0;
78
mtklein36352bf2015-03-25 18:17:31 -070079 bool onCombineIfPossible(GrBatch* t) override {
joshualitt95964c62015-02-11 13:45:50 -080080 return false;
81 }
82
83 virtual void onGenerateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) = 0;
84
85 struct BatchTracker {
86 GrColor fColor;
87 bool fUsesLocalCoords;
88 bool fColorIgnored;
89 bool fCoverageIgnored;
90 };
91
92 SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
93 BatchTracker fBatch;
94};
95
96#endif