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 | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 22 | protected: |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 23 | GrDrawPathOpBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor, |
| 24 | GrPathRendering::FillType fill) |
| 25 | : INHERITED(classID), fViewMatrix(viewMatrix), fColor(initialColor), fFillType(fill) {} |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 26 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 27 | const GrStencilSettings& stencilPassSettings() const { |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 28 | SkASSERT(!fStencilPassSettings.isDisabled()); // This shouldn't be called before onPrepare. |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 29 | return fStencilPassSettings; |
| 30 | } |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 31 | |
| 32 | protected: |
| 33 | const GrPipelineOptimizations& optimizations() const { return fOptimizations; } |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 34 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 35 | GrColor color() const { return fColor; } |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 36 | GrPathRendering::FillType fillType() const { return fFillType; } |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 37 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 38 | private: |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 39 | void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override { |
| 40 | input->pipelineColorInput()->setKnownFourComponents(fColor); |
| 41 | input->pipelineCoverageInput()->setKnownSingleComponent(0xFF); |
| 42 | } |
| 43 | |
| 44 | void applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) override { |
| 45 | optimizations.getOverrideColorIfSet(&fColor); |
| 46 | fOptimizations = optimizations; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 49 | void onPrepare(GrOpFlushState*) override; // Initializes fStencilPassSettings. |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 50 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 51 | SkMatrix fViewMatrix; |
| 52 | GrColor fColor; |
| 53 | GrPathRendering::FillType fFillType; |
| 54 | GrStencilSettings fStencilPassSettings; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 55 | GrPipelineOptimizations fOptimizations; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 56 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 57 | typedef GrDrawOp INHERITED; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 60 | class GrDrawPathOp final : public GrDrawPathOpBase { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 61 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 62 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 63 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame^] | 64 | static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, GrColor color, |
| 65 | const GrPath* path) { |
| 66 | return std::unique_ptr<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 | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 81 | void onExecute(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 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame^] | 95 | struct InstanceData : private ::SkNoncopyable { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 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 | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame^] | 154 | static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, |
| 155 | SkScalar y, GrColor color, GrPathRendering::FillType fill, |
| 156 | GrPathRange* range, const InstanceData* instanceData, |
| 157 | const SkRect& bounds) { |
| 158 | return std::unique_ptr<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y, color, fill, |
| 159 | range, instanceData, bounds)); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 162 | const char* name() const override { return "DrawPathRange"; } |
| 163 | |
| 164 | SkString dumpInfo() const override; |
| 165 | |
| 166 | private: |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 167 | GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y, |
| 168 | GrColor color, GrPathRendering::FillType fill, GrPathRange* range, |
| 169 | const InstanceData* instanceData, const SkRect& bounds); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 170 | |
| 171 | TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); } |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 172 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 173 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 174 | |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 175 | void onExecute(GrOpFlushState* state, const SkRect& bounds) override; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 176 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 177 | struct Draw { |
| 178 | void set(const InstanceData* instanceData, SkScalar x, SkScalar y) { |
| 179 | fInstanceData.reset(SkRef(instanceData)); |
| 180 | fX = x; |
| 181 | fY = y; |
| 182 | } |
| 183 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 184 | sk_sp<const InstanceData> fInstanceData; |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 185 | SkScalar fX, fY; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 188 | typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 189 | typedef SkTLList<Draw, 4> DrawList; |
| 190 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 191 | PendingPathRange fPathRange; |
| 192 | DrawList fDraws; |
| 193 | int fTotalPathCount; |
| 194 | SkScalar fScale; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 195 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 196 | typedef GrDrawPathOpBase INHERITED; |
bsalomon | add79ef | 2015-08-19 13:26:49 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | #endif |