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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 13 | #include "SkRect.h" |
| 14 | #include "SkRRect.h" |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 15 | |
| 16 | namespace skiagm { |
| 17 | |
| 18 | class ComplexClip2GM : public GM { |
| 19 | public: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 20 | enum Clip { |
| 21 | kRect_Clip, |
| 22 | kRRect_Clip, |
| 23 | kPath_Clip |
| 24 | }; |
| 25 | |
| 26 | ComplexClip2GM(Clip clip, bool antiAlias) |
| 27 | : fClip(clip) |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 28 | , fAntiAlias(antiAlias) { |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 29 | SkScalar xA = 0.65f; |
| 30 | SkScalar xF = 50.65f; |
| 31 | |
| 32 | SkScalar yA = 0.65f; |
| 33 | SkScalar yF = 50.65f; |
| 34 | |
| 35 | fWidth = xF - xA; |
| 36 | fHeight = yF - yA; |
| 37 | |
| 38 | fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX; |
| 39 | fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY; |
| 40 | } |
| 41 | |
| 42 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | void onOnceBeforeDraw() override { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 44 | this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 45 | |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 46 | // offset the rects a bit so we get antialiasing even in the rect case |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 47 | SkScalar xA = 0.65f; |
| 48 | SkScalar xB = 10.65f; |
| 49 | SkScalar xC = 20.65f; |
| 50 | SkScalar xD = 30.65f; |
| 51 | SkScalar xE = 40.65f; |
| 52 | SkScalar xF = 50.65f; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 54 | SkScalar yA = 0.65f; |
| 55 | SkScalar yB = 10.65f; |
| 56 | SkScalar yC = 20.65f; |
| 57 | SkScalar yD = 30.65f; |
| 58 | SkScalar yE = 40.65f; |
| 59 | SkScalar yF = 50.65f; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 61 | fRects[0].set(xB, yB, xE, yE); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 62 | fRRects[0].setRectXY(fRects[0], 7, 7); |
| 63 | fPaths[0].addRoundRect(fRects[0], 5, 5); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 64 | fRectColors[0] = SK_ColorRED; |
| 65 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 66 | fRects[1].set(xA, yA, xD, yD); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 67 | fRRects[1].setRectXY(fRects[1], 7, 7); |
| 68 | fPaths[1].addRoundRect(fRects[1], 5, 5); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 69 | fRectColors[1] = SK_ColorGREEN; |
| 70 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 71 | fRects[2].set(xC, yA, xF, yD); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 72 | fRRects[2].setRectXY(fRects[2], 7, 7); |
| 73 | fPaths[2].addRoundRect(fRects[2], 5, 5); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 74 | fRectColors[2] = SK_ColorBLUE; |
| 75 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 76 | fRects[3].set(xA, yC, xD, yF); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 77 | fRRects[3].setRectXY(fRects[3], 7, 7); |
| 78 | fPaths[3].addRoundRect(fRects[3], 5, 5); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 79 | fRectColors[3] = SK_ColorYELLOW; |
| 80 | |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 81 | fRects[4].set(xC, yC, xF, yF); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 82 | fRRects[4].setRectXY(fRects[4], 7, 7); |
| 83 | fPaths[4].addRoundRect(fRects[4], 5, 5); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 84 | fRectColors[4] = SK_ColorCYAN; |
| 85 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 86 | const SkClipOp ops[] = { |
| 87 | kDifference_SkClipOp, |
| 88 | kIntersect_SkClipOp, |
| 89 | kUnion_SkClipOp, |
| 90 | kXOR_SkClipOp, |
| 91 | kReverseDifference_SkClipOp, |
| 92 | kReplace_SkClipOp, |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 95 | SkRandom r; |
bsalomon@google.com | 574f29d | 2011-08-29 18:05:05 +0000 | [diff] [blame] | 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 | } |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 103 | } |
| 104 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 105 | static constexpr int kRows = 5; |
| 106 | static constexpr int kCols = 5; |
| 107 | static constexpr int kPadX = 20; |
| 108 | static constexpr int kPadY = 20; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 109 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 110 | static const char* ClipStr(Clip clip) { |
| 111 | switch (clip) { |
| 112 | case kRect_Clip: |
| 113 | return "rect"; |
| 114 | case kRRect_Clip: |
| 115 | return "rrect"; |
| 116 | case kPath_Clip: |
| 117 | return "path"; |
| 118 | } |
| 119 | SkDEBUGFAIL("Unknown clip type."); |
| 120 | return ""; |
| 121 | } |
| 122 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 123 | SkString onShortName() override { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 124 | if (kRect_Clip == fClip && !fAntiAlias) { |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 125 | return SkString("complexclip2"); |
| 126 | } |
| 127 | |
| 128 | SkString str; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 129 | str.printf("complexclip2_%s_%s", |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 130 | ClipStr(fClip), |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 131 | fAntiAlias ? "aa" : "bw"); |
| 132 | return str; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 133 | } |
| 134 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 135 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 136 | return SkISize::Make(SkScalarRoundToInt(fTotalWidth), |
| 137 | SkScalarRoundToInt(fTotalHeight)); |
reed@google.com | 45482d1 | 2011-08-29 19:02:39 +0000 | [diff] [blame] | 138 | } |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 139 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 140 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 141 | SkPaint rectPaint; |
| 142 | rectPaint.setStyle(SkPaint::kStroke_Style); |
| 143 | rectPaint.setStrokeWidth(-1); |
| 144 | |
| 145 | SkPaint fillPaint; |
| 146 | fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0)); |
| 147 | |
| 148 | for (int i = 0; i < kRows; ++i) { |
| 149 | for (int j = 0; j < kCols; ++j) { |
| 150 | canvas->save(); |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 151 | |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 152 | canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j, |
| 153 | kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i); |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 154 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 155 | // draw the original shapes first so we can see the |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 156 | // antialiasing on the clipped draw |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 157 | for (int k = 0; k < 5; ++k) { |
| 158 | rectPaint.setColor(fRectColors[k]); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 159 | switch (fClip) { |
| 160 | case kRect_Clip: |
| 161 | canvas->drawRect(fRects[k], rectPaint); |
| 162 | break; |
| 163 | case kRRect_Clip: |
| 164 | canvas->drawRRect(fRRects[k], rectPaint); |
| 165 | break; |
| 166 | case kPath_Clip: |
| 167 | canvas->drawPath(fPaths[k], rectPaint); |
| 168 | break; |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 169 | } |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 170 | } |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 171 | |
| 172 | for (int k = 0; k < 5; ++k) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 173 | switch (fClip) { |
| 174 | case kRect_Clip: |
| 175 | canvas->clipRect(fRects[k], |
| 176 | fOps[j*kRows+i][k], |
| 177 | fAntiAlias); |
| 178 | break; |
| 179 | case kRRect_Clip: |
| 180 | canvas->clipRRect(fRRects[k], |
| 181 | fOps[j*kRows+i][k], |
| 182 | fAntiAlias); |
| 183 | break; |
| 184 | case kPath_Clip: |
| 185 | canvas->clipPath(fPaths[k], |
| 186 | fOps[j*kRows+i][k], |
| 187 | fAntiAlias); |
| 188 | break; |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint); |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 192 | canvas->restore(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | private: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 197 | Clip fClip; |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 198 | bool fAntiAlias; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 199 | SkRect fRects[5]; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 200 | SkRRect fRRects[5]; |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 201 | SkPath fPaths[5]; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 202 | SkColor fRectColors[5]; |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 203 | SkClipOp fOps[kRows * kCols][5]; |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 204 | SkScalar fWidth; |
| 205 | SkScalar fHeight; |
| 206 | SkScalar fTotalWidth; |
| 207 | SkScalar fTotalHeight; |
| 208 | |
| 209 | typedef GM INHERITED; |
| 210 | }; |
| 211 | |
| 212 | ////////////////////////////////////////////////////////////////////////////// |
| 213 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 214 | // bw |
| 215 | DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); ) |
| 216 | DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); ) |
| 217 | DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); ) |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 218 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 219 | // aa |
| 220 | DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); ) |
| 221 | DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); ) |
| 222 | DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); ) |
robertphillips@google.com | 2470b25 | 2012-05-15 17:03:16 +0000 | [diff] [blame] | 223 | |
reed@google.com | 30db599 | 2011-08-29 17:41:02 +0000 | [diff] [blame] | 224 | } |