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