blob: a6a1f4851dae543fc5b32f56af29f5c8caceb04b [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
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400174 void visitProxies(VisitProxyFunc func) const override {
175 fHelper.visitProxies(func);
176 }
177
Brian Salomon7c3e7182016-12-01 09:35:30 -0500178 SkString dumpInfo() const override {
179 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500180 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500181 string.appendf(
182 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
183 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
184 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Salomon8c5bad32016-12-20 14:43:36 -0500185 info.fColor, info.fDevOutside.fLeft, info.fDevOutside.fTop,
186 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
187 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
188 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
189 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500190 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400191 string += fHelper.dumpInfo();
192 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500193 return string;
194 }
195
Brian Salomonbaaf4392017-06-15 09:59:23 -0400196 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400197
Brian Salomonf86d37b2017-06-16 10:04:34 -0400198 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400199 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
200 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400201 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400202
203private:
Brian Salomon91326c32017-08-09 16:02:19 -0400204 void onPrepareDraws(Target*) override;
joshualittaa37a962015-09-18 13:03:25 -0700205
joshualitt3566d442015-09-18 07:12:55 -0700206 static const int kMiterIndexCnt = 3 * 24;
207 static const int kMiterVertexCnt = 16;
208 static const int kNumMiterRectsInIndexBuffer = 256;
209
210 static const int kBevelIndexCnt = 48 + 36 + 24;
211 static const int kBevelVertexCnt = 24;
212 static const int kNumBevelRectsInIndexBuffer = 256;
213
cdalton397536c2016-03-25 12:15:03 -0700214 static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700215
joshualittaa37a962015-09-18 13:03:25 -0700216 const SkMatrix& viewMatrix() const { return fViewMatrix; }
217 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700218
Brian Salomon25a88092016-12-01 09:36:50 -0500219 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700220
221 void generateAAStrokeRectGeometry(void* vertices,
222 size_t offset,
223 size_t vertexStride,
224 int outerVertexNum,
225 int innerVertexNum,
226 GrColor color,
227 const SkRect& devOutside,
228 const SkRect& devOutsideAssist,
229 const SkRect& devInside,
230 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700231 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700232 bool tweakAlphaForCoverage) const;
233
bsalomon8b7a9e12016-07-06 13:06:22 -0700234 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500235 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700236 GrColor fColor;
237 SkRect fDevOutside;
238 SkRect fDevOutsideAssist;
239 SkRect fDevInside;
240 bool fDegenerate;
241 };
242
Brian Salomonbaaf4392017-06-15 09:59:23 -0400243 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500244 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700245 SkMatrix fViewMatrix;
246 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700247
Brian Salomonbaaf4392017-06-15 09:59:23 -0400248 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700249};
250
Brian Salomonbaaf4392017-06-15 09:59:23 -0400251} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700252
Brian Salomon91326c32017-08-09 16:02:19 -0400253void AAStrokeRectOp::onPrepareDraws(Target* target) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400254 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(fHelper.compatibleWithAlphaAsCoverage(),
bungeman06ca8ec2016-06-09 08:01:03 -0700255 this->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400256 fHelper.usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700257 if (!gp) {
258 SkDebugf("Couldn't create GrGeometryProcessor\n");
259 return;
260 }
261
joshualitt9ff64252015-08-10 09:03:51 -0700262 size_t vertexStride = gp->getVertexStride();
263
Brian Salomonbaaf4392017-06-15 09:59:23 -0400264 SkASSERT(fHelper.compatibleWithAlphaAsCoverage()
Brian Salomon6a639042016-12-14 11:08:17 -0500265 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
266 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700267 int innerVertexNum = 4;
268 int outerVertexNum = this->miterStroke() ? 4 : 8;
269 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
270 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500271 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700272
Hal Canary144caf52016-11-07 17:57:18 -0500273 const sk_sp<const GrBuffer> indexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500274 GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
Chris Dalton3809bab2017-06-13 10:55:06 -0600275 PatternHelper helper(GrPrimitiveType::kTriangles);
Brian Salomon6a639042016-12-14 11:08:17 -0500276 void* vertices =
Chris Daltonbca46e22017-05-15 11:03:26 -0600277 helper.init(target, vertexStride, indexBuffer.get(),
Brian Salomon6a639042016-12-14 11:08:17 -0500278 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700279 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500280 SkDebugf("Could not allocate vertices\n");
281 return;
282 }
joshualitt9ff64252015-08-10 09:03:51 -0700283
284 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500285 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700286 this->generateAAStrokeRectGeometry(vertices,
287 i * verticesPerInstance * vertexStride,
288 vertexStride,
289 outerVertexNum,
290 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500291 info.fColor,
292 info.fDevOutside,
293 info.fDevOutsideAssist,
294 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700295 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500296 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400297 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700298 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400299 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
joshualitt9ff64252015-08-10 09:03:51 -0700300}
301
Brian Salomon6a639042016-12-14 11:08:17 -0500302const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
303 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700304 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500305 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700306 static const uint16_t gMiterIndices[] = {
307 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
308 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
309 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
310 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
311
312 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
313 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
314 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
315 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
316
317 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
318 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
319 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
320 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
321 };
Brian Salomon6a639042016-12-14 11:08:17 -0500322 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700323 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
324 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400325 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500326 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
327 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700328 } else {
329 /**
330 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
331 * from the first index. The index layout:
332 * outer AA line: 0~3, 4~7
333 * outer edge: 8~11, 12~15
334 * inner edge: 16~19
335 * inner AA line: 20~23
336 * Following comes a bevel-stroke rect and its indices:
337 *
338 * 4 7
339 * *********************************
340 * * ______________________________ *
341 * * / 12 15 \ *
342 * * / \ *
343 * 0 * |8 16_____________________19 11 | * 3
344 * * | | | | *
345 * * | | **************** | | *
346 * * | | * 20 23 * | | *
347 * * | | * * | | *
348 * * | | * 21 22 * | | *
349 * * | | **************** | | *
350 * * | |____________________| | *
351 * 1 * |9 17 18 10| * 2
352 * * \ / *
353 * * \13 __________________________14/ *
354 * * *
355 * **********************************
356 * 5 6
357 */
Brian Salomon6a639042016-12-14 11:08:17 -0500358 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700359 static const uint16_t gBevelIndices[] = {
360 // Draw outer AA, from outer AA line to outer edge, shift is 0.
361 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
362 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
363 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
364 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
365 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
366 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
367 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
368 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
369
370 // Draw the stroke, from outer edge to inner edge, shift is 8.
371 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
372 1 + 8, 5 + 8, 9 + 8,
373 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
374 6 + 8, 2 + 8, 10 + 8,
375 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
376 3 + 8, 7 + 8, 11 + 8,
377 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
378 4 + 8, 0 + 8, 8 + 8,
379
380 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
381 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
382 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
383 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
384 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
385 };
Brian Salomon6a639042016-12-14 11:08:17 -0500386 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700387 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
388
389 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400390 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500391 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
392 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700393 }
394}
395
Brian Salomon6a639042016-12-14 11:08:17 -0500396bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
397 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700398
Brian Salomonbaaf4392017-06-15 09:59:23 -0400399 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt9ff64252015-08-10 09:03:51 -0700400 return false;
401 }
402
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500403 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700404 if (this->miterStroke() != that->miterStroke()) {
405 return false;
406 }
407
408 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400409 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
410 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt9ff64252015-08-10 09:03:51 -0700411 return false;
412 }
413
Brian Salomon8c5bad32016-12-20 14:43:36 -0500414 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700415 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700416 return true;
417}
418
joshualitt11edad92015-09-22 10:32:28 -0700419static void setup_scale(int* scale, SkScalar inset) {
420 if (inset < SK_ScalarHalf) {
421 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
422 SkASSERT(*scale >= 0 && *scale <= 255);
423 } else {
424 *scale = 0xff;
425 }
426}
427
Brian Salomon6a639042016-12-14 11:08:17 -0500428void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
429 size_t offset,
430 size_t vertexStride,
431 int outerVertexNum,
432 int innerVertexNum,
433 GrColor color,
434 const SkRect& devOutside,
435 const SkRect& devOutsideAssist,
436 const SkRect& devInside,
437 bool miterStroke,
438 bool degenerate,
439 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700440 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
441
442 // We create vertices for four nested rectangles. There are two ramps from 0 to full
443 // coverage, one on the exterior of the stroke and the other on the interior.
444 // The following pointers refer to the four rects, from outermost to innermost.
445 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
446 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
447 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500448 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
449 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700450
451#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
452 // TODO: this only really works if the X & Y margins are the same all around
453 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700454 SkScalar inset;
455 if (!degenerate) {
456 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
457 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
458 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
459 if (miterStroke) {
460 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
461 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500462 inset = SK_ScalarHalf *
463 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700464 }
465 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700466 } else {
joshualitt11edad92015-09-22 10:32:28 -0700467 // TODO use real devRect here
468 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500469 inset = SK_ScalarHalf *
470 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700471 }
joshualitt9ff64252015-08-10 09:03:51 -0700472#else
joshualitt11edad92015-09-22 10:32:28 -0700473 SkScalar inset;
474 if (!degenerate) {
475 inset = SK_ScalarHalf;
476 } else {
477 // TODO use real devRect here
478 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500479 inset = SK_ScalarHalf *
480 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700481 }
joshualitt9ff64252015-08-10 09:03:51 -0700482#endif
483
484 if (miterStroke) {
485 // outermost
486 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
487 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500488 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700489 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500490 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700491 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500492 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700493 } else {
494 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500495 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
496 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
497 devInside.fBottom, vertexStride);
498 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
499 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700500 }
joshualitt9ff64252015-08-10 09:03:51 -0700501 } else {
502 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500503 SkPoint* fan1AssistPos =
504 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700505 // outermost
506 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
507 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
508 -SK_ScalarHalf);
509 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500510 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
511 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700512 if (!degenerate) {
513 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500514 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700515 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500516 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700517 } else {
518 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500519 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
520 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
521 devInside.fBottom, vertexStride);
522 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
523 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700524 }
joshualitt9ff64252015-08-10 09:03:51 -0700525 }
526
527 // Make verts point to vertex color and then set all the color and coverage vertex attrs
528 // values. The outermost rect has 0 coverage
529 verts += sizeof(SkPoint);
530 for (int i = 0; i < outerVertexNum; ++i) {
531 if (tweakAlphaForCoverage) {
532 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
533 } else {
534 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
535 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
536 }
537 }
538
539 // scale is the coverage for the the inner two rects.
540 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700541 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700542
543 float innerCoverage = GrNormalizeByteToFloat(scale);
544 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
545
546 verts += outerVertexNum * vertexStride;
547 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
548 if (tweakAlphaForCoverage) {
549 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
550 } else {
551 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700552 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700553 }
554 }
555
joshualitt11edad92015-09-22 10:32:28 -0700556 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
557 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700558 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700559 if (!degenerate) {
560 innerCoverage = 0;
561 scaledColor = 0;
562 }
563
joshualitt9ff64252015-08-10 09:03:51 -0700564 for (int i = 0; i < innerVertexNum; ++i) {
565 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700566 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700567 } else {
568 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700569 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700570 }
571 }
572}
573
Brian Salomonbaaf4392017-06-15 09:59:23 -0400574namespace GrRectOpFactory {
joshualitt3566d442015-09-18 07:12:55 -0700575
Brian Salomonbaaf4392017-06-15 09:59:23 -0400576std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrPaint&& paint,
577 const SkMatrix& viewMatrix,
578 const SkRect rects[2]) {
579 SkASSERT(viewMatrix.rectStaysRect());
580 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
581
582 SkRect devOutside, devInside;
583 viewMatrix.mapRect(&devOutside, rects[0]);
584 viewMatrix.mapRect(&devInside, rects[1]);
585 if (devInside.isEmpty()) {
586 if (devOutside.isEmpty()) {
587 return nullptr;
588 }
Brian Salomon35186b52017-06-16 09:57:06 -0400589 return MakeAAFill(std::move(paint), viewMatrix, rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400590 }
591
592 return AAStrokeRectOp::Make(std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700593}
594
Brian Salomonbaaf4392017-06-15 09:59:23 -0400595std::unique_ptr<GrDrawOp> MakeAAStroke(GrPaint&& paint,
596 const SkMatrix& viewMatrix,
597 const SkRect& rect,
598 const SkStrokeRec& stroke) {
599 return AAStrokeRectOp::Make(std::move(paint), viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700600}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400601
602} // namespace GrRectOpFactory
joshualitt3566d442015-09-18 07:12:55 -0700603
joshualitt9ff64252015-08-10 09:03:51 -0700604///////////////////////////////////////////////////////////////////////////////////////////////////
605
Hal Canary6f6961e2017-01-31 13:50:44 -0500606#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700607
Brian Salomon5ec9def2016-12-20 15:34:05 -0500608#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700609
Brian Salomonbaaf4392017-06-15 09:59:23 -0400610GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700611 bool miterStroke = random->nextBool();
612
bsalomon40ef4852016-05-02 13:22:13 -0700613 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500614 SkRect rect =
615 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700616 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
617 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700618
bsalomon40ef4852016-05-02 13:22:13 -0700619 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
620 rec.setStrokeStyle(strokeWidth);
621 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500622 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700623 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400624 return GrRectOpFactory::MakeAAStroke(std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700625}
626
627#endif