blob: 20abb0307c4d34cb223250054eb62b5c3e5cb691 [file] [log] [blame]
caryclarkfeff7d22014-10-09 05:36:03 -07001/*
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.h"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkPath.h"
caryclarkfeff7d22014-10-09 05:36:03 -070010#include "SkRandom.h"
11
12#define W 400
13#define H 400
14#define N 10
15
caryclarkfeff7d22014-10-09 05:36:03 -070016static const SkScalar SH = SkIntToScalar(H);
17
scroggof9d61012014-12-15 12:54:51 -080018static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) {
caryclarkfeff7d22014-10-09 05:36:03 -070019 p->moveTo(rand.nextRangeScalar(0, W), rand.nextRangeScalar(0, H));
20 for (int x = 0; x < 2; ++x) {
21 p->quadTo(rand.nextRangeScalar(W / 4, W), rand.nextRangeScalar(0, H),
22 rand.nextRangeScalar(0, W), rand.nextRangeScalar(H / 4, H));
23 }
24 paint->setColor(rand.nextU());
25 SkScalar width = rand.nextRangeScalar(1, 5);
26 width *= width;
27 paint->setStrokeWidth(width);
28 paint->setAlpha(0xFF);
29}
30
scroggof9d61012014-12-15 12:54:51 -080031static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) {
caryclarkfeff7d22014-10-09 05:36:03 -070032 p->moveTo(rand.nextRangeScalar(0, W), rand.nextRangeScalar(0, H));
33 for (int x = 0; x < 2; ++x) {
34 p->cubicTo(rand.nextRangeScalar(W / 4, W), rand.nextRangeScalar(0, H),
35 rand.nextRangeScalar(0, W), rand.nextRangeScalar(H / 4, H),
36 rand.nextRangeScalar(W / 4, W), rand.nextRangeScalar(H / 4, H));
37 }
38 paint->setColor(rand.nextU());
39 SkScalar width = rand.nextRangeScalar(1, 5);
40 width *= width;
41 paint->setStrokeWidth(width);
42 paint->setAlpha(0xFF);
43}
44
45class BeziersGM : public skiagm::GM {
46public:
47 BeziersGM() {}
48
49protected:
caryclarkfeff7d22014-10-09 05:36:03 -070050
mtklein36352bf2015-03-25 18:17:31 -070051 SkString onShortName() override {
caryclarkfeff7d22014-10-09 05:36:03 -070052 return SkString("beziers");
53 }
54
mtklein36352bf2015-03-25 18:17:31 -070055 SkISize onISize() override {
caryclarkfeff7d22014-10-09 05:36:03 -070056 return SkISize::Make(W, H*2);
57 }
58
mtklein36352bf2015-03-25 18:17:31 -070059 void onDraw(SkCanvas* canvas) override {
caryclarkfeff7d22014-10-09 05:36:03 -070060 SkPaint paint;
61 paint.setStyle(SkPaint::kStroke_Style);
62 paint.setStrokeWidth(SkIntToScalar(9)/2);
63 paint.setAntiAlias(true);
64
scroggof9d61012014-12-15 12:54:51 -080065 SkRandom rand;
caryclarkfeff7d22014-10-09 05:36:03 -070066 for (int i = 0; i < N; i++) {
67 SkPath p;
68 rnd_quad(&p, &paint, rand);
69 canvas->drawPath(p, paint);
70 }
71 canvas->translate(0, SH);
72 for (int i = 0; i < N; i++) {
73 SkPath p;
74 rnd_cubic(&p, &paint, rand);
75 canvas->drawPath(p, paint);
76 }
77 }
78
79private:
80 typedef skiagm::GM INHERITED;
81};
82
scroggo96f16e82015-12-10 13:31:59 -080083DEF_GM( return new BeziersGM; )