blob: 64eedbef6bd9e6f4c1503a5bb53f4ae8b6cbd660 [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"
Robert Phillipsa9e28af2020-03-13 08:40:12 -040019#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrStyle.h"
22#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040023#include "src/gpu/geometry/GrPathUtils.h"
24#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
26#include "src/gpu/ops/GrAAConvexTessellator.h"
27#include "src/gpu/ops/GrAALinearizingConvexPathRenderer.h"
28#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050029#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070030
fmalitabd5d7e72015-06-26 07:18:24 -070031static const int DEFAULT_BUFFER_SIZE = 100;
32
33// The thicker the stroke, the harder it is to produce high-quality results using tessellation. For
34// the time being, we simply drop back to software rendering above this stroke width.
35static const SkScalar kMaxStrokeWidth = 20.0;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070036
Brian Salomona3762ee2020-04-10 09:16:04 -040037GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() = default;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070038
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 }
Brian Salomona3762ee2020-04-10 09:16:04 -040080 // This can almost handle perspective. It would need to use 3 component explicit local coords
81 // when there are FPs that require them. This is difficult to test because AAConvexPathRenderer
82 // takes almost all filled paths that could get here. So just avoid perspective fills.
83 if (args.fViewMatrix->hasPerspective()) {
84 return CanDrawPath::kNo;
85 }
Chris Dalton5ed44232017-09-07 13:22:46 -060086 return CanDrawPath::kYes;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070087}
88
89// extract the result vertices and indices from the GrAAConvexTessellator
90static void extract_verts(const GrAAConvexTessellator& tess,
Brian Salomona3762ee2020-04-10 09:16:04 -040091 const SkMatrix* localCoordsMatrix,
Brian Osmanf9aabff2018-11-13 16:11:38 -050092 void* vertData,
Brian Osman80879d42019-01-07 16:15:27 -050093 const GrVertexColor& color,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070094 uint16_t firstIndex,
Brian Osman0995fd52019-01-09 09:52:25 -050095 uint16_t* idxs) {
Brian Osmanf9aabff2018-11-13 16:11:38 -050096 GrVertexWriter verts{vertData};
Brian Osmanf3e6b902018-12-28 17:43:50 +000097 for (int i = 0; i < tess.numPts(); ++i) {
Brian Salomona3762ee2020-04-10 09:16:04 -040098 SkPoint lc;
99 if (localCoordsMatrix) {
100 localCoordsMatrix->mapPoints(&lc, &tess.point(i), 1);
101 }
Brian Salomondaf94c52020-04-10 15:42:27 -0400102 verts.write(tess.point(i), color, GrVertexWriter::If(localCoordsMatrix, lc),
Brian Salomona3762ee2020-04-10 09:16:04 -0400103 tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700104 }
105
106 for (int i = 0; i < tess.numIndices(); ++i) {
107 idxs[i] = tess.index(i) + firstIndex;
108 }
109}
110
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500111static GrGeometryProcessor* create_lines_only_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500112 bool tweakAlphaForCoverage,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500113 bool usesLocalCoords,
114 bool wideColor) {
joshualittdf0c5572015-08-03 11:35:28 -0700115 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700116
Brian Osman80879d42019-01-07 16:15:27 -0500117 Coverage::Type coverageType =
118 tweakAlphaForCoverage ? Coverage::kAttributeTweakAlpha_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500119 LocalCoords::Type localCoordsType =
Brian Salomona3762ee2020-04-10 09:16:04 -0400120 usesLocalCoords ? LocalCoords::kHasExplicit_Type : LocalCoords::kUnused_Type;
Brian Osman80879d42019-01-07 16:15:27 -0500121 Color::Type colorType =
122 wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type;
123
Brian Salomona3762ee2020-04-10 09:16:04 -0400124 return Make(arena, colorType, coverageType, localCoordsType, SkMatrix::I());
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700125}
126
Brian Salomonb2955732017-07-13 16:42:55 -0400127namespace {
128
129class AAFlatteningConvexPathOp final : public GrMeshDrawOp {
130private:
131 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
132
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700133public:
Brian Salomon25a88092016-12-01 09:36:50 -0500134 DEFINE_OP_CLASS_ID
Robert Phillipsb97da532019-02-12 15:24:12 -0500135 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400136 GrPaint&& paint,
Brian Salomonb2955732017-07-13 16:42:55 -0400137 const SkMatrix& viewMatrix,
138 const SkPath& path,
139 SkScalar strokeWidth,
140 SkStrokeRec::Style style,
141 SkPaint::Join join,
142 SkScalar miterLimit,
143 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400144 return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint),
145 viewMatrix, path,
Brian Salomonb2955732017-07-13 16:42:55 -0400146 strokeWidth, style, join, miterLimit,
147 stencilSettings);
Brian Salomon780dad12016-12-15 18:08:40 -0500148 }
149
Brian Salomonb2955732017-07-13 16:42:55 -0400150 AAFlatteningConvexPathOp(const Helper::MakeArgs& helperArgs,
Brian Osmancf860852018-10-31 14:04:39 -0400151 const SkPMColor4f& color,
Brian Salomon780dad12016-12-15 18:08:40 -0500152 const SkMatrix& viewMatrix,
153 const SkPath& path,
154 SkScalar strokeWidth,
155 SkStrokeRec::Style style,
156 SkPaint::Join join,
Brian Salomonb2955732017-07-13 16:42:55 -0400157 SkScalar miterLimit,
158 const GrUserStencilSettings* stencilSettings)
Brian Salomona3762ee2020-04-10 09:16:04 -0400159 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
160 fPaths.emplace_back(
161 PathData{viewMatrix, path, color, strokeWidth, miterLimit, style, join});
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700162
bsalomon0432dd62016-06-30 07:19:27 -0700163 // compute bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700164 SkRect bounds = path.getBounds();
bsalomon0432dd62016-06-30 07:19:27 -0700165 SkScalar w = strokeWidth;
166 if (w > 0) {
167 w /= 2;
Greg Daniel55e5be32019-09-30 12:23:26 -0400168 SkScalar maxScale = viewMatrix.getMaxScale();
169 // We should not have a perspective matrix, thus we should have a valid scale.
170 SkASSERT(maxScale != -1);
171 if (SkPaint::kMiter_Join == join && w * maxScale > 1.f) {
bsalomon0432dd62016-06-30 07:19:27 -0700172 w *= miterLimit;
173 }
bsalomon88cf17d2016-07-08 06:40:56 -0700174 bounds.outset(w, w);
bsalomon0432dd62016-06-30 07:19:27 -0700175 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400176 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsHairline::kNo);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700177 }
178
Brian Salomonb2955732017-07-13 16:42:55 -0400179 const char* name() const override { return "AAFlatteningConvexPathOp"; }
180
Chris Dalton1706cbf2019-05-21 19:35:29 -0600181 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400182 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600183 fProgramInfo->visitFPProxies(func);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400184 } else {
185 fHelper.visitProxies(func);
186 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400187 }
188
Brian Osman9a390ac2018-11-12 09:47:48 -0500189#ifdef SK_DEBUG
Brian Salomonb2955732017-07-13 16:42:55 -0400190 SkString dumpInfo() const override {
191 SkString string;
192 for (const auto& path : fPaths) {
193 string.appendf(
194 "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, "
195 "MiterLimit: %.2f\n",
Brian Osmancf860852018-10-31 14:04:39 -0400196 path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400197 path.fMiterLimit);
Brian Salomonb2955732017-07-13 16:42:55 -0400198 }
199 string += fHelper.dumpInfo();
200 string += INHERITED::dumpInfo();
201 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500202 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500203#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500204
Brian Salomonb2955732017-07-13 16:42:55 -0400205 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
206
Chris Dalton6ce447a2019-06-23 18:07:38 -0600207 GrProcessorSet::Analysis finalize(
208 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
209 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700210 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600211 caps, clip, hasMixedSampledCoverage, clampType,
212 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700213 }
214
Brian Salomonb2955732017-07-13 16:42:55 -0400215private:
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400216 GrProgramInfo* programInfo() override { return fProgramInfo; }
Robert Phillips2669a7b2020-03-12 12:07:19 -0400217
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400218 void onCreateProgramInfo(const GrCaps* caps,
219 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400220 const GrSurfaceProxyView* writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400221 GrAppliedClip&& appliedClip,
222 const GrXferProcessor::DstProxyView& dstProxyView) override {
223 GrGeometryProcessor* gp = create_lines_only_gp(arena,
224 fHelper.compatibleWithCoverageAsAlpha(),
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400225 fHelper.usesLocalCoords(),
226 fWideColor);
227 if (!gp) {
228 SkDebugf("Couldn't create a GrGeometryProcessor\n");
229 return;
230 }
231
Brian Salomon8afde5f2020-04-01 16:22:00 -0400232 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400233 std::move(appliedClip), dstProxyView,
234 gp, GrPrimitiveType::kTriangles);
Robert Phillips4133dc42020-03-11 15:55:55 -0400235 }
236
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400237 void recordDraw(Target* target,
238 int vertexCount, size_t vertexStride, void* vertices,
239 int indexCount, uint16_t* indices) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700240 if (vertexCount == 0 || indexCount == 0) {
241 return;
242 }
Brian Salomon12d22642019-01-29 14:38:50 -0500243 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600244 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700245 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600246 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700247 if (!verts) {
248 SkDebugf("Could not allocate vertices\n");
249 return;
250 }
251 memcpy(verts, vertices, vertexCount * vertexStride);
252
Brian Salomon12d22642019-01-29 14:38:50 -0500253 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600254 int firstIndex;
255 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700256 if (!idxs) {
257 SkDebugf("Could not allocate indices\n");
258 return;
259 }
260 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
Chris Daltoneb694b72020-03-16 09:25:50 -0600261 GrSimpleMesh* mesh = target->allocMesh();
Brian Salomon12d22642019-01-29 14:38:50 -0500262 mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600263 GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400264 fMeshes.push_back(mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700265 }
mtklein002c2ce2015-08-26 05:43:22 -0700266
Brian Salomon91326c32017-08-09 16:02:19 -0400267 void onPrepareDraws(Target* target) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400268 if (!fProgramInfo) {
269 this->createProgramInfo(target);
270 if (!fProgramInfo) {
271 return;
272 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700273 }
274
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400275 size_t vertexStride = fProgramInfo->primProc().vertexStride();
Brian Salomon780dad12016-12-15 18:08:40 -0500276 int instanceCount = fPaths.count();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700277
Greg Danield5b45932018-06-07 13:15:10 -0400278 int64_t vertexCount = 0;
279 int64_t indexCount = 0;
280 int64_t maxVertices = DEFAULT_BUFFER_SIZE;
281 int64_t maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700282 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
283 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700284 for (int i = 0; i < instanceCount; i++) {
Brian Salomon780dad12016-12-15 18:08:40 -0500285 const PathData& args = fPaths[i];
robertphillips8c170972016-09-22 12:42:30 -0700286 GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth,
287 args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700288
Brian Salomona3762ee2020-04-10 09:16:04 -0400289 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700290 continue;
291 }
292
Greg Danield5b45932018-06-07 13:15:10 -0400293 int currentVertices = tess.numPts();
294 if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) {
mtklein002c2ce2015-08-26 05:43:22 -0700295 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700296 // uint16_t. Draw what we've got so far and reset.
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400297 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700298 vertexCount = 0;
299 indexCount = 0;
300 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700301 if (vertexCount + currentVertices > maxVertices) {
Brian Osman788b9162020-02-07 10:36:46 -0500302 maxVertices = std::max(vertexCount + currentVertices, maxVertices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400303 if (maxVertices * vertexStride > SK_MaxS32) {
304 sk_free(vertices);
305 sk_free(indices);
306 return;
307 }
mtklein002c2ce2015-08-26 05:43:22 -0700308 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700309 }
Greg Danield5b45932018-06-07 13:15:10 -0400310 int currentIndices = tess.numIndices();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700311 if (indexCount + currentIndices > maxIndices) {
Brian Osman788b9162020-02-07 10:36:46 -0500312 maxIndices = std::max(indexCount + currentIndices, maxIndices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400313 if (maxIndices * sizeof(uint16_t) > SK_MaxS32) {
314 sk_free(vertices);
315 sk_free(indices);
316 return;
317 }
mtklein002c2ce2015-08-26 05:43:22 -0700318 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700319 }
320
Brian Salomona3762ee2020-04-10 09:16:04 -0400321 const SkMatrix* localCoordsMatrix = nullptr;
322 SkMatrix ivm;
323 if (fHelper.usesLocalCoords()) {
324 if (!args.fViewMatrix.invert(&ivm)) {
325 ivm = SkMatrix::I();
326 }
327 localCoordsMatrix = &ivm;
328 }
329
330 extract_verts(tess, localCoordsMatrix, vertices + vertexStride * vertexCount,
Brian Osman0995fd52019-01-09 09:52:25 -0500331 GrVertexColor(args.fColor, fWideColor), vertexCount,
332 indices + indexCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700333 vertexCount += currentVertices;
334 indexCount += currentIndices;
335 }
Greg Danield5b45932018-06-07 13:15:10 -0400336 if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400337 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
Greg Danield5b45932018-06-07 13:15:10 -0400338 }
mtklein002c2ce2015-08-26 05:43:22 -0700339 sk_free(vertices);
340 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700341 }
342
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700343 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400344 if (!fProgramInfo || fMeshes.isEmpty()) {
345 return;
346 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500347
Chris Dalton765ed362020-03-16 17:34:44 -0600348 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
349 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400350 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600351 flushState->drawMesh(*fMeshes[i]);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400352 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700353 }
354
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500355 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
356 const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500357 AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>();
Brian Salomonb2955732017-07-13 16:42:55 -0400358 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000359 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700360 }
361
Brian Salomon780dad12016-12-15 18:08:40 -0500362 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman80879d42019-01-07 16:15:27 -0500363 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000364 return CombineResult::kMerged;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700365 }
366
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700367
Brian Salomon780dad12016-12-15 18:08:40 -0500368 struct PathData {
Brian Salomona3762ee2020-04-10 09:16:04 -0400369 SkMatrix fViewMatrix;
bsalomon0432dd62016-06-30 07:19:27 -0700370 SkPath fPath;
Brian Salomona3762ee2020-04-10 09:16:04 -0400371 SkPMColor4f fColor;
bsalomon0432dd62016-06-30 07:19:27 -0700372 SkScalar fStrokeWidth;
Brian Salomona3762ee2020-04-10 09:16:04 -0400373 SkScalar fMiterLimit;
robertphillips8c170972016-09-22 12:42:30 -0700374 SkStrokeRec::Style fStyle;
bsalomon0432dd62016-06-30 07:19:27 -0700375 SkPaint::Join fJoin;
bsalomon0432dd62016-06-30 07:19:27 -0700376 };
377
Brian Salomon780dad12016-12-15 18:08:40 -0500378 SkSTArray<1, PathData, true> fPaths;
Brian Salomonb2955732017-07-13 16:42:55 -0400379 Helper fHelper;
Brian Osman80879d42019-01-07 16:15:27 -0500380 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700381
Chris Daltoneb694b72020-03-16 09:25:50 -0600382 SkTDArray<GrSimpleMesh*> fMeshes;
383 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400384
Brian Salomonb2955732017-07-13 16:42:55 -0400385 typedef GrMeshDrawOp INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700386};
387
Brian Salomonb2955732017-07-13 16:42:55 -0400388} // anonymous namespace
389
bsalomon0aff2fa2015-07-31 06:48:27 -0700390bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400391 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800392 "GrAALinearizingConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600393 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700394 SkASSERT(!args.fShape->isEmpty());
robertphillips8c170972016-09-22 12:42:30 -0700395 SkASSERT(!args.fShape->style().pathEffect());
csmartdaltonecbc12b2016-06-08 10:08:43 -0700396
bsalomon0432dd62016-06-30 07:19:27 -0700397 SkPath path;
398 args.fShape->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700399 bool fill = args.fShape->style().isSimpleFill();
400 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
bsalomon0432dd62016-06-30 07:19:27 -0700401 SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth();
402 SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin();
403 SkScalar miterLimit = stroke.getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700404
Brian Salomonb2955732017-07-13 16:42:55 -0400405 std::unique_ptr<GrDrawOp> op = AAFlatteningConvexPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400406 args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth,
407 stroke.getStyle(), join, miterLimit, args.fUserStencilSettings);
Brian Salomonb2955732017-07-13 16:42:55 -0400408 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700409 return true;
410}
411
412///////////////////////////////////////////////////////////////////////////////////////////////////
413
Hal Canary6f6961e2017-01-31 13:50:44 -0500414#if GR_TEST_UTILS
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700415
Brian Salomonb2955732017-07-13 16:42:55 -0400416GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) {
Brian Salomon780dad12016-12-15 18:08:40 -0500417 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
bsalomon0432dd62016-06-30 07:19:27 -0700418 SkPath path = GrTest::TestPathConvex(random);
robertphillips8c170972016-09-22 12:42:30 -0700419
420 SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style,
Herb Derby60c05f92016-12-13 15:18:55 -0500421 SkStrokeRec::kStroke_Style,
robertphillips8c170972016-09-22 12:42:30 -0700422 SkStrokeRec::kStrokeAndFill_Style };
423
424 SkStrokeRec::Style style = styles[random->nextU() % 3];
425
426 SkScalar strokeWidth = -1.f;
bsalomon0432dd62016-06-30 07:19:27 -0700427 SkPaint::Join join = SkPaint::kMiter_Join;
428 SkScalar miterLimit = 0.5f;
robertphillips8c170972016-09-22 12:42:30 -0700429
430 if (SkStrokeRec::kFill_Style != style) {
431 strokeWidth = random->nextRangeF(1.0f, 10.0f);
432 if (random->nextBool()) {
433 join = SkPaint::kMiter_Join;
434 } else {
435 join = SkPaint::kBevel_Join;
436 }
437 miterLimit = random->nextRangeF(0.5f, 2.0f);
438 }
Brian Salomonb2955732017-07-13 16:42:55 -0400439 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400440 return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth,
441 style, join, miterLimit, stencilSettings);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700442}
443
444#endif