blob: cf3ce2f11ce605ce3dbddea67b18d97978b97dba [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"
bsalomonadd79ef2015-08-19 13:26:49 -070012#include "GrGpu.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050013#include "GrOpFlushState.h"
bsalomonadd79ef2015-08-19 13:26:49 -070014#include "GrPath.h"
bsalomonadd79ef2015-08-19 13:26:49 -070015#include "GrPathProcessor.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050016#include "GrPathRendering.h"
Brian Salomon54d212e2017-03-21 14:22:38 -040017#include "GrProcessorSet.h"
csmartdaltonc633abb2016-11-01 08:55:55 -070018#include "GrStencilSettings.h"
bsalomonadd79ef2015-08-19 13:26:49 -070019
bsalomon1fcc01c2015-09-09 09:48:06 -070020#include "SkTLList.h"
21
Brian Salomon54d212e2017-03-21 14:22:38 -040022class GrPaint;
23
Brian Salomon82c263f2016-12-15 09:54:06 -050024class GrDrawPathOpBase : public GrDrawOp {
bsalomon1fcc01c2015-09-09 09:48:06 -070025protected:
Brian Salomon54d212e2017-03-21 14:22:38 -040026 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 Salomonc48af932017-03-16 19:51:42 +000030 }
Brian Salomon54d212e2017-03-21 14:22:38 -040031 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 Salomon92aee3d2016-12-21 09:20:25 -050037
38protected:
joshualittf2384692015-09-10 11:00:51 -070039 const SkMatrix& viewMatrix() const { return fViewMatrix; }
Brian Salomon54d212e2017-03-21 14:22:38 -040040 GrColor color() const { return fAnalysis.inputColor(); }
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; }
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 }
joshualittf2384692015-09-10 11:00:51 -070055
bsalomon1fcc01c2015-09-09 09:48:06 -070056private:
Brian Salomon54d212e2017-03-21 14:22:38 -040057 void onPrepare(GrOpFlushState*) final {}
cdalton193d9cf2016-05-12 11:52:02 -070058
Brian Salomon82c263f2016-12-15 09:54:06 -050059 SkMatrix fViewMatrix;
Brian Salomon54d212e2017-03-21 14:22:38 -040060 GrProcessorSet fProcessorSet;
61 GrProcessorSet::FragmentProcessorAnalysis fAnalysis;
Brian Salomon82c263f2016-12-15 09:54:06 -050062 GrPathRendering::FillType fFillType;
Brian Salomon54d212e2017-03-21 14:22:38 -040063 GrAA fAA;
bsalomon1fcc01c2015-09-09 09:48:06 -070064
Brian Salomon9afd3712016-12-01 10:59:09 -050065 typedef GrDrawOp INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070066};
67
Brian Salomon82c263f2016-12-15 09:54:06 -050068class GrDrawPathOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070069public:
Brian Salomon25a88092016-12-01 09:36:50 -050070 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070071
Brian Salomon54d212e2017-03-21 14:22:38 -040072 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));
bsalomonadd79ef2015-08-19 13:26:49 -070075 }
76
77 const char* name() const override { return "DrawPath"; }
78
bsalomon1fcc01c2015-09-09 09:48:06 -070079 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070080
81private:
Brian Salomon54d212e2017-03-21 14:22:38 -040082 GrDrawPathOp(const SkMatrix& viewMatrix, GrPaint&& paint, GrAA aa, const GrPath* path)
83 : GrDrawPathOpBase(ClassID(), viewMatrix, std::move(paint), path->getFillType(), aa)
84 , fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070085 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070086 }
stephana1dc17212016-04-25 07:01:22 -070087
Brian Salomon25a88092016-12-01 09:36:50 -050088 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070089
Brian Salomon9e50f7b2017-03-06 12:02:34 -050090 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -070091
92 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070093
Brian Salomon82c263f2016-12-15 09:54:06 -050094 typedef GrDrawPathOpBase INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070095};
96
bsalomon1fcc01c2015-09-09 09:48:06 -070097// Template this if we decide to support index types other than 16bit
Brian Salomon82c263f2016-12-15 09:54:06 -050098class GrDrawPathRangeOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070099public:
cdaltoncdd46822015-12-08 10:48:31 -0800100 typedef GrPathRendering::PathTransformType TransformType;
101
Brian Salomon25a88092016-12-01 09:36:50 -0500102 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700103
Brian Salomonf8334782017-01-03 09:42:58 -0500104 struct InstanceData : private ::SkNoncopyable {
cdaltoncdd46822015-12-08 10:48:31 -0800105 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 Salomon82c263f2016-12-15 09:54:06 -0500118 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt);
cdaltoncdd46822015-12-08 10:48:31 -0800119 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 Salomon82c263f2016-12-15 09:54:06 -0500155 uint16_t* fIndices;
156 float* fTransformValues;
157 TransformType fTransformType;
158 int fInstanceCount;
159 mutable int fRefCnt;
cdaltoncdd46822015-12-08 10:48:31 -0800160 SkDEBUGCODE(int fReserveCnt;)
161 };
162
Brian Salomonf8334782017-01-03 09:42:58 -0500163 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
Brian Salomon54d212e2017-03-21 14:22:38 -0400164 SkScalar y, GrPaint&& paint,
165 GrPathRendering::FillType fill, GrAA aa,
Brian Salomonf8334782017-01-03 09:42:58 -0500166 GrPathRange* range, const InstanceData* instanceData,
167 const SkRect& bounds) {
Brian Salomon54d212e2017-03-21 14:22:38 -0400168 return std::unique_ptr<GrDrawOp>(new GrDrawPathRangeOp(
169 viewMatrix, scale, x, y, std::move(paint), fill, aa, range, instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700170 }
171
bsalomon1fcc01c2015-09-09 09:48:06 -0700172 const char* name() const override { return "DrawPathRange"; }
173
174 SkString dumpInfo() const override;
175
176private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500177 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
Brian Salomon54d212e2017-03-21 14:22:38 -0400178 GrPaint&& paint, GrPathRendering::FillType fill, GrAA aa, GrPathRange* range,
Brian Salomon82c263f2016-12-15 09:54:06 -0500179 const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800180
181 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700182
Brian Salomon25a88092016-12-01 09:36:50 -0500183 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700184
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500185 void onExecute(GrOpFlushState* state) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700186
cdaltoncdd46822015-12-08 10:48:31 -0800187 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 Canary144caf52016-11-07 17:57:18 -0500194 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500195 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800196 };
197
cdalton8585dd22015-10-08 08:04:09 -0700198 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800199 typedef SkTLList<Draw, 4> DrawList;
200
Brian Salomon82c263f2016-12-15 09:54:06 -0500201 PendingPathRange fPathRange;
202 DrawList fDraws;
203 int fTotalPathCount;
204 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700205
Brian Salomon82c263f2016-12-15 09:54:06 -0500206 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700207};
208
209#endif