blob: a165f4ab4e321789c416019a5d05c756f6406c87 [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"
bsalomon72e3ae42015-04-28 08:08:46 -070012#include "GrVertexBuffer.h"
joshualitt95964c62015-02-11 13:45:50 -080013
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 */
18class GrTestBatch : public GrBatch {
19public:
20 struct Geometry {
21 GrColor fColor;
22 };
23
mtklein36352bf2015-03-25 18:17:31 -070024 virtual const char* name() const override = 0;
joshualitt95964c62015-02-11 13:45:50 -080025
mtklein36352bf2015-03-25 18:17:31 -070026 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt95964c62015-02-11 13:45:50 -080027 // When this is called on a batch, there is only one geometry bundle
joshualitt88c23fc2015-05-13 14:18:07 -070028 out->setKnownFourComponents(this->geoData(0)->fColor);
joshualitt95964c62015-02-11 13:45:50 -080029 }
30
mtklein36352bf2015-03-25 18:17:31 -070031 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt95964c62015-02-11 13:45:50 -080032 out->setUnknownSingleComponent();
33 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt95964c62015-02-11 13:45:50 -080036 // Handle any color overrides
37 if (init.fColorIgnored) {
38 this->geoData(0)->fColor = GrColor_ILLEGAL;
39 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
40 this->geoData(0)->fColor = init.fOverrideColor;
41 }
42
43 // setup batch properties
44 fBatch.fColorIgnored = init.fColorIgnored;
45 fBatch.fColor = this->geoData(0)->fColor;
46 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
47 fBatch.fCoverageIgnored = init.fCoverageIgnored;
48 }
49
mtklein36352bf2015-03-25 18:17:31 -070050 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt95964c62015-02-11 13:45:50 -080051 batchTarget->initDraw(fGeometryProcessor, pipeline);
52
53 // TODO this is hacky, but the only way we have to initialize the GP is to use the
54 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
55 // everywhere we can remove this nastiness
56 GrPipelineInfo init;
57 init.fColorIgnored = fBatch.fColorIgnored;
58 init.fOverrideColor = GrColor_ILLEGAL;
59 init.fCoverageIgnored = fBatch.fCoverageIgnored;
60 init.fUsesLocalCoords = fBatch.fUsesLocalCoords;
61 fGeometryProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init);
62
63 this->onGenerateGeometry(batchTarget, pipeline);
64 }
65
66protected:
joshualitt99c7c072015-05-01 13:43:30 -070067 GrTestBatch(const GrGeometryProcessor* gp, const SkRect& bounds) {
joshualitt95964c62015-02-11 13:45:50 -080068 fGeometryProcessor.reset(SkRef(gp));
joshualitt99c7c072015-05-01 13:43:30 -070069
70 this->setBounds(bounds);
joshualitt95964c62015-02-11 13:45:50 -080071 }
72
73 const GrGeometryProcessor* geometryProcessor() const { return fGeometryProcessor; }
74
75private:
76 virtual Geometry* geoData(int index) = 0;
joshualitt88c23fc2015-05-13 14:18:07 -070077 virtual const Geometry* geoData(int index) const = 0;
joshualitt95964c62015-02-11 13:45:50 -080078
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
joshualitt95964c62015-02-11 13:45:50 -080092 SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
93 BatchTracker fBatch;
94};
95
96#endif