Debian Codegraph @

gm/rrects.cpp

raw
1 /*
2 * Copyright 2012 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 "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypes.h"
20 #include "include/effects/SkGradient.h"
21 #include "include/private/gpu/ganesh/GrTypesPriv.h"
22 #include "src/core/SkCanvasPriv.h"
23 #include "src/gpu/ganesh/GrCanvas.h"
24 #include "src/gpu/ganesh/GrCaps.h"
25 #include "src/gpu/ganesh/GrFragmentProcessor.h"
26 #include "src/gpu/ganesh/GrPaint.h"
27 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
28 #include "src/gpu/ganesh/SurfaceDrawContext.h"
29 #include "src/gpu/ganesh/effects/GrPorterDuffXferProcessor.h"
30 #include "src/gpu/ganesh/effects/GrRRectEffect.h"
31 #include "src/gpu/ganesh/ops/FillRectOp.h"
32 #include "src/gpu/ganesh/ops/GrDrawOp.h"
33
34 #include <memory>
35 #include <utility>
36
37 namespace skiagm {
38
39 ///////////////////////////////////////////////////////////////////////////////
40
41 class RRectGM : public GM {
42 public:
43 enum Type {
44 kBW_Draw_Type,
45 kAA_Draw_Type,
46 kBW_Clip_Type,
47 kAA_Clip_Type,
48 kEffect_Type,
49 };
50 RRectGM(Type type) : fType(type) { }
51
52 protected:
53
54 void onOnceBeforeDraw() override {
55 this->setBGColor(0xFFDDDDDD);
56 this->setUpRRects();
57 }
58
59 SkString getName() const override {
60 SkString name("rrect");
61 switch (fType) {
62 case kBW_Draw_Type:
63 name.append("_draw_bw");
64 break;
65 case kAA_Draw_Type:
66 name.append("_draw_aa");
67 break;
68 case kBW_Clip_Type:
69 name.append("_clip_bw");
70 break;
71 case kAA_Clip_Type:
72 name.append("_clip_aa");
73 break;
74 case kEffect_Type:
75 name.append("_effect");
76 break;
77 }
78 return name;
79 }
80
81 SkISize getISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
82
83 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
84 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
85
86 auto rContext = canvas->recordingContext();
87 if (kEffect_Type == fType && (!sdc || !rContext)) {
88 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
89 return DrawResult::kSkip;
90 }
91
92 SkPaint paint;
93 if (kAA_Draw_Type == fType) {
94 paint.setAntiAlias(true);
95 }
96
97 if (fType == kBW_Clip_Type || fType == kAA_Clip_Type) {
98 // Add a gradient to the paint to ensure local coords are respected.
99 SkPoint pts[] = {{0, 0}, {1.5f, 1}};
100 SkColor4f colors[] = {SkColors::kBlack, SkColors::kYellow};
101 paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}}));
102 }
103
104 #ifdef SK_DEBUG
105 const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
106 SkIntToScalar(kImageHeight));
107 #endif
108
109 int lastEdgeType = (kEffect_Type == fType) ? (int) GrClipEdgeType::kLast: 0;
110
111 int y = 1;
112 for (int et = 0; et <= lastEdgeType; ++et) {
113 int x = 1;
114 for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) {
115 bool drew = true;
116 #ifdef SK_DEBUG
117 if (curRRect != kNumRRects - 1) { // skip last rrect, which is large but clipped
118 SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
119 imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
120 SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
121 }
122 #endif
123 canvas->save();
124 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
125
126 SkRRect rrect = fRRects[curRRect];
127 if (curRRect == kNumRRects - 1) {
128 canvas->clipRect({0, 0, kTileX - 2, kTileY - 2});
129 canvas->translate(-0.14f * rrect.rect().width(),
130 -0.14f * rrect.rect().height());
131 }
132 if (kEffect_Type == fType) {
133 if (auto rr = fRRects[curRRect].transform(canvas->getLocalToDeviceAs3x3())){
134 rrect = *rr;
135 }
136
137 GrClipEdgeType edgeType = (GrClipEdgeType) et;
138 const auto& caps = *rContext->priv().caps()->shaderCaps();
139 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr,
140 edgeType, rrect, caps);
141 if (success) {
142 GrPaint grPaint;
143 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
144 grPaint.setCoverageFragmentProcessor(std::move(fp));
145 grPaint.setColor4f({ 0, 0, 0, 1.f });
146
147 SkRect bounds = rrect.getBounds();
148 bounds.intersect(SkRect::MakeXYWH(x, y, kTileX - 2, kTileY - 2));
149 if (et >= (int) GrClipEdgeType::kInverseFillBW) {
150 bounds.outset(2.f, 2.f);
151 }
152
153 sdc->addDrawOp(skgpu::ganesh::FillRectOp::MakeNonAARect(
154 rContext, std::move(grPaint), SkMatrix::I(), bounds));
155 } else {
156 drew = false;
157 }
158 } else if (fType == kBW_Clip_Type || fType == kAA_Clip_Type) {
159 bool aaClip = (kAA_Clip_Type == fType);
160 canvas->clipRRect(rrect, aaClip);
161 canvas->setMatrix(SkMatrix::Scale(kImageWidth, kImageHeight));
162 canvas->drawRect(SkRect::MakeWH(1, 1), paint);
163 } else {
164 canvas->drawRRect(rrect, paint);
165 }
166
167 canvas->restore();
168 if (drew) {
169 x = x + kTileX;
170 if (x > kImageWidth) {
171 x = 1;
172 y += kTileY;
173 }
174 }
175 }
176 if (x != 1) {
177 y += kTileY;
178 }
179 }
180 return DrawResult::kOk;
181 }
182
183 void setUpRRects() {
184 // each RRect must fit in a 0x0 -> (kTileX-2)x(kTileY-2) block. These will be tiled across
185 // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA.
186
187 // simple cases
188 fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2));
189 fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2));
190 fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10);
191 fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5);
192 // small circular corners are an interesting test case for gpu clipping
193 fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1);
194 fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f);
195 fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f);
196
197 // The first complex case needs special handling since it is a square
198 fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]);
199 for (size_t i = 1; i < std::size(gRadii); ++i) {
200 fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]);
201 }
202 // The last case is larger than kTileX-2 x kTileY-2 but will be drawn at an offset
203 // into a clip rect that respects the tile size and highlights the rrect's corner curve.
204 fRRects[kNumRRects - 1].setRectXY({9.f, 9.f, 1699.f, 1699.f}, 843.749f, 843.75f);
205 }
206
207 private:
208 Type fType;
209
210 inline static constexpr int kImageWidth = 640;
211 inline static constexpr int kImageHeight = 480;
212
213 inline static constexpr int kTileX = 80;
214 inline static constexpr int kTileY = 40;
215
216 inline static constexpr int kNumSimpleCases = 7;
217 inline static constexpr int kNumComplexCases = 35;
218
219 static const SkVector gRadii[kNumComplexCases][4];
220
221 inline static constexpr int kNumRRects = kNumSimpleCases + kNumComplexCases + 1 /* extra big */;
222 SkRRect fRRects[kNumRRects];
223
224 using INHERITED = GM;
225 };
226
227 // Radii for the various test cases. Order is UL, UR, LR, LL
228 const SkVector RRectGM::gRadii[kNumComplexCases][4] = {
229 // a circle
230 { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } },
231
232 // odd ball cases
233 { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } },
234 { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } },
235 { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } },
236
237 // UL
238 { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
239 { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
240 { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
241
242 // UR
243 { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } },
244 { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } },
245 { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } },
246
247 // LR
248 { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } },
249 { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } },
250 { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } },
251
252 // LL
253 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } },
254 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } },
255 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } },
256
257 // over-sized radii
258 { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } },
259 { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } },
260 { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } },
261
262 // circular corner tabs
263 { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } },
264 { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } },
265 { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } },
266 { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } },
267
268 // small radius circular corner tabs
269 { { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } },
270 { { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } },
271
272 // single circular corner cases
273 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } },
274 { { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } },
275 { { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } },
276 { { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
277
278 // nine patch elliptical
279 { { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } },
280 { { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } },
281
282 // nine patch elliptical, small radii
283 { { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } },
284 { { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } },
285 { { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } },
286 { { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } },
287
288 };
289
290 ///////////////////////////////////////////////////////////////////////////////
291
292 DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); )
293 DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); )
294 DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); )
295 DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); )
296 DEF_GM( return new RRectGM(RRectGM::kEffect_Type); )
297
298 // This GM is designed to test a variety of fill and stroked rectangles and round rectangles, with
299 // different stroke width and join type scenarios. The geometry parameters are chosen so that
300 // Graphite should be able to use its AnalyticRoundRectRenderStep and batch into a single draw.
301 DEF_SIMPLE_GM(stroke_rect_rrects, canvas, 1350, 700) {
302 canvas->scale(0.5f, 0.5f);
303 canvas->translate(50.f, 50.f);
304
305 auto draw = [&](int cx, int cy, bool rrect, float width, SkPaint::Join join) {
306 SkPaint p;
307 p.setAntiAlias(true);
308 p.setStrokeWidth(width);
309 p.setStyle(width >= 0.f ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
310 p.setStrokeJoin(join);
311
312 canvas->save();
313 canvas->translate(cx * 110.f, cy * 110.f);
314 float dx = cx % 2 ? 0.5f : 0.f;
315 float dy = cy % 2 ? 0.5f : 0.f;
316 SkRect rect = SkRect::MakeWH(50.f, 40.f);
317 rect.offset(dx, dy);
318
319 if (width < 0.0) {
320 rect.outset(25.f, 25.f); // make it the same size as the largest stroke
321 }
322
323 // Filled rounded rects can have arbitrary corners
324 float cornerScale = std::min(rect.width(), rect.height());
325 SkVector outerRadii[4] = { { 0.25f * cornerScale, 0.75f * cornerScale },
326 { 0.f, 0.f},
327 { 0.50f * cornerScale, 0.50f * cornerScale },
328 { 0.75f * cornerScale, 0.25f * cornerScale } };
329 // Stroked rounded rects will only have circular corners so that they remain compatible with
330 // Graphite's AnalyticRoundRectRenderStep's requirements.
331 SkVector strokeRadii[4] = { { 0.25f * cornerScale, 0.25f * cornerScale },
332 { 0.f, 0.f }, // this corner matches join type
333 { 0.50f * cornerScale, 0.50f * cornerScale },
334 { 0.75f * cornerScale, 0.75f * cornerScale } };
335
336 if (rrect) {
337 SkRRect r;
338 if (width >= 0.0) {
339 r.setRectRadii(rect, strokeRadii);
340 } else {
341 r.setRectRadii(rect, outerRadii);
342 }
343 canvas->drawRRect(r, p);
344 } else {
345 canvas->drawRect(rect, p);
346 }
347 canvas->restore();
348 };
349
350 // The stroke widths are chosen to test when the inner stroke edges have completely crossed
351 // over (50); when the inner corner circles intersect each other (30); a typical "nice"
352 // stroke (10); a skinny stroke (1); and a hairline (0).
353 int i = 0;
354 for (float width : {-1.f, 50.f, 30.f, 10.f, 1.f, 0.f}) {
355 int j = 0;
356 for (SkPaint::Join join : { SkPaint::kMiter_Join,
357 SkPaint::kBevel_Join,
358 SkPaint::kRound_Join }) {
359 if (width < 0 && join != SkPaint::kMiter_Join) {
360 continue; // Don't repeat fills, since join type is ignored
361 }
362 draw(2*i, 2*j, false, width, join);
363 draw(2*i+1, 2*j, false, width, join);
364 draw(2*i, 2*j+1, false, width, join);
365 draw(2*i+1, 2*j+1, false, width, join);
366 j++;
367 }
368 i++;
369 }
370
371 canvas->translate(0.f, 50.f);
372
373 i = 0;
374 for (float width : {-1.f, 50.f, 30.f, 10.f, 1.f, 0.f}) {
375 int j = 3;
376 for (SkPaint::Join join : { SkPaint::kMiter_Join,
377 SkPaint::kBevel_Join,
378 SkPaint::kRound_Join }) {
379 if (width < 0 && join != SkPaint::kMiter_Join) {
380 continue;
381 }
382 draw(2*i, 2*j, true, width, join);
383 draw(2*i+1, 2*j, true, width, join);
384 draw(2*i, 2*j+1, true, width, join);
385 draw(2*i+1, 2*j+1, true, width, join);
386 j++;
387 }
388 i++;
389 }
390
391 // Rotated "footballs"
392 auto drawComplex = [&](int cx, int cy, float width, float stretch) {
393 SkPaint p;
394 p.setAntiAlias(true);
395 p.setStrokeWidth(width);
396 p.setStyle(SkPaint::kStroke_Style);
397 p.setStrokeJoin(SkPaint::kBevel_Join);
398
399 canvas->save();
400 canvas->translate(cx * 110.f, cy * 110.f);
401
402 SkRect rect = SkRect::MakeWH(cx % 2 ? 50.f : (40.f + stretch),
403 cx % 2 ? (40.f + stretch) : 50.f);
404 const SkVector kBigCorner{30.f, 30.f};
405 const SkVector kRectCorner{0.f, 0.f};
406
407 SkVector strokeRadii[4] = { cy % 2 ? kRectCorner : kBigCorner,
408 cy % 2 ? kBigCorner : kRectCorner,
409 cy % 2 ? kRectCorner : kBigCorner,
410 cy % 2 ? kBigCorner : kRectCorner };
411
412 SkRRect r;
413 r.setRectRadii(rect, strokeRadii);
414 canvas->drawRRect(r, p);
415
416 canvas->restore();
417 };
418
419 canvas->translate(0.f, -50.f);
420 i = 6;
421 for (float width : {50.f, 30.f, 20.f, 10.f, 1.f, 0.f}) {
422 int j = 0;
423 for (float stretch: {0.f, 5.f, 10.f}) {
424 drawComplex(2*i, 2*j, width, stretch);
425 drawComplex(2*i+1, 2*j, width, stretch);
426 drawComplex(2*i, 2*j+1, width, stretch);
427 drawComplex(2*i+1, 2*j+1, width, stretch);
428 j++;
429 }
430 i++;
431 }
432
433 // Rotated "D"s
434 auto drawComplex2 = [&](int cx, int cy, float width, float stretch) {
435 SkPaint p;
436 p.setAntiAlias(true);
437 p.setStrokeWidth(width);
438 p.setStyle(SkPaint::kStroke_Style);
439 p.setStrokeJoin(SkPaint::kMiter_Join);
440
441 canvas->save();
442 canvas->translate(cx * 110.f, cy * 110.f);
443
444 SkRect rect = SkRect::MakeWH(cx % 2 ? 50.f : (40.f + stretch),
445 cx % 2 ? (40.f + stretch) : 50.f);
446 const SkVector kBigCorner{30.f, 30.f};
447 const SkVector kRectCorner{0.f, 0.f};
448
449 SkVector strokeRadii[4] = { cx % 2 ? kRectCorner : kBigCorner,
450 (cx % 2) ^ (cy % 2) ? kBigCorner : kRectCorner,
451 cx % 2 ? kBigCorner : kRectCorner,
452 (cx % 2) ^ (cy % 2) ? kRectCorner : kBigCorner };
453
454 SkRRect r;
455 r.setRectRadii(rect, strokeRadii);
456 canvas->drawRRect(r, p);
457
458 canvas->restore();
459 };
460
461 canvas->translate(0.f, 50.f);
462 i = 6;
463 for (float width : {50.f, 30.f, 20.f, 10.f, 1.f, 0.f}) {
464 int j = 3;
465 for (float stretch: {0.f, 5.f, 10.f}) {
466 drawComplex2(2*i, 2*j, width, stretch);
467 drawComplex2(2*i+1, 2*j, width, stretch);
468 drawComplex2(2*i, 2*j+1, width, stretch);
469 drawComplex2(2*i+1, 2*j+1, width, stretch);
470 j++;
471 }
472 i++;
473 }
474 }
475
476 } // namespace skiagm