blob: 0b24fe0546d3e7f28ba49ba35fccdccb91ade018 [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
8#ifndef GrDrawPathBatch_DEFINED
9#define GrDrawPathBatch_DEFINED
10
bsalomon1fcc01c2015-09-09 09:48:06 -070011#include "GrBatchFlushState.h"
bsalomonadd79ef2015-08-19 13:26:49 -070012#include "GrDrawBatch.h"
13#include "GrGpu.h"
14#include "GrPath.h"
15#include "GrPathRendering.h"
16#include "GrPathProcessor.h"
17
bsalomon1fcc01c2015-09-09 09:48:06 -070018#include "SkTLList.h"
19
20class GrDrawPathBatchBase : public GrDrawBatch {
bsalomonadd79ef2015-08-19 13:26:49 -070021public:
ethannicholasff210322015-11-24 12:10:10 -080022 void computePipelineOptimizations(GrInitInvariantOutput* color,
23 GrInitInvariantOutput* coverage,
24 GrBatchToXPOverrides* overrides) const override {
25 color->setKnownFourComponents(fColor);
26 coverage->setKnownSingleComponent(0xff);
27 overrides->fUsePLSDstRead = false;
bsalomon1fcc01c2015-09-09 09:48:06 -070028 }
29
cdalton8ff8d242015-12-08 10:20:32 -080030 GrPathRendering::FillType fillType() const { return fFillType; }
31
bsalomon1fcc01c2015-09-09 09:48:06 -070032 void setStencilSettings(const GrStencilSettings& stencil) { fStencilSettings = stencil; }
33
34protected:
cdalton8ff8d242015-12-08 10:20:32 -080035 GrDrawPathBatchBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor,
36 GrPathRendering::FillType fill)
reed1b55a962015-09-17 20:16:13 -070037 : INHERITED(classID)
38 , fViewMatrix(viewMatrix)
cdalton8ff8d242015-12-08 10:20:32 -080039 , fColor(initialColor)
40 , fFillType(fill) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070041
bsalomon1fcc01c2015-09-09 09:48:06 -070042 const GrStencilSettings& stencilSettings() const { return fStencilSettings; }
ethannicholasff210322015-11-24 12:10:10 -080043 const GrXPOverridesForBatch& overrides() const { return fOverrides; }
joshualittf2384692015-09-10 11:00:51 -070044 const SkMatrix& viewMatrix() const { return fViewMatrix; }
45 GrColor color() const { return fColor; }
46
bsalomon1fcc01c2015-09-09 09:48:06 -070047private:
ethannicholasff210322015-11-24 12:10:10 -080048 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
49 overrides.getOverrideColorIfSet(&fColor);
50 fOverrides = overrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070051 }
52
joshualittf2384692015-09-10 11:00:51 -070053 SkMatrix fViewMatrix;
54 GrColor fColor;
cdalton8ff8d242015-12-08 10:20:32 -080055 GrPathRendering::FillType fFillType;
bsalomon1fcc01c2015-09-09 09:48:06 -070056 GrStencilSettings fStencilSettings;
ethannicholasff210322015-11-24 12:10:10 -080057 GrXPOverridesForBatch fOverrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070058
59 typedef GrDrawBatch INHERITED;
60};
61
62class GrDrawPathBatch final : public GrDrawPathBatchBase {
63public:
reed1b55a962015-09-17 20:16:13 -070064 DEFINE_BATCH_CLASS_ID
65
bsalomon1fcc01c2015-09-09 09:48:06 -070066 // This can't return a more abstract type because we install the stencil settings late :(
joshualittf2384692015-09-10 11:00:51 -070067 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, GrColor color,
cdalton8ff8d242015-12-08 10:20:32 -080068 GrPathRendering::FillType fill, const GrPath* path) {
69 return new GrDrawPathBatch(viewMatrix, color, fill, path);
bsalomonadd79ef2015-08-19 13:26:49 -070070 }
71
72 const char* name() const override { return "DrawPath"; }
73
bsalomon1fcc01c2015-09-09 09:48:06 -070074 SkString dumpInfo() const override;
bsalomonadd79ef2015-08-19 13:26:49 -070075
76private:
cdalton8ff8d242015-12-08 10:20:32 -080077 GrDrawPathBatch(const SkMatrix& viewMatrix, GrColor color, GrPathRendering::FillType fill,
78 const GrPath* path)
79 : INHERITED(ClassID(), viewMatrix, color, fill)
bsalomon1fcc01c2015-09-09 09:48:06 -070080 , fPath(path) {
bsalomonadd79ef2015-08-19 13:26:49 -070081 fBounds = path->getBounds();
joshualittf2384692015-09-10 11:00:51 -070082 viewMatrix.mapRect(&fBounds);
bsalomonadd79ef2015-08-19 13:26:49 -070083 }
84
bsalomonadd79ef2015-08-19 13:26:49 -070085 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; }
86
87 void onPrepare(GrBatchFlushState*) override {}
88
bsalomon1fcc01c2015-09-09 09:48:06 -070089 void onDraw(GrBatchFlushState* state) override;
90
91 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
92
93 typedef GrDrawPathBatchBase INHERITED;
94};
95
bsalomon1fcc01c2015-09-09 09:48:06 -070096// Template this if we decide to support index types other than 16bit
97class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
98public:
cdaltoncdd46822015-12-08 10:48:31 -080099 typedef GrPathRendering::PathTransformType TransformType;
100
reed1b55a962015-09-17 20:16:13 -0700101 DEFINE_BATCH_CLASS_ID
102
cdaltoncdd46822015-12-08 10:48:31 -0800103 struct InstanceData : public SkNoncopyable {
104 public:
105 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
106 int transformSize = GrPathRendering::PathTransformSize(transformType);
107 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
108 Align32(reserveCnt * sizeof(uint16_t)) +
109 reserveCnt * transformSize * sizeof(float));
110 InstanceData* instanceData = (InstanceData*)ptr;
111 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
112 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
113 Align32(reserveCnt * sizeof(uint16_t))];
114 instanceData->fTransformType = transformType;
115 instanceData->fInstanceCount = 0;
116 instanceData->fRefCnt = 1;
117 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt;)
118 return instanceData;
119 }
120
121 // Overload this method if we start using other transform types.
122 void append(uint16_t index, float x, float y) {
123 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
124 SkASSERT(fInstanceCount < fReserveCnt);
125 fIndices[fInstanceCount] = index;
126 fTransformValues[2 * fInstanceCount] = x;
127 fTransformValues[2 * fInstanceCount + 1] = y;
128 ++fInstanceCount;
129 }
130
131 TransformType transformType() const { return fTransformType; }
132 int count() const { return fInstanceCount; }
133
134 const uint16_t* indices() const { return fIndices; }
135 uint16_t* indices() { return fIndices; }
136
137 const float* transformValues() const { return fTransformValues; }
138 float* transformValues() { return fTransformValues; }
139
140 void ref() const { ++fRefCnt; }
141
142 void unref() const {
143 if (0 == --fRefCnt) {
144 sk_free(const_cast<InstanceData*>(this));
145 }
146 }
147
148 private:
149 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
150
151 InstanceData() {}
152 ~InstanceData() {}
153
154 uint16_t* fIndices;
155 float* fTransformValues;
156 TransformType fTransformType;
157 int fInstanceCount;
158 mutable int fRefCnt;
159 SkDEBUGCODE(int fReserveCnt;)
160 };
161
reed1b55a962015-09-17 20:16:13 -0700162 // This can't return a more abstract type because we install the stencil settings late :(
cdaltoncdd46822015-12-08 10:48:31 -0800163 static GrDrawPathBatchBase* Create(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
164 SkScalar y, GrColor color, GrPathRendering::FillType fill,
165 GrPathRange* range, const InstanceData* instanceData,
bsalomonbf074552015-11-23 14:25:19 -0800166 const SkRect& bounds) {
cdaltoncdd46822015-12-08 10:48:31 -0800167 return new GrDrawPathRangeBatch(viewMatrix, scale, x, y, color, fill, range, instanceData,
cdalton8ff8d242015-12-08 10:20:32 -0800168 bounds);
bsalomon1fcc01c2015-09-09 09:48:06 -0700169 }
170
bsalomon1fcc01c2015-09-09 09:48:06 -0700171 const char* name() const override { return "DrawPathRange"; }
172
173 SkString dumpInfo() const override;
174
175private:
cdaltoncdd46822015-12-08 10:48:31 -0800176 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
177 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
178 const InstanceData* instanceData, const SkRect& bounds);
179
180 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700181
182 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override;
183
184 void onPrepare(GrBatchFlushState*) override {}
185
186 void onDraw(GrBatchFlushState* state) override;
187
cdaltoncdd46822015-12-08 10:48:31 -0800188 struct Draw {
189 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
190 fInstanceData.reset(SkRef(instanceData));
191 fX = x;
192 fY = y;
193 }
194
195 SkAutoTUnref<const InstanceData> fInstanceData;
196 SkScalar fX, fY;
197 };
198
cdalton8585dd22015-10-08 08:04:09 -0700199 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800200 typedef SkTLList<Draw, 4> DrawList;
201
cdalton8585dd22015-10-08 08:04:09 -0700202 PendingPathRange fPathRange;
203 DrawList fDraws;
204 int fTotalPathCount;
cdaltoncdd46822015-12-08 10:48:31 -0800205 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700206
207 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700208};
209
210#endif