blob: 899c6b1ef1acb9ae856dcf5c535f7618c17df91a [file] [log] [blame]
ethannicholas1a1b3ac2015-06-10 12:11:17 -07001
2/*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "GrAALinearizingConvexPathRenderer.h"
10
11#include "GrAAConvexTessellator.h"
bsalomon75398562015-08-17 12:55:38 -070012#include "GrBatchFlushState.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070013#include "GrBatchTest.h"
14#include "GrContext.h"
15#include "GrDefaultGeoProcFactory.h"
16#include "GrGeometryProcessor.h"
17#include "GrInvariantOutput.h"
18#include "GrPathUtils.h"
19#include "GrProcessor.h"
20#include "GrPipelineBuilder.h"
21#include "GrStrokeInfo.h"
22#include "SkGeometry.h"
23#include "SkString.h"
24#include "SkTraceEvent.h"
fmalitabd5d7e72015-06-26 07:18:24 -070025#include "SkPathPriv.h"
bsalomon16b99132015-08-13 14:55:50 -070026#include "batches/GrVertexBatch.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070027#include "gl/GrGLProcessor.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070028#include "gl/GrGLGeometryProcessor.h"
29#include "gl/builders/GrGLProgramBuilder.h"
30
fmalitabd5d7e72015-06-26 07:18:24 -070031static const int DEFAULT_BUFFER_SIZE = 100;
32
33// The thicker the stroke, the harder it is to produce high-quality results using tessellation. For
34// the time being, we simply drop back to software rendering above this stroke width.
35static const SkScalar kMaxStrokeWidth = 20.0;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070036
37GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() {
38}
39
40///////////////////////////////////////////////////////////////////////////////
41
bsalomon0aff2fa2015-07-31 06:48:27 -070042bool GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
43 if (!args.fAntiAlias) {
fmalitabd5d7e72015-06-26 07:18:24 -070044 return false;
45 }
bsalomon0aff2fa2015-07-31 06:48:27 -070046 if (args.fPath->isInverseFillType()) {
fmalitabd5d7e72015-06-26 07:18:24 -070047 return false;
48 }
bsalomon0aff2fa2015-07-31 06:48:27 -070049 if (!args.fPath->isConvex()) {
fmalitabd5d7e72015-06-26 07:18:24 -070050 return false;
51 }
bsalomon0aff2fa2015-07-31 06:48:27 -070052 if (args.fStroke->getStyle() == SkStrokeRec::kStroke_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070053 if (!args.fViewMatrix->isSimilarity()) {
54 return false;
55 }
56 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * args.fStroke->getWidth();
57 return strokeWidth >= 1.0f && strokeWidth <= kMaxStrokeWidth && !args.fStroke->isDashed() &&
bsalomon0aff2fa2015-07-31 06:48:27 -070058 SkPathPriv::LastVerbIsClose(*args.fPath) &&
59 args.fStroke->getJoin() != SkPaint::Join::kRound_Join;
fmalitabd5d7e72015-06-26 07:18:24 -070060 }
bsalomon0aff2fa2015-07-31 06:48:27 -070061 return args.fStroke->getStyle() == SkStrokeRec::kFill_Style;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070062}
63
64// extract the result vertices and indices from the GrAAConvexTessellator
65static void extract_verts(const GrAAConvexTessellator& tess,
66 void* vertices,
67 size_t vertexStride,
68 GrColor color,
69 uint16_t firstIndex,
70 uint16_t* idxs,
71 bool tweakAlphaForCoverage) {
72 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
73
74 for (int i = 0; i < tess.numPts(); ++i) {
75 *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i);
76 }
77
78 // Make 'verts' point to the colors
79 verts += sizeof(SkPoint);
80 for (int i = 0; i < tess.numPts(); ++i) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -070081 if (tweakAlphaForCoverage) {
fmalitabd5d7e72015-06-26 07:18:24 -070082 SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255);
83 unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070084 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
85 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
86 } else {
87 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
88 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) =
fmalitabd5d7e72015-06-26 07:18:24 -070089 tess.coverage(i);
ethannicholas1a1b3ac2015-06-10 12:11:17 -070090 }
91 }
92
93 for (int i = 0; i < tess.numIndices(); ++i) {
94 idxs[i] = tess.index(i) + firstIndex;
95 }
96}
97
98static const GrGeometryProcessor* create_fill_gp(bool tweakAlphaForCoverage,
joshualittdf0c5572015-08-03 11:35:28 -070099 const SkMatrix& viewMatrix,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700100 bool usesLocalCoords,
101 bool coverageIgnored) {
joshualittdf0c5572015-08-03 11:35:28 -0700102 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700103
joshualittdf0c5572015-08-03 11:35:28 -0700104 Color color(Color::kAttribute_Type);
105 Coverage::Type coverageType;
106 // TODO remove coverage if coverage is ignored
107 /*if (coverageIgnored) {
108 coverageType = Coverage::kNone_Type;
109 } else*/ if (tweakAlphaForCoverage) {
110 coverageType = Coverage::kSolid_Type;
111 } else {
112 coverageType = Coverage::kAttribute_Type;
113 }
114 Coverage coverage(coverageType);
115 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type :
116 LocalCoords::kUnused_Type);
117 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700118}
119
bsalomonabd30f52015-08-13 13:34:48 -0700120class AAFlatteningConvexPathBatch : public GrVertexBatch {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700121public:
122 struct Geometry {
123 GrColor fColor;
124 SkMatrix fViewMatrix;
125 SkPath fPath;
fmalitabd5d7e72015-06-26 07:18:24 -0700126 SkScalar fStrokeWidth;
127 SkPaint::Join fJoin;
128 SkScalar fMiterLimit;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700129 };
130
bsalomonabd30f52015-08-13 13:34:48 -0700131 static GrDrawBatch* Create(const Geometry& geometry) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700132 return SkNEW_ARGS(AAFlatteningConvexPathBatch, (geometry));
133 }
134
135 const char* name() const override { return "AAConvexBatch"; }
136
137 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
138 // When this is called on a batch, there is only one geometry bundle
139 out->setKnownFourComponents(fGeoData[0].fColor);
140 }
141 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
142 out->setUnknownSingleComponent();
143 }
144
bsalomone46f9fe2015-08-18 06:05:14 -0700145private:
bsalomon91d844d2015-08-10 10:47:29 -0700146 void initBatchTracker(const GrPipelineOptimizations& opt) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700147 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700148 if (!opt.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700149 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700150 }
bsalomon91d844d2015-08-10 10:47:29 -0700151 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700152
153 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700154 fBatch.fColorIgnored = !opt.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700155 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -0700156 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
157 fBatch.fCoverageIgnored = !opt.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700158 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
bsalomon91d844d2015-08-10 10:47:29 -0700159 fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700160 }
161
bsalomon75398562015-08-17 12:55:38 -0700162 void draw(GrVertexBatch::Target* target, const GrPipeline* pipeline, int vertexCount,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700163 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) {
164 if (vertexCount == 0 || indexCount == 0) {
165 return;
166 }
167 const GrVertexBuffer* vertexBuffer;
168 GrVertices info;
169 int firstVertex;
bsalomon75398562015-08-17 12:55:38 -0700170 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
171 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700172 if (!verts) {
173 SkDebugf("Could not allocate vertices\n");
174 return;
175 }
176 memcpy(verts, vertices, vertexCount * vertexStride);
177
178 const GrIndexBuffer* indexBuffer;
179 int firstIndex;
bsalomon75398562015-08-17 12:55:38 -0700180 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700181 if (!idxs) {
182 SkDebugf("Could not allocate indices\n");
183 return;
184 }
185 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
186 info.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
187 firstIndex, vertexCount, indexCount);
bsalomon75398562015-08-17 12:55:38 -0700188 target->draw(info);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700189 }
fmalitabd5d7e72015-06-26 07:18:24 -0700190
bsalomon75398562015-08-17 12:55:38 -0700191 void onPrepareDraws(Target* target) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700192 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
193
joshualittdf0c5572015-08-03 11:35:28 -0700194 // Setup GrGeometryProcessor
195 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage,
196 this->viewMatrix(),
197 this->usesLocalCoords(),
198 this->coverageIgnored()));
199 if (!gp) {
200 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700201 return;
202 }
203
bsalomon75398562015-08-17 12:55:38 -0700204 target->initDraw(gp, this->pipeline());
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700205
206 size_t vertexStride = gp->getVertexStride();
207
208 SkASSERT(canTweakAlphaForCoverage ?
209 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) :
210 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
211
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700212 int instanceCount = fGeoData.count();
213
214 int vertexCount = 0;
215 int indexCount = 0;
216 int maxVertices = DEFAULT_BUFFER_SIZE;
217 int maxIndices = DEFAULT_BUFFER_SIZE;
218 uint8_t* vertices = (uint8_t*) malloc(maxVertices * vertexStride);
219 uint16_t* indices = (uint16_t*) malloc(maxIndices * sizeof(uint16_t));
220 for (int i = 0; i < instanceCount; i++) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700221 Geometry& args = fGeoData[i];
fmalitabd5d7e72015-06-26 07:18:24 -0700222 GrAAConvexTessellator tess(args.fStrokeWidth, args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700223
224 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
225 continue;
226 }
227
228 int currentIndices = tess.numIndices();
229 SkASSERT(currentIndices <= UINT16_MAX);
230 if (indexCount + currentIndices > UINT16_MAX) {
231 // if we added the current instance, we would overflow the indices we can store in a
232 // uint16_t. Draw what we've got so far and reset.
bsalomon75398562015-08-17 12:55:38 -0700233 draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
234 indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700235 vertexCount = 0;
236 indexCount = 0;
237 }
238 int currentVertices = tess.numPts();
239 if (vertexCount + currentVertices > maxVertices) {
240 maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2);
241 vertices = (uint8_t*) realloc(vertices, maxVertices * vertexStride);
242 }
243 if (indexCount + currentIndices > maxIndices) {
244 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
245 indices = (uint16_t*) realloc(indices, maxIndices * sizeof(uint16_t));
246 }
247
248 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor,
249 vertexCount, indices + indexCount, canTweakAlphaForCoverage);
250 vertexCount += currentVertices;
251 indexCount += currentIndices;
252 }
bsalomon75398562015-08-17 12:55:38 -0700253 draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
bsalomonfb1141a2015-08-06 08:52:49 -0700254 indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700255 free(vertices);
256 free(indices);
257 }
258
259 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
260
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700261 AAFlatteningConvexPathBatch(const Geometry& geometry) {
262 this->initClassID<AAFlatteningConvexPathBatch>();
263 fGeoData.push_back(geometry);
264
265 // compute bounds
266 fBounds = geometry.fPath.getBounds();
267 geometry.fViewMatrix.mapRect(&fBounds);
268 }
269
bsalomoncb02b382015-08-12 11:14:50 -0700270 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700271 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
272 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
273 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700274 return false;
275 }
276
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700277 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
278 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
279 return false;
280 }
281
282 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
283 // not tweaking
284 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
285 fBatch.fCanTweakAlphaForCoverage = false;
286 }
287
288 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
289 this->joinBounds(that->bounds());
290 return true;
291 }
292
293 GrColor color() const { return fBatch.fColor; }
294 bool linesOnly() const { return fBatch.fLinesOnly; }
295 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
296 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
297 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
298 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
299
300 struct BatchTracker {
301 GrColor fColor;
302 bool fUsesLocalCoords;
303 bool fColorIgnored;
304 bool fCoverageIgnored;
305 bool fLinesOnly;
306 bool fCanTweakAlphaForCoverage;
307 };
308
309 BatchTracker fBatch;
310 SkSTArray<1, Geometry, true> fGeoData;
311};
312
bsalomon0aff2fa2015-07-31 06:48:27 -0700313bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
314 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700315 return true;
316 }
317 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700318 geometry.fColor = args.fColor;
319 geometry.fViewMatrix = *args.fViewMatrix;
320 geometry.fPath = *args.fPath;
321 geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth();
322 geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join :
323 args.fStroke->getJoin();
324 geometry.fMiterLimit = args.fStroke->getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700325
bsalomonabd30f52015-08-13 13:34:48 -0700326 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700327 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700328
329 return true;
330}
331
332///////////////////////////////////////////////////////////////////////////////////////////////////
333
334#ifdef GR_TEST_UTILS
335
bsalomonabd30f52015-08-13 13:34:48 -0700336DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700337 AAFlatteningConvexPathBatch::Geometry geometry;
338 geometry.fColor = GrRandomColor(random);
339 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
340 geometry.fPath = GrTest::TestPathConvex(random);
341
342 return AAFlatteningConvexPathBatch::Create(geometry);
343}
344
345#endif