blob: 71c5aecc3bd06be0fe7c67b8c1a440f0b2df7b05 [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
Michael Ludwig72ab3462018-12-10 12:43:36 -05008#include "GrStrokeRectOp.h"
9
Robert Phillipsbe9aff22019-02-15 11:33:22 -050010#include "GrCaps.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050011#include "GrColor.h"
joshualitt9ff64252015-08-10 09:03:51 -070012#include "GrDefaultGeoProcFactory.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050013#include "GrDrawOpTest.h"
14#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050015#include "GrOpFlushState.h"
joshualitt9ff64252015-08-10 09:03:51 -070016#include "GrResourceKey.h"
17#include "GrResourceProvider.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040018#include "GrSimpleMeshDrawOpHelper.h"
Brian Osmancfec9d52018-11-20 11:39:15 -050019#include "GrVertexWriter.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050020#include "ops/GrFillRectOp.h"
21#include "SkRandom.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050022#include "SkStrokeRec.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
92 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
93 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 }
120 Helper::Flags flags = Helper::Flags::kNone;
121 // 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) {
125 flags |= Helper::Flags::kSnapVerticesToPixelCenters;
126 }
127 return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), flags,
128 viewMatrix, rect,
129 stroke, aaType);
130 }
131
132 NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
133 Helper::Flags flags, const SkMatrix& viewMatrix, const SkRect& rect,
134 const SkStrokeRec& stroke, GrAAType aaType)
135 : INHERITED(ClassID()), fHelper(helperArgs, aaType, flags) {
136 fColor = color;
137 fViewMatrix = viewMatrix;
138 fRect = rect;
139 // Sort the rect for hairlines
140 fRect.sort();
141 fStrokeWidth = stroke.getWidth();
142
143 SkScalar rad = SkScalarHalf(fStrokeWidth);
144 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
148 if (flags & Helper::Flags::kSnapVerticesToPixelCenters) {
149 viewMatrix.mapRect(&bounds);
150 // We want to be consistent with how we snap non-aa lines. To match what we do in
151 // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a
152 // pixel to force us to pixel centers.
153 bounds.set(SkScalarFloorToScalar(bounds.fLeft),
154 SkScalarFloorToScalar(bounds.fTop),
155 SkScalarFloorToScalar(bounds.fRight),
156 SkScalarFloorToScalar(bounds.fBottom));
157 bounds.offset(0.5f, 0.5f);
158 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
159 } else {
160 this->setTransformedBounds(bounds, fViewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
161 }
162 }
163
164 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
165
Chris Dalton4b62aed2019-01-15 11:53:00 -0700166 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
167 return fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kNone, &fColor);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500168 }
169
170private:
171 void onPrepareDraws(Target* target) override {
172 sk_sp<GrGeometryProcessor> gp;
173 {
174 using namespace GrDefaultGeoProcFactory;
175 Color color(fColor);
176 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
177 ? LocalCoords::kUsePosition_Type
178 : LocalCoords::kUnused_Type;
179 gp = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(), color,
180 Coverage::kSolid_Type, localCoordsType,
181 fViewMatrix);
182 }
183
184 size_t kVertexStride = gp->vertexStride();
185 int vertexCount = kVertsPerHairlineRect;
186 if (fStrokeWidth > 0) {
187 vertexCount = kVertsPerStrokeRect;
188 }
189
Brian Salomon12d22642019-01-29 14:38:50 -0500190 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500191 int firstVertex;
192
193 void* verts =
194 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
195
196 if (!verts) {
197 SkDebugf("Could not allocate vertices\n");
198 return;
199 }
200
201 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
202
203 GrPrimitiveType primType;
204 if (fStrokeWidth > 0) {
205 primType = GrPrimitiveType::kTriangleStrip;
206 init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
207 } else {
208 // hairline
209 primType = GrPrimitiveType::kLineStrip;
210 vertex[0].set(fRect.fLeft, fRect.fTop);
211 vertex[1].set(fRect.fRight, fRect.fTop);
212 vertex[2].set(fRect.fRight, fRect.fBottom);
213 vertex[3].set(fRect.fLeft, fRect.fBottom);
214 vertex[4].set(fRect.fLeft, fRect.fTop);
215 }
216
217 GrMesh* mesh = target->allocMesh(primType);
218 mesh->setNonIndexedNonInstanced(vertexCount);
Brian Salomon12d22642019-01-29 14:38:50 -0500219 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700220 target->recordDraw(std::move(gp), mesh);
221 }
222
223 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
224 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500225 }
226
227 // TODO: override onCombineIfPossible
228
229 Helper fHelper;
230 SkPMColor4f fColor;
231 SkMatrix fViewMatrix;
232 SkRect fRect;
233 SkScalar fStrokeWidth;
234
235 const static int kVertsPerHairlineRect = 5;
236 const static int kVertsPerStrokeRect = 10;
237
238 typedef GrMeshDrawOp INHERITED;
239};
240
241///////////////////////////////////////////////////////////////////////////////////////////////////
242// AA Stroking
243///////////////////////////////////////////////////////////////////////////////////////////////////
244
245GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
246GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
247
248static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
249 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
250 SkScalar strokeWidth, bool miterStroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700251 SkRect devRect;
252 viewMatrix.mapRect(&devRect, rect);
253
254 SkVector devStrokeSize;
255 if (strokeWidth > 0) {
256 devStrokeSize.set(strokeWidth, strokeWidth);
257 viewMatrix.mapVectors(&devStrokeSize, 1);
258 devStrokeSize.setAbs(devStrokeSize);
259 } else {
260 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
261 }
262
263 const SkScalar dx = devStrokeSize.fX;
264 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500265 const SkScalar rx = SkScalarHalf(dx);
266 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700267
268 *devOutside = devRect;
269 *devOutsideAssist = devRect;
270 *devInside = devRect;
271
272 devOutside->outset(rx, ry);
273 devInside->inset(rx, ry);
274
275 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
276 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
277 // together when we render these rects.
278 SkScalar spare;
279 {
280 SkScalar w = devRect.width() - dx;
281 SkScalar h = devRect.height() - dy;
282 spare = SkTMin(w, h);
283 }
284
285 *isDegenerate = spare <= 0;
286 if (*isDegenerate) {
287 devInside->fLeft = devInside->fRight = devRect.centerX();
288 devInside->fTop = devInside->fBottom = devRect.centerY();
289 }
290
291 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
292 // to draw the outside of the octagon. Because there are 8 vertices on the outer
293 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
294 if (!miterStroke) {
295 devOutside->inset(0, ry);
296 devOutsideAssist->outset(0, ry);
297 }
298}
299
Michael Ludwig72ab3462018-12-10 12:43:36 -0500300static sk_sp<GrGeometryProcessor> create_aa_stroke_rect_gp(const GrShaderCaps* shaderCaps,
301 bool tweakAlphaForCoverage,
302 const SkMatrix& viewMatrix,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500303 bool usesLocalCoords,
304 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700305 using namespace GrDefaultGeoProcFactory;
306
Brian Osman2a4c4df2018-12-20 14:06:54 -0500307 Coverage::Type coverageType =
308 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500309 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500310 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
311 Color::Type colorType =
312 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
313
314 return MakeForDeviceSpace(shaderCaps, colorType, coverageType, localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700315}
316
Brian Salomonbaaf4392017-06-15 09:59:23 -0400317class AAStrokeRectOp final : public GrMeshDrawOp {
318private:
319 using Helper = GrSimpleMeshDrawOpHelper;
320
joshualitt3566d442015-09-18 07:12:55 -0700321public:
Brian Salomon25a88092016-12-01 09:36:50 -0500322 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700323
Robert Phillipsb97da532019-02-12 15:24:12 -0500324 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400325 GrPaint&& paint,
326 const SkMatrix& viewMatrix,
327 const SkRect& devOutside,
328 const SkRect& devInside) {
329 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
330 devOutside, devInside);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400331 }
332
Brian Osmancf860852018-10-31 14:04:39 -0400333 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400334 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400335 : INHERITED(ClassID())
336 , fHelper(helperArgs, GrAAType::kCoverage)
337 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700338 SkASSERT(!devOutside.isEmpty());
339 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700340
Brian Salomon8c5bad32016-12-20 14:43:36 -0500341 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700342 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700343 fMiterStroke = true;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500344 fWideColor = !SkPMColor4fFitsInBytes(color);
bsalomon8b7a9e12016-07-06 13:06:22 -0700345 }
346
Robert Phillipsb97da532019-02-12 15:24:12 -0500347 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400348 GrPaint&& paint,
349 const SkMatrix& viewMatrix,
350 const SkRect& rect,
351 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700352 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500353 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700354 return nullptr;
355 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400356 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
357 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400358 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700359
Brian Osmancf860852018-10-31 14:04:39 -0400360 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400361 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
362 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400363 : INHERITED(ClassID())
364 , fHelper(helperArgs, GrAAType::kCoverage)
365 , fViewMatrix(viewMatrix) {
366 fMiterStroke = isMiter;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500367 fWideColor = !SkPMColor4fFitsInBytes(color);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400368 RectInfo& info = fRects.push_back();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500369 compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
370 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500371 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400372 if (isMiter) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400373 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400374 } else {
375 // The outer polygon of the bevel stroke is an octagon specified by the points of a
376 // pair of overlapping rectangles where one is wide and the other is narrow.
377 SkRect bounds = info.fDevOutside;
378 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400379 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400380 }
joshualitt3566d442015-09-18 07:12:55 -0700381 }
382
383 const char* name() const override { return "AAStrokeRect"; }
384
Brian Salomon7d94bb52018-10-12 14:37:19 -0400385 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400386 fHelper.visitProxies(func);
387 }
388
Brian Osman9a390ac2018-11-12 09:47:48 -0500389#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500390 SkString dumpInfo() const override {
391 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500392 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500393 string.appendf(
394 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
395 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
396 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Osmancf860852018-10-31 14:04:39 -0400397 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500398 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
399 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
400 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
401 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500402 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400403 string += fHelper.dumpInfo();
404 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500405 return string;
406 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500407#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500408
Brian Salomonbaaf4392017-06-15 09:59:23 -0400409 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400410
Chris Dalton4b62aed2019-01-15 11:53:00 -0700411 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
412 return fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
413 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400414 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400415
416private:
Brian Salomon91326c32017-08-09 16:02:19 -0400417 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700418 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
joshualittaa37a962015-09-18 13:03:25 -0700419
joshualitt3566d442015-09-18 07:12:55 -0700420 static const int kMiterIndexCnt = 3 * 24;
421 static const int kMiterVertexCnt = 16;
422 static const int kNumMiterRectsInIndexBuffer = 256;
423
424 static const int kBevelIndexCnt = 48 + 36 + 24;
425 static const int kBevelVertexCnt = 24;
426 static const int kNumBevelRectsInIndexBuffer = 256;
427
Brian Salomondbf70722019-02-07 11:31:24 -0500428 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700429
joshualittaa37a962015-09-18 13:03:25 -0700430 const SkMatrix& viewMatrix() const { return fViewMatrix; }
431 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700432
Brian Salomon7eae3e02018-08-07 14:02:38 +0000433 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700434
Brian Osmancfec9d52018-11-20 11:39:15 -0500435 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500436 const SkPMColor4f& color,
437 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700438 const SkRect& devOutside,
439 const SkRect& devOutsideAssist,
440 const SkRect& devInside,
441 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700442 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700443 bool tweakAlphaForCoverage) const;
444
bsalomon8b7a9e12016-07-06 13:06:22 -0700445 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500446 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400447 SkPMColor4f fColor;
bsalomon8b7a9e12016-07-06 13:06:22 -0700448 SkRect fDevOutside;
449 SkRect fDevOutsideAssist;
450 SkRect fDevInside;
451 bool fDegenerate;
452 };
453
Brian Salomonbaaf4392017-06-15 09:59:23 -0400454 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500455 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700456 SkMatrix fViewMatrix;
457 bool fMiterStroke;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500458 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700459
Brian Salomonbaaf4392017-06-15 09:59:23 -0400460 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700461};
462
Brian Salomon91326c32017-08-09 16:02:19 -0400463void AAStrokeRectOp::onPrepareDraws(Target* target) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500464 sk_sp<GrGeometryProcessor> gp(create_aa_stroke_rect_gp(target->caps().shaderCaps(),
465 fHelper.compatibleWithAlphaAsCoverage(),
466 this->viewMatrix(),
Brian Osman2a4c4df2018-12-20 14:06:54 -0500467 fHelper.usesLocalCoords(),
468 fWideColor));
joshualitt9ff64252015-08-10 09:03:51 -0700469 if (!gp) {
470 SkDebugf("Couldn't create GrGeometryProcessor\n");
471 return;
472 }
473
joshualitt9ff64252015-08-10 09:03:51 -0700474 int innerVertexNum = 4;
475 int outerVertexNum = this->miterStroke() ? 4 : 8;
476 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
477 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500478 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700479
Brian Salomondbf70722019-02-07 11:31:24 -0500480 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000481 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500482 if (!indexBuffer) {
483 SkDebugf("Could not allocate indices\n");
484 return;
485 }
486 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
487 std::move(indexBuffer), verticesPerInstance, indicesPerInstance,
488 instanceCount);
Brian Osmancfec9d52018-11-20 11:39:15 -0500489 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500490 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500491 SkDebugf("Could not allocate vertices\n");
492 return;
493 }
joshualitt9ff64252015-08-10 09:03:51 -0700494
495 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500496 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700497 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500498 info.fColor,
499 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500500 info.fDevOutside,
501 info.fDevOutsideAssist,
502 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700503 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500504 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400505 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700506 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700507 helper.recordDraw(target, std::move(gp));
508}
509
510void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
511 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt9ff64252015-08-10 09:03:51 -0700512}
513
Brian Salomondbf70722019-02-07 11:31:24 -0500514sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
515 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700516 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500517 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700518 static const uint16_t gMiterIndices[] = {
519 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
520 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
521 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
522 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
523
524 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
525 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
526 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
527 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
528
529 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
530 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
531 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
532 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
533 };
Brian Salomon6a639042016-12-14 11:08:17 -0500534 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700535 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
536 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400537 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500538 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
539 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700540 } else {
541 /**
542 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
543 * from the first index. The index layout:
544 * outer AA line: 0~3, 4~7
545 * outer edge: 8~11, 12~15
546 * inner edge: 16~19
547 * inner AA line: 20~23
548 * Following comes a bevel-stroke rect and its indices:
549 *
550 * 4 7
551 * *********************************
552 * * ______________________________ *
553 * * / 12 15 \ *
554 * * / \ *
555 * 0 * |8 16_____________________19 11 | * 3
556 * * | | | | *
557 * * | | **************** | | *
558 * * | | * 20 23 * | | *
559 * * | | * * | | *
560 * * | | * 21 22 * | | *
561 * * | | **************** | | *
562 * * | |____________________| | *
563 * 1 * |9 17 18 10| * 2
564 * * \ / *
565 * * \13 __________________________14/ *
566 * * *
567 * **********************************
568 * 5 6
569 */
Brian Salomon6a639042016-12-14 11:08:17 -0500570 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700571 static const uint16_t gBevelIndices[] = {
572 // Draw outer AA, from outer AA line to outer edge, shift is 0.
573 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
574 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
575 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
576 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
577 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
578 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
579 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
580 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
581
582 // Draw the stroke, from outer edge to inner edge, shift is 8.
583 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
584 1 + 8, 5 + 8, 9 + 8,
585 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
586 6 + 8, 2 + 8, 10 + 8,
587 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
588 3 + 8, 7 + 8, 11 + 8,
589 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
590 4 + 8, 0 + 8, 8 + 8,
591
592 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
593 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
594 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
595 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
596 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
597 };
Brian Salomon6a639042016-12-14 11:08:17 -0500598 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700599 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
600
601 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400602 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500603 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
604 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700605 }
606}
607
Brian Salomon7eae3e02018-08-07 14:02:38 +0000608GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500609 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700610
Brian Salomonbaaf4392017-06-15 09:59:23 -0400611 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000612 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700613 }
614
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500615 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700616 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000617 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700618 }
619
620 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400621 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
622 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000623 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700624 }
625
Brian Salomon8c5bad32016-12-20 14:43:36 -0500626 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500627 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000628 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700629}
630
joshualitt11edad92015-09-22 10:32:28 -0700631static void setup_scale(int* scale, SkScalar inset) {
632 if (inset < SK_ScalarHalf) {
633 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
634 SkASSERT(*scale >= 0 && *scale <= 255);
635 } else {
636 *scale = 0xff;
637 }
638}
639
Brian Osmancfec9d52018-11-20 11:39:15 -0500640void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500641 const SkPMColor4f& color,
642 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500643 const SkRect& devOutside,
644 const SkRect& devOutsideAssist,
645 const SkRect& devInside,
646 bool miterStroke,
647 bool degenerate,
648 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700649 // We create vertices for four nested rectangles. There are two ramps from 0 to full
650 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700651
joshualitt9ff64252015-08-10 09:03:51 -0700652 // TODO: this only really works if the X & Y margins are the same all around
653 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700654 SkScalar inset;
655 if (!degenerate) {
656 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
657 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
658 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
659 if (miterStroke) {
660 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
661 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500662 inset = SK_ScalarHalf *
663 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700664 }
665 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700666 } else {
joshualitt11edad92015-09-22 10:32:28 -0700667 // TODO use real devRect here
668 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500669 inset = SK_ScalarHalf *
670 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700671 }
joshualitt9ff64252015-08-10 09:03:51 -0700672
Brian Osmancfec9d52018-11-20 11:39:15 -0500673 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
674 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
675 };
joshualitt9ff64252015-08-10 09:03:51 -0700676
Brian Osmancfec9d52018-11-20 11:39:15 -0500677 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
678 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
679 };
680
Brian Osman2a4c4df2018-12-20 14:06:54 -0500681 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500682
683 // Outermost rect
684 vertices.writeQuad(inset_fan(devOutside, -SK_ScalarHalf, -SK_ScalarHalf),
685 outerColor,
686 maybe_coverage(0.0f));
687
688 if (!miterStroke) {
689 // Second outermost
690 vertices.writeQuad(inset_fan(devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf),
691 outerColor,
692 maybe_coverage(0.0f));
joshualitt9ff64252015-08-10 09:03:51 -0700693 }
694
695 // scale is the coverage for the the inner two rects.
696 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700697 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700698
699 float innerCoverage = GrNormalizeByteToFloat(scale);
Brian Osman2a4c4df2018-12-20 14:06:54 -0500700 SkPMColor4f scaledColor = color * innerCoverage;
701 GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700702
Brian Osmancfec9d52018-11-20 11:39:15 -0500703 // Inner rect
704 vertices.writeQuad(inset_fan(devOutside, inset, inset),
705 innerColor,
706 maybe_coverage(innerCoverage));
707
708 if (!miterStroke) {
709 // Second inner
710 vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset),
711 innerColor,
712 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700713 }
714
joshualitt11edad92015-09-22 10:32:28 -0700715 if (!degenerate) {
Brian Osmancfec9d52018-11-20 11:39:15 -0500716 vertices.writeQuad(inset_fan(devInside, -inset, -inset),
717 innerColor,
718 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700719
Brian Osmancfec9d52018-11-20 11:39:15 -0500720 // The innermost rect has 0 coverage...
721 vertices.writeQuad(inset_fan(devInside, SK_ScalarHalf, SK_ScalarHalf),
Brian Osman2a4c4df2018-12-20 14:06:54 -0500722 GrVertexColor(SK_PMColor4fTRANSPARENT, wideColor),
Brian Osmancfec9d52018-11-20 11:39:15 -0500723 maybe_coverage(0.0f));
724 } else {
725 // When the interior rect has become degenerate we smoosh to a single point
726 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
727
728 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
729 innerColor,
730 maybe_coverage(innerCoverage));
731
732 // ... unless we are degenerate, in which case we must apply the scaled coverage
733 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
734 innerColor,
735 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700736 }
737}
738
Michael Ludwig72ab3462018-12-10 12:43:36 -0500739} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700740
Michael Ludwig72ab3462018-12-10 12:43:36 -0500741namespace GrStrokeRectOp {
742
Robert Phillipsb97da532019-02-12 15:24:12 -0500743std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500744 GrPaint&& paint,
745 GrAAType aaType,
746 const SkMatrix& viewMatrix,
747 const SkRect& rect,
748 const SkStrokeRec& stroke) {
749 if (aaType == GrAAType::kCoverage) {
750 // The AA op only supports axis-aligned rectangles
751 if (!viewMatrix.rectStaysRect()) {
752 return nullptr;
753 }
754 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
755 } else {
756 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
757 }
758}
759
Robert Phillipsb97da532019-02-12 15:24:12 -0500760std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500761 GrPaint&& paint,
762 const SkMatrix& viewMatrix,
763 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400764 SkASSERT(viewMatrix.rectStaysRect());
765 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
766
767 SkRect devOutside, devInside;
768 viewMatrix.mapRect(&devOutside, rects[0]);
769 viewMatrix.mapRect(&devInside, rects[1]);
770 if (devInside.isEmpty()) {
771 if (devOutside.isEmpty()) {
772 return nullptr;
773 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500774 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, viewMatrix,
775 rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400776 }
777
Robert Phillips7c525e62018-06-12 10:11:12 -0400778 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700779}
780
Michael Ludwig72ab3462018-12-10 12:43:36 -0500781} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700782
Hal Canary6f6961e2017-01-31 13:50:44 -0500783#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700784
Brian Salomon5ec9def2016-12-20 15:34:05 -0500785#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700786
Michael Ludwig72ab3462018-12-10 12:43:36 -0500787GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
788 SkMatrix viewMatrix = GrTest::TestMatrix(random);
789 SkRect rect = GrTest::TestRect(random);
790 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
791 SkPaint strokePaint;
792 strokePaint.setStrokeWidth(strokeWidth);
793 strokePaint.setStyle(SkPaint::kStroke_Style);
794 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
795 SkStrokeRec strokeRec(strokePaint);
796 GrAAType aaType = GrAAType::kNone;
797 if (fsaaType == GrFSAAType::kUnifiedMSAA) {
798 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
799 }
800 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
801}
802
Brian Salomonbaaf4392017-06-15 09:59:23 -0400803GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700804 bool miterStroke = random->nextBool();
805
bsalomon40ef4852016-05-02 13:22:13 -0700806 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500807 SkRect rect =
808 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700809 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
810 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700811
bsalomon40ef4852016-05-02 13:22:13 -0700812 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
813 rec.setStrokeStyle(strokeWidth);
814 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500815 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700816 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500817 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700818}
819
820#endif