blob: b684782f2375706318f6903e9bd7f39a3f6ab55d [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 Salomon6a639042016-12-14 11:08:17 -0500113class AAStrokeRectOp final : public GrMeshDrawOp {
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 Salomon649a3412017-03-09 13:50:43 -0500128 static std::unique_ptr<GrMeshDrawOp> 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 Salomon649a3412017-03-09 13:50:43 -0500151 return std::unique_ptr<GrMeshDrawOp>(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 Salomon5298dc82017-02-22 11:52:03 -0500177 void getFragmentProcessorAnalysisInputs(FragmentProcessorAnalysisInputs* input) const override {
178 input->colorInput()->setToConstant(fRects[0].fColor);
179 input->coverageInput()->setToUnknown();
Brian Salomon92aee3d2016-12-21 09:20:25 -0500180 }
181 void applyPipelineOptimizations(const GrPipelineOptimizations&) override;
joshualitt144c3c82015-11-30 12:30:13 -0800182 void onPrepareDraws(Target*) const override;
joshualittaa37a962015-09-18 13:03:25 -0700183
joshualitt3566d442015-09-18 07:12:55 -0700184 static const int kMiterIndexCnt = 3 * 24;
185 static const int kMiterVertexCnt = 16;
186 static const int kNumMiterRectsInIndexBuffer = 256;
187
188 static const int kBevelIndexCnt = 48 + 36 + 24;
189 static const int kBevelVertexCnt = 24;
190 static const int kNumBevelRectsInIndexBuffer = 256;
191
cdalton397536c2016-03-25 12:15:03 -0700192 static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700193
Brian Salomon8c5bad32016-12-20 14:43:36 -0500194 bool usesLocalCoords() const { return fUsesLocalCoords; }
195 bool canTweakAlphaForCoverage() const { return fCanTweakAlphaForCoverage; }
joshualittaa37a962015-09-18 13:03:25 -0700196 const SkMatrix& viewMatrix() const { return fViewMatrix; }
197 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700198
Brian Salomon25a88092016-12-01 09:36:50 -0500199 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700200
201 void generateAAStrokeRectGeometry(void* vertices,
202 size_t offset,
203 size_t vertexStride,
204 int outerVertexNum,
205 int innerVertexNum,
206 GrColor color,
207 const SkRect& devOutside,
208 const SkRect& devOutsideAssist,
209 const SkRect& devInside,
210 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700211 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700212 bool tweakAlphaForCoverage) const;
213
bsalomon8b7a9e12016-07-06 13:06:22 -0700214 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500215 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700216 GrColor fColor;
217 SkRect fDevOutside;
218 SkRect fDevOutsideAssist;
219 SkRect fDevInside;
220 bool fDegenerate;
221 };
222
Brian Salomon8c5bad32016-12-20 14:43:36 -0500223 SkSTArray<1, RectInfo, true> fRects;
224 bool fUsesLocalCoords;
225 bool fCanTweakAlphaForCoverage;
joshualittaa37a962015-09-18 13:03:25 -0700226 SkMatrix fViewMatrix;
227 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700228
Brian Salomondad29232016-12-01 16:40:24 -0500229 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700230};
231
Brian Salomon92aee3d2016-12-21 09:20:25 -0500232void AAStrokeRectOp::applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500233 optimizations.getOverrideColorIfSet(&fRects[0].fColor);
joshualitt9ff64252015-08-10 09:03:51 -0700234
Brian Salomon92aee3d2016-12-21 09:20:25 -0500235 fUsesLocalCoords = optimizations.readsLocalCoords();
236 fCanTweakAlphaForCoverage = optimizations.canTweakAlphaForCoverage();
237 fCanTweakAlphaForCoverage = optimizations.canTweakAlphaForCoverage();
joshualitt9ff64252015-08-10 09:03:51 -0700238}
239
Brian Salomon6a639042016-12-14 11:08:17 -0500240void AAStrokeRectOp::onPrepareDraws(Target* target) const {
joshualitt9ff64252015-08-10 09:03:51 -0700241 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
242
bungeman06ca8ec2016-06-09 08:01:03 -0700243 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(canTweakAlphaForCoverage,
244 this->viewMatrix(),
Brian Salomon8c5bad32016-12-20 14:43:36 -0500245 this->usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700246 if (!gp) {
247 SkDebugf("Couldn't create GrGeometryProcessor\n");
248 return;
249 }
250
joshualitt9ff64252015-08-10 09:03:51 -0700251 size_t vertexStride = gp->getVertexStride();
252
Brian Salomon6a639042016-12-14 11:08:17 -0500253 SkASSERT(canTweakAlphaForCoverage
254 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
255 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700256 int innerVertexNum = 4;
257 int outerVertexNum = this->miterStroke() ? 4 : 8;
258 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
259 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500260 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700261
Hal Canary144caf52016-11-07 17:57:18 -0500262 const sk_sp<const GrBuffer> indexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500263 GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
joshualitt9ff64252015-08-10 09:03:51 -0700264 InstancedHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500265 void* vertices =
266 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
267 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700268 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500269 SkDebugf("Could not allocate vertices\n");
270 return;
271 }
joshualitt9ff64252015-08-10 09:03:51 -0700272
273 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500274 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700275 this->generateAAStrokeRectGeometry(vertices,
276 i * verticesPerInstance * vertexStride,
277 vertexStride,
278 outerVertexNum,
279 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500280 info.fColor,
281 info.fDevOutside,
282 info.fDevOutsideAssist,
283 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700284 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500285 info.fDegenerate,
joshualitt9ff64252015-08-10 09:03:51 -0700286 canTweakAlphaForCoverage);
287 }
bungeman06ca8ec2016-06-09 08:01:03 -0700288 helper.recordDraw(target, gp.get());
joshualitt9ff64252015-08-10 09:03:51 -0700289}
290
Brian Salomon6a639042016-12-14 11:08:17 -0500291const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
292 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700293 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500294 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700295 static const uint16_t gMiterIndices[] = {
296 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
297 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
298 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
299 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
300
301 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
302 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
303 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
304 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
305
306 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
307 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
308 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
309 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
310 };
Brian Salomon6a639042016-12-14 11:08:17 -0500311 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700312 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
313 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Brian Salomon6a639042016-12-14 11:08:17 -0500314 return resourceProvider->findOrCreateInstancedIndexBuffer(
315 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
316 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700317 } else {
318 /**
319 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
320 * from the first index. The index layout:
321 * outer AA line: 0~3, 4~7
322 * outer edge: 8~11, 12~15
323 * inner edge: 16~19
324 * inner AA line: 20~23
325 * Following comes a bevel-stroke rect and its indices:
326 *
327 * 4 7
328 * *********************************
329 * * ______________________________ *
330 * * / 12 15 \ *
331 * * / \ *
332 * 0 * |8 16_____________________19 11 | * 3
333 * * | | | | *
334 * * | | **************** | | *
335 * * | | * 20 23 * | | *
336 * * | | * * | | *
337 * * | | * 21 22 * | | *
338 * * | | **************** | | *
339 * * | |____________________| | *
340 * 1 * |9 17 18 10| * 2
341 * * \ / *
342 * * \13 __________________________14/ *
343 * * *
344 * **********************************
345 * 5 6
346 */
Brian Salomon6a639042016-12-14 11:08:17 -0500347 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700348 static const uint16_t gBevelIndices[] = {
349 // Draw outer AA, from outer AA line to outer edge, shift is 0.
350 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
351 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
352 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
353 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
354 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
355 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
356 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
357 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
358
359 // Draw the stroke, from outer edge to inner edge, shift is 8.
360 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
361 1 + 8, 5 + 8, 9 + 8,
362 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
363 6 + 8, 2 + 8, 10 + 8,
364 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
365 3 + 8, 7 + 8, 11 + 8,
366 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
367 4 + 8, 0 + 8, 8 + 8,
368
369 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
370 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
371 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
372 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
373 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
374 };
Brian Salomon6a639042016-12-14 11:08:17 -0500375 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700376 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
377
378 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Brian Salomon6a639042016-12-14 11:08:17 -0500379 return resourceProvider->findOrCreateInstancedIndexBuffer(
380 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
381 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700382 }
383}
384
Brian Salomon6a639042016-12-14 11:08:17 -0500385bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
386 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700387
388 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
389 that->bounds(), caps)) {
joshualitt9ff64252015-08-10 09:03:51 -0700390 return false;
391 }
392
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500393 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700394 if (this->miterStroke() != that->miterStroke()) {
395 return false;
396 }
397
398 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500399 // local coords then we won't be able to combine. We could actually upload the viewmatrix
joshualitt9ff64252015-08-10 09:03:51 -0700400 // using vertex attributes in these cases, but haven't investigated that
401 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
402 return false;
403 }
404
Brian Salomon6a639042016-12-14 11:08:17 -0500405 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
406 // tweaking.
joshualitt9ff64252015-08-10 09:03:51 -0700407 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500408 fCanTweakAlphaForCoverage = false;
joshualitt9ff64252015-08-10 09:03:51 -0700409 }
410
Brian Salomon8c5bad32016-12-20 14:43:36 -0500411 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700412 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700413 return true;
414}
415
joshualitt11edad92015-09-22 10:32:28 -0700416static void setup_scale(int* scale, SkScalar inset) {
417 if (inset < SK_ScalarHalf) {
418 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
419 SkASSERT(*scale >= 0 && *scale <= 255);
420 } else {
421 *scale = 0xff;
422 }
423}
424
Brian Salomon6a639042016-12-14 11:08:17 -0500425void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
426 size_t offset,
427 size_t vertexStride,
428 int outerVertexNum,
429 int innerVertexNum,
430 GrColor color,
431 const SkRect& devOutside,
432 const SkRect& devOutsideAssist,
433 const SkRect& devInside,
434 bool miterStroke,
435 bool degenerate,
436 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700437 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
438
439 // We create vertices for four nested rectangles. There are two ramps from 0 to full
440 // coverage, one on the exterior of the stroke and the other on the interior.
441 // The following pointers refer to the four rects, from outermost to innermost.
442 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
443 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
444 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500445 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
446 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700447
448#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
449 // TODO: this only really works if the X & Y margins are the same all around
450 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700451 SkScalar inset;
452 if (!degenerate) {
453 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
454 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
455 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
456 if (miterStroke) {
457 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
458 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500459 inset = SK_ScalarHalf *
460 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700461 }
462 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700463 } else {
joshualitt11edad92015-09-22 10:32:28 -0700464 // TODO use real devRect here
465 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500466 inset = SK_ScalarHalf *
467 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700468 }
joshualitt9ff64252015-08-10 09:03:51 -0700469#else
joshualitt11edad92015-09-22 10:32:28 -0700470 SkScalar inset;
471 if (!degenerate) {
472 inset = SK_ScalarHalf;
473 } else {
474 // TODO use real devRect here
475 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500476 inset = SK_ScalarHalf *
477 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700478 }
joshualitt9ff64252015-08-10 09:03:51 -0700479#endif
480
481 if (miterStroke) {
482 // outermost
483 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
484 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500485 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700486 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500487 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700488 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500489 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700490 } else {
491 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500492 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
493 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
494 devInside.fBottom, vertexStride);
495 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
496 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700497 }
joshualitt9ff64252015-08-10 09:03:51 -0700498 } else {
499 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500500 SkPoint* fan1AssistPos =
501 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700502 // outermost
503 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
504 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
505 -SK_ScalarHalf);
506 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500507 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
508 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700509 if (!degenerate) {
510 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500511 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700512 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500513 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700514 } else {
515 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500516 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
517 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
518 devInside.fBottom, vertexStride);
519 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
520 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700521 }
joshualitt9ff64252015-08-10 09:03:51 -0700522 }
523
524 // Make verts point to vertex color and then set all the color and coverage vertex attrs
525 // values. The outermost rect has 0 coverage
526 verts += sizeof(SkPoint);
527 for (int i = 0; i < outerVertexNum; ++i) {
528 if (tweakAlphaForCoverage) {
529 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
530 } else {
531 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
532 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
533 }
534 }
535
536 // scale is the coverage for the the inner two rects.
537 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700538 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700539
540 float innerCoverage = GrNormalizeByteToFloat(scale);
541 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
542
543 verts += outerVertexNum * vertexStride;
544 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
545 if (tweakAlphaForCoverage) {
546 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
547 } else {
548 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700549 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700550 }
551 }
552
joshualitt11edad92015-09-22 10:32:28 -0700553 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
554 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700555 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700556 if (!degenerate) {
557 innerCoverage = 0;
558 scaledColor = 0;
559 }
560
joshualitt9ff64252015-08-10 09:03:51 -0700561 for (int i = 0; i < innerVertexNum; ++i) {
562 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700563 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700564 } else {
565 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700566 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700567 }
568 }
569}
570
Brian Salomon6a639042016-12-14 11:08:17 -0500571namespace GrAAStrokeRectOp {
joshualitt3566d442015-09-18 07:12:55 -0700572
Brian Salomon649a3412017-03-09 13:50:43 -0500573std::unique_ptr<GrMeshDrawOp> MakeFillBetweenRects(GrColor color,
574 const SkMatrix& viewMatrix,
575 const SkRect& devOutside,
576 const SkRect& devInside) {
577 return std::unique_ptr<GrMeshDrawOp>(
578 new AAStrokeRectOp(color, viewMatrix, devOutside, devInside));
joshualittaa37a962015-09-18 13:03:25 -0700579}
580
Brian Salomon649a3412017-03-09 13:50:43 -0500581std::unique_ptr<GrMeshDrawOp> Make(GrColor color,
582 const SkMatrix& viewMatrix,
583 const SkRect& rect,
584 const SkStrokeRec& stroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500585 return AAStrokeRectOp::Make(color, viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700586}
bsalomon8b7a9e12016-07-06 13:06:22 -0700587}
joshualitt3566d442015-09-18 07:12:55 -0700588
joshualitt9ff64252015-08-10 09:03:51 -0700589///////////////////////////////////////////////////////////////////////////////////////////////////
590
Hal Canary6f6961e2017-01-31 13:50:44 -0500591#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700592
Brian Salomon5ec9def2016-12-20 15:34:05 -0500593#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700594
Brian Salomon5ec9def2016-12-20 15:34:05 -0500595DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700596 bool miterStroke = random->nextBool();
597
bsalomon40ef4852016-05-02 13:22:13 -0700598 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500599 SkRect rect =
600 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700601 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
602 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700603
joshualitt3566d442015-09-18 07:12:55 -0700604 GrColor color = GrRandomColor(random);
joshualitt9ff64252015-08-10 09:03:51 -0700605
bsalomon40ef4852016-05-02 13:22:13 -0700606 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
607 rec.setStrokeStyle(strokeWidth);
608 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500609 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700610 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Brian Salomon5ec9def2016-12-20 15:34:05 -0500611 return GrAAStrokeRectOp::Make(color, matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700612}
613
614#endif