blob: 252889240c1103d8f12decfc573b88bd26b102e5 [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"
14
15GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
16GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
17
Brian Salomon6a639042016-12-14 11:08:17 -050018static void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, SkScalar dy) {
19 pts->setRectFan(r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
joshualitt9ff64252015-08-10 09:03:51 -070020}
21
bsalomon8b7a9e12016-07-06 13:06:22 -070022// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
23// limit makes a miter join effectively beveled.
24inline static bool allowed_stroke(const SkStrokeRec& stroke, bool* isMiter) {
25 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
26 stroke.getStyle() == SkStrokeRec::kHairline_Style);
27 // For hairlines, make bevel and round joins appear the same as mitered ones.
28 if (!stroke.getWidth()) {
29 *isMiter = true;
30 return true;
31 }
32 if (stroke.getJoin() == SkPaint::kBevel_Join) {
33 *isMiter = false;
34 return true;
35 }
36 if (stroke.getJoin() == SkPaint::kMiter_Join) {
37 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
38 return true;
39 }
40 return false;
41}
42
43static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
44 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
45 SkScalar strokeWidth, bool miterStroke) {
46 SkRect devRect;
47 viewMatrix.mapRect(&devRect, rect);
48
49 SkVector devStrokeSize;
50 if (strokeWidth > 0) {
51 devStrokeSize.set(strokeWidth, strokeWidth);
52 viewMatrix.mapVectors(&devStrokeSize, 1);
53 devStrokeSize.setAbs(devStrokeSize);
54 } else {
55 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
56 }
57
58 const SkScalar dx = devStrokeSize.fX;
59 const SkScalar dy = devStrokeSize.fY;
60 const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf);
61 const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf);
62
63 *devOutside = devRect;
64 *devOutsideAssist = devRect;
65 *devInside = devRect;
66
67 devOutside->outset(rx, ry);
68 devInside->inset(rx, ry);
69
70 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
71 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
72 // together when we render these rects.
73 SkScalar spare;
74 {
75 SkScalar w = devRect.width() - dx;
76 SkScalar h = devRect.height() - dy;
77 spare = SkTMin(w, h);
78 }
79
80 *isDegenerate = spare <= 0;
81 if (*isDegenerate) {
82 devInside->fLeft = devInside->fRight = devRect.centerX();
83 devInside->fTop = devInside->fBottom = devRect.centerY();
84 }
85
86 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
87 // to draw the outside of the octagon. Because there are 8 vertices on the outer
88 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
89 if (!miterStroke) {
90 devOutside->inset(0, ry);
91 devOutsideAssist->outset(0, ry);
92 }
93}
94
bungeman06ca8ec2016-06-09 08:01:03 -070095static sk_sp<GrGeometryProcessor> create_stroke_rect_gp(bool tweakAlphaForCoverage,
joshualitt9ff64252015-08-10 09:03:51 -070096 const SkMatrix& viewMatrix,
Brian Salomon8c5bad32016-12-20 14:43:36 -050097 bool usesLocalCoords) {
joshualitt9ff64252015-08-10 09:03:51 -070098 using namespace GrDefaultGeoProcFactory;
99
100 Color color(Color::kAttribute_Type);
101 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 }
107 Coverage coverage(coverageType);
Brian Salomon6a639042016-12-14 11:08:17 -0500108 LocalCoords localCoords(usesLocalCoords ? LocalCoords::kUsePosition_Type
109 : LocalCoords::kUnused_Type);
bungeman06ca8ec2016-06-09 08:01:03 -0700110 return MakeForDeviceSpace(color, coverage, localCoords, 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 Salomon6a639042016-12-14 11:08:17 -0500128 static sk_sp<GrDrawOp> Make(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
129 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;
141 op->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon6a639042016-12-14 11:08:17 -0500142 op->fViewMatrix = viewMatrix;
143 return sk_sp<GrDrawOp>(op);
joshualitt3566d442015-09-18 07:12:55 -0700144 }
145
146 const char* name() const override { return "AAStrokeRect"; }
147
Brian Salomon7c3e7182016-12-01 09:35:30 -0500148 SkString dumpInfo() const override {
149 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500150 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500151 string.appendf(
152 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
153 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
154 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Salomon8c5bad32016-12-20 14:43:36 -0500155 info.fColor, info.fDevOutside.fLeft, info.fDevOutside.fTop,
156 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
157 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
158 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
159 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500160 }
161 string.append(DumpPipelineInfo(*this->pipeline()));
162 string.append(INHERITED::dumpInfo());
163 return string;
164 }
165
halcanary9d524f22016-03-29 09:03:52 -0700166 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800167 GrInitInvariantOutput* coverage,
168 GrBatchToXPOverrides* overrides) const override {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500169 color->setKnownFourComponents(fRects[0].fColor);
ethannicholasff210322015-11-24 12:10:10 -0800170 coverage->setUnknownSingleComponent();
joshualitt3566d442015-09-18 07:12:55 -0700171 }
172
joshualittaa37a962015-09-18 13:03:25 -0700173private:
Brian Salomon6a639042016-12-14 11:08:17 -0500174 AAStrokeRectOp() : INHERITED(ClassID()) {}
joshualittaa37a962015-09-18 13:03:25 -0700175
joshualitt144c3c82015-11-30 12:30:13 -0800176 void onPrepareDraws(Target*) const override;
ethannicholasff210322015-11-24 12:10:10 -0800177 void initBatchTracker(const GrXPOverridesForBatch&) override;
joshualittaa37a962015-09-18 13:03:25 -0700178
joshualitt3566d442015-09-18 07:12:55 -0700179 static const int kMiterIndexCnt = 3 * 24;
180 static const int kMiterVertexCnt = 16;
181 static const int kNumMiterRectsInIndexBuffer = 256;
182
183 static const int kBevelIndexCnt = 48 + 36 + 24;
184 static const int kBevelVertexCnt = 24;
185 static const int kNumBevelRectsInIndexBuffer = 256;
186
cdalton397536c2016-03-25 12:15:03 -0700187 static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700188
Brian Salomon8c5bad32016-12-20 14:43:36 -0500189 bool usesLocalCoords() const { return fUsesLocalCoords; }
190 bool canTweakAlphaForCoverage() const { return fCanTweakAlphaForCoverage; }
joshualittaa37a962015-09-18 13:03:25 -0700191 const SkMatrix& viewMatrix() const { return fViewMatrix; }
192 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700193
Brian Salomon25a88092016-12-01 09:36:50 -0500194 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700195
196 void generateAAStrokeRectGeometry(void* vertices,
197 size_t offset,
198 size_t vertexStride,
199 int outerVertexNum,
200 int innerVertexNum,
201 GrColor color,
202 const SkRect& devOutside,
203 const SkRect& devOutsideAssist,
204 const SkRect& devInside,
205 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700206 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700207 bool tweakAlphaForCoverage) const;
208
bsalomon8b7a9e12016-07-06 13:06:22 -0700209 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500210 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700211 GrColor fColor;
212 SkRect fDevOutside;
213 SkRect fDevOutsideAssist;
214 SkRect fDevInside;
215 bool fDegenerate;
216 };
217
Brian Salomon8c5bad32016-12-20 14:43:36 -0500218 SkSTArray<1, RectInfo, true> fRects;
219 bool fUsesLocalCoords;
220 bool fCanTweakAlphaForCoverage;
joshualittaa37a962015-09-18 13:03:25 -0700221 SkMatrix fViewMatrix;
222 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700223
Brian Salomondad29232016-12-01 16:40:24 -0500224 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700225};
226
Brian Salomon6a639042016-12-14 11:08:17 -0500227void AAStrokeRectOp::initBatchTracker(const GrXPOverridesForBatch& overrides) {
ethannicholasff210322015-11-24 12:10:10 -0800228 if (!overrides.readsColor()) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500229 fRects[0].fColor = GrColor_ILLEGAL;
joshualitt9ff64252015-08-10 09:03:51 -0700230 }
Brian Salomon8c5bad32016-12-20 14:43:36 -0500231 overrides.getOverrideColorIfSet(&fRects[0].fColor);
joshualitt9ff64252015-08-10 09:03:51 -0700232
233 // setup batch properties
Brian Salomon8c5bad32016-12-20 14:43:36 -0500234 fUsesLocalCoords = overrides.readsLocalCoords();
235 fCanTweakAlphaForCoverage = overrides.canTweakAlphaForCoverage();
joshualitt9ff64252015-08-10 09:03:51 -0700236}
237
Brian Salomon6a639042016-12-14 11:08:17 -0500238void AAStrokeRectOp::onPrepareDraws(Target* target) const {
joshualitt9ff64252015-08-10 09:03:51 -0700239 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
240
bungeman06ca8ec2016-06-09 08:01:03 -0700241 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(canTweakAlphaForCoverage,
242 this->viewMatrix(),
Brian Salomon8c5bad32016-12-20 14:43:36 -0500243 this->usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700244 if (!gp) {
245 SkDebugf("Couldn't create GrGeometryProcessor\n");
246 return;
247 }
248
joshualitt9ff64252015-08-10 09:03:51 -0700249 size_t vertexStride = gp->getVertexStride();
250
Brian Salomon6a639042016-12-14 11:08:17 -0500251 SkASSERT(canTweakAlphaForCoverage
252 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
253 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700254 int innerVertexNum = 4;
255 int outerVertexNum = this->miterStroke() ? 4 : 8;
256 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
257 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500258 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700259
Hal Canary144caf52016-11-07 17:57:18 -0500260 const sk_sp<const GrBuffer> indexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500261 GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
joshualitt9ff64252015-08-10 09:03:51 -0700262 InstancedHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500263 void* vertices =
264 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
265 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700266 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500267 SkDebugf("Could not allocate vertices\n");
268 return;
269 }
joshualitt9ff64252015-08-10 09:03:51 -0700270
271 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500272 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700273 this->generateAAStrokeRectGeometry(vertices,
274 i * verticesPerInstance * vertexStride,
275 vertexStride,
276 outerVertexNum,
277 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500278 info.fColor,
279 info.fDevOutside,
280 info.fDevOutsideAssist,
281 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700282 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500283 info.fDegenerate,
joshualitt9ff64252015-08-10 09:03:51 -0700284 canTweakAlphaForCoverage);
285 }
bungeman06ca8ec2016-06-09 08:01:03 -0700286 helper.recordDraw(target, gp.get());
joshualitt9ff64252015-08-10 09:03:51 -0700287}
288
Brian Salomon6a639042016-12-14 11:08:17 -0500289const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
290 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700291 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500292 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700293 static const uint16_t gMiterIndices[] = {
294 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
295 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
296 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
297 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
298
299 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
300 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
301 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
302 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
303
304 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
305 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
306 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
307 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
308 };
Brian Salomon6a639042016-12-14 11:08:17 -0500309 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700310 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
311 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Brian Salomon6a639042016-12-14 11:08:17 -0500312 return resourceProvider->findOrCreateInstancedIndexBuffer(
313 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
314 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700315 } else {
316 /**
317 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
318 * from the first index. The index layout:
319 * outer AA line: 0~3, 4~7
320 * outer edge: 8~11, 12~15
321 * inner edge: 16~19
322 * inner AA line: 20~23
323 * Following comes a bevel-stroke rect and its indices:
324 *
325 * 4 7
326 * *********************************
327 * * ______________________________ *
328 * * / 12 15 \ *
329 * * / \ *
330 * 0 * |8 16_____________________19 11 | * 3
331 * * | | | | *
332 * * | | **************** | | *
333 * * | | * 20 23 * | | *
334 * * | | * * | | *
335 * * | | * 21 22 * | | *
336 * * | | **************** | | *
337 * * | |____________________| | *
338 * 1 * |9 17 18 10| * 2
339 * * \ / *
340 * * \13 __________________________14/ *
341 * * *
342 * **********************************
343 * 5 6
344 */
Brian Salomon6a639042016-12-14 11:08:17 -0500345 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700346 static const uint16_t gBevelIndices[] = {
347 // Draw outer AA, from outer AA line to outer edge, shift is 0.
348 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
349 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
350 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
351 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
352 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
353 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
354 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
355 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
356
357 // Draw the stroke, from outer edge to inner edge, shift is 8.
358 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
359 1 + 8, 5 + 8, 9 + 8,
360 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
361 6 + 8, 2 + 8, 10 + 8,
362 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
363 3 + 8, 7 + 8, 11 + 8,
364 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
365 4 + 8, 0 + 8, 8 + 8,
366
367 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
368 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
369 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
370 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
371 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
372 };
Brian Salomon6a639042016-12-14 11:08:17 -0500373 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700374 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
375
376 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Brian Salomon6a639042016-12-14 11:08:17 -0500377 return resourceProvider->findOrCreateInstancedIndexBuffer(
378 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
379 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700380 }
381}
382
Brian Salomon6a639042016-12-14 11:08:17 -0500383bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
384 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700385
386 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
387 that->bounds(), caps)) {
joshualitt9ff64252015-08-10 09:03:51 -0700388 return false;
389 }
390
joshualitt9ff64252015-08-10 09:03:51 -0700391 // TODO batch across miterstroke changes
392 if (this->miterStroke() != that->miterStroke()) {
393 return false;
394 }
395
396 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
397 // local coords then we won't be able to batch. We could actually upload the viewmatrix
398 // using vertex attributes in these cases, but haven't investigated that
399 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
400 return false;
401 }
402
Brian Salomon6a639042016-12-14 11:08:17 -0500403 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
404 // tweaking.
joshualitt9ff64252015-08-10 09:03:51 -0700405 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500406 fCanTweakAlphaForCoverage = false;
joshualitt9ff64252015-08-10 09:03:51 -0700407 }
408
Brian Salomon8c5bad32016-12-20 14:43:36 -0500409 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700410 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700411 return true;
412}
413
joshualitt11edad92015-09-22 10:32:28 -0700414static void setup_scale(int* scale, SkScalar inset) {
415 if (inset < SK_ScalarHalf) {
416 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
417 SkASSERT(*scale >= 0 && *scale <= 255);
418 } else {
419 *scale = 0xff;
420 }
421}
422
Brian Salomon6a639042016-12-14 11:08:17 -0500423void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
424 size_t offset,
425 size_t vertexStride,
426 int outerVertexNum,
427 int innerVertexNum,
428 GrColor color,
429 const SkRect& devOutside,
430 const SkRect& devOutsideAssist,
431 const SkRect& devInside,
432 bool miterStroke,
433 bool degenerate,
434 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700435 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
436
437 // We create vertices for four nested rectangles. There are two ramps from 0 to full
438 // coverage, one on the exterior of the stroke and the other on the interior.
439 // The following pointers refer to the four rects, from outermost to innermost.
440 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
441 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
442 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500443 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
444 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700445
446#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
447 // TODO: this only really works if the X & Y margins are the same all around
448 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700449 SkScalar inset;
450 if (!degenerate) {
451 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
452 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
453 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
454 if (miterStroke) {
455 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
456 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500457 inset = SK_ScalarHalf *
458 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700459 }
460 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700461 } else {
joshualitt11edad92015-09-22 10:32:28 -0700462 // TODO use real devRect here
463 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500464 inset = SK_ScalarHalf *
465 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700466 }
joshualitt9ff64252015-08-10 09:03:51 -0700467#else
joshualitt11edad92015-09-22 10:32:28 -0700468 SkScalar inset;
469 if (!degenerate) {
470 inset = SK_ScalarHalf;
471 } else {
472 // TODO use real devRect here
473 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500474 inset = SK_ScalarHalf *
475 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700476 }
joshualitt9ff64252015-08-10 09:03:51 -0700477#endif
478
479 if (miterStroke) {
480 // outermost
481 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
482 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500483 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700484 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500485 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700486 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500487 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700488 } else {
489 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500490 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
491 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
492 devInside.fBottom, vertexStride);
493 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
494 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700495 }
joshualitt9ff64252015-08-10 09:03:51 -0700496 } else {
497 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500498 SkPoint* fan1AssistPos =
499 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700500 // outermost
501 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
502 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
503 -SK_ScalarHalf);
504 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500505 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
506 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700507 if (!degenerate) {
508 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500509 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700510 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500511 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700512 } else {
513 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500514 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
515 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
516 devInside.fBottom, vertexStride);
517 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
518 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700519 }
joshualitt9ff64252015-08-10 09:03:51 -0700520 }
521
522 // Make verts point to vertex color and then set all the color and coverage vertex attrs
523 // values. The outermost rect has 0 coverage
524 verts += sizeof(SkPoint);
525 for (int i = 0; i < outerVertexNum; ++i) {
526 if (tweakAlphaForCoverage) {
527 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
528 } else {
529 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
530 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
531 }
532 }
533
534 // scale is the coverage for the the inner two rects.
535 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700536 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700537
538 float innerCoverage = GrNormalizeByteToFloat(scale);
539 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
540
541 verts += outerVertexNum * vertexStride;
542 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
543 if (tweakAlphaForCoverage) {
544 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
545 } else {
546 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700547 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700548 }
549 }
550
joshualitt11edad92015-09-22 10:32:28 -0700551 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
552 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700553 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700554 if (!degenerate) {
555 innerCoverage = 0;
556 scaledColor = 0;
557 }
558
joshualitt9ff64252015-08-10 09:03:51 -0700559 for (int i = 0; i < innerVertexNum; ++i) {
560 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700561 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700562 } else {
563 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700564 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700565 }
566 }
567}
568
Brian Salomon6a639042016-12-14 11:08:17 -0500569namespace GrAAStrokeRectOp {
joshualitt3566d442015-09-18 07:12:55 -0700570
Brian Salomon6a639042016-12-14 11:08:17 -0500571sk_sp<GrDrawOp> MakeFillBetweenRects(GrColor color,
572 const SkMatrix& viewMatrix,
573 const SkRect& devOutside,
574 const SkRect& devInside) {
575 return sk_sp<GrDrawOp>(new AAStrokeRectOp(color, viewMatrix, devOutside, devInside));
joshualittaa37a962015-09-18 13:03:25 -0700576}
577
Brian Salomon6a639042016-12-14 11:08:17 -0500578sk_sp<GrDrawOp> Make(GrColor color,
579 const SkMatrix& viewMatrix,
580 const SkRect& rect,
581 const SkStrokeRec& stroke) {
582 return AAStrokeRectOp::Make(color, viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700583}
bsalomon8b7a9e12016-07-06 13:06:22 -0700584}
joshualitt3566d442015-09-18 07:12:55 -0700585
joshualitt9ff64252015-08-10 09:03:51 -0700586///////////////////////////////////////////////////////////////////////////////////////////////////
587
588#ifdef GR_TEST_UTILS
589
590#include "GrBatchTest.h"
591
Brian Salomon6a639042016-12-14 11:08:17 -0500592DRAW_BATCH_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700593 bool miterStroke = random->nextBool();
594
bsalomon40ef4852016-05-02 13:22:13 -0700595 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500596 SkRect rect =
597 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700598 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
599 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700600
joshualitt3566d442015-09-18 07:12:55 -0700601 GrColor color = GrRandomColor(random);
joshualitt9ff64252015-08-10 09:03:51 -0700602
bsalomon40ef4852016-05-02 13:22:13 -0700603 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
604 rec.setStrokeStyle(strokeWidth);
605 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500606 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700607 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Brian Salomon6a639042016-12-14 11:08:17 -0500608 return GrAAStrokeRectOp::Make(color, matrix, rect, rec).release();
joshualitt9ff64252015-08-10 09:03:51 -0700609}
610
611#endif