| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrAALinearizingConvexPathRenderer.h" | 8 #include "GrAALinearizingConvexPathRenderer.h" |
| 9 | 9 |
| 10 #include "GrAAConvexTessellator.h" | 10 #include "GrAAConvexTessellator.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 Coverage coverage(coverageType); | 115 Coverage coverage(coverageType); |
| 116 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : | 116 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : |
| 117 LocalCoords::kUnused_Type); | 117 LocalCoords::kUnused_Type); |
| 118 return MakeForDeviceSpace(color, coverage, localCoords, viewMatrix); | 118 return MakeForDeviceSpace(color, coverage, localCoords, viewMatrix); |
| 119 } | 119 } |
| 120 | 120 |
| 121 class AAFlatteningConvexPathBatch : public GrVertexBatch { | 121 class AAFlatteningConvexPathBatch : public GrVertexBatch { |
| 122 public: | 122 public: |
| 123 DEFINE_BATCH_CLASS_ID | 123 DEFINE_BATCH_CLASS_ID |
| 124 | 124 |
| 125 struct Geometry { | 125 AAFlatteningConvexPathBatch(GrColor color, |
| 126 GrColor fColor; | 126 const SkMatrix& viewMatrix, |
| 127 SkMatrix fViewMatrix; | 127 const SkPath& path, |
| 128 SkPath fPath; | 128 SkScalar strokeWidth, |
| 129 SkScalar fStrokeWidth; | 129 SkPaint::Join join, |
| 130 SkPaint::Join fJoin; | 130 SkScalar miterLimit) : INHERITED(ClassID()) { |
| 131 SkScalar fMiterLimit; | 131 fGeoData.emplace_back(Geometry{color, viewMatrix, path, strokeWidth, joi
n, miterLimit}); |
| 132 }; | |
| 133 | 132 |
| 134 static GrDrawBatch* Create(const Geometry& geometry) { | 133 // compute bounds |
| 135 return new AAFlatteningConvexPathBatch(geometry); | 134 fBounds = path.getBounds(); |
| 135 SkScalar w = strokeWidth; |
| 136 if (w > 0) { |
| 137 w /= 2; |
| 138 // If the half stroke width is < 1 then we effectively fallback to b
evel joins. |
| 139 if (SkPaint::kMiter_Join == join && w > 1.f) { |
| 140 w *= miterLimit; |
| 141 } |
| 142 fBounds.outset(w, w); |
| 143 } |
| 144 viewMatrix.mapRect(&fBounds); |
| 136 } | 145 } |
| 137 | 146 |
| 138 const char* name() const override { return "AAConvexBatch"; } | 147 const char* name() const override { return "AAConvexBatch"; } |
| 139 | 148 |
| 140 void computePipelineOptimizations(GrInitInvariantOutput* color, | 149 void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 141 GrInitInvariantOutput* coverage, | 150 GrInitInvariantOutput* coverage, |
| 142 GrBatchToXPOverrides* overrides) const ove
rride { | 151 GrBatchToXPOverrides* overrides) const ove
rride { |
| 143 // When this is called on a batch, there is only one geometry bundle | 152 // When this is called on a batch, there is only one geometry bundle |
| 144 color->setKnownFourComponents(fGeoData[0].fColor); | 153 color->setKnownFourComponents(fGeoData[0].fColor); |
| 145 coverage->setUnknownSingleComponent(); | 154 coverage->setUnknownSingleComponent(); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStr
ide, args.fColor, | 258 extract_verts(tess, vertices + vertexStride * vertexCount, vertexStr
ide, args.fColor, |
| 250 vertexCount, indices + indexCount, canTweakAlphaForCoverage)
; | 259 vertexCount, indices + indexCount, canTweakAlphaForCoverage)
; |
| 251 vertexCount += currentVertices; | 260 vertexCount += currentVertices; |
| 252 indexCount += currentIndices; | 261 indexCount += currentIndices; |
| 253 } | 262 } |
| 254 this->draw(target, gp.get(), vertexCount, vertexStride, vertices, indexC
ount, indices); | 263 this->draw(target, gp.get(), vertexCount, vertexStride, vertices, indexC
ount, indices); |
| 255 sk_free(vertices); | 264 sk_free(vertices); |
| 256 sk_free(indices); | 265 sk_free(indices); |
| 257 } | 266 } |
| 258 | 267 |
| 259 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | |
| 260 | |
| 261 AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID())
{ | |
| 262 fGeoData.push_back(geometry); | |
| 263 | |
| 264 // compute bounds | |
| 265 fBounds = geometry.fPath.getBounds(); | |
| 266 SkScalar w = geometry.fStrokeWidth; | |
| 267 if (w > 0) { | |
| 268 w /= 2; | |
| 269 // If the miter limit is < 1 then we effectively fallback to bevel j
oins. | |
| 270 if (SkPaint::kMiter_Join == geometry.fJoin && w > 1.f) { | |
| 271 w *= geometry.fMiterLimit; | |
| 272 } | |
| 273 fBounds.outset(w, w); | |
| 274 } | |
| 275 geometry.fViewMatrix.mapRect(&fBounds); | |
| 276 } | |
| 277 | |
| 278 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 268 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 279 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>
(); | 269 AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>
(); |
| 280 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), | 270 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
| 281 that->bounds(), caps)) { | 271 that->bounds(), caps)) { |
| 282 return false; | 272 return false; |
| 283 } | 273 } |
| 284 | 274 |
| 285 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); | 275 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 286 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->vi
ewMatrix())) { | 276 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->vi
ewMatrix())) { |
| 287 return false; | 277 return false; |
| 288 } | 278 } |
| 289 | 279 |
| 290 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to | 280 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to |
| 291 // not tweaking | 281 // not tweaking |
| 292 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()
) { | 282 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()
) { |
| 293 fBatch.fCanTweakAlphaForCoverage = false; | 283 fBatch.fCanTweakAlphaForCoverage = false; |
| 294 } | 284 } |
| 295 | 285 |
| 296 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; | 286 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
| 297 this->joinBounds(that->bounds()); | 287 this->joinBounds(that->bounds()); |
| 298 return true; | 288 return true; |
| 299 } | 289 } |
| 300 | 290 |
| 301 GrColor color() const { return fBatch.fColor; } | 291 GrColor color() const { return fBatch.fColor; } |
| 302 bool linesOnly() const { return fBatch.fLinesOnly; } | 292 bool linesOnly() const { return fBatch.fLinesOnly; } |
| 303 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | 293 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 304 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCover
age; } | 294 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCover
age; } |
| 305 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } | 295 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 306 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } | 296 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 307 | 297 |
| 308 struct BatchTracker { | 298 struct BatchTracker { |
| 309 GrColor fColor; | 299 GrColor fColor; |
| 310 bool fUsesLocalCoords; | 300 bool fUsesLocalCoords; |
| 311 bool fColorIgnored; | 301 bool fColorIgnored; |
| 312 bool fCoverageIgnored; | 302 bool fCoverageIgnored; |
| 313 bool fLinesOnly; | 303 bool fLinesOnly; |
| 314 bool fCanTweakAlphaForCoverage; | 304 bool fCanTweakAlphaForCoverage; |
| 315 }; | 305 }; |
| 316 | 306 |
| 307 struct Geometry { |
| 308 GrColor fColor; |
| 309 SkMatrix fViewMatrix; |
| 310 SkPath fPath; |
| 311 SkScalar fStrokeWidth; |
| 312 SkPaint::Join fJoin; |
| 313 SkScalar fMiterLimit; |
| 314 }; |
| 315 |
| 317 BatchTracker fBatch; | 316 BatchTracker fBatch; |
| 318 SkSTArray<1, Geometry, true> fGeoData; | 317 SkSTArray<1, Geometry, true> fGeoData; |
| 319 | 318 |
| 320 typedef GrVertexBatch INHERITED; | 319 typedef GrVertexBatch INHERITED; |
| 321 }; | 320 }; |
| 322 | 321 |
| 323 bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { | 322 bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 324 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(), | 323 GR_AUDIT_TRAIL_AUTO_FRAME(args.fDrawContext->auditTrail(), |
| 325 "GrAALinearizingConvexPathRenderer::onDrawPath"); | 324 "GrAALinearizingConvexPathRenderer::onDrawPath"); |
| 326 SkASSERT(!args.fDrawContext->isUnifiedMultisampled()); | 325 SkASSERT(!args.fDrawContext->isUnifiedMultisampled()); |
| 327 SkASSERT(!args.fShape->isEmpty()); | 326 SkASSERT(!args.fShape->isEmpty()); |
| 328 | 327 |
| 329 AAFlatteningConvexPathBatch::Geometry geometry; | 328 SkPath path; |
| 330 geometry.fColor = args.fColor; | 329 args.fShape->asPath(&path); |
| 331 geometry.fViewMatrix = *args.fViewMatrix; | |
| 332 args.fShape->asPath(&geometry.fPath); | |
| 333 bool fill = args.fShape->style().isSimpleFill(); | 330 bool fill = args.fShape->style().isSimpleFill(); |
| 334 const SkStrokeRec& stroke = args.fShape->style().strokeRec(); | 331 const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| 335 geometry.fStrokeWidth = fill ? -1.0f : stroke.getWidth(); | 332 SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth(); |
| 336 geometry.fJoin = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); | 333 SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); |
| 337 geometry.fMiterLimit = stroke.getMiter(); | 334 SkScalar miterLimit = stroke.getMiter(); |
| 338 | 335 |
| 339 SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry
)); | 336 SkAutoTUnref<GrDrawBatch> batch(new AAFlatteningConvexPathBatch(args.fColor,
*args.fViewMatrix, |
| 337 path, stroke
Width, join, |
| 338 miterLimit))
; |
| 340 | 339 |
| 341 GrPipelineBuilder pipelineBuilder(*args.fPaint); | 340 GrPipelineBuilder pipelineBuilder(*args.fPaint); |
| 342 pipelineBuilder.setUserStencil(args.fUserStencilSettings); | 341 pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 343 | 342 |
| 344 args.fDrawContext->drawBatch(pipelineBuilder, *args.fClip, batch); | 343 args.fDrawContext->drawBatch(pipelineBuilder, *args.fClip, batch); |
| 345 | 344 |
| 346 return true; | 345 return true; |
| 347 } | 346 } |
| 348 | 347 |
| 349 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 348 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 350 | 349 |
| 351 #ifdef GR_TEST_UTILS | 350 #ifdef GR_TEST_UTILS |
| 352 | 351 |
| 353 DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) { | 352 DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) { |
| 354 AAFlatteningConvexPathBatch::Geometry geometry; | 353 GrColor color = GrRandomColor(random); |
| 355 geometry.fColor = GrRandomColor(random); | 354 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 356 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random); | 355 SkPath path = GrTest::TestPathConvex(random); |
| 357 geometry.fPath = GrTest::TestPathConvex(random); | 356 SkScalar strokeWidth = random->nextBool() ? -1.f : 2.f; |
| 358 | 357 SkPaint::Join join = SkPaint::kMiter_Join; |
| 359 return AAFlatteningConvexPathBatch::Create(geometry); | 358 SkScalar miterLimit = 0.5f; |
| 359 return new AAFlatteningConvexPathBatch(color, viewMatrix, path, strokeWidth,
join, miterLimit); |
| 360 } | 360 } |
| 361 | 361 |
| 362 #endif | 362 #endif |
| OLD | NEW |