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