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: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 22 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 23 | GrInitInvariantOutput* coverage, |
| 24 | GrBatchToXPOverrides* overrides) const override { |
| 25 | color->setKnownFourComponents(fColor); |
| 26 | coverage->setKnownSingleComponent(0xff); |
| 27 | overrides->fUsePLSDstRead = false; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 28 | } |
| 29 | |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 30 | GrPathRendering::FillType fillType() const { return fFillType; } |
| 31 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 32 | void setStencilSettings(const GrStencilSettings& stencil) { fStencilSettings = stencil; } |
| 33 | |
| 34 | protected: |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 35 | GrDrawPathBatchBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor, |
| 36 | GrPathRendering::FillType fill) |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 37 | : INHERITED(classID) |
| 38 | , fViewMatrix(viewMatrix) |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 39 | , fColor(initialColor) |
| 40 | , fFillType(fill) {} |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 41 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 42 | const GrStencilSettings& stencilSettings() const { return fStencilSettings; } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 43 | const GrXPOverridesForBatch& overrides() const { return fOverrides; } |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 44 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 45 | GrColor color() const { return fColor; } |
| 46 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 47 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 48 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 49 | overrides.getOverrideColorIfSet(&fColor); |
| 50 | fOverrides = overrides; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 51 | } |
| 52 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 53 | SkMatrix fViewMatrix; |
| 54 | GrColor fColor; |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 55 | GrPathRendering::FillType fFillType; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 56 | GrStencilSettings fStencilSettings; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 57 | GrXPOverridesForBatch fOverrides; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 58 | |
| 59 | typedef GrDrawBatch INHERITED; |
| 60 | }; |
| 61 | |
| 62 | class GrDrawPathBatch final : public GrDrawPathBatchBase { |
| 63 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 64 | DEFINE_BATCH_CLASS_ID |
| 65 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 66 | // 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] | 67 | static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, GrColor color, |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 68 | GrPathRendering::FillType fill, const GrPath* path) { |
| 69 | return new GrDrawPathBatch(viewMatrix, color, fill, path); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | const char* name() const override { return "DrawPath"; } |
| 73 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 74 | SkString dumpInfo() const override; |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 77 | GrDrawPathBatch(const SkMatrix& viewMatrix, GrColor color, GrPathRendering::FillType fill, |
| 78 | const GrPath* path) |
| 79 | : INHERITED(ClassID(), viewMatrix, color, fill) |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 80 | , fPath(path) { |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 81 | fBounds = path->getBounds(); |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 82 | viewMatrix.mapRect(&fBounds); |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 83 | } |
| 84 | |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 85 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; } |
| 86 | |
| 87 | void onPrepare(GrBatchFlushState*) override {} |
| 88 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 89 | void onDraw(GrBatchFlushState* state) override; |
| 90 | |
| 91 | GrPendingIOResource<const GrPath, kRead_GrIOType> fPath; |
| 92 | |
| 93 | typedef GrDrawPathBatchBase INHERITED; |
| 94 | }; |
| 95 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 96 | // Template this if we decide to support index types other than 16bit |
| 97 | class GrDrawPathRangeBatch final : public GrDrawPathBatchBase { |
| 98 | public: |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 99 | typedef GrPathRendering::PathTransformType TransformType; |
| 100 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 101 | DEFINE_BATCH_CLASS_ID |
| 102 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 103 | struct InstanceData : public SkNoncopyable { |
| 104 | public: |
| 105 | static InstanceData* Alloc(TransformType transformType, int reserveCnt) { |
| 106 | int transformSize = GrPathRendering::PathTransformSize(transformType); |
| 107 | uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) + |
| 108 | Align32(reserveCnt * sizeof(uint16_t)) + |
| 109 | reserveCnt * transformSize * sizeof(float)); |
| 110 | InstanceData* instanceData = (InstanceData*)ptr; |
| 111 | instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))]; |
| 112 | instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) + |
| 113 | Align32(reserveCnt * sizeof(uint16_t))]; |
| 114 | instanceData->fTransformType = transformType; |
| 115 | instanceData->fInstanceCount = 0; |
| 116 | instanceData->fRefCnt = 1; |
| 117 | SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt;) |
| 118 | return instanceData; |
| 119 | } |
| 120 | |
| 121 | // Overload this method if we start using other transform types. |
| 122 | void append(uint16_t index, float x, float y) { |
| 123 | SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType); |
| 124 | SkASSERT(fInstanceCount < fReserveCnt); |
| 125 | fIndices[fInstanceCount] = index; |
| 126 | fTransformValues[2 * fInstanceCount] = x; |
| 127 | fTransformValues[2 * fInstanceCount + 1] = y; |
| 128 | ++fInstanceCount; |
| 129 | } |
| 130 | |
| 131 | TransformType transformType() const { return fTransformType; } |
| 132 | int count() const { return fInstanceCount; } |
| 133 | |
| 134 | const uint16_t* indices() const { return fIndices; } |
| 135 | uint16_t* indices() { return fIndices; } |
| 136 | |
| 137 | const float* transformValues() const { return fTransformValues; } |
| 138 | float* transformValues() { return fTransformValues; } |
| 139 | |
| 140 | void ref() const { ++fRefCnt; } |
| 141 | |
| 142 | void unref() const { |
| 143 | if (0 == --fRefCnt) { |
| 144 | sk_free(const_cast<InstanceData*>(this)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; } |
| 150 | |
| 151 | InstanceData() {} |
| 152 | ~InstanceData() {} |
| 153 | |
| 154 | uint16_t* fIndices; |
| 155 | float* fTransformValues; |
| 156 | TransformType fTransformType; |
| 157 | int fInstanceCount; |
| 158 | mutable int fRefCnt; |
| 159 | SkDEBUGCODE(int fReserveCnt;) |
| 160 | }; |
| 161 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 162 | // This can't return a more abstract type because we install the stencil settings late :( |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 163 | static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, |
| 164 | SkScalar y, GrColor color, GrPathRendering::FillType fill, |
| 165 | GrPathRange* range, const InstanceData* instanceData, |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame] | 166 | const SkRect& bounds) { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 167 | return new GrDrawPathRangeBatch(viewMatrix, scale, x, y, color, fill, range, instanceData, |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 168 | bounds); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 169 | } |
| 170 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 171 | const char* name() const override { return "DrawPathRange"; } |
| 172 | |
| 173 | SkString dumpInfo() const override; |
| 174 | |
| 175 | private: |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 176 | GrDrawPathRangeBatch(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y, |
| 177 | GrColor color, GrPathRendering::FillType fill, GrPathRange* range, |
| 178 | const InstanceData* instanceData, const SkRect& bounds); |
| 179 | |
| 180 | TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); } |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 181 | |
| 182 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override; |
| 183 | |
| 184 | void onPrepare(GrBatchFlushState*) override {} |
| 185 | |
| 186 | void onDraw(GrBatchFlushState* state) override; |
| 187 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 188 | struct Draw { |
| 189 | void set(const InstanceData* instanceData, SkScalar x, SkScalar y) { |
| 190 | fInstanceData.reset(SkRef(instanceData)); |
| 191 | fX = x; |
| 192 | fY = y; |
| 193 | } |
| 194 | |
| 195 | SkAutoTUnref<const InstanceData> fInstanceData; |
| 196 | SkScalar fX, fY; |
| 197 | }; |
| 198 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 199 | typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 200 | typedef SkTLList<Draw, 4> DrawList; |
| 201 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 202 | PendingPathRange fPathRange; |
| 203 | DrawList fDraws; |
| 204 | int fTotalPathCount; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame^] | 205 | SkScalar fScale; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 206 | |
| 207 | typedef GrDrawPathBatchBase INHERITED; |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | #endif |