blob: 9694e1b728246fd94a059b09813716e14a04f0c6 [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
37GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() {
38}
39
40///////////////////////////////////////////////////////////////////////////////
41
Chris Dalton5ed44232017-09-07 13:22:46 -060042GrPathRenderer::CanDrawPath
43GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -060044 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -060045 return CanDrawPath::kNo;
fmalitabd5d7e72015-06-26 07:18:24 -070046 }
bsalomon8acedde2016-06-24 10:42:16 -070047 if (!args.fShape->knownToBeConvex()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060048 return CanDrawPath::kNo;
fmalitabd5d7e72015-06-26 07:18:24 -070049 }
bsalomon8acedde2016-06-24 10:42:16 -070050 if (args.fShape->style().pathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060051 return CanDrawPath::kNo;
fmalitabd5d7e72015-06-26 07:18:24 -070052 }
bsalomon8acedde2016-06-24 10:42:16 -070053 if (args.fShape->inverseFilled()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060054 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -070055 }
Brian Osmana98e3992017-06-26 15:14:53 -040056 if (args.fShape->bounds().width() <= 0 && args.fShape->bounds().height() <= 0) {
57 // Stroked zero length lines should draw, but this PR doesn't handle that case
Chris Dalton5ed44232017-09-07 13:22:46 -060058 return CanDrawPath::kNo;
Brian Osmana98e3992017-06-26 15:14:53 -040059 }
bsalomon8acedde2016-06-24 10:42:16 -070060 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
robertphillips8c170972016-09-22 12:42:30 -070061
62 if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
63 stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
ethannicholasfea77632015-08-19 12:09:12 -070064 if (!args.fViewMatrix->isSimilarity()) {
Chris Dalton5ed44232017-09-07 13:22:46 -060065 return CanDrawPath::kNo;
ethannicholasfea77632015-08-19 12:09:12 -070066 }
bsalomon6663acf2016-05-10 09:14:17 -070067 SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * stroke.getWidth();
robertphillips8c170972016-09-22 12:42:30 -070068 if (strokeWidth < 1.0f && stroke.getStyle() == SkStrokeRec::kStroke_Style) {
Chris Dalton5ed44232017-09-07 13:22:46 -060069 return CanDrawPath::kNo;
robertphillips8c170972016-09-22 12:42:30 -070070 }
Chris Dalton5ed44232017-09-07 13:22:46 -060071 if (strokeWidth > kMaxStrokeWidth ||
72 !args.fShape->knownToBeClosed() ||
73 stroke.getJoin() == SkPaint::Join::kRound_Join) {
74 return CanDrawPath::kNo;
75 }
76 return CanDrawPath::kYes;
fmalitabd5d7e72015-06-26 07:18:24 -070077 }
Chris Dalton5ed44232017-09-07 13:22:46 -060078 if (stroke.getStyle() != SkStrokeRec::kFill_Style) {
79 return CanDrawPath::kNo;
80 }
81 return CanDrawPath::kYes;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070082}
83
84// extract the result vertices and indices from the GrAAConvexTessellator
85static void extract_verts(const GrAAConvexTessellator& tess,
Brian Osmanf9aabff2018-11-13 16:11:38 -050086 void* vertData,
Brian Osman80879d42019-01-07 16:15:27 -050087 const GrVertexColor& color,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070088 uint16_t firstIndex,
Brian Osman0995fd52019-01-09 09:52:25 -050089 uint16_t* idxs) {
Brian Osmanf9aabff2018-11-13 16:11:38 -050090 GrVertexWriter verts{vertData};
Brian Osmanf3e6b902018-12-28 17:43:50 +000091 for (int i = 0; i < tess.numPts(); ++i) {
Brian Osman80879d42019-01-07 16:15:27 -050092 verts.write(tess.point(i), color, tess.coverage(i));
ethannicholas1a1b3ac2015-06-10 12:11:17 -070093 }
94
95 for (int i = 0; i < tess.numIndices(); ++i) {
96 idxs[i] = tess.index(i) + firstIndex;
97 }
98}
99
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500100static GrGeometryProcessor* create_lines_only_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500101 bool tweakAlphaForCoverage,
102 const SkMatrix& viewMatrix,
103 bool usesLocalCoords,
104 bool wideColor) {
joshualittdf0c5572015-08-03 11:35:28 -0700105 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700106
Brian Osman80879d42019-01-07 16:15:27 -0500107 Coverage::Type coverageType =
108 tweakAlphaForCoverage ? Coverage::kAttributeTweakAlpha_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500109 LocalCoords::Type localCoordsType =
Brian Osman80879d42019-01-07 16:15:27 -0500110 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
111 Color::Type colorType =
112 wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type;
113
Brian Osmanf0aee742020-03-12 09:28:44 -0400114 return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700115}
116
Brian Salomonb2955732017-07-13 16:42:55 -0400117namespace {
118
119class AAFlatteningConvexPathOp final : public GrMeshDrawOp {
120private:
121 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
122
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700123public:
Brian Salomon25a88092016-12-01 09:36:50 -0500124 DEFINE_OP_CLASS_ID
Robert Phillipsb97da532019-02-12 15:24:12 -0500125 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400126 GrPaint&& paint,
Brian Salomonb2955732017-07-13 16:42:55 -0400127 const SkMatrix& viewMatrix,
128 const SkPath& path,
129 SkScalar strokeWidth,
130 SkStrokeRec::Style style,
131 SkPaint::Join join,
132 SkScalar miterLimit,
133 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400134 return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint),
135 viewMatrix, path,
Brian Salomonb2955732017-07-13 16:42:55 -0400136 strokeWidth, style, join, miterLimit,
137 stencilSettings);
Brian Salomon780dad12016-12-15 18:08:40 -0500138 }
139
Brian Salomonb2955732017-07-13 16:42:55 -0400140 AAFlatteningConvexPathOp(const Helper::MakeArgs& helperArgs,
Brian Osmancf860852018-10-31 14:04:39 -0400141 const SkPMColor4f& color,
Brian Salomon780dad12016-12-15 18:08:40 -0500142 const SkMatrix& viewMatrix,
143 const SkPath& path,
144 SkScalar strokeWidth,
145 SkStrokeRec::Style style,
146 SkPaint::Join join,
Brian Salomonb2955732017-07-13 16:42:55 -0400147 SkScalar miterLimit,
148 const GrUserStencilSettings* stencilSettings)
Brian Salomonc70fd902020-03-23 10:39:39 -0400149 : INHERITED(ClassID())
150 , fHelper(helperArgs, GrAAType::kCoverage, stencilSettings)
151 , fViewMatrix(viewMatrix) {
152 fPaths.emplace_back(PathData{color, path, strokeWidth, style, join, miterLimit});
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700153
bsalomon0432dd62016-06-30 07:19:27 -0700154 // compute bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700155 SkRect bounds = path.getBounds();
bsalomon0432dd62016-06-30 07:19:27 -0700156 SkScalar w = strokeWidth;
157 if (w > 0) {
158 w /= 2;
Greg Daniel55e5be32019-09-30 12:23:26 -0400159 SkScalar maxScale = viewMatrix.getMaxScale();
160 // We should not have a perspective matrix, thus we should have a valid scale.
161 SkASSERT(maxScale != -1);
162 if (SkPaint::kMiter_Join == join && w * maxScale > 1.f) {
bsalomon0432dd62016-06-30 07:19:27 -0700163 w *= miterLimit;
164 }
bsalomon88cf17d2016-07-08 06:40:56 -0700165 bounds.outset(w, w);
bsalomon0432dd62016-06-30 07:19:27 -0700166 }
Greg Daniel5faf4742019-10-01 15:14:44 -0400167 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsHairline::kNo);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700168 }
169
Brian Salomonb2955732017-07-13 16:42:55 -0400170 const char* name() const override { return "AAFlatteningConvexPathOp"; }
171
Chris Dalton1706cbf2019-05-21 19:35:29 -0600172 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400173 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600174 fProgramInfo->visitFPProxies(func);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400175 } else {
176 fHelper.visitProxies(func);
177 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400178 }
179
Brian Osman9a390ac2018-11-12 09:47:48 -0500180#ifdef SK_DEBUG
Brian Salomonb2955732017-07-13 16:42:55 -0400181 SkString dumpInfo() const override {
182 SkString string;
183 for (const auto& path : fPaths) {
184 string.appendf(
185 "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, "
186 "MiterLimit: %.2f\n",
Brian Osmancf860852018-10-31 14:04:39 -0400187 path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400188 path.fMiterLimit);
Brian Salomonb2955732017-07-13 16:42:55 -0400189 }
190 string += fHelper.dumpInfo();
191 string += INHERITED::dumpInfo();
192 return string;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500193 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500194#endif
Brian Salomon92aee3d2016-12-21 09:20:25 -0500195
Brian Salomonb2955732017-07-13 16:42:55 -0400196 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
197
Chris Dalton6ce447a2019-06-23 18:07:38 -0600198 GrProcessorSet::Analysis finalize(
199 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
200 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700201 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600202 caps, clip, hasMixedSampledCoverage, clampType,
203 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700204 }
205
Brian Salomonb2955732017-07-13 16:42:55 -0400206private:
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400207 GrProgramInfo* programInfo() override { return fProgramInfo; }
Robert Phillips2669a7b2020-03-12 12:07:19 -0400208
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400209 void onCreateProgramInfo(const GrCaps* caps,
210 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400211 const GrSurfaceProxyView* writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400212 GrAppliedClip&& appliedClip,
213 const GrXferProcessor::DstProxyView& dstProxyView) override {
214 GrGeometryProcessor* gp = create_lines_only_gp(arena,
215 fHelper.compatibleWithCoverageAsAlpha(),
Brian Salomonc70fd902020-03-23 10:39:39 -0400216 fViewMatrix,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400217 fHelper.usesLocalCoords(),
218 fWideColor);
219 if (!gp) {
220 SkDebugf("Couldn't create a GrGeometryProcessor\n");
221 return;
222 }
223
Brian Salomon8afde5f2020-04-01 16:22:00 -0400224 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400225 std::move(appliedClip), dstProxyView,
226 gp, GrPrimitiveType::kTriangles);
Robert Phillips4133dc42020-03-11 15:55:55 -0400227 }
228
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400229 void recordDraw(Target* target,
230 int vertexCount, size_t vertexStride, void* vertices,
231 int indexCount, uint16_t* indices) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700232 if (vertexCount == 0 || indexCount == 0) {
233 return;
234 }
Brian Salomon12d22642019-01-29 14:38:50 -0500235 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600236 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700237 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600238 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700239 if (!verts) {
240 SkDebugf("Could not allocate vertices\n");
241 return;
242 }
243 memcpy(verts, vertices, vertexCount * vertexStride);
244
Brian Salomon12d22642019-01-29 14:38:50 -0500245 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600246 int firstIndex;
247 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700248 if (!idxs) {
249 SkDebugf("Could not allocate indices\n");
250 return;
251 }
252 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
Chris Daltoneb694b72020-03-16 09:25:50 -0600253 GrSimpleMesh* mesh = target->allocMesh();
Brian Salomon12d22642019-01-29 14:38:50 -0500254 mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600255 GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400256 fMeshes.push_back(mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700257 }
mtklein002c2ce2015-08-26 05:43:22 -0700258
Brian Salomon91326c32017-08-09 16:02:19 -0400259 void onPrepareDraws(Target* target) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400260 if (!fProgramInfo) {
261 this->createProgramInfo(target);
262 if (!fProgramInfo) {
263 return;
264 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700265 }
266
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400267 size_t vertexStride = fProgramInfo->primProc().vertexStride();
Brian Salomon780dad12016-12-15 18:08:40 -0500268 int instanceCount = fPaths.count();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700269
Greg Danield5b45932018-06-07 13:15:10 -0400270 int64_t vertexCount = 0;
271 int64_t indexCount = 0;
272 int64_t maxVertices = DEFAULT_BUFFER_SIZE;
273 int64_t maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700274 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
275 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700276 for (int i = 0; i < instanceCount; i++) {
Brian Salomon780dad12016-12-15 18:08:40 -0500277 const PathData& args = fPaths[i];
robertphillips8c170972016-09-22 12:42:30 -0700278 GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth,
279 args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700280
Brian Salomonc70fd902020-03-23 10:39:39 -0400281 if (!tess.tessellate(fViewMatrix, args.fPath)) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700282 continue;
283 }
284
Greg Danield5b45932018-06-07 13:15:10 -0400285 int currentVertices = tess.numPts();
286 if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) {
mtklein002c2ce2015-08-26 05:43:22 -0700287 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700288 // uint16_t. Draw what we've got so far and reset.
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400289 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700290 vertexCount = 0;
291 indexCount = 0;
292 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700293 if (vertexCount + currentVertices > maxVertices) {
Brian Osman788b9162020-02-07 10:36:46 -0500294 maxVertices = std::max(vertexCount + currentVertices, maxVertices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400295 if (maxVertices * vertexStride > SK_MaxS32) {
296 sk_free(vertices);
297 sk_free(indices);
298 return;
299 }
mtklein002c2ce2015-08-26 05:43:22 -0700300 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700301 }
Greg Danield5b45932018-06-07 13:15:10 -0400302 int currentIndices = tess.numIndices();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700303 if (indexCount + currentIndices > maxIndices) {
Brian Osman788b9162020-02-07 10:36:46 -0500304 maxIndices = std::max(indexCount + currentIndices, maxIndices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400305 if (maxIndices * sizeof(uint16_t) > SK_MaxS32) {
306 sk_free(vertices);
307 sk_free(indices);
308 return;
309 }
mtklein002c2ce2015-08-26 05:43:22 -0700310 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700311 }
312
Brian Osman80879d42019-01-07 16:15:27 -0500313 extract_verts(tess, vertices + vertexStride * vertexCount,
Brian Osman0995fd52019-01-09 09:52:25 -0500314 GrVertexColor(args.fColor, fWideColor), vertexCount,
315 indices + indexCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700316 vertexCount += currentVertices;
317 indexCount += currentIndices;
318 }
Greg Danield5b45932018-06-07 13:15:10 -0400319 if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400320 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
Greg Danield5b45932018-06-07 13:15:10 -0400321 }
mtklein002c2ce2015-08-26 05:43:22 -0700322 sk_free(vertices);
323 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700324 }
325
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700326 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400327 if (!fProgramInfo || fMeshes.isEmpty()) {
328 return;
329 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500330
Chris Dalton765ed362020-03-16 17:34:44 -0600331 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
332 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400333 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600334 flushState->drawMesh(*fMeshes[i]);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400335 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700336 }
337
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500338 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
339 const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500340 AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>();
Brian Salomonb2955732017-07-13 16:42:55 -0400341 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000342 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700343 }
Brian Salomonc70fd902020-03-23 10:39:39 -0400344 if (fViewMatrix != that->fViewMatrix) {
345 return CombineResult::kCannotCombine;
346 }
joshualitt8cab9a72015-07-16 09:13:50 -0700347
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
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700353
Brian Salomon780dad12016-12-15 18:08:40 -0500354 struct PathData {
Brian Osmancf860852018-10-31 14:04:39 -0400355 SkPMColor4f fColor;
bsalomon0432dd62016-06-30 07:19:27 -0700356 SkPath fPath;
357 SkScalar fStrokeWidth;
robertphillips8c170972016-09-22 12:42:30 -0700358 SkStrokeRec::Style fStyle;
bsalomon0432dd62016-06-30 07:19:27 -0700359 SkPaint::Join fJoin;
360 SkScalar fMiterLimit;
361 };
362
Brian Salomon780dad12016-12-15 18:08:40 -0500363 SkSTArray<1, PathData, true> fPaths;
Brian Salomonb2955732017-07-13 16:42:55 -0400364 Helper fHelper;
Brian Salomonc70fd902020-03-23 10:39:39 -0400365 SkMatrix fViewMatrix;
Brian Osman80879d42019-01-07 16:15:27 -0500366 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700367
Chris Daltoneb694b72020-03-16 09:25:50 -0600368 SkTDArray<GrSimpleMesh*> fMeshes;
369 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400370
Brian Salomonb2955732017-07-13 16:42:55 -0400371 typedef GrMeshDrawOp INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700372};
373
Brian Salomonb2955732017-07-13 16:42:55 -0400374} // anonymous namespace
375
bsalomon0aff2fa2015-07-31 06:48:27 -0700376bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400377 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800378 "GrAALinearizingConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600379 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700380 SkASSERT(!args.fShape->isEmpty());
robertphillips8c170972016-09-22 12:42:30 -0700381 SkASSERT(!args.fShape->style().pathEffect());
csmartdaltonecbc12b2016-06-08 10:08:43 -0700382
bsalomon0432dd62016-06-30 07:19:27 -0700383 SkPath path;
384 args.fShape->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700385 bool fill = args.fShape->style().isSimpleFill();
386 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
bsalomon0432dd62016-06-30 07:19:27 -0700387 SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth();
388 SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin();
389 SkScalar miterLimit = stroke.getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700390
Brian Salomonb2955732017-07-13 16:42:55 -0400391 std::unique_ptr<GrDrawOp> op = AAFlatteningConvexPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400392 args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth,
393 stroke.getStyle(), join, miterLimit, args.fUserStencilSettings);
Brian Salomonb2955732017-07-13 16:42:55 -0400394 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700395 return true;
396}
397
398///////////////////////////////////////////////////////////////////////////////////////////////////
399
Hal Canary6f6961e2017-01-31 13:50:44 -0500400#if GR_TEST_UTILS
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700401
Brian Salomonb2955732017-07-13 16:42:55 -0400402GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) {
Brian Salomon780dad12016-12-15 18:08:40 -0500403 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
bsalomon0432dd62016-06-30 07:19:27 -0700404 SkPath path = GrTest::TestPathConvex(random);
robertphillips8c170972016-09-22 12:42:30 -0700405
406 SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style,
Herb Derby60c05f92016-12-13 15:18:55 -0500407 SkStrokeRec::kStroke_Style,
robertphillips8c170972016-09-22 12:42:30 -0700408 SkStrokeRec::kStrokeAndFill_Style };
409
410 SkStrokeRec::Style style = styles[random->nextU() % 3];
411
412 SkScalar strokeWidth = -1.f;
bsalomon0432dd62016-06-30 07:19:27 -0700413 SkPaint::Join join = SkPaint::kMiter_Join;
414 SkScalar miterLimit = 0.5f;
robertphillips8c170972016-09-22 12:42:30 -0700415
416 if (SkStrokeRec::kFill_Style != style) {
417 strokeWidth = random->nextRangeF(1.0f, 10.0f);
418 if (random->nextBool()) {
419 join = SkPaint::kMiter_Join;
420 } else {
421 join = SkPaint::kBevel_Join;
422 }
423 miterLimit = random->nextRangeF(0.5f, 2.0f);
424 }
Brian Salomonb2955732017-07-13 16:42:55 -0400425 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400426 return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth,
427 style, join, miterLimit, stencilSettings);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700428}
429
430#endif