blob: f85e6fd13dab4c22474b9d2c6e4401a3b8318fea [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"
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 Salomon82c263f2016-12-15 09:54:06 -050021class GrDrawPathOpBase : public GrDrawOp {
bsalomonadd79ef2015-08-19 13:26:49 -070022public:
halcanary9d524f22016-03-29 09:03:52 -070023 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080024 GrInitInvariantOutput* coverage,
25 GrBatchToXPOverrides* overrides) const override {
26 color->setKnownFourComponents(fColor);
27 coverage->setKnownSingleComponent(0xff);
bsalomon1fcc01c2015-09-09 09:48:06 -070028 }
29
bsalomon1fcc01c2015-09-09 09:48:06 -070030protected:
Brian Salomon82c263f2016-12-15 09:54:06 -050031 GrDrawPathOpBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor,
32 GrPathRendering::FillType fill)
33 : INHERITED(classID), fViewMatrix(viewMatrix), fColor(initialColor), fFillType(fill) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070034
cdalton193d9cf2016-05-12 11:52:02 -070035 const GrStencilSettings& stencilPassSettings() const {
Brian Salomon82c263f2016-12-15 09:54:06 -050036 SkASSERT(!fStencilPassSettings.isDisabled()); // This shouldn't be called before onPrepare.
cdalton193d9cf2016-05-12 11:52:02 -070037 return fStencilPassSettings;
38 }
ethannicholasff210322015-11-24 12:10:10 -080039 const GrXPOverridesForBatch& overrides() const { return fOverrides; }
joshualittf2384692015-09-10 11:00:51 -070040 const SkMatrix& viewMatrix() const { return fViewMatrix; }
41 GrColor color() const { return fColor; }
cdalton193d9cf2016-05-12 11:52:02 -070042 GrPathRendering::FillType fillType() const { return fFillType; }
joshualittf2384692015-09-10 11:00:51 -070043
bsalomon1fcc01c2015-09-09 09:48:06 -070044private:
ethannicholasff210322015-11-24 12:10:10 -080045 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
46 overrides.getOverrideColorIfSet(&fColor);
47 fOverrides = overrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070048 }
49
Brian Salomon742e31d2016-12-07 17:06:19 -050050 void onPrepare(GrOpFlushState*) override; // Initializes fStencilPassSettings.
cdalton193d9cf2016-05-12 11:52:02 -070051
Brian Salomon82c263f2016-12-15 09:54:06 -050052 SkMatrix fViewMatrix;
53 GrColor fColor;
54 GrPathRendering::FillType fFillType;
55 GrStencilSettings fStencilPassSettings;
56 GrXPOverridesForBatch fOverrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070057
Brian Salomon9afd3712016-12-01 10:59:09 -050058 typedef GrDrawOp INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070059};
60
Brian Salomon82c263f2016-12-15 09:54:06 -050061class GrDrawPathOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070062public:
Brian Salomon25a88092016-12-01 09:36:50 -050063 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070064
Brian Salomon82c263f2016-12-15 09:54:06 -050065 static sk_sp<GrDrawOp> Make(const SkMatrix& viewMatrix, GrColor color, const GrPath* path) {
66 return sk_sp<GrDrawOp>(new GrDrawPathOp(viewMatrix, color, path));
bsalomonadd79ef2015-08-19 13:26:49 -070067 }
68
69 const char* name() const override { return "DrawPath"; }
70
bsalomon1fcc01c2015-09-09 09:48:06 -070071 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070072
73private:
Brian Salomon82c263f2016-12-15 09:54:06 -050074 GrDrawPathOp(const SkMatrix& viewMatrix, GrColor color, const GrPath* path)
75 : GrDrawPathOpBase(ClassID(), viewMatrix, color, path->getFillType()), fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070076 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070077 }
stephana1dc17212016-04-25 07:01:22 -070078
Brian Salomon25a88092016-12-01 09:36:50 -050079 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070080
Brian Salomon742e31d2016-12-07 17:06:19 -050081 void onDraw(GrOpFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -070082
83 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070084
Brian Salomon82c263f2016-12-15 09:54:06 -050085 typedef GrDrawPathOpBase INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070086};
87
bsalomon1fcc01c2015-09-09 09:48:06 -070088// Template this if we decide to support index types other than 16bit
Brian Salomon82c263f2016-12-15 09:54:06 -050089class GrDrawPathRangeOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070090public:
cdaltoncdd46822015-12-08 10:48:31 -080091 typedef GrPathRendering::PathTransformType TransformType;
92
Brian Salomon25a88092016-12-01 09:36:50 -050093 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070094
cdaltoncdd46822015-12-08 10:48:31 -080095 struct InstanceData : public SkNoncopyable {
96 public:
97 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
98 int transformSize = GrPathRendering::PathTransformSize(transformType);
99 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
100 Align32(reserveCnt * sizeof(uint16_t)) +
101 reserveCnt * transformSize * sizeof(float));
102 InstanceData* instanceData = (InstanceData*)ptr;
103 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
104 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
105 Align32(reserveCnt * sizeof(uint16_t))];
106 instanceData->fTransformType = transformType;
107 instanceData->fInstanceCount = 0;
108 instanceData->fRefCnt = 1;
Brian Salomon82c263f2016-12-15 09:54:06 -0500109 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt);
cdaltoncdd46822015-12-08 10:48:31 -0800110 return instanceData;
111 }
112
113 // Overload this method if we start using other transform types.
114 void append(uint16_t index, float x, float y) {
115 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
116 SkASSERT(fInstanceCount < fReserveCnt);
117 fIndices[fInstanceCount] = index;
118 fTransformValues[2 * fInstanceCount] = x;
119 fTransformValues[2 * fInstanceCount + 1] = y;
120 ++fInstanceCount;
121 }
122
123 TransformType transformType() const { return fTransformType; }
124 int count() const { return fInstanceCount; }
125
126 const uint16_t* indices() const { return fIndices; }
127 uint16_t* indices() { return fIndices; }
128
129 const float* transformValues() const { return fTransformValues; }
130 float* transformValues() { return fTransformValues; }
131
132 void ref() const { ++fRefCnt; }
133
134 void unref() const {
135 if (0 == --fRefCnt) {
136 sk_free(const_cast<InstanceData*>(this));
137 }
138 }
139
140 private:
141 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
142
143 InstanceData() {}
144 ~InstanceData() {}
145
Brian Salomon82c263f2016-12-15 09:54:06 -0500146 uint16_t* fIndices;
147 float* fTransformValues;
148 TransformType fTransformType;
149 int fInstanceCount;
150 mutable int fRefCnt;
cdaltoncdd46822015-12-08 10:48:31 -0800151 SkDEBUGCODE(int fReserveCnt;)
152 };
153
Brian Salomon82c263f2016-12-15 09:54:06 -0500154 static sk_sp<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
155 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
156 const InstanceData* instanceData, const SkRect& bounds) {
157 return sk_sp<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y, color, fill, range,
158 instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700159 }
160
bsalomon1fcc01c2015-09-09 09:48:06 -0700161 const char* name() const override { return "DrawPathRange"; }
162
163 SkString dumpInfo() const override;
164
165private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500166 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
167 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
168 const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800169
170 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700171
Brian Salomon25a88092016-12-01 09:36:50 -0500172 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700173
Brian Salomon742e31d2016-12-07 17:06:19 -0500174 void onDraw(GrOpFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700175
cdaltoncdd46822015-12-08 10:48:31 -0800176 struct Draw {
177 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
178 fInstanceData.reset(SkRef(instanceData));
179 fX = x;
180 fY = y;
181 }
182
Hal Canary144caf52016-11-07 17:57:18 -0500183 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500184 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800185 };
186
cdalton8585dd22015-10-08 08:04:09 -0700187 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800188 typedef SkTLList<Draw, 4> DrawList;
189
Brian Salomon82c263f2016-12-15 09:54:06 -0500190 PendingPathRange fPathRange;
191 DrawList fDraws;
192 int fTotalPathCount;
193 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700194
Brian Salomon82c263f2016-12-15 09:54:06 -0500195 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700196};
197
198#endif