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