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