1 2 /* 3 * Copyright 2011 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 #include "gm.h" 9 #include "SkGradientShader.h" 10 11 namespace skiagm { 12 13 class FillTypePerspGM : public GM { 14 SkPath fPath; 15 public: 16 FillTypePerspGM() {} 17 18 void makePath() { 19 if (fPath.isEmpty()) { 20 const SkScalar radius = SkIntToScalar(45); 21 fPath.addCircle(SkIntToScalar(50), SkIntToScalar(50), radius); 22 fPath.addCircle(SkIntToScalar(100), SkIntToScalar(100), radius); 23 } 24 } 25 26 protected: 27 virtual SkString onShortName() { 28 return SkString("filltypespersp"); 29 } 30 31 virtual SkISize onISize() { 32 return make_isize(835, 840); 33 } 34 35 void showPath(SkCanvas* canvas, int x, int y, SkPath::FillType ft, 36 SkScalar scale, const SkPaint& paint) { 37 38 const SkRect r = { 0, 0, SkIntToScalar(150), SkIntToScalar(150) }; 39 40 canvas->save(); 41 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 42 canvas->clipRect(r); 43 canvas->drawColor(SK_ColorWHITE); 44 fPath.setFillType(ft); 45 canvas->translate(r.centerX(), r.centerY()); 46 canvas->scale(scale, scale); 47 canvas->translate(-r.centerX(), -r.centerY()); 48 canvas->drawPath(fPath, paint); 49 canvas->restore(); 50 } 51 52 void showFour(SkCanvas* canvas, SkScalar scale, bool aa) { 53 54 SkPaint paint; 55 SkPoint center = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(100)); 56 SkColor colors[] = {SK_ColorBLUE, SK_ColorRED, SK_ColorGREEN}; 57 SkScalar pos[] = {0, SK_ScalarHalf, SK_Scalar1}; 58 SkShader* s = SkGradientShader::CreateRadial(center, 59 SkIntToScalar(100), 60 colors, 61 pos, 62 SK_ARRAY_COUNT(colors), 63 SkShader::kClamp_TileMode); 64 paint.setShader(s)->unref(); 65 paint.setAntiAlias(aa); 66 67 showPath(canvas, 0, 0, SkPath::kWinding_FillType, 68 scale, paint); 69 showPath(canvas, 200, 0, SkPath::kEvenOdd_FillType, 70 scale, paint); 71 showPath(canvas, 00, 200, SkPath::kInverseWinding_FillType, 72 scale, paint); 73 showPath(canvas, 200, 200, SkPath::kInverseEvenOdd_FillType, 74 scale, paint); 75 } 76 77 virtual void onDraw(SkCanvas* canvas) { 78 this->makePath(); 79 80 // do perspective drawPaint as the background; 81 SkPaint bkgnrd; 82 SkPoint center = SkPoint::Make(SkIntToScalar(100), 83 SkIntToScalar(100)); 84 SkColor colors[] = {SK_ColorBLACK, SK_ColorCYAN, 85 SK_ColorYELLOW, SK_ColorWHITE}; 86 SkScalar pos[] = {0, SK_ScalarHalf / 2, 87 3 * SK_ScalarHalf / 2, SK_Scalar1}; 88 SkShader* s = SkGradientShader::CreateRadial(center, 89 SkIntToScalar(1000), 90 colors, 91 pos, 92 SK_ARRAY_COUNT(colors), 93 SkShader::kClamp_TileMode); 94 bkgnrd.setShader(s)->unref(); 95 canvas->save(); 96 canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); 97 SkMatrix mat; 98 mat.reset(); 99 mat.setPerspY(SkScalarToPersp(SK_Scalar1 / 1000)); 100 canvas->concat(mat); 101 canvas->drawPaint(bkgnrd); 102 canvas->restore(); 103 104 // draw the paths in perspective 105 SkMatrix persp; 106 persp.reset(); 107 persp.setPerspX(SkScalarToPersp(-SK_Scalar1 / 1800)); 108 persp.setPerspY(SkScalarToPersp(SK_Scalar1 / 500)); 109 canvas->concat(persp); 110 111 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); 112 const SkScalar scale = SkIntToScalar(5)/4; 113 114 showFour(canvas, SK_Scalar1, false); 115 canvas->translate(SkIntToScalar(450), 0); 116 showFour(canvas, scale, false); 117 118 canvas->translate(SkIntToScalar(-450), SkIntToScalar(450)); 119 showFour(canvas, SK_Scalar1, true); 120 canvas->translate(SkIntToScalar(450), 0); 121 showFour(canvas, scale, true); 122 } 123 124 private: 125 typedef GM INHERITED; 126 }; 127 128 ////////////////////////////////////////////////////////////////////////////// 129 130 static GM* MyFactory(void*) { return new FillTypePerspGM; } 131 static GMRegistry reg(MyFactory); 132 133 } 134