ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkString.h" |
| 9 | #include "src/core/SkGeometry.h" |
| 10 | #include "src/core/SkPathPriv.h" |
| 11 | #include "src/core/SkTraceEvent.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrProcessor.h" |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/gpu/GrStyle.h" |
| 22 | #include "src/gpu/GrVertexWriter.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 23 | #include "src/gpu/geometry/GrPathUtils.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 24 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #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 Phillips | 55f681f | 2020-02-28 08:58:15 -0500 | [diff] [blame] | 29 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 30 | |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 31 | static 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. |
| 35 | static const SkScalar kMaxStrokeWidth = 20.0; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 36 | |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 37 | GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() = default; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 41 | GrPathRenderer::CanDrawPath |
| 42 | GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 43 | if (GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 44 | return CanDrawPath::kNo; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 45 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 46 | if (!args.fShape->knownToBeConvex()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 47 | return CanDrawPath::kNo; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 48 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 49 | if (args.fShape->style().pathEffect()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 50 | return CanDrawPath::kNo; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 51 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 52 | if (args.fShape->inverseFilled()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 53 | return CanDrawPath::kNo; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 54 | } |
Brian Osman | a98e399 | 2017-06-26 15:14:53 -0400 | [diff] [blame] | 55 | 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 Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 57 | return CanDrawPath::kNo; |
Brian Osman | a98e399 | 2017-06-26 15:14:53 -0400 | [diff] [blame] | 58 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 59 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 60 | |
| 61 | if (stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 62 | stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style) { |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 63 | if (!args.fViewMatrix->isSimilarity()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 64 | return CanDrawPath::kNo; |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 65 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 66 | SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * stroke.getWidth(); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 67 | if (strokeWidth < 1.0f && stroke.getStyle() == SkStrokeRec::kStroke_Style) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 68 | return CanDrawPath::kNo; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 69 | } |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 70 | if (strokeWidth > kMaxStrokeWidth || |
| 71 | !args.fShape->knownToBeClosed() || |
| 72 | stroke.getJoin() == SkPaint::Join::kRound_Join) { |
| 73 | return CanDrawPath::kNo; |
| 74 | } |
| 75 | return CanDrawPath::kYes; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 76 | } |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 77 | if (stroke.getStyle() != SkStrokeRec::kFill_Style) { |
| 78 | return CanDrawPath::kNo; |
| 79 | } |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 80 | // 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 Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 86 | return CanDrawPath::kYes; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // extract the result vertices and indices from the GrAAConvexTessellator |
| 90 | static void extract_verts(const GrAAConvexTessellator& tess, |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 91 | const SkMatrix* localCoordsMatrix, |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 92 | void* vertData, |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 93 | const GrVertexColor& color, |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 94 | uint16_t firstIndex, |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 95 | uint16_t* idxs) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 96 | GrVertexWriter verts{vertData}; |
Brian Osman | f3e6b90 | 2018-12-28 17:43:50 +0000 | [diff] [blame] | 97 | for (int i = 0; i < tess.numPts(); ++i) { |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 98 | SkPoint lc; |
| 99 | if (localCoordsMatrix) { |
| 100 | localCoordsMatrix->mapPoints(&lc, &tess.point(i), 1); |
| 101 | } |
Brian Salomon | daf94c5 | 2020-04-10 15:42:27 -0400 | [diff] [blame] | 102 | verts.write(tess.point(i), color, GrVertexWriter::If(localCoordsMatrix, lc), |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 103 | tess.coverage(i)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | for (int i = 0; i < tess.numIndices(); ++i) { |
| 107 | idxs[i] = tess.index(i) + firstIndex; |
| 108 | } |
| 109 | } |
| 110 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 111 | static GrGeometryProcessor* create_lines_only_gp(SkArenaAlloc* arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 112 | bool tweakAlphaForCoverage, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 113 | bool usesLocalCoords, |
| 114 | bool wideColor) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 115 | using namespace GrDefaultGeoProcFactory; |
joshualitt | e494a58 | 2015-08-03 09:32:36 -0700 | [diff] [blame] | 116 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 117 | Coverage::Type coverageType = |
| 118 | tweakAlphaForCoverage ? Coverage::kAttributeTweakAlpha_Type : Coverage::kAttribute_Type; |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 119 | LocalCoords::Type localCoordsType = |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 120 | usesLocalCoords ? LocalCoords::kHasExplicit_Type : LocalCoords::kUnused_Type; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 121 | Color::Type colorType = |
| 122 | wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type; |
| 123 | |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 124 | return Make(arena, colorType, coverageType, localCoordsType, SkMatrix::I()); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 127 | namespace { |
| 128 | |
| 129 | class AAFlatteningConvexPathOp final : public GrMeshDrawOp { |
| 130 | private: |
| 131 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 132 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 133 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 134 | DEFINE_OP_CLASS_ID |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 135 | |
| 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 Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 145 | return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint), |
| 146 | viewMatrix, path, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 147 | strokeWidth, style, join, miterLimit, |
| 148 | stencilSettings); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 151 | AAFlatteningConvexPathOp(GrProcessorSet* processorSet, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 152 | const SkPMColor4f& color, |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 153 | const SkMatrix& viewMatrix, |
| 154 | const SkPath& path, |
| 155 | SkScalar strokeWidth, |
| 156 | SkStrokeRec::Style style, |
| 157 | SkPaint::Join join, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 158 | SkScalar miterLimit, |
| 159 | const GrUserStencilSettings* stencilSettings) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 160 | : INHERITED(ClassID()), fHelper(processorSet, GrAAType::kCoverage, stencilSettings) { |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 161 | fPaths.emplace_back( |
| 162 | PathData{viewMatrix, path, color, strokeWidth, miterLimit, style, join}); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 163 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 164 | // compute bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 165 | SkRect bounds = path.getBounds(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 166 | SkScalar w = strokeWidth; |
| 167 | if (w > 0) { |
| 168 | w /= 2; |
Greg Daniel | 55e5be3 | 2019-09-30 12:23:26 -0400 | [diff] [blame] | 169 | 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) { |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 173 | w *= miterLimit; |
| 174 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 175 | bounds.outset(w, w); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 176 | } |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 177 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsHairline::kNo); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 180 | const char* name() const override { return "AAFlatteningConvexPathOp"; } |
| 181 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 182 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 183 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 184 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 185 | } else { |
| 186 | fHelper.visitProxies(func); |
| 187 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 190 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 191 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 192 | GrProcessorSet::Analysis finalize( |
| 193 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 194 | GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 195 | return fHelper.finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 196 | caps, clip, hasMixedSampledCoverage, clampType, |
| 197 | GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 200 | private: |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 201 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 202 | |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 203 | void onCreateProgramInfo(const GrCaps* caps, |
| 204 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 205 | const GrSurfaceProxyView* writeView, |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 206 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 207 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 208 | GrXferBarrierFlags renderPassXferBarriers) override { |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 209 | GrGeometryProcessor* gp = create_lines_only_gp(arena, |
| 210 | fHelper.compatibleWithCoverageAsAlpha(), |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 211 | fHelper.usesLocalCoords(), |
| 212 | fWideColor); |
| 213 | if (!gp) { |
| 214 | SkDebugf("Couldn't create a GrGeometryProcessor\n"); |
| 215 | return; |
| 216 | } |
| 217 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 218 | fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 219 | std::move(appliedClip), dstProxyView, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 220 | gp, GrPrimitiveType::kTriangles, |
| 221 | renderPassXferBarriers); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 222 | } |
| 223 | |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 224 | void recordDraw(Target* target, |
| 225 | int vertexCount, size_t vertexStride, void* vertices, |
| 226 | int indexCount, uint16_t* indices) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 227 | if (vertexCount == 0 || indexCount == 0) { |
| 228 | return; |
| 229 | } |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 230 | sk_sp<const GrBuffer> vertexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 231 | int firstVertex; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 232 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 233 | &firstVertex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 234 | if (!verts) { |
| 235 | SkDebugf("Could not allocate vertices\n"); |
| 236 | return; |
| 237 | } |
| 238 | memcpy(verts, vertices, vertexCount * vertexStride); |
| 239 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 240 | sk_sp<const GrBuffer> indexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 241 | int firstIndex; |
| 242 | uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 243 | if (!idxs) { |
| 244 | SkDebugf("Could not allocate indices\n"); |
| 245 | return; |
| 246 | } |
| 247 | memcpy(idxs, indices, indexCount * sizeof(uint16_t)); |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 248 | GrSimpleMesh* mesh = target->allocMesh(); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 249 | mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1, |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 250 | GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex); |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 251 | fMeshes.push_back(mesh); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 252 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 253 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 254 | void onPrepareDraws(Target* target) override { |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 255 | if (!fProgramInfo) { |
| 256 | this->createProgramInfo(target); |
| 257 | if (!fProgramInfo) { |
| 258 | return; |
| 259 | } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 262 | size_t vertexStride = fProgramInfo->primProc().vertexStride(); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 263 | int instanceCount = fPaths.count(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 264 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 265 | int64_t vertexCount = 0; |
| 266 | int64_t indexCount = 0; |
| 267 | int64_t maxVertices = DEFAULT_BUFFER_SIZE; |
| 268 | int64_t maxIndices = DEFAULT_BUFFER_SIZE; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 269 | uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride); |
| 270 | uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 271 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 272 | const PathData& args = fPaths[i]; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 273 | GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth, |
| 274 | args.fJoin, args.fMiterLimit); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 275 | |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 276 | if (!tess.tessellate(args.fViewMatrix, args.fPath)) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 277 | continue; |
| 278 | } |
| 279 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 280 | int currentVertices = tess.numPts(); |
| 281 | if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) { |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 282 | // if we added the current instance, we would overflow the indices we can store in a |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 283 | // uint16_t. Draw what we've got so far and reset. |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 284 | this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 285 | vertexCount = 0; |
| 286 | indexCount = 0; |
| 287 | } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 288 | if (vertexCount + currentVertices > maxVertices) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 289 | maxVertices = std::max(vertexCount + currentVertices, maxVertices * 2); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 290 | if (maxVertices * vertexStride > SK_MaxS32) { |
| 291 | sk_free(vertices); |
| 292 | sk_free(indices); |
| 293 | return; |
| 294 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 295 | vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 296 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 297 | int currentIndices = tess.numIndices(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 298 | if (indexCount + currentIndices > maxIndices) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 299 | maxIndices = std::max(indexCount + currentIndices, maxIndices * 2); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 300 | if (maxIndices * sizeof(uint16_t) > SK_MaxS32) { |
| 301 | sk_free(vertices); |
| 302 | sk_free(indices); |
| 303 | return; |
| 304 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 305 | indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 308 | 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 Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 318 | GrVertexColor(args.fColor, fWideColor), vertexCount, |
| 319 | indices + indexCount); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 320 | vertexCount += currentVertices; |
| 321 | indexCount += currentIndices; |
| 322 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 323 | if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) { |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 324 | this->recordDraw(target, vertexCount, vertexStride, vertices, indexCount, indices); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 325 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 326 | sk_free(vertices); |
| 327 | sk_free(indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 330 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 331 | if (!fProgramInfo || fMeshes.isEmpty()) { |
| 332 | return; |
| 333 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 334 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 335 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 336 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 337 | for (int i = 0; i < fMeshes.count(); ++i) { |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 338 | flushState->drawMesh(*fMeshes[i]); |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 339 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 342 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 343 | const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 344 | AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>(); |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 345 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 346 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 349 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 350 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 351 | return CombineResult::kMerged; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 352 | } |
| 353 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 354 | #if GR_TEST_UTILS |
| 355 | SkString onDumpInfo() const override { |
| 356 | SkString string; |
| 357 | for (const auto& path : fPaths) { |
| 358 | string.appendf( |
| 359 | "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, " |
| 360 | "MiterLimit: %.2f\n", |
| 361 | path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin, |
| 362 | path.fMiterLimit); |
| 363 | } |
| 364 | string += fHelper.dumpInfo(); |
| 365 | return string; |
| 366 | } |
| 367 | #endif |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 368 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 369 | struct PathData { |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 370 | SkMatrix fViewMatrix; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 371 | SkPath fPath; |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 372 | SkPMColor4f fColor; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 373 | SkScalar fStrokeWidth; |
Brian Salomon | a3762ee | 2020-04-10 09:16:04 -0400 | [diff] [blame] | 374 | SkScalar fMiterLimit; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 375 | SkStrokeRec::Style fStyle; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 376 | SkPaint::Join fJoin; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 379 | SkSTArray<1, PathData, true> fPaths; |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 380 | Helper fHelper; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 381 | bool fWideColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 382 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 383 | SkTDArray<GrSimpleMesh*> fMeshes; |
| 384 | GrProgramInfo* fProgramInfo = nullptr; |
Robert Phillips | a9e28af | 2020-03-13 08:40:12 -0400 | [diff] [blame] | 385 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 386 | using INHERITED = GrMeshDrawOp; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 387 | }; |
| 388 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 389 | } // anonymous namespace |
| 390 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 391 | bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 392 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 393 | "GrAALinearizingConvexPathRenderer::onDrawPath"); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 394 | SkASSERT(args.fRenderTargetContext->numSamples() <= 1); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 395 | SkASSERT(!args.fShape->isEmpty()); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 396 | SkASSERT(!args.fShape->style().pathEffect()); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 397 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 398 | SkPath path; |
| 399 | args.fShape->asPath(&path); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 400 | bool fill = args.fShape->style().isSimpleFill(); |
| 401 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 402 | SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth(); |
| 403 | SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); |
| 404 | SkScalar miterLimit = stroke.getMiter(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 405 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 406 | GrOp::Owner op = AAFlatteningConvexPathOp::Make( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 407 | args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth, |
| 408 | stroke.getStyle(), join, miterLimit, args.fUserStencilSettings); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 409 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | |
| 413 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 414 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 415 | #if GR_TEST_UTILS |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 416 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 417 | GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 418 | SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random); |
John Stiles | 31954bf | 2020-08-07 17:35:54 -0400 | [diff] [blame] | 419 | const SkPath& path = GrTest::TestPathConvex(random); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 420 | |
| 421 | SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style, |
Herb Derby | 60c05f9 | 2016-12-13 15:18:55 -0500 | [diff] [blame] | 422 | SkStrokeRec::kStroke_Style, |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 423 | SkStrokeRec::kStrokeAndFill_Style }; |
| 424 | |
| 425 | SkStrokeRec::Style style = styles[random->nextU() % 3]; |
| 426 | |
| 427 | SkScalar strokeWidth = -1.f; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 428 | SkPaint::Join join = SkPaint::kMiter_Join; |
| 429 | SkScalar miterLimit = 0.5f; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 430 | |
| 431 | if (SkStrokeRec::kFill_Style != style) { |
| 432 | strokeWidth = random->nextRangeF(1.0f, 10.0f); |
| 433 | if (random->nextBool()) { |
| 434 | join = SkPaint::kMiter_Join; |
| 435 | } else { |
| 436 | join = SkPaint::kBevel_Join; |
| 437 | } |
| 438 | miterLimit = random->nextRangeF(0.5f, 2.0f); |
| 439 | } |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 440 | const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 441 | return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth, |
| 442 | style, join, miterLimit, stencilSettings); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | #endif |