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" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 11 | #include "GrBatchFlushState.h" |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 12 | #include "GrBatchTest.h" |
| 13 | #include "GrContext.h" |
| 14 | #include "GrDefaultGeoProcFactory.h" |
| 15 | #include "GrGeometryProcessor.h" |
| 16 | #include "GrInvariantOutput.h" |
| 17 | #include "GrPathUtils.h" |
| 18 | #include "GrProcessor.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 19 | #include "GrPipelineBuilder.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" |
| 22 | #include "SkString.h" |
| 23 | #include "SkTraceEvent.h" |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 24 | #include "SkPathPriv.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 25 | #include "batches/GrVertexBatch.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 26 | #include "glsl/GrGLSLGeometryProcessor.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 { |
| 40 | if (!args.fAntiAlias) { |
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 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 126 | class AAFlatteningConvexPathBatch : public GrVertexBatch { |
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 |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 129 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 130 | AAFlatteningConvexPathBatch(GrColor color, |
| 131 | const SkMatrix& viewMatrix, |
| 132 | const SkPath& path, |
| 133 | SkScalar strokeWidth, |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 134 | SkStrokeRec::Style style, |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 135 | SkPaint::Join join, |
| 136 | SkScalar miterLimit) : INHERITED(ClassID()) { |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 137 | fGeoData.emplace_back(Geometry{ color, viewMatrix, path, |
| 138 | strokeWidth, style, join, miterLimit }); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 139 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 140 | // compute bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 141 | SkRect bounds = path.getBounds(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 142 | SkScalar w = strokeWidth; |
| 143 | if (w > 0) { |
| 144 | w /= 2; |
| 145 | // If the half stroke width is < 1 then we effectively fallback to bevel joins. |
| 146 | if (SkPaint::kMiter_Join == join && w > 1.f) { |
| 147 | w *= miterLimit; |
| 148 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 149 | bounds.outset(w, w); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 150 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 151 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kYes, IsZeroArea::kNo); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 154 | const char* name() const override { return "AAFlatteningConvexBatch"; } |
| 155 | |
| 156 | SkString dumpInfo() const override { |
| 157 | SkString string; |
| 158 | for (const auto& geo : fGeoData) { |
| 159 | string.appendf("Color: 0x%08x, StrokeWidth: %.2f, Style: %d, Join: %d, " |
| 160 | "MiterLimit: %.2f\n", |
| 161 | geo.fColor, geo.fStrokeWidth, geo.fStyle, geo.fJoin, geo.fMiterLimit); |
| 162 | } |
| 163 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 164 | string.append(INHERITED::dumpInfo()); |
| 165 | return string; |
| 166 | } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 167 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 168 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 169 | GrInitInvariantOutput* coverage, |
| 170 | GrBatchToXPOverrides* overrides) const override { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 171 | // When this is called on a batch, there is only one geometry bundle |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 172 | color->setKnownFourComponents(fGeoData[0].fColor); |
| 173 | coverage->setUnknownSingleComponent(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 174 | } |
| 175 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 176 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 177 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 178 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 179 | if (!overrides.readsColor()) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 180 | fGeoData[0].fColor = GrColor_ILLEGAL; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 181 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 182 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 183 | |
| 184 | // setup batch properties |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 185 | fBatch.fColorIgnored = !overrides.readsColor(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 186 | fBatch.fColor = fGeoData[0].fColor; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 187 | fBatch.fUsesLocalCoords = overrides.readsLocalCoords(); |
| 188 | fBatch.fCoverageIgnored = !overrides.readsCoverage(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 189 | fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks(); |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 190 | fBatch.fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 193 | void draw(GrVertexBatch::Target* target, const GrGeometryProcessor* gp, int vertexCount, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 194 | size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 195 | if (vertexCount == 0 || indexCount == 0) { |
| 196 | return; |
| 197 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 198 | const GrBuffer* vertexBuffer; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 199 | GrMesh mesh; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 200 | int firstVertex; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 201 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 202 | &firstVertex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 203 | if (!verts) { |
| 204 | SkDebugf("Could not allocate vertices\n"); |
| 205 | return; |
| 206 | } |
| 207 | memcpy(verts, vertices, vertexCount * vertexStride); |
| 208 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 209 | const GrBuffer* indexBuffer; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 210 | int firstIndex; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 211 | uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 212 | if (!idxs) { |
| 213 | SkDebugf("Could not allocate indices\n"); |
| 214 | return; |
| 215 | } |
| 216 | memcpy(idxs, indices, indexCount * sizeof(uint16_t)); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 217 | mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex, |
| 218 | firstIndex, vertexCount, indexCount); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 219 | target->draw(gp, mesh); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 220 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 221 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 222 | void onPrepareDraws(Target* target) const override { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 223 | bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); |
| 224 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 225 | // Setup GrGeometryProcessor |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 226 | sk_sp<GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage, |
| 227 | this->viewMatrix(), |
| 228 | this->usesLocalCoords(), |
| 229 | this->coverageIgnored())); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 230 | if (!gp) { |
| 231 | SkDebugf("Couldn't create a GrGeometryProcessor\n"); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 235 | size_t vertexStride = gp->getVertexStride(); |
| 236 | |
| 237 | SkASSERT(canTweakAlphaForCoverage ? |
| 238 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : |
| 239 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); |
| 240 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 241 | int instanceCount = fGeoData.count(); |
| 242 | |
| 243 | int vertexCount = 0; |
| 244 | int indexCount = 0; |
| 245 | int maxVertices = DEFAULT_BUFFER_SIZE; |
| 246 | int maxIndices = DEFAULT_BUFFER_SIZE; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 247 | uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride); |
| 248 | uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 249 | for (int i = 0; i < instanceCount; i++) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 250 | const Geometry& args = fGeoData[i]; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 251 | GrAAConvexTessellator tess(args.fStyle, args.fStrokeWidth, |
| 252 | args.fJoin, args.fMiterLimit); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 253 | |
| 254 | if (!tess.tessellate(args.fViewMatrix, args.fPath)) { |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | int currentIndices = tess.numIndices(); |
| 259 | SkASSERT(currentIndices <= UINT16_MAX); |
| 260 | if (indexCount + currentIndices > UINT16_MAX) { |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 261 | // 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] | 262 | // uint16_t. Draw what we've got so far and reset. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 263 | this->draw(target, gp.get(), |
| 264 | vertexCount, vertexStride, vertices, indexCount, indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 265 | vertexCount = 0; |
| 266 | indexCount = 0; |
| 267 | } |
| 268 | int currentVertices = tess.numPts(); |
| 269 | if (vertexCount + currentVertices > maxVertices) { |
| 270 | maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 271 | vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 272 | } |
| 273 | if (indexCount + currentIndices > maxIndices) { |
| 274 | maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 275 | indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 276 | } |
| 277 | |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 278 | extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor, |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 279 | vertexCount, indices + indexCount, canTweakAlphaForCoverage); |
| 280 | vertexCount += currentVertices; |
| 281 | indexCount += currentIndices; |
| 282 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 283 | this->draw(target, gp.get(), vertexCount, vertexStride, vertices, indexCount, indices); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 284 | sk_free(vertices); |
| 285 | sk_free(indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 288 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 289 | AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>(); |
| 290 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 291 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 292 | return false; |
| 293 | } |
| 294 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 295 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 296 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 301 | // not tweaking |
| 302 | if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { |
| 303 | fBatch.fCanTweakAlphaForCoverage = false; |
| 304 | } |
| 305 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 306 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 307 | this->joinBounds(*that); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
| 311 | GrColor color() const { return fBatch.fColor; } |
| 312 | bool linesOnly() const { return fBatch.fLinesOnly; } |
| 313 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 314 | bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } |
| 315 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 316 | bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 317 | |
| 318 | struct BatchTracker { |
| 319 | GrColor fColor; |
| 320 | bool fUsesLocalCoords; |
| 321 | bool fColorIgnored; |
| 322 | bool fCoverageIgnored; |
| 323 | bool fLinesOnly; |
| 324 | bool fCanTweakAlphaForCoverage; |
| 325 | }; |
| 326 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 327 | struct Geometry { |
| 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 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 337 | BatchTracker fBatch; |
| 338 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 339 | |
| 340 | typedef GrVertexBatch INHERITED; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 341 | }; |
| 342 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 343 | bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 344 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 345 | "GrAALinearizingConvexPathRenderer::onDrawPath"); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 346 | SkASSERT(!args.fRenderTargetContext->isUnifiedMultisampled()); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 347 | SkASSERT(!args.fShape->isEmpty()); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 348 | SkASSERT(!args.fShape->style().pathEffect()); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 349 | |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 350 | SkPath path; |
| 351 | args.fShape->asPath(&path); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 352 | bool fill = args.fShape->style().isSimpleFill(); |
| 353 | const SkStrokeRec& stroke = args.fShape->style().strokeRec(); |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 354 | SkScalar strokeWidth = fill ? -1.0f : stroke.getWidth(); |
| 355 | SkPaint::Join join = fill ? SkPaint::Join::kMiter_Join : stroke.getJoin(); |
| 356 | SkScalar miterLimit = stroke.getMiter(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 357 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame^] | 358 | sk_sp<GrDrawOp> batch(new AAFlatteningConvexPathBatch(args.fPaint->getColor(), |
| 359 | *args.fViewMatrix, |
| 360 | path, strokeWidth, |
| 361 | stroke.getStyle(), |
| 362 | join, miterLimit)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 363 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 364 | GrPipelineBuilder pipelineBuilder(*args.fPaint); |
| 365 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
| 366 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 367 | args.fRenderTargetContext->drawBatch(pipelineBuilder, *args.fClip, batch.get()); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 368 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | |
| 372 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 373 | |
| 374 | #ifdef GR_TEST_UTILS |
| 375 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 376 | DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) { |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 377 | GrColor color = GrRandomColor(random); |
| 378 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 379 | SkPath path = GrTest::TestPathConvex(random); |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 380 | |
| 381 | SkStrokeRec::Style styles[3] = { SkStrokeRec::kFill_Style, |
| 382 | SkStrokeRec::kStroke_Style, |
| 383 | SkStrokeRec::kStrokeAndFill_Style }; |
| 384 | |
| 385 | SkStrokeRec::Style style = styles[random->nextU() % 3]; |
| 386 | |
| 387 | SkScalar strokeWidth = -1.f; |
bsalomon | 0432dd6 | 2016-06-30 07:19:27 -0700 | [diff] [blame] | 388 | SkPaint::Join join = SkPaint::kMiter_Join; |
| 389 | SkScalar miterLimit = 0.5f; |
robertphillips | 8c17097 | 2016-09-22 12:42:30 -0700 | [diff] [blame] | 390 | |
| 391 | if (SkStrokeRec::kFill_Style != style) { |
| 392 | strokeWidth = random->nextRangeF(1.0f, 10.0f); |
| 393 | if (random->nextBool()) { |
| 394 | join = SkPaint::kMiter_Join; |
| 395 | } else { |
| 396 | join = SkPaint::kBevel_Join; |
| 397 | } |
| 398 | miterLimit = random->nextRangeF(0.5f, 2.0f); |
| 399 | } |
| 400 | |
| 401 | return new AAFlatteningConvexPathBatch(color, viewMatrix, path, strokeWidth, |
| 402 | style, join, miterLimit); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | #endif |