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