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