blob: 96242faf5f9a5a04fd24de1c9f3af2a30cd89e37 [file] [log] [blame]
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +00001/*
2 * Copyright 2013 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 "SkCanvas.h"
10#include "SkTArray.h"
11
reedd1bd1d72014-12-31 20:07:01 -080012class ConicPathsGM : public skiagm::GM {
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000013protected:
14
reedd1bd1d72014-12-31 20:07:01 -080015 SkString onShortName() SK_OVERRIDE {
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000016 return SkString("conicpaths");
17 }
18
reedd1bd1d72014-12-31 20:07:01 -080019 SkISize onISize() SK_OVERRIDE {
20 return SkISize::Make(950, 1000);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000021 }
22
reedd1bd1d72014-12-31 20:07:01 -080023 void onOnceBeforeDraw() SK_OVERRIDE {
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000024 {
reedd1bd1d72014-12-31 20:07:01 -080025 const SkScalar w = SkScalarSqrt(2)/2;
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000026 SkPath* conicCirlce = &fPaths.push_back();
27 conicCirlce->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080028 conicCirlce->conicTo(0, 50, 50, 50, w);
29 conicCirlce->rConicTo(50, 0, 50, -50, w);
30 conicCirlce->rConicTo(0, -50, -50, -50, w);
31 conicCirlce->rConicTo(-50, 0, -50, 50, w);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000032
33 }
34 {
35 SkPath* hyperbola = &fPaths.push_back();
36 hyperbola->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080037 hyperbola->conicTo(0, 100, 100, 100, 2);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000038 }
39 {
40 SkPath* thinHyperbola = &fPaths.push_back();
41 thinHyperbola->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080042 thinHyperbola->conicTo(100, 100, 5, 0, 2);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000043 }
44 {
45 SkPath* veryThinHyperbola = &fPaths.push_back();
46 veryThinHyperbola->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080047 veryThinHyperbola->conicTo(100, 100, 1, 0, 2);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000048 }
49 {
50 SkPath* closedHyperbola = &fPaths.push_back();
51 closedHyperbola->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080052 closedHyperbola->conicTo(100, 100, 0, 0, 2);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000053 }
54 {
55 // using 1 as weight defaults to using quadTo
56 SkPath* nearParabola = &fPaths.push_back();
57 nearParabola->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080058 nearParabola->conicTo(0, 100, 100, 100, 0.999f);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000059 }
60 {
61 SkPath* thinEllipse = &fPaths.push_back();
62 thinEllipse->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080063 thinEllipse->conicTo(100, 100, 5, 0, SK_ScalarHalf);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000064 }
65 {
66 SkPath* veryThinEllipse = &fPaths.push_back();
67 veryThinEllipse->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080068 veryThinEllipse->conicTo(100, 100, 1, 0, SK_ScalarHalf);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000069 }
70 {
71 SkPath* closedEllipse = &fPaths.push_back();
72 closedEllipse->moveTo(0, -0);
reedd1bd1d72014-12-31 20:07:01 -080073 closedEllipse->conicTo(100, 100, 0, 0, SK_ScalarHalf);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000074 }
75 }
76
reedd1bd1d72014-12-31 20:07:01 -080077 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
78 const SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000079
80 SkScalar maxH = 0;
reedd1bd1d72014-12-31 20:07:01 -080081 const SkScalar margin = 15;
82 canvas->translate(margin, margin);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000083 canvas->save();
84
reedd1bd1d72014-12-31 20:07:01 -080085 SkScalar x = margin;
86 int counter = 0;
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000087 for (int p = 0; p < fPaths.count(); ++p) {
88 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) {
89 for (int aa = 0; aa < 2; ++aa) {
90 for (int fh = 0; fh < 2; ++fh) {
91
92 const SkRect& bounds = fPaths[p].getBounds();
93
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000094 SkPaint paint;
95 paint.setARGB(kAlphaValue[a], 0, 0, 0);
96 paint.setAntiAlias(SkToBool(aa));
97 if (fh == 1) {
98 paint.setStyle(SkPaint::kStroke_Style);
99 paint.setStrokeWidth(0);
100 } else if (fh == 0) {
101 paint.setStyle(SkPaint::kFill_Style);
102 }
103 canvas->save();
104 canvas->translate(-bounds.fLeft, -bounds.fTop);
105 canvas->drawPath(fPaths[p], paint);
106 canvas->restore();
107
108 maxH = SkMaxScalar(maxH, bounds.height());
109
reedd1bd1d72014-12-31 20:07:01 -0800110 SkScalar dx = bounds.width() + margin;
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000111 x += dx;
112 canvas->translate(dx, 0);
reedd1bd1d72014-12-31 20:07:01 -0800113
114 if (++counter == 8) {
115 counter = 0;
116
117 canvas->restore();
118 canvas->translate(0, maxH + margin);
119 canvas->save();
120 maxH = 0;
121 x = margin;
122 }
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000123 }
124 }
125 }
126 }
127 canvas->restore();
128 }
129
reedd1bd1d72014-12-31 20:07:01 -0800130 uint32_t onGetFlags() const SK_OVERRIDE {
131 return kSkipPDF_Flag;
reed@google.comc464cbd2013-06-20 17:37:24 +0000132 }
133
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000134private:
135 SkTArray<SkPath> fPaths;
reedd1bd1d72014-12-31 20:07:01 -0800136 typedef skiagm::GM INHERITED;
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000137};
reedd1bd1d72014-12-31 20:07:01 -0800138DEF_GM( return SkNEW(ConicPathsGM); )
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000139
140//////////////////////////////////////////////////////////////////////////////
141