blob: 9de29e21717dcb9eeec3e6acfdb14fa322841b80 [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"
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
21class GrDrawPathBatchBase : public GrDrawBatch {
bsalomonadd79ef2015-08-19 13:26:49 -070022public:
halcanary9d524f22016-03-29 09:03:52 -070023 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080024 GrInitInvariantOutput* coverage,
25 GrBatchToXPOverrides* overrides) const override {
26 color->setKnownFourComponents(fColor);
27 coverage->setKnownSingleComponent(0xff);
bsalomon1fcc01c2015-09-09 09:48:06 -070028 }
29
bsalomon1fcc01c2015-09-09 09:48:06 -070030protected:
cdalton8ff8d242015-12-08 10:20:32 -080031 GrDrawPathBatchBase(uint32_t classID, const SkMatrix& viewMatrix, GrColor initialColor,
32 GrPathRendering::FillType fill)
reed1b55a962015-09-17 20:16:13 -070033 : INHERITED(classID)
34 , fViewMatrix(viewMatrix)
cdalton8ff8d242015-12-08 10:20:32 -080035 , fColor(initialColor)
36 , fFillType(fill) {}
bsalomon1fcc01c2015-09-09 09:48:06 -070037
cdalton193d9cf2016-05-12 11:52:02 -070038 const GrStencilSettings& stencilPassSettings() const {
39 SkASSERT(!fStencilPassSettings.isDisabled()); // This shouldn't be called before onPrepare.
40 return fStencilPassSettings;
41 }
ethannicholasff210322015-11-24 12:10:10 -080042 const GrXPOverridesForBatch& overrides() const { return fOverrides; }
joshualittf2384692015-09-10 11:00:51 -070043 const SkMatrix& viewMatrix() const { return fViewMatrix; }
44 GrColor color() const { return fColor; }
cdalton193d9cf2016-05-12 11:52:02 -070045 GrPathRendering::FillType fillType() const { return fFillType; }
joshualittf2384692015-09-10 11:00:51 -070046
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
cdalton193d9cf2016-05-12 11:52:02 -070053 void onPrepare(GrBatchFlushState*) override; // Initializes fStencilPassSettings.
54
joshualittf2384692015-09-10 11:00:51 -070055 SkMatrix fViewMatrix;
56 GrColor fColor;
cdalton8ff8d242015-12-08 10:20:32 -080057 GrPathRendering::FillType fFillType;
cdalton193d9cf2016-05-12 11:52:02 -070058 GrStencilSettings fStencilPassSettings;
ethannicholasff210322015-11-24 12:10:10 -080059 GrXPOverridesForBatch fOverrides;
bsalomon1fcc01c2015-09-09 09:48:06 -070060
61 typedef GrDrawBatch INHERITED;
62};
63
64class GrDrawPathBatch final : public GrDrawPathBatchBase {
65public:
Brian Salomon25a88092016-12-01 09:36:50 -050066 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070067
bsalomona224bb72016-10-03 09:48:22 -070068 static GrDrawBatch* Create(const SkMatrix& viewMatrix, GrColor color, const GrPath* path) {
69 return new GrDrawPathBatch(viewMatrix, color, 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:
bsalomona224bb72016-10-03 09:48:22 -070077 GrDrawPathBatch(const SkMatrix& viewMatrix, GrColor color, const GrPath* path)
78 : INHERITED(ClassID(), viewMatrix, color, path->getFillType())
stephana1dc17212016-04-25 07:01:22 -070079 , fPath(path) {
bsalomon88cf17d2016-07-08 06:40:56 -070080 this->setTransformedBounds(path->getBounds(), viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomonadd79ef2015-08-19 13:26:49 -070081 }
stephana1dc17212016-04-25 07:01:22 -070082
Brian Salomon25a88092016-12-01 09:36:50 -050083 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { return false; }
bsalomonadd79ef2015-08-19 13:26:49 -070084
Greg Daniel36a77ee2016-10-18 10:33:25 -040085 void onDraw(GrBatchFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -070086
87 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
stephana1dc17212016-04-25 07:01:22 -070088
bsalomon1fcc01c2015-09-09 09:48:06 -070089 typedef GrDrawPathBatchBase INHERITED;
90};
91
bsalomon1fcc01c2015-09-09 09:48:06 -070092// Template this if we decide to support index types other than 16bit
93class GrDrawPathRangeBatch final : public GrDrawPathBatchBase {
94public:
cdaltoncdd46822015-12-08 10:48:31 -080095 typedef GrPathRendering::PathTransformType TransformType;
96
Brian Salomon25a88092016-12-01 09:36:50 -050097 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -070098
cdaltoncdd46822015-12-08 10:48:31 -080099 struct InstanceData : public SkNoncopyable {
100 public:
101 static InstanceData* Alloc(TransformType transformType, int reserveCnt) {
102 int transformSize = GrPathRendering::PathTransformSize(transformType);
103 uint8_t* ptr = (uint8_t*)sk_malloc_throw(Align32(sizeof(InstanceData)) +
104 Align32(reserveCnt * sizeof(uint16_t)) +
105 reserveCnt * transformSize * sizeof(float));
106 InstanceData* instanceData = (InstanceData*)ptr;
107 instanceData->fIndices = (uint16_t*)&ptr[Align32(sizeof(InstanceData))];
108 instanceData->fTransformValues = (float*)&ptr[Align32(sizeof(InstanceData)) +
109 Align32(reserveCnt * sizeof(uint16_t))];
110 instanceData->fTransformType = transformType;
111 instanceData->fInstanceCount = 0;
112 instanceData->fRefCnt = 1;
113 SkDEBUGCODE(instanceData->fReserveCnt = reserveCnt;)
114 return instanceData;
115 }
116
117 // Overload this method if we start using other transform types.
118 void append(uint16_t index, float x, float y) {
119 SkASSERT(GrPathRendering::kTranslate_PathTransformType == fTransformType);
120 SkASSERT(fInstanceCount < fReserveCnt);
121 fIndices[fInstanceCount] = index;
122 fTransformValues[2 * fInstanceCount] = x;
123 fTransformValues[2 * fInstanceCount + 1] = y;
124 ++fInstanceCount;
125 }
126
127 TransformType transformType() const { return fTransformType; }
128 int count() const { return fInstanceCount; }
129
130 const uint16_t* indices() const { return fIndices; }
131 uint16_t* indices() { return fIndices; }
132
133 const float* transformValues() const { return fTransformValues; }
134 float* transformValues() { return fTransformValues; }
135
136 void ref() const { ++fRefCnt; }
137
138 void unref() const {
139 if (0 == --fRefCnt) {
140 sk_free(const_cast<InstanceData*>(this));
141 }
142 }
143
144 private:
145 static int Align32(int sizeInBytes) { return (sizeInBytes + 3) & ~3; }
146
147 InstanceData() {}
148 ~InstanceData() {}
149
150 uint16_t* fIndices;
151 float* fTransformValues;
152 TransformType fTransformType;
153 int fInstanceCount;
154 mutable int fRefCnt;
155 SkDEBUGCODE(int fReserveCnt;)
156 };
157
cdalton193d9cf2016-05-12 11:52:02 -0700158 static GrDrawBatch* Create(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
159 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
160 const InstanceData* instanceData, const SkRect& bounds) {
cdaltoncdd46822015-12-08 10:48:31 -0800161 return new GrDrawPathRangeBatch(viewMatrix, scale, x, y, color, fill, range, instanceData,
cdalton8ff8d242015-12-08 10:20:32 -0800162 bounds);
bsalomon1fcc01c2015-09-09 09:48:06 -0700163 }
164
bsalomon1fcc01c2015-09-09 09:48:06 -0700165 const char* name() const override { return "DrawPathRange"; }
166
167 SkString dumpInfo() const override;
168
169private:
cdaltoncdd46822015-12-08 10:48:31 -0800170 GrDrawPathRangeBatch(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, SkScalar y,
171 GrColor color, GrPathRendering::FillType fill, GrPathRange* range,
172 const InstanceData* instanceData, const SkRect& bounds);
173
174 TransformType transformType() const { return fDraws.head()->fInstanceData->transformType(); }
bsalomon1fcc01c2015-09-09 09:48:06 -0700175
Brian Salomon25a88092016-12-01 09:36:50 -0500176 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700177
Greg Daniel36a77ee2016-10-18 10:33:25 -0400178 void onDraw(GrBatchFlushState* state, const SkRect& bounds) override;
bsalomon1fcc01c2015-09-09 09:48:06 -0700179
cdaltoncdd46822015-12-08 10:48:31 -0800180 struct Draw {
181 void set(const InstanceData* instanceData, SkScalar x, SkScalar y) {
182 fInstanceData.reset(SkRef(instanceData));
183 fX = x;
184 fY = y;
185 }
186
Hal Canary144caf52016-11-07 17:57:18 -0500187 sk_sp<const InstanceData> fInstanceData;
188 SkScalar fX, fY;
cdaltoncdd46822015-12-08 10:48:31 -0800189 };
190
cdalton8585dd22015-10-08 08:04:09 -0700191 typedef GrPendingIOResource<const GrPathRange, kRead_GrIOType> PendingPathRange;
cdaltoncdd46822015-12-08 10:48:31 -0800192 typedef SkTLList<Draw, 4> DrawList;
193
cdalton8585dd22015-10-08 08:04:09 -0700194 PendingPathRange fPathRange;
195 DrawList fDraws;
196 int fTotalPathCount;
cdaltoncdd46822015-12-08 10:48:31 -0800197 SkScalar fScale;
bsalomon1fcc01c2015-09-09 09:48:06 -0700198
199 typedef GrDrawPathBatchBase INHERITED;
bsalomonadd79ef2015-08-19 13:26:49 -0700200};
201
202#endif