blob: 2df5f3482eecccaa5fad97918ba9ebce7a1d13aa [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);
Brian Salomon54d212e2017-03-21 14:22:38 -040027 FixedFunctionFlags fixedFunctionFlags() const override {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040028 if (GrAATypeIsHW(fAAType)) {
29 return FixedFunctionFlags::kUsesHWAA | FixedFunctionFlags::kUsesStencil;
30 }
31 return FixedFunctionFlags::kUsesStencil;
Brian Salomonc48af932017-03-16 19:51:42 +000032 }
Brian Salomonf86d37b2017-06-16 10:04:34 -040033 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
34 return this->doProcessorAnalysis(caps, clip).requiresDstTexture() ? RequiresDstTexture::kYes
35 : RequiresDstTexture::kNo;
Brian Salomon54d212e2017-03-21 14:22:38 -040036 }
37
Brian Salomon92aee3d2016-12-21 09:20:25 -050038protected:
joshualittf2384692015-09-10 11:00:51 -070039 const SkMatrix& viewMatrix() const { return fViewMatrix; }
Brian Salomon48d1b4c2017-04-08 07:38:53 -040040 GrColor color() const { return fInputColor; }
cdalton193d9cf2016-05-12 11:52:02 -070041 GrPathRendering::FillType fillType() const { return fFillType; }
Brian Salomon54d212e2017-03-21 14:22:38 -040042 const GrProcessorSet& processors() const { return fProcessorSet; }
Brian Salomon91326c32017-08-09 16:02:19 -040043 GrProcessorSet detachProcessors() { return std::move(fProcessorSet); }
Brian Salomon611572c2017-04-28 08:57:12 -040044 uint32_t pipelineSRGBFlags() const { return fPipelineSRGBFlags; }
Brian Salomon972b2f62017-07-31 12:37:02 -040045 inline GrPipeline::InitArgs pipelineInitArgs(const GrOpFlushState&);
Brian Salomona811b122017-03-30 08:21:32 -040046 const GrProcessorSet::Analysis& doProcessorAnalysis(const GrCaps& caps,
47 const GrAppliedClip* clip) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040048 bool isMixedSamples = GrAAType::kMixedSamples == fAAType;
49 fAnalysis = fProcessorSet.finalize(fInputColor, GrProcessorAnalysisCoverage::kNone, clip,
50 isMixedSamples, caps, &fInputColor);
Brian Salomon54d212e2017-03-21 14:22:38 -040051 return fAnalysis;
52 }
Brian Salomona811b122017-03-30 08:21:32 -040053 const GrProcessorSet::Analysis& processorAnalysis() const {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040054 SkASSERT(fAnalysis.isInitialized());
Brian Salomon54d212e2017-03-21 14:22:38 -040055 return fAnalysis;
56 }
joshualittf2384692015-09-10 11:00:51 -070057
bsalomon1fcc01c2015-09-09 09:48:06 -070058private:
Brian Salomon54d212e2017-03-21 14:22:38 -040059 void onPrepare(GrOpFlushState*) final {}
cdalton193d9cf2016-05-12 11:52:02 -070060
Brian Salomon82c263f2016-12-15 09:54:06 -050061 SkMatrix fViewMatrix;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040062 GrColor fInputColor;
Brian Salomona811b122017-03-30 08:21:32 -040063 GrProcessorSet::Analysis fAnalysis;
Brian Salomon82c263f2016-12-15 09:54:06 -050064 GrPathRendering::FillType fFillType;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040065 GrAAType fAAType;
Brian Salomon611572c2017-04-28 08:57:12 -040066 uint32_t fPipelineSRGBFlags;
67 GrProcessorSet fProcessorSet;
bsalomon1fcc01c2015-09-09 09:48:06 -070068
Brian Salomon9afd3712016-12-01 10:59:09 -050069 typedef GrDrawOp INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070070};
71
Brian Salomon82c263f2016-12-15 09:54:06 -050072class GrDrawPathOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070073public:
Brian Salomon25a88092016-12-01 09:36:50 -050074 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070075
Brian Salomon48d1b4c2017-04-08 07:38:53 -040076 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, GrPaint&& paint,
77 GrAAType aaType, GrPath* path) {
78 return std::unique_ptr<GrDrawOp>(
79 new GrDrawPathOp(viewMatrix, std::move(paint), aaType, path));
bsalomonadd79ef2015-08-19 13:26:49 -070080 }
81
82 const char* name() const override { return "DrawPath"; }
83
bsalomon1fcc01c2015-09-09 09:48:06 -070084 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070085
86private:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040087 GrDrawPathOp(const SkMatrix& viewMatrix, GrPaint&& paint, GrAAType aaType, const GrPath* path)
88 : GrDrawPathOpBase(ClassID(), viewMatrix, std::move(paint), path->getFillType(), aaType)
Brian Salomon54d212e2017-03-21 14:22:38 -040089 , fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070090 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070091 }
stephana1dc17212016-04-25 07:01:22 -070092
Brian Salomon25a88092016-12-01 09:36:50 -050093 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070094
Brian Salomon9e50f7b2017-03-06 12:02:34 -050095 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -070096
97 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070098
Brian Salomon82c263f2016-12-15 09:54:06 -050099 typedef GrDrawPathOpBase INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -0700100};
101
bsalomon1fcc01c2015-09-09 09:48:06 -0700102// Template this if we decide to support index types other than 16bit
Brian Salomon82c263f2016-12-15 09:54:06 -0500103class GrDrawPathRangeOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -0700104public:
cdaltoncdd46822015-12-08 10:48:31 -0800105 typedef GrPathRendering::PathTransformType TransformType;
106
Brian Salomon25a88092016-12-01 09:36:50 -0500107 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700108
Brian Salomonf8334782017-01-03 09:42:58 -0500109 struct InstanceData : private ::SkNoncopyable {
cdaltoncdd46822015-12-08 10:48:31 -0800110 public:
111 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
112 int transformSize = GrPathRendering::PathTransformSize(transformType);
113 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
114 Align32(reserveCnt * sizeof(uint16_t)) +
115 reserveCnt * transformSize * sizeof(float));
116 InstanceData* instanceData = (InstanceData*)ptr;
117 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
118 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
119 Align32(reserveCnt * sizeof(uint16_t))];
120 instanceData->fTransformType = transformType;
121 instanceData->fInstanceCount = 0;
122 instanceData->fRefCnt = 1;
Brian Salomon82c263f2016-12-15 09:54:06 -0500123 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt);
cdaltoncdd46822015-12-08 10:48:31 -0800124 return instanceData;
125 }
126
127 // Overload this method if we start using other transform types.
128 void append(uint16_t index, float x, float y) {
129 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
130 SkASSERT(fInstanceCount < fReserveCnt);
131 fIndices[fInstanceCount] = index;
132 fTransformValues[2 * fInstanceCount] = x;
133 fTransformValues[2 * fInstanceCount + 1] = y;
134 ++fInstanceCount;
135 }
136
137 TransformType transformType() const { return fTransformType; }
138 int count() const { return fInstanceCount; }
139
140 const uint16_t* indices() const { return fIndices; }
141 uint16_t* indices() { return fIndices; }
142
143 const float* transformValues() const { return fTransformValues; }
144 float* transformValues() { return fTransformValues; }
145
146 void ref() const { ++fRefCnt; }
147
148 void unref() const {
149 if (0 == --fRefCnt) {
150 sk_free(const_cast<InstanceData*>(this));
151 }
152 }
153
154 private:
155 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
156
157 InstanceData() {}
158 ~InstanceData() {}
159
Brian Salomon82c263f2016-12-15 09:54:06 -0500160 uint16_t* fIndices;
161 float* fTransformValues;
162 TransformType fTransformType;
163 int fInstanceCount;
164 mutable int fRefCnt;
cdaltoncdd46822015-12-08 10:48:31 -0800165 SkDEBUGCODE(int fReserveCnt;)
166 };
167
Brian Salomonf8334782017-01-03 09:42:58 -0500168 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
Brian Salomon54d212e2017-03-21 14:22:38 -0400169 SkScalar y, GrPaint&& paint,
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400170 GrPathRendering::FillType fill, GrAAType aaType,
Brian Salomonf8334782017-01-03 09:42:58 -0500171 GrPathRange* range, const InstanceData* instanceData,
172 const SkRect& bounds) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400173 return std::unique_ptr<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y,
174 std::move(paint), fill, aaType,
175 range, instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700176 }
177
bsalomon1fcc01c2015-09-09 09:48:06 -0700178 const char* name() const override { return "DrawPathRange"; }
179
180 SkString dumpInfo() const override;
181
182private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500183 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400184 GrPaint&& paint, GrPathRendering::FillType fill, GrAAType aaType,
185 GrPathRange* range, const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800186
187 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700188
Brian Salomon25a88092016-12-01 09:36:50 -0500189 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700190
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500191 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700192
cdaltoncdd46822015-12-08 10:48:31 -0800193 struct Draw {
194 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
195 fInstanceData.reset(SkRef(instanceData));
196 fX = x;
197 fY = y;
198 }
199
Hal Canary144caf52016-11-07 17:57:18 -0500200 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500201 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800202 };
203
cdalton8585dd22015-10-08 08:04:09 -0700204 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800205 typedef SkTLList<Draw, 4> DrawList;
206
Brian Salomon82c263f2016-12-15 09:54:06 -0500207 PendingPathRange fPathRange;
208 DrawList fDraws;
209 int fTotalPathCount;
210 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700211
Brian Salomon82c263f2016-12-15 09:54:06 -0500212 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700213};
214
215#endif