Home | History | Annotate | Download | only in batches
      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 "GrBatchFlushState.h"
     12 #include "GrGeometryProcessor.h"
     13 #include "GrVertexBuffer.h"
     14 
     15 #include "batches/GrVertexBatch.h"
     16 
     17 /*
     18  * A simple batch only for testing purposes which actually doesn't batch at all, but can fit into
     19  * the batch pipeline and generate arbitrary geometry
     20  */
     21 class GrTestBatch : public GrVertexBatch {
     22 public:
     23     struct Geometry {
     24         GrColor fColor;
     25     };
     26 
     27     virtual const char* name() const override = 0;
     28 
     29     void computePipelineOptimizations(GrInitInvariantOutput* color,
     30                                       GrInitInvariantOutput* coverage,
     31                                       GrBatchToXPOverrides* overrides) const override {
     32         // When this is called on a batch, there is only one geometry bundle
     33         color->setKnownFourComponents(this->geoData(0)->fColor);
     34         coverage->setUnknownSingleComponent();
     35     }
     36 
     37     void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
     38         // Handle any color overrides
     39         if (!overrides.readsColor()) {
     40             this->geoData(0)->fColor = GrColor_ILLEGAL;
     41         }
     42         overrides.getOverrideColorIfSet(&this->geoData(0)->fColor);
     43 
     44         // setup batch properties
     45         fBatch.fColorIgnored = !overrides.readsColor();
     46         fBatch.fColor = this->geoData(0)->fColor;
     47         fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
     48         fBatch.fCoverageIgnored = !overrides.readsCoverage();
     49     }
     50 
     51 protected:
     52     GrTestBatch(uint32_t classID, const GrGeometryProcessor* gp, const SkRect& bounds)
     53         : INHERITED(classID) {
     54         fGeometryProcessor.reset(SkRef(gp));
     55 
     56         this->setBounds(bounds);
     57     }
     58 
     59     const GrGeometryProcessor* geometryProcessor() const { return fGeometryProcessor; }
     60 
     61 private:
     62     void onPrepareDraws(Target* target) const override {
     63         target->initDraw(fGeometryProcessor, this->pipeline());
     64         this->generateGeometry(target);
     65     }
     66 
     67     virtual Geometry* geoData(int index) = 0;
     68     virtual const Geometry* geoData(int index) const = 0;
     69 
     70     bool onCombineIfPossible(GrBatch* t, const GrCaps&) override {
     71         return false;
     72     }
     73 
     74     virtual void generateGeometry(Target*) const = 0;
     75 
     76     struct BatchTracker {
     77         GrColor fColor;
     78         bool fUsesLocalCoords;
     79         bool fColorIgnored;
     80         bool fCoverageIgnored;
     81     };
     82 
     83     SkAutoTUnref<const GrGeometryProcessor> fGeometryProcessor;
     84     BatchTracker fBatch;
     85 
     86     typedef GrVertexBatch INHERITED;
     87 };
     88 
     89 #endif
     90