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