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