xref: /aosp_15_r20/external/skia/gm/beziers.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 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/SkPaint.h"
11 #include "include/core/SkPathBuilder.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkString.h"
15 #include "src/base/SkRandom.h"
16 
17 #define W   400
18 #define H   400
19 #define N   10
20 
21 constexpr SkScalar SH = SkIntToScalar(H);
22 
rnd_quad(SkPaint * paint,SkRandom & rand)23 static SkPath rnd_quad(SkPaint* paint, SkRandom& rand) {
24     auto a = rand.nextRangeScalar(0,W),
25          b = rand.nextRangeScalar(0,H);
26 
27     SkPathBuilder builder;
28     builder.moveTo(a, b);
29     for (int x = 0; x < 2; ++x) {
30         auto c = rand.nextRangeScalar(W/4, W),
31              d = rand.nextRangeScalar(  0, H),
32              e = rand.nextRangeScalar(  0, W),
33              f = rand.nextRangeScalar(H/4, H);
34         builder.quadTo(c,d,e,f);
35     }
36     paint->setColor(rand.nextU());
37     SkScalar width = rand.nextRangeScalar(1, 5);
38     width *= width;
39     paint->setStrokeWidth(width);
40     paint->setAlphaf(1.0f);
41     return builder.detach();
42 }
43 
rnd_cubic(SkPaint * paint,SkRandom & rand)44 static SkPath rnd_cubic(SkPaint* paint, SkRandom& rand) {
45     auto a = rand.nextRangeScalar(0,W),
46          b = rand.nextRangeScalar(0,H);
47 
48     SkPathBuilder builder;
49     builder.moveTo(a, b);
50     for (int x = 0; x < 2; ++x) {
51         auto c = rand.nextRangeScalar(W/4, W),
52              d = rand.nextRangeScalar(  0, H),
53              e = rand.nextRangeScalar(  0, W),
54              f = rand.nextRangeScalar(H/4, H),
55              g = rand.nextRangeScalar(W/4, W),
56              h = rand.nextRangeScalar(H/4, H);
57         builder.cubicTo(c,d,e,f,g,h);
58     }
59     paint->setColor(rand.nextU());
60     SkScalar width = rand.nextRangeScalar(1, 5);
61     width *= width;
62     paint->setStrokeWidth(width);
63     paint->setAlphaf(1.0f);
64     return builder.detach();
65 }
66 
67 class BeziersGM : public skiagm::GM {
68 public:
BeziersGM()69     BeziersGM() {}
70 
71 protected:
getName() const72     SkString getName() const override { return SkString("beziers"); }
73 
getISize()74     SkISize getISize() override { return SkISize::Make(W, H * 2); }
75 
onDraw(SkCanvas * canvas)76     void onDraw(SkCanvas* canvas) override {
77         SkPaint paint;
78         paint.setStyle(SkPaint::kStroke_Style);
79         paint.setStrokeWidth(SkIntToScalar(9)/2);
80         paint.setAntiAlias(true);
81 
82         SkRandom rand;
83         for (int i = 0; i < N; i++) {
84             canvas->drawPath(rnd_quad(&paint, rand), paint);
85         }
86         canvas->translate(0, SH);
87         for (int i = 0; i < N; i++) {
88             canvas->drawPath(rnd_cubic(&paint, rand), paint);
89         }
90     }
91 
92 private:
93     using INHERITED = skiagm::GM;
94 };
95 
96 DEF_GM( return new BeziersGM; )
97