blob: ac212278cf026c159d90db106620e851ad911552 [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"
9#include "SkRandom.h"
10
11#define W 400
12#define H 400
13#define N 10
14
15static const SkScalar SW = SkIntToScalar(W);
16static const SkScalar SH = SkIntToScalar(H);
17
18static void rnd_quad(SkPath* p, SkPaint* paint, SkLCGRandom& rand) {
19 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
31static void rnd_cubic(SkPath* p, SkPaint* paint, SkLCGRandom& rand) {
32 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:
50 virtual uint32_t onGetFlags() const SK_OVERRIDE {
51 return kSkipTiled_Flag;
52 }
53
54 virtual SkString onShortName() {
55 return SkString("beziers");
56 }
57
58 virtual SkISize onISize() {
59 return SkISize::Make(W, H*2);
60 }
61
62 virtual void onDraw(SkCanvas* canvas) {
63 SkPaint paint;
64 paint.setStyle(SkPaint::kStroke_Style);
65 paint.setStrokeWidth(SkIntToScalar(9)/2);
66 paint.setAntiAlias(true);
67
68 SkLCGRandom rand;
69 for (int i = 0; i < N; i++) {
70 SkPath p;
71 rnd_quad(&p, &paint, rand);
72 canvas->drawPath(p, paint);
73 }
74 canvas->translate(0, SH);
75 for (int i = 0; i < N; i++) {
76 SkPath p;
77 rnd_cubic(&p, &paint, rand);
78 canvas->drawPath(p, paint);
79 }
80 }
81
82private:
83 typedef skiagm::GM INHERITED;
84};
85
86static skiagm::GM* F0(void*) { return new BeziersGM; }
87static skiagm::GMRegistry R0(F0);