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)), |
| 96 | SkIntToScalar(5), SkIntToScalar(10), |
| 97 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
| 98 | SkBlurDrawLooper::kOverrideColor_BlurFlag | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 99 | SkBlurDrawLooper::kHighQuality_BlurFlag)); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 100 | fPaints.push_back(p); |
| 101 | } |
| 102 | |
| 103 | { |
| 104 | // AA with stroke style |
| 105 | SkPaint p; |
| 106 | p.setColor(SK_ColorWHITE); |
| 107 | p.setAntiAlias(true); |
| 108 | p.setStyle(SkPaint::kStroke_Style); |
| 109 | p.setStrokeWidth(SkIntToScalar(3)); |
| 110 | fPaints.push_back(p); |
| 111 | } |
| 112 | |
| 113 | { |
commit-bot@chromium.org | a0fd1b3 | 2013-11-01 11:54:29 +0000 | [diff] [blame] | 114 | // AA with bevel-stroke style |
| 115 | SkPaint p; |
| 116 | p.setColor(SK_ColorWHITE); |
| 117 | p.setAntiAlias(true); |
| 118 | p.setStyle(SkPaint::kStroke_Style); |
| 119 | p.setStrokeJoin(SkPaint::kBevel_Join); |
| 120 | p.setStrokeWidth(SkIntToScalar(3)); |
| 121 | fPaints.push_back(p); |
| 122 | } |
| 123 | |
| 124 | { |
| 125 | // AA with round-stroke style |
| 126 | SkPaint p; |
| 127 | p.setColor(SK_ColorWHITE); |
| 128 | p.setAntiAlias(true); |
| 129 | p.setStyle(SkPaint::kStroke_Style); |
| 130 | p.setStrokeJoin(SkPaint::kRound_Join); |
| 131 | p.setStrokeWidth(SkIntToScalar(3)); |
| 132 | fPaints.push_back(p); |
| 133 | } |
| 134 | |
| 135 | { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 136 | // AA with stroke style, width = 0 |
| 137 | SkPaint p; |
| 138 | p.setColor(SK_ColorWHITE); |
| 139 | p.setAntiAlias(true); |
| 140 | p.setStyle(SkPaint::kStroke_Style); |
| 141 | fPaints.push_back(p); |
| 142 | } |
| 143 | |
| 144 | { |
commit-bot@chromium.org | a0fd1b3 | 2013-11-01 11:54:29 +0000 | [diff] [blame] | 145 | // AA with stroke style, width wider than rect width and/or height |
| 146 | SkPaint p; |
| 147 | p.setColor(SK_ColorWHITE); |
| 148 | p.setAntiAlias(true); |
| 149 | p.setStyle(SkPaint::kStroke_Style); |
| 150 | p.setStrokeWidth(SkIntToScalar(40)); |
| 151 | fPaints.push_back(p); |
| 152 | } |
| 153 | |
| 154 | { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 155 | // AA with stroke and fill style |
| 156 | SkPaint p; |
| 157 | p.setColor(SK_ColorWHITE); |
| 158 | p.setAntiAlias(true); |
| 159 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 160 | p.setStrokeWidth(SkIntToScalar(2)); |
| 161 | fPaints.push_back(p); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void makeMatrices() { |
| 166 | { |
| 167 | // 1x1.5 scale |
| 168 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 169 | m.setScale(1, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 170 | fMatrices.push_back(m); |
| 171 | } |
| 172 | |
| 173 | { |
| 174 | // 1.5x1.5 scale |
| 175 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 176 | m.setScale(1.5f, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 177 | fMatrices.push_back(m); |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | // 1x1.5 skew |
| 182 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 183 | m.setSkew(1, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 184 | fMatrices.push_back(m); |
| 185 | } |
| 186 | |
| 187 | { |
| 188 | // 1.5x1.5 skew |
| 189 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 190 | m.setSkew(1.5f, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 191 | fMatrices.push_back(m); |
| 192 | } |
| 193 | |
| 194 | { |
| 195 | // 30 degree rotation |
| 196 | SkMatrix m; |
| 197 | m.setRotate(SkIntToScalar(30)); |
| 198 | fMatrices.push_back(m); |
| 199 | } |
robertphillips@google.com | 6f5abdd | 2013-04-29 17:44:45 +0000 | [diff] [blame] | 200 | |
| 201 | { |
| 202 | // 90 degree rotation |
| 203 | SkMatrix m; |
| 204 | m.setRotate(SkIntToScalar(90)); |
| 205 | fMatrices.push_back(m); |
| 206 | } |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void makeRects() { |
| 210 | { |
| 211 | // small square |
| 212 | SkRect r = SkRect::MakeLTRB(0, 0, 30, 30); |
| 213 | fRects.push_back(r); |
| 214 | } |
| 215 | |
| 216 | { |
| 217 | // thin vertical |
| 218 | SkRect r = SkRect::MakeLTRB(0, 0, 2, 40); |
| 219 | fRects.push_back(r); |
| 220 | } |
| 221 | |
| 222 | { |
| 223 | // thin horizontal |
| 224 | SkRect r = SkRect::MakeLTRB(0, 0, 40, 2); |
| 225 | fRects.push_back(r); |
| 226 | } |
| 227 | |
| 228 | { |
| 229 | // very thin |
| 230 | SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10); |
| 231 | fRects.push_back(r); |
| 232 | } |
| 233 | |
| 234 | { |
| 235 | // zaftig |
| 236 | SkRect r = SkRect::MakeLTRB(0, 0, 60, 60); |
| 237 | fRects.push_back(r); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // position the current test on the canvas |
| 242 | static void position(SkCanvas* canvas, int testCount) { |
| 243 | canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4, |
| 244 | SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4); |
| 245 | } |
| 246 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 247 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 248 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
| 249 | |
| 250 | int testCount = 0; |
| 251 | |
| 252 | for (int i = 0; i < fPaints.count(); ++i) { |
| 253 | for (int j = 0; j < fRects.count(); ++j, ++testCount) { |
| 254 | canvas->save(); |
| 255 | this->position(canvas, testCount); |
| 256 | canvas->drawRect(fRects[j], fPaints[i]); |
| 257 | canvas->restore(); |
| 258 | } |
| 259 | } |
| 260 | |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 261 | SkPaint paint; |
| 262 | paint.setColor(SK_ColorWHITE); |
| 263 | paint.setAntiAlias(true); |
| 264 | |
| 265 | for (int i = 0; i < fMatrices.count(); ++i) { |
| 266 | for (int j = 0; j < fRects.count(); ++j, ++testCount) { |
| 267 | canvas->save(); |
| 268 | this->position(canvas, testCount); |
| 269 | canvas->concat(fMatrices[i]); |
| 270 | canvas->drawRect(fRects[j], paint); |
| 271 | canvas->restore(); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | private: |
| 277 | SkTArray<SkPaint> fPaints; |
| 278 | SkTArray<SkMatrix> fMatrices; |
| 279 | SkTArray<SkRect> fRects; |
| 280 | |
| 281 | typedef GM INHERITED; |
| 282 | }; |
| 283 | |
| 284 | ////////////////////////////////////////////////////////////////////////////// |
| 285 | |
| 286 | static GM* MyFactory(void*) { return new RectsGM; } |
| 287 | static GMRegistry reg(MyFactory); |
| 288 | |
| 289 | } |