blob: f2be50a4ac0cb245b69b70920eb1106e4b4256e9 [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 Osmancf860852018-10-31 14:04:39 -0400136 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400137 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 Osmancf860852018-10-31 14:04:39 -0400162 AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400163 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 Osman9a390ac2018-11-12 09:47:48 -0500190#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500191 SkString dumpInfo() const override {
192 SkString string;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500193 for (const auto& info : fRects) {
Brian Salomon6a639042016-12-14 11:08:17 -0500194 string.appendf(
195 "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
196 "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
197 "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d",
Brian Osmancf860852018-10-31 14:04:39 -0400198 info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500199 info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft,
200 info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight,
201 info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop,
202 info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate);
Brian Salomon7c3e7182016-12-01 09:35:30 -0500203 }
Brian Salomon82dfd3d2017-06-14 12:30:35 -0400204 string += fHelper.dumpInfo();
205 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500206 return string;
207 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500208#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500209
Brian Salomonbaaf4392017-06-15 09:59:23 -0400210 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
Brian Salomona0485d92017-06-14 19:08:01 -0400211
Brian Osman532b3f92018-07-11 10:02:07 -0400212 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
213 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400214 &fRects.back().fColor);
Brian Salomona0485d92017-06-14 19:08:01 -0400215 }
Brian Salomonbaaf4392017-06-15 09:59:23 -0400216
217private:
Brian Salomon91326c32017-08-09 16:02:19 -0400218 void onPrepareDraws(Target*) override;
joshualittaa37a962015-09-18 13:03:25 -0700219
joshualitt3566d442015-09-18 07:12:55 -0700220 static const int kMiterIndexCnt = 3 * 24;
221 static const int kMiterVertexCnt = 16;
222 static const int kNumMiterRectsInIndexBuffer = 256;
223
224 static const int kBevelIndexCnt = 48 + 36 + 24;
225 static const int kBevelVertexCnt = 24;
226 static const int kNumBevelRectsInIndexBuffer = 256;
227
Brian Salomond28a79d2017-10-16 13:01:07 -0400228 static sk_sp<const GrBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
joshualitt3566d442015-09-18 07:12:55 -0700229
joshualittaa37a962015-09-18 13:03:25 -0700230 const SkMatrix& viewMatrix() const { return fViewMatrix; }
231 bool miterStroke() const { return fMiterStroke; }
joshualitt3566d442015-09-18 07:12:55 -0700232
Brian Salomon7eae3e02018-08-07 14:02:38 +0000233 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
joshualitt3566d442015-09-18 07:12:55 -0700234
235 void generateAAStrokeRectGeometry(void* vertices,
236 size_t offset,
237 size_t vertexStride,
238 int outerVertexNum,
239 int innerVertexNum,
240 GrColor color,
241 const SkRect& devOutside,
242 const SkRect& devOutsideAssist,
243 const SkRect& devInside,
244 bool miterStroke,
joshualitt11edad92015-09-22 10:32:28 -0700245 bool degenerate,
joshualitt3566d442015-09-18 07:12:55 -0700246 bool tweakAlphaForCoverage) const;
247
bsalomon8b7a9e12016-07-06 13:06:22 -0700248 // TODO support AA rotated stroke rects by copying around view matrices
Brian Salomon8c5bad32016-12-20 14:43:36 -0500249 struct RectInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400250 SkPMColor4f fColor;
bsalomon8b7a9e12016-07-06 13:06:22 -0700251 SkRect fDevOutside;
252 SkRect fDevOutsideAssist;
253 SkRect fDevInside;
254 bool fDegenerate;
255 };
256
Brian Salomonbaaf4392017-06-15 09:59:23 -0400257 Helper fHelper;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500258 SkSTArray<1, RectInfo, true> fRects;
joshualittaa37a962015-09-18 13:03:25 -0700259 SkMatrix fViewMatrix;
260 bool fMiterStroke;
joshualitt3566d442015-09-18 07:12:55 -0700261
Brian Salomonbaaf4392017-06-15 09:59:23 -0400262 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700263};
264
Brian Salomonbaaf4392017-06-15 09:59:23 -0400265} // anonymous namespace
joshualitt9ff64252015-08-10 09:03:51 -0700266
Brian Salomon91326c32017-08-09 16:02:19 -0400267void AAStrokeRectOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400268 sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(target->caps().shaderCaps(),
269 fHelper.compatibleWithAlphaAsCoverage(),
bungeman06ca8ec2016-06-09 08:01:03 -0700270 this->viewMatrix(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400271 fHelper.usesLocalCoords()));
joshualitt9ff64252015-08-10 09:03:51 -0700272 if (!gp) {
273 SkDebugf("Couldn't create GrGeometryProcessor\n");
274 return;
275 }
276
Brian Salomon92be2f72018-06-19 14:33:47 -0400277 size_t vertexStride = fHelper.compatibleWithAlphaAsCoverage()
278 ? sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
279 : sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr);
joshualitt9ff64252015-08-10 09:03:51 -0700280
Brian Salomon92be2f72018-06-19 14:33:47 -0400281 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
joshualitt9ff64252015-08-10 09:03:51 -0700282 int innerVertexNum = 4;
283 int outerVertexNum = this->miterStroke() ? 4 : 8;
284 int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2;
285 int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
Brian Salomon8c5bad32016-12-20 14:43:36 -0500286 int instanceCount = fRects.count();
joshualitt9ff64252015-08-10 09:03:51 -0700287
Brian Salomon7eae3e02018-08-07 14:02:38 +0000288 sk_sp<const GrBuffer> indexBuffer =
289 GetIndexBuffer(target->resourceProvider(), this->miterStroke());
290 PatternHelper helper(target, GrPrimitiveType::kTriangles, vertexStride, indexBuffer.get(),
291 verticesPerInstance, indicesPerInstance, instanceCount);
292 void* vertices = helper.vertices();
joshualitt9ff64252015-08-10 09:03:51 -0700293 if (!vertices || !indexBuffer) {
Brian Salomon6a639042016-12-14 11:08:17 -0500294 SkDebugf("Could not allocate vertices\n");
295 return;
296 }
joshualitt9ff64252015-08-10 09:03:51 -0700297
298 for (int i = 0; i < instanceCount; i++) {
Brian Salomon8c5bad32016-12-20 14:43:36 -0500299 const RectInfo& info = fRects[i];
joshualitt9ff64252015-08-10 09:03:51 -0700300 this->generateAAStrokeRectGeometry(vertices,
301 i * verticesPerInstance * vertexStride,
302 vertexStride,
303 outerVertexNum,
304 innerVertexNum,
Brian Osmancf860852018-10-31 14:04:39 -0400305 info.fColor.toBytes_RGBA(), // TODO4F
Brian Salomon8c5bad32016-12-20 14:43:36 -0500306 info.fDevOutside,
307 info.fDevOutsideAssist,
308 info.fDevInside,
joshualittaa37a962015-09-18 13:03:25 -0700309 fMiterStroke,
Brian Salomon8c5bad32016-12-20 14:43:36 -0500310 info.fDegenerate,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400311 fHelper.compatibleWithAlphaAsCoverage());
joshualitt9ff64252015-08-10 09:03:51 -0700312 }
Brian Salomon49348902018-06-26 09:12:38 -0400313 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000314 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt9ff64252015-08-10 09:03:51 -0700315}
316
Brian Salomond28a79d2017-10-16 13:01:07 -0400317sk_sp<const GrBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
318 bool miterStroke) {
joshualitt9ff64252015-08-10 09:03:51 -0700319 if (miterStroke) {
Brian Salomon6a639042016-12-14 11:08:17 -0500320 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700321 static const uint16_t gMiterIndices[] = {
322 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0,
323 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0,
324 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0,
325 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
326
327 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4,
328 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4,
329 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4,
330 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
331
332 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8,
333 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8,
334 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8,
335 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8,
336 };
Brian Salomon6a639042016-12-14 11:08:17 -0500337 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700338 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt);
339 GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400340 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500341 gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt,
342 gMiterIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700343 } else {
344 /**
345 * As in miter-stroke, index = a + b, and a is the current index, b is the shift
346 * from the first index. The index layout:
347 * outer AA line: 0~3, 4~7
348 * outer edge: 8~11, 12~15
349 * inner edge: 16~19
350 * inner AA line: 20~23
351 * Following comes a bevel-stroke rect and its indices:
352 *
353 * 4 7
354 * *********************************
355 * * ______________________________ *
356 * * / 12 15 \ *
357 * * / \ *
358 * 0 * |8 16_____________________19 11 | * 3
359 * * | | | | *
360 * * | | **************** | | *
361 * * | | * 20 23 * | | *
362 * * | | * * | | *
363 * * | | * 21 22 * | | *
364 * * | | **************** | | *
365 * * | |____________________| | *
366 * 1 * |9 17 18 10| * 2
367 * * \ / *
368 * * \13 __________________________14/ *
369 * * *
370 * **********************************
371 * 5 6
372 */
Brian Salomon6a639042016-12-14 11:08:17 -0500373 // clang-format off
joshualitt9ff64252015-08-10 09:03:51 -0700374 static const uint16_t gBevelIndices[] = {
375 // Draw outer AA, from outer AA line to outer edge, shift is 0.
376 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0,
377 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0,
378 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0,
379 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0,
380 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0,
381 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0,
382 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0,
383 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0,
384
385 // Draw the stroke, from outer edge to inner edge, shift is 8.
386 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8,
387 1 + 8, 5 + 8, 9 + 8,
388 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8,
389 6 + 8, 2 + 8, 10 + 8,
390 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8,
391 3 + 8, 7 + 8, 11 + 8,
392 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8,
393 4 + 8, 0 + 8, 8 + 8,
394
395 // Draw the inner AA, from inner edge to inner AA line, shift is 16.
396 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16,
397 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16,
398 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16,
399 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16,
400 };
Brian Salomon6a639042016-12-14 11:08:17 -0500401 // clang-format on
joshualitt9ff64252015-08-10 09:03:51 -0700402 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt);
403
404 GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400405 return resourceProvider->findOrCreatePatternedIndexBuffer(
Brian Salomon6a639042016-12-14 11:08:17 -0500406 gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt,
407 gBevelIndexBufferKey);
joshualitt9ff64252015-08-10 09:03:51 -0700408 }
409}
410
Brian Salomon7eae3e02018-08-07 14:02:38 +0000411GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomon6a639042016-12-14 11:08:17 -0500412 AAStrokeRectOp* that = t->cast<AAStrokeRectOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700413
Brian Salomonbaaf4392017-06-15 09:59:23 -0400414 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000415 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700416 }
417
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500418 // TODO combine across miterstroke changes
joshualitt9ff64252015-08-10 09:03:51 -0700419 if (this->miterStroke() != that->miterStroke()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000420 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700421 }
422
423 // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses
Brian Salomonbaaf4392017-06-15 09:59:23 -0400424 // local coords then we won't be able to combine. TODO: Upload local coords as an attribute.
425 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000426 return CombineResult::kCannotCombine;
joshualitt9ff64252015-08-10 09:03:51 -0700427 }
428
Brian Salomon8c5bad32016-12-20 14:43:36 -0500429 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000430 return CombineResult::kMerged;
joshualitt9ff64252015-08-10 09:03:51 -0700431}
432
joshualitt11edad92015-09-22 10:32:28 -0700433static void setup_scale(int* scale, SkScalar inset) {
434 if (inset < SK_ScalarHalf) {
435 *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf));
436 SkASSERT(*scale >= 0 && *scale <= 255);
437 } else {
438 *scale = 0xff;
439 }
440}
441
Brian Salomon6a639042016-12-14 11:08:17 -0500442void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices,
443 size_t offset,
444 size_t vertexStride,
445 int outerVertexNum,
446 int innerVertexNum,
447 GrColor color,
448 const SkRect& devOutside,
449 const SkRect& devOutsideAssist,
450 const SkRect& devInside,
451 bool miterStroke,
452 bool degenerate,
453 bool tweakAlphaForCoverage) const {
joshualitt9ff64252015-08-10 09:03:51 -0700454 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
455
456 // We create vertices for four nested rectangles. There are two ramps from 0 to full
457 // coverage, one on the exterior of the stroke and the other on the interior.
458 // The following pointers refer to the four rects, from outermost to innermost.
459 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
460 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride);
461 SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500462 SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(
463 verts + (2 * outerVertexNum + innerVertexNum) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700464
465#ifndef SK_IGNORE_THIN_STROKED_RECT_FIX
466 // TODO: this only really works if the X & Y margins are the same all around
467 // the rect (or if they are all >= 1.0).
joshualitt11edad92015-09-22 10:32:28 -0700468 SkScalar inset;
469 if (!degenerate) {
470 inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight);
471 inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft);
472 inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop);
473 if (miterStroke) {
474 inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom);
475 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500476 inset = SK_ScalarHalf *
477 SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom);
joshualitt11edad92015-09-22 10:32:28 -0700478 }
479 SkASSERT(inset >= 0);
joshualitt9ff64252015-08-10 09:03:51 -0700480 } else {
joshualitt11edad92015-09-22 10:32:28 -0700481 // TODO use real devRect here
482 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500483 inset = SK_ScalarHalf *
484 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt9ff64252015-08-10 09:03:51 -0700485 }
joshualitt9ff64252015-08-10 09:03:51 -0700486#else
joshualitt11edad92015-09-22 10:32:28 -0700487 SkScalar inset;
488 if (!degenerate) {
489 inset = SK_ScalarHalf;
490 } else {
491 // TODO use real devRect here
492 inset = SkMinScalar(devOutside.width(), SK_Scalar1);
Brian Salomon6a639042016-12-14 11:08:17 -0500493 inset = SK_ScalarHalf *
494 SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height()));
joshualitt11edad92015-09-22 10:32:28 -0700495 }
joshualitt9ff64252015-08-10 09:03:51 -0700496#endif
497
498 if (miterStroke) {
499 // outermost
500 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
501 // inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500502 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700503 if (!degenerate) {
Brian Salomon6a639042016-12-14 11:08:17 -0500504 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700505 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500506 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700507 } else {
508 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500509 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500510 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500511 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500512 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500513 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700514 }
joshualitt9ff64252015-08-10 09:03:51 -0700515 } else {
516 SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
Brian Salomon6a639042016-12-14 11:08:17 -0500517 SkPoint* fan1AssistPos =
518 reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride);
joshualitt9ff64252015-08-10 09:03:51 -0700519 // outermost
520 set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf);
521 set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf,
522 -SK_ScalarHalf);
523 // outer one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500524 set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset);
525 set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset);
joshualitt11edad92015-09-22 10:32:28 -0700526 if (!degenerate) {
527 // inner one of the inner two
Brian Salomon6a639042016-12-14 11:08:17 -0500528 set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset);
joshualitt11edad92015-09-22 10:32:28 -0700529 // innermost
Brian Salomon6a639042016-12-14 11:08:17 -0500530 set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf);
joshualitt11edad92015-09-22 10:32:28 -0700531 } else {
532 // When the interior rect has become degenerate we smoosh to a single point
Brian Salomon6a639042016-12-14 11:08:17 -0500533 SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom);
Cary Clark74f623d2017-11-06 20:02:02 -0500534 SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500535 devInside.fBottom, vertexStride);
Cary Clark74f623d2017-11-06 20:02:02 -0500536 SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight,
Brian Salomon6a639042016-12-14 11:08:17 -0500537 devInside.fBottom, vertexStride);
joshualitt11edad92015-09-22 10:32:28 -0700538 }
joshualitt9ff64252015-08-10 09:03:51 -0700539 }
540
541 // Make verts point to vertex color and then set all the color and coverage vertex attrs
542 // values. The outermost rect has 0 coverage
543 verts += sizeof(SkPoint);
544 for (int i = 0; i < outerVertexNum; ++i) {
545 if (tweakAlphaForCoverage) {
546 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
547 } else {
548 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
549 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0;
550 }
551 }
552
553 // scale is the coverage for the the inner two rects.
554 int scale;
joshualitt11edad92015-09-22 10:32:28 -0700555 setup_scale(&scale, inset);
joshualitt9ff64252015-08-10 09:03:51 -0700556
557 float innerCoverage = GrNormalizeByteToFloat(scale);
558 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
559
560 verts += outerVertexNum * vertexStride;
561 for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) {
562 if (tweakAlphaForCoverage) {
563 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
564 } else {
565 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700566 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700567 }
568 }
569
joshualitt11edad92015-09-22 10:32:28 -0700570 // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the
571 // scaled coverage
joshualitt9ff64252015-08-10 09:03:51 -0700572 verts += (outerVertexNum + innerVertexNum) * vertexStride;
joshualitt11edad92015-09-22 10:32:28 -0700573 if (!degenerate) {
574 innerCoverage = 0;
575 scaledColor = 0;
576 }
577
joshualitt9ff64252015-08-10 09:03:51 -0700578 for (int i = 0; i < innerVertexNum; ++i) {
579 if (tweakAlphaForCoverage) {
joshualitt11edad92015-09-22 10:32:28 -0700580 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
joshualitt9ff64252015-08-10 09:03:51 -0700581 } else {
582 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
joshualitt11edad92015-09-22 10:32:28 -0700583 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage;
joshualitt9ff64252015-08-10 09:03:51 -0700584 }
585 }
586}
587
Brian Salomonbaaf4392017-06-15 09:59:23 -0400588namespace GrRectOpFactory {
joshualitt3566d442015-09-18 07:12:55 -0700589
Robert Phillips7c525e62018-06-12 10:11:12 -0400590std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrContext* context,
591 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400592 const SkMatrix& viewMatrix,
593 const SkRect rects[2]) {
594 SkASSERT(viewMatrix.rectStaysRect());
595 SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty());
596
597 SkRect devOutside, devInside;
598 viewMatrix.mapRect(&devOutside, rects[0]);
599 viewMatrix.mapRect(&devInside, rects[1]);
600 if (devInside.isEmpty()) {
601 if (devOutside.isEmpty()) {
602 return nullptr;
603 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400604 return MakeAAFill(context, std::move(paint), viewMatrix, rects[0]);
Brian Salomonbaaf4392017-06-15 09:59:23 -0400605 }
606
Robert Phillips7c525e62018-06-12 10:11:12 -0400607 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside);
joshualittaa37a962015-09-18 13:03:25 -0700608}
609
Robert Phillips7c525e62018-06-12 10:11:12 -0400610std::unique_ptr<GrDrawOp> MakeAAStroke(GrContext* context,
611 GrPaint&& paint,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400612 const SkMatrix& viewMatrix,
613 const SkRect& rect,
614 const SkStrokeRec& stroke) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400615 return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke);
joshualitt10cae832015-09-22 12:50:33 -0700616}
Brian Salomonbaaf4392017-06-15 09:59:23 -0400617
618} // namespace GrRectOpFactory
joshualitt3566d442015-09-18 07:12:55 -0700619
joshualitt9ff64252015-08-10 09:03:51 -0700620///////////////////////////////////////////////////////////////////////////////////////////////////
621
Hal Canary6f6961e2017-01-31 13:50:44 -0500622#if GR_TEST_UTILS
joshualitt9ff64252015-08-10 09:03:51 -0700623
Brian Salomon5ec9def2016-12-20 15:34:05 -0500624#include "GrDrawOpTest.h"
joshualitt9ff64252015-08-10 09:03:51 -0700625
Brian Salomonbaaf4392017-06-15 09:59:23 -0400626GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) {
joshualitt9ff64252015-08-10 09:03:51 -0700627 bool miterStroke = random->nextBool();
628
bsalomon40ef4852016-05-02 13:22:13 -0700629 // Create either a empty rect or a non-empty rect.
Brian Salomon6a639042016-12-14 11:08:17 -0500630 SkRect rect =
631 random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0);
bsalomon40ef4852016-05-02 13:22:13 -0700632 SkScalar minDim = SkMinScalar(rect.width(), rect.height());
633 SkScalar strokeWidth = random->nextUScalar1() * minDim;
joshualitt9ff64252015-08-10 09:03:51 -0700634
bsalomon40ef4852016-05-02 13:22:13 -0700635 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle);
636 rec.setStrokeStyle(strokeWidth);
637 rec.setStrokeParams(SkPaint::kButt_Cap,
Brian Salomon6a639042016-12-14 11:08:17 -0500638 miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f);
bsalomon40ef4852016-05-02 13:22:13 -0700639 SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random);
Robert Phillips7c525e62018-06-12 10:11:12 -0400640 return GrRectOpFactory::MakeAAStroke(context, std::move(paint), matrix, rect, rec);
joshualitt9ff64252015-08-10 09:03:51 -0700641}
642
643#endif