jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 10 | #include "SkTArray.h" |
| 11 | #include "SkRandom.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkBlurMaskFilter.h" |
mtklein | 7a1f45f | 2016-08-04 06:19:33 -0700 | [diff] [blame] | 14 | #include "SkColorFilter.h" |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 15 | #include "SkGradientShader.h" |
| 16 | #include "SkBlurDrawLooper.h" |
| 17 | #include "SkRect.h" |
| 18 | |
| 19 | namespace skiagm { |
| 20 | |
| 21 | class OvalGM : public GM { |
| 22 | public: |
| 23 | OvalGM() { |
| 24 | this->setBGColor(0xFF000000); |
| 25 | this->makePaints(); |
| 26 | this->makeMatrices(); |
| 27 | } |
| 28 | |
| 29 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | SkString onShortName() override { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 32 | return SkString("ovals"); |
| 33 | } |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 36 | return SkISize::Make(1200, 900); |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void makePaints() { |
| 40 | { |
| 41 | // no AA |
| 42 | SkPaint p; |
| 43 | fPaints.push_back(p); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | // AA |
| 48 | SkPaint p; |
| 49 | p.setAntiAlias(true); |
| 50 | fPaints.push_back(p); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | // AA with stroke style |
| 55 | SkPaint p; |
| 56 | p.setAntiAlias(true); |
| 57 | p.setStyle(SkPaint::kStroke_Style); |
| 58 | p.setStrokeWidth(SkIntToScalar(5)); |
| 59 | fPaints.push_back(p); |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | // AA with stroke style, width = 0 |
| 64 | SkPaint p; |
| 65 | p.setAntiAlias(true); |
| 66 | p.setStyle(SkPaint::kStroke_Style); |
| 67 | fPaints.push_back(p); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | // AA with stroke and fill style |
| 72 | SkPaint p; |
| 73 | p.setAntiAlias(true); |
| 74 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 75 | p.setStrokeWidth(SkIntToScalar(3)); |
| 76 | fPaints.push_back(p); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void makeMatrices() { |
| 81 | { |
| 82 | SkMatrix m; |
| 83 | m.setIdentity(); |
| 84 | fMatrices.push_back(m); |
| 85 | } |
| 86 | |
| 87 | { |
| 88 | SkMatrix m; |
| 89 | m.setScale(SkIntToScalar(3), SkIntToScalar(2)); |
| 90 | fMatrices.push_back(m); |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | SkMatrix m; |
| 95 | m.setScale(SkIntToScalar(2), SkIntToScalar(2)); |
| 96 | fMatrices.push_back(m); |
| 97 | } |
| 98 | |
| 99 | { |
| 100 | SkMatrix m; |
| 101 | m.setScale(SkIntToScalar(1), SkIntToScalar(2)); |
| 102 | fMatrices.push_back(m); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | SkMatrix m; |
| 107 | m.setScale(SkIntToScalar(4), SkIntToScalar(1)); |
| 108 | fMatrices.push_back(m); |
| 109 | } |
| 110 | |
| 111 | { |
| 112 | SkMatrix m; |
| 113 | m.setRotate(SkIntToScalar(90)); |
| 114 | fMatrices.push_back(m); |
| 115 | } |
| 116 | |
| 117 | { |
| 118 | SkMatrix m; |
| 119 | m.setSkew(SkIntToScalar(2), SkIntToScalar(3)); |
| 120 | fMatrices.push_back(m); |
| 121 | } |
| 122 | |
| 123 | { |
| 124 | SkMatrix m; |
| 125 | m.setRotate(SkIntToScalar(60)); |
| 126 | fMatrices.push_back(m); |
| 127 | } |
| 128 | } |
| 129 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 130 | SkColor genColor(SkRandom* rand) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 131 | SkScalar hsv[3]; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 132 | hsv[0] = rand->nextRangeF(0.0f, 360.0f); |
| 133 | hsv[1] = rand->nextRangeF(0.75f, 1.0f); |
| 134 | hsv[2] = rand->nextRangeF(0.75f, 1.0f); |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 135 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 136 | return sk_tool_utils::color_to_565(SkHSVToColor(hsv)); |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 137 | } |
| 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 140 | SkRandom rand(1); |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 141 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
| 142 | SkRect oval = SkRect::MakeLTRB(-20, -30, 20, 30); |
| 143 | |
| 144 | const SkScalar kXStart = 60.0f; |
| 145 | const SkScalar kYStart = 80.0f; |
| 146 | const int kXStep = 150; |
| 147 | const int kYStep = 160; |
| 148 | int maxX = fMatrices.count(); |
| 149 | |
| 150 | SkPaint rectPaint; |
| 151 | rectPaint.setAntiAlias(true); |
| 152 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 153 | rectPaint.setStrokeWidth(SkIntToScalar(0)); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 154 | rectPaint.setColor(sk_tool_utils::color_to_565(SK_ColorLTGRAY)); |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 155 | |
| 156 | int testCount = 0; |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 157 | for (int i = 0; i < fPaints.count(); ++i) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 158 | for (int j = 0; j < fMatrices.count(); ++j) { |
| 159 | canvas->save(); |
| 160 | SkMatrix mat = fMatrices[j]; |
| 161 | // position the oval, and make it at off-integer coords. |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 162 | mat.postTranslate(kXStart + SK_Scalar1 * kXStep * (testCount % maxX) + |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 163 | SK_Scalar1 / 4, |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 164 | kYStart + SK_Scalar1 * kYStep * (testCount / maxX) + |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 165 | 3 * SK_Scalar1 / 4); |
| 166 | canvas->concat(mat); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 167 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 168 | SkColor color = genColor(&rand); |
| 169 | fPaints[i].setColor(color); |
| 170 | |
| 171 | canvas->drawRect(oval, rectPaint); |
| 172 | canvas->drawOval(oval, fPaints[i]); |
| 173 | |
| 174 | canvas->restore(); |
| 175 | |
| 176 | ++testCount; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // special cases |
| 181 | |
| 182 | // non-scaled tall and skinny oval |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 183 | for (int i = 0; i < fPaints.count(); ++i) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 184 | SkRect oval = SkRect::MakeLTRB(-20, -60, 20, 60); |
| 185 | canvas->save(); |
| 186 | // position the oval, and make it at off-integer coords. |
| 187 | canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.55f + SK_Scalar1 / 4, |
| 188 | kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 189 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 190 | SkColor color = genColor(&rand); |
| 191 | fPaints[i].setColor(color); |
| 192 | |
| 193 | canvas->drawRect(oval, rectPaint); |
| 194 | canvas->drawOval(oval, fPaints[i]); |
| 195 | canvas->restore(); |
| 196 | } |
| 197 | |
| 198 | // non-scaled wide and short oval |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 199 | for (int i = 0; i < fPaints.count(); ++i) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 200 | SkRect oval = SkRect::MakeLTRB(-80, -30, 80, 30); |
| 201 | canvas->save(); |
| 202 | // position the oval, and make it at off-integer coords. |
| 203 | canvas->translate(kXStart + SK_Scalar1 * kXStep * 4 + SK_Scalar1 / 4, |
| 204 | kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 + |
| 205 | SK_ScalarHalf * kYStep); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 206 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 207 | SkColor color = genColor(&rand); |
| 208 | fPaints[i].setColor(color); |
| 209 | |
| 210 | canvas->drawRect(oval, rectPaint); |
| 211 | canvas->drawOval(oval, fPaints[i]); |
| 212 | canvas->restore(); |
| 213 | } |
| 214 | |
| 215 | // super skinny oval |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 216 | for (int i = 0; i < fPaints.count(); ++i) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 217 | SkRect oval = SkRect::MakeLTRB(0, -60, 1, 60); |
| 218 | canvas->save(); |
| 219 | // position the oval, and make it at off-integer coords. |
| 220 | canvas->translate(kXStart + SK_Scalar1 * kXStep * 3.25f + SK_Scalar1 / 4, |
| 221 | kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 222 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 223 | SkColor color = genColor(&rand); |
| 224 | fPaints[i].setColor(color); |
| 225 | |
| 226 | canvas->drawOval(oval, fPaints[i]); |
| 227 | canvas->restore(); |
| 228 | } |
| 229 | |
| 230 | // super short oval |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 231 | for (int i = 0; i < fPaints.count(); ++i) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 232 | SkRect oval = SkRect::MakeLTRB(-80, -1, 80, 0); |
| 233 | canvas->save(); |
| 234 | // position the oval, and make it at off-integer coords. |
| 235 | canvas->translate(kXStart + SK_Scalar1 * kXStep * 2.5f + SK_Scalar1 / 4, |
| 236 | kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 + |
| 237 | SK_ScalarHalf * kYStep); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 238 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 239 | SkColor color = genColor(&rand); |
| 240 | fPaints[i].setColor(color); |
| 241 | |
| 242 | canvas->drawOval(oval, fPaints[i]); |
| 243 | canvas->restore(); |
| 244 | } |
| 245 | |
| 246 | // radial gradient |
| 247 | SkPoint center = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0)); |
| 248 | SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN }; |
| 249 | SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 250 | auto shader = SkGradientShader::MakeRadial(center, 20, colors, pos, SK_ARRAY_COUNT(colors), |
| 251 | SkShader::kClamp_TileMode); |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 252 | |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 253 | for (int i = 0; i < fPaints.count(); ++i) { |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 254 | canvas->save(); |
| 255 | // position the path, and make it at off-integer coords. |
| 256 | canvas->translate(kXStart + SK_Scalar1 * kXStep * 0 + SK_Scalar1 / 4, |
| 257 | kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 + |
| 258 | SK_ScalarHalf * kYStep); |
skia.committer@gmail.com | cb6dc75 | 2013-04-19 07:01:00 +0000 | [diff] [blame] | 259 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 260 | SkColor color = genColor(&rand); |
| 261 | fPaints[i].setColor(color); |
| 262 | fPaints[i].setShader(shader); |
| 263 | |
| 264 | canvas->drawRect(oval, rectPaint); |
| 265 | canvas->drawOval(oval, fPaints[i]); |
jvanverth@google.com | cabd0ed | 2013-04-18 14:48:35 +0000 | [diff] [blame] | 266 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 267 | fPaints[i].setShader(nullptr); |
jvanverth@google.com | cabd0ed | 2013-04-18 14:48:35 +0000 | [diff] [blame] | 268 | |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 269 | canvas->restore(); |
| 270 | } |
Jim Van Verth | d952a99 | 2017-04-20 17:25:26 -0400 | [diff] [blame] | 271 | |
| 272 | // reflected oval |
| 273 | for (int i = 0; i < fPaints.count(); ++i) { |
| 274 | SkRect oval = SkRect::MakeLTRB(-30, -30, 30, 30); |
| 275 | canvas->save(); |
| 276 | // position the oval, and make it at off-integer coords. |
| 277 | canvas->translate(kXStart + SK_Scalar1 * kXStep * 5 + SK_Scalar1 / 4, |
| 278 | kYStart + SK_Scalar1 * kYStep * i + 3 * SK_Scalar1 / 4 + |
| 279 | SK_ScalarHalf * kYStep); |
| 280 | canvas->rotate(90); |
| 281 | canvas->scale(1, -1); |
| 282 | canvas->scale(1, 0.66f); |
| 283 | |
| 284 | SkColor color = genColor(&rand); |
| 285 | fPaints[i].setColor(color); |
| 286 | |
| 287 | canvas->drawRect(oval, rectPaint); |
| 288 | canvas->drawOval(oval, fPaints[i]); |
| 289 | canvas->restore(); |
| 290 | } |
jvanverth@google.com | 8e2962f | 2013-04-18 13:59:04 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | private: |
| 294 | SkTArray<SkPaint> fPaints; |
| 295 | SkTArray<SkMatrix> fMatrices; |
| 296 | |
| 297 | typedef GM INHERITED; |
| 298 | }; |
| 299 | |
| 300 | ////////////////////////////////////////////////////////////////////////////// |
| 301 | |
| 302 | static GM* MyFactory(void*) { return new OvalGM; } |
| 303 | static GMRegistry reg(MyFactory); |
| 304 | |
| 305 | } |