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