blob: 67d752aeb1f32224122b9fe05774b56b5205a8b9 [file] [log] [blame]
reed19d8f9f2015-01-29 10:48:16 -08001/*
2 * Copyright 2015 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"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed19d8f9f2015-01-29 10:48:16 -080010#include "SkCanvas.h"
reed61adb1b2015-02-09 13:01:05 -080011#include "SkPathMeasure.h"
reed19d8f9f2015-01-29 10:48:16 -080012#include "SkRandom.h"
13
14class AddArcGM : public skiagm::GM {
reedd9adfe62015-02-01 19:01:04 -080015public:
16 AddArcGM() : fRotate(0) {}
17
reed19d8f9f2015-01-29 10:48:16 -080018protected:
19 SkString onShortName() SK_OVERRIDE { return SkString("addarc"); }
20
21 SkISize onISize() SK_OVERRIDE { return SkISize::Make(1040, 1040); }
22
23 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
24 canvas->translate(20, 20);
25
26 SkRect r = SkRect::MakeWH(1000, 1000);
27
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setStyle(SkPaint::kStroke_Style);
31 paint.setStrokeWidth(15);
32
33 const SkScalar inset = paint.getStrokeWidth() + 4;
34 const SkScalar sweepAngle = 345;
35 SkRandom rand;
36
reedd9adfe62015-02-01 19:01:04 -080037 SkScalar sign = 1;
reed19d8f9f2015-01-29 10:48:16 -080038 while (r.width() > paint.getStrokeWidth() * 3) {
39 paint.setColor(rand.nextU() | (0xFF << 24));
40 SkScalar startAngle = rand.nextUScalar1() * 360;
41
reedd9adfe62015-02-01 19:01:04 -080042 SkScalar speed = SkScalarSqrt(16 / r.width()) * 0.5f;
43 startAngle += fRotate * 360 * speed * sign;
44
reed19d8f9f2015-01-29 10:48:16 -080045 SkPath path;
46 path.addArc(r, startAngle, sweepAngle);
47 canvas->drawPath(path, paint);
48
49 r.inset(inset, inset);
reedd9adfe62015-02-01 19:01:04 -080050 sign = -sign;
reed19d8f9f2015-01-29 10:48:16 -080051 }
52 }
53
reed76113a92015-02-02 12:55:02 -080054 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
55 fRotate = timer.scaled(1, 360);
reedd9adfe62015-02-01 19:01:04 -080056 return true;
57 }
58
reed19d8f9f2015-01-29 10:48:16 -080059private:
reedd9adfe62015-02-01 19:01:04 -080060 SkScalar fRotate;
reed19d8f9f2015-01-29 10:48:16 -080061 typedef skiagm::GM INHERITED;
62};
63DEF_GM( return new AddArcGM; )
reed61adb1b2015-02-09 13:01:05 -080064
65///////////////////////////////////////////////////
66
67#define R 400
68
69class AddArcMeasGM : public skiagm::GM {
70public:
71 AddArcMeasGM() {}
72
73protected:
74 SkString onShortName() SK_OVERRIDE { return SkString("addarc_meas"); }
75
76 SkISize onISize() SK_OVERRIDE { return SkISize::Make(2*R + 40, 2*R + 40); }
77
78 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
79 canvas->translate(R + 20, R + 20);
80
81 SkPaint paint;
82 paint.setAntiAlias(true);
83 paint.setStyle(SkPaint::kStroke_Style);
84
85 SkPaint measPaint;
86 measPaint.setAntiAlias(true);
87 measPaint.setColor(SK_ColorRED);
88
89 const SkRect oval = SkRect::MakeLTRB(-R, -R, R, R);
90 canvas->drawOval(oval, paint);
91
92 for (SkScalar deg = 0; deg < 360; deg += 10) {
93 const SkScalar rad = SkDegreesToRadians(deg);
94 SkScalar rx = SkScalarCos(rad) * R;
95 SkScalar ry = SkScalarSin(rad) * R;
96
97 canvas->drawLine(0, 0, rx, ry, paint);
98
99 SkPath path;
100 path.addArc(oval, 0, deg);
101 SkPathMeasure meas(path, false);
102 SkScalar arcLen = rad * R;
103 SkPoint pos;
104 if (meas.getPosTan(arcLen, &pos, NULL)) {
105 canvas->drawLine(0, 0, pos.x(), pos.y(), measPaint);
106 }
107 }
108 }
109
110private:
111 typedef skiagm::GM INHERITED;
112};
113DEF_GM( return new AddArcMeasGM; )
reed8ed666d2015-02-10 17:44:26 -0800114
115///////////////////////////////////////////////////
116
117// Emphasize drawing a stroked oval (containing conics) and then scaling the results up,
118// to ensure that we compute the stroke taking the CTM into account
119//
120class StrokeCircleGM : public skiagm::GM {
121public:
122 StrokeCircleGM() : fRotate(0) {}
123
124protected:
125 SkString onShortName() SK_OVERRIDE { return SkString("strokecircle"); }
126
127 SkISize onISize() SK_OVERRIDE { return SkISize::Make(520, 520); }
128
129 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
130 canvas->scale(20, 20);
131 canvas->translate(13, 13);
132
133 SkPaint paint;
134 paint.setAntiAlias(true);
135 paint.setStyle(SkPaint::kStroke_Style);
136 paint.setStrokeWidth(SK_Scalar1 / 2);
137
138 const SkScalar delta = paint.getStrokeWidth() * 3 / 2;
139 SkRect r = SkRect::MakeXYWH(-12, -12, 24, 24);
140 SkRandom rand;
141
142 SkScalar sign = 1;
143 while (r.width() > paint.getStrokeWidth() * 2) {
144 SkAutoCanvasRestore acr(canvas, true);
145 canvas->rotate(fRotate * sign);
146
147 paint.setColor(rand.nextU() | (0xFF << 24));
148 canvas->drawOval(r, paint);
149 r.inset(delta, delta);
150 sign = -sign;
151 }
152 }
153
154 bool onAnimate(const SkAnimTimer& timer) SK_OVERRIDE {
155 fRotate = timer.scaled(60, 360);
156 return true;
157 }
158
159private:
160 SkScalar fRotate;
161
162 typedef skiagm::GM INHERITED;
163};
164DEF_GM( return new StrokeCircleGM; )