reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 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 | */ |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 7 | |
| 8 | |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 9 | #include "gm.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkPath.h" |
| 12 | #include "SkRandom.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | class ComplexClip2GM : public GM { |
| 17 | public: |
| 18 | ComplexClip2GM() { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 19 | this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD)); |
| 20 | |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 21 | SkScalar xA = 0 * SK_Scalar1; |
| 22 | SkScalar xB = 10 * SK_Scalar1; |
| 23 | SkScalar xC = 20 * SK_Scalar1; |
| 24 | SkScalar xD = 30 * SK_Scalar1; |
| 25 | SkScalar xE = 40 * SK_Scalar1; |
| 26 | SkScalar xF = 50 * SK_Scalar1; |
| 27 | |
| 28 | SkScalar yA = 0 * SK_Scalar1; |
| 29 | SkScalar yB = 10 * SK_Scalar1; |
| 30 | SkScalar yC = 20 * SK_Scalar1; |
| 31 | SkScalar yD = 30 * SK_Scalar1; |
| 32 | SkScalar yE = 40 * SK_Scalar1; |
| 33 | SkScalar yF = 50 * SK_Scalar1; |
| 34 | |
| 35 | fWidth = xF - xA; |
| 36 | fHeight = yF - yA; |
| 37 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 38 | fRects[0].set(xB, yB, xE, yE); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 39 | fRectColors[0] = SK_ColorRED; |
| 40 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 41 | fRects[1].set(xA, yA, xD, yD); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 42 | fRectColors[1] = SK_ColorGREEN; |
| 43 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 44 | fRects[2].set(xC, yA, xF, yD); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 45 | fRectColors[2] = SK_ColorBLUE; |
| 46 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 47 | fRects[3].set(xA, yC, xD, yF); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 48 | fRectColors[3] = SK_ColorYELLOW; |
| 49 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 50 | fRects[4].set(xC, yC, xF, yF); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 51 | fRectColors[4] = SK_ColorCYAN; |
| 52 | |
| 53 | fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX; |
| 54 | fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY; |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 55 | |
| 56 | SkRegion::Op ops[] = { |
| 57 | SkRegion::kDifference_Op, |
| 58 | SkRegion::kIntersect_Op, |
| 59 | SkRegion::kUnion_Op, |
| 60 | SkRegion::kXOR_Op, |
| 61 | SkRegion::kReverseDifference_Op, |
| 62 | SkRegion::kReplace_Op, |
| 63 | }; |
| 64 | |
| 65 | SkRandom r; |
| 66 | for (int i = 0; i < kRows; ++i) { |
| 67 | for (int j = 0; j < kCols; ++j) { |
| 68 | for (int k = 0; k < 5; ++k) { |
| 69 | fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)]; |
| 70 | } |
| 71 | } |
| 72 | } |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 73 | } |
| 74 | |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 75 | protected: |
| 76 | |
| 77 | static const int kRows = 5; |
| 78 | static const int kCols = 5; |
| 79 | static const int kPadX = 20; |
| 80 | static const int kPadY = 20; |
| 81 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 82 | virtual SkString onShortName() { |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 83 | return SkString("complexclip2"); |
| 84 | } |
| 85 | |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 86 | virtual SkISize onISize() { |
| 87 | return make_isize(SkScalarRoundToInt(fTotalWidth), |
| 88 | SkScalarRoundToInt(fTotalHeight)); |
| 89 | } |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 90 | |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 91 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 92 | SkPaint rectPaint; |
| 93 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 94 | rectPaint.setStrokeWidth(-1); |
| 95 | |
| 96 | SkPaint fillPaint; |
| 97 | fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0)); |
| 98 | |
| 99 | for (int i = 0; i < kRows; ++i) { |
| 100 | for (int j = 0; j < kCols; ++j) { |
| 101 | canvas->save(); |
| 102 | canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j, |
| 103 | kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i); |
| 104 | canvas->save(); |
| 105 | for (int k = 0; k < 5; ++k) { |
| 106 | canvas->clipRect(fRects[k], fOps[j*kRows+i][k]); |
| 107 | } |
| 108 | canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint); |
| 109 | canvas->restore(); |
| 110 | for (int k = 0; k < 5; ++k) { |
| 111 | rectPaint.setColor(fRectColors[k]); |
| 112 | canvas->drawRect(fRects[k], rectPaint); |
| 113 | } |
| 114 | canvas->restore(); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | private: |
| 119 | SkRect fRects[5]; |
| 120 | SkColor fRectColors[5]; |
| 121 | SkRegion::Op fOps[kRows * kCols][5]; |
| 122 | SkScalar fWidth; |
| 123 | SkScalar fHeight; |
| 124 | SkScalar fTotalWidth; |
| 125 | SkScalar fTotalHeight; |
| 126 | |
| 127 | typedef GM INHERITED; |
| 128 | }; |
| 129 | |
| 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | static GM* MyFactory(void*) { return new ComplexClip2GM; } |
| 133 | static GMRegistry reg(MyFactory); |
| 134 | |
| 135 | } |