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