blob: dcd8997f977ef20ebc1abdd798566e0c579c0188 [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 Salomon611572c2017-04-28 08:57:12 -040043 uint32_t pipelineSRGBFlags() const { return fPipelineSRGBFlags; }
Brian Salomon972b2f62017-07-31 12:37:02 -040044 inline GrPipeline::InitArgs pipelineInitArgs(const GrOpFlushState&);
Brian Salomona811b122017-03-30 08:21:32 -040045 const GrProcessorSet::Analysis& doProcessorAnalysis(const GrCaps& caps,
46 const GrAppliedClip* clip) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040047 bool isMixedSamples = GrAAType::kMixedSamples == fAAType;
48 fAnalysis = fProcessorSet.finalize(fInputColor, GrProcessorAnalysisCoverage::kNone, clip,
49 isMixedSamples, caps, &fInputColor);
Brian Salomon54d212e2017-03-21 14:22:38 -040050 return fAnalysis;
51 }
Brian Salomona811b122017-03-30 08:21:32 -040052 const GrProcessorSet::Analysis& processorAnalysis() const {
Brian Salomon48d1b4c2017-04-08 07:38:53 -040053 SkASSERT(fAnalysis.isInitialized());
Brian Salomon54d212e2017-03-21 14:22:38 -040054 return fAnalysis;
55 }
joshualittf2384692015-09-10 11:00:51 -070056
bsalomon1fcc01c2015-09-09 09:48:06 -070057private:
Brian Salomon54d212e2017-03-21 14:22:38 -040058 void onPrepare(GrOpFlushState*) final {}
cdalton193d9cf2016-05-12 11:52:02 -070059
Brian Salomon82c263f2016-12-15 09:54:06 -050060 SkMatrix fViewMatrix;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040061 GrColor fInputColor;
Brian Salomona811b122017-03-30 08:21:32 -040062 GrProcessorSet::Analysis fAnalysis;
Brian Salomon82c263f2016-12-15 09:54:06 -050063 GrPathRendering::FillType fFillType;
Brian Salomon48d1b4c2017-04-08 07:38:53 -040064 GrAAType fAAType;
Brian Salomon611572c2017-04-28 08:57:12 -040065 uint32_t fPipelineSRGBFlags;
66 GrProcessorSet fProcessorSet;
bsalomon1fcc01c2015-09-09 09:48:06 -070067
Brian Salomon9afd3712016-12-01 10:59:09 -050068 typedef GrDrawOp INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070069};
70
Brian Salomon82c263f2016-12-15 09:54:06 -050071class GrDrawPathOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070072public:
Brian Salomon25a88092016-12-01 09:36:50 -050073 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070074
Brian Salomon48d1b4c2017-04-08 07:38:53 -040075 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, GrPaint&& paint,
76 GrAAType aaType, GrPath* path) {
77 return std::unique_ptr<GrDrawOp>(
78 new GrDrawPathOp(viewMatrix, std::move(paint), aaType, path));
bsalomonadd79ef2015-08-19 13:26:49 -070079 }
80
81 const char* name() const override { return "DrawPath"; }
82
bsalomon1fcc01c2015-09-09 09:48:06 -070083 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070084
85private:
Brian Salomon48d1b4c2017-04-08 07:38:53 -040086 GrDrawPathOp(const SkMatrix& viewMatrix, GrPaint&& paint, GrAAType aaType, const GrPath* path)
87 : GrDrawPathOpBase(ClassID(), viewMatrix, std::move(paint), path->getFillType(), aaType)
Brian Salomon54d212e2017-03-21 14:22:38 -040088 , fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070089 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070090 }
stephana1dc17212016-04-25 07:01:22 -070091
Brian Salomon25a88092016-12-01 09:36:50 -050092 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070093
Brian Salomon9e50f7b2017-03-06 12:02:34 -050094 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -070095
96 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070097
Brian Salomon82c263f2016-12-15 09:54:06 -050098 typedef GrDrawPathOpBase INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070099};
100
bsalomon1fcc01c2015-09-09 09:48:06 -0700101// Template this if we decide to support index types other than 16bit
Brian Salomon82c263f2016-12-15 09:54:06 -0500102class GrDrawPathRangeOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -0700103public:
cdaltoncdd46822015-12-08 10:48:31 -0800104 typedef GrPathRendering::PathTransformType TransformType;
105
Brian Salomon25a88092016-12-01 09:36:50 -0500106 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700107
Brian Salomonf8334782017-01-03 09:42:58 -0500108 struct InstanceData : private ::SkNoncopyable {
cdaltoncdd46822015-12-08 10:48:31 -0800109 public:
110 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
111 int transformSize = GrPathRendering::PathTransformSize(transformType);
112 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
113 Align32(reserveCnt * sizeof(uint16_t)) +
114 reserveCnt * transformSize * sizeof(float));
115 InstanceData* instanceData = (InstanceData*)ptr;
116 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
117 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
118 Align32(reserveCnt * sizeof(uint16_t))];
119 instanceData->fTransformType = transformType;
120 instanceData->fInstanceCount = 0;
121 instanceData->fRefCnt = 1;
Brian Salomon82c263f2016-12-15 09:54:06 -0500122 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt);
cdaltoncdd46822015-12-08 10:48:31 -0800123 return instanceData;
124 }
125
126 // Overload this method if we start using other transform types.
127 void append(uint16_t index, float x, float y) {
128 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
129 SkASSERT(fInstanceCount < fReserveCnt);
130 fIndices[fInstanceCount] = index;
131 fTransformValues[2 * fInstanceCount] = x;
132 fTransformValues[2 * fInstanceCount + 1] = y;
133 ++fInstanceCount;
134 }
135
136 TransformType transformType() const { return fTransformType; }
137 int count() const { return fInstanceCount; }
138
139 const uint16_t* indices() const { return fIndices; }
140 uint16_t* indices() { return fIndices; }
141
142 const float* transformValues() const { return fTransformValues; }
143 float* transformValues() { return fTransformValues; }
144
145 void ref() const { ++fRefCnt; }
146
147 void unref() const {
148 if (0 == --fRefCnt) {
149 sk_free(const_cast<InstanceData*>(this));
150 }
151 }
152
153 private:
154 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
155
156 InstanceData() {}
157 ~InstanceData() {}
158
Brian Salomon82c263f2016-12-15 09:54:06 -0500159 uint16_t* fIndices;
160 float* fTransformValues;
161 TransformType fTransformType;
162 int fInstanceCount;
163 mutable int fRefCnt;
cdaltoncdd46822015-12-08 10:48:31 -0800164 SkDEBUGCODE(int fReserveCnt;)
165 };
166
Brian Salomonf8334782017-01-03 09:42:58 -0500167 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
Brian Salomon54d212e2017-03-21 14:22:38 -0400168 SkScalar y, GrPaint&& paint,
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400169 GrPathRendering::FillType fill, GrAAType aaType,
Brian Salomonf8334782017-01-03 09:42:58 -0500170 GrPathRange* range, const InstanceData* instanceData,
171 const SkRect& bounds) {
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400172 return std::unique_ptr<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y,
173 std::move(paint), fill, aaType,
174 range, instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700175 }
176
bsalomon1fcc01c2015-09-09 09:48:06 -0700177 const char* name() const override { return "DrawPathRange"; }
178
179 SkString dumpInfo() const override;
180
181private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500182 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
Brian Salomon48d1b4c2017-04-08 07:38:53 -0400183 GrPaint&& paint, GrPathRendering::FillType fill, GrAAType aaType,
184 GrPathRange* range, const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800185
186 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700187
Brian Salomon25a88092016-12-01 09:36:50 -0500188 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700189
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500190 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700191
cdaltoncdd46822015-12-08 10:48:31 -0800192 struct Draw {
193 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
194 fInstanceData.reset(SkRef(instanceData));
195 fX = x;
196 fY = y;
197 }
198
Hal Canary144caf52016-11-07 17:57:18 -0500199 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500200 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800201 };
202
cdalton8585dd22015-10-08 08:04:09 -0700203 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800204 typedef SkTLList<Draw, 4> DrawList;
205
Brian Salomon82c263f2016-12-15 09:54:06 -0500206 PendingPathRange fPathRange;
207 DrawList fDraws;
208 int fTotalPathCount;
209 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700210
Brian Salomon82c263f2016-12-15 09:54:06 -0500211 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700212};
213
214#endif