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