blob: b9a6d88021e9b579a05c4baf4d042464fe2017e5 [file] [log] [blame]
joshualitt9ff64252015-08-10 09:03:51 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
joshualitt9ff64252015-08-10 09:03:51 -07008#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -05009#include "GrOpFlushState.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040010#include "GrRectOpFactory.h"
joshualitt9ff64252015-08-10 09:03:51 -070011#include "GrResourceKey.h"
12#include "GrResourceProvider.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040013#include "GrSimpleMeshDrawOpHelper.h"
Cary Clark74f623d2017-11-06 20:02:02 -050014#include "SkPointPriv.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050015#include "SkStrokeRec.h"
joshualitt9ff64252015-08-10 09:03:51 -070016
17GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
18GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
19
Brian Salomon6a639042016-12-14 11:08:17 -050020static void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, SkScalar dy) {
Cary Clark74f623d2017-11-06 20:02:02 -050021 SkPointPriv::SetRectFan(pts, r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
joshualitt9ff64252015-08-10 09:03:51 -070022}
23
bsalomon8b7a9e12016-07-06 13:06:22 -070024// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
25// limit makes a miter join effectively beveled.
26inline static bool allowed_stroke(const SkStrokeRec& stroke, bool* isMiter) {
27 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
28 stroke.getStyle() == SkStrokeRec::kHairline_Style);
29 // For hairlines, make bevel and round joins appear the same as mitered ones.
30 if (!stroke.getWidth()) {
31 *isMiter = true;
32 return true;
33 }
34 if (stroke.getJoin() == SkPaint::kBevel_Join) {
35 *isMiter = false;
36 return true;
37 }
38 if (stroke.getJoin() == SkPaint::kMiter_Join) {
39 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
40 return true;
41 }
42 return false;
43}
44
45static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
46 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
47 SkScalar strokeWidth, bool miterStroke) {
48 SkRect devRect;
49 viewMatrix.mapRect(&devRect, rect);
50
51 SkVector devStrokeSize;
52 if (strokeWidth > 0) {
53 devStrokeSize.set(strokeWidth, strokeWidth);
54 viewMatrix.mapVectors(&devStrokeSize, 1);
55 devStrokeSize.setAbs(devStrokeSize);
56 } else {
57 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
58 }
59
60 const SkScalar dx = devStrokeSize.fX;
61 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -050062 const SkScalar rx = SkScalarHalf(dx);
63 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -070064
65 *devOutside = devRect;
66 *devOutsideAssist = devRect;
67 *devInside = devRect;
68
69 devOutside->outset(rx, ry);
70 devInside->inset(rx, ry);
71
72 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
73 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
74 // together when we render these rects.
75 SkScalar spare;
76 {
77 SkScalar w = devRect.width() - dx;
78 SkScalar h = devRect.height() - dy;
79 spare = SkTMin(w, h);
80 }
81
82 *isDegenerate = spare <= 0;
83 if (*isDegenerate) {
84 devInside->fLeft = devInside->fRight = devRect.centerX();
85 devInside->fTop = devInside->fBottom = devRect.centerY();
86 }
87
88 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
89 // to draw the outside of the octagon. Because there are 8 vertices on the outer
90 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
91 if (!miterStroke) {
92 devOutside->inset(0, ry);
93 devOutsideAssist->outset(0, ry);
94 }
95}
96
bungeman06ca8ec2016-06-09 08:01:03 -070097static sk_sp<GrGeometryProcessor> create_stroke_rect_gp(bool tweakAlphaForCoverage,
joshualitt9ff64252015-08-10 09:03:51 -070098 const SkMatrix& viewMatrix,
Brian Salomon8c5bad32016-12-20 14:43:36 -050099 bool usesLocalCoords) {
joshualitt9ff64252015-08-10 09:03:51 -0700100 using namespace GrDefaultGeoProcFactory;
101
joshualitt9ff64252015-08-10 09:03:51 -0700102 Coverage::Type coverageType;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500103 if (tweakAlphaForCoverage) {
joshualitt9ff64252015-08-10 09:03:51 -0700104 coverageType = Coverage::kSolid_Type;
105 } else {
106 coverageType = Coverage::kAttribute_Type;
107 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500108 LocalCoords::Type localCoordsType =
109 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500110 return MakeForDeviceSpace(Color::kPremulGrColorAttribute_Type, coverageType, localCoordsType,
111 viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700112}
113
Brian Salomonbaaf4392017-06-15 09:59:23 -0400114namespace {
115
116class AAStrokeRectOp final : public GrMeshDrawOp {
117private:
118 using Helper = GrSimpleMeshDrawOpHelper;
119
joshualitt3566d442015-09-18 07:12:55 -0700120public:
Brian Salomon25a88092016-12-01 09:36:50 -0500121 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700122
Robert Phillips7c525e62018-06-12 10:11:12 -0400123 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
124 GrPaint&& paint,
125 const SkMatrix& viewMatrix,
126 const SkRect& devOutside,
127 const SkRect& devInside) {
128 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
129 devOutside, devInside);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400130 }
131
132 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
133 const SkRect& devOutside, const SkRect& devInside)
134 : INHERITED(ClassID())
135 , fHelper(helperArgs, GrAAType::kCoverage)
136 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700137 SkASSERT(!devOutside.isEmpty());
138 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700139
Brian Salomon8c5bad32016-12-20 14:43:36 -0500140 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700141 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700142 fMiterStroke = true;
143 }
144
Robert Phillips7c525e62018-06-12 10:11:12 -0400145 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
146 GrPaint&& paint,
147 const SkMatrix& viewMatrix,
148 const SkRect& rect,
149 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700150 bool isMiter;
151 if (!allowed_stroke(stroke, &isMiter)) {
152 return nullptr;
153 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400154 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
155 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400156 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700157
Brian Salomonbaaf4392017-06-15 09:59:23 -0400158 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
159 const SkRect& rect, const SkStrokeRec& stroke, bool isMiter)
160 : INHERITED(ClassID())
161 , fHelper(helperArgs, GrAAType::kCoverage)
162 , fViewMatrix(viewMatrix) {
163 fMiterStroke = isMiter;
164 RectInfo& info = fRects.push_back();
Brian Salomon8c5bad32016-12-20 14:43:36 -0500165 compute_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
166 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
167 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400168 if (isMiter) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400169 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400170 } else {
171 // The outer polygon of the bevel stroke is an octagon specified by the points of a
172 // pair of overlapping rectangles where one is wide and the other is narrow.
173 SkRect bounds = info.fDevOutside;
174 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400175 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400176 }
joshualitt3566d442015-09-18 07:12:55 -0700177 }
178
179 const char* name() const override { return "AAStrokeRect"; }
180
Robert Phillipsf1748f52017-09-14 14:11:24 -0400181 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400182 fHelper.visitProxies(func);
183 }
184
Brian Salomon7c3e7182016-12-01 09:35:30 -0500185 SkString dumpInfo() const override {
186 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500187 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500188 string.appendf(
189 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
190 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
191 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Salomon8c5bad32016-12-20 14:43:36 -0500192 info.fColor, info.fDevOutside.fLeft, info.fDevOutside.fTop,
193 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
194 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
195 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
196 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500197 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400198 string += fHelper.dumpInfo();
199 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500200 return string;
201 }
202
Brian Salomonbaaf4392017-06-15 09:59:23 -0400203 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400204
Brian Osman9a725dd2017-09-20 09:53:22 -0400205 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
206 GrPixelConfigIsClamped dstIsClamped) override {
207 return fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped,
208 GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400209 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400210 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400211
212private:
Brian Salomon91326c32017-08-09 16:02:19 -0400213 void onPrepareDraws(Target*) override;
joshualittaa37a962015-09-18 13:03:25 -0700214
joshualitt3566d442015-09-18 07:12:55 -0700215 static const int kMiterIndexCnt = 3 * 24;
216 static const int kMiterVertexCnt = 16;
217 static const int kNumMiterRectsInIndexBuffer = 256;
218
219 static const int kBevelIndexCnt = 48 + 36 + 24;
220 static const int kBevelVertexCnt = 24;
221 static const int kNumBevelRectsInIndexBuffer = 256;
222
Brian Salomond28a79d2017-10-16 13:01:07 -0400223 static sk_sp<const GrBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700224
joshualittaa37a962015-09-18 13:03:25 -0700225 const SkMatrix& viewMatrix() const { return fViewMatrix; }
226 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700227
Brian Salomon25a88092016-12-01 09:36:50 -0500228 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700229
230 void generateAAStrokeRectGeometry(void* vertices,
231 size_t offset,
232 size_t vertexStride,
233 int outerVertexNum,
234 int innerVertexNum,
235 GrColor color,
236 const SkRect& devOutside,
237 const SkRect& devOutsideAssist,
238 const SkRect& devInside,
239 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700240 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700241 bool tweakAlphaForCoverage) const;
242
bsalomon8b7a9e12016-07-06 13:06:22 -0700243 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500244 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700245 GrColor fColor;
246 SkRect fDevOutside;
247 SkRect fDevOutsideAssist;
248 SkRect fDevInside;
249 bool fDegenerate;
250 };
251
Brian Salomonbaaf4392017-06-15 09:59:23 -0400252 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500253 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700254 SkMatrix fViewMatrix;
255 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700256
Brian Salomonbaaf4392017-06-15 09:59:23 -0400257 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700258};
259
Brian Salomonbaaf4392017-06-15 09:59:23 -0400260} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700261
Brian Salomon91326c32017-08-09 16:02:19 -0400262void AAStrokeRectOp::onPrepareDraws(Target* target) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400263 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(fHelper.compatibleWithAlphaAsCoverage(),
bungeman06ca8ec2016-06-09 08:01:03 -0700264 this->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400265 fHelper.usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700266 if (!gp) {
267 SkDebugf("Couldn't create GrGeometryProcessor\n");
268 return;
269 }
270
joshualitt9ff64252015-08-10 09:03:51 -0700271 size_t vertexStride = gp->getVertexStride();
272
Brian Salomonbaaf4392017-06-15 09:59:23 -0400273 SkASSERT(fHelper.compatibleWithAlphaAsCoverage()
Brian Salomon6a639042016-12-14 11:08:17 -0500274 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
275 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700276 int innerVertexNum = 4;
277 int outerVertexNum = this->miterStroke() ? 4 : 8;
278 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
279 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500280 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700281
Brian Salomond28a79d2017-10-16 13:01:07 -0400282 sk_sp<const GrBuffer> indexBuffer = GetIndexBuffer(target->resourceProvider(), this->miterStroke());
Chris Dalton3809bab2017-06-13 10:55:06 -0600283 PatternHelper helper(GrPrimitiveType::kTriangles);
Brian Salomon6a639042016-12-14 11:08:17 -0500284 void* vertices =
Chris Daltonbca46e22017-05-15 11:03:26 -0600285 helper.init(target, vertexStride, indexBuffer.get(),
Brian Salomon6a639042016-12-14 11:08:17 -0500286 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700287 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500288 SkDebugf("Could not allocate vertices\n");
289 return;
290 }
joshualitt9ff64252015-08-10 09:03:51 -0700291
292 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500293 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700294 this->generateAAStrokeRectGeometry(vertices,
295 i * verticesPerInstance * vertexStride,
296 vertexStride,
297 outerVertexNum,
298 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500299 info.fColor,
300 info.fDevOutside,
301 info.fDevOutsideAssist,
302 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700303 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500304 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400305 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700306 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400307 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
joshualitt9ff64252015-08-10 09:03:51 -0700308}
309
Brian Salomond28a79d2017-10-16 13:01:07 -0400310sk_sp<const GrBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
311 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700312 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500313 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700314 static const uint16_t gMiterIndices[] = {
315 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
316 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
317 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
318 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
319
320 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
321 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
322 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
323 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
324
325 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
326 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
327 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
328 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
329 };
Brian Salomon6a639042016-12-14 11:08:17 -0500330 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700331 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
332 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400333 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500334 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
335 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700336 } else {
337 /**
338 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
339 * from the first index. The index layout:
340 * outer AA line: 0~3, 4~7
341 * outer edge: 8~11, 12~15
342 * inner edge: 16~19
343 * inner AA line: 20~23
344 * Following comes a bevel-stroke rect and its indices:
345 *
346 * 4 7
347 * *********************************
348 * * ______________________________ *
349 * * / 12 15 \ *
350 * * / \ *
351 * 0 * |8 16_____________________19 11 | * 3
352 * * | | | | *
353 * * | | **************** | | *
354 * * | | * 20 23 * | | *
355 * * | | * * | | *
356 * * | | * 21 22 * | | *
357 * * | | **************** | | *
358 * * | |____________________| | *
359 * 1 * |9 17 18 10| * 2
360 * * \ / *
361 * * \13 __________________________14/ *
362 * * *
363 * **********************************
364 * 5 6
365 */
Brian Salomon6a639042016-12-14 11:08:17 -0500366 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700367 static const uint16_t gBevelIndices[] = {
368 // Draw outer AA, from outer AA line to outer edge, shift is 0.
369 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
370 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
371 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
372 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
373 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
374 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
375 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
376 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
377
378 // Draw the stroke, from outer edge to inner edge, shift is 8.
379 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
380 1 + 8, 5 + 8, 9 + 8,
381 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
382 6 + 8, 2 + 8, 10 + 8,
383 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
384 3 + 8, 7 + 8, 11 + 8,
385 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
386 4 + 8, 0 + 8, 8 + 8,
387
388 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
389 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
390 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
391 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
392 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
393 };
Brian Salomon6a639042016-12-14 11:08:17 -0500394 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700395 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
396
397 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400398 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500399 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
400 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700401 }
402}
403
Brian Salomon6a639042016-12-14 11:08:17 -0500404bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
405 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700406
Brian Salomonbaaf4392017-06-15 09:59:23 -0400407 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt9ff64252015-08-10 09:03:51 -0700408 return false;
409 }
410
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500411 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700412 if (this->miterStroke() != that->miterStroke()) {
413 return false;
414 }
415
416 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400417 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
418 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt9ff64252015-08-10 09:03:51 -0700419 return false;
420 }
421
Brian Salomon8c5bad32016-12-20 14:43:36 -0500422 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700423 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700424 return true;
425}
426
joshualitt11edad92015-09-22 10:32:28 -0700427static void setup_scale(int* scale, SkScalar inset) {
428 if (inset < SK_ScalarHalf) {
429 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
430 SkASSERT(*scale >= 0 && *scale <= 255);
431 } else {
432 *scale = 0xff;
433 }
434}
435
Brian Salomon6a639042016-12-14 11:08:17 -0500436void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
437 size_t offset,
438 size_t vertexStride,
439 int outerVertexNum,
440 int innerVertexNum,
441 GrColor color,
442 const SkRect& devOutside,
443 const SkRect& devOutsideAssist,
444 const SkRect& devInside,
445 bool miterStroke,
446 bool degenerate,
447 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700448 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
449
450 // We create vertices for four nested rectangles. There are two ramps from 0 to full
451 // coverage, one on the exterior of the stroke and the other on the interior.
452 // The following pointers refer to the four rects, from outermost to innermost.
453 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
454 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
455 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500456 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
457 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700458
459#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
460 // TODO: this only really works if the X & Y margins are the same all around
461 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700462 SkScalar inset;
463 if (!degenerate) {
464 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
465 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
466 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
467 if (miterStroke) {
468 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
469 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500470 inset = SK_ScalarHalf *
471 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700472 }
473 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700474 } else {
joshualitt11edad92015-09-22 10:32:28 -0700475 // TODO use real devRect here
476 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500477 inset = SK_ScalarHalf *
478 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700479 }
joshualitt9ff64252015-08-10 09:03:51 -0700480#else
joshualitt11edad92015-09-22 10:32:28 -0700481 SkScalar inset;
482 if (!degenerate) {
483 inset = SK_ScalarHalf;
484 } else {
485 // TODO use real devRect here
486 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500487 inset = SK_ScalarHalf *
488 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700489 }
joshualitt9ff64252015-08-10 09:03:51 -0700490#endif
491
492 if (miterStroke) {
493 // outermost
494 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
495 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500496 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700497 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500498 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700499 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500500 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700501 } else {
502 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500503 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500504 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500505 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500506 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500507 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700508 }
joshualitt9ff64252015-08-10 09:03:51 -0700509 } else {
510 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500511 SkPoint* fan1AssistPos =
512 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700513 // outermost
514 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
515 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
516 -SK_ScalarHalf);
517 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500518 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
519 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700520 if (!degenerate) {
521 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500522 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700523 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500524 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700525 } else {
526 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500527 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500528 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500529 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500530 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500531 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700532 }
joshualitt9ff64252015-08-10 09:03:51 -0700533 }
534
535 // Make verts point to vertex color and then set all the color and coverage vertex attrs
536 // values. The outermost rect has 0 coverage
537 verts += sizeof(SkPoint);
538 for (int i = 0; i < outerVertexNum; ++i) {
539 if (tweakAlphaForCoverage) {
540 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
541 } else {
542 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
543 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
544 }
545 }
546
547 // scale is the coverage for the the inner two rects.
548 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700549 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700550
551 float innerCoverage = GrNormalizeByteToFloat(scale);
552 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
553
554 verts += outerVertexNum * vertexStride;
555 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
556 if (tweakAlphaForCoverage) {
557 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
558 } else {
559 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700560 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700561 }
562 }
563
joshualitt11edad92015-09-22 10:32:28 -0700564 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
565 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700566 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700567 if (!degenerate) {
568 innerCoverage = 0;
569 scaledColor = 0;
570 }
571
joshualitt9ff64252015-08-10 09:03:51 -0700572 for (int i = 0; i < innerVertexNum; ++i) {
573 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700574 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700575 } else {
576 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700577 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700578 }
579 }
580}
581
Brian Salomonbaaf4392017-06-15 09:59:23 -0400582namespace GrRectOpFactory {
joshualitt3566d442015-09-18 07:12:55 -0700583
Robert Phillips7c525e62018-06-12 10:11:12 -0400584std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrContext* context,
585 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400586 const SkMatrix& viewMatrix,
587 const SkRect rects[2]) {
588 SkASSERT(viewMatrix.rectStaysRect());
589 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
590
591 SkRect devOutside, devInside;
592 viewMatrix.mapRect(&devOutside, rects[0]);
593 viewMatrix.mapRect(&devInside, rects[1]);
594 if (devInside.isEmpty()) {
595 if (devOutside.isEmpty()) {
596 return nullptr;
597 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400598 return MakeAAFill(context, std::move(paint), viewMatrix, rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400599 }
600
Robert Phillips7c525e62018-06-12 10:11:12 -0400601 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700602}
603
Robert Phillips7c525e62018-06-12 10:11:12 -0400604std::unique_ptr<GrDrawOp> MakeAAStroke(GrContext* context,
605 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400606 const SkMatrix& viewMatrix,
607 const SkRect& rect,
608 const SkStrokeRec& stroke) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400609 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700610}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400611
612} // namespace GrRectOpFactory
joshualitt3566d442015-09-18 07:12:55 -0700613
joshualitt9ff64252015-08-10 09:03:51 -0700614///////////////////////////////////////////////////////////////////////////////////////////////////
615
Hal Canary6f6961e2017-01-31 13:50:44 -0500616#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700617
Brian Salomon5ec9def2016-12-20 15:34:05 -0500618#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700619
Brian Salomonbaaf4392017-06-15 09:59:23 -0400620GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700621 bool miterStroke = random->nextBool();
622
bsalomon40ef4852016-05-02 13:22:13 -0700623 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500624 SkRect rect =
625 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700626 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
627 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700628
bsalomon40ef4852016-05-02 13:22:13 -0700629 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
630 rec.setStrokeStyle(strokeWidth);
631 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500632 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700633 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Robert Phillips7c525e62018-06-12 10:11:12 -0400634 return GrRectOpFactory::MakeAAStroke(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700635}
636
637#endif