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