blob: 7b1072eea4845c3dd46934a92511c13c0a99f02c [file] [log] [blame]
bsalomonadd79ef2015-08-19 13:26:49 -07001/*
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 Salomon82c263f2016-12-15 09:54:06 -05008#ifndef GrDrawPathOp_DEFINED
9#define GrDrawPathOp_DEFINED
bsalomonadd79ef2015-08-19 13:26:49 -070010
Brian Salomon9afd3712016-12-01 10:59:09 -050011#include "GrDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050012#include "GrOpFlushState.h"
bsalomonadd79ef2015-08-19 13:26:49 -070013#include "GrPath.h"
bsalomonadd79ef2015-08-19 13:26:49 -070014#include "GrPathProcessor.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050015#include "GrPathRendering.h"
Brian Salomon54d212e2017-03-21 14:22:38 -040016#include "GrProcessorSet.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070017#include "GrStencilSettings.h"
bsalomonadd79ef2015-08-19 13:26:49 -070018
bsalomon1fcc01c2015-09-09 09:48:06 -070019#include "SkTLList.h"
20
Brian Salomon54d212e2017-03-21 14:22:38 -040021class GrPaint;
22
Brian Salomon82c263f2016-12-15 09:54:06 -050023class GrDrawPathOpBase : public GrDrawOp {
bsalomon1fcc01c2015-09-09 09:48:06 -070024protected:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040025 GrDrawPathOpBase(uint32_t classID, const SkMatrix& viewMatrix, GrPaint&&,
26 GrPathRendering::FillType, GrAAType);
Robert Phillipsb493eeb2017-09-13 13:10:52 -040027
Brian Salomon54d212e2017-03-21 14:22:38 -040028 FixedFunctionFlags fixedFunctionFlags() const override {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040029 if (GrAATypeIsHW(fAAType)) {
30 return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil;
31 }
32 return FixedFunctionFlags::kUsesStencil;
Brian Salomonc48af932017-03-16 19:51:42 +000033 }
Brian Osman9a725dd2017-09-20 09:53:22 -040034 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
35 GrPixelConfigIsClamped dstIsClamped) override {
36 return this->doProcessorAnalysis(caps, clip, dstIsClamped).requiresDstTexture()
37 ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
Brian Salomon54d212e2017-03-21 14:22:38 -040038 }
39
Robert Phillipsf1748f52017-09-14 14:11:24 -040040 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040041 fProcessorSet.visitProxies(func);
42 }
43
Brian Salomon92aee3d2016-12-21 09:20:25 -050044protected:
joshualittf2384692015-09-10 11:00:51 -070045 const SkMatrix& viewMatrix() const { return fViewMatrix; }
Brian Salomon48d1b4c2017-04-08 07:38:53 -040046 GrColor color() const { return fInputColor; }
cdalton193d9cf2016-05-12 11:52:02 -070047 GrPathRendering::FillType fillType() const { return fFillType; }
Brian Salomon54d212e2017-03-21 14:22:38 -040048 const GrProcessorSet& processors() const { return fProcessorSet; }
Brian Salomon91326c32017-08-09 16:02:19 -040049 GrProcessorSet detachProcessors() { return std::move(fProcessorSet); }
Brian Salomon611572c2017-04-28 08:57:12 -040050 uint32_t pipelineSRGBFlags() const { return fPipelineSRGBFlags; }
Brian Salomon972b2f62017-07-31 12:37:02 -040051 inline GrPipeline::InitArgs pipelineInitArgs(const GrOpFlushState&);
Brian Salomona811b122017-03-30 08:21:32 -040052 const GrProcessorSet::Analysis& doProcessorAnalysis(const GrCaps& caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040053 const GrAppliedClip* clip,
54 GrPixelConfigIsClamped dstIsClamped) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040055 bool isMixedSamples = GrAAType::kMixedSamples == fAAType;
56 fAnalysis = fProcessorSet.finalize(fInputColor, GrProcessorAnalysisCoverage::kNone, clip,
Brian Osman9a725dd2017-09-20 09:53:22 -040057 isMixedSamples, caps, dstIsClamped, &fInputColor);
Brian Salomon54d212e2017-03-21 14:22:38 -040058 return fAnalysis;
59 }
Brian Salomona811b122017-03-30 08:21:32 -040060 const GrProcessorSet::Analysis& processorAnalysis() const {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040061 SkASSERT(fAnalysis.isInitialized());
Brian Salomon54d212e2017-03-21 14:22:38 -040062 return fAnalysis;
63 }
joshualittf2384692015-09-10 11:00:51 -070064
bsalomon1fcc01c2015-09-09 09:48:06 -070065private:
Brian Salomon54d212e2017-03-21 14:22:38 -040066 void onPrepare(GrOpFlushState*) final {}
cdalton193d9cf2016-05-12 11:52:02 -070067
Brian Salomon82c263f2016-12-15 09:54:06 -050068 SkMatrix fViewMatrix;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040069 GrColor fInputColor;
Brian Salomona811b122017-03-30 08:21:32 -040070 GrProcessorSet::Analysis fAnalysis;
Brian Salomon82c263f2016-12-15 09:54:06 -050071 GrPathRendering::FillType fFillType;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040072 GrAAType fAAType;
Brian Salomon611572c2017-04-28 08:57:12 -040073 uint32_t fPipelineSRGBFlags;
74 GrProcessorSet fProcessorSet;
bsalomon1fcc01c2015-09-09 09:48:06 -070075
Brian Salomon9afd3712016-12-01 10:59:09 -050076 typedef GrDrawOp INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070077};
78
Brian Salomon82c263f2016-12-15 09:54:06 -050079class GrDrawPathOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070080public:
Brian Salomon25a88092016-12-01 09:36:50 -050081 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070082
Brian Salomon48d1b4c2017-04-08 07:38:53 -040083 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, GrPaint&& paint,
84 GrAAType aaType, GrPath* path) {
85 return std::unique_ptr<GrDrawOp>(
86 new GrDrawPathOp(viewMatrix, std::move(paint), aaType, path));
bsalomonadd79ef2015-08-19 13:26:49 -070087 }
88
89 const char* name() const override { return "DrawPath"; }
90
bsalomon1fcc01c2015-09-09 09:48:06 -070091 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070092
93private:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040094 GrDrawPathOp(const SkMatrix& viewMatrix, GrPaint&& paint, GrAAType aaType, const GrPath* path)
95 : GrDrawPathOpBase(ClassID(), viewMatrix, std::move(paint), path->getFillType(), aaType)
Brian Salomon54d212e2017-03-21 14:22:38 -040096 , fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070097 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070098 }
stephana1dc17212016-04-25 07:01:22 -070099
Brian Salomon25a88092016-12-01 09:36:50 -0500100 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -0700101
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500102 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700103
104 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -0700105
Brian Salomon82c263f2016-12-15 09:54:06 -0500106 typedef GrDrawPathOpBase INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -0700107};
108
bsalomon1fcc01c2015-09-09 09:48:06 -0700109// Template this if we decide to support index types other than 16bit
Brian Salomon82c263f2016-12-15 09:54:06 -0500110class GrDrawPathRangeOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -0700111public:
cdaltoncdd46822015-12-08 10:48:31 -0800112 typedef GrPathRendering::PathTransformType TransformType;
113
Brian Salomon25a88092016-12-01 09:36:50 -0500114 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700115
Brian Salomonf8334782017-01-03 09:42:58 -0500116 struct InstanceData : private ::SkNoncopyable {
cdaltoncdd46822015-12-08 10:48:31 -0800117 public:
118 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
119 int transformSize = GrPathRendering::PathTransformSize(transformType);
120 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
121 Align32(reserveCnt * sizeof(uint16_t)) +
122 reserveCnt * transformSize * sizeof(float));
123 InstanceData* instanceData = (InstanceData*)ptr;
124 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
125 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
126 Align32(reserveCnt * sizeof(uint16_t))];
127 instanceData->fTransformType = transformType;
128 instanceData->fInstanceCount = 0;
129 instanceData->fRefCnt = 1;
Brian Salomon82c263f2016-12-15 09:54:06 -0500130 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt);
cdaltoncdd46822015-12-08 10:48:31 -0800131 return instanceData;
132 }
133
134 // Overload this method if we start using other transform types.
135 void append(uint16_t index, float x, float y) {
136 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
137 SkASSERT(fInstanceCount < fReserveCnt);
138 fIndices[fInstanceCount] = index;
139 fTransformValues[2 * fInstanceCount] = x;
140 fTransformValues[2 * fInstanceCount + 1] = y;
141 ++fInstanceCount;
142 }
143
144 TransformType transformType() const { return fTransformType; }
145 int count() const { return fInstanceCount; }
146
147 const uint16_t* indices() const { return fIndices; }
148 uint16_t* indices() { return fIndices; }
149
150 const float* transformValues() const { return fTransformValues; }
151 float* transformValues() { return fTransformValues; }
152
153 void ref() const { ++fRefCnt; }
154
155 void unref() const {
156 if (0 == --fRefCnt) {
157 sk_free(const_cast<InstanceData*>(this));
158 }
159 }
160
161 private:
162 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
163
164 InstanceData() {}
165 ~InstanceData() {}
166
Brian Salomon82c263f2016-12-15 09:54:06 -0500167 uint16_t* fIndices;
168 float* fTransformValues;
169 TransformType fTransformType;
170 int fInstanceCount;
171 mutable int fRefCnt;
cdaltoncdd46822015-12-08 10:48:31 -0800172 SkDEBUGCODE(int fReserveCnt;)
173 };
174
Brian Salomonf8334782017-01-03 09:42:58 -0500175 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
Brian Salomon54d212e2017-03-21 14:22:38 -0400176 SkScalar y, GrPaint&& paint,
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400177 GrPathRendering::FillType fill, GrAAType aaType,
Brian Salomonf8334782017-01-03 09:42:58 -0500178 GrPathRange* range, const InstanceData* instanceData,
179 const SkRect& bounds) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400180 return std::unique_ptr<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y,
181 std::move(paint), fill, aaType,
182 range, instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700183 }
184
bsalomon1fcc01c2015-09-09 09:48:06 -0700185 const char* name() const override { return "DrawPathRange"; }
186
187 SkString dumpInfo() const override;
188
189private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500190 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400191 GrPaint&& paint, GrPathRendering::FillType fill, GrAAType aaType,
192 GrPathRange* range, const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800193
194 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700195
Brian Salomon25a88092016-12-01 09:36:50 -0500196 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700197
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500198 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700199
cdaltoncdd46822015-12-08 10:48:31 -0800200 struct Draw {
201 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
202 fInstanceData.reset(SkRef(instanceData));
203 fX = x;
204 fY = y;
205 }
206
Hal Canary144caf52016-11-07 17:57:18 -0500207 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500208 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800209 };
210
cdalton8585dd22015-10-08 08:04:09 -0700211 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800212 typedef SkTLList<Draw, 4> DrawList;
213
Brian Salomon82c263f2016-12-15 09:54:06 -0500214 PendingPathRange fPathRange;
215 DrawList fDraws;
216 int fTotalPathCount;
217 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700218
Brian Salomon82c263f2016-12-15 09:54:06 -0500219 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700220};
221
222#endif