blob: 91d3338c32481fa41803f6d934604f0c2322dc6d [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"
bsalomon6663acf2016-05-10 09:14:17 -070020#include "GrStyle.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070021#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 }
bsalomon6663acf2016-05-10 09:14:17 -070049 if (args.fStyle->pathEffect()) {
50 return false;
51 }
52 const SkStrokeRec& stroke = args.fStyle->strokeRec();
53 if (stroke.getStyle() == SkStrokeRec::kStroke_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070054 if (!args.fViewMatrix->isSimilarity()) {
55 return false;
56 }
bsalomon6663acf2016-05-10 09:14:17 -070057 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * stroke.getWidth();
58 return strokeWidth >= 1.0f && strokeWidth <= kMaxStrokeWidth &&
59 SkPathPriv::IsClosedSingleContour(*args.fPath) &&
60 stroke.getJoin() != SkPaint::Join::kRound_Join;
fmalitabd5d7e72015-06-26 07:18:24 -070061 }
bsalomon6663acf2016-05-10 09:14:17 -070062 return stroke.getStyle() == SkStrokeRec::kFill_Style;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070063}
64
65// extract the result vertices and indices from the GrAAConvexTessellator
66static void extract_verts(const GrAAConvexTessellator& tess,
67 void* vertices,
68 size_t vertexStride,
69 GrColor color,
70 uint16_t firstIndex,
71 uint16_t* idxs,
72 bool tweakAlphaForCoverage) {
73 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
74
75 for (int i = 0; i < tess.numPts(); ++i) {
76 *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i);
77 }
78
79 // Make 'verts' point to the colors
80 verts += sizeof(SkPoint);
81 for (int i = 0; i < tess.numPts(); ++i) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -070082 if (tweakAlphaForCoverage) {
fmalitabd5d7e72015-06-26 07:18:24 -070083 SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255);
84 unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070085 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
86 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
87 } else {
88 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
mtklein002c2ce2015-08-26 05:43:22 -070089 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) =
fmalitabd5d7e72015-06-26 07:18:24 -070090 tess.coverage(i);
ethannicholas1a1b3ac2015-06-10 12:11:17 -070091 }
92 }
93
94 for (int i = 0; i < tess.numIndices(); ++i) {
95 idxs[i] = tess.index(i) + firstIndex;
96 }
97}
98
99static const GrGeometryProcessor* create_fill_gp(bool tweakAlphaForCoverage,
joshualittdf0c5572015-08-03 11:35:28 -0700100 const SkMatrix& viewMatrix,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700101 bool usesLocalCoords,
102 bool coverageIgnored) {
joshualittdf0c5572015-08-03 11:35:28 -0700103 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700104
joshualittdf0c5572015-08-03 11:35:28 -0700105 Color color(Color::kAttribute_Type);
106 Coverage::Type coverageType;
107 // TODO remove coverage if coverage is ignored
108 /*if (coverageIgnored) {
109 coverageType = Coverage::kNone_Type;
110 } else*/ if (tweakAlphaForCoverage) {
111 coverageType = Coverage::kSolid_Type;
112 } else {
113 coverageType = Coverage::kAttribute_Type;
114 }
115 Coverage coverage(coverageType);
116 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type :
117 LocalCoords::kUnused_Type);
118 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700119}
120
bsalomonabd30f52015-08-13 13:34:48 -0700121class AAFlatteningConvexPathBatch : public GrVertexBatch {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700122public:
reed1b55a962015-09-17 20:16:13 -0700123 DEFINE_BATCH_CLASS_ID
124
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700125 struct Geometry {
126 GrColor fColor;
127 SkMatrix fViewMatrix;
128 SkPath fPath;
fmalitabd5d7e72015-06-26 07:18:24 -0700129 SkScalar fStrokeWidth;
130 SkPaint::Join fJoin;
131 SkScalar fMiterLimit;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700132 };
133
bsalomonabd30f52015-08-13 13:34:48 -0700134 static GrDrawBatch* Create(const Geometry& geometry) {
halcanary385fe4d2015-08-26 13:07:48 -0700135 return new AAFlatteningConvexPathBatch(geometry);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700136 }
137
138 const char* name() const override { return "AAConvexBatch"; }
139
halcanary9d524f22016-03-29 09:03:52 -0700140 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800141 GrInitInvariantOutput* coverage,
142 GrBatchToXPOverrides* overrides) const override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700143 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800144 color->setKnownFourComponents(fGeoData[0].fColor);
145 coverage->setUnknownSingleComponent();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700146 }
147
bsalomone46f9fe2015-08-18 06:05:14 -0700148private:
ethannicholasff210322015-11-24 12:10:10 -0800149 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700150 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -0800151 if (!overrides.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700152 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700153 }
ethannicholasff210322015-11-24 12:10:10 -0800154 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700155
156 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -0800157 fBatch.fColorIgnored = !overrides.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700158 fBatch.fColor = fGeoData[0].fColor;
ethannicholasff210322015-11-24 12:10:10 -0800159 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
160 fBatch.fCoverageIgnored = !overrides.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700161 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
ethannicholasff210322015-11-24 12:10:10 -0800162 fBatch.fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700163 }
164
bsalomon342bfc22016-04-01 06:06:20 -0700165 void draw(GrVertexBatch::Target* target, const GrGeometryProcessor* gp, int vertexCount,
joshualitt144c3c82015-11-30 12:30:13 -0800166 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700167 if (vertexCount == 0 || indexCount == 0) {
168 return;
169 }
cdalton397536c2016-03-25 12:15:03 -0700170 const GrBuffer* vertexBuffer;
egdaniel0e1853c2016-03-17 11:35:45 -0700171 GrMesh mesh;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700172 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700173 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
bsalomon75398562015-08-17 12:55:38 -0700174 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700175 if (!verts) {
176 SkDebugf("Could not allocate vertices\n");
177 return;
178 }
179 memcpy(verts, vertices, vertexCount * vertexStride);
180
cdalton397536c2016-03-25 12:15:03 -0700181 const GrBuffer* indexBuffer;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700182 int firstIndex;
bsalomon75398562015-08-17 12:55:38 -0700183 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700184 if (!idxs) {
185 SkDebugf("Could not allocate indices\n");
186 return;
187 }
188 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
egdaniel0e1853c2016-03-17 11:35:45 -0700189 mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
190 firstIndex, vertexCount, indexCount);
bsalomon342bfc22016-04-01 06:06:20 -0700191 target->draw(gp, mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700192 }
mtklein002c2ce2015-08-26 05:43:22 -0700193
joshualitt144c3c82015-11-30 12:30:13 -0800194 void onPrepareDraws(Target* target) const override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700195 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
196
joshualittdf0c5572015-08-03 11:35:28 -0700197 // Setup GrGeometryProcessor
198 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage,
199 this->viewMatrix(),
200 this->usesLocalCoords(),
201 this->coverageIgnored()));
202 if (!gp) {
203 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700204 return;
205 }
206
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700207 size_t vertexStride = gp->getVertexStride();
208
209 SkASSERT(canTweakAlphaForCoverage ?
210 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) :
211 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
212
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700213 int instanceCount = fGeoData.count();
214
215 int vertexCount = 0;
216 int indexCount = 0;
217 int maxVertices = DEFAULT_BUFFER_SIZE;
218 int maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700219 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
220 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700221 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800222 const Geometry& args = fGeoData[i];
fmalitabd5d7e72015-06-26 07:18:24 -0700223 GrAAConvexTessellator tess(args.fStrokeWidth, args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700224
225 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
226 continue;
227 }
228
229 int currentIndices = tess.numIndices();
230 SkASSERT(currentIndices <= UINT16_MAX);
231 if (indexCount + currentIndices > UINT16_MAX) {
mtklein002c2ce2015-08-26 05:43:22 -0700232 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700233 // uint16_t. Draw what we've got so far and reset.
bsalomon342bfc22016-04-01 06:06:20 -0700234 this->draw(target, gp, vertexCount, vertexStride, vertices, 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 }
bsalomon342bfc22016-04-01 06:06:20 -0700253 this->draw(target, gp, vertexCount, vertexStride, vertices, indexCount, indices);
mtklein002c2ce2015-08-26 05:43:22 -0700254 sk_free(vertices);
255 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700256 }
257
258 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
259
reed1b55a962015-09-17 20:16:13 -0700260 AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700261 fGeoData.push_back(geometry);
262
263 // compute bounds
264 fBounds = geometry.fPath.getBounds();
bsalomondb4758c2015-11-23 11:14:20 -0800265 SkScalar w = geometry.fStrokeWidth;
266 if (w > 0) {
267 w /= 2;
268 // If the miter limit is < 1 then we effectively fallback to bevel joins.
269 if (SkPaint::kMiter_Join == geometry.fJoin && w > 1.f) {
270 w *= geometry.fMiterLimit;
271 }
272 fBounds.outset(w, w);
273 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700274 geometry.fViewMatrix.mapRect(&fBounds);
275 }
276
bsalomoncb02b382015-08-12 11:14:50 -0700277 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700278 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
279 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
280 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700281 return false;
282 }
283
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700284 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
285 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
286 return false;
287 }
288
289 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
290 // not tweaking
291 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
292 fBatch.fCanTweakAlphaForCoverage = false;
293 }
294
295 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
296 this->joinBounds(that->bounds());
297 return true;
298 }
299
300 GrColor color() const { return fBatch.fColor; }
301 bool linesOnly() const { return fBatch.fLinesOnly; }
302 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
303 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
304 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
305 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
306
307 struct BatchTracker {
308 GrColor fColor;
309 bool fUsesLocalCoords;
310 bool fColorIgnored;
311 bool fCoverageIgnored;
312 bool fLinesOnly;
313 bool fCanTweakAlphaForCoverage;
314 };
315
316 BatchTracker fBatch;
317 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700318
319 typedef GrVertexBatch INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700320};
321
bsalomon0aff2fa2015-07-31 06:48:27 -0700322bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
joshualittde83b412016-01-14 09:58:36 -0800323 GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(),
324 "GrAALinearizingConvexPathRenderer::onDrawPath");
bsalomon0aff2fa2015-07-31 06:48:27 -0700325 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700326 return true;
327 }
328 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700329 geometry.fColor = args.fColor;
330 geometry.fViewMatrix = *args.fViewMatrix;
331 geometry.fPath = *args.fPath;
bsalomon6663acf2016-05-10 09:14:17 -0700332 bool fill = args.fStyle->isSimpleFill();
333 geometry.fStrokeWidth = fill ? -1.0f : args.fStyle->strokeRec().getWidth();
334 geometry.fJoin = fill ? SkPaint::Join::kMiter_Join : args.fStyle->strokeRec().getJoin();
335 geometry.fMiterLimit = args.fStyle->strokeRec().getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700336
bsalomonabd30f52015-08-13 13:34:48 -0700337 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700338 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700339
340 return true;
341}
342
343///////////////////////////////////////////////////////////////////////////////////////////////////
344
345#ifdef GR_TEST_UTILS
346
bsalomonabd30f52015-08-13 13:34:48 -0700347DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700348 AAFlatteningConvexPathBatch::Geometry geometry;
349 geometry.fColor = GrRandomColor(random);
350 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
351 geometry.fPath = GrTest::TestPathConvex(random);
352
353 return AAFlatteningConvexPathBatch::Create(geometry);
354}
355
356#endif