blob: 0cbf9b677a82cc8157bd04149686b6e2435ec536 [file] [log] [blame]
joshualitt9ff64252015-08-10 09:03:51 -07001/*
Michael Ludwig72ab3462018-12-10 12:43:36 -05002 * Copyright 2018 Google Inc.
joshualitt9ff64252015-08-10 09:03:51 -07003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrStrokeRectOp.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkStrokeRec.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrResourceKey.h"
12#include "include/utils/SkRandom.h"
13#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrDefaultGeoProcFactory.h"
16#include "src/gpu/GrDrawOpTest.h"
17#include "src/gpu/GrOpFlushState.h"
18#include "src/gpu/GrResourceProvider.h"
19#include "src/gpu/GrVertexWriter.h"
20#include "src/gpu/ops/GrFillRectOp.h"
21#include "src/gpu/ops/GrMeshDrawOp.h"
22#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt9ff64252015-08-10 09:03:51 -070023
Michael Ludwig72ab3462018-12-10 12:43:36 -050024namespace {
joshualitt9ff64252015-08-10 09:03:51 -070025
bsalomon8b7a9e12016-07-06 13:06:22 -070026// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
Michael Ludwig72ab3462018-12-10 12:43:36 -050027// limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only
28// supported when using an AA stroke.
29inline static bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) {
bsalomon8b7a9e12016-07-06 13:06:22 -070030 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
31 stroke.getStyle() == SkStrokeRec::kHairline_Style);
32 // For hairlines, make bevel and round joins appear the same as mitered ones.
33 if (!stroke.getWidth()) {
34 *isMiter = true;
35 return true;
36 }
37 if (stroke.getJoin() == SkPaint::kBevel_Join) {
38 *isMiter = false;
Michael Ludwig72ab3462018-12-10 12:43:36 -050039 return aa == GrAA::kYes; // bevel only supported with AA
bsalomon8b7a9e12016-07-06 13:06:22 -070040 }
41 if (stroke.getJoin() == SkPaint::kMiter_Join) {
42 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
Michael Ludwig72ab3462018-12-10 12:43:36 -050043 // Supported under non-AA only if it remains mitered
44 return aa == GrAA::kYes || *isMiter;
bsalomon8b7a9e12016-07-06 13:06:22 -070045 }
46 return false;
47}
48
Michael Ludwig72ab3462018-12-10 12:43:36 -050049
50///////////////////////////////////////////////////////////////////////////////////////////////////
51// Non-AA Stroking
52///////////////////////////////////////////////////////////////////////////////////////////////////
53
54/* create a triangle strip that strokes the specified rect. There are 8
55 unique vertices, but we repeat the last 2 to close up. Alternatively we
56 could use an indices array, and then only send 8 verts, but not sure that
57 would be faster.
58 */
59static void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
60 const SkScalar rad = SkScalarHalf(width);
61
62 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
63 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
64 verts[2].set(rect.fRight - rad, rect.fTop + rad);
65 verts[3].set(rect.fRight + rad, rect.fTop - rad);
66 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
67 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
68 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
69 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
70 verts[8] = verts[0];
71 verts[9] = verts[1];
72
73 // TODO: we should be catching this higher up the call stack and just draw a single
74 // non-AA rect
75 if (2*rad >= rect.width()) {
76 verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX();
77 }
78 if (2*rad >= rect.height()) {
79 verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY();
80 }
81}
82
83class NonAAStrokeRectOp final : public GrMeshDrawOp {
84private:
85 using Helper = GrSimpleMeshDrawOpHelper;
86
87public:
88 DEFINE_OP_CLASS_ID
89
90 const char* name() const override { return "NonAAStrokeRectOp"; }
91
Chris Dalton1706cbf2019-05-21 19:35:29 -060092 void visitProxies(const VisitProxyFunc& func) const override {
Michael Ludwig72ab3462018-12-10 12:43:36 -050093 fHelper.visitProxies(func);
94 }
95
96#ifdef SK_DEBUG
97 SkString dumpInfo() const override {
98 SkString string;
99 string.appendf(
100 "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
101 "StrokeWidth: %.2f\n",
102 fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom,
103 fStrokeWidth);
104 string += fHelper.dumpInfo();
105 string += INHERITED::dumpInfo();
106 return string;
107 }
108#endif
109
Robert Phillipsb97da532019-02-12 15:24:12 -0500110 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500111 GrPaint&& paint,
112 const SkMatrix& viewMatrix,
113 const SkRect& rect,
114 const SkStrokeRec& stroke,
115 GrAAType aaType) {
116 bool isMiter;
117 if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) {
118 return nullptr;
119 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600120 Helper::InputFlags inputFlags = Helper::InputFlags::kNone;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500121 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
122 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
123 // when MSAA is enabled because it can cause ugly artifacts.
124 if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) {
Chris Daltonbaa1b352019-04-03 12:03:00 -0600125 inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500126 }
Chris Daltonbaa1b352019-04-03 12:03:00 -0600127 return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500128 viewMatrix, rect,
129 stroke, aaType);
130 }
131
132 NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Chris Daltonbaa1b352019-04-03 12:03:00 -0600133 Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500134 const SkStrokeRec& stroke, GrAAType aaType)
Chris Daltonbaa1b352019-04-03 12:03:00 -0600135 : INHERITED(ClassID()), fHelper(helperArgs, aaType, inputFlags) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500136 fColor = color;
137 fViewMatrix = viewMatrix;
138 fRect = rect;
139 // Sort the rect for hairlines
140 fRect.sort();
141 fStrokeWidth = stroke.getWidth();
142
Greg Daniel5faf4742019-10-01 15:14:44 -0400143 SkScalar rad = SkScalarHalf(fStrokeWidth);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500144 SkRect bounds = rect;
145 bounds.outset(rad, rad);
146
147 // If our caller snaps to pixel centers then we have to round out the bounds
Chris Daltonbaa1b352019-04-03 12:03:00 -0600148 if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400149 SkASSERT(!fStrokeWidth || aaType == GrAAType::kNone);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500150 viewMatrix.mapRect(&bounds);
151 // We want to be consistent with how we snap non-aa lines. To match what we do in
152 // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a
153 // pixel to force us to pixel centers.
Mike Reed92b33352019-08-24 19:39:13 -0400154 bounds.setLTRB(SkScalarFloorToScalar(bounds.fLeft),
155 SkScalarFloorToScalar(bounds.fTop),
156 SkScalarFloorToScalar(bounds.fRight),
157 SkScalarFloorToScalar(bounds.fBottom));
Michael Ludwig72ab3462018-12-10 12:43:36 -0500158 bounds.offset(0.5f, 0.5f);
Greg Daniel5faf4742019-10-01 15:14:44 -0400159 this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500160 } else {
Greg Daniel5faf4742019-10-01 15:14:44 -0400161 HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
162 this->setTransformedBounds(bounds, fViewMatrix, aaBloat,
163 fStrokeWidth ? IsHairline::kNo : IsHairline::kYes);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500164 }
165 }
166
167 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
168
Chris Dalton6ce447a2019-06-23 18:07:38 -0600169 GrProcessorSet::Analysis finalize(
170 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
171 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400172 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600173 return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400174 GrProcessorAnalysisCoverage::kNone, &fColor, nullptr);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500175 }
176
177private:
178 void onPrepareDraws(Target* target) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500179 GrGeometryProcessor* gp;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500180 {
181 using namespace GrDefaultGeoProcFactory;
182 Color color(fColor);
183 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
184 ? LocalCoords::kUsePosition_Type
185 : LocalCoords::kUnused_Type;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500186 gp = GrDefaultGeoProcFactory::Make(target->allocator(), target->caps().shaderCaps(),
187 color, Coverage::kSolid_Type, localCoordsType,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500188 fViewMatrix);
189 }
190
191 size_t kVertexStride = gp->vertexStride();
192 int vertexCount = kVertsPerHairlineRect;
193 if (fStrokeWidth > 0) {
194 vertexCount = kVertsPerStrokeRect;
195 }
196
Brian Salomon12d22642019-01-29 14:38:50 -0500197 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500198 int firstVertex;
199
200 void* verts =
201 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
202
203 if (!verts) {
204 SkDebugf("Could not allocate vertices\n");
205 return;
206 }
207
208 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
209
210 GrPrimitiveType primType;
211 if (fStrokeWidth > 0) {
212 primType = GrPrimitiveType::kTriangleStrip;
213 init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
214 } else {
215 // hairline
216 primType = GrPrimitiveType::kLineStrip;
217 vertex[0].set(fRect.fLeft, fRect.fTop);
218 vertex[1].set(fRect.fRight, fRect.fTop);
219 vertex[2].set(fRect.fRight, fRect.fBottom);
220 vertex[3].set(fRect.fLeft, fRect.fBottom);
221 vertex[4].set(fRect.fLeft, fRect.fTop);
222 }
223
224 GrMesh* mesh = target->allocMesh(primType);
225 mesh->setNonIndexedNonInstanced(vertexCount);
Brian Salomon12d22642019-01-29 14:38:50 -0500226 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500227 target->recordDraw(gp, mesh, 1, primType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700228 }
229
230 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500231 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
232 fHelper.detachProcessorSet(),
233 fHelper.pipelineFlags());
234
235 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500236 }
237
238 // TODO: override onCombineIfPossible
239
240 Helper fHelper;
241 SkPMColor4f fColor;
242 SkMatrix fViewMatrix;
243 SkRect fRect;
244 SkScalar fStrokeWidth;
245
246 const static int kVertsPerHairlineRect = 5;
247 const static int kVertsPerStrokeRect = 10;
248
249 typedef GrMeshDrawOp INHERITED;
250};
251
252///////////////////////////////////////////////////////////////////////////////////////////////////
253// AA Stroking
254///////////////////////////////////////////////////////////////////////////////////////////////////
255
256GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
257GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
258
259static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
260 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
261 SkScalar strokeWidth, bool miterStroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700262 SkRect devRect;
263 viewMatrix.mapRect(&devRect, rect);
264
265 SkVector devStrokeSize;
266 if (strokeWidth > 0) {
267 devStrokeSize.set(strokeWidth, strokeWidth);
268 viewMatrix.mapVectors(&devStrokeSize, 1);
269 devStrokeSize.setAbs(devStrokeSize);
270 } else {
271 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
272 }
273
274 const SkScalar dx = devStrokeSize.fX;
275 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500276 const SkScalar rx = SkScalarHalf(dx);
277 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700278
279 *devOutside = devRect;
280 *devOutsideAssist = devRect;
281 *devInside = devRect;
282
283 devOutside->outset(rx, ry);
284 devInside->inset(rx, ry);
285
286 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
287 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
288 // together when we render these rects.
289 SkScalar spare;
290 {
291 SkScalar w = devRect.width() - dx;
292 SkScalar h = devRect.height() - dy;
293 spare = SkTMin(w, h);
294 }
295
296 *isDegenerate = spare <= 0;
297 if (*isDegenerate) {
298 devInside->fLeft = devInside->fRight = devRect.centerX();
299 devInside->fTop = devInside->fBottom = devRect.centerY();
300 }
301
302 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
303 // to draw the outside of the octagon. Because there are 8 vertices on the outer
304 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
305 if (!miterStroke) {
306 devOutside->inset(0, ry);
307 devOutsideAssist->outset(0, ry);
308 }
309}
310
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500311static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena,
312 const GrShaderCaps* shaderCaps,
313 bool tweakAlphaForCoverage,
314 const SkMatrix& viewMatrix,
315 bool usesLocalCoords,
316 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700317 using namespace GrDefaultGeoProcFactory;
318
Brian Osman2a4c4df2018-12-20 14:06:54 -0500319 Coverage::Type coverageType =
320 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500321 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500322 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
323 Color::Type colorType =
324 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
325
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500326 return MakeForDeviceSpace(arena, shaderCaps, colorType, coverageType,
327 localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700328}
329
Brian Salomonbaaf4392017-06-15 09:59:23 -0400330class AAStrokeRectOp final : public GrMeshDrawOp {
331private:
332 using Helper = GrSimpleMeshDrawOpHelper;
333
joshualitt3566d442015-09-18 07:12:55 -0700334public:
Brian Salomon25a88092016-12-01 09:36:50 -0500335 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700336
Robert Phillipsb97da532019-02-12 15:24:12 -0500337 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400338 GrPaint&& paint,
339 const SkMatrix& viewMatrix,
340 const SkRect& devOutside,
341 const SkRect& devInside) {
342 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
343 devOutside, devInside);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400344 }
345
Brian Osmancf860852018-10-31 14:04:39 -0400346 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400347 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400348 : INHERITED(ClassID())
349 , fHelper(helperArgs, GrAAType::kCoverage)
350 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700351 SkASSERT(!devOutside.isEmpty());
352 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700353
Brian Salomon8c5bad32016-12-20 14:43:36 -0500354 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
Greg Daniel5faf4742019-10-01 15:14:44 -0400355 this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700356 fMiterStroke = true;
357 }
358
Robert Phillipsb97da532019-02-12 15:24:12 -0500359 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400360 GrPaint&& paint,
361 const SkMatrix& viewMatrix,
362 const SkRect& rect,
363 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700364 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500365 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700366 return nullptr;
367 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400368 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
369 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400370 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700371
Brian Osmancf860852018-10-31 14:04:39 -0400372 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400373 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
374 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400375 : INHERITED(ClassID())
376 , fHelper(helperArgs, GrAAType::kCoverage)
377 , fViewMatrix(viewMatrix) {
378 fMiterStroke = isMiter;
379 RectInfo& info = fRects.push_back();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500380 compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
381 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500382 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400383 if (isMiter) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400384 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400385 } else {
386 // The outer polygon of the bevel stroke is an octagon specified by the points of a
387 // pair of overlapping rectangles where one is wide and the other is narrow.
388 SkRect bounds = info.fDevOutside;
389 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Greg Daniel5faf4742019-10-01 15:14:44 -0400390 this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400391 }
joshualitt3566d442015-09-18 07:12:55 -0700392 }
393
394 const char* name() const override { return "AAStrokeRect"; }
395
Chris Dalton1706cbf2019-05-21 19:35:29 -0600396 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400397 fHelper.visitProxies(func);
398 }
399
Brian Osman9a390ac2018-11-12 09:47:48 -0500400#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500401 SkString dumpInfo() const override {
402 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500403 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500404 string.appendf(
405 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
406 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
407 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Osmancf860852018-10-31 14:04:39 -0400408 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500409 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
410 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
411 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
412 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500413 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400414 string += fHelper.dumpInfo();
415 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500416 return string;
417 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500418#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500419
Brian Salomonbaaf4392017-06-15 09:59:23 -0400420 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400421
Chris Dalton6ce447a2019-06-23 18:07:38 -0600422 GrProcessorSet::Analysis finalize(
423 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
424 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700425 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600426 caps, clip, hasMixedSampledCoverage, clampType,
427 GrProcessorAnalysisCoverage::kSingleChannel, &fRects.back().fColor, &fWideColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400428 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400429
430private:
Brian Salomon91326c32017-08-09 16:02:19 -0400431 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700432 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
joshualittaa37a962015-09-18 13:03:25 -0700433
joshualitt3566d442015-09-18 07:12:55 -0700434 static const int kMiterIndexCnt = 3 * 24;
435 static const int kMiterVertexCnt = 16;
436 static const int kNumMiterRectsInIndexBuffer = 256;
437
438 static const int kBevelIndexCnt = 48 + 36 + 24;
439 static const int kBevelVertexCnt = 24;
440 static const int kNumBevelRectsInIndexBuffer = 256;
441
Brian Salomondbf70722019-02-07 11:31:24 -0500442 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700443
joshualittaa37a962015-09-18 13:03:25 -0700444 const SkMatrix& viewMatrix() const { return fViewMatrix; }
445 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700446
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500447 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700448
Brian Osmancfec9d52018-11-20 11:39:15 -0500449 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500450 const SkPMColor4f& color,
451 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700452 const SkRect& devOutside,
453 const SkRect& devOutsideAssist,
454 const SkRect& devInside,
455 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700456 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700457 bool tweakAlphaForCoverage) const;
458
bsalomon8b7a9e12016-07-06 13:06:22 -0700459 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500460 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400461 SkPMColor4f fColor;
bsalomon8b7a9e12016-07-06 13:06:22 -0700462 SkRect fDevOutside;
463 SkRect fDevOutsideAssist;
464 SkRect fDevInside;
465 bool fDegenerate;
466 };
467
Brian Salomonbaaf4392017-06-15 09:59:23 -0400468 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500469 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700470 SkMatrix fViewMatrix;
471 bool fMiterStroke;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500472 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700473
Brian Salomonbaaf4392017-06-15 09:59:23 -0400474 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700475};
476
Brian Salomon91326c32017-08-09 16:02:19 -0400477void AAStrokeRectOp::onPrepareDraws(Target* target) {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500478 GrGeometryProcessor* gp = create_aa_stroke_rect_gp(target->allocator(),
479 target->caps().shaderCaps(),
480 fHelper.compatibleWithCoverageAsAlpha(),
481 this->viewMatrix(),
482 fHelper.usesLocalCoords(),
483 fWideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700484 if (!gp) {
485 SkDebugf("Couldn't create GrGeometryProcessor\n");
486 return;
487 }
488
joshualitt9ff64252015-08-10 09:03:51 -0700489 int innerVertexNum = 4;
490 int outerVertexNum = this->miterStroke() ? 4 : 8;
491 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
492 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500493 int instanceCount = fRects.count();
Robert Phillipsee08d522019-10-28 16:34:44 -0400494 int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer;
joshualitt9ff64252015-08-10 09:03:51 -0700495
Brian Salomondbf70722019-02-07 11:31:24 -0500496 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000497 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500498 if (!indexBuffer) {
499 SkDebugf("Could not allocate indices\n");
500 return;
501 }
502 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
503 std::move(indexBuffer), verticesPerInstance, indicesPerInstance,
Robert Phillipsee08d522019-10-28 16:34:44 -0400504 instanceCount, maxQuads);
Brian Osmancfec9d52018-11-20 11:39:15 -0500505 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500506 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500507 SkDebugf("Could not allocate vertices\n");
508 return;
509 }
joshualitt9ff64252015-08-10 09:03:51 -0700510
511 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500512 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700513 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500514 info.fColor,
515 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500516 info.fDevOutside,
517 info.fDevOutsideAssist,
518 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700519 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500520 info.fDegenerate,
Brian Osman605c6d52019-03-15 12:10:35 -0400521 fHelper.compatibleWithCoverageAsAlpha());
joshualitt9ff64252015-08-10 09:03:51 -0700522 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500523 helper.recordDraw(target, gp);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700524}
525
526void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500527 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
528 fHelper.detachProcessorSet(),
529 fHelper.pipelineFlags());
530
531 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
joshualitt9ff64252015-08-10 09:03:51 -0700532}
533
Brian Salomondbf70722019-02-07 11:31:24 -0500534sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
535 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700536 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500537 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700538 static const uint16_t gMiterIndices[] = {
539 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
540 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
541 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
542 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
543
544 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
545 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
546 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
547 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
548
549 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
550 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
551 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
552 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
553 };
Brian Salomon6a639042016-12-14 11:08:17 -0500554 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500555 static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700556 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400557 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500558 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
559 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700560 } else {
561 /**
562 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
563 * from the first index. The index layout:
564 * outer AA line: 0~3, 4~7
565 * outer edge: 8~11, 12~15
566 * inner edge: 16~19
567 * inner AA line: 20~23
568 * Following comes a bevel-stroke rect and its indices:
569 *
570 * 4 7
571 * *********************************
572 * * ______________________________ *
573 * * / 12 15 \ *
574 * * / \ *
575 * 0 * |8 16_____________________19 11 | * 3
576 * * | | | | *
577 * * | | **************** | | *
578 * * | | * 20 23 * | | *
579 * * | | * * | | *
580 * * | | * 21 22 * | | *
581 * * | | **************** | | *
582 * * | |____________________| | *
583 * 1 * |9 17 18 10| * 2
584 * * \ / *
585 * * \13 __________________________14/ *
586 * * *
587 * **********************************
588 * 5 6
589 */
Brian Salomon6a639042016-12-14 11:08:17 -0500590 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700591 static const uint16_t gBevelIndices[] = {
592 // Draw outer AA, from outer AA line to outer edge, shift is 0.
593 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
594 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
595 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
596 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
597 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
598 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
599 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
600 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
601
602 // Draw the stroke, from outer edge to inner edge, shift is 8.
603 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
604 1 + 8, 5 + 8, 9 + 8,
605 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
606 6 + 8, 2 + 8, 10 + 8,
607 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
608 3 + 8, 7 + 8, 11 + 8,
609 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
610 4 + 8, 0 + 8, 8 + 8,
611
612 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
613 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
614 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
615 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
616 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
617 };
Brian Salomon6a639042016-12-14 11:08:17 -0500618 // clang-format on
Brian Salomon4dea72a2019-12-18 10:43:10 -0500619 static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
joshualitt9ff64252015-08-10 09:03:51 -0700620
621 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400622 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500623 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
624 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700625 }
626}
627
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500628GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
Michael Ludwigd0840ec2019-12-12 09:48:38 -0500629 const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500630 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700631
Brian Salomonbaaf4392017-06-15 09:59:23 -0400632 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000633 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700634 }
635
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500636 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700637 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000638 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700639 }
640
641 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400642 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
Mike Reed2c383152019-12-18 16:47:47 -0500643 if (fHelper.usesLocalCoords() &&
644 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix()))
645 {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000646 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700647 }
648
Brian Salomon8c5bad32016-12-20 14:43:36 -0500649 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500650 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000651 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700652}
653
joshualitt11edad92015-09-22 10:32:28 -0700654static void setup_scale(int* scale, SkScalar inset) {
655 if (inset < SK_ScalarHalf) {
656 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
657 SkASSERT(*scale >= 0 && *scale <= 255);
658 } else {
659 *scale = 0xff;
660 }
661}
662
Brian Osmancfec9d52018-11-20 11:39:15 -0500663void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500664 const SkPMColor4f& color,
665 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500666 const SkRect& devOutside,
667 const SkRect& devOutsideAssist,
668 const SkRect& devInside,
669 bool miterStroke,
670 bool degenerate,
671 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700672 // We create vertices for four nested rectangles. There are two ramps from 0 to full
673 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700674
joshualitt9ff64252015-08-10 09:03:51 -0700675 // TODO: this only really works if the X & Y margins are the same all around
676 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700677 SkScalar inset;
678 if (!degenerate) {
679 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
680 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
681 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
682 if (miterStroke) {
683 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
684 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500685 inset = SK_ScalarHalf *
686 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700687 }
688 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700689 } else {
joshualitt11edad92015-09-22 10:32:28 -0700690 // TODO use real devRect here
691 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500692 inset = SK_ScalarHalf *
693 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700694 }
joshualitt9ff64252015-08-10 09:03:51 -0700695
Brian Osmancfec9d52018-11-20 11:39:15 -0500696 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
697 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
698 };
joshualitt9ff64252015-08-10 09:03:51 -0700699
Brian Osmancfec9d52018-11-20 11:39:15 -0500700 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
701 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
702 };
703
Brian Osman2a4c4df2018-12-20 14:06:54 -0500704 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500705
706 // Outermost rect
707 vertices.writeQuad(inset_fan(devOutside, -SK_ScalarHalf, -SK_ScalarHalf),
708 outerColor,
709 maybe_coverage(0.0f));
710
711 if (!miterStroke) {
712 // Second outermost
713 vertices.writeQuad(inset_fan(devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf),
714 outerColor,
715 maybe_coverage(0.0f));
joshualitt9ff64252015-08-10 09:03:51 -0700716 }
717
718 // scale is the coverage for the the inner two rects.
719 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700720 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700721
722 float innerCoverage = GrNormalizeByteToFloat(scale);
Brian Osman2a4c4df2018-12-20 14:06:54 -0500723 SkPMColor4f scaledColor = color * innerCoverage;
724 GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700725
Brian Osmancfec9d52018-11-20 11:39:15 -0500726 // Inner rect
727 vertices.writeQuad(inset_fan(devOutside, inset, inset),
728 innerColor,
729 maybe_coverage(innerCoverage));
730
731 if (!miterStroke) {
732 // Second inner
733 vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset),
734 innerColor,
735 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700736 }
737
joshualitt11edad92015-09-22 10:32:28 -0700738 if (!degenerate) {
Brian Osmancfec9d52018-11-20 11:39:15 -0500739 vertices.writeQuad(inset_fan(devInside, -inset, -inset),
740 innerColor,
741 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700742
Brian Osmancfec9d52018-11-20 11:39:15 -0500743 // The innermost rect has 0 coverage...
744 vertices.writeQuad(inset_fan(devInside, SK_ScalarHalf, SK_ScalarHalf),
Brian Salomon747b3402019-09-16 17:25:01 -0400745 outerColor,
Brian Osmancfec9d52018-11-20 11:39:15 -0500746 maybe_coverage(0.0f));
747 } else {
748 // When the interior rect has become degenerate we smoosh to a single point
749 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
750
751 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
752 innerColor,
753 maybe_coverage(innerCoverage));
754
755 // ... unless we are degenerate, in which case we must apply the scaled coverage
756 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
757 innerColor,
758 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700759 }
760}
761
Michael Ludwig72ab3462018-12-10 12:43:36 -0500762} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700763
Michael Ludwig72ab3462018-12-10 12:43:36 -0500764namespace GrStrokeRectOp {
765
Robert Phillipsb97da532019-02-12 15:24:12 -0500766std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500767 GrPaint&& paint,
768 GrAAType aaType,
769 const SkMatrix& viewMatrix,
770 const SkRect& rect,
771 const SkStrokeRec& stroke) {
772 if (aaType == GrAAType::kCoverage) {
773 // The AA op only supports axis-aligned rectangles
774 if (!viewMatrix.rectStaysRect()) {
775 return nullptr;
776 }
777 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
778 } else {
779 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
780 }
781}
782
Robert Phillipsb97da532019-02-12 15:24:12 -0500783std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500784 GrPaint&& paint,
785 const SkMatrix& viewMatrix,
786 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400787 SkASSERT(viewMatrix.rectStaysRect());
788 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
789
790 SkRect devOutside, devInside;
791 viewMatrix.mapRect(&devOutside, rects[0]);
792 viewMatrix.mapRect(&devInside, rects[1]);
793 if (devInside.isEmpty()) {
794 if (devOutside.isEmpty()) {
795 return nullptr;
796 }
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400797 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage,
798 GrQuadAAFlags::kAll,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400799 GrQuad::MakeFromRect(rects[0], viewMatrix),
800 GrQuad(rects[0]));
Brian Salomonbaaf4392017-06-15 09:59:23 -0400801 }
802
Robert Phillips7c525e62018-06-12 10:11:12 -0400803 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700804}
805
Michael Ludwig72ab3462018-12-10 12:43:36 -0500806} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700807
Hal Canary6f6961e2017-01-31 13:50:44 -0500808#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700809
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500810#include "src/gpu/GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700811
Michael Ludwig72ab3462018-12-10 12:43:36 -0500812GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
813 SkMatrix viewMatrix = GrTest::TestMatrix(random);
814 SkRect rect = GrTest::TestRect(random);
815 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
816 SkPaint strokePaint;
817 strokePaint.setStrokeWidth(strokeWidth);
818 strokePaint.setStyle(SkPaint::kStroke_Style);
819 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
820 SkStrokeRec strokeRec(strokePaint);
821 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600822 if (numSamples > 1) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500823 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
824 }
825 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
826}
827
Brian Salomonbaaf4392017-06-15 09:59:23 -0400828GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700829 bool miterStroke = random->nextBool();
830
bsalomon40ef4852016-05-02 13:22:13 -0700831 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500832 SkRect rect =
833 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700834 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
835 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700836
bsalomon40ef4852016-05-02 13:22:13 -0700837 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
838 rec.setStrokeStyle(strokeWidth);
839 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500840 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700841 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500842 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700843}
844
845#endif