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