blob: 7819d0fbdc1ce111616732f46d7153c5b2c1af06 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
Mike Reed093de4e2020-08-03 16:33:14 -040011#include "include/core/SkPathBuilder.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040012#include "include/core/SkScalar.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/utils/SkRandom.h"
caryclarkfeff7d22014-10-09 05:36:03 -070016
17#define W 400
18#define H 400
19#define N 10
20
mtkleindbfd7ab2016-09-01 11:24:54 -070021constexpr SkScalar SH = SkIntToScalar(H);
caryclarkfeff7d22014-10-09 05:36:03 -070022
Mike Reed093de4e2020-08-03 16:33:14 -040023static SkPath rnd_quad(SkPaint* paint, SkRandom& rand) {
mtklein477ba0c2016-09-26 08:18:43 -070024 auto a = rand.nextRangeScalar(0,W),
25 b = rand.nextRangeScalar(0,H);
Mike Reed093de4e2020-08-03 16:33:14 -040026
27 SkPathBuilder builder;
28 builder.moveTo(a, b);
caryclarkfeff7d22014-10-09 05:36:03 -070029 for (int x = 0; x < 2; ++x) {
mtklein477ba0c2016-09-26 08:18:43 -070030 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);
Mike Reed093de4e2020-08-03 16:33:14 -040034 builder.quadTo(c,d,e,f);
caryclarkfeff7d22014-10-09 05:36:03 -070035 }
36 paint->setColor(rand.nextU());
37 SkScalar width = rand.nextRangeScalar(1, 5);
38 width *= width;
39 paint->setStrokeWidth(width);
Mike Reed9407e242019-02-15 16:13:57 -050040 paint->setAlphaf(1.0f);
Mike Reed093de4e2020-08-03 16:33:14 -040041 return builder.detach();
caryclarkfeff7d22014-10-09 05:36:03 -070042}
43
Mike Reed093de4e2020-08-03 16:33:14 -040044static SkPath rnd_cubic(SkPaint* paint, SkRandom& rand) {
mtklein1c127cc2016-09-26 06:29:18 -070045 auto a = rand.nextRangeScalar(0,W),
46 b = rand.nextRangeScalar(0,H);
Mike Reed093de4e2020-08-03 16:33:14 -040047
48 SkPathBuilder builder;
49 builder.moveTo(a, b);
caryclarkfeff7d22014-10-09 05:36:03 -070050 for (int x = 0; x < 2; ++x) {
mtklein1c127cc2016-09-26 06:29:18 -070051 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);
Mike Reed093de4e2020-08-03 16:33:14 -040057 builder.cubicTo(c,d,e,f,g,h);
caryclarkfeff7d22014-10-09 05:36:03 -070058 }
59 paint->setColor(rand.nextU());
60 SkScalar width = rand.nextRangeScalar(1, 5);
61 width *= width;
62 paint->setStrokeWidth(width);
Mike Reed9407e242019-02-15 16:13:57 -050063 paint->setAlphaf(1.0f);
Mike Reed093de4e2020-08-03 16:33:14 -040064 return builder.detach();
caryclarkfeff7d22014-10-09 05:36:03 -070065}
66
67class BeziersGM : public skiagm::GM {
68public:
69 BeziersGM() {}
70
71protected:
caryclarkfeff7d22014-10-09 05:36:03 -070072
mtklein36352bf2015-03-25 18:17:31 -070073 SkString onShortName() override {
caryclarkfeff7d22014-10-09 05:36:03 -070074 return SkString("beziers");
75 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 SkISize onISize() override {
caryclarkfeff7d22014-10-09 05:36:03 -070078 return SkISize::Make(W, H*2);
79 }
80
mtklein36352bf2015-03-25 18:17:31 -070081 void onDraw(SkCanvas* canvas) override {
caryclarkfeff7d22014-10-09 05:36:03 -070082 SkPaint paint;
83 paint.setStyle(SkPaint::kStroke_Style);
84 paint.setStrokeWidth(SkIntToScalar(9)/2);
85 paint.setAntiAlias(true);
86
scroggof9d61012014-12-15 12:54:51 -080087 SkRandom rand;
caryclarkfeff7d22014-10-09 05:36:03 -070088 for (int i = 0; i < N; i++) {
Mike Reed093de4e2020-08-03 16:33:14 -040089 canvas->drawPath(rnd_quad(&paint, rand), paint);
caryclarkfeff7d22014-10-09 05:36:03 -070090 }
91 canvas->translate(0, SH);
92 for (int i = 0; i < N; i++) {
Mike Reed093de4e2020-08-03 16:33:14 -040093 canvas->drawPath(rnd_cubic(&paint, rand), paint);
caryclarkfeff7d22014-10-09 05:36:03 -070094 }
95 }
96
97private:
John Stiles7571f9e2020-09-02 22:42:33 -040098 using INHERITED = skiagm::GM;
caryclarkfeff7d22014-10-09 05:36:03 -070099};
100
scroggo96f16e82015-12-10 13:31:59 -0800101DEF_GM( return new BeziersGM; )