bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [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 GrDrawPathBatch_DEFINED |
| 9 | #define GrDrawPathBatch_DEFINED |
| 10 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 11 | #include "GrBatchFlushState.h" |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 12 | #include "GrDrawBatch.h" |
| 13 | #include "GrGpu.h" |
| 14 | #include "GrPath.h" |
| 15 | #include "GrPathRendering.h" |
| 16 | #include "GrPathProcessor.h" |
| 17 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 18 | #include "SkTLList.h" |
| 19 | |
| 20 | class GrDrawPathBatchBase : public GrDrawBatch { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 21 | public: |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 22 | void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 23 | out->setKnownFourComponents(fColor); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 27 | out->setKnownSingleComponent(0xff); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void setStencilSettings(const GrStencilSettings& stencil) { fStencilSettings = stencil; } |
| 31 | |
| 32 | protected: |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 33 | GrDrawPathBatchBase(const SkMatrix& viewMatrix, GrColor initialColor) |
| 34 | : fViewMatrix(viewMatrix) |
| 35 | , fColor(initialColor) {} |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 36 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 37 | const GrStencilSettings& stencilSettings() const { return fStencilSettings; } |
| 38 | const GrPipelineOptimizations& opts() const { return fOpts; } |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 39 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 40 | GrColor color() const { return fColor; } |
| 41 | |
| 42 | // TODO delete |
| 43 | const GrBatchTracker* tracker() const { return &fBatchTracker; } |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | void initBatchTracker(const GrPipelineOptimizations& opts) override { |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 47 | opts.getOverrideColorIfSet(&fColor); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 48 | fOpts = opts; |
| 49 | } |
| 50 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 51 | SkMatrix fViewMatrix; |
| 52 | GrColor fColor; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 53 | GrStencilSettings fStencilSettings; |
| 54 | GrPipelineOptimizations fOpts; |
| 55 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 56 | // TODO delete |
| 57 | GrBatchTracker fBatchTracker; |
| 58 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 59 | typedef GrDrawBatch INHERITED; |
| 60 | }; |
| 61 | |
| 62 | class GrDrawPathBatch final : public GrDrawPathBatchBase { |
| 63 | public: |
| 64 | // This can't return a more abstract type because we install the stencil settings late :( |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 65 | static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, GrColor color, |
| 66 | const GrPath* path) { |
| 67 | return new GrDrawPathBatch(viewMatrix, color, path); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | const char* name() const override { return "DrawPath"; } |
| 71 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 72 | SkString dumpInfo() const override; |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 73 | |
| 74 | private: |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 75 | GrDrawPathBatch(const SkMatrix& viewMatrix, GrColor color, const GrPath* path) |
| 76 | : INHERITED(viewMatrix, color) |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 77 | , fPath(path) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 78 | fBounds = path->getBounds(); |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 79 | viewMatrix.mapRect(&fBounds); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 80 | this->initClassID<GrDrawPathBatch>(); |
| 81 | } |
| 82 | |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 83 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; } |
| 84 | |
| 85 | void onPrepare(GrBatchFlushState*) override {} |
| 86 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 87 | void onDraw(GrBatchFlushState* state) override; |
| 88 | |
| 89 | GrPendingIOResource<const GrPath, kRead_GrIOType> fPath; |
| 90 | |
| 91 | typedef GrDrawPathBatchBase INHERITED; |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * This could be nested inside the batch class, but for now it must be declarable in a public |
| 96 | * header (GrDrawContext) |
| 97 | */ |
| 98 | class GrPathRangeDraw : public GrNonAtomicRef { |
| 99 | public: |
| 100 | typedef GrPathRendering::PathTransformType TransformType; |
| 101 | |
| 102 | static GrPathRangeDraw* Create(GrPathRange* range, TransformType transformType, |
| 103 | int reserveCnt) { |
| 104 | return SkNEW_ARGS(GrPathRangeDraw, (range, transformType, reserveCnt)); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 107 | void append(uint16_t index, float transform[]) { |
| 108 | fTransforms.push_back_n(GrPathRendering::PathTransformSize(fTransformType), transform); |
| 109 | fIndices.push_back(index); |
| 110 | } |
| 111 | |
| 112 | int count() const { return fIndices.count(); } |
| 113 | |
| 114 | TransformType transformType() const { return fTransformType; } |
| 115 | |
| 116 | const float* transforms() const { return fTransforms.begin(); } |
| 117 | |
| 118 | const uint16_t* indices() const { return fIndices.begin(); } |
| 119 | |
| 120 | const GrPathRange* range() const { return fPathRange.get(); } |
| 121 | |
| 122 | static bool CanMerge(const GrPathRangeDraw& a, const GrPathRangeDraw& b) { |
| 123 | return a.transformType() == b.transformType() && a.range() == b.range(); |
| 124 | } |
| 125 | |
| 126 | private: |
| 127 | GrPathRangeDraw(GrPathRange* range, TransformType transformType, int reserveCnt) |
bsalomon | a82bf95 | 2015-09-10 09:31:59 -0700 | [diff] [blame] | 128 | : fPathRange(range) |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 129 | , fTransformType(transformType) |
| 130 | , fIndices(reserveCnt) |
| 131 | , fTransforms(reserveCnt * GrPathRendering::PathTransformSize(transformType)) { |
| 132 | SkDEBUGCODE(fUsedInBatch = false;) |
| 133 | } |
| 134 | |
| 135 | // Reserve space for 64 paths where indices are 16 bit and transforms are translations. |
| 136 | static const int kIndexReserveCnt = 64; |
| 137 | static const int kTransformBufferReserveCnt = 2 * 64; |
| 138 | |
| 139 | GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange; |
| 140 | GrPathRendering::PathTransformType fTransformType; |
| 141 | SkSTArray<kIndexReserveCnt, uint16_t, true> fIndices; |
| 142 | SkSTArray<kTransformBufferReserveCnt, float, true> fTransforms; |
| 143 | |
| 144 | // To ensure we don't reuse these across batches. |
| 145 | #ifdef SK_DEBUG |
| 146 | bool fUsedInBatch; |
| 147 | friend class GrDrawPathRangeBatch; |
| 148 | #endif |
| 149 | |
| 150 | typedef GrNonAtomicRef INHERITED; |
| 151 | }; |
| 152 | |
| 153 | // Template this if we decide to support index types other than 16bit |
| 154 | class GrDrawPathRangeBatch final : public GrDrawPathBatchBase { |
| 155 | public: |
| 156 | // This can't return a more abstracet type because we install the stencil settings late :( |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 157 | static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, |
| 158 | GrColor color, GrPathRangeDraw* pathRangeDraw) { |
| 159 | return SkNEW_ARGS(GrDrawPathRangeBatch, (viewMatrix, localMatrix, color, pathRangeDraw)); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | ~GrDrawPathRangeBatch() override; |
| 163 | |
| 164 | const char* name() const override { return "DrawPathRange"; } |
| 165 | |
| 166 | SkString dumpInfo() const override; |
| 167 | |
| 168 | private: |
| 169 | inline bool isWinding() const; |
| 170 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 171 | GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, GrColor color, |
| 172 | GrPathRangeDraw* pathRangeDraw); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 173 | |
| 174 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override; |
| 175 | |
| 176 | void onPrepare(GrBatchFlushState*) override {} |
| 177 | |
| 178 | void onDraw(GrBatchFlushState* state) override; |
| 179 | |
| 180 | typedef SkTLList<GrPathRangeDraw*> DrawList; |
| 181 | DrawList fDraws; |
| 182 | int fTotalPathCount; |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame^] | 183 | SkMatrix fLocalMatrix; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 184 | |
| 185 | typedef GrDrawPathBatchBase INHERITED; |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #endif |