blob: 0114673945469865b90f1df22908d9fc584e1b89 [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
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 Salomonb2955732017-07-13 16:42:55 -0400189 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
190
Chris Dalton6ce447a2019-06-23 18:07:38 -0600191 GrProcessorSet::Analysis finalize(
192 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
193 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700194 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600195 caps, clip, hasMixedSampledCoverage, clampType,
196 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700197 }
198
Brian Salomonb2955732017-07-13 16:42:55 -0400199private:
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400200 GrProgramInfo* programInfo() override { return fProgramInfo; }
Robert Phillips2669a7b2020-03-12 12:07:19 -0400201
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400202 void onCreateProgramInfo(const GrCaps* caps,
203 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400204 const GrSurfaceProxyView* writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400205 GrAppliedClip&& appliedClip,
206 const GrXferProcessor::DstProxyView& dstProxyView) override {
207 GrGeometryProcessor* gp = create_lines_only_gp(arena,
208 fHelper.compatibleWithCoverageAsAlpha(),
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400209 fHelper.usesLocalCoords(),
210 fWideColor);
211 if (!gp) {
212 SkDebugf("Couldn't create a GrGeometryProcessor\n");
213 return;
214 }
215
Brian Salomon8afde5f2020-04-01 16:22:00 -0400216 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400217 std::move(appliedClip), dstProxyView,
218 gp, GrPrimitiveType::kTriangles);
Robert Phillips4133dc42020-03-11 15:55:55 -0400219 }
220
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400221 void recordDraw(Target* target,
222 int vertexCount, size_t vertexStride, void* vertices,
223 int indexCount, uint16_t* indices) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700224 if (vertexCount == 0 || indexCount == 0) {
225 return;
226 }
Brian Salomon12d22642019-01-29 14:38:50 -0500227 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600228 int firstVertex;
mtklein002c2ce2015-08-26 05:43:22 -0700229 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer,
Chris Daltonbca46e22017-05-15 11:03:26 -0600230 &firstVertex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700231 if (!verts) {
232 SkDebugf("Could not allocate vertices\n");
233 return;
234 }
235 memcpy(verts, vertices, vertexCount * vertexStride);
236
Brian Salomon12d22642019-01-29 14:38:50 -0500237 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600238 int firstIndex;
239 uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700240 if (!idxs) {
241 SkDebugf("Could not allocate indices\n");
242 return;
243 }
244 memcpy(idxs, indices, indexCount * sizeof(uint16_t));
Chris Daltoneb694b72020-03-16 09:25:50 -0600245 GrSimpleMesh* mesh = target->allocMesh();
Brian Salomon12d22642019-01-29 14:38:50 -0500246 mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600247 GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400248 fMeshes.push_back(mesh);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700249 }
mtklein002c2ce2015-08-26 05:43:22 -0700250
Brian Salomon91326c32017-08-09 16:02:19 -0400251 void onPrepareDraws(Target* target) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400252 if (!fProgramInfo) {
253 this->createProgramInfo(target);
254 if (!fProgramInfo) {
255 return;
256 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700257 }
258
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400259 size_t vertexStride = fProgramInfo->primProc().vertexStride();
Brian Salomon780dad12016-12-15 18:08:40 -0500260 int instanceCount = fPaths.count();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700261
Greg Danield5b45932018-06-07 13:15:10 -0400262 int64_t vertexCount = 0;
263 int64_t indexCount = 0;
264 int64_t maxVertices = DEFAULT_BUFFER_SIZE;
265 int64_t maxIndices = DEFAULT_BUFFER_SIZE;
mtklein002c2ce2015-08-26 05:43:22 -0700266 uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride);
267 uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700268 for (int i = 0; i < instanceCount; i++) {
Brian Salomon780dad12016-12-15 18:08:40 -0500269 const PathData& args = fPaths[i];
robertphillips8c170972016-09-22 12:42:30 -0700270 GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth,
271 args.fJoin, args.fMiterLimit);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700272
Brian Salomona3762ee2020-04-10 09:16:04 -0400273 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700274 continue;
275 }
276
Greg Danield5b45932018-06-07 13:15:10 -0400277 int currentVertices = tess.numPts();
278 if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) {
mtklein002c2ce2015-08-26 05:43:22 -0700279 // if we added the current instance, we would overflow the indices we can store in a
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700280 // uint16_t. Draw what we've got so far and reset.
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400281 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700282 vertexCount = 0;
283 indexCount = 0;
284 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700285 if (vertexCount + currentVertices > maxVertices) {
Brian Osman788b9162020-02-07 10:36:46 -0500286 maxVertices = std::max(vertexCount + currentVertices, maxVertices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400287 if (maxVertices * vertexStride > SK_MaxS32) {
288 sk_free(vertices);
289 sk_free(indices);
290 return;
291 }
mtklein002c2ce2015-08-26 05:43:22 -0700292 vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700293 }
Greg Danield5b45932018-06-07 13:15:10 -0400294 int currentIndices = tess.numIndices();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700295 if (indexCount + currentIndices > maxIndices) {
Brian Osman788b9162020-02-07 10:36:46 -0500296 maxIndices = std::max(indexCount + currentIndices, maxIndices * 2);
Greg Danield5b45932018-06-07 13:15:10 -0400297 if (maxIndices * sizeof(uint16_t) > SK_MaxS32) {
298 sk_free(vertices);
299 sk_free(indices);
300 return;
301 }
mtklein002c2ce2015-08-26 05:43:22 -0700302 indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700303 }
304
Brian Salomona3762ee2020-04-10 09:16:04 -0400305 const SkMatrix* localCoordsMatrix = nullptr;
306 SkMatrix ivm;
307 if (fHelper.usesLocalCoords()) {
308 if (!args.fViewMatrix.invert(&ivm)) {
309 ivm = SkMatrix::I();
310 }
311 localCoordsMatrix = &ivm;
312 }
313
314 extract_verts(tess, localCoordsMatrix, vertices + vertexStride * vertexCount,
Brian Osman0995fd52019-01-09 09:52:25 -0500315 GrVertexColor(args.fColor, fWideColor), vertexCount,
316 indices + indexCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700317 vertexCount += currentVertices;
318 indexCount += currentIndices;
319 }
Greg Danield5b45932018-06-07 13:15:10 -0400320 if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400321 this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices);
Greg Danield5b45932018-06-07 13:15:10 -0400322 }
mtklein002c2ce2015-08-26 05:43:22 -0700323 sk_free(vertices);
324 sk_free(indices);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700325 }
326
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700327 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400328 if (!fProgramInfo || fMeshes.isEmpty()) {
329 return;
330 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500331
Chris Dalton765ed362020-03-16 17:34:44 -0600332 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
333 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400334 for (int i = 0; i < fMeshes.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600335 flushState->drawMesh(*fMeshes[i]);
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400336 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700337 }
338
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500339 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
340 const GrCaps& caps) override {
Brian Salomon780dad12016-12-15 18:08:40 -0500341 AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>();
Brian Salomonb2955732017-07-13 16:42:55 -0400342 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000343 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700344 }
345
Brian Salomon780dad12016-12-15 18:08:40 -0500346 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman80879d42019-01-07 16:15:27 -0500347 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000348 return CombineResult::kMerged;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700349 }
350
John Stilesaf366522020-08-13 09:57:34 -0400351#if GR_TEST_UTILS
352 SkString onDumpInfo() const override {
353 SkString string;
354 for (const auto& path : fPaths) {
355 string.appendf(
356 "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, "
357 "MiterLimit: %.2f\n",
358 path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin,
359 path.fMiterLimit);
360 }
361 string += fHelper.dumpInfo();
362 return string;
363 }
364#endif
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700365
Brian Salomon780dad12016-12-15 18:08:40 -0500366 struct PathData {
Brian Salomona3762ee2020-04-10 09:16:04 -0400367 SkMatrix fViewMatrix;
bsalomon0432dd62016-06-30 07:19:27 -0700368 SkPath fPath;
Brian Salomona3762ee2020-04-10 09:16:04 -0400369 SkPMColor4f fColor;
bsalomon0432dd62016-06-30 07:19:27 -0700370 SkScalar fStrokeWidth;
Brian Salomona3762ee2020-04-10 09:16:04 -0400371 SkScalar fMiterLimit;
robertphillips8c170972016-09-22 12:42:30 -0700372 SkStrokeRec::Style fStyle;
bsalomon0432dd62016-06-30 07:19:27 -0700373 SkPaint::Join fJoin;
bsalomon0432dd62016-06-30 07:19:27 -0700374 };
375
Brian Salomon780dad12016-12-15 18:08:40 -0500376 SkSTArray<1, PathData, true> fPaths;
Brian Salomonb2955732017-07-13 16:42:55 -0400377 Helper fHelper;
Brian Osman80879d42019-01-07 16:15:27 -0500378 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700379
Chris Daltoneb694b72020-03-16 09:25:50 -0600380 SkTDArray<GrSimpleMesh*> fMeshes;
381 GrProgramInfo* fProgramInfo = nullptr;
Robert Phillipsa9e28af2020-03-13 08:40:12 -0400382
Brian Salomonb2955732017-07-13 16:42:55 -0400383 typedef GrMeshDrawOp INHERITED;
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700384};
385
Brian Salomonb2955732017-07-13 16:42:55 -0400386} // anonymous namespace
387
bsalomon0aff2fa2015-07-31 06:48:27 -0700388bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400389 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
joshualittde83b412016-01-14 09:58:36 -0800390 "GrAALinearizingConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600391 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700392 SkASSERT(!args.fShape->isEmpty());
robertphillips8c170972016-09-22 12:42:30 -0700393 SkASSERT(!args.fShape->style().pathEffect());
csmartdaltonecbc12b2016-06-08 10:08:43 -0700394
bsalomon0432dd62016-06-30 07:19:27 -0700395 SkPath path;
396 args.fShape->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700397 bool fill = args.fShape->style().isSimpleFill();
398 const SkStrokeRec& stroke = args.fShape->style().strokeRec();
bsalomon0432dd62016-06-30 07:19:27 -0700399 SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth();
400 SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin();
401 SkScalar miterLimit = stroke.getMiter();
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700402
Brian Salomonb2955732017-07-13 16:42:55 -0400403 std::unique_ptr<GrDrawOp> op = AAFlatteningConvexPathOp::Make(
Robert Phillips7c525e62018-06-12 10:11:12 -0400404 args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth,
405 stroke.getStyle(), join, miterLimit, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400406 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700407 return true;
408}
409
410///////////////////////////////////////////////////////////////////////////////////////////////////
411
Hal Canary6f6961e2017-01-31 13:50:44 -0500412#if GR_TEST_UTILS
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700413
Brian Salomonb2955732017-07-13 16:42:55 -0400414GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) {
Brian Salomon780dad12016-12-15 18:08:40 -0500415 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
John Stiles31954bf2020-08-07 17:35:54 -0400416 const SkPath& path = GrTest::TestPathConvex(random);
robertphillips8c170972016-09-22 12:42:30 -0700417
418 SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style,
Herb Derby60c05f92016-12-13 15:18:55 -0500419 SkStrokeRec::kStroke_Style,
robertphillips8c170972016-09-22 12:42:30 -0700420 SkStrokeRec::kStrokeAndFill_Style };
421
422 SkStrokeRec::Style style = styles[random->nextU() % 3];
423
424 SkScalar strokeWidth = -1.f;
bsalomon0432dd62016-06-30 07:19:27 -0700425 SkPaint::Join join = SkPaint::kMiter_Join;
426 SkScalar miterLimit = 0.5f;
robertphillips8c170972016-09-22 12:42:30 -0700427
428 if (SkStrokeRec::kFill_Style != style) {
429 strokeWidth = random->nextRangeF(1.0f, 10.0f);
430 if (random->nextBool()) {
431 join = SkPaint::kMiter_Join;
432 } else {
433 join = SkPaint::kBevel_Join;
434 }
435 miterLimit = random->nextRangeF(0.5f, 2.0f);
436 }
Brian Salomonb2955732017-07-13 16:42:55 -0400437 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400438 return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth,
439 style, join, miterLimit, stencilSettings);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700440}
441
442#endif