1 | /* |
2 | * Copyright 2018 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 | |
8 | #include "src/gpu/ops/GrStrokeRectOp.h" |
9 | |
10 | #include "include/core/SkStrokeRec.h" |
11 | #include "include/private/GrResourceKey.h" |
12 | #include "include/utils/SkRandom.h" |
13 | #include "src/gpu/GrCaps.h" |
14 | #include "src/gpu/GrColor.h" |
15 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
16 | #include "src/gpu/GrDrawOpTest.h" |
17 | #include "src/gpu/GrOpFlushState.h" |
18 | #include "src/gpu/GrProgramInfo.h" |
19 | #include "src/gpu/GrResourceProvider.h" |
20 | #include "src/gpu/GrVertexWriter.h" |
21 | #include "src/gpu/ops/GrFillRectOp.h" |
22 | #include "src/gpu/ops/GrMeshDrawOp.h" |
23 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
24 | |
25 | namespace { |
26 | |
27 | // We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter |
28 | // limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only |
29 | // supported when using an AA stroke. |
30 | inline static bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) { |
31 | SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style || |
32 | stroke.getStyle() == SkStrokeRec::kHairline_Style); |
33 | // For hairlines, make bevel and round joins appear the same as mitered ones. |
34 | if (!stroke.getWidth()) { |
35 | *isMiter = true; |
36 | return true; |
37 | } |
38 | if (stroke.getJoin() == SkPaint::kBevel_Join) { |
39 | *isMiter = false; |
40 | return aa == GrAA::kYes; // bevel only supported with AA |
41 | } |
42 | if (stroke.getJoin() == SkPaint::kMiter_Join) { |
43 | *isMiter = stroke.getMiter() >= SK_ScalarSqrt2; |
44 | // Supported under non-AA only if it remains mitered |
45 | return aa == GrAA::kYes || *isMiter; |
46 | } |
47 | return false; |
48 | } |
49 | |
50 | |
51 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
52 | // Non-AA Stroking |
53 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
54 | |
55 | /* create a triangle strip that strokes the specified rect. There are 8 |
56 | unique vertices, but we repeat the last 2 to close up. Alternatively we |
57 | could use an indices array, and then only send 8 verts, but not sure that |
58 | would be faster. |
59 | */ |
60 | static void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
61 | const SkScalar rad = SkScalarHalf(width); |
62 | |
63 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
64 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
65 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
66 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
67 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
68 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
69 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
70 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
71 | verts[8] = verts[0]; |
72 | verts[9] = verts[1]; |
73 | |
74 | // TODO: we should be catching this higher up the call stack and just draw a single |
75 | // non-AA rect |
76 | if (2*rad >= rect.width()) { |
77 | verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX(); |
78 | } |
79 | if (2*rad >= rect.height()) { |
80 | verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY(); |
81 | } |
82 | } |
83 | |
84 | class NonAAStrokeRectOp final : public GrMeshDrawOp { |
85 | private: |
86 | using Helper = GrSimpleMeshDrawOpHelper; |
87 | |
88 | public: |
89 | DEFINE_OP_CLASS_ID |
90 | |
91 | const char* name() const override { return "NonAAStrokeRectOp" ; } |
92 | |
93 | void visitProxies(const VisitProxyFunc& func) const override { |
94 | if (fProgramInfo) { |
95 | fProgramInfo->visitFPProxies(func); |
96 | } else { |
97 | fHelper.visitProxies(func); |
98 | } |
99 | } |
100 | |
101 | #ifdef SK_DEBUG |
102 | SkString dumpInfo() const override { |
103 | SkString string; |
104 | string.appendf( |
105 | "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
106 | "StrokeWidth: %.2f\n" , |
107 | fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom, |
108 | fStrokeWidth); |
109 | string += fHelper.dumpInfo(); |
110 | string += INHERITED::dumpInfo(); |
111 | return string; |
112 | } |
113 | #endif |
114 | |
115 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
116 | GrPaint&& paint, |
117 | const SkMatrix& viewMatrix, |
118 | const SkRect& rect, |
119 | const SkStrokeRec& stroke, |
120 | GrAAType aaType) { |
121 | bool isMiter; |
122 | if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) { |
123 | return nullptr; |
124 | } |
125 | Helper::InputFlags inputFlags = Helper::InputFlags::kNone; |
126 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
127 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not |
128 | // when MSAA is enabled because it can cause ugly artifacts. |
129 | if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) { |
130 | inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters; |
131 | } |
132 | return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags, |
133 | viewMatrix, rect, |
134 | stroke, aaType); |
135 | } |
136 | |
137 | NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
138 | Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect, |
139 | const SkStrokeRec& stroke, GrAAType aaType) |
140 | : INHERITED(ClassID()) |
141 | , fHelper(helperArgs, aaType, inputFlags) { |
142 | fColor = color; |
143 | fViewMatrix = viewMatrix; |
144 | fRect = rect; |
145 | // Sort the rect for hairlines |
146 | fRect.sort(); |
147 | fStrokeWidth = stroke.getWidth(); |
148 | |
149 | SkScalar rad = SkScalarHalf(fStrokeWidth); |
150 | SkRect bounds = rect; |
151 | bounds.outset(rad, rad); |
152 | |
153 | // If our caller snaps to pixel centers then we have to round out the bounds |
154 | if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) { |
155 | SkASSERT(!fStrokeWidth || aaType == GrAAType::kNone); |
156 | viewMatrix.mapRect(&bounds); |
157 | // We want to be consistent with how we snap non-aa lines. To match what we do in |
158 | // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a |
159 | // pixel to force us to pixel centers. |
160 | bounds.setLTRB(SkScalarFloorToScalar(bounds.fLeft), |
161 | SkScalarFloorToScalar(bounds.fTop), |
162 | SkScalarFloorToScalar(bounds.fRight), |
163 | SkScalarFloorToScalar(bounds.fBottom)); |
164 | bounds.offset(0.5f, 0.5f); |
165 | this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo); |
166 | } else { |
167 | HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes; |
168 | this->setTransformedBounds(bounds, fViewMatrix, aaBloat, |
169 | fStrokeWidth ? IsHairline::kNo : IsHairline::kYes); |
170 | } |
171 | } |
172 | |
173 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
174 | |
175 | GrProcessorSet::Analysis finalize( |
176 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
177 | GrClampType clampType) override { |
178 | // This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
179 | return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType, |
180 | GrProcessorAnalysisCoverage::kNone, &fColor, nullptr); |
181 | } |
182 | |
183 | private: |
184 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
185 | |
186 | void onCreateProgramInfo(const GrCaps* caps, |
187 | SkArenaAlloc* arena, |
188 | const GrSurfaceProxyView* writeView, |
189 | GrAppliedClip&& clip, |
190 | const GrXferProcessor::DstProxyView& dstProxyView) override { |
191 | GrGeometryProcessor* gp; |
192 | { |
193 | using namespace GrDefaultGeoProcFactory; |
194 | Color color(fColor); |
195 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
196 | ? LocalCoords::kUsePosition_Type |
197 | : LocalCoords::kUnused_Type; |
198 | gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType, |
199 | fViewMatrix); |
200 | } |
201 | |
202 | GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip |
203 | : GrPrimitiveType::kLineStrip; |
204 | |
205 | fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(clip), |
206 | dstProxyView, gp, primType); |
207 | } |
208 | |
209 | void onPrepareDraws(Target* target) override { |
210 | if (!fProgramInfo) { |
211 | this->createProgramInfo(target); |
212 | } |
213 | |
214 | size_t kVertexStride = fProgramInfo->primProc().vertexStride(); |
215 | int vertexCount = kVertsPerHairlineRect; |
216 | if (fStrokeWidth > 0) { |
217 | vertexCount = kVertsPerStrokeRect; |
218 | } |
219 | |
220 | sk_sp<const GrBuffer> vertexBuffer; |
221 | int firstVertex; |
222 | |
223 | void* verts = |
224 | target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex); |
225 | |
226 | if (!verts) { |
227 | SkDebugf("Could not allocate vertices\n" ); |
228 | return; |
229 | } |
230 | |
231 | SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
232 | |
233 | if (fStrokeWidth > 0) { |
234 | init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth); |
235 | } else { |
236 | // hairline |
237 | vertex[0].set(fRect.fLeft, fRect.fTop); |
238 | vertex[1].set(fRect.fRight, fRect.fTop); |
239 | vertex[2].set(fRect.fRight, fRect.fBottom); |
240 | vertex[3].set(fRect.fLeft, fRect.fBottom); |
241 | vertex[4].set(fRect.fLeft, fRect.fTop); |
242 | } |
243 | |
244 | fMesh = target->allocMesh(); |
245 | fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex); |
246 | } |
247 | |
248 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
249 | if (!fMesh) { |
250 | return; |
251 | } |
252 | |
253 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
254 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
255 | flushState->drawMesh(*fMesh); |
256 | } |
257 | |
258 | // TODO: override onCombineIfPossible |
259 | |
260 | Helper fHelper; |
261 | SkPMColor4f fColor; |
262 | SkMatrix fViewMatrix; |
263 | SkRect fRect; |
264 | SkScalar fStrokeWidth; |
265 | GrSimpleMesh* fMesh = nullptr; |
266 | GrProgramInfo* fProgramInfo = nullptr; |
267 | |
268 | const static int kVertsPerHairlineRect = 5; |
269 | const static int kVertsPerStrokeRect = 10; |
270 | |
271 | typedef GrMeshDrawOp INHERITED; |
272 | }; |
273 | |
274 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
275 | // AA Stroking |
276 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
277 | |
278 | GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
279 | GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
280 | |
281 | static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside, |
282 | bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect, |
283 | SkScalar strokeWidth, bool miterStroke, SkVector* devHalfStrokeSize) { |
284 | SkRect devRect; |
285 | viewMatrix.mapRect(&devRect, rect); |
286 | |
287 | SkVector devStrokeSize; |
288 | if (strokeWidth > 0) { |
289 | devStrokeSize.set(strokeWidth, strokeWidth); |
290 | viewMatrix.mapVectors(&devStrokeSize, 1); |
291 | devStrokeSize.setAbs(devStrokeSize); |
292 | } else { |
293 | devStrokeSize.set(SK_Scalar1, SK_Scalar1); |
294 | } |
295 | |
296 | const SkScalar dx = devStrokeSize.fX; |
297 | const SkScalar dy = devStrokeSize.fY; |
298 | const SkScalar rx = SkScalarHalf(dx); |
299 | const SkScalar ry = SkScalarHalf(dy); |
300 | |
301 | devHalfStrokeSize->fX = rx; |
302 | devHalfStrokeSize->fY = ry; |
303 | |
304 | *devOutside = devRect; |
305 | *devOutsideAssist = devRect; |
306 | *devInside = devRect; |
307 | |
308 | devOutside->outset(rx, ry); |
309 | devInside->inset(rx, ry); |
310 | |
311 | // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we |
312 | // make a degenerate inside rect to avoid double hitting. We will also jam all of the points |
313 | // together when we render these rects. |
314 | SkScalar spare; |
315 | { |
316 | SkScalar w = devRect.width() - dx; |
317 | SkScalar h = devRect.height() - dy; |
318 | spare = std::min(w, h); |
319 | } |
320 | |
321 | *isDegenerate = spare <= 0; |
322 | if (*isDegenerate) { |
323 | devInside->fLeft = devInside->fRight = devRect.centerX(); |
324 | devInside->fTop = devInside->fBottom = devRect.centerY(); |
325 | } |
326 | |
327 | // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) |
328 | // to draw the outside of the octagon. Because there are 8 vertices on the outer |
329 | // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
330 | if (!miterStroke) { |
331 | devOutside->inset(0, ry); |
332 | devOutsideAssist->outset(0, ry); |
333 | } |
334 | } |
335 | |
336 | static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena, |
337 | bool tweakAlphaForCoverage, |
338 | const SkMatrix& viewMatrix, |
339 | bool usesLocalCoords, |
340 | bool wideColor) { |
341 | using namespace GrDefaultGeoProcFactory; |
342 | |
343 | Coverage::Type coverageType = |
344 | tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type; |
345 | LocalCoords::Type localCoordsType = |
346 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
347 | Color::Type colorType = |
348 | wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type; |
349 | |
350 | return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix); |
351 | } |
352 | |
353 | class AAStrokeRectOp final : public GrMeshDrawOp { |
354 | private: |
355 | using Helper = GrSimpleMeshDrawOpHelper; |
356 | |
357 | public: |
358 | DEFINE_OP_CLASS_ID |
359 | |
360 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
361 | GrPaint&& paint, |
362 | const SkMatrix& viewMatrix, |
363 | const SkRect& devOutside, |
364 | const SkRect& devInside, |
365 | const SkVector& devHalfStrokeSize) { |
366 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, |
367 | devOutside, devInside, devHalfStrokeSize); |
368 | } |
369 | |
370 | AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
371 | const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside, |
372 | const SkVector& devHalfStrokeSize) |
373 | : INHERITED(ClassID()) |
374 | , fHelper(helperArgs, GrAAType::kCoverage) |
375 | , fViewMatrix(viewMatrix) { |
376 | SkASSERT(!devOutside.isEmpty()); |
377 | SkASSERT(!devInside.isEmpty()); |
378 | |
379 | fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false}); |
380 | this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo); |
381 | fMiterStroke = true; |
382 | } |
383 | |
384 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
385 | GrPaint&& paint, |
386 | const SkMatrix& viewMatrix, |
387 | const SkRect& rect, |
388 | const SkStrokeRec& stroke) { |
389 | bool isMiter; |
390 | if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) { |
391 | return nullptr; |
392 | } |
393 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect, |
394 | stroke, isMiter); |
395 | } |
396 | |
397 | AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
398 | const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke, |
399 | bool isMiter) |
400 | : INHERITED(ClassID()) |
401 | , fHelper(helperArgs, GrAAType::kCoverage) |
402 | , fViewMatrix(viewMatrix) { |
403 | fMiterStroke = isMiter; |
404 | RectInfo& info = fRects.push_back(); |
405 | compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside, |
406 | &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter, |
407 | &info.fDevHalfStrokeSize); |
408 | info.fColor = color; |
409 | if (isMiter) { |
410 | this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo); |
411 | } else { |
412 | // The outer polygon of the bevel stroke is an octagon specified by the points of a |
413 | // pair of overlapping rectangles where one is wide and the other is narrow. |
414 | SkRect bounds = info.fDevOutside; |
415 | bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist); |
416 | this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo); |
417 | } |
418 | } |
419 | |
420 | const char* name() const override { return "AAStrokeRect" ; } |
421 | |
422 | void visitProxies(const VisitProxyFunc& func) const override { |
423 | if (fProgramInfo) { |
424 | fProgramInfo->visitFPProxies(func); |
425 | } else { |
426 | fHelper.visitProxies(func); |
427 | } |
428 | } |
429 | |
430 | #ifdef SK_DEBUG |
431 | SkString dumpInfo() const override { |
432 | SkString string; |
433 | for (const auto& info : fRects) { |
434 | string.appendf( |
435 | "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
436 | "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
437 | "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d" , |
438 | info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop, |
439 | info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft, |
440 | info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight, |
441 | info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop, |
442 | info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate); |
443 | } |
444 | string += fHelper.dumpInfo(); |
445 | string += INHERITED::dumpInfo(); |
446 | return string; |
447 | } |
448 | #endif |
449 | |
450 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
451 | |
452 | GrProcessorSet::Analysis finalize( |
453 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
454 | GrClampType clampType) override { |
455 | return fHelper.finalizeProcessors( |
456 | caps, clip, hasMixedSampledCoverage, clampType, |
457 | GrProcessorAnalysisCoverage::kSingleChannel, &fRects.back().fColor, &fWideColor); |
458 | } |
459 | |
460 | private: |
461 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
462 | |
463 | void onCreateProgramInfo(const GrCaps*, |
464 | SkArenaAlloc*, |
465 | const GrSurfaceProxyView* writeView, |
466 | GrAppliedClip&&, |
467 | const GrXferProcessor::DstProxyView&) override; |
468 | |
469 | void onPrepareDraws(Target*) override; |
470 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
471 | |
472 | static const int kMiterIndexCnt = 3 * 24; |
473 | static const int kMiterVertexCnt = 16; |
474 | static const int kNumMiterRectsInIndexBuffer = 256; |
475 | |
476 | static const int kBevelIndexCnt = 48 + 36 + 24; |
477 | static const int kBevelVertexCnt = 24; |
478 | static const int kNumBevelRectsInIndexBuffer = 256; |
479 | |
480 | static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke); |
481 | |
482 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
483 | bool miterStroke() const { return fMiterStroke; } |
484 | |
485 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, const GrCaps&) override; |
486 | |
487 | void generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
488 | const SkPMColor4f& color, |
489 | bool wideColor, |
490 | const SkRect& devOutside, |
491 | const SkRect& devOutsideAssist, |
492 | const SkRect& devInside, |
493 | bool miterStroke, |
494 | bool degenerate, |
495 | bool tweakAlphaForCoverage, |
496 | const SkVector& devHalfStrokeSize) const; |
497 | |
498 | // TODO support AA rotated stroke rects by copying around view matrices |
499 | struct RectInfo { |
500 | SkPMColor4f fColor; |
501 | SkRect fDevOutside; |
502 | SkRect fDevOutsideAssist; |
503 | SkRect fDevInside; |
504 | SkVector fDevHalfStrokeSize; |
505 | bool fDegenerate; |
506 | }; |
507 | |
508 | Helper fHelper; |
509 | SkSTArray<1, RectInfo, true> fRects; |
510 | SkMatrix fViewMatrix; |
511 | GrSimpleMesh* fMesh = nullptr; |
512 | GrProgramInfo* fProgramInfo = nullptr; |
513 | bool fMiterStroke; |
514 | bool fWideColor; |
515 | |
516 | typedef GrMeshDrawOp INHERITED; |
517 | }; |
518 | |
519 | void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps, |
520 | SkArenaAlloc* arena, |
521 | const GrSurfaceProxyView* writeView, |
522 | GrAppliedClip&& appliedClip, |
523 | const GrXferProcessor::DstProxyView& dstProxyView) { |
524 | |
525 | GrGeometryProcessor* gp = create_aa_stroke_rect_gp(arena, |
526 | fHelper.compatibleWithCoverageAsAlpha(), |
527 | this->viewMatrix(), |
528 | fHelper.usesLocalCoords(), |
529 | fWideColor); |
530 | if (!gp) { |
531 | SkDebugf("Couldn't create GrGeometryProcessor\n" ); |
532 | return; |
533 | } |
534 | |
535 | fProgramInfo = fHelper.createProgramInfo(caps, |
536 | arena, |
537 | writeView, |
538 | std::move(appliedClip), |
539 | dstProxyView, |
540 | gp, |
541 | GrPrimitiveType::kTriangles); |
542 | } |
543 | |
544 | void AAStrokeRectOp::onPrepareDraws(Target* target) { |
545 | |
546 | if (!fProgramInfo) { |
547 | this->createProgramInfo(target); |
548 | if (!fProgramInfo) { |
549 | return; |
550 | } |
551 | } |
552 | |
553 | int innerVertexNum = 4; |
554 | int outerVertexNum = this->miterStroke() ? 4 : 8; |
555 | int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; |
556 | int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; |
557 | int instanceCount = fRects.count(); |
558 | int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer; |
559 | |
560 | sk_sp<const GrGpuBuffer> indexBuffer = |
561 | GetIndexBuffer(target->resourceProvider(), this->miterStroke()); |
562 | if (!indexBuffer) { |
563 | SkDebugf("Could not allocate indices\n" ); |
564 | return; |
565 | } |
566 | PatternHelper helper(target, GrPrimitiveType::kTriangles, |
567 | fProgramInfo->primProc().vertexStride(), std::move(indexBuffer), |
568 | verticesPerInstance, indicesPerInstance, instanceCount, maxQuads); |
569 | GrVertexWriter vertices{ helper.vertices() }; |
570 | if (!vertices.fPtr) { |
571 | SkDebugf("Could not allocate vertices\n" ); |
572 | return; |
573 | } |
574 | |
575 | for (int i = 0; i < instanceCount; i++) { |
576 | const RectInfo& info = fRects[i]; |
577 | this->generateAAStrokeRectGeometry(vertices, |
578 | info.fColor, |
579 | fWideColor, |
580 | info.fDevOutside, |
581 | info.fDevOutsideAssist, |
582 | info.fDevInside, |
583 | fMiterStroke, |
584 | info.fDegenerate, |
585 | fHelper.compatibleWithCoverageAsAlpha(), |
586 | info.fDevHalfStrokeSize); |
587 | } |
588 | fMesh = helper.mesh(); |
589 | } |
590 | |
591 | void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
592 | if (!fProgramInfo || !fMesh) { |
593 | return; |
594 | } |
595 | |
596 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
597 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
598 | flushState->drawMesh(*fMesh); |
599 | } |
600 | |
601 | sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider, |
602 | bool miterStroke) { |
603 | if (miterStroke) { |
604 | // clang-format off |
605 | static const uint16_t gMiterIndices[] = { |
606 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
607 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
608 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
609 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
610 | |
611 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
612 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
613 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
614 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
615 | |
616 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
617 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
618 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
619 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
620 | }; |
621 | // clang-format on |
622 | static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); |
623 | GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
624 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
625 | gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, |
626 | gMiterIndexBufferKey); |
627 | } else { |
628 | /** |
629 | * As in miter-stroke, index = a + b, and a is the current index, b is the shift |
630 | * from the first index. The index layout: |
631 | * outer AA line: 0~3, 4~7 |
632 | * outer edge: 8~11, 12~15 |
633 | * inner edge: 16~19 |
634 | * inner AA line: 20~23 |
635 | * Following comes a bevel-stroke rect and its indices: |
636 | * |
637 | * 4 7 |
638 | * ********************************* |
639 | * * ______________________________ * |
640 | * * / 12 15 \ * |
641 | * * / \ * |
642 | * 0 * |8 16_____________________19 11 | * 3 |
643 | * * | | | | * |
644 | * * | | **************** | | * |
645 | * * | | * 20 23 * | | * |
646 | * * | | * * | | * |
647 | * * | | * 21 22 * | | * |
648 | * * | | **************** | | * |
649 | * * | |____________________| | * |
650 | * 1 * |9 17 18 10| * 2 |
651 | * * \ / * |
652 | * * \13 __________________________14/ * |
653 | * * * |
654 | * ********************************** |
655 | * 5 6 |
656 | */ |
657 | // clang-format off |
658 | static const uint16_t gBevelIndices[] = { |
659 | // Draw outer AA, from outer AA line to outer edge, shift is 0. |
660 | 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, |
661 | 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, |
662 | 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, |
663 | 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, |
664 | 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, |
665 | 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, |
666 | 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, |
667 | 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, |
668 | |
669 | // Draw the stroke, from outer edge to inner edge, shift is 8. |
670 | 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, |
671 | 1 + 8, 5 + 8, 9 + 8, |
672 | 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, |
673 | 6 + 8, 2 + 8, 10 + 8, |
674 | 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, |
675 | 3 + 8, 7 + 8, 11 + 8, |
676 | 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, |
677 | 4 + 8, 0 + 8, 8 + 8, |
678 | |
679 | // Draw the inner AA, from inner edge to inner AA line, shift is 16. |
680 | 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, |
681 | 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, |
682 | 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, |
683 | 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, |
684 | }; |
685 | // clang-format on |
686 | static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); |
687 | |
688 | GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
689 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
690 | gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, |
691 | gBevelIndexBufferKey); |
692 | } |
693 | } |
694 | |
695 | GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
696 | const GrCaps& caps) { |
697 | AAStrokeRectOp* that = t->cast<AAStrokeRectOp>(); |
698 | |
699 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
700 | return CombineResult::kCannotCombine; |
701 | } |
702 | |
703 | // TODO combine across miterstroke changes |
704 | if (this->miterStroke() != that->miterStroke()) { |
705 | return CombineResult::kCannotCombine; |
706 | } |
707 | |
708 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
709 | // local coords then we won't be able to combine. TODO: Upload local coords as an attribute. |
710 | if (fHelper.usesLocalCoords() && |
711 | !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) |
712 | { |
713 | return CombineResult::kCannotCombine; |
714 | } |
715 | |
716 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
717 | fWideColor |= that->fWideColor; |
718 | return CombineResult::kMerged; |
719 | } |
720 | |
721 | // Compute the coverage for the inner two rects. |
722 | static float compute_inner_coverage(SkScalar maxDevHalfStrokeSize) { |
723 | if (maxDevHalfStrokeSize < SK_ScalarHalf) { |
724 | return 2.0f * maxDevHalfStrokeSize / (maxDevHalfStrokeSize + SK_ScalarHalf); |
725 | } |
726 | |
727 | return 1.0f; |
728 | } |
729 | |
730 | void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
731 | const SkPMColor4f& color, |
732 | bool wideColor, |
733 | const SkRect& devOutside, |
734 | const SkRect& devOutsideAssist, |
735 | const SkRect& devInside, |
736 | bool miterStroke, |
737 | bool degenerate, |
738 | bool tweakAlphaForCoverage, |
739 | const SkVector& devHalfStrokeSize) const { |
740 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
741 | // coverage, one on the exterior of the stroke and the other on the interior. |
742 | |
743 | // The following code only really works if either devStrokeSize's fX and fY are |
744 | // equal (in which case innerCoverage is same for all sides of the rects) or |
745 | // if devStrokeSize's fX and fY are both greater than 1.0 (in which case |
746 | // innerCoverage will always be 1). The most problematic case is when one of |
747 | // fX and fY is greater than 1.0 and the other is less than 1.0. In this case |
748 | // the smaller side should have a partial coverage but the larger side will |
749 | // force the coverage to be 1.0. |
750 | |
751 | auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) { |
752 | return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy)); |
753 | }; |
754 | |
755 | auto maybe_coverage = [tweakAlphaForCoverage](float coverage) { |
756 | return GrVertexWriter::If(!tweakAlphaForCoverage, coverage); |
757 | }; |
758 | |
759 | GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor); |
760 | |
761 | // For device-space stroke widths less than one we can't inset more than the original |
762 | // device space stroke width if we want to keep the sizing of all the rects correct. |
763 | const SkScalar insetX = std::min(SK_ScalarHalf, devHalfStrokeSize.fX); |
764 | const SkScalar insetY = std::min(SK_ScalarHalf, devHalfStrokeSize.fY); |
765 | |
766 | // But, correspondingly, we always want to keep the AA picture frame one pixel wide. |
767 | const SkScalar outsetX = SK_Scalar1 - insetX; |
768 | const SkScalar outsetY = SK_Scalar1 - insetY; |
769 | |
770 | // Outermost rect |
771 | vertices.writeQuad(inset_fan(devOutside, -outsetX, -outsetY), |
772 | outerColor, |
773 | maybe_coverage(0.0f)); |
774 | |
775 | if (!miterStroke) { |
776 | // Second outermost |
777 | vertices.writeQuad(inset_fan(devOutsideAssist, -outsetX, -outsetY), |
778 | outerColor, |
779 | maybe_coverage(0.0f)); |
780 | } |
781 | |
782 | float innerCoverage = compute_inner_coverage(std::max(devHalfStrokeSize.fX, |
783 | devHalfStrokeSize.fY)); |
784 | |
785 | SkPMColor4f scaledColor = color * innerCoverage; |
786 | GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor); |
787 | |
788 | // Inner rect |
789 | vertices.writeQuad(inset_fan(devOutside, insetX, insetY), |
790 | innerColor, |
791 | maybe_coverage(innerCoverage)); |
792 | |
793 | if (!miterStroke) { |
794 | // Second inner |
795 | vertices.writeQuad(inset_fan(devOutsideAssist, insetX, insetY), |
796 | innerColor, |
797 | maybe_coverage(innerCoverage)); |
798 | } |
799 | |
800 | if (!degenerate) { |
801 | vertices.writeQuad(inset_fan(devInside, -insetX, -insetY), |
802 | innerColor, |
803 | maybe_coverage(innerCoverage)); |
804 | |
805 | // The innermost rect has 0 coverage... |
806 | vertices.writeQuad(inset_fan(devInside, outsetX, outsetY), |
807 | outerColor, |
808 | maybe_coverage(0.0f)); |
809 | } else { |
810 | // When the interior rect has become degenerate we smoosh to a single point |
811 | SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom); |
812 | |
813 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
814 | innerColor, |
815 | maybe_coverage(innerCoverage)); |
816 | |
817 | // ... unless we are degenerate, in which case we must apply the scaled coverage |
818 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
819 | innerColor, |
820 | maybe_coverage(innerCoverage)); |
821 | } |
822 | } |
823 | |
824 | } // anonymous namespace |
825 | |
826 | namespace GrStrokeRectOp { |
827 | |
828 | std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
829 | GrPaint&& paint, |
830 | GrAAType aaType, |
831 | const SkMatrix& viewMatrix, |
832 | const SkRect& rect, |
833 | const SkStrokeRec& stroke) { |
834 | if (aaType == GrAAType::kCoverage) { |
835 | // The AA op only supports axis-aligned rectangles |
836 | if (!viewMatrix.rectStaysRect()) { |
837 | return nullptr; |
838 | } |
839 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke); |
840 | } else { |
841 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType); |
842 | } |
843 | } |
844 | |
845 | std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context, |
846 | GrPaint&& paint, |
847 | const SkMatrix& viewMatrix, |
848 | const SkRect rects[2]) { |
849 | SkASSERT(viewMatrix.rectStaysRect()); |
850 | SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty()); |
851 | |
852 | SkRect devOutside, devInside; |
853 | viewMatrix.mapRect(&devOutside, rects[0]); |
854 | viewMatrix.mapRect(&devInside, rects[1]); |
855 | if (devInside.isEmpty()) { |
856 | if (devOutside.isEmpty()) { |
857 | return nullptr; |
858 | } |
859 | DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]), |
860 | GrQuadAAFlags::kAll}; |
861 | return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad); |
862 | } |
863 | |
864 | SkVector devHalfStrokeSize{ SkScalarHalf(devOutside.fRight - devInside.fRight), |
865 | SkScalarHalf(devOutside.fBottom - devInside.fBottom) }; |
866 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, |
867 | devInside, devHalfStrokeSize); |
868 | } |
869 | |
870 | } // namespace GrStrokeRectOp |
871 | |
872 | #if GR_TEST_UTILS |
873 | |
874 | #include "src/gpu/GrDrawOpTest.h" |
875 | |
876 | GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) { |
877 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
878 | SkRect rect = GrTest::TestRect(random); |
879 | SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f; |
880 | SkPaint strokePaint; |
881 | strokePaint.setStrokeWidth(strokeWidth); |
882 | strokePaint.setStyle(SkPaint::kStroke_Style); |
883 | strokePaint.setStrokeJoin(SkPaint::kMiter_Join); |
884 | SkStrokeRec strokeRec(strokePaint); |
885 | GrAAType aaType = GrAAType::kNone; |
886 | if (numSamples > 1) { |
887 | aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone; |
888 | } |
889 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType); |
890 | } |
891 | |
892 | GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) { |
893 | bool miterStroke = random->nextBool(); |
894 | |
895 | // Create either a empty rect or a non-empty rect. |
896 | SkRect rect = |
897 | random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0); |
898 | SkScalar minDim = std::min(rect.width(), rect.height()); |
899 | SkScalar strokeWidth = random->nextUScalar1() * minDim; |
900 | |
901 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
902 | rec.setStrokeStyle(strokeWidth); |
903 | rec.setStrokeParams(SkPaint::kButt_Cap, |
904 | miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f); |
905 | SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random); |
906 | return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec); |
907 | } |
908 | |
909 | #endif |
910 | |