blob: 879a831e70ccdc7681e1131301778957757d9642 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
bsalomon@google.com30085192011-08-19 15:42:31 +00007
Robert Phillips07531a02020-07-15 15:11:09 -04008#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/core/SkDrawProcs.h"
10#include "src/gpu/GrCaps.h"
11#include "src/gpu/GrPaint.h"
12#include "src/gpu/GrPathRenderer.h"
13#include "src/gpu/GrRecordingContextPriv.h"
14#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrUserStencilSettings.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000016#include "src/gpu/geometry/GrStyledShape.h"
bsalomon@google.comffca4002011-02-22 20:34:01 +000017
Brian Salomon653f42f2018-07-10 10:07:31 -040018#ifdef SK_DEBUG
19void GrPathRenderer::StencilPathArgs::validate() const {
20 SkASSERT(fContext);
21 SkASSERT(fRenderTargetContext);
22 SkASSERT(fClipConservativeBounds);
23 SkASSERT(fViewMatrix);
24 SkASSERT(fShape);
25 SkASSERT(fShape->style().isSimpleFill());
Brian Salomon653f42f2018-07-10 10:07:31 -040026 SkPath path;
27 fShape->asPath(&path);
28 SkASSERT(!path.isInverseFillType());
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000029}
Brian Salomon653f42f2018-07-10 10:07:31 -040030#endif
31
32//////////////////////////////////////////////////////////////////////////////
33
34GrPathRenderer::GrPathRenderer() {}
35
Michael Ludwig2686d692020-04-17 20:21:37 +000036GrPathRenderer::StencilSupport GrPathRenderer::getStencilSupport(const GrStyledShape& shape) const {
Brian Salomon653f42f2018-07-10 10:07:31 -040037 SkDEBUGCODE(SkPath path;)
38 SkDEBUGCODE(shape.asPath(&path);)
39 SkASSERT(shape.style().isSimpleFill());
40 SkASSERT(!path.isInverseFillType());
41 return this->onGetStencilSupport(shape);
42}
43
44bool GrPathRenderer::drawPath(const DrawPathArgs& args) {
45#ifdef SK_DEBUG
46 args.validate();
47 CanDrawPathArgs canArgs;
Robert Phillips9da87e02019-02-04 13:26:26 -050048 canArgs.fCaps = args.fContext->priv().caps();
Greg Daniel46e366a2019-12-16 14:38:36 -050049 canArgs.fProxy = args.fRenderTargetContext->asRenderTargetProxy();
Brian Salomon653f42f2018-07-10 10:07:31 -040050 canArgs.fClipConservativeBounds = args.fClipConservativeBounds;
51 canArgs.fViewMatrix = args.fViewMatrix;
52 canArgs.fShape = args.fShape;
Ethan Nicholasafe2c902020-04-28 13:55:02 -040053 canArgs.fPaint = &args.fPaint;
Chris Dalton6ce447a2019-06-23 18:07:38 -060054 canArgs.fAAType = args.fAAType;
Greg Danielbe7fc462019-01-03 16:40:42 -050055 canArgs.fTargetIsWrappedVkSecondaryCB = args.fRenderTargetContext->wrapsVkSecondaryCB();
Brian Salomon653f42f2018-07-10 10:07:31 -040056 canArgs.validate();
57
58 canArgs.fHasUserStencilSettings = !args.fUserStencilSettings->isUnused();
Brian Salomon653f42f2018-07-10 10:07:31 -040059 SkASSERT(CanDrawPath::kNo != this->canDrawPath(canArgs));
60 if (!args.fUserStencilSettings->isUnused()) {
61 SkPath path;
62 args.fShape->asPath(&path);
63 SkASSERT(args.fShape->style().isSimpleFill());
64 SkASSERT(kNoRestriction_StencilSupport == this->getStencilSupport(*args.fShape));
65 }
66#endif
67 return this->onDrawPath(args);
68}
69
70bool GrPathRenderer::IsStrokeHairlineOrEquivalent(const GrStyle& style, const SkMatrix& matrix,
Robert Phillips20390c32018-08-17 11:01:03 -040071 SkScalar* outCoverage) {
Brian Salomon653f42f2018-07-10 10:07:31 -040072 if (style.pathEffect()) {
73 return false;
74 }
75 const SkStrokeRec& stroke = style.strokeRec();
76 if (stroke.isHairlineStyle()) {
77 if (outCoverage) {
78 *outCoverage = SK_Scalar1;
79 }
80 return true;
81 }
82 return stroke.getStyle() == SkStrokeRec::kStroke_Style &&
83 SkDrawTreatAAStrokeAsHairline(stroke.getWidth(), matrix, outCoverage);
84}
85
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +000086void GrPathRenderer::GetPathDevBounds(const SkPath& path,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040087 SkISize devSize,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +000088 const SkMatrix& matrix,
89 SkRect* bounds) {
90 if (path.isInverseFillType()) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -040091 *bounds = SkRect::Make(devSize);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +000092 return;
93 }
94 *bounds = path.getBounds();
95 matrix.mapRect(bounds);
96}
Brian Salomon653f42f2018-07-10 10:07:31 -040097
98void GrPathRenderer::onStencilPath(const StencilPathArgs& args) {
99 static constexpr GrUserStencilSettings kIncrementStencil(
100 GrUserStencilSettings::StaticInit<
101 0xffff,
102 GrUserStencilTest::kAlways,
103 0xffff,
104 GrUserStencilOp::kReplace,
105 GrUserStencilOp::kReplace,
106 0xffff>()
107 );
108
109 GrPaint paint;
110 DrawPathArgs drawArgs{args.fContext,
111 std::move(paint),
112 &kIncrementStencil,
113 args.fRenderTargetContext,
114 nullptr, // clip
115 args.fClipConservativeBounds,
116 args.fViewMatrix,
117 args.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600118 (GrAA::kYes == args.fDoStencilMSAA) ? GrAAType::kMSAA : GrAAType::kNone,
Brian Salomon653f42f2018-07-10 10:07:31 -0400119 false};
120 this->drawPath(drawArgs);
121}