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 | |
| 8 | #include "GrAALinearizingConvexPathRenderer.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 9 | #include "GrAAConvexTessellator.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 12 | #include "GrDrawOpTest.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 13 | #include "GrGeometryProcessor.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 14 | #include "GrOpFlushState.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 15 | #include "GrPathUtils.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 16 | #include "GrProcessor.h" |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 17 | #include "GrRenderTargetContext.h" |
| 18 | #include "GrShape.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 19 | #include "GrStyle.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 20 | #include "SkGeometry.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 21 | #include "SkPathPriv.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 22 | #include "SkString.h" |
| 23 | #include "SkTraceEvent.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLGeometryProcessor.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 25 | #include "ops/GrMeshDrawOp.h" |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 26 | #include "ops/GrSimpleMeshDrawOpHelper.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 27 | |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 28 | static const int DEFAULT_BUFFER_SIZE = 100; |
| 29 | |
| 30 | // The thicker the stroke, the harder it is to produce high-quality results using tessellation. For |
| 31 | // the time being, we simply drop back to software rendering above this stroke width. |
| 32 | static const SkScalar kMaxStrokeWidth = 20.0; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 33 | |
| 34 | GrAALinearizingConvexPathRenderer::GrAALinearizingConvexPathRenderer() { |
| 35 | } |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 39 | GrPathRenderer::CanDrawPath |
| 40 | GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 41 | if (GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 42 | return CanDrawPath::kNo; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 43 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 44 | if (!args.fShape->knownToBeConvex()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 45 | return CanDrawPath::kNo; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 46 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 47 | if (args.fShape->style().pathEffect()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 48 | return CanDrawPath::kNo; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 49 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 50 | if (args.fShape->inverseFilled()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 51 | return CanDrawPath::kNo; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 52 | } |
Brian Osman | a98e399 | 2017-06-26 15:14:53 -0400 | [diff] [blame] | 53 | if (args.fShape->bounds().width() <= 0 && args.fShape->bounds().height() <= 0) { |
| 54 | // 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] | 55 | return CanDrawPath::kNo; |
Brian Osman | a98e399 | 2017-06-26 15:14:53 -0400 | [diff] [blame] | 56 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 57 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 58 | |
| 59 | if (stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 60 | stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style) { |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 61 | if (!args.fViewMatrix->isSimilarity()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 62 | return CanDrawPath::kNo; |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 63 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 64 | SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * stroke.getWidth(); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 65 | if (strokeWidth < 1.0f && stroke.getStyle() == SkStrokeRec::kStroke_Style) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 66 | return CanDrawPath::kNo; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 67 | } |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 68 | if (strokeWidth > kMaxStrokeWidth || |
| 69 | !args.fShape->knownToBeClosed() || |
| 70 | stroke.getJoin() == SkPaint::Join::kRound_Join) { |
| 71 | return CanDrawPath::kNo; |
| 72 | } |
| 73 | return CanDrawPath::kYes; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 74 | } |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 75 | if (stroke.getStyle() != SkStrokeRec::kFill_Style) { |
| 76 | return CanDrawPath::kNo; |
| 77 | } |
| 78 | return CanDrawPath::kYes; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // extract the result vertices and indices from the GrAAConvexTessellator |
| 82 | static void extract_verts(const GrAAConvexTessellator& tess, |
| 83 | void* vertices, |
| 84 | size_t vertexStride, |
| 85 | GrColor color, |
| 86 | uint16_t firstIndex, |
| 87 | uint16_t* idxs, |
| 88 | bool tweakAlphaForCoverage) { |
| 89 | intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
| 90 | |
| 91 | for (int i = 0; i < tess.numPts(); ++i) { |
| 92 | *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i); |
| 93 | } |
| 94 | |
| 95 | // Make 'verts' point to the colors |
| 96 | verts += sizeof(SkPoint); |
| 97 | for (int i = 0; i < tess.numPts(); ++i) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 98 | if (tweakAlphaForCoverage) { |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 99 | SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255); |
| 100 | unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 101 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 102 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 103 | } else { |
| 104 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 105 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 106 | tess.coverage(i); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | for (int i = 0; i < tess.numIndices(); ++i) { |
| 111 | idxs[i] = tess.index(i) + firstIndex; |
| 112 | } |
| 113 | } |
| 114 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 115 | static sk_sp<GrGeometryProcessor> create_lines_only_gp(const GrShaderCaps* shaderCaps, |
| 116 | bool tweakAlphaForCoverage, |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 117 | const SkMatrix& viewMatrix, |
| 118 | bool usesLocalCoords) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 119 | using namespace GrDefaultGeoProcFactory; |
joshualitt | e494a58 | 2015-08-03 09:32:36 -0700 | [diff] [blame] | 120 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 121 | Coverage::Type coverageType; |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 122 | if (tweakAlphaForCoverage) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 123 | coverageType = Coverage::kSolid_Type; |
| 124 | } else { |
| 125 | coverageType = Coverage::kAttribute_Type; |
| 126 | } |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 127 | LocalCoords::Type localCoordsType = |
| 128 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 129 | return MakeForDeviceSpace(shaderCaps, |
| 130 | Color::kPremulGrColorAttribute_Type, |
| 131 | coverageType, |
| 132 | localCoordsType, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 133 | viewMatrix); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 136 | namespace { |
| 137 | |
| 138 | class AAFlatteningConvexPathOp final : public GrMeshDrawOp { |
| 139 | private: |
| 140 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 141 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 142 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 143 | DEFINE_OP_CLASS_ID |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 144 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 145 | GrPaint&& paint, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 146 | const SkMatrix& viewMatrix, |
| 147 | const SkPath& path, |
| 148 | SkScalar strokeWidth, |
| 149 | SkStrokeRec::Style style, |
| 150 | SkPaint::Join join, |
| 151 | SkScalar miterLimit, |
| 152 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 153 | return Helper::FactoryHelper<AAFlatteningConvexPathOp>(context, std::move(paint), |
| 154 | viewMatrix, path, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 155 | strokeWidth, style, join, miterLimit, |
| 156 | stencilSettings); |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 157 | } |
| 158 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 159 | AAFlatteningConvexPathOp(const Helper::MakeArgs& helperArgs, |
| 160 | GrColor color, |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 161 | const SkMatrix& viewMatrix, |
| 162 | const SkPath& path, |
| 163 | SkScalar strokeWidth, |
| 164 | SkStrokeRec::Style style, |
| 165 | SkPaint::Join join, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 166 | SkScalar miterLimit, |
| 167 | const GrUserStencilSettings* stencilSettings) |
| 168 | : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 169 | fPaths.emplace_back( |
| 170 | PathData{color, viewMatrix, path, strokeWidth, style, join, miterLimit}); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 171 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 172 | // compute bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 173 | SkRect bounds = path.getBounds(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 174 | SkScalar w = strokeWidth; |
| 175 | if (w > 0) { |
| 176 | w /= 2; |
| 177 | // If the half stroke width is < 1 then we effectively fallback to bevel joins. |
| 178 | if (SkPaint::kMiter_Join == join && w > 1.f) { |
| 179 | w *= miterLimit; |
| 180 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 181 | bounds.outset(w, w); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 182 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 183 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsZeroArea::kNo); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 186 | const char* name() const override { return "AAFlatteningConvexPathOp"; } |
| 187 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 188 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 189 | fHelper.visitProxies(func); |
| 190 | } |
| 191 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 192 | SkString dumpInfo() const override { |
| 193 | SkString string; |
| 194 | for (const auto& path : fPaths) { |
| 195 | string.appendf( |
| 196 | "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, " |
| 197 | "MiterLimit: %.2f\n", |
| 198 | path.fColor, path.fStrokeWidth, path.fStyle, path.fJoin, path.fMiterLimit); |
| 199 | } |
| 200 | string += fHelper.dumpInfo(); |
| 201 | string += INHERITED::dumpInfo(); |
| 202 | return string; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 205 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 206 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 207 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 208 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 209 | &fPaths.back().fColor); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 212 | private: |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 213 | void draw(Target* target, const GrGeometryProcessor* gp, const GrPipeline* pipeline, |
| 214 | const GrPipeline::FixedDynamicState* fixedDynamicState, int vertexCount, |
| 215 | size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 216 | if (vertexCount == 0 || indexCount == 0) { |
| 217 | return; |
| 218 | } |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 219 | const GrBuffer* vertexBuffer; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 220 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 221 | int firstVertex; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 222 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 223 | &firstVertex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 224 | if (!verts) { |
| 225 | SkDebugf("Could not allocate vertices\n"); |
| 226 | return; |
| 227 | } |
| 228 | memcpy(verts, vertices, vertexCount * vertexStride); |
| 229 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 230 | const GrBuffer* indexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 231 | int firstIndex; |
| 232 | uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 233 | if (!idxs) { |
| 234 | SkDebugf("Could not allocate indices\n"); |
| 235 | return; |
| 236 | } |
| 237 | memcpy(idxs, indices, indexCount * sizeof(uint16_t)); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 238 | mesh.setIndexed(indexBuffer, indexCount, firstIndex, 0, vertexCount - 1, |
| 239 | GrPrimitiveRestart::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 240 | mesh.setVertexData(vertexBuffer, firstVertex); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 241 | target->draw(gp, pipeline, fixedDynamicState, mesh); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 242 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 243 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 244 | void onPrepareDraws(Target* target) override { |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 245 | auto pipe = fHelper.makePipeline(target); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 246 | // Setup GrGeometryProcessor |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 247 | sk_sp<GrGeometryProcessor> gp(create_lines_only_gp(target->caps().shaderCaps(), |
| 248 | fHelper.compatibleWithAlphaAsCoverage(), |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 249 | this->viewMatrix(), |
| 250 | fHelper.usesLocalCoords())); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 251 | if (!gp) { |
| 252 | SkDebugf("Couldn't create a GrGeometryProcessor\n"); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 256 | size_t vertexStride = fHelper.compatibleWithAlphaAsCoverage() |
| 257 | ? sizeof(GrDefaultGeoProcFactory::PositionColorAttr) |
| 258 | : sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr); |
| 259 | SkASSERT(vertexStride == gp->debugOnly_vertexStride()); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 260 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 261 | int instanceCount = fPaths.count(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 262 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 263 | int64_t vertexCount = 0; |
| 264 | int64_t indexCount = 0; |
| 265 | int64_t maxVertices = DEFAULT_BUFFER_SIZE; |
| 266 | int64_t maxIndices = DEFAULT_BUFFER_SIZE; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 267 | uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride); |
| 268 | uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 269 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 270 | const PathData& args = fPaths[i]; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 271 | GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth, |
| 272 | args.fJoin, args.fMiterLimit); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 273 | |
| 274 | if (!tess.tessellate(args.fViewMatrix, args.fPath)) { |
| 275 | continue; |
| 276 | } |
| 277 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 278 | int currentVertices = tess.numPts(); |
| 279 | if (vertexCount + currentVertices > static_cast<int>(UINT16_MAX)) { |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 280 | // 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] | 281 | // uint16_t. Draw what we've got so far and reset. |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 282 | this->draw(target, gp.get(), pipe.fPipeline, pipe.fFixedDynamicState, vertexCount, |
| 283 | vertexStride, vertices, indexCount, indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 284 | vertexCount = 0; |
| 285 | indexCount = 0; |
| 286 | } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 287 | if (vertexCount + currentVertices > maxVertices) { |
| 288 | maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 289 | if (maxVertices * vertexStride > SK_MaxS32) { |
| 290 | sk_free(vertices); |
| 291 | sk_free(indices); |
| 292 | return; |
| 293 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 294 | vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 295 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 296 | int currentIndices = tess.numIndices(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 297 | if (indexCount + currentIndices > maxIndices) { |
| 298 | maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 299 | if (maxIndices * sizeof(uint16_t) > SK_MaxS32) { |
| 300 | sk_free(vertices); |
| 301 | sk_free(indices); |
| 302 | return; |
| 303 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 304 | indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 305 | } |
| 306 | |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 307 | extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor, |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 308 | vertexCount, indices + indexCount, |
| 309 | fHelper.compatibleWithAlphaAsCoverage()); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 310 | vertexCount += currentVertices; |
| 311 | indexCount += currentIndices; |
| 312 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 313 | if (vertexCount <= SK_MaxS32 && indexCount <= SK_MaxS32) { |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 314 | this->draw(target, gp.get(), pipe.fPipeline, pipe.fFixedDynamicState, vertexCount, |
| 315 | vertexStride, vertices, indexCount, indices); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 316 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 317 | sk_free(vertices); |
| 318 | sk_free(indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 321 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 322 | AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>(); |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 323 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 327 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 328 | this->joinBounds(*that); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 332 | const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 333 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 334 | struct PathData { |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 335 | GrColor fColor; |
| 336 | SkMatrix fViewMatrix; |
| 337 | SkPath fPath; |
| 338 | SkScalar fStrokeWidth; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 339 | SkStrokeRec::Style fStyle; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 340 | SkPaint::Join fJoin; |
| 341 | SkScalar fMiterLimit; |
| 342 | }; |
| 343 | |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 344 | SkSTArray<1, PathData, true> fPaths; |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 345 | Helper fHelper; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 346 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 347 | typedef GrMeshDrawOp INHERITED; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 350 | } // anonymous namespace |
| 351 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 352 | bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 353 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 354 | "GrAALinearizingConvexPathRenderer::onDrawPath"); |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 355 | SkASSERT(GrFSAAType::kUnifiedMSAA != args.fRenderTargetContext->fsaaType()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 356 | SkASSERT(!args.fShape->isEmpty()); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 357 | SkASSERT(!args.fShape->style().pathEffect()); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 358 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 359 | SkPath path; |
| 360 | args.fShape->asPath(&path); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 361 | bool fill = args.fShape->style().isSimpleFill(); |
| 362 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 363 | SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth(); |
| 364 | SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); |
| 365 | SkScalar miterLimit = stroke.getMiter(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 366 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 367 | std::unique_ptr<GrDrawOp> op = AAFlatteningConvexPathOp::Make( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 368 | args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, strokeWidth, |
| 369 | stroke.getStyle(), join, miterLimit, args.fUserStencilSettings); |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 370 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
| 374 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 375 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 376 | #if GR_TEST_UTILS |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 377 | |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 378 | GR_DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) { |
Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 379 | SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 380 | SkPath path = GrTest::TestPathConvex(random); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 381 | |
| 382 | SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style, |
Herb Derby | 60c05f9 | 2016-12-13 15:18:55 -0500 | [diff] [blame] | 383 | SkStrokeRec::kStroke_Style, |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 384 | SkStrokeRec::kStrokeAndFill_Style }; |
| 385 | |
| 386 | SkStrokeRec::Style style = styles[random->nextU() % 3]; |
| 387 | |
| 388 | SkScalar strokeWidth = -1.f; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 389 | SkPaint::Join join = SkPaint::kMiter_Join; |
| 390 | SkScalar miterLimit = 0.5f; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 391 | |
| 392 | if (SkStrokeRec::kFill_Style != style) { |
| 393 | strokeWidth = random->nextRangeF(1.0f, 10.0f); |
| 394 | if (random->nextBool()) { |
| 395 | join = SkPaint::kMiter_Join; |
| 396 | } else { |
| 397 | join = SkPaint::kBevel_Join; |
| 398 | } |
| 399 | miterLimit = random->nextRangeF(0.5f, 2.0f); |
| 400 | } |
Brian Salomon | b295573 | 2017-07-13 16:42:55 -0400 | [diff] [blame] | 401 | const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 402 | return AAFlatteningConvexPathOp::Make(context, std::move(paint), viewMatrix, path, strokeWidth, |
| 403 | style, join, miterLimit, stencilSettings); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #endif |