bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 7 | |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 8 | #include "gm.h" |
bsalomon@google.com | cecd3ac | 2011-09-13 15:39:00 +0000 | [diff] [blame] | 9 | #include "SkGradientShader.h" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 10 | #include "SkPath.h" |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class FillTypePerspGM : public GM { |
| 15 | SkPath fPath; |
| 16 | public: |
reed@google.com | d42e3f6 | 2012-03-30 20:04:21 +0000 | [diff] [blame] | 17 | FillTypePerspGM() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 18 | |
reed@google.com | d42e3f6 | 2012-03-30 20:04:21 +0000 | [diff] [blame] | 19 | void makePath() { |
| 20 | if (fPath.isEmpty()) { |
| 21 | const SkScalar radius = SkIntToScalar(45); |
| 22 | fPath.addCircle(SkIntToScalar(50), SkIntToScalar(50), radius); |
| 23 | fPath.addCircle(SkIntToScalar(100), SkIntToScalar(100), radius); |
| 24 | } |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 25 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 27 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkString onShortName() override { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 30 | return SkString("filltypespersp"); |
| 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 34 | return SkISize::Make(835, 840); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void showPath(SkCanvas* canvas, int x, int y, SkPath::FillType ft, |
| 38 | SkScalar scale, const SkPaint& paint) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 39 | const SkRect r = { 0, 0, SkIntToScalar(150), SkIntToScalar(150) }; |
| 40 | |
| 41 | canvas->save(); |
| 42 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 43 | canvas->clipRect(r); |
| 44 | canvas->drawColor(SK_ColorWHITE); |
| 45 | fPath.setFillType(ft); |
| 46 | canvas->translate(r.centerX(), r.centerY()); |
| 47 | canvas->scale(scale, scale); |
| 48 | canvas->translate(-r.centerX(), -r.centerY()); |
| 49 | canvas->drawPath(fPath, paint); |
| 50 | canvas->restore(); |
| 51 | } |
| 52 | |
| 53 | void showFour(SkCanvas* canvas, SkScalar scale, bool aa) { |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 54 | SkPaint paint; |
| 55 | SkPoint center = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(100)); |
| 56 | SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN}; |
| 57 | SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1}; |
| 58 | SkShader* s = SkGradientShader::CreateRadial(center, |
| 59 | SkIntToScalar(100), |
| 60 | colors, |
| 61 | pos, |
| 62 | SK_ARRAY_COUNT(colors), |
| 63 | SkShader::kClamp_TileMode); |
| 64 | paint.setShader(s)->unref(); |
| 65 | paint.setAntiAlias(aa); |
| 66 | |
| 67 | showPath(canvas, 0, 0, SkPath::kWinding_FillType, |
| 68 | scale, paint); |
| 69 | showPath(canvas, 200, 0, SkPath::kEvenOdd_FillType, |
| 70 | scale, paint); |
| 71 | showPath(canvas, 00, 200, SkPath::kInverseWinding_FillType, |
| 72 | scale, paint); |
| 73 | showPath(canvas, 200, 200, SkPath::kInverseEvenOdd_FillType, |
| 74 | scale, paint); |
| 75 | } |
| 76 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | d42e3f6 | 2012-03-30 20:04:21 +0000 | [diff] [blame] | 78 | this->makePath(); |
| 79 | |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 80 | // do perspective drawPaint as the background; |
| 81 | SkPaint bkgnrd; |
bsalomon@google.com | cecd3ac | 2011-09-13 15:39:00 +0000 | [diff] [blame] | 82 | SkPoint center = SkPoint::Make(SkIntToScalar(100), |
| 83 | SkIntToScalar(100)); |
| 84 | SkColor colors[] = {SK_ColorBLACK, SK_ColorCYAN, |
| 85 | SK_ColorYELLOW, SK_ColorWHITE}; |
| 86 | SkScalar pos[] = {0, SK_ScalarHalf / 2, |
| 87 | 3 * SK_ScalarHalf / 2, SK_Scalar1}; |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 88 | SkShader* s = SkGradientShader::CreateRadial(center, |
| 89 | SkIntToScalar(1000), |
| 90 | colors, |
| 91 | pos, |
| 92 | SK_ARRAY_COUNT(colors), |
| 93 | SkShader::kClamp_TileMode); |
| 94 | bkgnrd.setShader(s)->unref(); |
| 95 | canvas->save(); |
| 96 | canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); |
| 97 | SkMatrix mat; |
| 98 | mat.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 99 | mat.setPerspY(SK_Scalar1 / 1000); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 100 | canvas->concat(mat); |
| 101 | canvas->drawPaint(bkgnrd); |
| 102 | canvas->restore(); |
| 103 | |
| 104 | // draw the paths in perspective |
| 105 | SkMatrix persp; |
| 106 | persp.reset(); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 107 | persp.setPerspX(-SK_Scalar1 / 1800); |
| 108 | persp.setPerspY(SK_Scalar1 / 500); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 109 | canvas->concat(persp); |
| 110 | |
| 111 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 112 | const SkScalar scale = SkIntToScalar(5)/4; |
| 113 | |
| 114 | showFour(canvas, SK_Scalar1, false); |
| 115 | canvas->translate(SkIntToScalar(450), 0); |
bsalomon@google.com | cecd3ac | 2011-09-13 15:39:00 +0000 | [diff] [blame] | 116 | showFour(canvas, scale, false); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 117 | |
| 118 | canvas->translate(SkIntToScalar(-450), SkIntToScalar(450)); |
| 119 | showFour(canvas, SK_Scalar1, true); |
| 120 | canvas->translate(SkIntToScalar(450), 0); |
bsalomon@google.com | cecd3ac | 2011-09-13 15:39:00 +0000 | [diff] [blame] | 121 | showFour(canvas, scale, true); |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 122 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 123 | |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 124 | private: |
| 125 | typedef GM INHERITED; |
| 126 | }; |
| 127 | |
| 128 | ////////////////////////////////////////////////////////////////////////////// |
| 129 | |
bsalomon@google.com | cecd3ac | 2011-09-13 15:39:00 +0000 | [diff] [blame] | 130 | static GM* MyFactory(void*) { return new FillTypePerspGM; } |
bsalomon@google.com | 8c2fe99 | 2011-09-13 15:27:18 +0000 | [diff] [blame] | 131 | static GMRegistry reg(MyFactory); |
| 132 | |
| 133 | } |