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