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