blob: 5d6c35d0e9748a91fe5f5e1025062f553b056616 [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 Salomon82c263f2016-12-15 09:54:06 -050064 static sk_sp<GrDrawOp> Make(const SkMatrix& viewMatrix, GrColor color, const GrPath* path) {
65 return sk_sp<GrDrawOp>(new GrDrawPathOp(viewMatrix, color, path));
bsalomonadd79ef2015-08-19 13:26:49 -070066 }
67
68 const char* name() const override { return "DrawPath"; }
69
bsalomon1fcc01c2015-09-09 09:48:06 -070070 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070071
72private:
Brian Salomon82c263f2016-12-15 09:54:06 -050073 GrDrawPathOp(const SkMatrix& viewMatrix, GrColor color, const GrPath* path)
74 : GrDrawPathOpBase(ClassID(), viewMatrix, color, path->getFillType()), fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070075 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070076 }
stephana1dc17212016-04-25 07:01:22 -070077
Brian Salomon25a88092016-12-01 09:36:50 -050078 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070079
Brian Salomon742e31d2016-12-07 17:06:19 -050080 void onDraw(GrOpFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -070081
82 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070083
Brian Salomon82c263f2016-12-15 09:54:06 -050084 typedef GrDrawPathOpBase INHERITED;
bsalomon1fcc01c2015-09-09 09:48:06 -070085};
86
bsalomon1fcc01c2015-09-09 09:48:06 -070087// Template this if we decide to support index types other than 16bit
Brian Salomon82c263f2016-12-15 09:54:06 -050088class GrDrawPathRangeOp final : public GrDrawPathOpBase {
bsalomon1fcc01c2015-09-09 09:48:06 -070089public:
cdaltoncdd46822015-12-08 10:48:31 -080090 typedef GrPathRendering::PathTransformType TransformType;
91
Brian Salomon25a88092016-12-01 09:36:50 -050092 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070093
cdaltoncdd46822015-12-08 10:48:31 -080094 struct InstanceData : public SkNoncopyable {
95 public:
96 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
97 int transformSize = GrPathRendering::PathTransformSize(transformType);
98 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
99 Align32(reserveCnt * sizeof(uint16_t)) +
100 reserveCnt * transformSize * sizeof(float));
101 InstanceData* instanceData = (InstanceData*)ptr;
102 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
103 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
104 Align32(reserveCnt * sizeof(uint16_t))];
105 instanceData->fTransformType = transformType;
106 instanceData->fInstanceCount = 0;
107 instanceData->fRefCnt = 1;
Brian Salomon82c263f2016-12-15 09:54:06 -0500108 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt);
cdaltoncdd46822015-12-08 10:48:31 -0800109 return instanceData;
110 }
111
112 // Overload this method if we start using other transform types.
113 void append(uint16_t index, float x, float y) {
114 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
115 SkASSERT(fInstanceCount < fReserveCnt);
116 fIndices[fInstanceCount] = index;
117 fTransformValues[2 * fInstanceCount] = x;
118 fTransformValues[2 * fInstanceCount + 1] = y;
119 ++fInstanceCount;
120 }
121
122 TransformType transformType() const { return fTransformType; }
123 int count() const { return fInstanceCount; }
124
125 const uint16_t* indices() const { return fIndices; }
126 uint16_t* indices() { return fIndices; }
127
128 const float* transformValues() const { return fTransformValues; }
129 float* transformValues() { return fTransformValues; }
130
131 void ref() const { ++fRefCnt; }
132
133 void unref() const {
134 if (0 == --fRefCnt) {
135 sk_free(const_cast<InstanceData*>(this));
136 }
137 }
138
139 private:
140 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
141
142 InstanceData() {}
143 ~InstanceData() {}
144
Brian Salomon82c263f2016-12-15 09:54:06 -0500145 uint16_t* fIndices;
146 float* fTransformValues;
147 TransformType fTransformType;
148 int fInstanceCount;
149 mutable int fRefCnt;
cdaltoncdd46822015-12-08 10:48:31 -0800150 SkDEBUGCODE(int fReserveCnt;)
151 };
152
Brian Salomon82c263f2016-12-15 09:54:06 -0500153 static sk_sp<GrDrawOp> Make(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
154 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
155 const InstanceData* instanceData, const SkRect& bounds) {
156 return sk_sp<GrDrawOp>(new GrDrawPathRangeOp(viewMatrix, scale, x, y, color, fill, range,
157 instanceData, bounds));
bsalomon1fcc01c2015-09-09 09:48:06 -0700158 }
159
bsalomon1fcc01c2015-09-09 09:48:06 -0700160 const char* name() const override { return "DrawPathRange"; }
161
162 SkString dumpInfo() const override;
163
164private:
Brian Salomon82c263f2016-12-15 09:54:06 -0500165 GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
166 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
167 const InstanceData* instanceData, const SkRect& bounds);
cdaltoncdd46822015-12-08 10:48:31 -0800168
169 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700170
Brian Salomon25a88092016-12-01 09:36:50 -0500171 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700172
Brian Salomon742e31d2016-12-07 17:06:19 -0500173 void onDraw(GrOpFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700174
cdaltoncdd46822015-12-08 10:48:31 -0800175 struct Draw {
176 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
177 fInstanceData.reset(SkRef(instanceData));
178 fX = x;
179 fY = y;
180 }
181
Hal Canary144caf52016-11-07 17:57:18 -0500182 sk_sp<const InstanceData> fInstanceData;
Brian Salomon82c263f2016-12-15 09:54:06 -0500183 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800184 };
185
cdalton8585dd22015-10-08 08:04:09 -0700186 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800187 typedef SkTLList<Draw, 4> DrawList;
188
Brian Salomon82c263f2016-12-15 09:54:06 -0500189 PendingPathRange fPathRange;
190 DrawList fDraws;
191 int fTotalPathCount;
192 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700193
Brian Salomon82c263f2016-12-15 09:54:06 -0500194 typedef GrDrawPathOpBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700195};
196
197#endif