xref: /aosp_15_r20/external/skia/gm/largeclippedpath.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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/SkCanvas.h"
10 #include "include/core/SkPath.h"
11 
12 constexpr int kSize = 1000;
13 
14 // Makes sure PathInnerTriangulateOp uses correct stencil settings when there is a clip in the
15 // stencil buffer.
draw_clipped_flower(SkCanvas * canvas,SkPathFillType fillType)16 static void draw_clipped_flower(SkCanvas* canvas, SkPathFillType fillType) {
17     canvas->clear(SK_ColorCYAN);
18     SkPath clip;
19     clip.setFillType(SkPathFillType::kWinding);
20     constexpr static int kGridCount = 50;
21     constexpr static float kCellSize = (float)kSize / kGridCount;
22     for (int y = 0; y < kGridCount; ++y) {
23         clip.addRect(0, y * kCellSize, kSize, (y + 1) * kCellSize, SkPathDirection(y & 1));
24     }
25     for (int x = 0; x < kGridCount; ++x) {
26         clip.addRect(x * kCellSize, 0, (x + 1) * kCellSize, kSize, SkPathDirection(x & 1));
27     }
28     canvas->clipPath(clip);
29     SkPath flower;
30     flower.setFillType(fillType);
31     flower.moveTo(1, 0);
32     constexpr static int kNumPetals = 9;
33     for (int i = 1; i <= kNumPetals; ++i) {
34         float c = 2*SK_ScalarPI*(i - .5f) / kNumPetals;
35         float theta = 2*SK_ScalarPI*i / kNumPetals;
36         flower.quadTo(cosf(c)*2, sinf(c)*2, cosf(theta), sinf(theta));
37     }
38     flower.close();
39     flower.addArc(SkRect::MakeLTRB(-.75f, -.75f, .75f, .75f), 0, 360);
40     canvas->translate(kSize/2.f, kSize/2.f);
41     canvas->scale(kSize/3.f, kSize/3.f);
42     SkPaint p;
43     p.setAntiAlias(true);
44     p.setColor(SK_ColorMAGENTA);
45     canvas->drawPath(flower, p);
46 }
47 
DEF_SIMPLE_GM(largeclippedpath_winding,canvas,kSize,kSize)48 DEF_SIMPLE_GM(largeclippedpath_winding, canvas, kSize, kSize) {
49     draw_clipped_flower(canvas, SkPathFillType::kWinding);
50 }
51 
DEF_SIMPLE_GM(largeclippedpath_evenodd,canvas,kSize,kSize)52 DEF_SIMPLE_GM(largeclippedpath_evenodd, canvas, kSize, kSize) {
53     draw_clipped_flower(canvas, SkPathFillType::kEvenOdd);
54 }
55