blob: 458c705db5903f95306ecad243fd28c0f0e97b67 [file] [log] [blame]
joshualitt9ff64252015-08-10 09:03:51 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
joshualitt9ff64252015-08-10 09:03:51 -07008#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -05009#include "GrOpFlushState.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040010#include "GrRectOpFactory.h"
joshualitt9ff64252015-08-10 09:03:51 -070011#include "GrResourceKey.h"
12#include "GrResourceProvider.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040013#include "GrSimpleMeshDrawOpHelper.h"
Cary Clark74f623d2017-11-06 20:02:02 -050014#include "SkPointPriv.h"
Hal Canary6f6961e2017-01-31 13:50:44 -050015#include "SkStrokeRec.h"
joshualitt9ff64252015-08-10 09:03:51 -070016
17GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
18GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
19
Brian Salomon6a639042016-12-14 11:08:17 -050020static void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, SkScalar dy) {
Cary Clark74f623d2017-11-06 20:02:02 -050021 SkPointPriv::SetRectFan(pts, r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride);
joshualitt9ff64252015-08-10 09:03:51 -070022}
23
bsalomon8b7a9e12016-07-06 13:06:22 -070024// We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter
25// limit makes a miter join effectively beveled.
26inline static bool allowed_stroke(const SkStrokeRec& stroke, bool* isMiter) {
27 SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style ||
28 stroke.getStyle() == SkStrokeRec::kHairline_Style);
29 // For hairlines, make bevel and round joins appear the same as mitered ones.
30 if (!stroke.getWidth()) {
31 *isMiter = true;
32 return true;
33 }
34 if (stroke.getJoin() == SkPaint::kBevel_Join) {
35 *isMiter = false;
36 return true;
37 }
38 if (stroke.getJoin() == SkPaint::kMiter_Join) {
39 *isMiter = stroke.getMiter() >= SK_ScalarSqrt2;
40 return true;
41 }
42 return false;
43}
44
45static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside,
46 bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect,
47 SkScalar strokeWidth, bool miterStroke) {
48 SkRect devRect;
49 viewMatrix.mapRect(&devRect, rect);
50
51 SkVector devStrokeSize;
52 if (strokeWidth > 0) {
53 devStrokeSize.set(strokeWidth, strokeWidth);
54 viewMatrix.mapVectors(&devStrokeSize, 1);
55 devStrokeSize.setAbs(devStrokeSize);
56 } else {
57 devStrokeSize.set(SK_Scalar1, SK_Scalar1);
58 }
59
60 const SkScalar dx = devStrokeSize.fX;
61 const SkScalar dy = devStrokeSize.fY;
Mike Reed8be952a2017-02-13 20:44:33 -050062 const SkScalar rx = SkScalarHalf(dx);
63 const SkScalar ry = SkScalarHalf(dy);
bsalomon8b7a9e12016-07-06 13:06:22 -070064
65 *devOutside = devRect;
66 *devOutsideAssist = devRect;
67 *devInside = devRect;
68
69 devOutside->outset(rx, ry);
70 devInside->inset(rx, ry);
71
72 // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we
73 // make a degenerate inside rect to avoid double hitting. We will also jam all of the points
74 // together when we render these rects.
75 SkScalar spare;
76 {
77 SkScalar w = devRect.width() - dx;
78 SkScalar h = devRect.height() - dy;
79 spare = SkTMin(w, h);
80 }
81
82 *isDegenerate = spare <= 0;
83 if (*isDegenerate) {
84 devInside->fLeft = devInside->fRight = devRect.centerX();
85 devInside->fTop = devInside->fBottom = devRect.centerY();
86 }
87
88 // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist)
89 // to draw the outside of the octagon. Because there are 8 vertices on the outer
90 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
91 if (!miterStroke) {
92 devOutside->inset(0, ry);
93 devOutsideAssist->outset(0, ry);
94 }
95}
96
Ruiqi Maob609e6d2018-07-17 10:19:38 -040097static sk_sp<GrGeometryProcessor> create_stroke_rect_gp(const GrShaderCaps* shaderCaps,
98 bool tweakAlphaForCoverage,
joshualitt9ff64252015-08-10 09:03:51 -070099 const SkMatrix& viewMatrix,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500100 bool usesLocalCoords) {
joshualitt9ff64252015-08-10 09:03:51 -0700101 using namespace GrDefaultGeoProcFactory;
102
joshualitt9ff64252015-08-10 09:03:51 -0700103 Coverage::Type coverageType;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500104 if (tweakAlphaForCoverage) {
joshualitt9ff64252015-08-10 09:03:51 -0700105 coverageType = Coverage::kSolid_Type;
106 } else {
107 coverageType = Coverage::kAttribute_Type;
108 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500109 LocalCoords::Type localCoordsType =
110 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400111 return MakeForDeviceSpace(shaderCaps,
112 Color::kPremulGrColorAttribute_Type,
113 coverageType,
114 localCoordsType,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500115 viewMatrix);
joshualitt9ff64252015-08-10 09:03:51 -0700116}
117
Brian Salomonbaaf4392017-06-15 09:59:23 -0400118namespace {
119
120class AAStrokeRectOp final : public GrMeshDrawOp {
121private:
122 using Helper = GrSimpleMeshDrawOpHelper;
123
joshualitt3566d442015-09-18 07:12:55 -0700124public:
Brian Salomon25a88092016-12-01 09:36:50 -0500125 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -0700126
Robert Phillips7c525e62018-06-12 10:11:12 -0400127 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
128 GrPaint&& paint,
129 const SkMatrix& viewMatrix,
130 const SkRect& devOutside,
131 const SkRect& devInside) {
132 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix,
133 devOutside, devInside);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400134 }
135
Brian Osman936fe7d2018-10-30 15:30:35 -0400136 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const GrColor4h& color,
137 const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400138 : INHERITED(ClassID())
139 , fHelper(helperArgs, GrAAType::kCoverage)
140 , fViewMatrix(viewMatrix) {
caryclarkd6562002016-07-27 12:02:07 -0700141 SkASSERT(!devOutside.isEmpty());
142 SkASSERT(!devInside.isEmpty());
joshualitt3566d442015-09-18 07:12:55 -0700143
Brian Salomon8c5bad32016-12-20 14:43:36 -0500144 fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false});
bsalomon88cf17d2016-07-08 06:40:56 -0700145 this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo);
bsalomon8b7a9e12016-07-06 13:06:22 -0700146 fMiterStroke = true;
147 }
148
Robert Phillips7c525e62018-06-12 10:11:12 -0400149 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
150 GrPaint&& paint,
151 const SkMatrix& viewMatrix,
152 const SkRect& rect,
153 const SkStrokeRec& stroke) {
bsalomon8b7a9e12016-07-06 13:06:22 -0700154 bool isMiter;
155 if (!allowed_stroke(stroke, &isMiter)) {
156 return nullptr;
157 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400158 return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect,
159 stroke, isMiter);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400160 }
bsalomon8b7a9e12016-07-06 13:06:22 -0700161
Brian Osman936fe7d2018-10-30 15:30:35 -0400162 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const GrColor4h& color,
163 const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke,
164 bool isMiter)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400165 : INHERITED(ClassID())
166 , fHelper(helperArgs, GrAAType::kCoverage)
167 , fViewMatrix(viewMatrix) {
168 fMiterStroke = isMiter;
169 RectInfo& info = fRects.push_back();
Brian Salomon8c5bad32016-12-20 14:43:36 -0500170 compute_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside,
171 &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter);
172 info.fColor = color;
Brian Salomon510dd422017-03-16 12:15:22 -0400173 if (isMiter) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400174 this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400175 } else {
176 // The outer polygon of the bevel stroke is an octagon specified by the points of a
177 // pair of overlapping rectangles where one is wide and the other is narrow.
178 SkRect bounds = info.fDevOutside;
179 bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400180 this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
Brian Salomon510dd422017-03-16 12:15:22 -0400181 }
joshualitt3566d442015-09-18 07:12:55 -0700182 }
183
184 const char* name() const override { return "AAStrokeRect"; }
185
Brian Salomon7d94bb52018-10-12 14:37:19 -0400186 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400187 fHelper.visitProxies(func);
188 }
189
Brian Salomon7c3e7182016-12-01 09:35:30 -0500190 SkString dumpInfo() const override {
191 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500192 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500193 string.appendf(
194 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
195 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
196 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Osman1be2b7c2018-10-29 16:07:15 -0400197 info.fColor.toGrColor(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500198 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
199 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
200 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
201 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500202 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400203 string += fHelper.dumpInfo();
204 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500205 return string;
206 }
207
Brian Salomonbaaf4392017-06-15 09:59:23 -0400208 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400209
Brian Osman532b3f92018-07-11 10:02:07 -0400210 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
211 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400212 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400213 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400214
215private:
Brian Salomon91326c32017-08-09 16:02:19 -0400216 void onPrepareDraws(Target*) override;
joshualittaa37a962015-09-18 13:03:25 -0700217
joshualitt3566d442015-09-18 07:12:55 -0700218 static const int kMiterIndexCnt = 3 * 24;
219 static const int kMiterVertexCnt = 16;
220 static const int kNumMiterRectsInIndexBuffer = 256;
221
222 static const int kBevelIndexCnt = 48 + 36 + 24;
223 static const int kBevelVertexCnt = 24;
224 static const int kNumBevelRectsInIndexBuffer = 256;
225
Brian Salomond28a79d2017-10-16 13:01:07 -0400226 static sk_sp<const GrBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700227
joshualittaa37a962015-09-18 13:03:25 -0700228 const SkMatrix& viewMatrix() const { return fViewMatrix; }
229 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700230
Brian Salomon7eae3e02018-08-07 14:02:38 +0000231 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700232
233 void generateAAStrokeRectGeometry(void* vertices,
234 size_t offset,
235 size_t vertexStride,
236 int outerVertexNum,
237 int innerVertexNum,
238 GrColor color,
239 const SkRect& devOutside,
240 const SkRect& devOutsideAssist,
241 const SkRect& devInside,
242 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700243 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700244 bool tweakAlphaForCoverage) const;
245
bsalomon8b7a9e12016-07-06 13:06:22 -0700246 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500247 struct RectInfo {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400248 GrColor4h fColor;
bsalomon8b7a9e12016-07-06 13:06:22 -0700249 SkRect fDevOutside;
250 SkRect fDevOutsideAssist;
251 SkRect fDevInside;
252 bool fDegenerate;
253 };
254
Brian Salomonbaaf4392017-06-15 09:59:23 -0400255 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500256 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700257 SkMatrix fViewMatrix;
258 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700259
Brian Salomonbaaf4392017-06-15 09:59:23 -0400260 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700261};
262
Brian Salomonbaaf4392017-06-15 09:59:23 -0400263} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700264
Brian Salomon91326c32017-08-09 16:02:19 -0400265void AAStrokeRectOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400266 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(target->caps().shaderCaps(),
267 fHelper.compatibleWithAlphaAsCoverage(),
bungeman06ca8ec2016-06-09 08:01:03 -0700268 this->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400269 fHelper.usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700270 if (!gp) {
271 SkDebugf("Couldn't create GrGeometryProcessor\n");
272 return;
273 }
274
Brian Salomon92be2f72018-06-19 14:33:47 -0400275 size_t vertexStride = fHelper.compatibleWithAlphaAsCoverage()
276 ? sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
277 : sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr);
joshualitt9ff64252015-08-10 09:03:51 -0700278
Brian Salomon92be2f72018-06-19 14:33:47 -0400279 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
joshualitt9ff64252015-08-10 09:03:51 -0700280 int innerVertexNum = 4;
281 int outerVertexNum = this->miterStroke() ? 4 : 8;
282 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
283 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500284 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700285
Brian Salomon7eae3e02018-08-07 14:02:38 +0000286 sk_sp<const GrBuffer> indexBuffer =
287 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
288 PatternHelper helper(target, GrPrimitiveType::kTriangles, vertexStride, indexBuffer.get(),
289 verticesPerInstance, indicesPerInstance, instanceCount);
290 void* vertices = helper.vertices();
joshualitt9ff64252015-08-10 09:03:51 -0700291 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500292 SkDebugf("Could not allocate vertices\n");
293 return;
294 }
joshualitt9ff64252015-08-10 09:03:51 -0700295
296 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500297 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700298 this->generateAAStrokeRectGeometry(vertices,
299 i * verticesPerInstance * vertexStride,
300 vertexStride,
301 outerVertexNum,
302 innerVertexNum,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400303 info.fColor.toGrColor(), // TODO4F
Brian Salomon8c5bad32016-12-20 14:43:36 -0500304 info.fDevOutside,
305 info.fDevOutsideAssist,
306 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700307 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500308 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400309 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700310 }
Brian Salomon49348902018-06-26 09:12:38 -0400311 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000312 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt9ff64252015-08-10 09:03:51 -0700313}
314
Brian Salomond28a79d2017-10-16 13:01:07 -0400315sk_sp<const GrBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
316 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700317 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500318 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700319 static const uint16_t gMiterIndices[] = {
320 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
321 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
322 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
323 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
324
325 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
326 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
327 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
328 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
329
330 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
331 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
332 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
333 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
334 };
Brian Salomon6a639042016-12-14 11:08:17 -0500335 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700336 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
337 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400338 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500339 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
340 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700341 } else {
342 /**
343 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
344 * from the first index. The index layout:
345 * outer AA line: 0~3, 4~7
346 * outer edge: 8~11, 12~15
347 * inner edge: 16~19
348 * inner AA line: 20~23
349 * Following comes a bevel-stroke rect and its indices:
350 *
351 * 4 7
352 * *********************************
353 * * ______________________________ *
354 * * / 12 15 \ *
355 * * / \ *
356 * 0 * |8 16_____________________19 11 | * 3
357 * * | | | | *
358 * * | | **************** | | *
359 * * | | * 20 23 * | | *
360 * * | | * * | | *
361 * * | | * 21 22 * | | *
362 * * | | **************** | | *
363 * * | |____________________| | *
364 * 1 * |9 17 18 10| * 2
365 * * \ / *
366 * * \13 __________________________14/ *
367 * * *
368 * **********************************
369 * 5 6
370 */
Brian Salomon6a639042016-12-14 11:08:17 -0500371 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700372 static const uint16_t gBevelIndices[] = {
373 // Draw outer AA, from outer AA line to outer edge, shift is 0.
374 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
375 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
376 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
377 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
378 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
379 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
380 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
381 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
382
383 // Draw the stroke, from outer edge to inner edge, shift is 8.
384 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
385 1 + 8, 5 + 8, 9 + 8,
386 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
387 6 + 8, 2 + 8, 10 + 8,
388 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
389 3 + 8, 7 + 8, 11 + 8,
390 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
391 4 + 8, 0 + 8, 8 + 8,
392
393 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
394 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
395 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
396 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
397 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
398 };
Brian Salomon6a639042016-12-14 11:08:17 -0500399 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700400 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
401
402 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400403 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500404 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
405 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700406 }
407}
408
Brian Salomon7eae3e02018-08-07 14:02:38 +0000409GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500410 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700411
Brian Salomonbaaf4392017-06-15 09:59:23 -0400412 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000413 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700414 }
415
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500416 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700417 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000418 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700419 }
420
421 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400422 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
423 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000424 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700425 }
426
Brian Salomon8c5bad32016-12-20 14:43:36 -0500427 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000428 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700429}
430
joshualitt11edad92015-09-22 10:32:28 -0700431static void setup_scale(int* scale, SkScalar inset) {
432 if (inset < SK_ScalarHalf) {
433 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
434 SkASSERT(*scale >= 0 && *scale <= 255);
435 } else {
436 *scale = 0xff;
437 }
438}
439
Brian Salomon6a639042016-12-14 11:08:17 -0500440void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
441 size_t offset,
442 size_t vertexStride,
443 int outerVertexNum,
444 int innerVertexNum,
445 GrColor color,
446 const SkRect& devOutside,
447 const SkRect& devOutsideAssist,
448 const SkRect& devInside,
449 bool miterStroke,
450 bool degenerate,
451 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700452 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
453
454 // We create vertices for four nested rectangles. There are two ramps from 0 to full
455 // coverage, one on the exterior of the stroke and the other on the interior.
456 // The following pointers refer to the four rects, from outermost to innermost.
457 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
458 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
459 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500460 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
461 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700462
463#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
464 // TODO: this only really works if the X & Y margins are the same all around
465 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700466 SkScalar inset;
467 if (!degenerate) {
468 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
469 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
470 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
471 if (miterStroke) {
472 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
473 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500474 inset = SK_ScalarHalf *
475 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700476 }
477 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700478 } else {
joshualitt11edad92015-09-22 10:32:28 -0700479 // TODO use real devRect here
480 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500481 inset = SK_ScalarHalf *
482 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700483 }
joshualitt9ff64252015-08-10 09:03:51 -0700484#else
joshualitt11edad92015-09-22 10:32:28 -0700485 SkScalar inset;
486 if (!degenerate) {
487 inset = SK_ScalarHalf;
488 } else {
489 // TODO use real devRect here
490 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500491 inset = SK_ScalarHalf *
492 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700493 }
joshualitt9ff64252015-08-10 09:03:51 -0700494#endif
495
496 if (miterStroke) {
497 // outermost
498 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
499 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500500 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700501 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500502 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700503 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500504 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700505 } else {
506 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500507 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500508 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500509 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500510 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500511 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700512 }
joshualitt9ff64252015-08-10 09:03:51 -0700513 } else {
514 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500515 SkPoint* fan1AssistPos =
516 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700517 // outermost
518 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
519 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
520 -SK_ScalarHalf);
521 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500522 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
523 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700524 if (!degenerate) {
525 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500526 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700527 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500528 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700529 } else {
530 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500531 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500532 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500533 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500534 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500535 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700536 }
joshualitt9ff64252015-08-10 09:03:51 -0700537 }
538
539 // Make verts point to vertex color and then set all the color and coverage vertex attrs
540 // values. The outermost rect has 0 coverage
541 verts += sizeof(SkPoint);
542 for (int i = 0; i < outerVertexNum; ++i) {
543 if (tweakAlphaForCoverage) {
544 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
545 } else {
546 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
547 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
548 }
549 }
550
551 // scale is the coverage for the the inner two rects.
552 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700553 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700554
555 float innerCoverage = GrNormalizeByteToFloat(scale);
556 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
557
558 verts += outerVertexNum * vertexStride;
559 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
560 if (tweakAlphaForCoverage) {
561 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
562 } else {
563 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700564 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700565 }
566 }
567
joshualitt11edad92015-09-22 10:32:28 -0700568 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
569 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700570 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700571 if (!degenerate) {
572 innerCoverage = 0;
573 scaledColor = 0;
574 }
575
joshualitt9ff64252015-08-10 09:03:51 -0700576 for (int i = 0; i < innerVertexNum; ++i) {
577 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700578 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700579 } else {
580 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700581 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700582 }
583 }
584}
585
Brian Salomonbaaf4392017-06-15 09:59:23 -0400586namespace GrRectOpFactory {
joshualitt3566d442015-09-18 07:12:55 -0700587
Robert Phillips7c525e62018-06-12 10:11:12 -0400588std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrContext* context,
589 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400590 const SkMatrix& viewMatrix,
591 const SkRect rects[2]) {
592 SkASSERT(viewMatrix.rectStaysRect());
593 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
594
595 SkRect devOutside, devInside;
596 viewMatrix.mapRect(&devOutside, rects[0]);
597 viewMatrix.mapRect(&devInside, rects[1]);
598 if (devInside.isEmpty()) {
599 if (devOutside.isEmpty()) {
600 return nullptr;
601 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400602 return MakeAAFill(context, std::move(paint), viewMatrix, rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400603 }
604
Robert Phillips7c525e62018-06-12 10:11:12 -0400605 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700606}
607
Robert Phillips7c525e62018-06-12 10:11:12 -0400608std::unique_ptr<GrDrawOp> MakeAAStroke(GrContext* context,
609 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400610 const SkMatrix& viewMatrix,
611 const SkRect& rect,
612 const SkStrokeRec& stroke) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400613 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700614}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400615
616} // namespace GrRectOpFactory
joshualitt3566d442015-09-18 07:12:55 -0700617
joshualitt9ff64252015-08-10 09:03:51 -0700618///////////////////////////////////////////////////////////////////////////////////////////////////
619
Hal Canary6f6961e2017-01-31 13:50:44 -0500620#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700621
Brian Salomon5ec9def2016-12-20 15:34:05 -0500622#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700623
Brian Salomonbaaf4392017-06-15 09:59:23 -0400624GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700625 bool miterStroke = random->nextBool();
626
bsalomon40ef4852016-05-02 13:22:13 -0700627 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500628 SkRect rect =
629 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700630 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
631 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700632
bsalomon40ef4852016-05-02 13:22:13 -0700633 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
634 rec.setStrokeStyle(strokeWidth);
635 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500636 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700637 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Robert Phillips7c525e62018-06-12 10:11:12 -0400638 return GrRectOpFactory::MakeAAStroke(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700639}
640
641#endif