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