blob: 82b4ec13faa1cdf585ce64e5e622ddbc6ffb71cd [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"
Hal Canary6f6961e2017-01-31 13:50:44 -050014#include "SkStrokeRec.h"
joshualitt9ff64252015-08-10 09:03:51 -070015
16GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
17GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
18
Brian Salomon6a639042016-12-14 11:08:17 -050019static void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, SkScalar dy) {
20 pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
joshualitt9ff64252015-08-10 09:03:51 -070021}
22
bsalomon8b7a9e12016-07-06 13:06:22 -070023// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
24// limit makes a miter join effectively beveled.
25inline static bool allowed_stroke(const SkStrokeRec& stroke, bool* isMiter) {
26 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
27 stroke.getStyle() == SkStrokeRec::kHairline_Style);
28 // For hairlines, make bevel and round joins appear the same as mitered ones.
29 if (!stroke.getWidth()) {
30 *isMiter = true;
31 return true;
32 }
33 if (stroke.getJoin() == SkPaint::kBevel_Join) {
34 *isMiter = false;
35 return true;
36 }
37 if (stroke.getJoin() == SkPaint::kMiter_Join) {
38 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
39 return true;
40 }
41 return false;
42}
43
44static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
45 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
46 SkScalar strokeWidth, bool miterStroke) {
47 SkRect devRect;
48 viewMatrix.mapRect(&devRect, rect);
49
50 SkVector devStrokeSize;
51 if (strokeWidth > 0) {
52 devStrokeSize.set(strokeWidth, strokeWidth);
53 viewMatrix.mapVectors(&devStrokeSize, 1);
54 devStrokeSize.setAbs(devStrokeSize);
55 } else {
56 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
57 }
58
59 const SkScalar dx = devStrokeSize.fX;
60 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -050061 const SkScalar rx = SkScalarHalf(dx);
62 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -070063
64 *devOutside = devRect;
65 *devOutsideAssist = devRect;
66 *devInside = devRect;
67
68 devOutside->outset(rx, ry);
69 devInside->inset(rx, ry);
70
71 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
72 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
73 // together when we render these rects.
74 SkScalar spare;
75 {
76 SkScalar w = devRect.width() - dx;
77 SkScalar h = devRect.height() - dy;
78 spare = SkTMin(w, h);
79 }
80
81 *isDegenerate = spare <= 0;
82 if (*isDegenerate) {
83 devInside->fLeft = devInside->fRight = devRect.centerX();
84 devInside->fTop = devInside->fBottom = devRect.centerY();
85 }
86
87 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
88 // to draw the outside of the octagon. Because there are 8 vertices on the outer
89 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
90 if (!miterStroke) {
91 devOutside->inset(0, ry);
92 devOutsideAssist->outset(0, ry);
93 }
94}
95
bungeman06ca8ec2016-06-09 08:01:03 -070096static sk_sp<GrGeometryProcessor> create_stroke_rect_gp(bool tweakAlphaForCoverage,
joshualitt9ff64252015-08-10 09:03:51 -070097 const SkMatrix& viewMatrix,
Brian Salomon8c5bad32016-12-20 14:43:36 -050098 bool usesLocalCoords) {
joshualitt9ff64252015-08-10 09:03:51 -070099 using namespace GrDefaultGeoProcFactory;
100
joshualitt9ff64252015-08-10 09:03:51 -0700101 Coverage::Type coverageType;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500102 if (tweakAlphaForCoverage) {
joshualitt9ff64252015-08-10 09:03:51 -0700103 coverageType = Coverage::kSolid_Type;
104 } else {
105 coverageType = Coverage::kAttribute_Type;
106 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500107 LocalCoords::Type localCoordsType =
108 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500109 return MakeForDeviceSpace(Color::kPremulGrColorAttribute_Type, coverageType, localCoordsType,
110 viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700111}
112
Brian Salomonbaaf4392017-06-15 09:59:23 -0400113namespace {
114
115class AAStrokeRectOp final : public GrMeshDrawOp {
116private:
117 using Helper = GrSimpleMeshDrawOpHelper;
118
joshualitt3566d442015-09-18 07:12:55 -0700119public:
Brian Salomon25a88092016-12-01 09:36:50 -0500120 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700121
Brian Salomonbaaf4392017-06-15 09:59:23 -0400122 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix,
123 const SkRect& devOutside, const SkRect& devInside) {
124 return Helper::FactoryHelper<AAStrokeRectOp>(std::move(paint), viewMatrix, devOutside,
125 devInside);
126 }
127
128 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
129 const SkRect& devOutside, const SkRect& devInside)
130 : INHERITED(ClassID())
131 , fHelper(helperArgs, GrAAType::kCoverage)
132 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700133 SkASSERT(!devOutside.isEmpty());
134 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700135
Brian Salomon8c5bad32016-12-20 14:43:36 -0500136 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700137 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700138 fMiterStroke = true;
139 }
140
Brian Salomonbaaf4392017-06-15 09:59:23 -0400141 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix,
142 const SkRect& rect, const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700143 bool isMiter;
144 if (!allowed_stroke(stroke, &isMiter)) {
145 return nullptr;
146 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400147 return Helper::FactoryHelper<AAStrokeRectOp>(std::move(paint), viewMatrix, rect, stroke,
148 isMiter);
149 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700150
Brian Salomonbaaf4392017-06-15 09:59:23 -0400151 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
152 const SkRect& rect, const SkStrokeRec& stroke, bool isMiter)
153 : INHERITED(ClassID())
154 , fHelper(helperArgs, GrAAType::kCoverage)
155 , fViewMatrix(viewMatrix) {
156 fMiterStroke = isMiter;
157 RectInfo& info = fRects.push_back();
Brian Salomon8c5bad32016-12-20 14:43:36 -0500158 compute_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
159 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
160 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400161 if (isMiter) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400162 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400163 } else {
164 // The outer polygon of the bevel stroke is an octagon specified by the points of a
165 // pair of overlapping rectangles where one is wide and the other is narrow.
166 SkRect bounds = info.fDevOutside;
167 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400168 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400169 }
joshualitt3566d442015-09-18 07:12:55 -0700170 }
171
172 const char* name() const override { return "AAStrokeRect"; }
173
Brian Salomon7c3e7182016-12-01 09:35:30 -0500174 SkString dumpInfo() const override {
175 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500176 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500177 string.appendf(
178 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
179 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
180 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Salomon8c5bad32016-12-20 14:43:36 -0500181 info.fColor, info.fDevOutside.fLeft, info.fDevOutside.fTop,
182 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
183 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
184 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
185 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500186 }
Brian Salomon7c3e7182016-12-01 09:35:30 -0500187 string.append(INHERITED::dumpInfo());
188 return string;
189 }
190
Brian Salomonbaaf4392017-06-15 09:59:23 -0400191 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400192
Brian Salomonbaaf4392017-06-15 09:59:23 -0400193 bool xpRequiresDstTexture(const GrCaps& caps, const GrAppliedClip* clip) override {
194 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
195 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400196 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400197
198private:
joshualitt144c3c82015-11-30 12:30:13 -0800199 void onPrepareDraws(Target*) const override;
joshualittaa37a962015-09-18 13:03:25 -0700200
joshualitt3566d442015-09-18 07:12:55 -0700201 static const int kMiterIndexCnt = 3 * 24;
202 static const int kMiterVertexCnt = 16;
203 static const int kNumMiterRectsInIndexBuffer = 256;
204
205 static const int kBevelIndexCnt = 48 + 36 + 24;
206 static const int kBevelVertexCnt = 24;
207 static const int kNumBevelRectsInIndexBuffer = 256;
208
cdalton397536c2016-03-25 12:15:03 -0700209 static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700210
joshualittaa37a962015-09-18 13:03:25 -0700211 const SkMatrix& viewMatrix() const { return fViewMatrix; }
212 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700213
Brian Salomon25a88092016-12-01 09:36:50 -0500214 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700215
216 void generateAAStrokeRectGeometry(void* vertices,
217 size_t offset,
218 size_t vertexStride,
219 int outerVertexNum,
220 int innerVertexNum,
221 GrColor color,
222 const SkRect& devOutside,
223 const SkRect& devOutsideAssist,
224 const SkRect& devInside,
225 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700226 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700227 bool tweakAlphaForCoverage) const;
228
bsalomon8b7a9e12016-07-06 13:06:22 -0700229 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500230 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700231 GrColor fColor;
232 SkRect fDevOutside;
233 SkRect fDevOutsideAssist;
234 SkRect fDevInside;
235 bool fDegenerate;
236 };
237
Brian Salomonbaaf4392017-06-15 09:59:23 -0400238 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500239 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700240 SkMatrix fViewMatrix;
241 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700242
Brian Salomonbaaf4392017-06-15 09:59:23 -0400243 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700244};
245
Brian Salomonbaaf4392017-06-15 09:59:23 -0400246} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700247
Brian Salomon6a639042016-12-14 11:08:17 -0500248void AAStrokeRectOp::onPrepareDraws(Target* target) const {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400249 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(fHelper.compatibleWithAlphaAsCoverage(),
bungeman06ca8ec2016-06-09 08:01:03 -0700250 this->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400251 fHelper.usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700252 if (!gp) {
253 SkDebugf("Couldn't create GrGeometryProcessor\n");
254 return;
255 }
256
joshualitt9ff64252015-08-10 09:03:51 -0700257 size_t vertexStride = gp->getVertexStride();
258
Brian Salomonbaaf4392017-06-15 09:59:23 -0400259 SkASSERT(fHelper.compatibleWithAlphaAsCoverage()
Brian Salomon6a639042016-12-14 11:08:17 -0500260 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
261 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700262 int innerVertexNum = 4;
263 int outerVertexNum = this->miterStroke() ? 4 : 8;
264 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
265 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500266 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700267
Hal Canary144caf52016-11-07 17:57:18 -0500268 const sk_sp<const GrBuffer> indexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500269 GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
Chris Dalton3809bab2017-06-13 10:55:06 -0600270 PatternHelper helper(GrPrimitiveType::kTriangles);
Brian Salomon6a639042016-12-14 11:08:17 -0500271 void* vertices =
Chris Daltonbca46e22017-05-15 11:03:26 -0600272 helper.init(target, vertexStride, indexBuffer.get(),
Brian Salomon6a639042016-12-14 11:08:17 -0500273 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700274 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500275 SkDebugf("Could not allocate vertices\n");
276 return;
277 }
joshualitt9ff64252015-08-10 09:03:51 -0700278
279 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500280 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700281 this->generateAAStrokeRectGeometry(vertices,
282 i * verticesPerInstance * vertexStride,
283 vertexStride,
284 outerVertexNum,
285 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500286 info.fColor,
287 info.fDevOutside,
288 info.fDevOutsideAssist,
289 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700290 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500291 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400292 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700293 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400294 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
joshualitt9ff64252015-08-10 09:03:51 -0700295}
296
Brian Salomon6a639042016-12-14 11:08:17 -0500297const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
298 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700299 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500300 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700301 static const uint16_t gMiterIndices[] = {
302 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
303 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
304 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
305 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
306
307 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
308 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
309 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
310 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
311
312 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
313 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
314 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
315 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
316 };
Brian Salomon6a639042016-12-14 11:08:17 -0500317 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700318 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
319 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400320 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500321 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
322 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700323 } else {
324 /**
325 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
326 * from the first index. The index layout:
327 * outer AA line: 0~3, 4~7
328 * outer edge: 8~11, 12~15
329 * inner edge: 16~19
330 * inner AA line: 20~23
331 * Following comes a bevel-stroke rect and its indices:
332 *
333 * 4 7
334 * *********************************
335 * * ______________________________ *
336 * * / 12 15 \ *
337 * * / \ *
338 * 0 * |8 16_____________________19 11 | * 3
339 * * | | | | *
340 * * | | **************** | | *
341 * * | | * 20 23 * | | *
342 * * | | * * | | *
343 * * | | * 21 22 * | | *
344 * * | | **************** | | *
345 * * | |____________________| | *
346 * 1 * |9 17 18 10| * 2
347 * * \ / *
348 * * \13 __________________________14/ *
349 * * *
350 * **********************************
351 * 5 6
352 */
Brian Salomon6a639042016-12-14 11:08:17 -0500353 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700354 static const uint16_t gBevelIndices[] = {
355 // Draw outer AA, from outer AA line to outer edge, shift is 0.
356 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
357 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
358 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
359 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
360 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
361 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
362 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
363 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
364
365 // Draw the stroke, from outer edge to inner edge, shift is 8.
366 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
367 1 + 8, 5 + 8, 9 + 8,
368 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
369 6 + 8, 2 + 8, 10 + 8,
370 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
371 3 + 8, 7 + 8, 11 + 8,
372 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
373 4 + 8, 0 + 8, 8 + 8,
374
375 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
376 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
377 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
378 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
379 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
380 };
Brian Salomon6a639042016-12-14 11:08:17 -0500381 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700382 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
383
384 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400385 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500386 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
387 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700388 }
389}
390
Brian Salomon6a639042016-12-14 11:08:17 -0500391bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
392 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700393
Brian Salomonbaaf4392017-06-15 09:59:23 -0400394 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt9ff64252015-08-10 09:03:51 -0700395 return false;
396 }
397
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500398 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700399 if (this->miterStroke() != that->miterStroke()) {
400 return false;
401 }
402
403 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400404 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
405 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt9ff64252015-08-10 09:03:51 -0700406 return false;
407 }
408
Brian Salomon8c5bad32016-12-20 14:43:36 -0500409 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700410 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700411 return true;
412}
413
joshualitt11edad92015-09-22 10:32:28 -0700414static void setup_scale(int* scale, SkScalar inset) {
415 if (inset < SK_ScalarHalf) {
416 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
417 SkASSERT(*scale >= 0 && *scale <= 255);
418 } else {
419 *scale = 0xff;
420 }
421}
422
Brian Salomon6a639042016-12-14 11:08:17 -0500423void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
424 size_t offset,
425 size_t vertexStride,
426 int outerVertexNum,
427 int innerVertexNum,
428 GrColor color,
429 const SkRect& devOutside,
430 const SkRect& devOutsideAssist,
431 const SkRect& devInside,
432 bool miterStroke,
433 bool degenerate,
434 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700435 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
436
437 // We create vertices for four nested rectangles. There are two ramps from 0 to full
438 // coverage, one on the exterior of the stroke and the other on the interior.
439 // The following pointers refer to the four rects, from outermost to innermost.
440 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
441 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
442 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500443 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
444 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700445
446#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
447 // TODO: this only really works if the X & Y margins are the same all around
448 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700449 SkScalar inset;
450 if (!degenerate) {
451 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
452 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
453 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
454 if (miterStroke) {
455 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
456 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500457 inset = SK_ScalarHalf *
458 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700459 }
460 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700461 } else {
joshualitt11edad92015-09-22 10:32:28 -0700462 // TODO use real devRect here
463 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500464 inset = SK_ScalarHalf *
465 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700466 }
joshualitt9ff64252015-08-10 09:03:51 -0700467#else
joshualitt11edad92015-09-22 10:32:28 -0700468 SkScalar inset;
469 if (!degenerate) {
470 inset = SK_ScalarHalf;
471 } else {
472 // TODO use real devRect here
473 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500474 inset = SK_ScalarHalf *
475 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700476 }
joshualitt9ff64252015-08-10 09:03:51 -0700477#endif
478
479 if (miterStroke) {
480 // outermost
481 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
482 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500483 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700484 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500485 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700486 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500487 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700488 } else {
489 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500490 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
491 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
492 devInside.fBottom, vertexStride);
493 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
494 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700495 }
joshualitt9ff64252015-08-10 09:03:51 -0700496 } else {
497 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500498 SkPoint* fan1AssistPos =
499 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700500 // outermost
501 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
502 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
503 -SK_ScalarHalf);
504 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500505 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
506 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700507 if (!degenerate) {
508 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500509 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700510 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500511 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700512 } else {
513 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500514 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
515 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
516 devInside.fBottom, vertexStride);
517 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
518 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700519 }
joshualitt9ff64252015-08-10 09:03:51 -0700520 }
521
522 // Make verts point to vertex color and then set all the color and coverage vertex attrs
523 // values. The outermost rect has 0 coverage
524 verts += sizeof(SkPoint);
525 for (int i = 0; i < outerVertexNum; ++i) {
526 if (tweakAlphaForCoverage) {
527 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
528 } else {
529 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
530 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
531 }
532 }
533
534 // scale is the coverage for the the inner two rects.
535 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700536 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700537
538 float innerCoverage = GrNormalizeByteToFloat(scale);
539 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
540
541 verts += outerVertexNum * vertexStride;
542 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
543 if (tweakAlphaForCoverage) {
544 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
545 } else {
546 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700547 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700548 }
549 }
550
joshualitt11edad92015-09-22 10:32:28 -0700551 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
552 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700553 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700554 if (!degenerate) {
555 innerCoverage = 0;
556 scaledColor = 0;
557 }
558
joshualitt9ff64252015-08-10 09:03:51 -0700559 for (int i = 0; i < innerVertexNum; ++i) {
560 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700561 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700562 } else {
563 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700564 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700565 }
566 }
567}
568
Brian Salomonbaaf4392017-06-15 09:59:23 -0400569namespace GrRectOpFactory {
joshualitt3566d442015-09-18 07:12:55 -0700570
Brian Salomonbaaf4392017-06-15 09:59:23 -0400571std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrPaint&& paint,
572 const SkMatrix& viewMatrix,
573 const SkRect rects[2]) {
574 SkASSERT(viewMatrix.rectStaysRect());
575 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
576
577 SkRect devOutside, devInside;
578 viewMatrix.mapRect(&devOutside, rects[0]);
579 viewMatrix.mapRect(&devInside, rects[1]);
580 if (devInside.isEmpty()) {
581 if (devOutside.isEmpty()) {
582 return nullptr;
583 }
584 return MakeAAFillWithDevRect(std::move(paint), viewMatrix, rects[0], devOutside);
585 }
586
587 return AAStrokeRectOp::Make(std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700588}
589
Brian Salomonbaaf4392017-06-15 09:59:23 -0400590std::unique_ptr<GrDrawOp> MakeAAStroke(GrPaint&& paint,
591 const SkMatrix& viewMatrix,
592 const SkRect& rect,
593 const SkStrokeRec& stroke) {
594 return AAStrokeRectOp::Make(std::move(paint), viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700595}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400596
597} // namespace GrRectOpFactory
joshualitt3566d442015-09-18 07:12:55 -0700598
joshualitt9ff64252015-08-10 09:03:51 -0700599///////////////////////////////////////////////////////////////////////////////////////////////////
600
Hal Canary6f6961e2017-01-31 13:50:44 -0500601#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700602
Brian Salomon5ec9def2016-12-20 15:34:05 -0500603#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700604
Brian Salomonbaaf4392017-06-15 09:59:23 -0400605GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700606 bool miterStroke = random->nextBool();
607
bsalomon40ef4852016-05-02 13:22:13 -0700608 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500609 SkRect rect =
610 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700611 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
612 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700613
bsalomon40ef4852016-05-02 13:22:13 -0700614 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
615 rec.setStrokeStyle(strokeWidth);
616 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500617 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700618 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400619 return GrRectOpFactory::MakeAAStroke(std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700620}
621
622#endif