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