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