blob: 717dd8e1dd8d1c20d107d5207892cde14df374c5 [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
mtklein36352bf2015-03-25 18:17:31 -070015 SkString onShortName() override {
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000016 return SkString("conicpaths");
17 }
18
mtklein36352bf2015-03-25 18:17:31 -070019 SkISize onISize() override {
reed40c85e42015-01-05 10:01:25 -080020 return SkISize::Make(920, 960);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000021 }
22
mtklein36352bf2015-03-25 18:17:31 -070023 void onOnceBeforeDraw() 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();
reed40c85e42015-01-05 10:01:25 -080027 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();
reed40c85e42015-01-05 10:01:25 -080036 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();
reed40c85e42015-01-05 10:01:25 -080041 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();
reed40c85e42015-01-05 10:01:25 -080046 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();
reed40c85e42015-01-05 10:01:25 -080051 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();
reed40c85e42015-01-05 10:01:25 -080057 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();
reed40c85e42015-01-05 10:01:25 -080062 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();
reed40c85e42015-01-05 10:01:25 -080067 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();
reed40c85e42015-01-05 10:01:25 -080072 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 }
egdaniel5a23a142015-02-25 06:41:47 -080075 {
76 const SkScalar w = SkScalarSqrt(2)/2;
77 fGiantCircle.moveTo(2.1e+11f, -1.05e+11f);
78 fGiantCircle.conicTo(2.1e+11f, 0, 1.05e+11f, 0, w);
79 fGiantCircle.conicTo(0, 0, 0, -1.05e+11f, w);
80 fGiantCircle.conicTo(0, -2.1e+11f, 1.05e+11f, -2.1e+11f, w);
81 fGiantCircle.conicTo(2.1e+11f, -2.1e+11f, 2.1e+11f, -1.05e+11f, w);
82
83 }
84 }
85
86 void drawGiantCircle(SkCanvas* canvas) {
87 SkPaint paint;
88 canvas->drawPath(fGiantCircle, paint);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000089 }
90
mtklein36352bf2015-03-25 18:17:31 -070091 void onDraw(SkCanvas* canvas) override {
reedd1bd1d72014-12-31 20:07:01 -080092 const SkAlpha kAlphaValue[] = { 0xFF, 0x40 };
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000093
reedd1bd1d72014-12-31 20:07:01 -080094 const SkScalar margin = 15;
95 canvas->translate(margin, margin);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000096
reed40c85e42015-01-05 10:01:25 -080097 SkPaint paint;
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +000098 for (int p = 0; p < fPaths.count(); ++p) {
reed40c85e42015-01-05 10:01:25 -080099 canvas->save();
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000100 for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) {
reed40c85e42015-01-05 10:01:25 -0800101 paint.setARGB(kAlphaValue[a], 0, 0, 0);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000102 for (int aa = 0; aa < 2; ++aa) {
reed40c85e42015-01-05 10:01:25 -0800103 paint.setAntiAlias(SkToBool(aa));
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000104 for (int fh = 0; fh < 2; ++fh) {
reed40c85e42015-01-05 10:01:25 -0800105 paint.setStyle(fh ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000106
107 const SkRect& bounds = fPaths[p].getBounds();
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000108 canvas->save();
109 canvas->translate(-bounds.fLeft, -bounds.fTop);
110 canvas->drawPath(fPaths[p], paint);
111 canvas->restore();
112
reed40c85e42015-01-05 10:01:25 -0800113 canvas->translate(110, 0);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000114 }
115 }
116 }
reed40c85e42015-01-05 10:01:25 -0800117 canvas->restore();
118 canvas->translate(0, 110);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000119 }
120 canvas->restore();
egdaniel5a23a142015-02-25 06:41:47 -0800121
122 this->drawGiantCircle(canvas);
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000123 }
124
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000125private:
126 SkTArray<SkPath> fPaths;
egdaniel5a23a142015-02-25 06:41:47 -0800127 SkPath fGiantCircle;
reedd1bd1d72014-12-31 20:07:01 -0800128 typedef skiagm::GM INHERITED;
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000129};
reedd1bd1d72014-12-31 20:07:01 -0800130DEF_GM( return SkNEW(ConicPathsGM); )
egdaniel@google.comdef9f6e2013-06-20 16:54:31 +0000131
132//////////////////////////////////////////////////////////////////////////////
133