blob: a66a9aee46f2b4d0038355cfb9b3d73f05c2bcdb [file] [log] [blame]
ethannicholas1a1b3ac2015-06-10 12:11:17 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkString.h"
9#include "src/core/SkGeometry.h"
10#include "src/core/SkPathPriv.h"
11#include "src/core/SkTraceEvent.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrDefaultGeoProcFactory.h"
15#include "src/gpu/GrDrawOpTest.h"
16#include "src/gpu/GrGeometryProcessor.h"
17#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrProcessor.h"
19#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrStyle.h"
21#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040022#include "src/gpu/geometry/GrPathUtils.h"
23#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
25#include "src/gpu/ops/GrAAConvexTessellator.h"
26#include "src/gpu/ops/GrAALinearizingConvexPathRenderer.h"
27#include "src/gpu/ops/GrMeshDrawOp.h"
28#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070029
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
Chris Dalton5ed44232017-09-07 13:22:46 -060041GrPathRenderer::CanDrawPath
42GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -060043 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060044 return CanDrawPath::kNo;
fmalitabd5d7e72015-06-26 07:18:24 -070045 }
bsalomon8acedde2016-06-24 10:42:16 -070046 if (!args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060047 return CanDrawPath::kNo;
fmalitabd5d7e72015-06-26 07:18:24 -070048 }
bsalomon8acedde2016-06-24 10:42:16 -070049 if (args.fShape->style().pathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060050 return CanDrawPath::kNo;
fmalitabd5d7e72015-06-26 07:18:24 -070051 }
bsalomon8acedde2016-06-24 10:42:16 -070052 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060053 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -070054 }
Brian Osmana98e3992017-06-26 15:14:53 -040055 if (args.fShape->bounds().width() <= 0 && args.fShape->bounds().height() <= 0) {
56 // Stroked zero length lines should draw, but this PR doesn't handle that case
Chris Dalton5ed44232017-09-07 13:22:46 -060057 return CanDrawPath::kNo;
Brian Osmana98e3992017-06-26 15:14:53 -040058 }
bsalomon8acedde2016-06-24 10:42:16 -070059 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
robertphillips8c170972016-09-22 12:42:30 -070060
61 if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
62 stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070063 if (!args.fViewMatrix->isSimilarity()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060064 return CanDrawPath::kNo;
ethannicholasfea77632015-08-19 12:09:12 -070065 }
bsalomon6663acf2016-05-10 09:14:17 -070066 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * stroke.getWidth();
robertphillips8c170972016-09-22 12:42:30 -070067 if (strokeWidth < 1.0f && stroke.getStyle() == SkStrokeRec::kStroke_Style) {
Chris Dalton5ed44232017-09-07 13:22:46 -060068 return CanDrawPath::kNo;
robertphillips8c170972016-09-22 12:42:30 -070069 }
Chris Dalton5ed44232017-09-07 13:22:46 -060070 if (strokeWidth > kMaxStrokeWidth ||
71 !args.fShape->knownToBeClosed() ||
72 stroke.getJoin() == SkPaint::Join::kRound_Join) {
73 return CanDrawPath::kNo;
74 }
75 return CanDrawPath::kYes;
fmalitabd5d7e72015-06-26 07:18:24 -070076 }
Chris Dalton5ed44232017-09-07 13:22:46 -060077 if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
78 return CanDrawPath::kNo;
79 }
80 return CanDrawPath::kYes;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070081}
82
83// extract the result vertices and indices from the GrAAConvexTessellator
84static void extract_verts(const GrAAConvexTessellator& tess,
Brian Osmanf9aabff2018-11-13 16:11:38 -050085 void* vertData,
Brian Osman80879d42019-01-07 16:15:27 -050086 const GrVertexColor& color,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070087 uint16_t firstIndex,
Brian Osman0995fd52019-01-09 09:52:25 -050088 uint16_t* idxs) {
Brian Osmanf9aabff2018-11-13 16:11:38 -050089 GrVertexWriter verts{vertData};
Brian Osmanf3e6b902018-12-28 17:43:50 +000090 for (int i = 0; i < tess.numPts(); ++i) {
Brian Osman80879d42019-01-07 16:15:27 -050091 verts.write(tess.point(i), color, tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070092 }
93
94 for (int i = 0; i < tess.numIndices(); ++i) {
95 idxs[i] = tess.index(i) + firstIndex;
96 }
97}
98
Ruiqi Maob609e6d2018-07-17 10:19:38 -040099static sk_sp<GrGeometryProcessor> create_lines_only_gp(const GrShaderCaps* shaderCaps,
100 bool tweakAlphaForCoverage,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400101 const SkMatrix& viewMatrix,
Brian Osman80879d42019-01-07 16:15:27 -0500102 bool usesLocalCoords,
103 bool wideColor) {
joshualittdf0c5572015-08-03 11:35:28 -0700104 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700105
Brian Osman80879d42019-01-07 16:15:27 -0500106 Coverage::Type coverageType =
107 tweakAlphaForCoverage ? Coverage::kAttributeTweakAlpha_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500108 LocalCoords::Type localCoordsType =
Brian Osman80879d42019-01-07 16:15:27 -0500109 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
110 Color::Type colorType =
111 wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type;
112
113 return MakeForDeviceSpace(shaderCaps, colorType, coverageType, localCoordsType, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700114}
115
Brian Salomonb2955732017-07-13 16:42:55 -0400116namespace {
117
118class AAFlatteningConvexPathOp final : public GrMeshDrawOp {
119private:
120 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
121
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700122public:
Brian Salomon25a88092016-12-01 09:36:50 -0500123 DEFINE_OP_CLASS_ID
Robert Phillipsb97da532019-02-12 15:24:12 -0500124 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400125 GrPaint&& paint,
Brian Salomonb2955732017-07-13 16:42:55 -0400126 const SkMatrix& viewMatrix,
127 const SkPath& path,
128 SkScalar strokeWidth,
129 SkStrokeRec::Style style,
130 SkPaint::Join join,
131 SkScalar miterLimit,
132 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400133 return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint),
134 viewMatrix, path,
Brian Salomonb2955732017-07-13 16:42:55 -0400135 strokeWidth, style, join, miterLimit,
136 stencilSettings);
Brian Salomon780dad12016-12-15 18:08:40 -0500137 }
138
Brian Salomonb2955732017-07-13 16:42:55 -0400139 AAFlatteningConvexPathOp(const Helper::MakeArgs& helperArgs,
Brian Osmancf860852018-10-31 14:04:39 -0400140 const SkPMColor4f& color,
Brian Salomon780dad12016-12-15 18:08:40 -0500141 const SkMatrix& viewMatrix,
142 const SkPath& path,
143 SkScalar strokeWidth,
144 SkStrokeRec::Style style,
145 SkPaint::Join join,
Brian Salomonb2955732017-07-13 16:42:55 -0400146 SkScalar miterLimit,
147 const GrUserStencilSettings* stencilSettings)
148 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
Brian Salomon780dad12016-12-15 18:08:40 -0500149 fPaths.emplace_back(
150 PathData{color, viewMatrix, path, strokeWidth, style, join, miterLimit});
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700151
bsalomon0432dd62016-06-30 07:19:27 -0700152 // compute bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700153 SkRect bounds = path.getBounds();
bsalomon0432dd62016-06-30 07:19:27 -0700154 SkScalar w = strokeWidth;
155 if (w > 0) {
156 w /= 2;
157 // If the half stroke width is < 1 then we effectively fallback to bevel joins.
158 if (SkPaint::kMiter_Join == join && w > 1.f) {
159 w *= miterLimit;
160 }
bsalomon88cf17d2016-07-08 06:40:56 -0700161 bounds.outset(w, w);
bsalomon0432dd62016-06-30 07:19:27 -0700162 }
bsalomon88cf17d2016-07-08 06:40:56 -0700163 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700164 }
165
Brian Salomonb2955732017-07-13 16:42:55 -0400166 const char* name() const override { return "AAFlatteningConvexPathOp"; }
167
Chris Dalton1706cbf2019-05-21 19:35:29 -0600168 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400169 fHelper.visitProxies(func);
170 }
171
Brian Osman9a390ac2018-11-12 09:47:48 -0500172#ifdef SK_DEBUG
Brian Salomonb2955732017-07-13 16:42:55 -0400173 SkString dumpInfo() const override {
174 SkString string;
175 for (const auto& path : fPaths) {
176 string.appendf(
177 "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, "
178 "MiterLimit: %.2f\n",
Brian Osmancf860852018-10-31 14:04:39 -0400179 path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400180 path.fMiterLimit);
Brian Salomonb2955732017-07-13 16:42:55 -0400181 }
182 string += fHelper.dumpInfo();
183 string += INHERITED::dumpInfo();
184 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500185 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500186#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500187
Brian Salomonb2955732017-07-13 16:42:55 -0400188 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
189
Chris Dalton6ce447a2019-06-23 18:07:38 -0600190 GrProcessorSet::Analysis finalize(
191 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
192 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700193 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600194 caps, clip, hasMixedSampledCoverage, clampType,
195 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700196 }
197
Brian Salomonb2955732017-07-13 16:42:55 -0400198private:
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700199 void recordDraw(Target* target, sk_sp<const GrGeometryProcessor> gp, int vertexCount,
200 size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700201 if (vertexCount == 0 || indexCount == 0) {
202 return;
203 }
Brian Salomon12d22642019-01-29 14:38:50 -0500204 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600205 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700206 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600207 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700208 if (!verts) {
209 SkDebugf("Could not allocate vertices\n");
210 return;
211 }
212 memcpy(verts, vertices, vertexCount * vertexStride);
213
Brian Salomon12d22642019-01-29 14:38:50 -0500214 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600215 int firstIndex;
216 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700217 if (!idxs) {
218 SkDebugf("Could not allocate indices\n");
219 return;
220 }
221 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
Brian Salomon7eae3e02018-08-07 14:02:38 +0000222 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -0500223 mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000224 GrPrimitiveRestart::kNo);
Brian Salomon12d22642019-01-29 14:38:50 -0500225 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700226 target->recordDraw(std::move(gp), mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700227 }
mtklein002c2ce2015-08-26 05:43:22 -0700228
Brian Salomon91326c32017-08-09 16:02:19 -0400229 void onPrepareDraws(Target* target) override {
joshualittdf0c5572015-08-03 11:35:28 -0700230 // Setup GrGeometryProcessor
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400231 sk_sp<GrGeometryProcessor> gp(create_lines_only_gp(target->caps().shaderCaps(),
Brian Osman605c6d52019-03-15 12:10:35 -0400232 fHelper.compatibleWithCoverageAsAlpha(),
Brian Salomonb2955732017-07-13 16:42:55 -0400233 this->viewMatrix(),
Brian Osman80879d42019-01-07 16:15:27 -0500234 fHelper.usesLocalCoords(),
235 fWideColor));
joshualittdf0c5572015-08-03 11:35:28 -0700236 if (!gp) {
237 SkDebugf("Couldn't create a GrGeometryProcessor\n");
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700238 return;
239 }
240
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500241 size_t vertexStride = gp->vertexStride();
Brian Salomon780dad12016-12-15 18:08:40 -0500242 int instanceCount = fPaths.count();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700243
Greg Danield5b45932018-06-07 13:15:10 -0400244 int64_t vertexCount = 0;
245 int64_t indexCount = 0;
246 int64_t maxVertices = DEFAULT_BUFFER_SIZE;
247 int64_t maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700248 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
249 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700250 for (int i = 0; i < instanceCount; i++) {
Brian Salomon780dad12016-12-15 18:08:40 -0500251 const PathData& args = fPaths[i];
robertphillips8c170972016-09-22 12:42:30 -0700252 GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth,
253 args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700254
255 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
256 continue;
257 }
258
Greg Danield5b45932018-06-07 13:15:10 -0400259 int currentVertices = tess.numPts();
260 if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) {
mtklein002c2ce2015-08-26 05:43:22 -0700261 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700262 // uint16_t. Draw what we've got so far and reset.
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700263 this->recordDraw(
264 target, gp, vertexCount, vertexStride, vertices, indexCount, indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700265 vertexCount = 0;
266 indexCount = 0;
267 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700268 if (vertexCount + currentVertices > maxVertices) {
269 maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400270 if (maxVertices * vertexStride > SK_MaxS32) {
271 sk_free(vertices);
272 sk_free(indices);
273 return;
274 }
mtklein002c2ce2015-08-26 05:43:22 -0700275 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700276 }
Greg Danield5b45932018-06-07 13:15:10 -0400277 int currentIndices = tess.numIndices();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700278 if (indexCount + currentIndices > maxIndices) {
279 maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400280 if (maxIndices * sizeof(uint16_t) > SK_MaxS32) {
281 sk_free(vertices);
282 sk_free(indices);
283 return;
284 }
mtklein002c2ce2015-08-26 05:43:22 -0700285 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700286 }
287
Brian Osman80879d42019-01-07 16:15:27 -0500288 extract_verts(tess, vertices + vertexStride * vertexCount,
Brian Osman0995fd52019-01-09 09:52:25 -0500289 GrVertexColor(args.fColor, fWideColor), vertexCount,
290 indices + indexCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700291 vertexCount += currentVertices;
292 indexCount += currentIndices;
293 }
Greg Danield5b45932018-06-07 13:15:10 -0400294 if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) {
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700295 this->recordDraw(target, std::move(gp), vertexCount, vertexStride, vertices, indexCount,
296 indices);
Greg Danield5b45932018-06-07 13:15:10 -0400297 }
mtklein002c2ce2015-08-26 05:43:22 -0700298 sk_free(vertices);
299 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700300 }
301
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700302 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
303 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
304 }
305
Brian Salomon7eae3e02018-08-07 14:02:38 +0000306 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500307 AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>();
Brian Salomonb2955732017-07-13 16:42:55 -0400308 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000309 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700310 }
311
Brian Salomon780dad12016-12-15 18:08:40 -0500312 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman80879d42019-01-07 16:15:27 -0500313 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000314 return CombineResult::kMerged;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700315 }
316
Brian Salomon780dad12016-12-15 18:08:40 -0500317 const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700318
Brian Salomon780dad12016-12-15 18:08:40 -0500319 struct PathData {
Brian Osmancf860852018-10-31 14:04:39 -0400320 SkPMColor4f fColor;
bsalomon0432dd62016-06-30 07:19:27 -0700321 SkMatrix fViewMatrix;
322 SkPath fPath;
323 SkScalar fStrokeWidth;
robertphillips8c170972016-09-22 12:42:30 -0700324 SkStrokeRec::Style fStyle;
bsalomon0432dd62016-06-30 07:19:27 -0700325 SkPaint::Join fJoin;
326 SkScalar fMiterLimit;
327 };
328
Brian Salomon780dad12016-12-15 18:08:40 -0500329 SkSTArray<1, PathData, true> fPaths;
Brian Salomonb2955732017-07-13 16:42:55 -0400330 Helper fHelper;
Brian Osman80879d42019-01-07 16:15:27 -0500331 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700332
Brian Salomonb2955732017-07-13 16:42:55 -0400333 typedef GrMeshDrawOp INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700334};
335
Brian Salomonb2955732017-07-13 16:42:55 -0400336} // anonymous namespace
337
bsalomon0aff2fa2015-07-31 06:48:27 -0700338bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400339 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800340 "GrAALinearizingConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600341 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700342 SkASSERT(!args.fShape->isEmpty());
robertphillips8c170972016-09-22 12:42:30 -0700343 SkASSERT(!args.fShape->style().pathEffect());
csmartdaltonecbc12b2016-06-08 10:08:43 -0700344
bsalomon0432dd62016-06-30 07:19:27 -0700345 SkPath path;
346 args.fShape->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700347 bool fill = args.fShape->style().isSimpleFill();
348 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
bsalomon0432dd62016-06-30 07:19:27 -0700349 SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth();
350 SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin();
351 SkScalar miterLimit = stroke.getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700352
Brian Salomonb2955732017-07-13 16:42:55 -0400353 std::unique_ptr<GrDrawOp> op = AAFlatteningConvexPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400354 args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth,
355 stroke.getStyle(), join, miterLimit, args.fUserStencilSettings);
Brian Salomonb2955732017-07-13 16:42:55 -0400356 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700357 return true;
358}
359
360///////////////////////////////////////////////////////////////////////////////////////////////////
361
Hal Canary6f6961e2017-01-31 13:50:44 -0500362#if GR_TEST_UTILS
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700363
Brian Salomonb2955732017-07-13 16:42:55 -0400364GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) {
Brian Salomon780dad12016-12-15 18:08:40 -0500365 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
bsalomon0432dd62016-06-30 07:19:27 -0700366 SkPath path = GrTest::TestPathConvex(random);
robertphillips8c170972016-09-22 12:42:30 -0700367
368 SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style,
Herb Derby60c05f92016-12-13 15:18:55 -0500369 SkStrokeRec::kStroke_Style,
robertphillips8c170972016-09-22 12:42:30 -0700370 SkStrokeRec::kStrokeAndFill_Style };
371
372 SkStrokeRec::Style style = styles[random->nextU() % 3];
373
374 SkScalar strokeWidth = -1.f;
bsalomon0432dd62016-06-30 07:19:27 -0700375 SkPaint::Join join = SkPaint::kMiter_Join;
376 SkScalar miterLimit = 0.5f;
robertphillips8c170972016-09-22 12:42:30 -0700377
378 if (SkStrokeRec::kFill_Style != style) {
379 strokeWidth = random->nextRangeF(1.0f, 10.0f);
380 if (random->nextBool()) {
381 join = SkPaint::kMiter_Join;
382 } else {
383 join = SkPaint::kBevel_Join;
384 }
385 miterLimit = random->nextRangeF(0.5f, 2.0f);
386 }
Brian Salomonb2955732017-07-13 16:42:55 -0400387 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400388 return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth,
389 style, join, miterLimit, stencilSettings);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700390}
391
392#endif