blob: d1dc00dbed1d0382753e36138f46a58f2b03a1a2 [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"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070012#include "GrBatchTarget.h"
13#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) {
53 return args.fViewMatrix->isSimilarity() && args.fStroke->getWidth() >= 1.0f &&
54 args.fStroke->getWidth() <= kMaxStrokeWidth && !args.fStroke->isDashed() &&
55 SkPathPriv::LastVerbIsClose(*args.fPath) &&
56 args.fStroke->getJoin() != SkPaint::Join::kRound_Join;
fmalitabd5d7e72015-06-26 07:18:24 -070057 }
bsalomon0aff2fa2015-07-31 06:48:27 -070058 return args.fStroke->getStyle() == SkStrokeRec::kFill_Style;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070059}
60
61// extract the result vertices and indices from the GrAAConvexTessellator
62static void extract_verts(const GrAAConvexTessellator& tess,
63 void* vertices,
64 size_t vertexStride,
65 GrColor color,
66 uint16_t firstIndex,
67 uint16_t* idxs,
68 bool tweakAlphaForCoverage) {
69 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
70
71 for (int i = 0; i < tess.numPts(); ++i) {
72 *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i);
73 }
74
75 // Make 'verts' point to the colors
76 verts += sizeof(SkPoint);
77 for (int i = 0; i < tess.numPts(); ++i) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -070078 if (tweakAlphaForCoverage) {
fmalitabd5d7e72015-06-26 07:18:24 -070079 SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255);
80 unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070081 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
82 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
83 } else {
84 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
85 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) =
fmalitabd5d7e72015-06-26 07:18:24 -070086 tess.coverage(i);
ethannicholas1a1b3ac2015-06-10 12:11:17 -070087 }
88 }
89
90 for (int i = 0; i < tess.numIndices(); ++i) {
91 idxs[i] = tess.index(i) + firstIndex;
92 }
93}
94
95static const GrGeometryProcessor* create_fill_gp(bool tweakAlphaForCoverage,
joshualittdf0c5572015-08-03 11:35:28 -070096 const SkMatrix& viewMatrix,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070097 bool usesLocalCoords,
98 bool coverageIgnored) {
joshualittdf0c5572015-08-03 11:35:28 -070099 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700100
joshualittdf0c5572015-08-03 11:35:28 -0700101 Color color(Color::kAttribute_Type);
102 Coverage::Type coverageType;
103 // TODO remove coverage if coverage is ignored
104 /*if (coverageIgnored) {
105 coverageType = Coverage::kNone_Type;
106 } else*/ if (tweakAlphaForCoverage) {
107 coverageType = Coverage::kSolid_Type;
108 } else {
109 coverageType = Coverage::kAttribute_Type;
110 }
111 Coverage coverage(coverageType);
112 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type :
113 LocalCoords::kUnused_Type);
114 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700115}
116
bsalomonabd30f52015-08-13 13:34:48 -0700117class AAFlatteningConvexPathBatch : public GrVertexBatch {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700118public:
119 struct Geometry {
120 GrColor fColor;
121 SkMatrix fViewMatrix;
122 SkPath fPath;
fmalitabd5d7e72015-06-26 07:18:24 -0700123 SkScalar fStrokeWidth;
124 SkPaint::Join fJoin;
125 SkScalar fMiterLimit;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700126 };
127
bsalomonabd30f52015-08-13 13:34:48 -0700128 static GrDrawBatch* Create(const Geometry& geometry) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700129 return SkNEW_ARGS(AAFlatteningConvexPathBatch, (geometry));
130 }
131
132 const char* name() const override { return "AAConvexBatch"; }
133
134 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
135 // When this is called on a batch, there is only one geometry bundle
136 out->setKnownFourComponents(fGeoData[0].fColor);
137 }
138 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
139 out->setUnknownSingleComponent();
140 }
141
bsalomon91d844d2015-08-10 10:47:29 -0700142 void initBatchTracker(const GrPipelineOptimizations& opt) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700143 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700144 if (!opt.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700145 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700146 }
bsalomon91d844d2015-08-10 10:47:29 -0700147 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700148
149 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700150 fBatch.fColorIgnored = !opt.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700151 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -0700152 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
153 fBatch.fCoverageIgnored = !opt.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700154 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
bsalomon91d844d2015-08-10 10:47:29 -0700155 fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700156 }
157
158 void draw(GrBatchTarget* batchTarget, const GrPipeline* pipeline, int vertexCount,
159 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) {
160 if (vertexCount == 0 || indexCount == 0) {
161 return;
162 }
163 const GrVertexBuffer* vertexBuffer;
164 GrVertices info;
165 int firstVertex;
166 void* verts = batchTarget->makeVertSpace(vertexStride, vertexCount, &vertexBuffer,
167 &firstVertex);
168 if (!verts) {
169 SkDebugf("Could not allocate vertices\n");
170 return;
171 }
172 memcpy(verts, vertices, vertexCount * vertexStride);
173
174 const GrIndexBuffer* indexBuffer;
175 int firstIndex;
176 uint16_t* idxs = batchTarget->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
177 if (!idxs) {
178 SkDebugf("Could not allocate indices\n");
179 return;
180 }
181 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
182 info.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
183 firstIndex, vertexCount, indexCount);
184 batchTarget->draw(info);
185 }
fmalitabd5d7e72015-06-26 07:18:24 -0700186
bsalomonfb1141a2015-08-06 08:52:49 -0700187 void generateGeometry(GrBatchTarget* batchTarget) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700188 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
189
joshualittdf0c5572015-08-03 11:35:28 -0700190 // Setup GrGeometryProcessor
191 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage,
192 this->viewMatrix(),
193 this->usesLocalCoords(),
194 this->coverageIgnored()));
195 if (!gp) {
196 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700197 return;
198 }
199
bsalomonfb1141a2015-08-06 08:52:49 -0700200 batchTarget->initDraw(gp, this->pipeline());
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700201
202 size_t vertexStride = gp->getVertexStride();
203
204 SkASSERT(canTweakAlphaForCoverage ?
205 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) :
206 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
207
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700208 int instanceCount = fGeoData.count();
209
210 int vertexCount = 0;
211 int indexCount = 0;
212 int maxVertices = DEFAULT_BUFFER_SIZE;
213 int maxIndices = DEFAULT_BUFFER_SIZE;
214 uint8_t* vertices = (uint8_t*) malloc(maxVertices * vertexStride);
215 uint16_t* indices = (uint16_t*) malloc(maxIndices * sizeof(uint16_t));
216 for (int i = 0; i < instanceCount; i++) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700217 Geometry& args = fGeoData[i];
fmalitabd5d7e72015-06-26 07:18:24 -0700218 GrAAConvexTessellator tess(args.fStrokeWidth, args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700219
220 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
221 continue;
222 }
223
224 int currentIndices = tess.numIndices();
225 SkASSERT(currentIndices <= UINT16_MAX);
226 if (indexCount + currentIndices > UINT16_MAX) {
227 // if we added the current instance, we would overflow the indices we can store in a
228 // uint16_t. Draw what we've got so far and reset.
bsalomonfb1141a2015-08-06 08:52:49 -0700229 draw(batchTarget, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700230 indices);
231 vertexCount = 0;
232 indexCount = 0;
233 }
234 int currentVertices = tess.numPts();
235 if (vertexCount + currentVertices > maxVertices) {
236 maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2);
237 vertices = (uint8_t*) realloc(vertices, maxVertices * vertexStride);
238 }
239 if (indexCount + currentIndices > maxIndices) {
240 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
241 indices = (uint16_t*) realloc(indices, maxIndices * sizeof(uint16_t));
242 }
243
244 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor,
245 vertexCount, indices + indexCount, canTweakAlphaForCoverage);
246 vertexCount += currentVertices;
247 indexCount += currentIndices;
248 }
bsalomonfb1141a2015-08-06 08:52:49 -0700249 draw(batchTarget, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
250 indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700251 free(vertices);
252 free(indices);
253 }
254
255 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
256
257private:
258 AAFlatteningConvexPathBatch(const Geometry& geometry) {
259 this->initClassID<AAFlatteningConvexPathBatch>();
260 fGeoData.push_back(geometry);
261
262 // compute bounds
263 fBounds = geometry.fPath.getBounds();
264 geometry.fViewMatrix.mapRect(&fBounds);
265 }
266
bsalomoncb02b382015-08-12 11:14:50 -0700267 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700268 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
269 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
270 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700271 return false;
272 }
273
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700274 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
275 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
276 return false;
277 }
278
279 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
280 // not tweaking
281 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
282 fBatch.fCanTweakAlphaForCoverage = false;
283 }
284
285 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
286 this->joinBounds(that->bounds());
287 return true;
288 }
289
290 GrColor color() const { return fBatch.fColor; }
291 bool linesOnly() const { return fBatch.fLinesOnly; }
292 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
293 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
294 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
295 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
296
297 struct BatchTracker {
298 GrColor fColor;
299 bool fUsesLocalCoords;
300 bool fColorIgnored;
301 bool fCoverageIgnored;
302 bool fLinesOnly;
303 bool fCanTweakAlphaForCoverage;
304 };
305
306 BatchTracker fBatch;
307 SkSTArray<1, Geometry, true> fGeoData;
308};
309
bsalomon0aff2fa2015-07-31 06:48:27 -0700310bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
311 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700312 return true;
313 }
314 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700315 geometry.fColor = args.fColor;
316 geometry.fViewMatrix = *args.fViewMatrix;
317 geometry.fPath = *args.fPath;
318 geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth();
319 geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join :
320 args.fStroke->getJoin();
321 geometry.fMiterLimit = args.fStroke->getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700322
bsalomonabd30f52015-08-13 13:34:48 -0700323 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700324 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700325
326 return true;
327}
328
329///////////////////////////////////////////////////////////////////////////////////////////////////
330
331#ifdef GR_TEST_UTILS
332
bsalomonabd30f52015-08-13 13:34:48 -0700333DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700334 AAFlatteningConvexPathBatch::Geometry geometry;
335 geometry.fColor = GrRandomColor(random);
336 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
337 geometry.fPath = GrTest::TestPathConvex(random);
338
339 return AAFlatteningConvexPathBatch::Create(geometry);
340}
341
342#endif