blob: 6e691a75decaf4dff1f7f9f2f69beaf9c5b195aa [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"
Michael Ludwig2686d692020-04-17 20:21:37 +000024#include "src/gpu/geometry/GrStyledShape.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
Herb Derbyc76d4092020-10-07 16:46:15 -0400135
136 static GrOp::Owner Make(GrRecordingContext* context,
137 GrPaint&& paint,
138 const SkMatrix& viewMatrix,
139 const SkPath& path,
140 SkScalar strokeWidth,
141 SkStrokeRec::Style style,
142 SkPaint::Join join,
143 SkScalar miterLimit,
144 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400145 return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint),
146 viewMatrix, path,
Brian Salomonb2955732017-07-13 16:42:55 -0400147 strokeWidth, style, join, miterLimit,
148 stencilSettings);
Brian Salomon780dad12016-12-15 18:08:40 -0500149 }
150
Herb Derbyc76d4092020-10-07 16:46:15 -0400151 AAFlatteningConvexPathOp(GrProcessorSet* processorSet,
Brian Osmancf860852018-10-31 14:04:39 -0400152 const SkPMColor4f& color,
Brian Salomon780dad12016-12-15 18:08:40 -0500153 const SkMatrix& viewMatrix,
154 const SkPath& path,
155 SkScalar strokeWidth,
156 SkStrokeRec::Style style,
157 SkPaint::Join join,
Brian Salomonb2955732017-07-13 16:42:55 -0400158 SkScalar miterLimit,
159 const GrUserStencilSettings* stencilSettings)
Herb Derbyc76d4092020-10-07 16:46:15 -0400160 : INHERITED(ClassID()), fHelper(processorSet, GrAAType::kCoverage, stencilSettings) {
Brian Salomona3762ee2020-04-10 09:16:04 -0400161 fPaths.emplace_back(
162 PathData{viewMatrix, path, color, strokeWidth, miterLimit, style, join});
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700163
bsalomon0432dd62016-06-30 07:19:27 -0700164 // compute bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700165 SkRect bounds = path.getBounds();
bsalomon0432dd62016-06-30 07:19:27 -0700166 SkScalar w = strokeWidth;
167 if (w > 0) {
168 w /= 2;
Greg Daniel55e5be32019-09-30 12:23:26 -0400169 SkScalar maxScale = viewMatrix.getMaxScale();
170 // We should not have a perspective matrix, thus we should have a valid scale.
171 SkASSERT(maxScale != -1);
172 if (SkPaint::kMiter_Join == join && w * maxScale > 1.f) {
bsalomon0432dd62016-06-30 07:19:27 -0700173 w *= miterLimit;
174 }
bsalomon88cf17d2016-07-08 06:40:56 -0700175 bounds.outset(w, w);
bsalomon0432dd62016-06-30 07:19:27 -0700176 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400177 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsHairline::kNo);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700178 }
179
Brian Salomonb2955732017-07-13 16:42:55 -0400180 const char* name() const override { return "AAFlatteningConvexPathOp"; }
181
Chris Dalton1706cbf2019-05-21 19:35:29 -0600182 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400183 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600184 fProgramInfo->visitFPProxies(func);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400185 } else {
186 fHelper.visitProxies(func);
187 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400188 }
189
Brian Salomonb2955732017-07-13 16:42:55 -0400190 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
191
Chris Dalton6ce447a2019-06-23 18:07:38 -0600192 GrProcessorSet::Analysis finalize(
193 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
194 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700195 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600196 caps, clip, hasMixedSampledCoverage, clampType,
197 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700198 }
199
Brian Salomonb2955732017-07-13 16:42:55 -0400200private:
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400201 GrProgramInfo* programInfo() override { return fProgramInfo; }
Robert Phillips2669a7b2020-03-12 12:07:19 -0400202
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400203 void onCreateProgramInfo(const GrCaps* caps,
204 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500205 const GrSurfaceProxyView& writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400206 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400207 const GrXferProcessor::DstProxyView& dstProxyView,
208 GrXferBarrierFlags renderPassXferBarriers) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400209 GrGeometryProcessor* gp = create_lines_only_gp(arena,
210 fHelper.compatibleWithCoverageAsAlpha(),
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400211 fHelper.usesLocalCoords(),
212 fWideColor);
213 if (!gp) {
214 SkDebugf("Couldn't create a GrGeometryProcessor\n");
215 return;
216 }
217
Brian Salomon8afde5f2020-04-01 16:22:00 -0400218 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400219 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400220 gp, GrPrimitiveType::kTriangles,
221 renderPassXferBarriers);
Robert Phillips4133dc42020-03-11 15:55:55 -0400222 }
223
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400224 void recordDraw(Target* target,
225 int vertexCount, size_t vertexStride, void* vertices,
226 int indexCount, uint16_t* indices) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700227 if (vertexCount == 0 || indexCount == 0) {
228 return;
229 }
Brian Salomon12d22642019-01-29 14:38:50 -0500230 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600231 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700232 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600233 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700234 if (!verts) {
235 SkDebugf("Could not allocate vertices\n");
236 return;
237 }
238 memcpy(verts, vertices, vertexCount * vertexStride);
239
Brian Salomon12d22642019-01-29 14:38:50 -0500240 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600241 int firstIndex;
242 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700243 if (!idxs) {
244 SkDebugf("Could not allocate indices\n");
245 return;
246 }
247 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
Chris Daltoneb694b72020-03-16 09:25:50 -0600248 GrSimpleMesh* mesh = target->allocMesh();
Brian Salomon12d22642019-01-29 14:38:50 -0500249 mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600250 GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400251 fMeshes.push_back(mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700252 }
mtklein002c2ce2015-08-26 05:43:22 -0700253
Brian Salomon91326c32017-08-09 16:02:19 -0400254 void onPrepareDraws(Target* target) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400255 if (!fProgramInfo) {
256 this->createProgramInfo(target);
257 if (!fProgramInfo) {
258 return;
259 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700260 }
261
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400262 size_t vertexStride = fProgramInfo->primProc().vertexStride();
Brian Salomon780dad12016-12-15 18:08:40 -0500263 int instanceCount = fPaths.count();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700264
Greg Danield5b45932018-06-07 13:15:10 -0400265 int64_t vertexCount = 0;
266 int64_t indexCount = 0;
267 int64_t maxVertices = DEFAULT_BUFFER_SIZE;
268 int64_t maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700269 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
270 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700271 for (int i = 0; i < instanceCount; i++) {
Brian Salomon780dad12016-12-15 18:08:40 -0500272 const PathData& args = fPaths[i];
robertphillips8c170972016-09-22 12:42:30 -0700273 GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth,
274 args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700275
Brian Salomona3762ee2020-04-10 09:16:04 -0400276 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700277 continue;
278 }
279
Greg Danield5b45932018-06-07 13:15:10 -0400280 int currentVertices = tess.numPts();
281 if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) {
mtklein002c2ce2015-08-26 05:43:22 -0700282 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700283 // uint16_t. Draw what we've got so far and reset.
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400284 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700285 vertexCount = 0;
286 indexCount = 0;
287 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700288 if (vertexCount + currentVertices > maxVertices) {
Brian Osman788b9162020-02-07 10:36:46 -0500289 maxVertices = std::max(vertexCount + currentVertices, maxVertices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400290 if (maxVertices * vertexStride > SK_MaxS32) {
291 sk_free(vertices);
292 sk_free(indices);
293 return;
294 }
mtklein002c2ce2015-08-26 05:43:22 -0700295 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700296 }
Greg Danield5b45932018-06-07 13:15:10 -0400297 int currentIndices = tess.numIndices();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700298 if (indexCount + currentIndices > maxIndices) {
Brian Osman788b9162020-02-07 10:36:46 -0500299 maxIndices = std::max(indexCount + currentIndices, maxIndices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400300 if (maxIndices * sizeof(uint16_t) > SK_MaxS32) {
301 sk_free(vertices);
302 sk_free(indices);
303 return;
304 }
mtklein002c2ce2015-08-26 05:43:22 -0700305 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700306 }
307
Brian Salomona3762ee2020-04-10 09:16:04 -0400308 const SkMatrix* localCoordsMatrix = nullptr;
309 SkMatrix ivm;
310 if (fHelper.usesLocalCoords()) {
311 if (!args.fViewMatrix.invert(&ivm)) {
312 ivm = SkMatrix::I();
313 }
314 localCoordsMatrix = &ivm;
315 }
316
317 extract_verts(tess, localCoordsMatrix, vertices + vertexStride * vertexCount,
Brian Osman0995fd52019-01-09 09:52:25 -0500318 GrVertexColor(args.fColor, fWideColor), vertexCount,
319 indices + indexCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700320 vertexCount += currentVertices;
321 indexCount += currentIndices;
322 }
Greg Danield5b45932018-06-07 13:15:10 -0400323 if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400324 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
Greg Danield5b45932018-06-07 13:15:10 -0400325 }
mtklein002c2ce2015-08-26 05:43:22 -0700326 sk_free(vertices);
327 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700328 }
329
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700330 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400331 if (!fProgramInfo || fMeshes.isEmpty()) {
332 return;
333 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500334
Chris Dalton765ed362020-03-16 17:34:44 -0600335 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
336 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400337 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600338 flushState->drawMesh(*fMeshes[i]);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400339 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700340 }
341
Herb Derbye25c3002020-10-27 15:57:27 -0400342 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500343 AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>();
Brian Salomonb2955732017-07-13 16:42:55 -0400344 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000345 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700346 }
347
Brian Salomon780dad12016-12-15 18:08:40 -0500348 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman80879d42019-01-07 16:15:27 -0500349 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000350 return CombineResult::kMerged;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700351 }
352
John Stilesaf366522020-08-13 09:57:34 -0400353#if GR_TEST_UTILS
354 SkString onDumpInfo() const override {
355 SkString string;
356 for (const auto& path : fPaths) {
357 string.appendf(
358 "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, "
359 "MiterLimit: %.2f\n",
360 path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin,
361 path.fMiterLimit);
362 }
363 string += fHelper.dumpInfo();
364 return string;
365 }
366#endif
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
John Stiles7571f9e2020-09-02 22:42:33 -0400385 using INHERITED = GrMeshDrawOp;
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
Herb Derbyc76d4092020-10-07 16:46:15 -0400405 GrOp::Owner 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);
Michael Ludwig7c12e282020-05-29 09:54:07 -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);
John Stiles31954bf2020-08-07 17:35:54 -0400418 const 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