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 | |
| 8 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurDrawLooper.h" |
| 10 | #include "SkBlurMask.h" |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 11 | #include "SkBlurMaskFilter.h" |
| 12 | #include "SkGradientShader.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
| 14 | #include "SkTArray.h" |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 15 | |
| 16 | namespace skiagm { |
| 17 | |
| 18 | class RectsGM : public GM { |
| 19 | public: |
| 20 | RectsGM() { |
| 21 | this->setBGColor(0xFF000000); |
| 22 | this->makePaints(); |
| 23 | this->makeMatrices(); |
| 24 | this->makeRects(); |
| 25 | } |
| 26 | |
| 27 | protected: |
| 28 | virtual SkString onShortName() SK_OVERRIDE { |
| 29 | return SkString("rects"); |
| 30 | } |
| 31 | |
| 32 | virtual SkISize onISize() SK_OVERRIDE { |
| 33 | return make_isize(1200, 900); |
| 34 | } |
| 35 | |
| 36 | void makePaints() { |
| 37 | { |
| 38 | // no AA |
| 39 | SkPaint p; |
| 40 | p.setColor(SK_ColorWHITE); |
| 41 | fPaints.push_back(p); |
| 42 | } |
| 43 | |
| 44 | { |
| 45 | // AA |
| 46 | SkPaint p; |
| 47 | p.setColor(SK_ColorWHITE); |
| 48 | p.setAntiAlias(true); |
| 49 | fPaints.push_back(p); |
| 50 | } |
| 51 | |
| 52 | { |
| 53 | // AA with mask filter |
| 54 | SkPaint p; |
| 55 | p.setColor(SK_ColorWHITE); |
| 56 | p.setAntiAlias(true); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 57 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 58 | SkBlurMaskFilter::kNormal_BlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 59 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 60 | SkBlurMaskFilter::kHighQuality_BlurFlag); |
| 61 | p.setMaskFilter(mf)->unref(); |
| 62 | fPaints.push_back(p); |
| 63 | } |
| 64 | |
| 65 | { |
| 66 | // AA with radial shader |
| 67 | SkPaint p; |
| 68 | p.setColor(SK_ColorWHITE); |
| 69 | p.setAntiAlias(true); |
| 70 | SkPoint center = SkPoint::Make(SkIntToScalar(-5), SkIntToScalar(30)); |
| 71 | SkColor colors[] = { SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN }; |
| 72 | SkScalar pos[] = { 0, SK_ScalarHalf, SK_Scalar1 }; |
| 73 | SkShader* s = SkGradientShader::CreateRadial(center, |
| 74 | SkIntToScalar(20), |
| 75 | colors, |
| 76 | pos, |
| 77 | SK_ARRAY_COUNT(colors), |
| 78 | SkShader::kClamp_TileMode); |
| 79 | p.setShader(s)->unref(); |
| 80 | fPaints.push_back(p); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | // AA with blur |
| 85 | SkPaint p; |
| 86 | p.setColor(SK_ColorWHITE); |
| 87 | p.setAntiAlias(true); |
| 88 | SkBlurDrawLooper* shadowLooper = |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 89 | new SkBlurDrawLooper (SK_ColorWHITE, |
| 90 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(10)), |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 91 | SkIntToScalar(5), SkIntToScalar(10), |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 92 | SkBlurDrawLooper::kIgnoreTransform_BlurFlag | |
| 93 | SkBlurDrawLooper::kOverrideColor_BlurFlag | |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 94 | SkBlurDrawLooper::kHighQuality_BlurFlag); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 95 | SkAutoUnref aurL0(shadowLooper); |
| 96 | p.setLooper(shadowLooper); |
| 97 | fPaints.push_back(p); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | // AA with stroke style |
| 102 | SkPaint p; |
| 103 | p.setColor(SK_ColorWHITE); |
| 104 | p.setAntiAlias(true); |
| 105 | p.setStyle(SkPaint::kStroke_Style); |
| 106 | p.setStrokeWidth(SkIntToScalar(3)); |
| 107 | fPaints.push_back(p); |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | // AA with stroke style, width = 0 |
| 112 | SkPaint p; |
| 113 | p.setColor(SK_ColorWHITE); |
| 114 | p.setAntiAlias(true); |
| 115 | p.setStyle(SkPaint::kStroke_Style); |
| 116 | fPaints.push_back(p); |
| 117 | } |
| 118 | |
| 119 | { |
| 120 | // AA with stroke and fill style |
| 121 | SkPaint p; |
| 122 | p.setColor(SK_ColorWHITE); |
| 123 | p.setAntiAlias(true); |
| 124 | p.setStyle(SkPaint::kStrokeAndFill_Style); |
| 125 | p.setStrokeWidth(SkIntToScalar(2)); |
| 126 | fPaints.push_back(p); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void makeMatrices() { |
| 131 | { |
| 132 | // 1x1.5 scale |
| 133 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 134 | m.setScale(1, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 135 | fMatrices.push_back(m); |
| 136 | } |
| 137 | |
| 138 | { |
| 139 | // 1.5x1.5 scale |
| 140 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 141 | m.setScale(1.5f, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 142 | fMatrices.push_back(m); |
| 143 | } |
| 144 | |
| 145 | { |
| 146 | // 1x1.5 skew |
| 147 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 148 | m.setSkew(1, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 149 | fMatrices.push_back(m); |
| 150 | } |
| 151 | |
| 152 | { |
| 153 | // 1.5x1.5 skew |
| 154 | SkMatrix m; |
reed@google.com | 56a2e0d | 2013-03-19 14:50:41 +0000 | [diff] [blame] | 155 | m.setSkew(1.5f, 1.5f); |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 156 | fMatrices.push_back(m); |
| 157 | } |
| 158 | |
| 159 | { |
| 160 | // 30 degree rotation |
| 161 | SkMatrix m; |
| 162 | m.setRotate(SkIntToScalar(30)); |
| 163 | fMatrices.push_back(m); |
| 164 | } |
robertphillips@google.com | 6f5abdd | 2013-04-29 17:44:45 +0000 | [diff] [blame] | 165 | |
| 166 | { |
| 167 | // 90 degree rotation |
| 168 | SkMatrix m; |
| 169 | m.setRotate(SkIntToScalar(90)); |
| 170 | fMatrices.push_back(m); |
| 171 | } |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void makeRects() { |
| 175 | { |
| 176 | // small square |
| 177 | SkRect r = SkRect::MakeLTRB(0, 0, 30, 30); |
| 178 | fRects.push_back(r); |
| 179 | } |
| 180 | |
| 181 | { |
| 182 | // thin vertical |
| 183 | SkRect r = SkRect::MakeLTRB(0, 0, 2, 40); |
| 184 | fRects.push_back(r); |
| 185 | } |
| 186 | |
| 187 | { |
| 188 | // thin horizontal |
| 189 | SkRect r = SkRect::MakeLTRB(0, 0, 40, 2); |
| 190 | fRects.push_back(r); |
| 191 | } |
| 192 | |
| 193 | { |
| 194 | // very thin |
| 195 | SkRect r = SkRect::MakeLTRB(0, 0, 0.25f, 10); |
| 196 | fRects.push_back(r); |
| 197 | } |
| 198 | |
| 199 | { |
| 200 | // zaftig |
| 201 | SkRect r = SkRect::MakeLTRB(0, 0, 60, 60); |
| 202 | fRects.push_back(r); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // position the current test on the canvas |
| 207 | static void position(SkCanvas* canvas, int testCount) { |
| 208 | canvas->translate(SK_Scalar1 * 100 * (testCount % 10) + SK_Scalar1 / 4, |
| 209 | SK_Scalar1 * 100 * (testCount / 10) + 3 * SK_Scalar1 / 4); |
| 210 | } |
| 211 | |
| 212 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 213 | SkAutoCommentBlock acb(canvas, "onDraw"); |
| 214 | |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 215 | canvas->translate(20 * SK_Scalar1, 20 * SK_Scalar1); |
| 216 | |
| 217 | int testCount = 0; |
| 218 | |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 219 | canvas->addComment("Test", "Various Paints"); |
| 220 | |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 221 | for (int i = 0; i < fPaints.count(); ++i) { |
| 222 | for (int j = 0; j < fRects.count(); ++j, ++testCount) { |
| 223 | canvas->save(); |
| 224 | this->position(canvas, testCount); |
| 225 | canvas->drawRect(fRects[j], fPaints[i]); |
| 226 | canvas->restore(); |
| 227 | } |
| 228 | } |
| 229 | |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 230 | canvas->addComment("Test", "Matrices"); |
| 231 | |
robertphillips@google.com | 14c3fc6 | 2013-03-19 14:11:15 +0000 | [diff] [blame] | 232 | SkPaint paint; |
| 233 | paint.setColor(SK_ColorWHITE); |
| 234 | paint.setAntiAlias(true); |
| 235 | |
| 236 | for (int i = 0; i < fMatrices.count(); ++i) { |
| 237 | for (int j = 0; j < fRects.count(); ++j, ++testCount) { |
| 238 | canvas->save(); |
| 239 | this->position(canvas, testCount); |
| 240 | canvas->concat(fMatrices[i]); |
| 241 | canvas->drawRect(fRects[j], paint); |
| 242 | canvas->restore(); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | private: |
| 248 | SkTArray<SkPaint> fPaints; |
| 249 | SkTArray<SkMatrix> fMatrices; |
| 250 | SkTArray<SkRect> fRects; |
| 251 | |
| 252 | typedef GM INHERITED; |
| 253 | }; |
| 254 | |
| 255 | ////////////////////////////////////////////////////////////////////////////// |
| 256 | |
| 257 | static GM* MyFactory(void*) { return new RectsGM; } |
| 258 | static GMRegistry reg(MyFactory); |
| 259 | |
| 260 | } |