| 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" |
| 9 | |
| 10 | #include "GrAAConvexTessellator.h" |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 11 | #include "GrContext.h" |
| 12 | #include "GrDefaultGeoProcFactory.h" |
| Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame^] | 13 | #include "GrDrawOpTest.h" |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 14 | #include "GrGeometryProcessor.h" |
| 15 | #include "GrInvariantOutput.h" |
| Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 16 | #include "GrOpFlushState.h" |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 17 | #include "GrPathUtils.h" |
| bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 18 | #include "GrPipelineBuilder.h" |
| Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 19 | #include "GrProcessor.h" |
| bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 20 | #include "GrStyle.h" |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 21 | #include "SkGeometry.h" |
| Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 22 | #include "SkPathPriv.h" |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 23 | #include "SkString.h" |
| 24 | #include "SkTraceEvent.h" |
| egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 25 | #include "glsl/GrGLSLGeometryProcessor.h" |
| Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 26 | #include "ops/GrMeshDrawOp.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 | |
| bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 39 | bool GrAALinearizingConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 40 | if (GrAAType::kCoverage != args.fAAType) { |
| fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 43 | if (!args.fShape->knownToBeConvex()) { |
| fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 46 | if (args.fShape->style().pathEffect()) { |
| fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 49 | if (args.fShape->inverseFilled()) { |
| bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 50 | return false; |
| 51 | } |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 52 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 53 | |
| 54 | if (stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 55 | stroke.getStyle() == SkStrokeRec::kStrokeAndFill_Style) { |
| ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 56 | if (!args.fViewMatrix->isSimilarity()) { |
| 57 | return false; |
| 58 | } |
| bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 59 | SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * stroke.getWidth(); |
| robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 60 | if (strokeWidth < 1.0f && stroke.getStyle() == SkStrokeRec::kStroke_Style) { |
| 61 | return false; |
| 62 | } |
| 63 | return strokeWidth <= kMaxStrokeWidth && |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 64 | args.fShape->knownToBeClosed() && |
| bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 65 | stroke.getJoin() != SkPaint::Join::kRound_Join; |
| fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 66 | } |
| bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 67 | return stroke.getStyle() == SkStrokeRec::kFill_Style; |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // extract the result vertices and indices from the GrAAConvexTessellator |
| 71 | static void extract_verts(const GrAAConvexTessellator& tess, |
| 72 | void* vertices, |
| 73 | size_t vertexStride, |
| 74 | GrColor color, |
| 75 | uint16_t firstIndex, |
| 76 | uint16_t* idxs, |
| 77 | bool tweakAlphaForCoverage) { |
| 78 | intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
| 79 | |
| 80 | for (int i = 0; i < tess.numPts(); ++i) { |
| 81 | *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i); |
| 82 | } |
| 83 | |
| 84 | // Make 'verts' point to the colors |
| 85 | verts += sizeof(SkPoint); |
| 86 | for (int i = 0; i < tess.numPts(); ++i) { |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 87 | if (tweakAlphaForCoverage) { |
| fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 88 | SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255); |
| 89 | unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i)); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 90 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 91 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 92 | } else { |
| 93 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 94 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = |
| fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 95 | tess.coverage(i); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | for (int i = 0; i < tess.numIndices(); ++i) { |
| 100 | idxs[i] = tess.index(i) + firstIndex; |
| 101 | } |
| 102 | } |
| 103 | |
| bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 104 | static sk_sp<GrGeometryProcessor> create_fill_gp(bool tweakAlphaForCoverage, |
| joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 105 | const SkMatrix& viewMatrix, |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 106 | bool usesLocalCoords, |
| 107 | bool coverageIgnored) { |
| joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 108 | using namespace GrDefaultGeoProcFactory; |
| joshualitt | e494a58 | 2015-08-03 09:32:36 -0700 | [diff] [blame] | 109 | |
| joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 110 | Color color(Color::kAttribute_Type); |
| 111 | Coverage::Type coverageType; |
| 112 | // TODO remove coverage if coverage is ignored |
| 113 | /*if (coverageIgnored) { |
| 114 | coverageType = Coverage::kNone_Type; |
| 115 | } else*/ if (tweakAlphaForCoverage) { |
| 116 | coverageType = Coverage::kSolid_Type; |
| 117 | } else { |
| 118 | coverageType = Coverage::kAttribute_Type; |
| 119 | } |
| 120 | Coverage coverage(coverageType); |
| 121 | LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : |
| 122 | LocalCoords::kUnused_Type); |
| bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 123 | return MakeForDeviceSpace(color, coverage, localCoords, viewMatrix); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 126 | class AAFlatteningConvexPathOp final : public GrMeshDrawOp { |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 127 | public: |
| Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 128 | DEFINE_OP_CLASS_ID |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 129 | static sk_sp<GrDrawOp> Make(GrColor color, |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 130 | const SkMatrix& viewMatrix, |
| 131 | const SkPath& path, |
| 132 | SkScalar strokeWidth, |
| robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 133 | SkStrokeRec::Style style, |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 134 | SkPaint::Join join, |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 135 | SkScalar miterLimit) { |
| 136 | return sk_sp<GrDrawOp>(new AAFlatteningConvexPathOp(color, viewMatrix, path, strokeWidth, |
| 137 | style, join, miterLimit)); |
| 138 | } |
| 139 | |
| 140 | const char* name() const override { return "AAFlatteningConvexPathOp"; } |
| 141 | |
| 142 | SkString dumpInfo() const override { |
| 143 | SkString string; |
| 144 | for (const auto& path : fPaths) { |
| 145 | string.appendf( |
| 146 | "Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, " |
| 147 | "MiterLimit: %.2f\n", |
| 148 | path.fColor, path.fStrokeWidth, path.fStyle, path.fJoin, path.fMiterLimit); |
| 149 | } |
| 150 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 151 | string.append(INHERITED::dumpInfo()); |
| 152 | return string; |
| 153 | } |
| 154 | |
| 155 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 156 | GrInitInvariantOutput* coverage, |
| 157 | GrBatchToXPOverrides* overrides) const override { |
| 158 | // When this is called there is only one path. |
| 159 | color->setKnownFourComponents(fPaths[0].fColor); |
| 160 | coverage->setUnknownSingleComponent(); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | AAFlatteningConvexPathOp(GrColor color, |
| 165 | const SkMatrix& viewMatrix, |
| 166 | const SkPath& path, |
| 167 | SkScalar strokeWidth, |
| 168 | SkStrokeRec::Style style, |
| 169 | SkPaint::Join join, |
| 170 | SkScalar miterLimit) |
| 171 | : INHERITED(ClassID()) { |
| 172 | fPaths.emplace_back( |
| 173 | PathData{color, viewMatrix, path, strokeWidth, style, join, miterLimit}); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 174 | |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 175 | // compute bounds |
| bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 176 | SkRect bounds = path.getBounds(); |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 177 | SkScalar w = strokeWidth; |
| 178 | if (w > 0) { |
| 179 | w /= 2; |
| 180 | // If the half stroke width is < 1 then we effectively fallback to bevel joins. |
| 181 | if (SkPaint::kMiter_Join == join && w > 1.f) { |
| 182 | w *= miterLimit; |
| 183 | } |
| bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 184 | bounds.outset(w, w); |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 185 | } |
| bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 186 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsZeroArea::kNo); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 189 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 190 | // Handle any color overrides |
| ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 191 | if (!overrides.readsColor()) { |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 192 | fPaths[0].fColor = GrColor_ILLEGAL; |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 193 | } |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 194 | overrides.getOverrideColorIfSet(&fPaths[0].fColor); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 195 | |
| 196 | // setup batch properties |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 197 | fColor = fPaths[0].fColor; |
| 198 | fUsesLocalCoords = overrides.readsLocalCoords(); |
| 199 | fCoverageIgnored = !overrides.readsCoverage(); |
| 200 | fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage(); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 203 | void draw(GrMeshDrawOp::Target* target, const GrGeometryProcessor* gp, int vertexCount, |
| joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [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 | } |
| cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 208 | const GrBuffer* vertexBuffer; |
| egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 209 | GrMesh mesh; |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 210 | int firstVertex; |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 211 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
| bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 212 | &firstVertex); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 213 | if (!verts) { |
| 214 | SkDebugf("Could not allocate vertices\n"); |
| 215 | return; |
| 216 | } |
| 217 | memcpy(verts, vertices, vertexCount * vertexStride); |
| 218 | |
| cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 219 | const GrBuffer* indexBuffer; |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 220 | int firstIndex; |
| bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 221 | uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 222 | if (!idxs) { |
| 223 | SkDebugf("Could not allocate indices\n"); |
| 224 | return; |
| 225 | } |
| 226 | memcpy(idxs, indices, indexCount * sizeof(uint16_t)); |
| egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 227 | mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex, |
| 228 | firstIndex, vertexCount, indexCount); |
| bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 229 | target->draw(gp, mesh); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 230 | } |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 231 | |
| joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 232 | void onPrepareDraws(Target* target) const override { |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 233 | bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); |
| 234 | |
| joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 235 | // Setup GrGeometryProcessor |
| bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 236 | sk_sp<GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage, |
| 237 | this->viewMatrix(), |
| 238 | this->usesLocalCoords(), |
| 239 | this->coverageIgnored())); |
| joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 240 | if (!gp) { |
| 241 | SkDebugf("Couldn't create a GrGeometryProcessor\n"); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 245 | size_t vertexStride = gp->getVertexStride(); |
| 246 | |
| 247 | SkASSERT(canTweakAlphaForCoverage ? |
| 248 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : |
| 249 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); |
| 250 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 251 | int instanceCount = fPaths.count(); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 252 | |
| 253 | int vertexCount = 0; |
| 254 | int indexCount = 0; |
| 255 | int maxVertices = DEFAULT_BUFFER_SIZE; |
| 256 | int maxIndices = DEFAULT_BUFFER_SIZE; |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 257 | uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride); |
| 258 | uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t)); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 259 | for (int i = 0; i < instanceCount; i++) { |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 260 | const PathData& args = fPaths[i]; |
| robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 261 | GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth, |
| 262 | args.fJoin, args.fMiterLimit); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 263 | |
| 264 | if (!tess.tessellate(args.fViewMatrix, args.fPath)) { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | int currentIndices = tess.numIndices(); |
| 269 | SkASSERT(currentIndices <= UINT16_MAX); |
| 270 | if (indexCount + currentIndices > UINT16_MAX) { |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 271 | // 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] | 272 | // uint16_t. Draw what we've got so far and reset. |
| bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 273 | this->draw(target, gp.get(), |
| 274 | vertexCount, vertexStride, vertices, indexCount, indices); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 275 | vertexCount = 0; |
| 276 | indexCount = 0; |
| 277 | } |
| 278 | int currentVertices = tess.numPts(); |
| 279 | if (vertexCount + currentVertices > maxVertices) { |
| 280 | maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2); |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 281 | vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 282 | } |
| 283 | if (indexCount + currentIndices > maxIndices) { |
| 284 | maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2); |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 285 | indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t)); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 288 | extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor, |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 289 | vertexCount, indices + indexCount, canTweakAlphaForCoverage); |
| 290 | vertexCount += currentVertices; |
| 291 | indexCount += currentIndices; |
| 292 | } |
| bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 293 | this->draw(target, gp.get(), vertexCount, vertexStride, vertices, indexCount, indices); |
| mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 294 | sk_free(vertices); |
| 295 | sk_free(indices); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 298 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 299 | AAFlatteningConvexPathOp* that = t->cast<AAFlatteningConvexPathOp>(); |
| bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 300 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 301 | that->bounds(), caps)) { |
| joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 305 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 306 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 307 | return false; |
| 308 | } |
| 309 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 310 | // In the event of two ops, one who can tweak, one who cannot, we just fall back to not |
| 311 | // tweaking |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 312 | if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 313 | fCanTweakAlphaForCoverage = false; |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -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()); |
| bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 317 | this->joinBounds(*that); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 321 | GrColor color() const { return fColor; } |
| 322 | bool usesLocalCoords() const { return fUsesLocalCoords; } |
| 323 | bool canTweakAlphaForCoverage() const { return fCanTweakAlphaForCoverage; } |
| 324 | const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; } |
| 325 | bool coverageIgnored() const { return fCoverageIgnored; } |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 326 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 327 | struct PathData { |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 328 | GrColor fColor; |
| 329 | SkMatrix fViewMatrix; |
| 330 | SkPath fPath; |
| 331 | SkScalar fStrokeWidth; |
| robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 332 | SkStrokeRec::Style fStyle; |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 333 | SkPaint::Join fJoin; |
| 334 | SkScalar fMiterLimit; |
| 335 | }; |
| 336 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 337 | GrColor fColor; |
| 338 | bool fUsesLocalCoords; |
| 339 | bool fCoverageIgnored; |
| 340 | bool fCanTweakAlphaForCoverage; |
| 341 | SkSTArray<1, PathData, true> fPaths; |
| reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 342 | |
| Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 343 | typedef GrMeshDrawOp INHERITED; |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 344 | }; |
| 345 | |
| bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 346 | bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 347 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
| joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 348 | "GrAALinearizingConvexPathRenderer::onDrawPath"); |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 349 | SkASSERT(!args.fRenderTargetContext->isUnifiedMultisampled()); |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 350 | SkASSERT(!args.fShape->isEmpty()); |
| robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 351 | SkASSERT(!args.fShape->style().pathEffect()); |
| csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 352 | |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 353 | SkPath path; |
| 354 | args.fShape->asPath(&path); |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 355 | bool fill = args.fShape->style().isSimpleFill(); |
| 356 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 357 | SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth(); |
| 358 | SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); |
| 359 | SkScalar miterLimit = stroke.getMiter(); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 360 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 361 | sk_sp<GrDrawOp> op = |
| 362 | AAFlatteningConvexPathOp::Make(args.fPaint->getColor(), *args.fViewMatrix, path, |
| 363 | strokeWidth, stroke.getStyle(), join, miterLimit); |
| robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 364 | |
| Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 365 | GrPipelineBuilder pipelineBuilder(*args.fPaint, args.fAAType); |
| bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 366 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 367 | |
| Brian Salomon | 24f1978 | 2016-12-13 15:10:11 -0500 | [diff] [blame] | 368 | args.fRenderTargetContext->addDrawOp(pipelineBuilder, *args.fClip, std::move(op)); |
| bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 369 | |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | |
| 373 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 374 | |
| 375 | #ifdef GR_TEST_UTILS |
| 376 | |
| Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame^] | 377 | DRAW_OP_TEST_DEFINE(AAFlatteningConvexPathOp) { |
| bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 378 | GrColor color = GrRandomColor(random); |
| 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 | } |
| 401 | |
| Brian Salomon | 780dad1 | 2016-12-15 18:08:40 -0500 | [diff] [blame] | 402 | return AAFlatteningConvexPathOp::Make(color, viewMatrix, path, strokeWidth, style, join, |
| Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame^] | 403 | miterLimit); |
| ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #endif |