blob: cf16d1bea4199bd2c07cfad69004fc2cf42710b9 [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
Brian Salomon6a639042016-12-14 11:08:17 -05008#include "GrAAStrokeRectOp.h"
joshualitt9ff64252015-08-10 09:03:51 -07009
10#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
joshualitt9ff64252015-08-10 09:03:51 -070012#include "GrResourceKey.h"
13#include "GrResourceProvider.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 Salomond3ccb0a2017-04-03 10:38:00 -0400113class AAStrokeRectOp final : public GrLegacyMeshDrawOp {
joshualitt3566d442015-09-18 07:12:55 -0700114public:
Brian Salomon25a88092016-12-01 09:36:50 -0500115 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700116
Brian Salomon6a639042016-12-14 11:08:17 -0500117 AAStrokeRectOp(GrColor color, const SkMatrix& viewMatrix, const SkRect& devOutside,
118 const SkRect& devInside)
119 : INHERITED(ClassID()), fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700120 SkASSERT(!devOutside.isEmpty());
121 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700122
Brian Salomon8c5bad32016-12-20 14:43:36 -0500123 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700124 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700125 fMiterStroke = true;
126 }
127
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400128 static std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, const SkMatrix& viewMatrix,
129 const SkRect& rect, const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700130 bool isMiter;
131 if (!allowed_stroke(stroke, &isMiter)) {
132 return nullptr;
133 }
134
Brian Salomon6a639042016-12-14 11:08:17 -0500135 AAStrokeRectOp* op = new AAStrokeRectOp();
136 op->fMiterStroke = isMiter;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500137 RectInfo& info = op->fRects.push_back();
138 compute_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
139 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
140 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400141 if (isMiter) {
142 op->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
143 } else {
144 // The outer polygon of the bevel stroke is an octagon specified by the points of a
145 // pair of overlapping rectangles where one is wide and the other is narrow.
146 SkRect bounds = info.fDevOutside;
147 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
148 op->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
149 }
Brian Salomon6a639042016-12-14 11:08:17 -0500150 op->fViewMatrix = viewMatrix;
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400151 return std::unique_ptr<GrLegacyMeshDrawOp>(op);
joshualitt3566d442015-09-18 07:12:55 -0700152 }
153
154 const char* name() const override { return "AAStrokeRect"; }
155
Brian Salomon7c3e7182016-12-01 09:35:30 -0500156 SkString dumpInfo() const override {
157 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500158 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500159 string.appendf(
160 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
161 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
162 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Salomon8c5bad32016-12-20 14:43:36 -0500163 info.fColor, info.fDevOutside.fLeft, info.fDevOutside.fTop,
164 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
165 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
166 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
167 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500168 }
169 string.append(DumpPipelineInfo(*this->pipeline()));
170 string.append(INHERITED::dumpInfo());
171 return string;
172 }
173
joshualittaa37a962015-09-18 13:03:25 -0700174private:
Brian Salomon6a639042016-12-14 11:08:17 -0500175 AAStrokeRectOp() : INHERITED(ClassID()) {}
joshualittaa37a962015-09-18 13:03:25 -0700176
Brian Salomona811b122017-03-30 08:21:32 -0400177 void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
178 GrProcessorAnalysisCoverage* coverage) const override {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400179 color->setToConstant(fRects[0].fColor);
Brian Salomona811b122017-03-30 08:21:32 -0400180 *coverage = GrProcessorAnalysisCoverage::kSingleChannel;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500181 }
Brian Salomone7d30482017-03-29 12:09:15 -0400182 void applyPipelineOptimizations(const PipelineOptimizations&) override;
joshualitt144c3c82015-11-30 12:30:13 -0800183 void onPrepareDraws(Target*) const override;
joshualittaa37a962015-09-18 13:03:25 -0700184
joshualitt3566d442015-09-18 07:12:55 -0700185 static const int kMiterIndexCnt = 3 * 24;
186 static const int kMiterVertexCnt = 16;
187 static const int kNumMiterRectsInIndexBuffer = 256;
188
189 static const int kBevelIndexCnt = 48 + 36 + 24;
190 static const int kBevelVertexCnt = 24;
191 static const int kNumBevelRectsInIndexBuffer = 256;
192
cdalton397536c2016-03-25 12:15:03 -0700193 static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700194
Brian Salomon8c5bad32016-12-20 14:43:36 -0500195 bool usesLocalCoords() const { return fUsesLocalCoords; }
196 bool canTweakAlphaForCoverage() const { return fCanTweakAlphaForCoverage; }
joshualittaa37a962015-09-18 13:03:25 -0700197 const SkMatrix& viewMatrix() const { return fViewMatrix; }
198 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700199
Brian Salomon25a88092016-12-01 09:36:50 -0500200 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700201
202 void generateAAStrokeRectGeometry(void* vertices,
203 size_t offset,
204 size_t vertexStride,
205 int outerVertexNum,
206 int innerVertexNum,
207 GrColor color,
208 const SkRect& devOutside,
209 const SkRect& devOutsideAssist,
210 const SkRect& devInside,
211 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700212 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700213 bool tweakAlphaForCoverage) const;
214
bsalomon8b7a9e12016-07-06 13:06:22 -0700215 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500216 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700217 GrColor fColor;
218 SkRect fDevOutside;
219 SkRect fDevOutsideAssist;
220 SkRect fDevInside;
221 bool fDegenerate;
222 };
223
Brian Salomon8c5bad32016-12-20 14:43:36 -0500224 SkSTArray<1, RectInfo, true> fRects;
225 bool fUsesLocalCoords;
226 bool fCanTweakAlphaForCoverage;
joshualittaa37a962015-09-18 13:03:25 -0700227 SkMatrix fViewMatrix;
228 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700229
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400230 typedef GrLegacyMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700231};
232
Brian Salomone7d30482017-03-29 12:09:15 -0400233void AAStrokeRectOp::applyPipelineOptimizations(const PipelineOptimizations& optimizations) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500234 optimizations.getOverrideColorIfSet(&fRects[0].fColor);
joshualitt9ff64252015-08-10 09:03:51 -0700235
Brian Salomon92aee3d2016-12-21 09:20:25 -0500236 fUsesLocalCoords = optimizations.readsLocalCoords();
237 fCanTweakAlphaForCoverage = optimizations.canTweakAlphaForCoverage();
238 fCanTweakAlphaForCoverage = optimizations.canTweakAlphaForCoverage();
joshualitt9ff64252015-08-10 09:03:51 -0700239}
240
Brian Salomon6a639042016-12-14 11:08:17 -0500241void AAStrokeRectOp::onPrepareDraws(Target* target) const {
joshualitt9ff64252015-08-10 09:03:51 -0700242 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
243
bungeman06ca8ec2016-06-09 08:01:03 -0700244 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(canTweakAlphaForCoverage,
245 this->viewMatrix(),
Brian Salomon8c5bad32016-12-20 14:43:36 -0500246 this->usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700247 if (!gp) {
248 SkDebugf("Couldn't create GrGeometryProcessor\n");
249 return;
250 }
251
joshualitt9ff64252015-08-10 09:03:51 -0700252 size_t vertexStride = gp->getVertexStride();
253
Brian Salomon6a639042016-12-14 11:08:17 -0500254 SkASSERT(canTweakAlphaForCoverage
255 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
256 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700257 int innerVertexNum = 4;
258 int outerVertexNum = this->miterStroke() ? 4 : 8;
259 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
260 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500261 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700262
Hal Canary144caf52016-11-07 17:57:18 -0500263 const sk_sp<const GrBuffer> indexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500264 GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
Chris Daltonff926502017-05-03 14:36:54 -0400265 PatternHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500266 void* vertices =
267 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
268 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700269 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500270 SkDebugf("Could not allocate vertices\n");
271 return;
272 }
joshualitt9ff64252015-08-10 09:03:51 -0700273
274 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500275 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700276 this->generateAAStrokeRectGeometry(vertices,
277 i * verticesPerInstance * vertexStride,
278 vertexStride,
279 outerVertexNum,
280 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500281 info.fColor,
282 info.fDevOutside,
283 info.fDevOutsideAssist,
284 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700285 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500286 info.fDegenerate,
joshualitt9ff64252015-08-10 09:03:51 -0700287 canTweakAlphaForCoverage);
288 }
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400289 helper.recordDraw(target, gp.get(), this->pipeline());
joshualitt9ff64252015-08-10 09:03:51 -0700290}
291
Brian Salomon6a639042016-12-14 11:08:17 -0500292const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
293 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700294 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500295 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700296 static const uint16_t gMiterIndices[] = {
297 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
298 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
299 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
300 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
301
302 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
303 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
304 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
305 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
306
307 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
308 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
309 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
310 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
311 };
Brian Salomon6a639042016-12-14 11:08:17 -0500312 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700313 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
314 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400315 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500316 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
317 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700318 } else {
319 /**
320 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
321 * from the first index. The index layout:
322 * outer AA line: 0~3, 4~7
323 * outer edge: 8~11, 12~15
324 * inner edge: 16~19
325 * inner AA line: 20~23
326 * Following comes a bevel-stroke rect and its indices:
327 *
328 * 4 7
329 * *********************************
330 * * ______________________________ *
331 * * / 12 15 \ *
332 * * / \ *
333 * 0 * |8 16_____________________19 11 | * 3
334 * * | | | | *
335 * * | | **************** | | *
336 * * | | * 20 23 * | | *
337 * * | | * * | | *
338 * * | | * 21 22 * | | *
339 * * | | **************** | | *
340 * * | |____________________| | *
341 * 1 * |9 17 18 10| * 2
342 * * \ / *
343 * * \13 __________________________14/ *
344 * * *
345 * **********************************
346 * 5 6
347 */
Brian Salomon6a639042016-12-14 11:08:17 -0500348 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700349 static const uint16_t gBevelIndices[] = {
350 // Draw outer AA, from outer AA line to outer edge, shift is 0.
351 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
352 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
353 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
354 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
355 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
356 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
357 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
358 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
359
360 // Draw the stroke, from outer edge to inner edge, shift is 8.
361 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
362 1 + 8, 5 + 8, 9 + 8,
363 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
364 6 + 8, 2 + 8, 10 + 8,
365 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
366 3 + 8, 7 + 8, 11 + 8,
367 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
368 4 + 8, 0 + 8, 8 + 8,
369
370 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
371 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
372 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
373 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
374 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
375 };
Brian Salomon6a639042016-12-14 11:08:17 -0500376 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700377 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
378
379 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400380 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500381 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
382 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700383 }
384}
385
Brian Salomon6a639042016-12-14 11:08:17 -0500386bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
387 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700388
389 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
390 that->bounds(), caps)) {
joshualitt9ff64252015-08-10 09:03:51 -0700391 return false;
392 }
393
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500394 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700395 if (this->miterStroke() != that->miterStroke()) {
396 return false;
397 }
398
399 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500400 // local coords then we won't be able to combine. We could actually upload the viewmatrix
joshualitt9ff64252015-08-10 09:03:51 -0700401 // using vertex attributes in these cases, but haven't investigated that
402 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
403 return false;
404 }
405
Brian Salomon6a639042016-12-14 11:08:17 -0500406 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
407 // tweaking.
joshualitt9ff64252015-08-10 09:03:51 -0700408 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500409 fCanTweakAlphaForCoverage = false;
joshualitt9ff64252015-08-10 09:03:51 -0700410 }
411
Brian Salomon8c5bad32016-12-20 14:43:36 -0500412 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700413 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700414 return true;
415}
416
joshualitt11edad92015-09-22 10:32:28 -0700417static void setup_scale(int* scale, SkScalar inset) {
418 if (inset < SK_ScalarHalf) {
419 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
420 SkASSERT(*scale >= 0 && *scale <= 255);
421 } else {
422 *scale = 0xff;
423 }
424}
425
Brian Salomon6a639042016-12-14 11:08:17 -0500426void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
427 size_t offset,
428 size_t vertexStride,
429 int outerVertexNum,
430 int innerVertexNum,
431 GrColor color,
432 const SkRect& devOutside,
433 const SkRect& devOutsideAssist,
434 const SkRect& devInside,
435 bool miterStroke,
436 bool degenerate,
437 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700438 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
439
440 // We create vertices for four nested rectangles. There are two ramps from 0 to full
441 // coverage, one on the exterior of the stroke and the other on the interior.
442 // The following pointers refer to the four rects, from outermost to innermost.
443 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
444 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
445 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500446 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
447 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700448
449#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
450 // TODO: this only really works if the X & Y margins are the same all around
451 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700452 SkScalar inset;
453 if (!degenerate) {
454 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
455 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
456 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
457 if (miterStroke) {
458 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
459 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500460 inset = SK_ScalarHalf *
461 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700462 }
463 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700464 } else {
joshualitt11edad92015-09-22 10:32:28 -0700465 // TODO use real devRect here
466 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500467 inset = SK_ScalarHalf *
468 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700469 }
joshualitt9ff64252015-08-10 09:03:51 -0700470#else
joshualitt11edad92015-09-22 10:32:28 -0700471 SkScalar inset;
472 if (!degenerate) {
473 inset = SK_ScalarHalf;
474 } else {
475 // 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()));
joshualitt11edad92015-09-22 10:32:28 -0700479 }
joshualitt9ff64252015-08-10 09:03:51 -0700480#endif
481
482 if (miterStroke) {
483 // outermost
484 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
485 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500486 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700487 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500488 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700489 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500490 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700491 } else {
492 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500493 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
494 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
495 devInside.fBottom, vertexStride);
496 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
497 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700498 }
joshualitt9ff64252015-08-10 09:03:51 -0700499 } else {
500 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500501 SkPoint* fan1AssistPos =
502 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700503 // outermost
504 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
505 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
506 -SK_ScalarHalf);
507 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500508 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
509 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700510 if (!degenerate) {
511 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500512 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700513 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500514 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700515 } else {
516 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500517 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
518 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
519 devInside.fBottom, vertexStride);
520 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
521 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700522 }
joshualitt9ff64252015-08-10 09:03:51 -0700523 }
524
525 // Make verts point to vertex color and then set all the color and coverage vertex attrs
526 // values. The outermost rect has 0 coverage
527 verts += sizeof(SkPoint);
528 for (int i = 0; i < outerVertexNum; ++i) {
529 if (tweakAlphaForCoverage) {
530 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
531 } else {
532 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
533 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
534 }
535 }
536
537 // scale is the coverage for the the inner two rects.
538 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700539 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700540
541 float innerCoverage = GrNormalizeByteToFloat(scale);
542 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
543
544 verts += outerVertexNum * vertexStride;
545 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
546 if (tweakAlphaForCoverage) {
547 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
548 } else {
549 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700550 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700551 }
552 }
553
joshualitt11edad92015-09-22 10:32:28 -0700554 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
555 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700556 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700557 if (!degenerate) {
558 innerCoverage = 0;
559 scaledColor = 0;
560 }
561
joshualitt9ff64252015-08-10 09:03:51 -0700562 for (int i = 0; i < innerVertexNum; ++i) {
563 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700564 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700565 } else {
566 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700567 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700568 }
569 }
570}
571
Brian Salomon6a639042016-12-14 11:08:17 -0500572namespace GrAAStrokeRectOp {
joshualitt3566d442015-09-18 07:12:55 -0700573
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400574std::unique_ptr<GrLegacyMeshDrawOp> MakeFillBetweenRects(GrColor color,
575 const SkMatrix& viewMatrix,
576 const SkRect& devOutside,
577 const SkRect& devInside) {
578 return std::unique_ptr<GrLegacyMeshDrawOp>(
Brian Salomon649a3412017-03-09 13:50:43 -0500579 new AAStrokeRectOp(color, viewMatrix, devOutside, devInside));
joshualittaa37a962015-09-18 13:03:25 -0700580}
581
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400582std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color,
583 const SkMatrix& viewMatrix,
584 const SkRect& rect,
585 const SkStrokeRec& stroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500586 return AAStrokeRectOp::Make(color, viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700587}
bsalomon8b7a9e12016-07-06 13:06:22 -0700588}
joshualitt3566d442015-09-18 07:12:55 -0700589
joshualitt9ff64252015-08-10 09:03:51 -0700590///////////////////////////////////////////////////////////////////////////////////////////////////
591
Hal Canary6f6961e2017-01-31 13:50:44 -0500592#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700593
Brian Salomon5ec9def2016-12-20 15:34:05 -0500594#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700595
Brian Salomon5ec9def2016-12-20 15:34:05 -0500596DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700597 bool miterStroke = random->nextBool();
598
bsalomon40ef4852016-05-02 13:22:13 -0700599 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500600 SkRect rect =
601 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700602 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
603 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700604
joshualitt3566d442015-09-18 07:12:55 -0700605 GrColor color = GrRandomColor(random);
joshualitt9ff64252015-08-10 09:03:51 -0700606
bsalomon40ef4852016-05-02 13:22:13 -0700607 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
608 rec.setStrokeStyle(strokeWidth);
609 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500610 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700611 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Brian Salomon5ec9def2016-12-20 15:34:05 -0500612 return GrAAStrokeRectOp::Make(color, matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700613}
614
615#endif