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" |
| 19 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrStyle.h" |
| 21 | #include "src/gpu/GrVertexWriter.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 22 | #include "src/gpu/geometry/GrPathUtils.h" |
| 23 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 25 | #include "src/gpu/ops/GrAAConvexTessellator.h" |
| 26 | #include "src/gpu/ops/GrAALinearizingConvexPathRenderer.h" |
| 27 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 28 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 29 | |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 30 | static const int DEFAULT_BUFFER_SIZE = 100; |
| 31 | |
| 32 | // The thicker the stroke, the harder it is to produce high-quality results using tessellation. For |
| 33 | // the time being, we simply drop back to software rendering above this stroke width. |
| 34 | static const SkScalar kMaxStrokeWidth = 20.0; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 35 | |
| 36 | GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() { |
| 37 | } |
| 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 | } |
| 80 | return CanDrawPath::kYes; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // extract the result vertices and indices from the GrAAConvexTessellator |
| 84 | static void extract_verts(const GrAAConvexTessellator& tess, |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 85 | void* vertData, |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 86 | const GrVertexColor& color, |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 87 | uint16_t firstIndex, |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 88 | uint16_t* idxs) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 89 | GrVertexWriter verts{vertData}; |
Brian Osman | f3e6b90 | 2018-12-28 17:43:50 +0000 | [diff] [blame] | 90 | for (int i = 0; i < tess.numPts(); ++i) { |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 91 | verts.write(tess.point(i), color, tess.coverage(i)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | for (int i = 0; i < tess.numIndices(); ++i) { |
| 95 | idxs[i] = tess.index(i) + firstIndex; |
| 96 | } |
| 97 | } |
| 98 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame^] | 99 | static GrGeometryProcessor* create_lines_only_gp(SkArenaAlloc* arena, |
| 100 | const GrShaderCaps* shaderCaps, |
| 101 | bool tweakAlphaForCoverage, |
| 102 | const SkMatrix& viewMatrix, |
| 103 | bool usesLocalCoords, |
| 104 | bool wideColor) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 105 | using namespace GrDefaultGeoProcFactory; |
joshualitt | e494a58 | 2015-08-03 09:32:36 -0700 | [diff] [blame] | 106 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 107 | Coverage::Type coverageType = |
| 108 | tweakAlphaForCoverage ? Coverage::kAttributeTweakAlpha_Type : Coverage::kAttribute_Type; |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 109 | LocalCoords::Type localCoordsType = |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 110 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
| 111 | Color::Type colorType = |
| 112 | wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type; |
| 113 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame^] | 114 | return MakeForDeviceSpace(arena, shaderCaps, colorType, coverageType, |
| 115 | localCoordsType, viewMatrix); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 118 | namespace { |
| 119 | |
| 120 | class AAFlatteningConvexPathOp final : public GrMeshDrawOp { |
| 121 | private: |
| 122 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 123 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 124 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 125 | DEFINE_OP_CLASS_ID |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 126 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 127 | GrPaint&& paint, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 128 | const SkMatrix& viewMatrix, |
| 129 | const SkPath& path, |
| 130 | SkScalar strokeWidth, |
| 131 | SkStrokeRec::Style style, |
| 132 | SkPaint::Join join, |
| 133 | SkScalar miterLimit, |
| 134 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 135 | return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint), |
| 136 | viewMatrix, path, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 137 | strokeWidth, style, join, miterLimit, |
| 138 | stencilSettings); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 141 | AAFlatteningConvexPathOp(const Helper::MakeArgs& helperArgs, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 142 | const SkPMColor4f& color, |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 143 | const SkMatrix& viewMatrix, |
| 144 | const SkPath& path, |
| 145 | SkScalar strokeWidth, |
| 146 | SkStrokeRec::Style style, |
| 147 | SkPaint::Join join, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 148 | SkScalar miterLimit, |
| 149 | const GrUserStencilSettings* stencilSettings) |
| 150 | : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 151 | fPaths.emplace_back( |
| 152 | PathData{color, viewMatrix, path, strokeWidth, style, join, miterLimit}); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 153 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 154 | // compute bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 155 | SkRect bounds = path.getBounds(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 156 | SkScalar w = strokeWidth; |
| 157 | if (w > 0) { |
| 158 | w /= 2; |
Greg Daniel | 55e5be3 | 2019-09-30 12:23:26 -0400 | [diff] [blame] | 159 | 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) { |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 163 | w *= miterLimit; |
| 164 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 165 | bounds.outset(w, w); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 166 | } |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 167 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsHairline::kNo); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 170 | const char* name() const override { return "AAFlatteningConvexPathOp"; } |
| 171 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 172 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 173 | fHelper.visitProxies(func); |
| 174 | } |
| 175 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 176 | #ifdef SK_DEBUG |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 177 | SkString dumpInfo() const override { |
| 178 | SkString string; |
| 179 | for (const auto& path : fPaths) { |
| 180 | string.appendf( |
| 181 | "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, " |
| 182 | "MiterLimit: %.2f\n", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 183 | path.fColor.toBytes_RGBA(), path.fStrokeWidth, path.fStyle, path.fJoin, |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 184 | path.fMiterLimit); |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 185 | } |
| 186 | string += fHelper.dumpInfo(); |
| 187 | string += INHERITED::dumpInfo(); |
| 188 | return string; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 189 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 190 | #endif |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 191 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 192 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 193 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 194 | GrProcessorSet::Analysis finalize( |
| 195 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 196 | GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 197 | return fHelper.finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 198 | caps, clip, hasMixedSampledCoverage, clampType, |
| 199 | GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 202 | private: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame^] | 203 | void recordDraw(Target* target, const GrGeometryProcessor* gp, int vertexCount, |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 204 | size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 205 | if (vertexCount == 0 || indexCount == 0) { |
| 206 | return; |
| 207 | } |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 208 | sk_sp<const GrBuffer> vertexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 209 | int firstVertex; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 210 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 211 | &firstVertex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 212 | if (!verts) { |
| 213 | SkDebugf("Could not allocate vertices\n"); |
| 214 | return; |
| 215 | } |
| 216 | memcpy(verts, vertices, vertexCount * vertexStride); |
| 217 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 218 | sk_sp<const GrBuffer> indexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 219 | int firstIndex; |
| 220 | uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 221 | if (!idxs) { |
| 222 | SkDebugf("Could not allocate indices\n"); |
| 223 | return; |
| 224 | } |
| 225 | memcpy(idxs, indices, indexCount * sizeof(uint16_t)); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 226 | GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 227 | mesh->setIndexed(std::move(indexBuffer), indexCount, firstIndex, 0, vertexCount - 1, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 228 | GrPrimitiveRestart::kNo); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 229 | mesh->setVertexData(std::move(vertexBuffer), firstVertex); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame^] | 230 | target->recordDraw(gp, mesh, 1, GrPrimitiveType::kTriangles); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 231 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 232 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 233 | void onPrepareDraws(Target* target) override { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 234 | // Setup GrGeometryProcessor |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame^] | 235 | GrGeometryProcessor* gp = create_lines_only_gp(target->allocator(), |
| 236 | target->caps().shaderCaps(), |
| 237 | fHelper.compatibleWithCoverageAsAlpha(), |
| 238 | this->viewMatrix(), |
| 239 | fHelper.usesLocalCoords(), |
| 240 | fWideColor); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 241 | if (!gp) { |
| 242 | SkDebugf("Couldn't create a GrGeometryProcessor\n"); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 246 | size_t vertexStride = gp->vertexStride(); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 247 | int instanceCount = fPaths.count(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 248 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 249 | int64_t vertexCount = 0; |
| 250 | int64_t indexCount = 0; |
| 251 | int64_t maxVertices = DEFAULT_BUFFER_SIZE; |
| 252 | int64_t maxIndices = DEFAULT_BUFFER_SIZE; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 253 | uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride); |
| 254 | uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 255 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 256 | const PathData& args = fPaths[i]; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 257 | GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth, |
| 258 | args.fJoin, args.fMiterLimit); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 259 | |
| 260 | if (!tess.tessellate(args.fViewMatrix, args.fPath)) { |
| 261 | continue; |
| 262 | } |
| 263 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 264 | int currentVertices = tess.numPts(); |
| 265 | if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) { |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 266 | // 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] | 267 | // uint16_t. Draw what we've got so far and reset. |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 268 | this->recordDraw( |
| 269 | target, gp, vertexCount, vertexStride, vertices, indexCount, indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 270 | vertexCount = 0; |
| 271 | indexCount = 0; |
| 272 | } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 273 | if (vertexCount + currentVertices > maxVertices) { |
| 274 | maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 275 | if (maxVertices * vertexStride > SK_MaxS32) { |
| 276 | sk_free(vertices); |
| 277 | sk_free(indices); |
| 278 | return; |
| 279 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 280 | vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 281 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 282 | int currentIndices = tess.numIndices(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 283 | if (indexCount + currentIndices > maxIndices) { |
| 284 | maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 285 | if (maxIndices * sizeof(uint16_t) > SK_MaxS32) { |
| 286 | sk_free(vertices); |
| 287 | sk_free(indices); |
| 288 | return; |
| 289 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 290 | indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 293 | extract_verts(tess, vertices + vertexStride * vertexCount, |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 294 | GrVertexColor(args.fColor, fWideColor), vertexCount, |
| 295 | indices + indexCount); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 296 | vertexCount += currentVertices; |
| 297 | indexCount += currentIndices; |
| 298 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 299 | if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame^] | 300 | this->recordDraw(target, gp, vertexCount, vertexStride, vertices, indexCount, indices); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 301 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 302 | sk_free(vertices); |
| 303 | sk_free(indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 306 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 307 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
| 308 | } |
| 309 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 310 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 311 | AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>(); |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 312 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 313 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 316 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 317 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 318 | return CombineResult::kMerged; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 321 | const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 322 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 323 | struct PathData { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 324 | SkPMColor4f fColor; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 325 | SkMatrix fViewMatrix; |
| 326 | SkPath fPath; |
| 327 | SkScalar fStrokeWidth; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 328 | SkStrokeRec::Style fStyle; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 329 | SkPaint::Join fJoin; |
| 330 | SkScalar fMiterLimit; |
| 331 | }; |
| 332 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 333 | SkSTArray<1, PathData, true> fPaths; |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 334 | Helper fHelper; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 335 | bool fWideColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 336 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 337 | typedef GrMeshDrawOp INHERITED; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 338 | }; |
| 339 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 340 | } // anonymous namespace |
| 341 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 342 | bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 343 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 344 | "GrAALinearizingConvexPathRenderer::onDrawPath"); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 345 | SkASSERT(args.fRenderTargetContext->numSamples() <= 1); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 346 | SkASSERT(!args.fShape->isEmpty()); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 347 | SkASSERT(!args.fShape->style().pathEffect()); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 348 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 349 | SkPath path; |
| 350 | args.fShape->asPath(&path); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 351 | bool fill = args.fShape->style().isSimpleFill(); |
| 352 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 353 | SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth(); |
| 354 | SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); |
| 355 | SkScalar miterLimit = stroke.getMiter(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 356 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 357 | std::unique_ptr<GrDrawOp> op = AAFlatteningConvexPathOp::Make( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 358 | args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth, |
| 359 | stroke.getStyle(), join, miterLimit, args.fUserStencilSettings); |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 360 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 361 | return true; |
| 362 | } |
| 363 | |
| 364 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 365 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 366 | #if GR_TEST_UTILS |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 367 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 368 | GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 369 | SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 370 | SkPath path = GrTest::TestPathConvex(random); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 371 | |
| 372 | SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style, |
Herb Derby | 60c05f9 | 2016-12-13 15:18:55 -0500 | [diff] [blame] | 373 | SkStrokeRec::kStroke_Style, |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 374 | SkStrokeRec::kStrokeAndFill_Style }; |
| 375 | |
| 376 | SkStrokeRec::Style style = styles[random->nextU() % 3]; |
| 377 | |
| 378 | SkScalar strokeWidth = -1.f; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 379 | SkPaint::Join join = SkPaint::kMiter_Join; |
| 380 | SkScalar miterLimit = 0.5f; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 381 | |
| 382 | if (SkStrokeRec::kFill_Style != style) { |
| 383 | strokeWidth = random->nextRangeF(1.0f, 10.0f); |
| 384 | if (random->nextBool()) { |
| 385 | join = SkPaint::kMiter_Join; |
| 386 | } else { |
| 387 | join = SkPaint::kBevel_Join; |
| 388 | } |
| 389 | miterLimit = random->nextRangeF(0.5f, 2.0f); |
| 390 | } |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 391 | const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 392 | return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth, |
| 393 | style, join, miterLimit, stencilSettings); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | #endif |