blob: 7e695d83df6c0232202024c39170d9f4e09e20e2 [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:
halcanary9d524f22016-03-29 09:03:52 -070022 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080023 GrInitInvariantOutput* coverage,
24 GrBatchToXPOverrides* overrides) const override {
25 color->setKnownFourComponents(fColor);
26 coverage->setKnownSingleComponent(0xff);
bsalomon1fcc01c2015-09-09 09:48:06 -070027 }
28
bsalomon1fcc01c2015-09-09 09:48:06 -070029protected:
cdalton8ff8d242015-12-08 10:20:32 -080030 GrDrawPathBatchBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor,
31 GrPathRendering::FillType fill)
reed1b55a962015-09-17 20:16:13 -070032 : INHERITED(classID)
33 , fViewMatrix(viewMatrix)
cdalton8ff8d242015-12-08 10:20:32 -080034 , fColor(initialColor)
35 , fFillType(fill) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070036
cdalton193d9cf2016-05-12 11:52:02 -070037 const GrStencilSettings& stencilPassSettings() const {
38 SkASSERT(!fStencilPassSettings.isDisabled()); // This shouldn't be called before onPrepare.
39 return fStencilPassSettings;
40 }
ethannicholasff210322015-11-24 12:10:10 -080041 const GrXPOverridesForBatch& overrides() const { return fOverrides; }
joshualittf2384692015-09-10 11:00:51 -070042 const SkMatrix& viewMatrix() const { return fViewMatrix; }
43 GrColor color() const { return fColor; }
cdalton193d9cf2016-05-12 11:52:02 -070044 GrPathRendering::FillType fillType() const { return fFillType; }
joshualittf2384692015-09-10 11:00:51 -070045
bsalomon1fcc01c2015-09-09 09:48:06 -070046private:
ethannicholasff210322015-11-24 12:10:10 -080047 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
48 overrides.getOverrideColorIfSet(&fColor);
49 fOverrides = overrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070050 }
51
cdalton193d9cf2016-05-12 11:52:02 -070052 void onPrepare(GrBatchFlushState*) override; // Initializes fStencilPassSettings.
53
joshualittf2384692015-09-10 11:00:51 -070054 SkMatrix fViewMatrix;
55 GrColor fColor;
cdalton8ff8d242015-12-08 10:20:32 -080056 GrPathRendering::FillType fFillType;
cdalton193d9cf2016-05-12 11:52:02 -070057 GrStencilSettings fStencilPassSettings;
ethannicholasff210322015-11-24 12:10:10 -080058 GrXPOverridesForBatch fOverrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070059
60 typedef GrDrawBatch INHERITED;
61};
62
63class GrDrawPathBatch final : public GrDrawPathBatchBase {
64public:
reed1b55a962015-09-17 20:16:13 -070065 DEFINE_BATCH_CLASS_ID
66
cdalton193d9cf2016-05-12 11:52:02 -070067 static GrDrawBatch* Create(const SkMatrix& viewMatrix, GrColor color,
68 GrPathRendering::FillType fill, const GrPath* path) {
cdalton8ff8d242015-12-08 10:20:32 -080069 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)
stephana1dc17212016-04-25 07:01:22 -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 }
stephana1dc17212016-04-25 07:01:22 -070084
85 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070086
bsalomon1fcc01c2015-09-09 09:48:06 -070087 void onDraw(GrBatchFlushState* state) override;
88
89 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070090
bsalomon1fcc01c2015-09-09 09:48:06 -070091 typedef GrDrawPathBatchBase INHERITED;
92};
93
bsalomon1fcc01c2015-09-09 09:48:06 -070094// Template this if we decide to support index types other than 16bit
95class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
96public:
cdaltoncdd46822015-12-08 10:48:31 -080097 typedef GrPathRendering::PathTransformType TransformType;
98
reed1b55a962015-09-17 20:16:13 -070099 DEFINE_BATCH_CLASS_ID
100
cdaltoncdd46822015-12-08 10:48:31 -0800101 struct InstanceData : public SkNoncopyable {
102 public:
103 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
104 int transformSize = GrPathRendering::PathTransformSize(transformType);
105 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
106 Align32(reserveCnt * sizeof(uint16_t)) +
107 reserveCnt * transformSize * sizeof(float));
108 InstanceData* instanceData = (InstanceData*)ptr;
109 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
110 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
111 Align32(reserveCnt * sizeof(uint16_t))];
112 instanceData->fTransformType = transformType;
113 instanceData->fInstanceCount = 0;
114 instanceData->fRefCnt = 1;
115 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt;)
116 return instanceData;
117 }
118
119 // Overload this method if we start using other transform types.
120 void append(uint16_t index, float x, float y) {
121 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
122 SkASSERT(fInstanceCount < fReserveCnt);
123 fIndices[fInstanceCount] = index;
124 fTransformValues[2 * fInstanceCount] = x;
125 fTransformValues[2 * fInstanceCount + 1] = y;
126 ++fInstanceCount;
127 }
128
129 TransformType transformType() const { return fTransformType; }
130 int count() const { return fInstanceCount; }
131
132 const uint16_t* indices() const { return fIndices; }
133 uint16_t* indices() { return fIndices; }
134
135 const float* transformValues() const { return fTransformValues; }
136 float* transformValues() { return fTransformValues; }
137
138 void ref() const { ++fRefCnt; }
139
140 void unref() const {
141 if (0 == --fRefCnt) {
142 sk_free(const_cast<InstanceData*>(this));
143 }
144 }
145
146 private:
147 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
148
149 InstanceData() {}
150 ~InstanceData() {}
151
152 uint16_t* fIndices;
153 float* fTransformValues;
154 TransformType fTransformType;
155 int fInstanceCount;
156 mutable int fRefCnt;
157 SkDEBUGCODE(int fReserveCnt;)
158 };
159
cdalton193d9cf2016-05-12 11:52:02 -0700160 static GrDrawBatch* Create(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
161 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
162 const InstanceData* instanceData, const SkRect& bounds) {
cdaltoncdd46822015-12-08 10:48:31 -0800163 return new GrDrawPathRangeBatch(viewMatrix, scale, x, y, color, fill, range, instanceData,
cdalton8ff8d242015-12-08 10:20:32 -0800164 bounds);
bsalomon1fcc01c2015-09-09 09:48:06 -0700165 }
166
bsalomon1fcc01c2015-09-09 09:48:06 -0700167 const char* name() const override { return "DrawPathRange"; }
168
169 SkString dumpInfo() const override;
170
171private:
cdaltoncdd46822015-12-08 10:48:31 -0800172 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
173 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
174 const InstanceData* instanceData, const SkRect& bounds);
175
176 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700177
178 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override;
179
bsalomon1fcc01c2015-09-09 09:48:06 -0700180 void onDraw(GrBatchFlushState* state) override;
181
cdaltoncdd46822015-12-08 10:48:31 -0800182 struct Draw {
183 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
184 fInstanceData.reset(SkRef(instanceData));
185 fX = x;
186 fY = y;
187 }
188
189 SkAutoTUnref<const InstanceData> fInstanceData;
190 SkScalar fX, fY;
191 };
192
cdalton8585dd22015-10-08 08:04:09 -0700193 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800194 typedef SkTLList<Draw, 4> DrawList;
195
cdalton8585dd22015-10-08 08:04:09 -0700196 PendingPathRange fPathRange;
197 DrawList fDraws;
198 int fTotalPathCount;
cdaltoncdd46822015-12-08 10:48:31 -0800199 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700200
201 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700202};
203
204#endif