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