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