blob: 4585c10214e65539d6d7fc536d52bf80c2b819b3 [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/GrGLGeometryProcessor.h"
28#include "gl/builders/GrGLProgramBuilder.h"
29
fmalitabd5d7e72015-06-26 07:18:24 -070030static const int DEFAULT_BUFFER_SIZE = 100;
31
32// The thicker the stroke, the harder it is to produce high-quality results using tessellation. For
33// the time being, we simply drop back to software rendering above this stroke width.
34static const SkScalar kMaxStrokeWidth = 20.0;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070035
36GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() {
37}
38
39///////////////////////////////////////////////////////////////////////////////
40
bsalomon0aff2fa2015-07-31 06:48:27 -070041bool GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
42 if (!args.fAntiAlias) {
fmalitabd5d7e72015-06-26 07:18:24 -070043 return false;
44 }
bsalomon0aff2fa2015-07-31 06:48:27 -070045 if (args.fPath->isInverseFillType()) {
fmalitabd5d7e72015-06-26 07:18:24 -070046 return false;
47 }
bsalomon0aff2fa2015-07-31 06:48:27 -070048 if (!args.fPath->isConvex()) {
fmalitabd5d7e72015-06-26 07:18:24 -070049 return false;
50 }
bsalomon0aff2fa2015-07-31 06:48:27 -070051 if (args.fStroke->getStyle() == SkStrokeRec::kStroke_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070052 if (!args.fViewMatrix->isSimilarity()) {
53 return false;
54 }
55 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * args.fStroke->getWidth();
mtklein002c2ce2015-08-26 05:43:22 -070056 return strokeWidth >= 1.0f && strokeWidth <= kMaxStrokeWidth && !args.fStroke->isDashed() &&
bsalomon0aff2fa2015-07-31 06:48:27 -070057 SkPathPriv::LastVerbIsClose(*args.fPath) &&
58 args.fStroke->getJoin() != SkPaint::Join::kRound_Join;
fmalitabd5d7e72015-06-26 07:18:24 -070059 }
bsalomon0aff2fa2015-07-31 06:48:27 -070060 return args.fStroke->getStyle() == SkStrokeRec::kFill_Style;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070061}
62
63// extract the result vertices and indices from the GrAAConvexTessellator
64static void extract_verts(const GrAAConvexTessellator& tess,
65 void* vertices,
66 size_t vertexStride,
67 GrColor color,
68 uint16_t firstIndex,
69 uint16_t* idxs,
70 bool tweakAlphaForCoverage) {
71 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
72
73 for (int i = 0; i < tess.numPts(); ++i) {
74 *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i);
75 }
76
77 // Make 'verts' point to the colors
78 verts += sizeof(SkPoint);
79 for (int i = 0; i < tess.numPts(); ++i) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -070080 if (tweakAlphaForCoverage) {
fmalitabd5d7e72015-06-26 07:18:24 -070081 SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255);
82 unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070083 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
84 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
85 } else {
86 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
mtklein002c2ce2015-08-26 05:43:22 -070087 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) =
fmalitabd5d7e72015-06-26 07:18:24 -070088 tess.coverage(i);
ethannicholas1a1b3ac2015-06-10 12:11:17 -070089 }
90 }
91
92 for (int i = 0; i < tess.numIndices(); ++i) {
93 idxs[i] = tess.index(i) + firstIndex;
94 }
95}
96
97static const GrGeometryProcessor* create_fill_gp(bool tweakAlphaForCoverage,
joshualittdf0c5572015-08-03 11:35:28 -070098 const SkMatrix& viewMatrix,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070099 bool usesLocalCoords,
100 bool coverageIgnored) {
joshualittdf0c5572015-08-03 11:35:28 -0700101 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700102
joshualittdf0c5572015-08-03 11:35:28 -0700103 Color color(Color::kAttribute_Type);
104 Coverage::Type coverageType;
105 // TODO remove coverage if coverage is ignored
106 /*if (coverageIgnored) {
107 coverageType = Coverage::kNone_Type;
108 } else*/ if (tweakAlphaForCoverage) {
109 coverageType = Coverage::kSolid_Type;
110 } else {
111 coverageType = Coverage::kAttribute_Type;
112 }
113 Coverage coverage(coverageType);
114 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type :
115 LocalCoords::kUnused_Type);
116 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700117}
118
bsalomonabd30f52015-08-13 13:34:48 -0700119class AAFlatteningConvexPathBatch : public GrVertexBatch {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700120public:
reed1b55a962015-09-17 20:16:13 -0700121 DEFINE_BATCH_CLASS_ID
122
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700123 struct Geometry {
124 GrColor fColor;
125 SkMatrix fViewMatrix;
126 SkPath fPath;
fmalitabd5d7e72015-06-26 07:18:24 -0700127 SkScalar fStrokeWidth;
128 SkPaint::Join fJoin;
129 SkScalar fMiterLimit;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700130 };
131
bsalomonabd30f52015-08-13 13:34:48 -0700132 static GrDrawBatch* Create(const Geometry& geometry) {
halcanary385fe4d2015-08-26 13:07:48 -0700133 return new AAFlatteningConvexPathBatch(geometry);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700134 }
135
136 const char* name() const override { return "AAConvexBatch"; }
137
138 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
139 // When this is called on a batch, there is only one geometry bundle
140 out->setKnownFourComponents(fGeoData[0].fColor);
141 }
142 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
143 out->setUnknownSingleComponent();
144 }
145
bsalomone46f9fe2015-08-18 06:05:14 -0700146private:
bsalomon91d844d2015-08-10 10:47:29 -0700147 void initBatchTracker(const GrPipelineOptimizations& opt) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700148 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700149 if (!opt.readsColor()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700150 fGeoData[0].fColor = GrColor_ILLEGAL;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700151 }
bsalomon91d844d2015-08-10 10:47:29 -0700152 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700153
154 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700155 fBatch.fColorIgnored = !opt.readsColor();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700156 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -0700157 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
158 fBatch.fCoverageIgnored = !opt.readsCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700159 fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks();
bsalomon91d844d2015-08-10 10:47:29 -0700160 fBatch.fCanTweakAlphaForCoverage = opt.canTweakAlphaForCoverage();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700161 }
162
mtklein002c2ce2015-08-26 05:43:22 -0700163 void draw(GrVertexBatch::Target* target, const GrPipeline* pipeline, int vertexCount,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700164 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) {
165 if (vertexCount == 0 || indexCount == 0) {
166 return;
167 }
168 const GrVertexBuffer* vertexBuffer;
169 GrVertices info;
170 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700171 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
bsalomon75398562015-08-17 12:55:38 -0700172 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700173 if (!verts) {
174 SkDebugf("Could not allocate vertices\n");
175 return;
176 }
177 memcpy(verts, vertices, vertexCount * vertexStride);
178
179 const GrIndexBuffer* indexBuffer;
180 int firstIndex;
bsalomon75398562015-08-17 12:55:38 -0700181 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700182 if (!idxs) {
183 SkDebugf("Could not allocate indices\n");
184 return;
185 }
186 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
mtklein002c2ce2015-08-26 05:43:22 -0700187 info.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700188 firstIndex, vertexCount, indexCount);
bsalomon75398562015-08-17 12:55:38 -0700189 target->draw(info);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700190 }
mtklein002c2ce2015-08-26 05:43:22 -0700191
bsalomon75398562015-08-17 12:55:38 -0700192 void onPrepareDraws(Target* target) override {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700193 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
194
joshualittdf0c5572015-08-03 11:35:28 -0700195 // Setup GrGeometryProcessor
196 SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage,
197 this->viewMatrix(),
198 this->usesLocalCoords(),
199 this->coverageIgnored()));
200 if (!gp) {
201 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700202 return;
203 }
204
bsalomon75398562015-08-17 12:55:38 -0700205 target->initDraw(gp, this->pipeline());
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700206
207 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++) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700222 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.
mtklein002c2ce2015-08-26 05:43:22 -0700234 draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
bsalomon75398562015-08-17 12:55:38 -0700235 indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700236 vertexCount = 0;
237 indexCount = 0;
238 }
239 int currentVertices = tess.numPts();
240 if (vertexCount + currentVertices > maxVertices) {
241 maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700242 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700243 }
244 if (indexCount + currentIndices > maxIndices) {
245 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
mtklein002c2ce2015-08-26 05:43:22 -0700246 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700247 }
248
mtklein002c2ce2015-08-26 05:43:22 -0700249 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700250 vertexCount, indices + indexCount, canTweakAlphaForCoverage);
251 vertexCount += currentVertices;
252 indexCount += currentIndices;
253 }
bsalomon75398562015-08-17 12:55:38 -0700254 draw(target, this->pipeline(), vertexCount, vertexStride, vertices, indexCount,
bsalomonfb1141a2015-08-06 08:52:49 -0700255 indices);
mtklein002c2ce2015-08-26 05:43:22 -0700256 sk_free(vertices);
257 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700258 }
259
260 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
261
reed1b55a962015-09-17 20:16:13 -0700262 AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700263 fGeoData.push_back(geometry);
264
265 // compute bounds
266 fBounds = geometry.fPath.getBounds();
267 geometry.fViewMatrix.mapRect(&fBounds);
268 }
269
bsalomoncb02b382015-08-12 11:14:50 -0700270 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700271 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>();
272 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
273 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700274 return false;
275 }
276
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700277 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
278 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
279 return false;
280 }
281
282 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
283 // not tweaking
284 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
285 fBatch.fCanTweakAlphaForCoverage = false;
286 }
287
288 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
289 this->joinBounds(that->bounds());
290 return true;
291 }
292
293 GrColor color() const { return fBatch.fColor; }
294 bool linesOnly() const { return fBatch.fLinesOnly; }
295 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
296 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; }
297 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
298 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
299
300 struct BatchTracker {
301 GrColor fColor;
302 bool fUsesLocalCoords;
303 bool fColorIgnored;
304 bool fCoverageIgnored;
305 bool fLinesOnly;
306 bool fCanTweakAlphaForCoverage;
307 };
308
309 BatchTracker fBatch;
310 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700311
312 typedef GrVertexBatch INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700313};
314
bsalomon0aff2fa2015-07-31 06:48:27 -0700315bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
316 if (args.fPath->isEmpty()) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700317 return true;
318 }
319 AAFlatteningConvexPathBatch::Geometry geometry;
bsalomon0aff2fa2015-07-31 06:48:27 -0700320 geometry.fColor = args.fColor;
321 geometry.fViewMatrix = *args.fViewMatrix;
322 geometry.fPath = *args.fPath;
323 geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth();
324 geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join :
325 args.fStroke->getJoin();
326 geometry.fMiterLimit = args.fStroke->getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700327
bsalomonabd30f52015-08-13 13:34:48 -0700328 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry));
bsalomon0aff2fa2015-07-31 06:48:27 -0700329 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700330
331 return true;
332}
333
334///////////////////////////////////////////////////////////////////////////////////////////////////
335
336#ifdef GR_TEST_UTILS
337
bsalomonabd30f52015-08-13 13:34:48 -0700338DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700339 AAFlatteningConvexPathBatch::Geometry geometry;
340 geometry.fColor = GrRandomColor(random);
341 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
342 geometry.fPath = GrTest::TestPathConvex(random);
343
344 return AAFlatteningConvexPathBatch::Create(geometry);
345}
346
347#endif