OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 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 "SkBlurMaskFilter.h" |
| 10 #include "SkDashPathEffect.h" |
| 11 #include "SkGradientShader.h" |
| 12 #include "SkPaint.h" |
| 13 #include "SkPath.h" |
| 14 #include "SkPoint3.h" |
| 15 |
| 16 static const int kNumColumns = 6; |
| 17 static const int kNumRows = 8; |
| 18 static const int kRadius = 40; // radius of the snowflake |
| 19 static const int kPad = 5; // padding on both sides of the snowflake |
| 20 static const int kNumSpokes = 6; |
| 21 static const SkScalar kStrokeWidth = 5.0f; |
| 22 |
| 23 static void draw_fins(SkCanvas* canvas, const SkPoint& offset, float angle, cons
t SkPaint& paint) { |
| 24 SkScalar cos, sin; |
| 25 |
| 26 // first fin |
| 27 sin = SkScalarSinCos(angle + (SK_ScalarPI/4), &cos); |
| 28 sin *= kRadius / 2.0f; |
| 29 cos *= kRadius / 2.0f; |
| 30 |
| 31 SkPath p; |
| 32 p.moveTo(offset.fX, offset.fY); |
| 33 p.lineTo(offset.fX + cos, offset.fY + sin); |
| 34 canvas->drawPath(p, paint); |
| 35 |
| 36 // second fin |
| 37 sin = SkScalarSinCos(angle - (SK_ScalarPI/4), &cos); |
| 38 sin *= kRadius / 2.0f; |
| 39 cos *= kRadius / 2.0f; |
| 40 |
| 41 p.reset(); |
| 42 p.moveTo(offset.fX, offset.fY); |
| 43 p.lineTo(offset.fX + cos, offset.fY + sin); |
| 44 canvas->drawPath(p, paint); |
| 45 } |
| 46 |
| 47 // draw a snowflake centered at the origin |
| 48 static void draw_snowflake(SkCanvas* canvas, const SkPaint& paint) { |
| 49 |
| 50 canvas->clipRect(SkRect::MakeLTRB(-kRadius-kPad, -kRadius-kPad, kRadius+kPad
, kRadius+kPad)); |
| 51 |
| 52 SkScalar sin, cos, angle = 0.0f; |
| 53 for (int i = 0; i < kNumSpokes/2; ++i, angle += SK_ScalarPI/(kNumSpokes/2))
{ |
| 54 sin = SkScalarSinCos(angle, &cos); |
| 55 sin *= kRadius; |
| 56 cos *= kRadius; |
| 57 |
| 58 // main spoke |
| 59 SkPath p; |
| 60 p.moveTo(-cos, -sin); |
| 61 p.lineTo(cos, sin); |
| 62 canvas->drawPath(p, paint); |
| 63 |
| 64 // fins on positive side |
| 65 const SkPoint posOffset = SkPoint::Make(0.5f * cos, 0.5f * sin); |
| 66 draw_fins(canvas, posOffset, angle, paint); |
| 67 |
| 68 // fins on negative side |
| 69 const SkPoint negOffset = SkPoint::Make(-0.5f * cos, -0.5f * sin); |
| 70 draw_fins(canvas, negOffset, angle+SK_ScalarPI, paint); |
| 71 } |
| 72 } |
| 73 |
| 74 static void draw_row(SkCanvas* canvas, const SkPaint& paint, const SkMatrix& loc
alMatrix) { |
| 75 canvas->translate(kRadius+kPad, 0.0f); |
| 76 |
| 77 for (auto cap : { SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_
Cap }) { |
| 78 for (auto isAA : { true, false }) { |
| 79 SkPaint tmp(paint); |
| 80 tmp.setStrokeWidth(kStrokeWidth); |
| 81 tmp.setStyle(SkPaint::kStroke_Style); |
| 82 tmp.setStrokeCap(cap); |
| 83 tmp.setAntiAlias(isAA); |
| 84 |
| 85 int saveCount = canvas->save(); |
| 86 canvas->concat(localMatrix); |
| 87 draw_snowflake(canvas, tmp); |
| 88 canvas->restoreToCount(saveCount); |
| 89 |
| 90 canvas->translate(2*(kRadius+kPad), 0.0f); |
| 91 } |
| 92 } |
| 93 } |
| 94 |
| 95 namespace skiagm { |
| 96 |
| 97 // This GM exercises the special case of a stroked lines. |
| 98 // Various shaders are applied to ensure the coordinate spaces work out right. |
| 99 class StrokedLinesGM : public GM { |
| 100 public: |
| 101 StrokedLinesGM() { |
| 102 this->setBGColor(sk_tool_utils::color_to_565(0xFF1A65D7)); |
| 103 } |
| 104 |
| 105 protected: |
| 106 SkString onShortName() override { |
| 107 return SkString("strokedlines"); |
| 108 } |
| 109 |
| 110 SkISize onISize() override { |
| 111 return SkISize::Make(kNumColumns * (2*kRadius+2*kPad), kNumRows * (2*kRa
dius+2*kPad)); |
| 112 } |
| 113 |
| 114 void onOnceBeforeDraw() override { |
| 115 // paints |
| 116 { |
| 117 // basic white |
| 118 SkPaint p; |
| 119 p.setColor(SK_ColorWHITE); |
| 120 fPaints.push_back(p); |
| 121 } |
| 122 { |
| 123 // gradient |
| 124 SkColor colors[] = { SK_ColorRED, SK_ColorGREEN }; |
| 125 SkPoint pts[] = { {-kRadius-kPad, -kRadius-kPad }, { kRadius+kPad, k
Radius+kPad } }; |
| 126 |
| 127 SkPaint p; |
| 128 p.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 129 SkShader::kClamp_TileMode,
0, nullptr)); |
| 130 |
| 131 fPaints.push_back(p); |
| 132 } |
| 133 { |
| 134 // dashing |
| 135 SkScalar intervals[] = { kStrokeWidth, kStrokeWidth }; |
| 136 int intervalCount = (int) SK_ARRAY_COUNT(intervals); |
| 137 SkPaint p; |
| 138 p.setColor(SK_ColorWHITE); |
| 139 p.setPathEffect(SkDashPathEffect::Make(intervals, intervalCount, kSt
rokeWidth)); |
| 140 |
| 141 fPaints.push_back(p); |
| 142 } |
| 143 { |
| 144 // Bitmap shader |
| 145 SkBitmap bm; |
| 146 bm.allocN32Pixels(2, 2); |
| 147 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
| 148 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = 0x0; |
| 149 |
| 150 SkMatrix m; |
| 151 m.setRotate(12.0f); |
| 152 m.preScale(3.0f, 3.0f);; |
| 153 |
| 154 SkPaint p; |
| 155 p.setShader(SkShader::MakeBitmapShader(bm, |
| 156 SkShader::kRepeat_TileMode, |
| 157 SkShader::kRepeat_TileMode, |
| 158 &m)); |
| 159 fPaints.push_back(p); |
| 160 } |
| 161 { |
| 162 // blur |
| 163 SkPaint p; |
| 164 p.setColor(SK_ColorWHITE); |
| 165 p.setMaskFilter(SkBlurMaskFilter::Make(kOuter_SkBlurStyle, 3.0f, |
| 166 SkBlurMaskFilter::kHighQualit
y_BlurFlag)); |
| 167 fPaints.push_back(p); |
| 168 } |
| 169 |
| 170 // matrices |
| 171 { |
| 172 // rotation |
| 173 SkMatrix m; |
| 174 m.setRotate(12.0f); |
| 175 |
| 176 fMatrices.push_back(m); |
| 177 } |
| 178 { |
| 179 // skew |
| 180 SkMatrix m; |
| 181 m.setSkew(0.3f, 0.5f); |
| 182 |
| 183 fMatrices.push_back(m); |
| 184 } |
| 185 { |
| 186 // perspective |
| 187 SkMatrix m; |
| 188 m.reset(); |
| 189 m.setPerspX(-SK_Scalar1 / 300); |
| 190 m.setPerspY(SK_Scalar1 / 300); |
| 191 |
| 192 fMatrices.push_back(m); |
| 193 } |
| 194 |
| 195 SkASSERT(kNumRows == fPaints.count() + fMatrices.count()); |
| 196 } |
| 197 |
| 198 void onDraw(SkCanvas* canvas) override { |
| 199 canvas->translate(0, kRadius+kPad); |
| 200 |
| 201 for (int i = 0; i < fPaints.count(); ++i) { |
| 202 int saveCount = canvas->save(); |
| 203 draw_row(canvas, fPaints[i], SkMatrix::I()); |
| 204 canvas->restoreToCount(saveCount); |
| 205 |
| 206 canvas->translate(0, 2*(kRadius+kPad)); |
| 207 } |
| 208 |
| 209 for (int i = 0; i < fMatrices.count(); ++i) { |
| 210 int saveCount = canvas->save(); |
| 211 draw_row(canvas, fPaints[0], fMatrices[i]); |
| 212 canvas->restoreToCount(saveCount); |
| 213 |
| 214 canvas->translate(0, 2*(kRadius+kPad)); |
| 215 } |
| 216 } |
| 217 |
| 218 private: |
| 219 SkTArray<SkPaint> fPaints; |
| 220 SkTArray<SkMatrix> fMatrices; |
| 221 |
| 222 typedef GM INHERITED; |
| 223 }; |
| 224 |
| 225 ////////////////////////////////////////////////////////////////////////////// |
| 226 |
| 227 DEF_GM(return new StrokedLinesGM;) |
| 228 } |
OLD | NEW |