robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 1 | /* |
| 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" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurDrawLooper.h" |
| 10 | #include "SkBlurMask.h" |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 11 | #include "SkBlurMaskFilter.h" |
mtklein | 7a1f45f | 2016-08-04 06:19:33 -0700 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| 15 | #include "SkTArray.h" |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class RectsGM : public GM { |
| 20 | public: |
| 21 | RectsGM() { |
| 22 | this->setBGColor(0xFF000000); |
| 23 | this->makePaints(); |
| 24 | this->makeMatrices(); |
| 25 | this->makeRects(); |
| 26 | } |
| 27 | |
| 28 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 31 | return SkString("rects"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 35 | return SkISize::Make(1200, 900); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void makePaints() { |
| 39 | { |
| 40 | // no AA |
| 41 | SkPaint p; |
| 42 | p.setColor(SK_ColorWHITE); |
| 43 | fPaints.push_back(p); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | // AA |
| 48 | SkPaint p; |
| 49 | p.setColor(SK_ColorWHITE); |
| 50 | p.setAntiAlias(true); |
| 51 | fPaints.push_back(p); |
| 52 | } |
| 53 | |
| 54 | { |
commit-bot@chromium.org | a0fd1b3 | 2013-11-01 11:54:29 +0000 | [diff] [blame] | 55 | // AA with translucent |
| 56 | SkPaint p; |
| 57 | p.setColor(SK_ColorWHITE); |
| 58 | p.setAntiAlias(true); |
| 59 | p.setAlpha(0x66); |
| 60 | fPaints.push_back(p); |
| 61 | } |
| 62 | |
| 63 | { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 64 | // AA with mask filter |
| 65 | SkPaint p; |
| 66 | p.setColor(SK_ColorWHITE); |
| 67 | p.setAntiAlias(true); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 68 | p.setMaskFilter(SkBlurMaskFilter::Make( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 69 | kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 70 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 71 | SkBlurMaskFilter::kHighQuality_BlurFlag)); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 72 | fPaints.push_back(p); |
| 73 | } |
| 74 | |
| 75 | { |
| 76 | // AA with radial shader |
| 77 | SkPaint p; |
| 78 | p.setColor(SK_ColorWHITE); |
| 79 | p.setAntiAlias(true); |
| 80 | SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30)); |
| 81 | SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN }; |
| 82 | SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 83 | p.setShader(SkGradientShader::MakeRadial(center, 20, colors, pos, |
| 84 | SK_ARRAY_COUNT(colors), |
| 85 | SkShader::kClamp_TileMode)); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 86 | fPaints.push_back(p); |
| 87 | } |
| 88 | |
| 89 | { |
| 90 | // AA with blur |
| 91 | SkPaint p; |
| 92 | p.setColor(SK_ColorWHITE); |
| 93 | p.setAntiAlias(true); |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 94 | p.setLooper(SkBlurDrawLooper::Make(SK_ColorWHITE, |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 95 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)), |
Matt Sarett | f160ad4 | 2017-03-23 16:23:38 -0400 | [diff] [blame] | 96 | SkIntToScalar(5), SkIntToScalar(10))); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 97 | fPaints.push_back(p); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | // AA with stroke style |
| 102 | SkPaint p; |
| 103 | p.setColor(SK_ColorWHITE); |
| 104 | p.setAntiAlias(true); |
| 105 | p.setStyle(SkPaint::kStroke_Style); |
| 106 | p.setStrokeWidth(SkIntToScalar(3)); |
| 107 | fPaints.push_back(p); |
| 108 | } |
| 109 | |
| 110 | { |
commit-bot@chromium.org | a0fd1b3 | 2013-11-01 11:54:29 +0000 | [diff] [blame] | 111 | // AA with bevel-stroke style |
| 112 | SkPaint p; |
| 113 | p.setColor(SK_ColorWHITE); |
| 114 | p.setAntiAlias(true); |
| 115 | p.setStyle(SkPaint::kStroke_Style); |
| 116 | p.setStrokeJoin(SkPaint::kBevel_Join); |
| 117 | p.setStrokeWidth(SkIntToScalar(3)); |
| 118 | fPaints.push_back(p); |
| 119 | } |
| 120 | |
| 121 | { |
| 122 | // AA with round-stroke style |
| 123 | SkPaint p; |
| 124 | p.setColor(SK_ColorWHITE); |
| 125 | p.setAntiAlias(true); |
| 126 | p.setStyle(SkPaint::kStroke_Style); |
| 127 | p.setStrokeJoin(SkPaint::kRound_Join); |
| 128 | p.setStrokeWidth(SkIntToScalar(3)); |
| 129 | fPaints.push_back(p); |
| 130 | } |
| 131 | |
| 132 | { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 133 | // AA with stroke style, width = 0 |
| 134 | SkPaint p; |
| 135 | p.setColor(SK_ColorWHITE); |
| 136 | p.setAntiAlias(true); |
| 137 | p.setStyle(SkPaint::kStroke_Style); |
| 138 | fPaints.push_back(p); |
| 139 | } |
| 140 | |
| 141 | { |
commit-bot@chromium.org | a0fd1b3 | 2013-11-01 11:54:29 +0000 | [diff] [blame] | 142 | // AA with stroke style, width wider than rect width and/or height |
| 143 | SkPaint p; |
| 144 | p.setColor(SK_ColorWHITE); |
| 145 | p.setAntiAlias(true); |
| 146 | p.setStyle(SkPaint::kStroke_Style); |
| 147 | p.setStrokeWidth(SkIntToScalar(40)); |
| 148 | fPaints.push_back(p); |
| 149 | } |
| 150 | |
| 151 | { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 152 | // AA with stroke and fill style |
| 153 | SkPaint p; |
| 154 | p.setColor(SK_ColorWHITE); |
| 155 | p.setAntiAlias(true); |
| 156 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 157 | p.setStrokeWidth(SkIntToScalar(2)); |
| 158 | fPaints.push_back(p); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void makeMatrices() { |
| 163 | { |
| 164 | // 1x1.5 scale |
| 165 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 166 | m.setScale(1, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 167 | fMatrices.push_back(m); |
| 168 | } |
| 169 | |
| 170 | { |
| 171 | // 1.5x1.5 scale |
| 172 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 173 | m.setScale(1.5f, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 174 | fMatrices.push_back(m); |
| 175 | } |
| 176 | |
| 177 | { |
| 178 | // 1x1.5 skew |
| 179 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 180 | m.setSkew(1, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 181 | fMatrices.push_back(m); |
| 182 | } |
| 183 | |
| 184 | { |
| 185 | // 1.5x1.5 skew |
| 186 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 187 | m.setSkew(1.5f, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 188 | fMatrices.push_back(m); |
| 189 | } |
| 190 | |
| 191 | { |
| 192 | // 30 degree rotation |
| 193 | SkMatrix m; |
| 194 | m.setRotate(SkIntToScalar(30)); |
| 195 | fMatrices.push_back(m); |
| 196 | } |
robertphillips@google.com | 6f5abdd | 2013-04-29 17:44:45 +0000 | [diff] [blame] | 197 | |
| 198 | { |
| 199 | // 90 degree rotation |
| 200 | SkMatrix m; |
| 201 | m.setRotate(SkIntToScalar(90)); |
| 202 | fMatrices.push_back(m); |
| 203 | } |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void makeRects() { |
| 207 | { |
| 208 | // small square |
| 209 | SkRect r = SkRect::MakeLTRB(0, 0, 30, 30); |
| 210 | fRects.push_back(r); |
| 211 | } |
| 212 | |
| 213 | { |
| 214 | // thin vertical |
| 215 | SkRect r = SkRect::MakeLTRB(0, 0, 2, 40); |
| 216 | fRects.push_back(r); |
| 217 | } |
| 218 | |
| 219 | { |
| 220 | // thin horizontal |
| 221 | SkRect r = SkRect::MakeLTRB(0, 0, 40, 2); |
| 222 | fRects.push_back(r); |
| 223 | } |
| 224 | |
| 225 | { |
| 226 | // very thin |
| 227 | SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10); |
| 228 | fRects.push_back(r); |
| 229 | } |
| 230 | |
| 231 | { |
| 232 | // zaftig |
| 233 | SkRect r = SkRect::MakeLTRB(0, 0, 60, 60); |
| 234 | fRects.push_back(r); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // position the current test on the canvas |
| 239 | static void position(SkCanvas* canvas, int testCount) { |
| 240 | canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4, |
| 241 | SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4); |
| 242 | } |
| 243 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 244 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 245 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
| 246 | |
| 247 | int testCount = 0; |
| 248 | |
| 249 | for (int i = 0; i < fPaints.count(); ++i) { |
| 250 | for (int j = 0; j < fRects.count(); ++j, ++testCount) { |
| 251 | canvas->save(); |
| 252 | this->position(canvas, testCount); |
| 253 | canvas->drawRect(fRects[j], fPaints[i]); |
| 254 | canvas->restore(); |
| 255 | } |
| 256 | } |
| 257 | |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 258 | SkPaint paint; |
| 259 | paint.setColor(SK_ColorWHITE); |
| 260 | paint.setAntiAlias(true); |
| 261 | |
| 262 | for (int i = 0; i < fMatrices.count(); ++i) { |
| 263 | for (int j = 0; j < fRects.count(); ++j, ++testCount) { |
| 264 | canvas->save(); |
| 265 | this->position(canvas, testCount); |
| 266 | canvas->concat(fMatrices[i]); |
| 267 | canvas->drawRect(fRects[j], paint); |
| 268 | canvas->restore(); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | SkTArray<SkPaint> fPaints; |
| 275 | SkTArray<SkMatrix> fMatrices; |
| 276 | SkTArray<SkRect> fRects; |
| 277 | |
| 278 | typedef GM INHERITED; |
| 279 | }; |
| 280 | |
| 281 | ////////////////////////////////////////////////////////////////////////////// |
| 282 | |
| 283 | static GM* MyFactory(void*) { return new RectsGM; } |
| 284 | static GMRegistry reg(MyFactory); |
| 285 | |
| 286 | } |