1 /*
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 "GrDashLinePathRenderer.h"
9 
10 #include "GrAuditTrail.h"
11 #include "GrGpu.h"
12 #include "ops/GrDashOp.h"
13 #include "ops/GrMeshDrawOp.h"
14 
15 GrPathRenderer::CanDrawPath
16 GrDashLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
17     SkPoint pts[2];
18     bool inverted;
19     if (args.fShape->style().isDashed() && args.fShape->asLine(pts, &inverted)) {
20         if (args.fAAType == GrAAType::kMixedSamples) {
21             return CanDrawPath::kNo;
22         }
23         // We should never have an inverse dashed case.
24         SkASSERT(!inverted);
25         if (!GrDashOp::CanDrawDashLine(pts, args.fShape->style(), *args.fViewMatrix)) {
26             return CanDrawPath::kNo;
27         }
28         return CanDrawPath::kYes;
29     }
30     return CanDrawPath::kNo;
31 }
32 
33 bool GrDashLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
34     GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
35                               "GrDashLinePathRenderer::onDrawPath");
36     GrDashOp::AAMode aaMode = GrDashOp::AAMode::kNone;
37     switch (args.fAAType) {
38         case GrAAType::kNone:
39             break;
40         case GrAAType::kCoverage:
41         case GrAAType::kMixedSamples:
42             aaMode = GrDashOp::AAMode::kCoverage;
43             break;
44         case GrAAType::kMSAA:
45             // In this mode we will use aa between dashes but the outer border uses MSAA. Otherwise,
46             // we can wind up with external edges antialiased and internal edges unantialiased.
47             aaMode = GrDashOp::AAMode::kCoverageWithMSAA;
48             break;
49     }
50     SkPoint pts[2];
51     SkAssertResult(args.fShape->asLine(pts, nullptr));
52     std::unique_ptr<GrDrawOp> op =
53             GrDashOp::MakeDashLineOp(std::move(args.fPaint), *args.fViewMatrix, pts, aaMode,
54                                      args.fShape->style(), args.fUserStencilSettings);
55     if (!op) {
56         return false;
57     }
58     args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
59     return true;
60 }
61