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