bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Intel 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 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 8 | #include "SkBlurDrawLooper.h" |
| 9 | #include "SkBlurMask.h" |
mtklein | 7a1f45f | 2016-08-04 06:19:33 -0700 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 12 | #include "SkMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkTArray.h" |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class CircleGM : public GM { |
| 20 | public: |
| 21 | CircleGM() { |
| 22 | this->setBGColor(0xFF000000); |
| 23 | this->makePaints(); |
| 24 | this->makeMatrices(); |
| 25 | } |
| 26 | |
| 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 | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 30 | return SkString("circles"); |
| 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(1200, 900); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void makePaints() { |
| 38 | { |
| 39 | // no AA |
| 40 | SkPaint p; |
| 41 | fPaints.push_back(p); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | // AA |
| 46 | SkPaint p; |
| 47 | p.setAntiAlias(true); |
| 48 | fPaints.push_back(p); |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | // AA with mask filter |
| 53 | SkPaint p; |
| 54 | p.setAntiAlias(true); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 55 | p.setMaskFilter(SkMaskFilter::MakeBlur( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 56 | kNormal_SkBlurStyle, |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 57 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)))); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 58 | fPaints.push_back(p); |
| 59 | } |
| 60 | |
| 61 | { |
| 62 | // AA with radial shader |
| 63 | SkPaint p; |
| 64 | p.setAntiAlias(true); |
| 65 | SkPoint center = SkPoint::Make(SkIntToScalar(40), SkIntToScalar(40)); |
| 66 | SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN }; |
| 67 | SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 68 | p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors), |
| 69 | SkShader::kClamp_TileMode)); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 70 | fPaints.push_back(p); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | // AA with blur |
| 75 | SkPaint p; |
| 76 | p.setAntiAlias(true); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 77 | p.setLooper(SkBlurDrawLooper::Make(SK_ColorBLUE, |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 78 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)), |
Matt Sarett | f160ad4 | 2017-03-23 16:23:38 -0400 | [diff] [blame] | 79 | SkIntToScalar(5), SkIntToScalar(10))); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 80 | fPaints.push_back(p); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | // AA with stroke style |
| 85 | SkPaint p; |
| 86 | p.setAntiAlias(true); |
| 87 | p.setStyle(SkPaint::kStroke_Style); |
| 88 | p.setStrokeWidth(SkIntToScalar(3)); |
| 89 | fPaints.push_back(p); |
| 90 | } |
| 91 | |
| 92 | { |
| 93 | // AA with stroke style, width = 0 |
| 94 | SkPaint p; |
| 95 | p.setAntiAlias(true); |
| 96 | p.setStyle(SkPaint::kStroke_Style); |
| 97 | fPaints.push_back(p); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | // AA with stroke and fill style |
| 102 | SkPaint p; |
| 103 | p.setAntiAlias(true); |
| 104 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 105 | p.setStrokeWidth(SkIntToScalar(2)); |
| 106 | fPaints.push_back(p); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void makeMatrices() { |
| 111 | { |
| 112 | SkMatrix m; |
| 113 | m.setScale(SkIntToScalar(2), SkIntToScalar(3)); |
| 114 | fMatrices.push_back(m); |
| 115 | } |
| 116 | |
| 117 | { |
| 118 | SkMatrix m; |
| 119 | m.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 120 | fMatrices.push_back(m); |
| 121 | } |
| 122 | |
| 123 | { |
| 124 | SkMatrix m; |
| 125 | m.setSkew(SkIntToScalar(2), SkIntToScalar(3)); |
| 126 | fMatrices.push_back(m); |
| 127 | } |
| 128 | |
| 129 | { |
| 130 | SkMatrix m; |
| 131 | m.setSkew(SkIntToScalar(2), SkIntToScalar(2)); |
| 132 | fMatrices.push_back(m); |
| 133 | } |
| 134 | |
| 135 | { |
| 136 | SkMatrix m; |
| 137 | m.setRotate(SkIntToScalar(30)); |
| 138 | fMatrices.push_back(m); |
| 139 | } |
| 140 | } |
| 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | void onDraw(SkCanvas* canvas) override { |
bsalomon | ce1c886 | 2014-12-15 07:11:22 -0800 | [diff] [blame] | 143 | // Draw a giant AA circle as the background. |
| 144 | SkISize size = this->getISize(); |
| 145 | SkScalar giantRadius = SkTMin(SkIntToScalar(size.fWidth), |
| 146 | SkIntToScalar(size.fHeight)) / 2.f; |
| 147 | SkPoint giantCenter = SkPoint::Make(SkIntToScalar(size.fWidth/2), |
| 148 | SkIntToScalar(size.fHeight/2)); |
| 149 | SkPaint giantPaint; |
| 150 | giantPaint.setAntiAlias(true); |
| 151 | giantPaint.setColor(0x80808080); |
Hal Canary | 23e474c | 2017-05-15 13:35:35 -0400 | [diff] [blame] | 152 | canvas->drawCircle(giantCenter, giantRadius, giantPaint); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 153 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 154 | SkRandom rand; |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 155 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
bsalomon@google.com | 0b26c15 | 2012-04-16 14:19:32 +0000 | [diff] [blame] | 156 | int i; |
| 157 | for (i = 0; i < fPaints.count(); ++i) { |
| 158 | canvas->save(); |
| 159 | // position the path, and make it at off-integer coords. |
| 160 | canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4, |
| 161 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4); |
| 162 | SkColor color = rand.nextU(); |
| 163 | color |= 0xff000000; |
| 164 | fPaints[i].setColor(color); |
| 165 | |
| 166 | canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40), |
| 167 | SkIntToScalar(20), |
| 168 | fPaints[i]); |
| 169 | canvas->restore(); |
| 170 | } |
| 171 | |
| 172 | for (int j = 0; j < fMatrices.count(); ++j, ++i) { |
| 173 | canvas->save(); |
| 174 | |
| 175 | canvas->translate(SK_Scalar1 * 200 * (i % 5) + SK_Scalar1 / 4, |
| 176 | SK_Scalar1 * 200 * (i / 5) + 3 * SK_Scalar1 / 4); |
| 177 | |
| 178 | canvas->concat(fMatrices[j]); |
| 179 | |
| 180 | SkPaint paint; |
| 181 | paint.setAntiAlias(true); |
| 182 | |
| 183 | SkColor color = rand.nextU(); |
| 184 | color |= 0xff000000; |
| 185 | paint.setColor(color); |
| 186 | |
| 187 | canvas->drawCircle(SkIntToScalar(40), SkIntToScalar(40), |
| 188 | SkIntToScalar(20), |
| 189 | paint); |
| 190 | |
| 191 | canvas->restore(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | typedef GM INHERITED; |
| 197 | SkTArray<SkPaint> fPaints; |
| 198 | SkTArray<SkMatrix> fMatrices; |
| 199 | }; |
| 200 | |
| 201 | ////////////////////////////////////////////////////////////////////////////// |
| 202 | |
| 203 | static GM* MyFactory(void*) { return new CircleGM; } |
| 204 | static GMRegistry reg(MyFactory); |
| 205 | |
| 206 | } |