blob: 25f507110dd532fe66d27c1c274450020ea34414 [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 Salomonf8334782017-01-03 09:42:58 -0500128 static std::unique_ptr<GrDrawOp> 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;
141 op->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon6a639042016-12-14 11:08:17 -0500142 op->fViewMatrix = viewMatrix;
Brian Salomonf8334782017-01-03 09:42:58 -0500143 return std::unique_ptr<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
joshualittaa37a962015-09-18 13:03:25 -0700166private:
Brian Salomon6a639042016-12-14 11:08:17 -0500167 AAStrokeRectOp() : INHERITED(ClassID()) {}
joshualittaa37a962015-09-18 13:03:25 -0700168
Brian Salomon92aee3d2016-12-21 09:20:25 -0500169 void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override {
170 input->pipelineColorInput()->setKnownFourComponents(fRects[0].fColor);
171 input->pipelineCoverageInput()->setUnknownSingleComponent();
172 }
173 void applyPipelineOptimizations(const GrPipelineOptimizations&) override;
joshualitt144c3c82015-11-30 12:30:13 -0800174 void onPrepareDraws(Target*) const override;
joshualittaa37a962015-09-18 13:03:25 -0700175
joshualitt3566d442015-09-18 07:12:55 -0700176 static const int kMiterIndexCnt = 3 * 24;
177 static const int kMiterVertexCnt = 16;
178 static const int kNumMiterRectsInIndexBuffer = 256;
179
180 static const int kBevelIndexCnt = 48 + 36 + 24;
181 static const int kBevelVertexCnt = 24;
182 static const int kNumBevelRectsInIndexBuffer = 256;
183
cdalton397536c2016-03-25 12:15:03 -0700184 static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700185
Brian Salomon8c5bad32016-12-20 14:43:36 -0500186 bool usesLocalCoords() const { return fUsesLocalCoords; }
187 bool canTweakAlphaForCoverage() const { return fCanTweakAlphaForCoverage; }
joshualittaa37a962015-09-18 13:03:25 -0700188 const SkMatrix& viewMatrix() const { return fViewMatrix; }
189 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700190
Brian Salomon25a88092016-12-01 09:36:50 -0500191 bool onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700192
193 void generateAAStrokeRectGeometry(void* vertices,
194 size_t offset,
195 size_t vertexStride,
196 int outerVertexNum,
197 int innerVertexNum,
198 GrColor color,
199 const SkRect& devOutside,
200 const SkRect& devOutsideAssist,
201 const SkRect& devInside,
202 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700203 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700204 bool tweakAlphaForCoverage) const;
205
bsalomon8b7a9e12016-07-06 13:06:22 -0700206 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500207 struct RectInfo {
bsalomon8b7a9e12016-07-06 13:06:22 -0700208 GrColor fColor;
209 SkRect fDevOutside;
210 SkRect fDevOutsideAssist;
211 SkRect fDevInside;
212 bool fDegenerate;
213 };
214
Brian Salomon8c5bad32016-12-20 14:43:36 -0500215 SkSTArray<1, RectInfo, true> fRects;
216 bool fUsesLocalCoords;
217 bool fCanTweakAlphaForCoverage;
joshualittaa37a962015-09-18 13:03:25 -0700218 SkMatrix fViewMatrix;
219 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700220
Brian Salomondad29232016-12-01 16:40:24 -0500221 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700222};
223
Brian Salomon92aee3d2016-12-21 09:20:25 -0500224void AAStrokeRectOp::applyPipelineOptimizations(const GrPipelineOptimizations& optimizations) {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500225 optimizations.getOverrideColorIfSet(&fRects[0].fColor);
joshualitt9ff64252015-08-10 09:03:51 -0700226
Brian Salomon92aee3d2016-12-21 09:20:25 -0500227 fUsesLocalCoords = optimizations.readsLocalCoords();
228 fCanTweakAlphaForCoverage = optimizations.canTweakAlphaForCoverage();
229 fCanTweakAlphaForCoverage = optimizations.canTweakAlphaForCoverage();
joshualitt9ff64252015-08-10 09:03:51 -0700230}
231
Brian Salomon6a639042016-12-14 11:08:17 -0500232void AAStrokeRectOp::onPrepareDraws(Target* target) const {
joshualitt9ff64252015-08-10 09:03:51 -0700233 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
234
bungeman06ca8ec2016-06-09 08:01:03 -0700235 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(canTweakAlphaForCoverage,
236 this->viewMatrix(),
Brian Salomon8c5bad32016-12-20 14:43:36 -0500237 this->usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700238 if (!gp) {
239 SkDebugf("Couldn't create GrGeometryProcessor\n");
240 return;
241 }
242
joshualitt9ff64252015-08-10 09:03:51 -0700243 size_t vertexStride = gp->getVertexStride();
244
Brian Salomon6a639042016-12-14 11:08:17 -0500245 SkASSERT(canTweakAlphaForCoverage
246 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
247 : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
joshualitt9ff64252015-08-10 09:03:51 -0700248 int innerVertexNum = 4;
249 int outerVertexNum = this->miterStroke() ? 4 : 8;
250 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
251 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500252 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700253
Hal Canary144caf52016-11-07 17:57:18 -0500254 const sk_sp<const GrBuffer> indexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500255 GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
joshualitt9ff64252015-08-10 09:03:51 -0700256 InstancedHelper helper;
Brian Salomon6a639042016-12-14 11:08:17 -0500257 void* vertices =
258 helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
259 verticesPerInstance, indicesPerInstance, instanceCount);
joshualitt9ff64252015-08-10 09:03:51 -0700260 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500261 SkDebugf("Could not allocate vertices\n");
262 return;
263 }
joshualitt9ff64252015-08-10 09:03:51 -0700264
265 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500266 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700267 this->generateAAStrokeRectGeometry(vertices,
268 i * verticesPerInstance * vertexStride,
269 vertexStride,
270 outerVertexNum,
271 innerVertexNum,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500272 info.fColor,
273 info.fDevOutside,
274 info.fDevOutsideAssist,
275 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700276 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500277 info.fDegenerate,
joshualitt9ff64252015-08-10 09:03:51 -0700278 canTweakAlphaForCoverage);
279 }
bungeman06ca8ec2016-06-09 08:01:03 -0700280 helper.recordDraw(target, gp.get());
joshualitt9ff64252015-08-10 09:03:51 -0700281}
282
Brian Salomon6a639042016-12-14 11:08:17 -0500283const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
284 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700285 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500286 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700287 static const uint16_t gMiterIndices[] = {
288 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
289 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
290 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
291 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
292
293 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
294 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
295 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
296 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
297
298 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
299 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
300 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
301 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
302 };
Brian Salomon6a639042016-12-14 11:08:17 -0500303 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700304 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
305 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Brian Salomon6a639042016-12-14 11:08:17 -0500306 return resourceProvider->findOrCreateInstancedIndexBuffer(
307 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
308 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700309 } else {
310 /**
311 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
312 * from the first index. The index layout:
313 * outer AA line: 0~3, 4~7
314 * outer edge: 8~11, 12~15
315 * inner edge: 16~19
316 * inner AA line: 20~23
317 * Following comes a bevel-stroke rect and its indices:
318 *
319 * 4 7
320 * *********************************
321 * * ______________________________ *
322 * * / 12 15 \ *
323 * * / \ *
324 * 0 * |8 16_____________________19 11 | * 3
325 * * | | | | *
326 * * | | **************** | | *
327 * * | | * 20 23 * | | *
328 * * | | * * | | *
329 * * | | * 21 22 * | | *
330 * * | | **************** | | *
331 * * | |____________________| | *
332 * 1 * |9 17 18 10| * 2
333 * * \ / *
334 * * \13 __________________________14/ *
335 * * *
336 * **********************************
337 * 5 6
338 */
Brian Salomon6a639042016-12-14 11:08:17 -0500339 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700340 static const uint16_t gBevelIndices[] = {
341 // Draw outer AA, from outer AA line to outer edge, shift is 0.
342 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
343 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
344 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
345 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
346 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
347 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
348 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
349 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
350
351 // Draw the stroke, from outer edge to inner edge, shift is 8.
352 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
353 1 + 8, 5 + 8, 9 + 8,
354 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
355 6 + 8, 2 + 8, 10 + 8,
356 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
357 3 + 8, 7 + 8, 11 + 8,
358 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
359 4 + 8, 0 + 8, 8 + 8,
360
361 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
362 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
363 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
364 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
365 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
366 };
Brian Salomon6a639042016-12-14 11:08:17 -0500367 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700368 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
369
370 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Brian Salomon6a639042016-12-14 11:08:17 -0500371 return resourceProvider->findOrCreateInstancedIndexBuffer(
372 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
373 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700374 }
375}
376
Brian Salomon6a639042016-12-14 11:08:17 -0500377bool AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
378 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700379
380 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
381 that->bounds(), caps)) {
joshualitt9ff64252015-08-10 09:03:51 -0700382 return false;
383 }
384
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500385 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700386 if (this->miterStroke() != that->miterStroke()) {
387 return false;
388 }
389
390 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500391 // local coords then we won't be able to combine. We could actually upload the viewmatrix
joshualitt9ff64252015-08-10 09:03:51 -0700392 // using vertex attributes in these cases, but haven't investigated that
393 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
394 return false;
395 }
396
Brian Salomon6a639042016-12-14 11:08:17 -0500397 // In the event of two ops, one who can tweak, one who cannot, we just fall back to not
398 // tweaking.
joshualitt9ff64252015-08-10 09:03:51 -0700399 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500400 fCanTweakAlphaForCoverage = false;
joshualitt9ff64252015-08-10 09:03:51 -0700401 }
402
Brian Salomon8c5bad32016-12-20 14:43:36 -0500403 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700404 this->joinBounds(*that);
joshualitt9ff64252015-08-10 09:03:51 -0700405 return true;
406}
407
joshualitt11edad92015-09-22 10:32:28 -0700408static void setup_scale(int* scale, SkScalar inset) {
409 if (inset < SK_ScalarHalf) {
410 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
411 SkASSERT(*scale >= 0 && *scale <= 255);
412 } else {
413 *scale = 0xff;
414 }
415}
416
Brian Salomon6a639042016-12-14 11:08:17 -0500417void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
418 size_t offset,
419 size_t vertexStride,
420 int outerVertexNum,
421 int innerVertexNum,
422 GrColor color,
423 const SkRect& devOutside,
424 const SkRect& devOutsideAssist,
425 const SkRect& devInside,
426 bool miterStroke,
427 bool degenerate,
428 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700429 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
430
431 // We create vertices for four nested rectangles. There are two ramps from 0 to full
432 // coverage, one on the exterior of the stroke and the other on the interior.
433 // The following pointers refer to the four rects, from outermost to innermost.
434 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
435 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
436 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500437 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
438 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700439
440#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
441 // TODO: this only really works if the X & Y margins are the same all around
442 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700443 SkScalar inset;
444 if (!degenerate) {
445 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
446 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
447 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
448 if (miterStroke) {
449 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
450 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500451 inset = SK_ScalarHalf *
452 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700453 }
454 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700455 } else {
joshualitt11edad92015-09-22 10:32:28 -0700456 // TODO use real devRect here
457 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500458 inset = SK_ScalarHalf *
459 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700460 }
joshualitt9ff64252015-08-10 09:03:51 -0700461#else
joshualitt11edad92015-09-22 10:32:28 -0700462 SkScalar inset;
463 if (!degenerate) {
464 inset = SK_ScalarHalf;
465 } else {
466 // TODO use real devRect here
467 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500468 inset = SK_ScalarHalf *
469 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700470 }
joshualitt9ff64252015-08-10 09:03:51 -0700471#endif
472
473 if (miterStroke) {
474 // outermost
475 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
476 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500477 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700478 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500479 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700480 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500481 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700482 } else {
483 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500484 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
485 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
486 devInside.fBottom, vertexStride);
487 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
488 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700489 }
joshualitt9ff64252015-08-10 09:03:51 -0700490 } else {
491 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500492 SkPoint* fan1AssistPos =
493 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700494 // outermost
495 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
496 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
497 -SK_ScalarHalf);
498 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500499 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
500 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700501 if (!degenerate) {
502 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500503 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700504 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500505 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700506 } else {
507 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500508 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
509 fan2Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
510 devInside.fBottom, vertexStride);
511 fan3Pos->setRectFan(devInside.fLeft, devInside.fTop, devInside.fRight,
512 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700513 }
joshualitt9ff64252015-08-10 09:03:51 -0700514 }
515
516 // Make verts point to vertex color and then set all the color and coverage vertex attrs
517 // values. The outermost rect has 0 coverage
518 verts += sizeof(SkPoint);
519 for (int i = 0; i < outerVertexNum; ++i) {
520 if (tweakAlphaForCoverage) {
521 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
522 } else {
523 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
524 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
525 }
526 }
527
528 // scale is the coverage for the the inner two rects.
529 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700530 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700531
532 float innerCoverage = GrNormalizeByteToFloat(scale);
533 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
534
535 verts += outerVertexNum * vertexStride;
536 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
537 if (tweakAlphaForCoverage) {
538 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
539 } else {
540 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700541 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700542 }
543 }
544
joshualitt11edad92015-09-22 10:32:28 -0700545 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
546 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700547 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700548 if (!degenerate) {
549 innerCoverage = 0;
550 scaledColor = 0;
551 }
552
joshualitt9ff64252015-08-10 09:03:51 -0700553 for (int i = 0; i < innerVertexNum; ++i) {
554 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700555 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700556 } else {
557 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700558 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700559 }
560 }
561}
562
Brian Salomon6a639042016-12-14 11:08:17 -0500563namespace GrAAStrokeRectOp {
joshualitt3566d442015-09-18 07:12:55 -0700564
Brian Salomonf8334782017-01-03 09:42:58 -0500565std::unique_ptr<GrDrawOp> MakeFillBetweenRects(GrColor color,
566 const SkMatrix& viewMatrix,
567 const SkRect& devOutside,
568 const SkRect& devInside) {
569 return std::unique_ptr<GrDrawOp>(new AAStrokeRectOp(color, viewMatrix, devOutside, devInside));
joshualittaa37a962015-09-18 13:03:25 -0700570}
571
Brian Salomonf8334782017-01-03 09:42:58 -0500572std::unique_ptr<GrDrawOp> Make(GrColor color,
573 const SkMatrix& viewMatrix,
574 const SkRect& rect,
575 const SkStrokeRec& stroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500576 return AAStrokeRectOp::Make(color, viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700577}
bsalomon8b7a9e12016-07-06 13:06:22 -0700578}
joshualitt3566d442015-09-18 07:12:55 -0700579
joshualitt9ff64252015-08-10 09:03:51 -0700580///////////////////////////////////////////////////////////////////////////////////////////////////
581
Hal Canary6f6961e2017-01-31 13:50:44 -0500582#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700583
Brian Salomon5ec9def2016-12-20 15:34:05 -0500584#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700585
Brian Salomon5ec9def2016-12-20 15:34:05 -0500586DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700587 bool miterStroke = random->nextBool();
588
bsalomon40ef4852016-05-02 13:22:13 -0700589 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500590 SkRect rect =
591 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700592 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
593 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700594
joshualitt3566d442015-09-18 07:12:55 -0700595 GrColor color = GrRandomColor(random);
joshualitt9ff64252015-08-10 09:03:51 -0700596
bsalomon40ef4852016-05-02 13:22:13 -0700597 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
598 rec.setStrokeStyle(strokeWidth);
599 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500600 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700601 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Brian Salomon5ec9def2016-12-20 15:34:05 -0500602 return GrAAStrokeRectOp::Make(color, matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700603}
604
605#endif