blob: 0362946c325dc3699fa84841e091f06f4608c508 [file] [log] [blame]
joshualitt9ff64252015-08-10 09:03:51 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
joshualitt9ff64252015-08-10 09:03:51 -07008#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -05009#include "GrOpFlushState.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040010#include "GrRectOpFactory.h"
joshualitt9ff64252015-08-10 09:03:51 -070011#include "GrResourceKey.h"
12#include "GrResourceProvider.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040013#include "GrSimpleMeshDrawOpHelper.h"
Cary Clark74f623d2017-11-06 20:02:02 -050014#include "SkPointPriv.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050015#include "SkStrokeRec.h"
joshualitt9ff64252015-08-10 09:03:51 -070016
17GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
18GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
19
Brian Salomon6a639042016-12-14 11:08:17 -050020static void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, SkScalar dy) {
Cary Clark74f623d2017-11-06 20:02:02 -050021 SkPointPriv::SetRectFan(pts, r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
joshualitt9ff64252015-08-10 09:03:51 -070022}
23
bsalomon8b7a9e12016-07-06 13:06:22 -070024// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
25// limit makes a miter join effectively beveled.
26inline static bool allowed_stroke(const SkStrokeRec& stroke, bool* isMiter) {
27 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
28 stroke.getStyle() == SkStrokeRec::kHairline_Style);
29 // For hairlines, make bevel and round joins appear the same as mitered ones.
30 if (!stroke.getWidth()) {
31 *isMiter = true;
32 return true;
33 }
34 if (stroke.getJoin() == SkPaint::kBevel_Join) {
35 *isMiter = false;
36 return true;
37 }
38 if (stroke.getJoin() == SkPaint::kMiter_Join) {
39 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
40 return true;
41 }
42 return false;
43}
44
45static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
46 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
47 SkScalar strokeWidth, bool miterStroke) {
48 SkRect devRect;
49 viewMatrix.mapRect(&devRect, rect);
50
51 SkVector devStrokeSize;
52 if (strokeWidth > 0) {
53 devStrokeSize.set(strokeWidth, strokeWidth);
54 viewMatrix.mapVectors(&devStrokeSize, 1);
55 devStrokeSize.setAbs(devStrokeSize);
56 } else {
57 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
58 }
59
60 const SkScalar dx = devStrokeSize.fX;
61 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -050062 const SkScalar rx = SkScalarHalf(dx);
63 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -070064
65 *devOutside = devRect;
66 *devOutsideAssist = devRect;
67 *devInside = devRect;
68
69 devOutside->outset(rx, ry);
70 devInside->inset(rx, ry);
71
72 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
73 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
74 // together when we render these rects.
75 SkScalar spare;
76 {
77 SkScalar w = devRect.width() - dx;
78 SkScalar h = devRect.height() - dy;
79 spare = SkTMin(w, h);
80 }
81
82 *isDegenerate = spare <= 0;
83 if (*isDegenerate) {
84 devInside->fLeft = devInside->fRight = devRect.centerX();
85 devInside->fTop = devInside->fBottom = devRect.centerY();
86 }
87
88 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
89 // to draw the outside of the octagon. Because there are 8 vertices on the outer
90 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
91 if (!miterStroke) {
92 devOutside->inset(0, ry);
93 devOutsideAssist->outset(0, ry);
94 }
95}
96
Ruiqi Maob609e6d2018-07-17 10:19:38 -040097static sk_sp<GrGeometryProcessor> create_stroke_rect_gp(const GrShaderCaps* shaderCaps,
98 bool tweakAlphaForCoverage,
joshualitt9ff64252015-08-10 09:03:51 -070099 const SkMatrix& viewMatrix,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500100 bool usesLocalCoords) {
joshualitt9ff64252015-08-10 09:03:51 -0700101 using namespace GrDefaultGeoProcFactory;
102
joshualitt9ff64252015-08-10 09:03:51 -0700103 Coverage::Type coverageType;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500104 if (tweakAlphaForCoverage) {
joshualitt9ff64252015-08-10 09:03:51 -0700105 coverageType = Coverage::kSolid_Type;
106 } else {
107 coverageType = Coverage::kAttribute_Type;
108 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500109 LocalCoords::Type localCoordsType =
110 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400111 return MakeForDeviceSpace(shaderCaps,
112 Color::kPremulGrColorAttribute_Type,
113 coverageType,
114 localCoordsType,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500115 viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700116}
117
Brian Salomonbaaf4392017-06-15 09:59:23 -0400118namespace {
119
120class AAStrokeRectOp final : public GrMeshDrawOp {
121private:
122 using Helper = GrSimpleMeshDrawOpHelper;
123
joshualitt3566d442015-09-18 07:12:55 -0700124public:
Brian Salomon25a88092016-12-01 09:36:50 -0500125 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700126
Robert Phillips7c525e62018-06-12 10:11:12 -0400127 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
128 GrPaint&& paint,
129 const SkMatrix& viewMatrix,
130 const SkRect& devOutside,
131 const SkRect& devInside) {
132 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
133 devOutside, devInside);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400134 }
135
136 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
137 const SkRect& devOutside, const SkRect& devInside)
138 : INHERITED(ClassID())
139 , fHelper(helperArgs, GrAAType::kCoverage)
140 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700141 SkASSERT(!devOutside.isEmpty());
142 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700143
Brian Salomon8c5bad32016-12-20 14:43:36 -0500144 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700145 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700146 fMiterStroke = true;
147 }
148
Robert Phillips7c525e62018-06-12 10:11:12 -0400149 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
150 GrPaint&& paint,
151 const SkMatrix& viewMatrix,
152 const SkRect& rect,
153 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700154 bool isMiter;
155 if (!allowed_stroke(stroke, &isMiter)) {
156 return nullptr;
157 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400158 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
159 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400160 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700161
Brian Salomonbaaf4392017-06-15 09:59:23 -0400162 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
163 const SkRect& rect, const SkStrokeRec& stroke, bool isMiter)
164 : INHERITED(ClassID())
165 , fHelper(helperArgs, GrAAType::kCoverage)
166 , fViewMatrix(viewMatrix) {
167 fMiterStroke = isMiter;
168 RectInfo& info = fRects.push_back();
Brian Salomon8c5bad32016-12-20 14:43:36 -0500169 compute_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
170 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
171 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400172 if (isMiter) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400173 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400174 } else {
175 // The outer polygon of the bevel stroke is an octagon specified by the points of a
176 // pair of overlapping rectangles where one is wide and the other is narrow.
177 SkRect bounds = info.fDevOutside;
178 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400179 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400180 }
joshualitt3566d442015-09-18 07:12:55 -0700181 }
182
183 const char* name() const override { return "AAStrokeRect"; }
184
Robert Phillipsf1748f52017-09-14 14:11:24 -0400185 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400186 fHelper.visitProxies(func);
187 }
188
Brian Salomon7c3e7182016-12-01 09:35:30 -0500189 SkString dumpInfo() const override {
190 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500191 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500192 string.appendf(
193 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
194 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
195 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Salomon8c5bad32016-12-20 14:43:36 -0500196 info.fColor, info.fDevOutside.fLeft, info.fDevOutside.fTop,
197 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
198 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
199 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
200 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500201 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400202 string += fHelper.dumpInfo();
203 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500204 return string;
205 }
206
Brian Salomonbaaf4392017-06-15 09:59:23 -0400207 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400208
Brian Osman532b3f92018-07-11 10:02:07 -0400209 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
210 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400211 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400212 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400213
214private:
Brian Salomon91326c32017-08-09 16:02:19 -0400215 void onPrepareDraws(Target*) override;
joshualittaa37a962015-09-18 13:03:25 -0700216
joshualitt3566d442015-09-18 07:12:55 -0700217 static const int kMiterIndexCnt = 3 * 24;
218 static const int kMiterVertexCnt = 16;
219 static const int kNumMiterRectsInIndexBuffer = 256;
220
221 static const int kBevelIndexCnt = 48 + 36 + 24;
222 static const int kBevelVertexCnt = 24;
223 static const int kNumBevelRectsInIndexBuffer = 256;
224
Brian Salomond28a79d2017-10-16 13:01:07 -0400225 static sk_sp<const GrBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700226
joshualittaa37a962015-09-18 13:03:25 -0700227 const SkMatrix& viewMatrix() const { return fViewMatrix; }
228 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700229
Brian Salomon7eae3e02018-08-07 14:02:38 +0000230 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700231
232 void generateAAStrokeRectGeometry(void* vertices,
233 size_t offset,
234 size_t vertexStride,
235 int outerVertexNum,
236 int innerVertexNum,
237 GrColor color,
238 const SkRect& devOutside,
239 const SkRect& devOutsideAssist,
240 const SkRect& devInside,
241 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700242 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700243 bool tweakAlphaForCoverage) const;
244
bsalomon8b7a9e12016-07-06 13:06:22 -0700245 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500246 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700247 GrColor fColor;
248 SkRect fDevOutside;
249 SkRect fDevOutsideAssist;
250 SkRect fDevInside;
251 bool fDegenerate;
252 };
253
Brian Salomonbaaf4392017-06-15 09:59:23 -0400254 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500255 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700256 SkMatrix fViewMatrix;
257 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700258
Brian Salomonbaaf4392017-06-15 09:59:23 -0400259 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700260};
261
Brian Salomonbaaf4392017-06-15 09:59:23 -0400262} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700263
Brian Salomon91326c32017-08-09 16:02:19 -0400264void AAStrokeRectOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400265 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(target->caps().shaderCaps(),
266 fHelper.compatibleWithAlphaAsCoverage(),
bungeman06ca8ec2016-06-09 08:01:03 -0700267 this->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400268 fHelper.usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700269 if (!gp) {
270 SkDebugf("Couldn't create GrGeometryProcessor\n");
271 return;
272 }
273
Brian Salomon92be2f72018-06-19 14:33:47 -0400274 size_t vertexStride = fHelper.compatibleWithAlphaAsCoverage()
275 ? sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
276 : sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr);
joshualitt9ff64252015-08-10 09:03:51 -0700277
Brian Salomon92be2f72018-06-19 14:33:47 -0400278 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
joshualitt9ff64252015-08-10 09:03:51 -0700279 int innerVertexNum = 4;
280 int outerVertexNum = this->miterStroke() ? 4 : 8;
281 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
282 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500283 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700284
Brian Salomon7eae3e02018-08-07 14:02:38 +0000285 sk_sp<const GrBuffer> indexBuffer =
286 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
287 PatternHelper helper(target, GrPrimitiveType::kTriangles, vertexStride, indexBuffer.get(),
288 verticesPerInstance, indicesPerInstance, instanceCount);
289 void* vertices = helper.vertices();
joshualitt9ff64252015-08-10 09:03:51 -0700290 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500291 SkDebugf("Could not allocate vertices\n");
292 return;
293 }
joshualitt9ff64252015-08-10 09:03:51 -0700294
295 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500296 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700297 this->generateAAStrokeRectGeometry(vertices,
298 i * verticesPerInstance * vertexStride,
299 vertexStride,
300 outerVertexNum,
301 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500302 info.fColor,
303 info.fDevOutside,
304 info.fDevOutsideAssist,
305 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700306 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500307 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400308 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700309 }
Brian Salomon49348902018-06-26 09:12:38 -0400310 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000311 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt9ff64252015-08-10 09:03:51 -0700312}
313
Brian Salomond28a79d2017-10-16 13:01:07 -0400314sk_sp<const GrBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
315 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700316 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500317 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700318 static const uint16_t gMiterIndices[] = {
319 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
320 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
321 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
322 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
323
324 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
325 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
326 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
327 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
328
329 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
330 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
331 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
332 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
333 };
Brian Salomon6a639042016-12-14 11:08:17 -0500334 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700335 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
336 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400337 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500338 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
339 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700340 } else {
341 /**
342 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
343 * from the first index. The index layout:
344 * outer AA line: 0~3, 4~7
345 * outer edge: 8~11, 12~15
346 * inner edge: 16~19
347 * inner AA line: 20~23
348 * Following comes a bevel-stroke rect and its indices:
349 *
350 * 4 7
351 * *********************************
352 * * ______________________________ *
353 * * / 12 15 \ *
354 * * / \ *
355 * 0 * |8 16_____________________19 11 | * 3
356 * * | | | | *
357 * * | | **************** | | *
358 * * | | * 20 23 * | | *
359 * * | | * * | | *
360 * * | | * 21 22 * | | *
361 * * | | **************** | | *
362 * * | |____________________| | *
363 * 1 * |9 17 18 10| * 2
364 * * \ / *
365 * * \13 __________________________14/ *
366 * * *
367 * **********************************
368 * 5 6
369 */
Brian Salomon6a639042016-12-14 11:08:17 -0500370 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700371 static const uint16_t gBevelIndices[] = {
372 // Draw outer AA, from outer AA line to outer edge, shift is 0.
373 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
374 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
375 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
376 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
377 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
378 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
379 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
380 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
381
382 // Draw the stroke, from outer edge to inner edge, shift is 8.
383 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
384 1 + 8, 5 + 8, 9 + 8,
385 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
386 6 + 8, 2 + 8, 10 + 8,
387 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
388 3 + 8, 7 + 8, 11 + 8,
389 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
390 4 + 8, 0 + 8, 8 + 8,
391
392 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
393 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
394 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
395 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
396 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
397 };
Brian Salomon6a639042016-12-14 11:08:17 -0500398 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700399 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
400
401 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400402 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500403 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
404 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700405 }
406}
407
Brian Salomon7eae3e02018-08-07 14:02:38 +0000408GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500409 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700410
Brian Salomonbaaf4392017-06-15 09:59:23 -0400411 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000412 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700413 }
414
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500415 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700416 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000417 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700418 }
419
420 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400421 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
422 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000423 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700424 }
425
Brian Salomon8c5bad32016-12-20 14:43:36 -0500426 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700427 this->joinBounds(*that);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000428 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700429}
430
joshualitt11edad92015-09-22 10:32:28 -0700431static void setup_scale(int* scale, SkScalar inset) {
432 if (inset < SK_ScalarHalf) {
433 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
434 SkASSERT(*scale >= 0 && *scale <= 255);
435 } else {
436 *scale = 0xff;
437 }
438}
439
Brian Salomon6a639042016-12-14 11:08:17 -0500440void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
441 size_t offset,
442 size_t vertexStride,
443 int outerVertexNum,
444 int innerVertexNum,
445 GrColor color,
446 const SkRect& devOutside,
447 const SkRect& devOutsideAssist,
448 const SkRect& devInside,
449 bool miterStroke,
450 bool degenerate,
451 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700452 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
453
454 // We create vertices for four nested rectangles. There are two ramps from 0 to full
455 // coverage, one on the exterior of the stroke and the other on the interior.
456 // The following pointers refer to the four rects, from outermost to innermost.
457 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
458 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
459 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500460 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
461 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700462
463#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
464 // TODO: this only really works if the X & Y margins are the same all around
465 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700466 SkScalar inset;
467 if (!degenerate) {
468 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
469 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
470 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
471 if (miterStroke) {
472 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
473 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500474 inset = SK_ScalarHalf *
475 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700476 }
477 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700478 } else {
joshualitt11edad92015-09-22 10:32:28 -0700479 // TODO use real devRect here
480 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500481 inset = SK_ScalarHalf *
482 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700483 }
joshualitt9ff64252015-08-10 09:03:51 -0700484#else
joshualitt11edad92015-09-22 10:32:28 -0700485 SkScalar inset;
486 if (!degenerate) {
487 inset = SK_ScalarHalf;
488 } else {
489 // TODO use real devRect here
490 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500491 inset = SK_ScalarHalf *
492 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700493 }
joshualitt9ff64252015-08-10 09:03:51 -0700494#endif
495
496 if (miterStroke) {
497 // outermost
498 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
499 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500500 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700501 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500502 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700503 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500504 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700505 } else {
506 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500507 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500508 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500509 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500510 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500511 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700512 }
joshualitt9ff64252015-08-10 09:03:51 -0700513 } else {
514 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500515 SkPoint* fan1AssistPos =
516 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700517 // outermost
518 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
519 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
520 -SK_ScalarHalf);
521 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500522 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
523 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700524 if (!degenerate) {
525 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500526 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700527 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500528 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700529 } else {
530 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500531 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500532 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500533 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500534 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500535 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700536 }
joshualitt9ff64252015-08-10 09:03:51 -0700537 }
538
539 // Make verts point to vertex color and then set all the color and coverage vertex attrs
540 // values. The outermost rect has 0 coverage
541 verts += sizeof(SkPoint);
542 for (int i = 0; i < outerVertexNum; ++i) {
543 if (tweakAlphaForCoverage) {
544 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
545 } else {
546 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
547 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
548 }
549 }
550
551 // scale is the coverage for the the inner two rects.
552 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700553 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700554
555 float innerCoverage = GrNormalizeByteToFloat(scale);
556 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
557
558 verts += outerVertexNum * vertexStride;
559 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
560 if (tweakAlphaForCoverage) {
561 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
562 } 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
joshualitt11edad92015-09-22 10:32:28 -0700568 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
569 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700570 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700571 if (!degenerate) {
572 innerCoverage = 0;
573 scaledColor = 0;
574 }
575
joshualitt9ff64252015-08-10 09:03:51 -0700576 for (int i = 0; i < innerVertexNum; ++i) {
577 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700578 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700579 } else {
580 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700581 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700582 }
583 }
584}
585
Brian Salomonbaaf4392017-06-15 09:59:23 -0400586namespace GrRectOpFactory {
joshualitt3566d442015-09-18 07:12:55 -0700587
Robert Phillips7c525e62018-06-12 10:11:12 -0400588std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrContext* context,
589 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400590 const SkMatrix& viewMatrix,
591 const SkRect rects[2]) {
592 SkASSERT(viewMatrix.rectStaysRect());
593 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
594
595 SkRect devOutside, devInside;
596 viewMatrix.mapRect(&devOutside, rects[0]);
597 viewMatrix.mapRect(&devInside, rects[1]);
598 if (devInside.isEmpty()) {
599 if (devOutside.isEmpty()) {
600 return nullptr;
601 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400602 return MakeAAFill(context, std::move(paint), viewMatrix, rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400603 }
604
Robert Phillips7c525e62018-06-12 10:11:12 -0400605 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700606}
607
Robert Phillips7c525e62018-06-12 10:11:12 -0400608std::unique_ptr<GrDrawOp> MakeAAStroke(GrContext* context,
609 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400610 const SkMatrix& viewMatrix,
611 const SkRect& rect,
612 const SkStrokeRec& stroke) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400613 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700614}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400615
616} // namespace GrRectOpFactory
joshualitt3566d442015-09-18 07:12:55 -0700617
joshualitt9ff64252015-08-10 09:03:51 -0700618///////////////////////////////////////////////////////////////////////////////////////////////////
619
Hal Canary6f6961e2017-01-31 13:50:44 -0500620#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700621
Brian Salomon5ec9def2016-12-20 15:34:05 -0500622#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700623
Brian Salomonbaaf4392017-06-15 09:59:23 -0400624GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700625 bool miterStroke = random->nextBool();
626
bsalomon40ef4852016-05-02 13:22:13 -0700627 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500628 SkRect rect =
629 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700630 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
631 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700632
bsalomon40ef4852016-05-02 13:22:13 -0700633 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
634 rec.setStrokeStyle(strokeWidth);
635 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500636 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700637 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Robert Phillips7c525e62018-06-12 10:11:12 -0400638 return GrRectOpFactory::MakeAAStroke(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700639}
640
641#endif