blob: 257719bd1a58daf307d77ee7759e6e6999137664 [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
10#include "GrColor.h"
joshualitt9ff64252015-08-10 09:03:51 -070011#include "GrDefaultGeoProcFactory.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050012#include "GrDrawOpTest.h"
13#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
joshualitt9ff64252015-08-10 09:03:51 -070015#include "GrResourceKey.h"
16#include "GrResourceProvider.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040017#include "GrSimpleMeshDrawOpHelper.h"
Brian Osmancfec9d52018-11-20 11:39:15 -050018#include "GrVertexWriter.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050019#include "ops/GrFillRectOp.h"
20#include "SkRandom.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050021#include "SkStrokeRec.h"
joshualitt9ff64252015-08-10 09:03:51 -070022
Michael Ludwig72ab3462018-12-10 12:43:36 -050023namespace {
joshualitt9ff64252015-08-10 09:03:51 -070024
bsalomon8b7a9e12016-07-06 13:06:22 -070025// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
Michael Ludwig72ab3462018-12-10 12:43:36 -050026// limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only
27// supported when using an AA stroke.
28inline static bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) {
bsalomon8b7a9e12016-07-06 13:06:22 -070029 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
30 stroke.getStyle() == SkStrokeRec::kHairline_Style);
31 // For hairlines, make bevel and round joins appear the same as mitered ones.
32 if (!stroke.getWidth()) {
33 *isMiter = true;
34 return true;
35 }
36 if (stroke.getJoin() == SkPaint::kBevel_Join) {
37 *isMiter = false;
Michael Ludwig72ab3462018-12-10 12:43:36 -050038 return aa == GrAA::kYes; // bevel only supported with AA
bsalomon8b7a9e12016-07-06 13:06:22 -070039 }
40 if (stroke.getJoin() == SkPaint::kMiter_Join) {
41 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
Michael Ludwig72ab3462018-12-10 12:43:36 -050042 // Supported under non-AA only if it remains mitered
43 return aa == GrAA::kYes || *isMiter;
bsalomon8b7a9e12016-07-06 13:06:22 -070044 }
45 return false;
46}
47
Michael Ludwig72ab3462018-12-10 12:43:36 -050048
49///////////////////////////////////////////////////////////////////////////////////////////////////
50// Non-AA Stroking
51///////////////////////////////////////////////////////////////////////////////////////////////////
52
53/* create a triangle strip that strokes the specified rect. There are 8
54 unique vertices, but we repeat the last 2 to close up. Alternatively we
55 could use an indices array, and then only send 8 verts, but not sure that
56 would be faster.
57 */
58static void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
59 const SkScalar rad = SkScalarHalf(width);
60
61 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
62 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
63 verts[2].set(rect.fRight - rad, rect.fTop + rad);
64 verts[3].set(rect.fRight + rad, rect.fTop - rad);
65 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
66 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
67 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
68 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
69 verts[8] = verts[0];
70 verts[9] = verts[1];
71
72 // TODO: we should be catching this higher up the call stack and just draw a single
73 // non-AA rect
74 if (2*rad >= rect.width()) {
75 verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX();
76 }
77 if (2*rad >= rect.height()) {
78 verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY();
79 }
80}
81
82class NonAAStrokeRectOp final : public GrMeshDrawOp {
83private:
84 using Helper = GrSimpleMeshDrawOpHelper;
85
86public:
87 DEFINE_OP_CLASS_ID
88
89 const char* name() const override { return "NonAAStrokeRectOp"; }
90
91 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
92 fHelper.visitProxies(func);
93 }
94
95#ifdef SK_DEBUG
96 SkString dumpInfo() const override {
97 SkString string;
98 string.appendf(
99 "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
100 "StrokeWidth: %.2f\n",
101 fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom,
102 fStrokeWidth);
103 string += fHelper.dumpInfo();
104 string += INHERITED::dumpInfo();
105 return string;
106 }
107#endif
108
Robert Phillipsb97da532019-02-12 15:24:12 -0500109 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500110 GrPaint&& paint,
111 const SkMatrix& viewMatrix,
112 const SkRect& rect,
113 const SkStrokeRec& stroke,
114 GrAAType aaType) {
115 bool isMiter;
116 if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) {
117 return nullptr;
118 }
119 Helper::Flags flags = Helper::Flags::kNone;
120 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
121 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
122 // when MSAA is enabled because it can cause ugly artifacts.
123 if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) {
124 flags |= Helper::Flags::kSnapVerticesToPixelCenters;
125 }
126 return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), flags,
127 viewMatrix, rect,
128 stroke, aaType);
129 }
130
131 NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
132 Helper::Flags flags, const SkMatrix& viewMatrix, const SkRect& rect,
133 const SkStrokeRec& stroke, GrAAType aaType)
134 : INHERITED(ClassID()), fHelper(helperArgs, aaType, flags) {
135 fColor = color;
136 fViewMatrix = viewMatrix;
137 fRect = rect;
138 // Sort the rect for hairlines
139 fRect.sort();
140 fStrokeWidth = stroke.getWidth();
141
142 SkScalar rad = SkScalarHalf(fStrokeWidth);
143 SkRect bounds = rect;
144 bounds.outset(rad, rad);
145
146 // If our caller snaps to pixel centers then we have to round out the bounds
147 if (flags & Helper::Flags::kSnapVerticesToPixelCenters) {
148 viewMatrix.mapRect(&bounds);
149 // We want to be consistent with how we snap non-aa lines. To match what we do in
150 // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a
151 // pixel to force us to pixel centers.
152 bounds.set(SkScalarFloorToScalar(bounds.fLeft),
153 SkScalarFloorToScalar(bounds.fTop),
154 SkScalarFloorToScalar(bounds.fRight),
155 SkScalarFloorToScalar(bounds.fBottom));
156 bounds.offset(0.5f, 0.5f);
157 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
158 } else {
159 this->setTransformedBounds(bounds, fViewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
160 }
161 }
162
163 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
164
Chris Dalton4b62aed2019-01-15 11:53:00 -0700165 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
166 return fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kNone, &fColor);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500167 }
168
169private:
170 void onPrepareDraws(Target* target) override {
171 sk_sp<GrGeometryProcessor> gp;
172 {
173 using namespace GrDefaultGeoProcFactory;
174 Color color(fColor);
175 LocalCoords::Type localCoordsType = fHelper.usesLocalCoords()
176 ? LocalCoords::kUsePosition_Type
177 : LocalCoords::kUnused_Type;
178 gp = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(), color,
179 Coverage::kSolid_Type, localCoordsType,
180 fViewMatrix);
181 }
182
183 size_t kVertexStride = gp->vertexStride();
184 int vertexCount = kVertsPerHairlineRect;
185 if (fStrokeWidth > 0) {
186 vertexCount = kVertsPerStrokeRect;
187 }
188
Brian Salomon12d22642019-01-29 14:38:50 -0500189 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500190 int firstVertex;
191
192 void* verts =
193 target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex);
194
195 if (!verts) {
196 SkDebugf("Could not allocate vertices\n");
197 return;
198 }
199
200 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
201
202 GrPrimitiveType primType;
203 if (fStrokeWidth > 0) {
204 primType = GrPrimitiveType::kTriangleStrip;
205 init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth);
206 } else {
207 // hairline
208 primType = GrPrimitiveType::kLineStrip;
209 vertex[0].set(fRect.fLeft, fRect.fTop);
210 vertex[1].set(fRect.fRight, fRect.fTop);
211 vertex[2].set(fRect.fRight, fRect.fBottom);
212 vertex[3].set(fRect.fLeft, fRect.fBottom);
213 vertex[4].set(fRect.fLeft, fRect.fTop);
214 }
215
216 GrMesh* mesh = target->allocMesh(primType);
217 mesh->setNonIndexedNonInstanced(vertexCount);
Brian Salomon12d22642019-01-29 14:38:50 -0500218 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500219 auto pipe = fHelper.makePipeline(target);
220 target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh);
221 }
222
223 // TODO: override onCombineIfPossible
224
225 Helper fHelper;
226 SkPMColor4f fColor;
227 SkMatrix fViewMatrix;
228 SkRect fRect;
229 SkScalar fStrokeWidth;
230
231 const static int kVertsPerHairlineRect = 5;
232 const static int kVertsPerStrokeRect = 10;
233
234 typedef GrMeshDrawOp INHERITED;
235};
236
237///////////////////////////////////////////////////////////////////////////////////////////////////
238// AA Stroking
239///////////////////////////////////////////////////////////////////////////////////////////////////
240
241GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
242GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
243
244static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
245 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
246 SkScalar strokeWidth, bool miterStroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700247 SkRect devRect;
248 viewMatrix.mapRect(&devRect, rect);
249
250 SkVector devStrokeSize;
251 if (strokeWidth > 0) {
252 devStrokeSize.set(strokeWidth, strokeWidth);
253 viewMatrix.mapVectors(&devStrokeSize, 1);
254 devStrokeSize.setAbs(devStrokeSize);
255 } else {
256 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
257 }
258
259 const SkScalar dx = devStrokeSize.fX;
260 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -0500261 const SkScalar rx = SkScalarHalf(dx);
262 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -0700263
264 *devOutside = devRect;
265 *devOutsideAssist = devRect;
266 *devInside = devRect;
267
268 devOutside->outset(rx, ry);
269 devInside->inset(rx, ry);
270
271 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
272 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
273 // together when we render these rects.
274 SkScalar spare;
275 {
276 SkScalar w = devRect.width() - dx;
277 SkScalar h = devRect.height() - dy;
278 spare = SkTMin(w, h);
279 }
280
281 *isDegenerate = spare <= 0;
282 if (*isDegenerate) {
283 devInside->fLeft = devInside->fRight = devRect.centerX();
284 devInside->fTop = devInside->fBottom = devRect.centerY();
285 }
286
287 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
288 // to draw the outside of the octagon. Because there are 8 vertices on the outer
289 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
290 if (!miterStroke) {
291 devOutside->inset(0, ry);
292 devOutsideAssist->outset(0, ry);
293 }
294}
295
Michael Ludwig72ab3462018-12-10 12:43:36 -0500296static sk_sp<GrGeometryProcessor> create_aa_stroke_rect_gp(const GrShaderCaps* shaderCaps,
297 bool tweakAlphaForCoverage,
298 const SkMatrix& viewMatrix,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500299 bool usesLocalCoords,
300 bool wideColor) {
joshualitt9ff64252015-08-10 09:03:51 -0700301 using namespace GrDefaultGeoProcFactory;
302
Brian Osman2a4c4df2018-12-20 14:06:54 -0500303 Coverage::Type coverageType =
304 tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500305 LocalCoords::Type localCoordsType =
Brian Osman2a4c4df2018-12-20 14:06:54 -0500306 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
307 Color::Type colorType =
308 wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type;
309
310 return MakeForDeviceSpace(shaderCaps, colorType, coverageType, localCoordsType, viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700311}
312
Brian Salomonbaaf4392017-06-15 09:59:23 -0400313class AAStrokeRectOp final : public GrMeshDrawOp {
314private:
315 using Helper = GrSimpleMeshDrawOpHelper;
316
joshualitt3566d442015-09-18 07:12:55 -0700317public:
Brian Salomon25a88092016-12-01 09:36:50 -0500318 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700319
Robert Phillipsb97da532019-02-12 15:24:12 -0500320 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400321 GrPaint&& paint,
322 const SkMatrix& viewMatrix,
323 const SkRect& devOutside,
324 const SkRect& devInside) {
325 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
326 devOutside, devInside);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400327 }
328
Brian Osmancf860852018-10-31 14:04:39 -0400329 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400330 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400331 : INHERITED(ClassID())
332 , fHelper(helperArgs, GrAAType::kCoverage)
333 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700334 SkASSERT(!devOutside.isEmpty());
335 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700336
Brian Salomon8c5bad32016-12-20 14:43:36 -0500337 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700338 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700339 fMiterStroke = true;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500340 fWideColor = !SkPMColor4fFitsInBytes(color);
bsalomon8b7a9e12016-07-06 13:06:22 -0700341 }
342
Robert Phillipsb97da532019-02-12 15:24:12 -0500343 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400344 GrPaint&& paint,
345 const SkMatrix& viewMatrix,
346 const SkRect& rect,
347 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700348 bool isMiter;
Michael Ludwig72ab3462018-12-10 12:43:36 -0500349 if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700350 return nullptr;
351 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400352 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
353 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400354 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700355
Brian Osmancf860852018-10-31 14:04:39 -0400356 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400357 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
358 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400359 : INHERITED(ClassID())
360 , fHelper(helperArgs, GrAAType::kCoverage)
361 , fViewMatrix(viewMatrix) {
362 fMiterStroke = isMiter;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500363 fWideColor = !SkPMColor4fFitsInBytes(color);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400364 RectInfo& info = fRects.push_back();
Michael Ludwig72ab3462018-12-10 12:43:36 -0500365 compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
366 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
Brian Salomon8c5bad32016-12-20 14:43:36 -0500367 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400368 if (isMiter) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400369 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400370 } else {
371 // The outer polygon of the bevel stroke is an octagon specified by the points of a
372 // pair of overlapping rectangles where one is wide and the other is narrow.
373 SkRect bounds = info.fDevOutside;
374 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400375 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400376 }
joshualitt3566d442015-09-18 07:12:55 -0700377 }
378
379 const char* name() const override { return "AAStrokeRect"; }
380
Brian Salomon7d94bb52018-10-12 14:37:19 -0400381 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400382 fHelper.visitProxies(func);
383 }
384
Brian Osman9a390ac2018-11-12 09:47:48 -0500385#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500386 SkString dumpInfo() const override {
387 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500388 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500389 string.appendf(
390 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
391 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
392 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Osmancf860852018-10-31 14:04:39 -0400393 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500394 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
395 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
396 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
397 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500398 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400399 string += fHelper.dumpInfo();
400 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500401 return string;
402 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500403#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500404
Brian Salomonbaaf4392017-06-15 09:59:23 -0400405 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400406
Chris Dalton4b62aed2019-01-15 11:53:00 -0700407 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
408 return fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
409 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400410 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400411
412private:
Brian Salomon91326c32017-08-09 16:02:19 -0400413 void onPrepareDraws(Target*) override;
joshualittaa37a962015-09-18 13:03:25 -0700414
joshualitt3566d442015-09-18 07:12:55 -0700415 static const int kMiterIndexCnt = 3 * 24;
416 static const int kMiterVertexCnt = 16;
417 static const int kNumMiterRectsInIndexBuffer = 256;
418
419 static const int kBevelIndexCnt = 48 + 36 + 24;
420 static const int kBevelVertexCnt = 24;
421 static const int kNumBevelRectsInIndexBuffer = 256;
422
Brian Salomondbf70722019-02-07 11:31:24 -0500423 static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700424
joshualittaa37a962015-09-18 13:03:25 -0700425 const SkMatrix& viewMatrix() const { return fViewMatrix; }
426 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700427
Brian Salomon7eae3e02018-08-07 14:02:38 +0000428 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700429
Brian Osmancfec9d52018-11-20 11:39:15 -0500430 void generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500431 const SkPMColor4f& color,
432 bool wideColor,
joshualitt3566d442015-09-18 07:12:55 -0700433 const SkRect& devOutside,
434 const SkRect& devOutsideAssist,
435 const SkRect& devInside,
436 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700437 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700438 bool tweakAlphaForCoverage) const;
439
bsalomon8b7a9e12016-07-06 13:06:22 -0700440 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500441 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400442 SkPMColor4f fColor;
bsalomon8b7a9e12016-07-06 13:06:22 -0700443 SkRect fDevOutside;
444 SkRect fDevOutsideAssist;
445 SkRect fDevInside;
446 bool fDegenerate;
447 };
448
Brian Salomonbaaf4392017-06-15 09:59:23 -0400449 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500450 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700451 SkMatrix fViewMatrix;
452 bool fMiterStroke;
Brian Osman2a4c4df2018-12-20 14:06:54 -0500453 bool fWideColor;
joshualitt3566d442015-09-18 07:12:55 -0700454
Brian Salomonbaaf4392017-06-15 09:59:23 -0400455 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700456};
457
Brian Salomon91326c32017-08-09 16:02:19 -0400458void AAStrokeRectOp::onPrepareDraws(Target* target) {
Michael Ludwig72ab3462018-12-10 12:43:36 -0500459 sk_sp<GrGeometryProcessor> gp(create_aa_stroke_rect_gp(target->caps().shaderCaps(),
460 fHelper.compatibleWithAlphaAsCoverage(),
461 this->viewMatrix(),
Brian Osman2a4c4df2018-12-20 14:06:54 -0500462 fHelper.usesLocalCoords(),
463 fWideColor));
joshualitt9ff64252015-08-10 09:03:51 -0700464 if (!gp) {
465 SkDebugf("Couldn't create GrGeometryProcessor\n");
466 return;
467 }
468
joshualitt9ff64252015-08-10 09:03:51 -0700469 int innerVertexNum = 4;
470 int outerVertexNum = this->miterStroke() ? 4 : 8;
471 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
472 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500473 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700474
Brian Salomondbf70722019-02-07 11:31:24 -0500475 sk_sp<const GrGpuBuffer> indexBuffer =
Brian Salomon7eae3e02018-08-07 14:02:38 +0000476 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Brian Salomon12d22642019-01-29 14:38:50 -0500477 if (!indexBuffer) {
478 SkDebugf("Could not allocate indices\n");
479 return;
480 }
481 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
482 std::move(indexBuffer), verticesPerInstance, indicesPerInstance,
483 instanceCount);
Brian Osmancfec9d52018-11-20 11:39:15 -0500484 GrVertexWriter vertices{ helper.vertices() };
Brian Salomon12d22642019-01-29 14:38:50 -0500485 if (!vertices.fPtr) {
Brian Salomon6a639042016-12-14 11:08:17 -0500486 SkDebugf("Could not allocate vertices\n");
487 return;
488 }
joshualitt9ff64252015-08-10 09:03:51 -0700489
490 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500491 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700492 this->generateAAStrokeRectGeometry(vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500493 info.fColor,
494 fWideColor,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500495 info.fDevOutside,
496 info.fDevOutsideAssist,
497 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700498 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500499 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400500 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700501 }
Brian Salomon49348902018-06-26 09:12:38 -0400502 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000503 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt9ff64252015-08-10 09:03:51 -0700504}
505
Brian Salomondbf70722019-02-07 11:31:24 -0500506sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
507 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700508 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500509 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700510 static const uint16_t gMiterIndices[] = {
511 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
512 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
513 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
514 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
515
516 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
517 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
518 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
519 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
520
521 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
522 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
523 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
524 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
525 };
Brian Salomon6a639042016-12-14 11:08:17 -0500526 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700527 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
528 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400529 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500530 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
531 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700532 } else {
533 /**
534 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
535 * from the first index. The index layout:
536 * outer AA line: 0~3, 4~7
537 * outer edge: 8~11, 12~15
538 * inner edge: 16~19
539 * inner AA line: 20~23
540 * Following comes a bevel-stroke rect and its indices:
541 *
542 * 4 7
543 * *********************************
544 * * ______________________________ *
545 * * / 12 15 \ *
546 * * / \ *
547 * 0 * |8 16_____________________19 11 | * 3
548 * * | | | | *
549 * * | | **************** | | *
550 * * | | * 20 23 * | | *
551 * * | | * * | | *
552 * * | | * 21 22 * | | *
553 * * | | **************** | | *
554 * * | |____________________| | *
555 * 1 * |9 17 18 10| * 2
556 * * \ / *
557 * * \13 __________________________14/ *
558 * * *
559 * **********************************
560 * 5 6
561 */
Brian Salomon6a639042016-12-14 11:08:17 -0500562 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700563 static const uint16_t gBevelIndices[] = {
564 // Draw outer AA, from outer AA line to outer edge, shift is 0.
565 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
566 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
567 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
568 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
569 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
570 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
571 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
572 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
573
574 // Draw the stroke, from outer edge to inner edge, shift is 8.
575 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
576 1 + 8, 5 + 8, 9 + 8,
577 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
578 6 + 8, 2 + 8, 10 + 8,
579 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
580 3 + 8, 7 + 8, 11 + 8,
581 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
582 4 + 8, 0 + 8, 8 + 8,
583
584 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
585 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
586 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
587 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
588 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
589 };
Brian Salomon6a639042016-12-14 11:08:17 -0500590 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700591 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
592
593 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400594 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500595 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
596 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700597 }
598}
599
Brian Salomon7eae3e02018-08-07 14:02:38 +0000600GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500601 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700602
Brian Salomonbaaf4392017-06-15 09:59:23 -0400603 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000604 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700605 }
606
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500607 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700608 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000609 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700610 }
611
612 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400613 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
614 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000615 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700616 }
617
Brian Salomon8c5bad32016-12-20 14:43:36 -0500618 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Osman2a4c4df2018-12-20 14:06:54 -0500619 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000620 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700621}
622
joshualitt11edad92015-09-22 10:32:28 -0700623static void setup_scale(int* scale, SkScalar inset) {
624 if (inset < SK_ScalarHalf) {
625 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
626 SkASSERT(*scale >= 0 && *scale <= 255);
627 } else {
628 *scale = 0xff;
629 }
630}
631
Brian Osmancfec9d52018-11-20 11:39:15 -0500632void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices,
Brian Osman2a4c4df2018-12-20 14:06:54 -0500633 const SkPMColor4f& color,
634 bool wideColor,
Brian Salomon6a639042016-12-14 11:08:17 -0500635 const SkRect& devOutside,
636 const SkRect& devOutsideAssist,
637 const SkRect& devInside,
638 bool miterStroke,
639 bool degenerate,
640 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700641 // We create vertices for four nested rectangles. There are two ramps from 0 to full
642 // coverage, one on the exterior of the stroke and the other on the interior.
joshualitt9ff64252015-08-10 09:03:51 -0700643
joshualitt9ff64252015-08-10 09:03:51 -0700644 // TODO: this only really works if the X & Y margins are the same all around
645 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700646 SkScalar inset;
647 if (!degenerate) {
648 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
649 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
650 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
651 if (miterStroke) {
652 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
653 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500654 inset = SK_ScalarHalf *
655 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700656 }
657 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700658 } else {
joshualitt11edad92015-09-22 10:32:28 -0700659 // TODO use real devRect here
660 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500661 inset = SK_ScalarHalf *
662 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700663 }
joshualitt9ff64252015-08-10 09:03:51 -0700664
Brian Osmancfec9d52018-11-20 11:39:15 -0500665 auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) {
666 return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy));
667 };
joshualitt9ff64252015-08-10 09:03:51 -0700668
Brian Osmancfec9d52018-11-20 11:39:15 -0500669 auto maybe_coverage = [tweakAlphaForCoverage](float coverage) {
670 return GrVertexWriter::If(!tweakAlphaForCoverage, coverage);
671 };
672
Brian Osman2a4c4df2018-12-20 14:06:54 -0500673 GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor);
Brian Osmancfec9d52018-11-20 11:39:15 -0500674
675 // Outermost rect
676 vertices.writeQuad(inset_fan(devOutside, -SK_ScalarHalf, -SK_ScalarHalf),
677 outerColor,
678 maybe_coverage(0.0f));
679
680 if (!miterStroke) {
681 // Second outermost
682 vertices.writeQuad(inset_fan(devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf),
683 outerColor,
684 maybe_coverage(0.0f));
joshualitt9ff64252015-08-10 09:03:51 -0700685 }
686
687 // scale is the coverage for the the inner two rects.
688 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700689 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700690
691 float innerCoverage = GrNormalizeByteToFloat(scale);
Brian Osman2a4c4df2018-12-20 14:06:54 -0500692 SkPMColor4f scaledColor = color * innerCoverage;
693 GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor);
joshualitt9ff64252015-08-10 09:03:51 -0700694
Brian Osmancfec9d52018-11-20 11:39:15 -0500695 // Inner rect
696 vertices.writeQuad(inset_fan(devOutside, inset, inset),
697 innerColor,
698 maybe_coverage(innerCoverage));
699
700 if (!miterStroke) {
701 // Second inner
702 vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset),
703 innerColor,
704 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700705 }
706
joshualitt11edad92015-09-22 10:32:28 -0700707 if (!degenerate) {
Brian Osmancfec9d52018-11-20 11:39:15 -0500708 vertices.writeQuad(inset_fan(devInside, -inset, -inset),
709 innerColor,
710 maybe_coverage(innerCoverage));
joshualitt11edad92015-09-22 10:32:28 -0700711
Brian Osmancfec9d52018-11-20 11:39:15 -0500712 // The innermost rect has 0 coverage...
713 vertices.writeQuad(inset_fan(devInside, SK_ScalarHalf, SK_ScalarHalf),
Brian Osman2a4c4df2018-12-20 14:06:54 -0500714 GrVertexColor(SK_PMColor4fTRANSPARENT, wideColor),
Brian Osmancfec9d52018-11-20 11:39:15 -0500715 maybe_coverage(0.0f));
716 } else {
717 // When the interior rect has become degenerate we smoosh to a single point
718 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
719
720 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
721 innerColor,
722 maybe_coverage(innerCoverage));
723
724 // ... unless we are degenerate, in which case we must apply the scaled coverage
725 vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside),
726 innerColor,
727 maybe_coverage(innerCoverage));
joshualitt9ff64252015-08-10 09:03:51 -0700728 }
729}
730
Michael Ludwig72ab3462018-12-10 12:43:36 -0500731} // anonymous namespace
joshualitt3566d442015-09-18 07:12:55 -0700732
Michael Ludwig72ab3462018-12-10 12:43:36 -0500733namespace GrStrokeRectOp {
734
Robert Phillipsb97da532019-02-12 15:24:12 -0500735std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500736 GrPaint&& paint,
737 GrAAType aaType,
738 const SkMatrix& viewMatrix,
739 const SkRect& rect,
740 const SkStrokeRec& stroke) {
741 if (aaType == GrAAType::kCoverage) {
742 // The AA op only supports axis-aligned rectangles
743 if (!viewMatrix.rectStaysRect()) {
744 return nullptr;
745 }
746 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
747 } else {
748 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType);
749 }
750}
751
Robert Phillipsb97da532019-02-12 15:24:12 -0500752std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500753 GrPaint&& paint,
754 const SkMatrix& viewMatrix,
755 const SkRect rects[2]) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400756 SkASSERT(viewMatrix.rectStaysRect());
757 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
758
759 SkRect devOutside, devInside;
760 viewMatrix.mapRect(&devOutside, rects[0]);
761 viewMatrix.mapRect(&devInside, rects[1]);
762 if (devInside.isEmpty()) {
763 if (devOutside.isEmpty()) {
764 return nullptr;
765 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500766 return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, viewMatrix,
767 rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400768 }
769
Robert Phillips7c525e62018-06-12 10:11:12 -0400770 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700771}
772
Michael Ludwig72ab3462018-12-10 12:43:36 -0500773} // namespace GrStrokeRectOp
joshualitt9ff64252015-08-10 09:03:51 -0700774
Hal Canary6f6961e2017-01-31 13:50:44 -0500775#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700776
Brian Salomon5ec9def2016-12-20 15:34:05 -0500777#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700778
Michael Ludwig72ab3462018-12-10 12:43:36 -0500779GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) {
780 SkMatrix viewMatrix = GrTest::TestMatrix(random);
781 SkRect rect = GrTest::TestRect(random);
782 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
783 SkPaint strokePaint;
784 strokePaint.setStrokeWidth(strokeWidth);
785 strokePaint.setStyle(SkPaint::kStroke_Style);
786 strokePaint.setStrokeJoin(SkPaint::kMiter_Join);
787 SkStrokeRec strokeRec(strokePaint);
788 GrAAType aaType = GrAAType::kNone;
789 if (fsaaType == GrFSAAType::kUnifiedMSAA) {
790 aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone;
791 }
792 return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType);
793}
794
Brian Salomonbaaf4392017-06-15 09:59:23 -0400795GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700796 bool miterStroke = random->nextBool();
797
bsalomon40ef4852016-05-02 13:22:13 -0700798 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500799 SkRect rect =
800 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700801 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
802 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700803
bsalomon40ef4852016-05-02 13:22:13 -0700804 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
805 rec.setStrokeStyle(strokeWidth);
806 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500807 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700808 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500809 return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700810}
811
812#endif