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