| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 | 8 |
| 9 #include "gm.h" | 9 #include "gm.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkPath.h" | 11 #include "SkPath.h" |
| 12 #include "SkRandom.h" | 12 #include "SkRandom.h" |
| 13 | 13 |
| 14 namespace skiagm { | 14 namespace skiagm { |
| 15 | 15 |
| 16 class ComplexClip2GM : public GM { | 16 class ComplexClip2GM : public GM { |
| 17 public: | 17 public: |
| 18 ComplexClip2GM(bool doPaths, bool antiAlias) | 18 enum Clip { |
| 19 : fDoPaths(doPaths) | 19 kRect_Clip, |
| 20 kRRect_Clip, |
| 21 kPath_Clip |
| 22 }; |
| 23 |
| 24 ComplexClip2GM(Clip clip, bool antiAlias) |
| 25 : fClip(clip) |
| 20 , fAntiAlias(antiAlias) { | 26 , fAntiAlias(antiAlias) { |
| 21 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD)); | 27 this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD)); |
| 22 | 28 |
| 23 // offset the rects a bit so we get antialiasing even in the rect case | 29 // offset the rects a bit so we get antialiasing even in the rect case |
| 24 SkScalar xA = 0.65f; | 30 SkScalar xA = 0.65f; |
| 25 SkScalar xB = 10.65f; | 31 SkScalar xB = 10.65f; |
| 26 SkScalar xC = 20.65f; | 32 SkScalar xC = 20.65f; |
| 27 SkScalar xD = 30.65f; | 33 SkScalar xD = 30.65f; |
| 28 SkScalar xE = 40.65f; | 34 SkScalar xE = 40.65f; |
| 29 SkScalar xF = 50.65f; | 35 SkScalar xF = 50.65f; |
| 30 | 36 |
| 31 SkScalar yA = 0.65f; | 37 SkScalar yA = 0.65f; |
| 32 SkScalar yB = 10.65f; | 38 SkScalar yB = 10.65f; |
| 33 SkScalar yC = 20.65f; | 39 SkScalar yC = 20.65f; |
| 34 SkScalar yD = 30.65f; | 40 SkScalar yD = 30.65f; |
| 35 SkScalar yE = 40.65f; | 41 SkScalar yE = 40.65f; |
| 36 SkScalar yF = 50.65f; | 42 SkScalar yF = 50.65f; |
| 37 | 43 |
| 38 fWidth = xF - xA; | 44 fWidth = xF - xA; |
| 39 fHeight = yF - yA; | 45 fHeight = yF - yA; |
| 40 | 46 |
| 41 fRects[0].set(xB, yB, xE, yE); | 47 fRects[0].set(xB, yB, xE, yE); |
| 42 fPaths[0].addRoundRect(fRects[0], SkIntToScalar(5), SkIntToScalar(5)); | 48 fRRects[0].setRectXY(fRects[0], 7, 7); |
| 49 fPaths[0].addRoundRect(fRects[0], 5, 5); |
| 43 fRectColors[0] = SK_ColorRED; | 50 fRectColors[0] = SK_ColorRED; |
| 44 | 51 |
| 45 fRects[1].set(xA, yA, xD, yD); | 52 fRects[1].set(xA, yA, xD, yD); |
| 46 fPaths[1].addRoundRect(fRects[1], SkIntToScalar(5), SkIntToScalar(5)); | 53 fRRects[1].setRectXY(fRects[1], 7, 7); |
| 54 fPaths[1].addRoundRect(fRects[1], 5, 5); |
| 47 fRectColors[1] = SK_ColorGREEN; | 55 fRectColors[1] = SK_ColorGREEN; |
| 48 | 56 |
| 49 fRects[2].set(xC, yA, xF, yD); | 57 fRects[2].set(xC, yA, xF, yD); |
| 50 fPaths[2].addRoundRect(fRects[2], SkIntToScalar(5), SkIntToScalar(5)); | 58 fRRects[2].setRectXY(fRects[2], 7, 7); |
| 59 fPaths[2].addRoundRect(fRects[2], 5, 5); |
| 51 fRectColors[2] = SK_ColorBLUE; | 60 fRectColors[2] = SK_ColorBLUE; |
| 52 | 61 |
| 53 fRects[3].set(xA, yC, xD, yF); | 62 fRects[3].set(xA, yC, xD, yF); |
| 54 fPaths[3].addRoundRect(fRects[3], SkIntToScalar(5), SkIntToScalar(5)); | 63 fRRects[3].setRectXY(fRects[3], 7, 7); |
| 64 fPaths[3].addRoundRect(fRects[3], 5, 5); |
| 55 fRectColors[3] = SK_ColorYELLOW; | 65 fRectColors[3] = SK_ColorYELLOW; |
| 56 | 66 |
| 57 fRects[4].set(xC, yC, xF, yF); | 67 fRects[4].set(xC, yC, xF, yF); |
| 58 fPaths[4].addRoundRect(fRects[4], SkIntToScalar(5), SkIntToScalar(5)); | 68 fRRects[4].setRectXY(fRects[4], 7, 7); |
| 69 fPaths[4].addRoundRect(fRects[4], 5, 5); |
| 59 fRectColors[4] = SK_ColorCYAN; | 70 fRectColors[4] = SK_ColorCYAN; |
| 60 | 71 |
| 61 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX; | 72 fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX; |
| 62 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY; | 73 fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY; |
| 63 | 74 |
| 64 SkRegion::Op ops[] = { | 75 SkRegion::Op ops[] = { |
| 65 SkRegion::kDifference_Op, | 76 SkRegion::kDifference_Op, |
| 66 SkRegion::kIntersect_Op, | 77 SkRegion::kIntersect_Op, |
| 67 SkRegion::kUnion_Op, | 78 SkRegion::kUnion_Op, |
| 68 SkRegion::kXOR_Op, | 79 SkRegion::kXOR_Op, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 80 } | 91 } |
| 81 } | 92 } |
| 82 | 93 |
| 83 protected: | 94 protected: |
| 84 | 95 |
| 85 static const int kRows = 5; | 96 static const int kRows = 5; |
| 86 static const int kCols = 5; | 97 static const int kCols = 5; |
| 87 static const int kPadX = 20; | 98 static const int kPadX = 20; |
| 88 static const int kPadY = 20; | 99 static const int kPadY = 20; |
| 89 | 100 |
| 101 static const char* ClipStr(Clip clip) { |
| 102 switch (clip) { |
| 103 case kRect_Clip: |
| 104 return "rect"; |
| 105 case kRRect_Clip: |
| 106 return "rrect"; |
| 107 case kPath_Clip: |
| 108 return "path"; |
| 109 } |
| 110 SkDEBUGFAIL("Unknown clip type."); |
| 111 return ""; |
| 112 } |
| 113 |
| 90 virtual SkString onShortName() { | 114 virtual SkString onShortName() { |
| 91 if (!fDoPaths && !fAntiAlias) { | 115 if (kRect_Clip == fClip && !fAntiAlias) { |
| 92 return SkString("complexclip2"); | 116 return SkString("complexclip2"); |
| 93 } | 117 } |
| 94 | 118 |
| 95 SkString str; | 119 SkString str; |
| 96 str.printf("complexclip2_%s_%s", | 120 str.printf("complexclip2_%s_%s", |
| 97 fDoPaths ? "path" : "rect", | 121 ClipStr(fClip), |
| 98 fAntiAlias ? "aa" : "bw"); | 122 fAntiAlias ? "aa" : "bw"); |
| 99 return str; | 123 return str; |
| 100 } | 124 } |
| 101 | 125 |
| 102 virtual SkISize onISize() { | 126 virtual SkISize onISize() { |
| 103 return make_isize(SkScalarRoundToInt(fTotalWidth), | 127 return make_isize(SkScalarRoundToInt(fTotalWidth), |
| 104 SkScalarRoundToInt(fTotalHeight)); | 128 SkScalarRoundToInt(fTotalHeight)); |
| 105 } | 129 } |
| 106 | 130 |
| 107 virtual void onDraw(SkCanvas* canvas) { | 131 virtual void onDraw(SkCanvas* canvas) { |
| 108 SkPaint rectPaint; | 132 SkPaint rectPaint; |
| 109 rectPaint.setStyle(SkPaint::kStroke_Style); | 133 rectPaint.setStyle(SkPaint::kStroke_Style); |
| 110 rectPaint.setStrokeWidth(-1); | 134 rectPaint.setStrokeWidth(-1); |
| 111 | 135 |
| 112 SkPaint fillPaint; | 136 SkPaint fillPaint; |
| 113 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0)); | 137 fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0)); |
| 114 | 138 |
| 115 for (int i = 0; i < kRows; ++i) { | 139 for (int i = 0; i < kRows; ++i) { |
| 116 for (int j = 0; j < kCols; ++j) { | 140 for (int j = 0; j < kCols; ++j) { |
| 117 canvas->save(); | 141 canvas->save(); |
| 118 | 142 |
| 119 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scal
ar1)*j, | 143 canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scal
ar1)*j, |
| 120 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Sca
lar1)*i); | 144 kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Sca
lar1)*i); |
| 121 | 145 |
| 122 // draw the original shapes first so we can see the | 146 // draw the original shapes first so we can see the |
| 123 // antialiasing on the clipped draw | 147 // antialiasing on the clipped draw |
| 124 for (int k = 0; k < 5; ++k) { | 148 for (int k = 0; k < 5; ++k) { |
| 125 rectPaint.setColor(fRectColors[k]); | 149 rectPaint.setColor(fRectColors[k]); |
| 126 if (fDoPaths) { | 150 switch (fClip) { |
| 127 canvas->drawPath(fPaths[k], rectPaint); | 151 case kRect_Clip: |
| 128 } else { | 152 canvas->drawRect(fRects[k], rectPaint); |
| 129 canvas->drawRect(fRects[k], rectPaint); | 153 break; |
| 154 case kRRect_Clip: |
| 155 canvas->drawRRect(fRRects[k], rectPaint); |
| 156 break; |
| 157 case kPath_Clip: |
| 158 canvas->drawPath(fPaths[k], rectPaint); |
| 159 break; |
| 130 } | 160 } |
| 131 } | 161 } |
| 132 | 162 |
| 133 for (int k = 0; k < 5; ++k) { | 163 for (int k = 0; k < 5; ++k) { |
| 134 if (fDoPaths) { | 164 switch (fClip) { |
| 135 canvas->clipPath(fPaths[k], | 165 case kRect_Clip: |
| 136 fOps[j*kRows+i][k], | 166 canvas->clipRect(fRects[k], |
| 137 fAntiAlias); | 167 fOps[j*kRows+i][k], |
| 138 } else { | 168 fAntiAlias); |
| 139 canvas->clipRect(fRects[k], | 169 break; |
| 140 fOps[j*kRows+i][k], | 170 case kRRect_Clip: |
| 141 fAntiAlias); | 171 canvas->clipRRect(fRRects[k], |
| 172 fOps[j*kRows+i][k], |
| 173 fAntiAlias); |
| 174 break; |
| 175 case kPath_Clip: |
| 176 canvas->clipPath(fPaths[k], |
| 177 fOps[j*kRows+i][k], |
| 178 fAntiAlias); |
| 179 break; |
| 142 } | 180 } |
| 143 } | 181 } |
| 144 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint); | 182 canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint); |
| 145 canvas->restore(); | 183 canvas->restore(); |
| 146 } | 184 } |
| 147 } | 185 } |
| 148 } | 186 } |
| 149 private: | 187 private: |
| 150 bool fDoPaths; | 188 Clip fClip; |
| 151 bool fAntiAlias; | 189 bool fAntiAlias; |
| 152 SkRect fRects[5]; | 190 SkRect fRects[5]; |
| 191 SkRRect fRRects[5]; |
| 153 SkPath fPaths[5]; | 192 SkPath fPaths[5]; |
| 154 SkColor fRectColors[5]; | 193 SkColor fRectColors[5]; |
| 155 SkRegion::Op fOps[kRows * kCols][5]; | 194 SkRegion::Op fOps[kRows * kCols][5]; |
| 156 SkScalar fWidth; | 195 SkScalar fWidth; |
| 157 SkScalar fHeight; | 196 SkScalar fHeight; |
| 158 SkScalar fTotalWidth; | 197 SkScalar fTotalWidth; |
| 159 SkScalar fTotalHeight; | 198 SkScalar fTotalHeight; |
| 160 | 199 |
| 161 typedef GM INHERITED; | 200 typedef GM INHERITED; |
| 162 }; | 201 }; |
| 163 | 202 |
| 164 ////////////////////////////////////////////////////////////////////////////// | 203 ////////////////////////////////////////////////////////////////////////////// |
| 165 | 204 |
| 166 // bw rects | 205 // bw |
| 167 static GM* MyFactory(void*) { return new ComplexClip2GM(false, false); } | 206 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); ) |
| 168 static GMRegistry reg(MyFactory); | 207 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); ) |
| 208 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); ) |
| 169 | 209 |
| 170 // bw paths | 210 // aa |
| 171 static GM* MyFactory2(void*) { return new ComplexClip2GM(true, false); } | 211 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); ) |
| 172 static GMRegistry reg2(MyFactory2); | 212 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); ) |
| 173 | 213 DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); ) |
| 174 // aa rects | |
| 175 static GM* MyFactory3(void*) { return new ComplexClip2GM(false, true); } | |
| 176 static GMRegistry reg3(MyFactory3); | |
| 177 | |
| 178 // aa paths | |
| 179 static GM* MyFactory4(void*) { return new ComplexClip2GM(true, true); } | |
| 180 static GMRegistry reg4(MyFactory4); | |
| 181 | 214 |
| 182 } | 215 } |
| OLD | NEW |