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