blob: b34f15e3bf0c7742e4682bd2b6c2d5d449116b6c [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 {
bsalomon1fcc01c2015-09-09 09:48:06 -070022protected:
Brian Salomon82c263f2016-12-15 09:54:06 -050023 GrDrawPathOpBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor,
24 GrPathRendering::FillType fill)
25 : INHERITED(classID), fViewMatrix(viewMatrix), fColor(initialColor), fFillType(fill) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070026
cdalton193d9cf2016-05-12 11:52:02 -070027 const GrStencilSettings& stencilPassSettings() const {
Brian Salomon82c263f2016-12-15 09:54:06 -050028 SkASSERT(!fStencilPassSettings.isDisabled()); // This shouldn't be called before onPrepare.
cdalton193d9cf2016-05-12 11:52:02 -070029 return fStencilPassSettings;
30 }
Brian Salomon92aee3d2016-12-21 09:20:25 -050031
32protected:
33 const GrPipelineOptimizations& optimizations() const { return fOptimizations; }
joshualittf2384692015-09-10 11:00:51 -070034 const SkMatrix& viewMatrix() const { return fViewMatrix; }
35 GrColor color() const { return fColor; }
cdalton193d9cf2016-05-12 11:52:02 -070036 GrPathRendering::FillType fillType() const { return fFillType; }
joshualittf2384692015-09-10 11:00:51 -070037
bsalomon1fcc01c2015-09-09 09:48:06 -070038private:
Brian Salomon92aee3d2016-12-21 09:20:25 -050039 void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override {
40 input->pipelineColorInput()->setKnownFourComponents(fColor);
41 input->pipelineCoverageInput()->setKnownSingleComponent(0xFF);
42 }
43
44 void applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) override {
45 optimizations.getOverrideColorIfSet(&fColor);
46 fOptimizations = optimizations;
bsalomon1fcc01c2015-09-09 09:48:06 -070047 }
48
Brian Salomon742e31d2016-12-07 17:06:19 -050049 void onPrepare(GrOpFlushState*) override; // Initializes fStencilPassSettings.
cdalton193d9cf2016-05-12 11:52:02 -070050
Brian Salomon82c263f2016-12-15 09:54:06 -050051 SkMatrix fViewMatrix;
52 GrColor fColor;
53 GrPathRendering::FillType fFillType;
54 GrStencilSettings fStencilPassSettings;
Brian Salomon92aee3d2016-12-21 09:20:25 -050055 GrPipelineOptimizations fOptimizations;
bsalomon1fcc01c2015-09-09 09:48:06 -070056
Brian Salomon9afd3712016-12-01 10:59:09 -050057 typedef GrDrawOp INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070058};
59
Brian Salomon82c263f2016-12-15 09:54:06 -050060class GrDrawPathOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070061public:
Brian Salomon25a88092016-12-01 09:36:50 -050062 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070063
Brian Salomonf8334782017-01-03 09:42:58 -050064 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, GrColor color,
65 const GrPath* path) {
66 return std::unique_ptr<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 Salomonbde42852016-12-21 11:37:49 -050081 void onExecute(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
Brian Salomonf8334782017-01-03 09:42:58 -050095 struct InstanceData : private ::SkNoncopyable {
cdaltoncdd46822015-12-08 10:48:31 -080096 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 Salomonf8334782017-01-03 09:42:58 -0500154 static std::unique_ptr<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
155 SkScalar y, GrColor color, GrPathRendering::FillType fill,
156 GrPathRange* range, const InstanceData* instanceData,
157 const SkRect& bounds) {
158 return std::unique_ptr<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y, color, fill,
159 range, instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700160 }
161
bsalomon1fcc01c2015-09-09 09:48:06 -0700162 const char* name() const override { return "DrawPathRange"; }
163
164 SkString dumpInfo() const override;
165
166private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500167 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
168 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
169 const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800170
171 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700172
Brian Salomon25a88092016-12-01 09:36:50 -0500173 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700174
Brian Salomonbde42852016-12-21 11:37:49 -0500175 void onExecute(GrOpFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700176
cdaltoncdd46822015-12-08 10:48:31 -0800177 struct Draw {
178 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
179 fInstanceData.reset(SkRef(instanceData));
180 fX = x;
181 fY = y;
182 }
183
Hal Canary144caf52016-11-07 17:57:18 -0500184 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500185 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800186 };
187
cdalton8585dd22015-10-08 08:04:09 -0700188 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800189 typedef SkTLList<Draw, 4> DrawList;
190
Brian Salomon82c263f2016-12-15 09:54:06 -0500191 PendingPathRange fPathRange;
192 DrawList fDraws;
193 int fTotalPathCount;
194 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700195
Brian Salomon82c263f2016-12-15 09:54:06 -0500196 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700197};
198
199#endif