blob: 446f67f1c01a36f09595ce051580ac08640ba9a8 [file] [log] [blame]
ethannicholas1a1b3ac2015-06-10 12:11:17 -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#include "GrAALinearizingConvexPathRenderer.h"
9
10#include "GrAAConvexTessellator.h"
bsalomon75398562015-08-17 12:55:38 -070011#include "GrBatchFlushState.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070012#include "GrBatchTest.h"
13#include "GrContext.h"
14#include "GrDefaultGeoProcFactory.h"
15#include "GrGeometryProcessor.h"
16#include "GrInvariantOutput.h"
17#include "GrPathUtils.h"
18#include "GrProcessor.h"
19#include "GrPipelineBuilder.h"
20#include "GrStrokeInfo.h"
21#include "SkGeometry.h"
22#include "SkString.h"
23#include "SkTraceEvent.h"
fmalitabd5d7e72015-06-26 07:18:24 -070024#include "SkPathPriv.h"
bsalomon16b99132015-08-13 14:55:50 -070025#include "batches/GrVertexBatch.h"
egdaniele659a582015-11-13 09:55:43 -080026#include "glsl/GrGLSLGeometryProcessor.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070027
fmalitabd5d7e72015-06-26 07:18:24 -070028static const int DEFAULT_BUFFER_SIZE = 100;
29
30// The thicker the stroke, the harder it is to produce high-quality results using tessellation. For
31// the time being, we simply drop back to software rendering above this stroke width.
32static const SkScalar kMaxStrokeWidth = 20.0;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070033
34GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() {
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
bsalomon0aff2fa2015-07-31 06:48:27 -070039bool GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
40 if (!args.fAntiAlias) {
fmalitabd5d7e72015-06-26 07:18:24 -070041 return false;
42 }
bsalomon0aff2fa2015-07-31 06:48:27 -070043 if (args.fPath->isInverseFillType()) {
fmalitabd5d7e72015-06-26 07:18:24 -070044 return false;
45 }
bsalomon0aff2fa2015-07-31 06:48:27 -070046 if (!args.fPath->isConvex()) {
fmalitabd5d7e72015-06-26 07:18:24 -070047 return false;
48 }
bsalomon0aff2fa2015-07-31 06:48:27 -070049 if (args.fStroke->getStyle() == SkStrokeRec::kStroke_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070050 if (!args.fViewMatrix->isSimilarity()) {
51 return false;
52 }
53 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * args.fStroke->getWidth();
mtklein002c2ce2015-08-26 05:43:22 -070054 return strokeWidth >= 1.0f && strokeWidth <= kMaxStrokeWidth && !args.fStroke->isDashed() &&
ethannicholasc88cb892015-12-15 11:01:12 -080055 SkPathPriv::IsClosedSingleContour(*args.fPath) &&
bsalomon0aff2fa2015-07-31 06:48:27 -070056 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;
mtklein002c2ce2015-08-26 05:43:22 -070085 *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:
reed1b55a962015-09-17 20:16:13 -0700119 DEFINE_BATCH_CLASS_ID
120
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700121 struct Geometry {
122 GrColor fColor;
123 SkMatrix fViewMatrix;
124 SkPath fPath;
fmalitabd5d7e72015-06-26 07:18:24 -0700125 SkScalar fStrokeWidth;
126 SkPaint::Join fJoin;
127 SkScalar fMiterLimit;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700128 };
129
bsalomonabd30f52015-08-13 13:34:48 -0700130 static GrDrawBatch* Create(const Geometry& geometry) {
halcanary385fe4d2015-08-26 13:07:48 -0700131 return new AAFlatteningConvexPathBatch(geometry);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700132 }
133
134 const char* name() const override { return "AAConvexBatch"; }
135
halcanary9d524f22016-03-29 09:03:52 -0700136 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800137 GrInitInvariantOutput* coverage,
138 GrBatchToXPOverrides* overrides) const override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700139 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800140 color->setKnownFourComponents(fGeoData[0].fColor);
141 coverage->setUnknownSingleComponent();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700142 }
143
bsalomone46f9fe2015-08-18 06:05:14 -0700144private:
ethannicholasff210322015-11-24 12:10:10 -0800145 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700146 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -0800147 if (!overrides.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700148 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700149 }
ethannicholasff210322015-11-24 12:10:10 -0800150 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700151
152 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -0800153 fBatch.fColorIgnored = !overrides.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700154 fBatch.fColor = fGeoData[0].fColor;
ethannicholasff210322015-11-24 12:10:10 -0800155 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
156 fBatch.fCoverageIgnored = !overrides.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700157 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
ethannicholasff210322015-11-24 12:10:10 -0800158 fBatch.fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700159 }
160
bsalomon342bfc22016-04-01 06:06:20 -0700161 void draw(GrVertexBatch::Target* target, const GrGeometryProcessor* gp, int vertexCount,
joshualitt144c3c82015-11-30 12:30:13 -0800162 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700163 if (vertexCount == 0 || indexCount == 0) {
164 return;
165 }
cdalton397536c2016-03-25 12:15:03 -0700166 const GrBuffer* vertexBuffer;
egdaniel0e1853c2016-03-17 11:35:45 -0700167 GrMesh mesh;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700168 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700169 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
bsalomon75398562015-08-17 12:55:38 -0700170 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700171 if (!verts) {
172 SkDebugf("Could not allocate vertices\n");
173 return;
174 }
175 memcpy(verts, vertices, vertexCount * vertexStride);
176
cdalton397536c2016-03-25 12:15:03 -0700177 const GrBuffer* indexBuffer;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700178 int firstIndex;
bsalomon75398562015-08-17 12:55:38 -0700179 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700180 if (!idxs) {
181 SkDebugf("Could not allocate indices\n");
182 return;
183 }
184 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
egdaniel0e1853c2016-03-17 11:35:45 -0700185 mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
186 firstIndex, vertexCount, indexCount);
bsalomon342bfc22016-04-01 06:06:20 -0700187 target->draw(gp, mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700188 }
mtklein002c2ce2015-08-26 05:43:22 -0700189
joshualitt144c3c82015-11-30 12:30:13 -0800190 void onPrepareDraws(Target* target) const override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700191 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
192
joshualittdf0c5572015-08-03 11:35:28 -0700193 // Setup GrGeometryProcessor
194 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage,
195 this->viewMatrix(),
196 this->usesLocalCoords(),
197 this->coverageIgnored()));
198 if (!gp) {
199 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700200 return;
201 }
202
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700203 size_t vertexStride = gp->getVertexStride();
204
205 SkASSERT(canTweakAlphaForCoverage ?
206 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) :
207 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
208
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700209 int instanceCount = fGeoData.count();
210
211 int vertexCount = 0;
212 int indexCount = 0;
213 int maxVertices = DEFAULT_BUFFER_SIZE;
214 int maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700215 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
216 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700217 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800218 const Geometry& args = fGeoData[i];
fmalitabd5d7e72015-06-26 07:18:24 -0700219 GrAAConvexTessellator tess(args.fStrokeWidth, args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700220
221 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
222 continue;
223 }
224
225 int currentIndices = tess.numIndices();
226 SkASSERT(currentIndices <= UINT16_MAX);
227 if (indexCount + currentIndices > UINT16_MAX) {
mtklein002c2ce2015-08-26 05:43:22 -0700228 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700229 // uint16_t. Draw what we've got so far and reset.
bsalomon342bfc22016-04-01 06:06:20 -0700230 this->draw(target, gp, vertexCount, vertexStride, vertices, indexCount, indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700231 vertexCount = 0;
232 indexCount = 0;
233 }
234 int currentVertices = tess.numPts();
235 if (vertexCount + currentVertices > maxVertices) {
236 maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700237 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700238 }
239 if (indexCount + currentIndices > maxIndices) {
240 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700241 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700242 }
243
mtklein002c2ce2015-08-26 05:43:22 -0700244 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700245 vertexCount, indices + indexCount, canTweakAlphaForCoverage);
246 vertexCount += currentVertices;
247 indexCount += currentIndices;
248 }
bsalomon342bfc22016-04-01 06:06:20 -0700249 this->draw(target, gp, vertexCount, vertexStride, vertices, indexCount, indices);
mtklein002c2ce2015-08-26 05:43:22 -0700250 sk_free(vertices);
251 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700252 }
253
254 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
255
reed1b55a962015-09-17 20:16:13 -0700256 AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700257 fGeoData.push_back(geometry);
258
259 // compute bounds
260 fBounds = geometry.fPath.getBounds();
bsalomondb4758c2015-11-23 11:14:20 -0800261 SkScalar w = geometry.fStrokeWidth;
262 if (w > 0) {
263 w /= 2;
264 // If the miter limit is < 1 then we effectively fallback to bevel joins.
265 if (SkPaint::kMiter_Join == geometry.fJoin && w > 1.f) {
266 w *= geometry.fMiterLimit;
267 }
268 fBounds.outset(w, w);
269 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700270 geometry.fViewMatrix.mapRect(&fBounds);
271 }
272
bsalomoncb02b382015-08-12 11:14:50 -0700273 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700274 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
275 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
276 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700277 return false;
278 }
279
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700280 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
281 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
282 return false;
283 }
284
285 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
286 // not tweaking
287 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
288 fBatch.fCanTweakAlphaForCoverage = false;
289 }
290
291 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
292 this->joinBounds(that->bounds());
293 return true;
294 }
295
296 GrColor color() const { return fBatch.fColor; }
297 bool linesOnly() const { return fBatch.fLinesOnly; }
298 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
299 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
300 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
301 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
302
303 struct BatchTracker {
304 GrColor fColor;
305 bool fUsesLocalCoords;
306 bool fColorIgnored;
307 bool fCoverageIgnored;
308 bool fLinesOnly;
309 bool fCanTweakAlphaForCoverage;
310 };
311
312 BatchTracker fBatch;
313 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700314
315 typedef GrVertexBatch INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700316};
317
bsalomon0aff2fa2015-07-31 06:48:27 -0700318bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
joshualittde83b412016-01-14 09:58:36 -0800319 GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(),
320 "GrAALinearizingConvexPathRenderer::onDrawPath");
bsalomon0aff2fa2015-07-31 06:48:27 -0700321 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700322 return true;
323 }
324 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700325 geometry.fColor = args.fColor;
326 geometry.fViewMatrix = *args.fViewMatrix;
327 geometry.fPath = *args.fPath;
328 geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth();
329 geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join :
330 args.fStroke->getJoin();
331 geometry.fMiterLimit = args.fStroke->getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700332
bsalomonabd30f52015-08-13 13:34:48 -0700333 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700334 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700335
336 return true;
337}
338
339///////////////////////////////////////////////////////////////////////////////////////////////////
340
341#ifdef GR_TEST_UTILS
342
bsalomonabd30f52015-08-13 13:34:48 -0700343DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700344 AAFlatteningConvexPathBatch::Geometry geometry;
345 geometry.fColor = GrRandomColor(random);
346 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
347 geometry.fPath = GrTest::TestPathConvex(random);
348
349 return AAFlatteningConvexPathBatch::Create(geometry);
350}
351
352#endif