commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +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 "SkCanvas.h" |
| 10 | #include "SkTArray.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | class HairlinesGM : public GM { |
| 15 | protected: |
| 16 | |
| 17 | virtual SkString onShortName() SK_OVERRIDE { |
| 18 | return SkString("hairlines"); |
| 19 | } |
| 20 | |
| 21 | virtual SkISize onISize() { return make_isize(800, 600); } |
| 22 | |
| 23 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 24 | { |
| 25 | SkPath* lineAnglesPath = &fPaths.push_back(); |
| 26 | enum { |
| 27 | kNumAngles = 15, |
| 28 | kRadius = 40, |
| 29 | }; |
| 30 | for (int i = 0; i < kNumAngles; ++i) { |
| 31 | SkScalar angle = SK_ScalarPI * SkIntToScalar(i) / kNumAngles; |
| 32 | SkScalar x = kRadius * SkScalarCos(angle); |
| 33 | SkScalar y = kRadius * SkScalarSin(angle); |
| 34 | lineAnglesPath->moveTo(x, y); |
| 35 | lineAnglesPath->lineTo(-x, -y); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | { |
| 40 | SkPath* kindaTightQuad = &fPaths.push_back(); |
| 41 | kindaTightQuad->moveTo(0, -10 * SK_Scalar1); |
| 42 | kindaTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -10 * SK_Scalar1, 0); |
| 43 | } |
| 44 | |
| 45 | { |
| 46 | SkPath* tightQuad = &fPaths.push_back(); |
| 47 | tightQuad->moveTo(0, -5 * SK_Scalar1); |
| 48 | tightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -5 * SK_Scalar1, 0); |
| 49 | } |
| 50 | |
| 51 | { |
| 52 | SkPath* tighterQuad = &fPaths.push_back(); |
| 53 | tighterQuad->moveTo(0, -2 * SK_Scalar1); |
| 54 | tighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -2 * SK_Scalar1, 0); |
| 55 | } |
| 56 | |
| 57 | { |
| 58 | SkPath* unevenTighterQuad = &fPaths.push_back(); |
| 59 | unevenTighterQuad->moveTo(0, -1 * SK_Scalar1); |
| 60 | SkPoint p; |
| 61 | p.set(-2 * SK_Scalar1 + 3 * SkIntToScalar(102) / 4, SkIntToScalar(75)); |
| 62 | unevenTighterQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), p.fX, p.fY); |
| 63 | } |
| 64 | |
| 65 | { |
| 66 | SkPath* reallyTightQuad = &fPaths.push_back(); |
| 67 | reallyTightQuad->moveTo(0, -1 * SK_Scalar1); |
| 68 | reallyTightQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), -1 * SK_Scalar1, 0); |
| 69 | } |
| 70 | |
| 71 | { |
| 72 | SkPath* closedQuad = &fPaths.push_back(); |
| 73 | closedQuad->moveTo(0, -0); |
| 74 | closedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), 0, 0); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | SkPath* unevenClosedQuad = &fPaths.push_back(); |
| 79 | unevenClosedQuad->moveTo(0, -0); |
| 80 | unevenClosedQuad->quadTo(SkIntToScalar(100), SkIntToScalar(100), |
| 81 | SkIntToScalar(75), SkIntToScalar(75)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 86 | static const SkAlpha kAlphaValue[] = { 0xFF, 0x40 }; |
| 87 | |
| 88 | enum { |
| 89 | kMargin = 5, |
| 90 | }; |
| 91 | int wrapX = canvas->getDeviceSize().fWidth - kMargin; |
| 92 | |
| 93 | SkScalar maxH = 0; |
| 94 | canvas->translate(SkIntToScalar(kMargin), SkIntToScalar(kMargin)); |
| 95 | canvas->save(); |
| 96 | |
| 97 | SkScalar x = SkIntToScalar(kMargin); |
| 98 | for (int p = 0; p < fPaths.count(); ++p) { |
| 99 | for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphaValue); ++a) { |
| 100 | for (int aa = 0; aa < 2; ++aa) { |
| 101 | const SkRect& bounds = fPaths[p].getBounds(); |
| 102 | |
| 103 | if (x + bounds.width() > wrapX) { |
| 104 | canvas->restore(); |
| 105 | canvas->translate(0, maxH + SkIntToScalar(kMargin)); |
| 106 | canvas->save(); |
| 107 | maxH = 0; |
| 108 | x = SkIntToScalar(kMargin); |
| 109 | } |
| 110 | |
| 111 | SkPaint paint; |
| 112 | paint.setARGB(kAlphaValue[a], 0, 0, 0); |
| 113 | paint.setAntiAlias(SkToBool(aa)); |
| 114 | paint.setStyle(SkPaint::kStroke_Style); |
| 115 | paint.setStrokeWidth(0); |
| 116 | |
| 117 | canvas->save(); |
| 118 | canvas->translate(-bounds.fLeft, -bounds.fTop); |
| 119 | canvas->drawPath(fPaths[p], paint); |
| 120 | canvas->restore(); |
| 121 | |
| 122 | maxH = SkMaxScalar(maxH, bounds.height()); |
| 123 | |
| 124 | SkScalar dx = bounds.width() + SkIntToScalar(kMargin); |
| 125 | x += dx; |
| 126 | canvas->translate(dx, 0); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | canvas->restore(); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | SkTArray<SkPath> fPaths; |
| 135 | typedef GM INHERITED; |
| 136 | }; |
| 137 | |
| 138 | ////////////////////////////////////////////////////////////////////////////// |
| 139 | |
| 140 | static GM* MyFactory(void*) { return new HairlinesGM; } |
| 141 | static GMRegistry reg(MyFactory); |
| 142 | |
| 143 | } |