blob: cfdbc02c3d6eec094b438497450cb250f19fe944 [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) {
bsalomon88cf17d2016-07-08 06:40:56 -070081 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070082 }
stephana1dc17212016-04-25 07:01:22 -070083
84 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070085
bsalomon1fcc01c2015-09-09 09:48:06 -070086 void onDraw(GrBatchFlushState* state) override;
87
88 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070089
bsalomon1fcc01c2015-09-09 09:48:06 -070090 typedef GrDrawPathBatchBase INHERITED;
91};
92
bsalomon1fcc01c2015-09-09 09:48:06 -070093// Template this if we decide to support index types other than 16bit
94class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
95public:
cdaltoncdd46822015-12-08 10:48:31 -080096 typedef GrPathRendering::PathTransformType TransformType;
97
reed1b55a962015-09-17 20:16:13 -070098 DEFINE_BATCH_CLASS_ID
99
cdaltoncdd46822015-12-08 10:48:31 -0800100 struct InstanceData : public SkNoncopyable {
101 public:
102 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
103 int transformSize = GrPathRendering::PathTransformSize(transformType);
104 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
105 Align32(reserveCnt * sizeof(uint16_t)) +
106 reserveCnt * transformSize * sizeof(float));
107 InstanceData* instanceData = (InstanceData*)ptr;
108 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
109 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
110 Align32(reserveCnt * sizeof(uint16_t))];
111 instanceData->fTransformType = transformType;
112 instanceData->fInstanceCount = 0;
113 instanceData->fRefCnt = 1;
114 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt;)
115 return instanceData;
116 }
117
118 // Overload this method if we start using other transform types.
119 void append(uint16_t index, float x, float y) {
120 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
121 SkASSERT(fInstanceCount < fReserveCnt);
122 fIndices[fInstanceCount] = index;
123 fTransformValues[2 * fInstanceCount] = x;
124 fTransformValues[2 * fInstanceCount + 1] = y;
125 ++fInstanceCount;
126 }
127
128 TransformType transformType() const { return fTransformType; }
129 int count() const { return fInstanceCount; }
130
131 const uint16_t* indices() const { return fIndices; }
132 uint16_t* indices() { return fIndices; }
133
134 const float* transformValues() const { return fTransformValues; }
135 float* transformValues() { return fTransformValues; }
136
137 void ref() const { ++fRefCnt; }
138
139 void unref() const {
140 if (0 == --fRefCnt) {
141 sk_free(const_cast<InstanceData*>(this));
142 }
143 }
144
145 private:
146 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
147
148 InstanceData() {}
149 ~InstanceData() {}
150
151 uint16_t* fIndices;
152 float* fTransformValues;
153 TransformType fTransformType;
154 int fInstanceCount;
155 mutable int fRefCnt;
156 SkDEBUGCODE(int fReserveCnt;)
157 };
158
cdalton193d9cf2016-05-12 11:52:02 -0700159 static GrDrawBatch* Create(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
160 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
161 const InstanceData* instanceData, const SkRect& bounds) {
cdaltoncdd46822015-12-08 10:48:31 -0800162 return new GrDrawPathRangeBatch(viewMatrix, scale, x, y, color, fill, range, instanceData,
cdalton8ff8d242015-12-08 10:20:32 -0800163 bounds);
bsalomon1fcc01c2015-09-09 09:48:06 -0700164 }
165
bsalomon1fcc01c2015-09-09 09:48:06 -0700166 const char* name() const override { return "DrawPathRange"; }
167
168 SkString dumpInfo() const override;
169
170private:
cdaltoncdd46822015-12-08 10:48:31 -0800171 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
172 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
173 const InstanceData* instanceData, const SkRect& bounds);
174
175 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700176
177 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override;
178
bsalomon1fcc01c2015-09-09 09:48:06 -0700179 void onDraw(GrBatchFlushState* state) override;
180
cdaltoncdd46822015-12-08 10:48:31 -0800181 struct Draw {
182 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
183 fInstanceData.reset(SkRef(instanceData));
184 fX = x;
185 fY = y;
186 }
187
188 SkAutoTUnref<const InstanceData> fInstanceData;
189 SkScalar fX, fY;
190 };
191
cdalton8585dd22015-10-08 08:04:09 -0700192 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800193 typedef SkTLList<Draw, 4> DrawList;
194
cdalton8585dd22015-10-08 08:04:09 -0700195 PendingPathRange fPathRange;
196 DrawList fDraws;
197 int fTotalPathCount;
cdaltoncdd46822015-12-08 10:48:31 -0800198 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700199
200 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700201};
202
203#endif