blob: 1cd8daf881ede324dc774803592d489b8e657782 [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"
egdaniele659a582015-11-13 09:55:43 -080027#include "glsl/GrGLSLGeometryProcessor.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070028
fmalitabd5d7e72015-06-26 07:18:24 -070029static const int DEFAULT_BUFFER_SIZE = 100;
30
31// The thicker the stroke, the harder it is to produce high-quality results using tessellation. For
32// the time being, we simply drop back to software rendering above this stroke width.
33static const SkScalar kMaxStrokeWidth = 20.0;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070034
35GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() {
36}
37
38///////////////////////////////////////////////////////////////////////////////
39
bsalomon0aff2fa2015-07-31 06:48:27 -070040bool GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
41 if (!args.fAntiAlias) {
fmalitabd5d7e72015-06-26 07:18:24 -070042 return false;
43 }
bsalomon0aff2fa2015-07-31 06:48:27 -070044 if (args.fPath->isInverseFillType()) {
fmalitabd5d7e72015-06-26 07:18:24 -070045 return false;
46 }
bsalomon0aff2fa2015-07-31 06:48:27 -070047 if (!args.fPath->isConvex()) {
fmalitabd5d7e72015-06-26 07:18:24 -070048 return false;
49 }
bsalomon0aff2fa2015-07-31 06:48:27 -070050 if (args.fStroke->getStyle() == SkStrokeRec::kStroke_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070051 if (!args.fViewMatrix->isSimilarity()) {
52 return false;
53 }
54 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * args.fStroke->getWidth();
mtklein002c2ce2015-08-26 05:43:22 -070055 return strokeWidth >= 1.0f && strokeWidth <= kMaxStrokeWidth && !args.fStroke->isDashed() &&
ethannicholasc88cb892015-12-15 11:01:12 -080056 SkPathPriv::IsClosedSingleContour(*args.fPath) &&
bsalomon0aff2fa2015-07-31 06:48:27 -070057 args.fStroke->getJoin() != SkPaint::Join::kRound_Join;
fmalitabd5d7e72015-06-26 07:18:24 -070058 }
bsalomon0aff2fa2015-07-31 06:48:27 -070059 return args.fStroke->getStyle() == SkStrokeRec::kFill_Style;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070060}
61
62// extract the result vertices and indices from the GrAAConvexTessellator
63static void extract_verts(const GrAAConvexTessellator& tess,
64 void* vertices,
65 size_t vertexStride,
66 GrColor color,
67 uint16_t firstIndex,
68 uint16_t* idxs,
69 bool tweakAlphaForCoverage) {
70 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
71
72 for (int i = 0; i < tess.numPts(); ++i) {
73 *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i);
74 }
75
76 // Make 'verts' point to the colors
77 verts += sizeof(SkPoint);
78 for (int i = 0; i < tess.numPts(); ++i) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -070079 if (tweakAlphaForCoverage) {
fmalitabd5d7e72015-06-26 07:18:24 -070080 SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255);
81 unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070082 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
83 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
84 } else {
85 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
mtklein002c2ce2015-08-26 05:43:22 -070086 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) =
fmalitabd5d7e72015-06-26 07:18:24 -070087 tess.coverage(i);
ethannicholas1a1b3ac2015-06-10 12:11:17 -070088 }
89 }
90
91 for (int i = 0; i < tess.numIndices(); ++i) {
92 idxs[i] = tess.index(i) + firstIndex;
93 }
94}
95
96static const GrGeometryProcessor* create_fill_gp(bool tweakAlphaForCoverage,
joshualittdf0c5572015-08-03 11:35:28 -070097 const SkMatrix& viewMatrix,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070098 bool usesLocalCoords,
99 bool coverageIgnored) {
joshualittdf0c5572015-08-03 11:35:28 -0700100 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700101
joshualittdf0c5572015-08-03 11:35:28 -0700102 Color color(Color::kAttribute_Type);
103 Coverage::Type coverageType;
104 // TODO remove coverage if coverage is ignored
105 /*if (coverageIgnored) {
106 coverageType = Coverage::kNone_Type;
107 } else*/ if (tweakAlphaForCoverage) {
108 coverageType = Coverage::kSolid_Type;
109 } else {
110 coverageType = Coverage::kAttribute_Type;
111 }
112 Coverage coverage(coverageType);
113 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type :
114 LocalCoords::kUnused_Type);
115 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700116}
117
bsalomonabd30f52015-08-13 13:34:48 -0700118class AAFlatteningConvexPathBatch : public GrVertexBatch {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700119public:
reed1b55a962015-09-17 20:16:13 -0700120 DEFINE_BATCH_CLASS_ID
121
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700122 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) {
halcanary385fe4d2015-08-26 13:07:48 -0700132 return new AAFlatteningConvexPathBatch(geometry);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700133 }
134
135 const char* name() const override { return "AAConvexBatch"; }
136
ethannicholasff210322015-11-24 12:10:10 -0800137 void computePipelineOptimizations(GrInitInvariantOutput* color,
138 GrInitInvariantOutput* coverage,
139 GrBatchToXPOverrides* overrides) const override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700140 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800141 color->setKnownFourComponents(fGeoData[0].fColor);
142 coverage->setUnknownSingleComponent();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700143 }
144
bsalomone46f9fe2015-08-18 06:05:14 -0700145private:
ethannicholasff210322015-11-24 12:10:10 -0800146 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700147 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -0800148 if (!overrides.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700149 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700150 }
ethannicholasff210322015-11-24 12:10:10 -0800151 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700152
153 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -0800154 fBatch.fColorIgnored = !overrides.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700155 fBatch.fColor = fGeoData[0].fColor;
ethannicholasff210322015-11-24 12:10:10 -0800156 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
157 fBatch.fCoverageIgnored = !overrides.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700158 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
ethannicholasff210322015-11-24 12:10:10 -0800159 fBatch.fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700160 }
161
mtklein002c2ce2015-08-26 05:43:22 -0700162 void draw(GrVertexBatch::Target* target, const GrPipeline* pipeline, int vertexCount,
joshualitt144c3c82015-11-30 12:30:13 -0800163 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700164 if (vertexCount == 0 || indexCount == 0) {
165 return;
166 }
cdalton397536c2016-03-25 12:15:03 -0700167 const GrBuffer* vertexBuffer;
egdaniel0e1853c2016-03-17 11:35:45 -0700168 GrMesh mesh;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700169 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700170 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
bsalomon75398562015-08-17 12:55:38 -0700171 &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
cdalton397536c2016-03-25 12:15:03 -0700178 const GrBuffer* indexBuffer;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700179 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));
egdaniel0e1853c2016-03-17 11:35:45 -0700186 mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
187 firstIndex, vertexCount, indexCount);
188 target->draw(mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700189 }
mtklein002c2ce2015-08-26 05:43:22 -0700190
joshualitt144c3c82015-11-30 12:30:13 -0800191 void onPrepareDraws(Target* target) const 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
egdaniel0e1853c2016-03-17 11:35:45 -0700204 target->initDraw(gp);
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;
mtklein002c2ce2015-08-26 05:43:22 -0700218 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
219 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700220 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800221 const 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) {
mtklein002c2ce2015-08-26 05:43:22 -0700231 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700232 // uint16_t. Draw what we've got so far and reset.
joshualitt144c3c82015-11-30 12:30:13 -0800233 this->draw(target, this->pipeline(), vertexCount, vertexStride, vertices,
234 indexCount, 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);
mtklein002c2ce2015-08-26 05:43:22 -0700241 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700242 }
243 if (indexCount + currentIndices > maxIndices) {
244 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700245 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700246 }
247
mtklein002c2ce2015-08-26 05:43:22 -0700248 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700249 vertexCount, indices + indexCount, canTweakAlphaForCoverage);
250 vertexCount += currentVertices;
251 indexCount += currentIndices;
252 }
joshualitt144c3c82015-11-30 12:30:13 -0800253 this->draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
254 indices);
mtklein002c2ce2015-08-26 05:43:22 -0700255 sk_free(vertices);
256 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700257 }
258
259 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
260
reed1b55a962015-09-17 20:16:13 -0700261 AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700262 fGeoData.push_back(geometry);
263
264 // compute bounds
265 fBounds = geometry.fPath.getBounds();
bsalomondb4758c2015-11-23 11:14:20 -0800266 SkScalar w = geometry.fStrokeWidth;
267 if (w > 0) {
268 w /= 2;
269 // If the miter limit is < 1 then we effectively fallback to bevel joins.
270 if (SkPaint::kMiter_Join == geometry.fJoin && w > 1.f) {
271 w *= geometry.fMiterLimit;
272 }
273 fBounds.outset(w, w);
274 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700275 geometry.fViewMatrix.mapRect(&fBounds);
276 }
277
bsalomoncb02b382015-08-12 11:14:50 -0700278 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700279 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
280 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
281 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700282 return false;
283 }
284
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700285 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
286 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
287 return false;
288 }
289
290 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
291 // not tweaking
292 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
293 fBatch.fCanTweakAlphaForCoverage = false;
294 }
295
296 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
297 this->joinBounds(that->bounds());
298 return true;
299 }
300
301 GrColor color() const { return fBatch.fColor; }
302 bool linesOnly() const { return fBatch.fLinesOnly; }
303 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
304 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
305 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
306 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
307
308 struct BatchTracker {
309 GrColor fColor;
310 bool fUsesLocalCoords;
311 bool fColorIgnored;
312 bool fCoverageIgnored;
313 bool fLinesOnly;
314 bool fCanTweakAlphaForCoverage;
315 };
316
317 BatchTracker fBatch;
318 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700319
320 typedef GrVertexBatch INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700321};
322
bsalomon0aff2fa2015-07-31 06:48:27 -0700323bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
joshualittde83b412016-01-14 09:58:36 -0800324 GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(),
325 "GrAALinearizingConvexPathRenderer::onDrawPath");
bsalomon0aff2fa2015-07-31 06:48:27 -0700326 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700327 return true;
328 }
329 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700330 geometry.fColor = args.fColor;
331 geometry.fViewMatrix = *args.fViewMatrix;
332 geometry.fPath = *args.fPath;
333 geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth();
334 geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join :
335 args.fStroke->getJoin();
336 geometry.fMiterLimit = args.fStroke->getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700337
bsalomonabd30f52015-08-13 13:34:48 -0700338 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700339 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700340
341 return true;
342}
343
344///////////////////////////////////////////////////////////////////////////////////////////////////
345
346#ifdef GR_TEST_UTILS
347
bsalomonabd30f52015-08-13 13:34:48 -0700348DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700349 AAFlatteningConvexPathBatch::Geometry geometry;
350 geometry.fColor = GrRandomColor(random);
351 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
352 geometry.fPath = GrTest::TestPathConvex(random);
353
354 return AAFlatteningConvexPathBatch::Create(geometry);
355}
356
357#endif