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