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" |
| 19 | #include "GrPipelineBuilder.h" |
| 20 | #include "GrStrokeInfo.h" |
| 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 | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 43 | if (args.fPath->isInverseFillType()) { |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 44 | return false; |
| 45 | } |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 46 | if (!args.fPath->isConvex()) { |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 49 | if (args.fStroke->getStyle() == SkStrokeRec::kStroke_Style) { |
ethannicholas | fea7763 | 2015-08-19 12:09:12 -0700 | [diff] [blame] | 50 | if (!args.fViewMatrix->isSimilarity()) { |
| 51 | return false; |
| 52 | } |
| 53 | SkScalar strokeWidth = args.fViewMatrix->getMaxScale() * args.fStroke->getWidth(); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 54 | return strokeWidth >= 1.0f && strokeWidth <= kMaxStrokeWidth && !args.fStroke->isDashed() && |
ethannicholas | c88cb89 | 2015-12-15 11:01:12 -0800 | [diff] [blame] | 55 | SkPathPriv::IsClosedSingleContour(*args.fPath) && |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 56 | args.fStroke->getJoin() != SkPaint::Join::kRound_Join; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 57 | } |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 58 | return args.fStroke->getStyle() == SkStrokeRec::kFill_Style; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // extract the result vertices and indices from the GrAAConvexTessellator |
| 62 | static void extract_verts(const GrAAConvexTessellator& tess, |
| 63 | void* vertices, |
| 64 | size_t vertexStride, |
| 65 | GrColor color, |
| 66 | uint16_t firstIndex, |
| 67 | uint16_t* idxs, |
| 68 | bool tweakAlphaForCoverage) { |
| 69 | intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
| 70 | |
| 71 | for (int i = 0; i < tess.numPts(); ++i) { |
| 72 | *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i); |
| 73 | } |
| 74 | |
| 75 | // Make 'verts' point to the colors |
| 76 | verts += sizeof(SkPoint); |
| 77 | for (int i = 0; i < tess.numPts(); ++i) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 78 | if (tweakAlphaForCoverage) { |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 79 | SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255); |
| 80 | unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 81 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 82 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 83 | } else { |
| 84 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 85 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 86 | tess.coverage(i); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | for (int i = 0; i < tess.numIndices(); ++i) { |
| 91 | idxs[i] = tess.index(i) + firstIndex; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static const GrGeometryProcessor* create_fill_gp(bool tweakAlphaForCoverage, |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 96 | const SkMatrix& viewMatrix, |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 97 | bool usesLocalCoords, |
| 98 | bool coverageIgnored) { |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 99 | using namespace GrDefaultGeoProcFactory; |
joshualitt | e494a58 | 2015-08-03 09:32:36 -0700 | [diff] [blame] | 100 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 101 | Color color(Color::kAttribute_Type); |
| 102 | Coverage::Type coverageType; |
| 103 | // TODO remove coverage if coverage is ignored |
| 104 | /*if (coverageIgnored) { |
| 105 | coverageType = Coverage::kNone_Type; |
| 106 | } else*/ if (tweakAlphaForCoverage) { |
| 107 | coverageType = Coverage::kSolid_Type; |
| 108 | } else { |
| 109 | coverageType = Coverage::kAttribute_Type; |
| 110 | } |
| 111 | Coverage coverage(coverageType); |
| 112 | LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type : |
| 113 | LocalCoords::kUnused_Type); |
| 114 | return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 115 | } |
| 116 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 117 | class AAFlatteningConvexPathBatch : public GrVertexBatch { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 118 | public: |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 119 | DEFINE_BATCH_CLASS_ID |
| 120 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 121 | struct Geometry { |
| 122 | GrColor fColor; |
| 123 | SkMatrix fViewMatrix; |
| 124 | SkPath fPath; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 125 | SkScalar fStrokeWidth; |
| 126 | SkPaint::Join fJoin; |
| 127 | SkScalar fMiterLimit; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 130 | static GrDrawBatch* Create(const Geometry& geometry) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 131 | return new AAFlatteningConvexPathBatch(geometry); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | const char* name() const override { return "AAConvexBatch"; } |
| 135 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 136 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 137 | GrInitInvariantOutput* coverage, |
| 138 | GrBatchToXPOverrides* overrides) const override { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 139 | // When this is called on a batch, there is only one geometry bundle |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 140 | color->setKnownFourComponents(fGeoData[0].fColor); |
| 141 | coverage->setUnknownSingleComponent(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 142 | } |
| 143 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 144 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 145 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 146 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 147 | if (!overrides.readsColor()) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 148 | fGeoData[0].fColor = GrColor_ILLEGAL; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 149 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 150 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 151 | |
| 152 | // setup batch properties |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 153 | fBatch.fColorIgnored = !overrides.readsColor(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 154 | fBatch.fColor = fGeoData[0].fColor; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 155 | fBatch.fUsesLocalCoords = overrides.readsLocalCoords(); |
| 156 | fBatch.fCoverageIgnored = !overrides.readsCoverage(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 157 | fBatch.fLinesOnly = SkPath::kLine_SegmentMask == fGeoData[0].fPath.getSegmentMasks(); |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 158 | fBatch.fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 159 | } |
| 160 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 161 | void draw(GrVertexBatch::Target* target, const GrGeometryProcessor* gp, int vertexCount, |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 162 | size_t vertexStride, void* vertices, int indexCount, uint16_t* indices) const { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 163 | if (vertexCount == 0 || indexCount == 0) { |
| 164 | return; |
| 165 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 166 | const GrBuffer* vertexBuffer; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 167 | GrMesh mesh; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 168 | int firstVertex; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 169 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 170 | &firstVertex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 171 | if (!verts) { |
| 172 | SkDebugf("Could not allocate vertices\n"); |
| 173 | return; |
| 174 | } |
| 175 | memcpy(verts, vertices, vertexCount * vertexStride); |
| 176 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 177 | const GrBuffer* indexBuffer; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 178 | int firstIndex; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 179 | uint16_t* idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 180 | if (!idxs) { |
| 181 | SkDebugf("Could not allocate indices\n"); |
| 182 | return; |
| 183 | } |
| 184 | memcpy(idxs, indices, indexCount * sizeof(uint16_t)); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 185 | mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex, |
| 186 | firstIndex, vertexCount, indexCount); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 187 | target->draw(gp, mesh); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 188 | } |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 189 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 190 | void onPrepareDraws(Target* target) const override { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 191 | bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); |
| 192 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 193 | // Setup GrGeometryProcessor |
| 194 | SkAutoTUnref<const GrGeometryProcessor> gp(create_fill_gp(canTweakAlphaForCoverage, |
| 195 | this->viewMatrix(), |
| 196 | this->usesLocalCoords(), |
| 197 | this->coverageIgnored())); |
| 198 | if (!gp) { |
| 199 | SkDebugf("Couldn't create a GrGeometryProcessor\n"); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 203 | size_t vertexStride = gp->getVertexStride(); |
| 204 | |
| 205 | SkASSERT(canTweakAlphaForCoverage ? |
| 206 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr) : |
| 207 | vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr)); |
| 208 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 209 | int instanceCount = fGeoData.count(); |
| 210 | |
| 211 | int vertexCount = 0; |
| 212 | int indexCount = 0; |
| 213 | int maxVertices = DEFAULT_BUFFER_SIZE; |
| 214 | int maxIndices = DEFAULT_BUFFER_SIZE; |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 215 | uint8_t* vertices = (uint8_t*) sk_malloc_throw(maxVertices * vertexStride); |
| 216 | uint16_t* indices = (uint16_t*) sk_malloc_throw(maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 217 | for (int i = 0; i < instanceCount; i++) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 218 | const Geometry& args = fGeoData[i]; |
fmalita | bd5d7e7 | 2015-06-26 07:18:24 -0700 | [diff] [blame] | 219 | GrAAConvexTessellator tess(args.fStrokeWidth, args.fJoin, args.fMiterLimit); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 220 | |
| 221 | if (!tess.tessellate(args.fViewMatrix, args.fPath)) { |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | int currentIndices = tess.numIndices(); |
| 226 | SkASSERT(currentIndices <= UINT16_MAX); |
| 227 | if (indexCount + currentIndices > UINT16_MAX) { |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 228 | // 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] | 229 | // uint16_t. Draw what we've got so far and reset. |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 230 | this->draw(target, gp, vertexCount, vertexStride, vertices, indexCount, indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 231 | vertexCount = 0; |
| 232 | indexCount = 0; |
| 233 | } |
| 234 | int currentVertices = tess.numPts(); |
| 235 | if (vertexCount + currentVertices > maxVertices) { |
| 236 | maxVertices = SkTMax(vertexCount + currentVertices, maxVertices * 2); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 237 | vertices = (uint8_t*) sk_realloc_throw(vertices, maxVertices * vertexStride); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 238 | } |
| 239 | if (indexCount + currentIndices > maxIndices) { |
| 240 | maxIndices = SkTMax(indexCount + currentIndices, maxIndices * 2); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 241 | indices = (uint16_t*) sk_realloc_throw(indices, maxIndices * sizeof(uint16_t)); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 242 | } |
| 243 | |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 244 | extract_verts(tess, vertices + vertexStride * vertexCount, vertexStride, args.fColor, |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 245 | vertexCount, indices + indexCount, canTweakAlphaForCoverage); |
| 246 | vertexCount += currentVertices; |
| 247 | indexCount += currentIndices; |
| 248 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 249 | this->draw(target, gp, vertexCount, vertexStride, vertices, indexCount, indices); |
mtklein | 002c2ce | 2015-08-26 05:43:22 -0700 | [diff] [blame] | 250 | sk_free(vertices); |
| 251 | sk_free(indices); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 255 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 256 | AAFlatteningConvexPathBatch(const Geometry& geometry) : INHERITED(ClassID()) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 257 | fGeoData.push_back(geometry); |
| 258 | |
| 259 | // compute bounds |
| 260 | fBounds = geometry.fPath.getBounds(); |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 261 | SkScalar w = geometry.fStrokeWidth; |
| 262 | if (w > 0) { |
| 263 | w /= 2; |
| 264 | // If the miter limit is < 1 then we effectively fallback to bevel joins. |
| 265 | if (SkPaint::kMiter_Join == geometry.fJoin && w > 1.f) { |
| 266 | w *= geometry.fMiterLimit; |
| 267 | } |
| 268 | fBounds.outset(w, w); |
| 269 | } |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 270 | geometry.fViewMatrix.mapRect(&fBounds); |
| 271 | } |
| 272 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 273 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 274 | AAFlatteningConvexPathBatch* that = t->cast<AAFlatteningConvexPathBatch>(); |
| 275 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 276 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 280 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 281 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 286 | // not tweaking |
| 287 | if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) { |
| 288 | fBatch.fCanTweakAlphaForCoverage = false; |
| 289 | } |
| 290 | |
| 291 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 292 | this->joinBounds(that->bounds()); |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | GrColor color() const { return fBatch.fColor; } |
| 297 | bool linesOnly() const { return fBatch.fLinesOnly; } |
| 298 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 299 | bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCoverage; } |
| 300 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 301 | bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 302 | |
| 303 | struct BatchTracker { |
| 304 | GrColor fColor; |
| 305 | bool fUsesLocalCoords; |
| 306 | bool fColorIgnored; |
| 307 | bool fCoverageIgnored; |
| 308 | bool fLinesOnly; |
| 309 | bool fCanTweakAlphaForCoverage; |
| 310 | }; |
| 311 | |
| 312 | BatchTracker fBatch; |
| 313 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 314 | |
| 315 | typedef GrVertexBatch INHERITED; |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 318 | bool GrAALinearizingConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
joshualitt | de83b41 | 2016-01-14 09:58:36 -0800 | [diff] [blame] | 319 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(), |
| 320 | "GrAALinearizingConvexPathRenderer::onDrawPath"); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 321 | if (args.fPath->isEmpty()) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | AAFlatteningConvexPathBatch::Geometry geometry; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 325 | geometry.fColor = args.fColor; |
| 326 | geometry.fViewMatrix = *args.fViewMatrix; |
| 327 | geometry.fPath = *args.fPath; |
| 328 | geometry.fStrokeWidth = args.fStroke->isFillStyle() ? -1.0f : args.fStroke->getWidth(); |
| 329 | geometry.fJoin = args.fStroke->isFillStyle() ? SkPaint::Join::kMiter_Join : |
| 330 | args.fStroke->getJoin(); |
| 331 | geometry.fMiterLimit = args.fStroke->getMiter(); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 332 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 333 | SkAutoTUnref<GrDrawBatch> batch(AAFlatteningConvexPathBatch::Create(geometry)); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 334 | args.fTarget->drawBatch(*args.fPipelineBuilder, batch); |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 335 | |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 340 | |
| 341 | #ifdef GR_TEST_UTILS |
| 342 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 343 | DRAW_BATCH_TEST_DEFINE(AAFlatteningConvexPathBatch) { |
ethannicholas | 1a1b3ac | 2015-06-10 12:11:17 -0700 | [diff] [blame] | 344 | AAFlatteningConvexPathBatch::Geometry geometry; |
| 345 | geometry.fColor = GrRandomColor(random); |
| 346 | geometry.fViewMatrix = GrTest::TestMatrixInvertible(random); |
| 347 | geometry.fPath = GrTest::TestPathConvex(random); |
| 348 | |
| 349 | return AAFlatteningConvexPathBatch::Create(geometry); |
| 350 | } |
| 351 | |
| 352 | #endif |