blob: a7e98249e216b77327c8b0dec48ecf162497ef48 [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();
mtklein002c2ce2015-08-26 05:43:22 -070057 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;
mtklein002c2ce2015-08-26 05:43:22 -070088 *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:
reed1b55a962015-09-17 20:16:13 -0700122 DEFINE_BATCH_CLASS_ID
123
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700124 struct Geometry {
125 GrColor fColor;
126 SkMatrix fViewMatrix;
127 SkPath fPath;
fmalitabd5d7e72015-06-26 07:18:24 -0700128 SkScalar fStrokeWidth;
129 SkPaint::Join fJoin;
130 SkScalar fMiterLimit;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700131 };
132
bsalomonabd30f52015-08-13 13:34:48 -0700133 static GrDrawBatch* Create(const Geometry& geometry) {
halcanary385fe4d2015-08-26 13:07:48 -0700134 return new AAFlatteningConvexPathBatch(geometry);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700135 }
136
137 const char* name() const override { return "AAConvexBatch"; }
138
139 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
140 // When this is called on a batch, there is only one geometry bundle
141 out->setKnownFourComponents(fGeoData[0].fColor);
142 }
143 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
144 out->setUnknownSingleComponent();
145 }
146
bsalomone46f9fe2015-08-18 06:05:14 -0700147private:
bsalomon91d844d2015-08-10 10:47:29 -0700148 void initBatchTracker(const GrPipelineOptimizations& opt) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700149 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700150 if (!opt.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700151 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700152 }
bsalomon91d844d2015-08-10 10:47:29 -0700153 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700154
155 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700156 fBatch.fColorIgnored = !opt.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700157 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -0700158 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
159 fBatch.fCoverageIgnored = !opt.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700160 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
bsalomon91d844d2015-08-10 10:47:29 -0700161 fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700162 }
163
mtklein002c2ce2015-08-26 05:43:22 -0700164 void draw(GrVertexBatch::Target* target, const GrPipeline* pipeline, int vertexCount,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700165 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) {
166 if (vertexCount == 0 || indexCount == 0) {
167 return;
168 }
169 const GrVertexBuffer* vertexBuffer;
170 GrVertices info;
171 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700172 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
bsalomon75398562015-08-17 12:55:38 -0700173 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700174 if (!verts) {
175 SkDebugf("Could not allocate vertices\n");
176 return;
177 }
178 memcpy(verts, vertices, vertexCount * vertexStride);
179
180 const GrIndexBuffer* indexBuffer;
181 int firstIndex;
bsalomon75398562015-08-17 12:55:38 -0700182 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700183 if (!idxs) {
184 SkDebugf("Could not allocate indices\n");
185 return;
186 }
187 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
mtklein002c2ce2015-08-26 05:43:22 -0700188 info.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700189 firstIndex, vertexCount, indexCount);
bsalomon75398562015-08-17 12:55:38 -0700190 target->draw(info);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700191 }
mtklein002c2ce2015-08-26 05:43:22 -0700192
bsalomon75398562015-08-17 12:55:38 -0700193 void onPrepareDraws(Target* target) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700194 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
195
joshualittdf0c5572015-08-03 11:35:28 -0700196 // Setup GrGeometryProcessor
197 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage,
198 this->viewMatrix(),
199 this->usesLocalCoords(),
200 this->coverageIgnored()));
201 if (!gp) {
202 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700203 return;
204 }
205
bsalomon75398562015-08-17 12:55:38 -0700206 target->initDraw(gp, this->pipeline());
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700207
208 size_t vertexStride = gp->getVertexStride();
209
210 SkASSERT(canTweakAlphaForCoverage ?
211 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) :
212 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
213
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700214 int instanceCount = fGeoData.count();
215
216 int vertexCount = 0;
217 int indexCount = 0;
218 int maxVertices = DEFAULT_BUFFER_SIZE;
219 int maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700220 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
221 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700222 for (int i = 0; i < instanceCount; i++) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700223 Geometry& args = fGeoData[i];
fmalitabd5d7e72015-06-26 07:18:24 -0700224 GrAAConvexTessellator tess(args.fStrokeWidth, args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700225
226 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
227 continue;
228 }
229
230 int currentIndices = tess.numIndices();
231 SkASSERT(currentIndices <= UINT16_MAX);
232 if (indexCount + currentIndices > UINT16_MAX) {
mtklein002c2ce2015-08-26 05:43:22 -0700233 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700234 // uint16_t. Draw what we've got so far and reset.
mtklein002c2ce2015-08-26 05:43:22 -0700235 draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
bsalomon75398562015-08-17 12:55:38 -0700236 indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700237 vertexCount = 0;
238 indexCount = 0;
239 }
240 int currentVertices = tess.numPts();
241 if (vertexCount + currentVertices > maxVertices) {
242 maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700243 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700244 }
245 if (indexCount + currentIndices > maxIndices) {
246 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700247 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700248 }
249
mtklein002c2ce2015-08-26 05:43:22 -0700250 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700251 vertexCount, indices + indexCount, canTweakAlphaForCoverage);
252 vertexCount += currentVertices;
253 indexCount += currentIndices;
254 }
bsalomon75398562015-08-17 12:55:38 -0700255 draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
bsalomonfb1141a2015-08-06 08:52:49 -0700256 indices);
mtklein002c2ce2015-08-26 05:43:22 -0700257 sk_free(vertices);
258 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700259 }
260
261 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
262
reed1b55a962015-09-17 20:16:13 -0700263 AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700264 fGeoData.push_back(geometry);
265
266 // compute bounds
267 fBounds = geometry.fPath.getBounds();
268 geometry.fViewMatrix.mapRect(&fBounds);
269 }
270
bsalomoncb02b382015-08-12 11:14:50 -0700271 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700272 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
273 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
274 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700275 return false;
276 }
277
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700278 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
279 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
280 return false;
281 }
282
283 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
284 // not tweaking
285 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
286 fBatch.fCanTweakAlphaForCoverage = false;
287 }
288
289 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
290 this->joinBounds(that->bounds());
291 return true;
292 }
293
294 GrColor color() const { return fBatch.fColor; }
295 bool linesOnly() const { return fBatch.fLinesOnly; }
296 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
297 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
298 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
299 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
300
301 struct BatchTracker {
302 GrColor fColor;
303 bool fUsesLocalCoords;
304 bool fColorIgnored;
305 bool fCoverageIgnored;
306 bool fLinesOnly;
307 bool fCanTweakAlphaForCoverage;
308 };
309
310 BatchTracker fBatch;
311 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700312
313 typedef GrVertexBatch INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700314};
315
bsalomon0aff2fa2015-07-31 06:48:27 -0700316bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
317 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700318 return true;
319 }
320 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700321 geometry.fColor = args.fColor;
322 geometry.fViewMatrix = *args.fViewMatrix;
323 geometry.fPath = *args.fPath;
324 geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth();
325 geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join :
326 args.fStroke->getJoin();
327 geometry.fMiterLimit = args.fStroke->getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700328
bsalomonabd30f52015-08-13 13:34:48 -0700329 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700330 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700331
332 return true;
333}
334
335///////////////////////////////////////////////////////////////////////////////////////////////////
336
337#ifdef GR_TEST_UTILS
338
bsalomonabd30f52015-08-13 13:34:48 -0700339DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700340 AAFlatteningConvexPathBatch::Geometry geometry;
341 geometry.fColor = GrRandomColor(random);
342 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
343 geometry.fPath = GrTest::TestPathConvex(random);
344
345 return AAFlatteningConvexPathBatch::Create(geometry);
346}
347
348#endif