epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 7 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 12 | |
| 13 | namespace skiagm { |
| 14 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 15 | constexpr SkColor gPathColor = SK_ColorBLACK; |
| 16 | constexpr SkColor gClipAColor = SK_ColorBLUE; |
| 17 | constexpr SkColor gClipBColor = SK_ColorRED; |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 18 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 19 | class ComplexClipGM : public GM { |
| 20 | public: |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 21 | ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw) |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 22 | : fDoAAClip(aaclip) |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 23 | , fDoSaveLayer(saveLayer) |
| 24 | , fInvertDraw(invertDraw) { |
caryclark | ceb9f3b | 2015-06-12 10:00:11 -0700 | [diff] [blame] | 25 | this->setBGColor(0xFFDEDFDE); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 29 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 30 | |
| 31 | SkString onShortName() { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 32 | SkString str; |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 33 | str.printf("complexclip_%s%s%s", |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 34 | fDoAAClip ? "aa" : "bw", |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 35 | fDoSaveLayer ? "_layer" : "", |
| 36 | fInvertDraw ? "_invert" : ""); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 37 | return str; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 38 | } |
| 39 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 40 | SkISize onISize() { return SkISize::Make(970, 780); } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 42 | virtual void onDraw(SkCanvas* canvas) { |
| 43 | SkPath path; |
| 44 | path.moveTo(SkIntToScalar(0), SkIntToScalar(50)); |
| 45 | path.quadTo(SkIntToScalar(0), SkIntToScalar(0), SkIntToScalar(50), SkIntToScalar(0)); |
| 46 | path.lineTo(SkIntToScalar(175), SkIntToScalar(0)); |
| 47 | path.quadTo(SkIntToScalar(200), SkIntToScalar(0), SkIntToScalar(200), SkIntToScalar(25)); |
| 48 | path.lineTo(SkIntToScalar(200), SkIntToScalar(150)); |
| 49 | path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200)); |
| 50 | path.lineTo(SkIntToScalar(0), SkIntToScalar(200)); |
| 51 | path.close(); |
| 52 | path.moveTo(SkIntToScalar(50), SkIntToScalar(50)); |
| 53 | path.lineTo(SkIntToScalar(150), SkIntToScalar(50)); |
| 54 | path.lineTo(SkIntToScalar(150), SkIntToScalar(125)); |
| 55 | path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150)); |
| 56 | path.lineTo(SkIntToScalar(50), SkIntToScalar(150)); |
| 57 | path.close(); |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 58 | if (fInvertDraw) { |
| 59 | path.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 60 | } else { |
| 61 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 62 | } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 63 | SkPaint pathPaint; |
| 64 | pathPaint.setAntiAlias(true); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 65 | pathPaint.setColor(gPathColor); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 66 | |
| 67 | SkPath clipA; |
| 68 | clipA.moveTo(SkIntToScalar(10), SkIntToScalar(20)); |
| 69 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22)); |
| 70 | clipA.lineTo(SkIntToScalar(70), SkIntToScalar(105)); |
| 71 | clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177)); |
| 72 | clipA.lineTo(SkIntToScalar(-5), SkIntToScalar(180)); |
| 73 | clipA.close(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 74 | |
| 75 | SkPath clipB; |
| 76 | clipB.moveTo(SkIntToScalar(40), SkIntToScalar(10)); |
| 77 | clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15)); |
| 78 | clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190)); |
| 79 | clipB.lineTo(SkIntToScalar(40), SkIntToScalar(185)); |
| 80 | clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100)); |
| 81 | clipB.close(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 82 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 83 | SkPaint paint; |
| 84 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 85 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 86 | paint.setTextSize(SkIntToScalar(20)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 87 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 88 | constexpr struct { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 89 | SkClipOp fOp; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 90 | const char* fName; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 91 | } gOps[] = { //extra spaces in names for measureText |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 92 | {kIntersect_SkClipOp, "Isect "}, |
| 93 | {kDifference_SkClipOp, "Diff " }, |
| 94 | {kUnion_SkClipOp, "Union "}, |
| 95 | {kXOR_SkClipOp, "Xor " }, |
| 96 | {kReverseDifference_SkClipOp, "RDiff "} |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 99 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 100 | canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 101 | |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 102 | if (fDoSaveLayer) { |
| 103 | // We want the layer to appear symmetric relative to actual |
| 104 | // device boundaries so we need to "undo" the effect of the |
| 105 | // scale and translate |
| 106 | SkRect bounds = SkRect::MakeLTRB( |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 107 | 4.0f/3.0f * -20, |
| 108 | 4.0f/3.0f * -20, |
| 109 | 4.0f/3.0f * (this->getISize().fWidth - 20), |
| 110 | 4.0f/3.0f * (this->getISize().fHeight - 20)); |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 111 | |
| 112 | bounds.inset(SkIntToScalar(100), SkIntToScalar(100)); |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 113 | SkPaint boundPaint; |
| 114 | boundPaint.setColor(SK_ColorRED); |
| 115 | boundPaint.setStyle(SkPaint::kStroke_Style); |
| 116 | canvas->drawRect(bounds, boundPaint); |
Ben Wagner | 788a2dc | 2018-05-09 13:23:38 -0400 | [diff] [blame] | 117 | canvas->clipRect(bounds); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | canvas->saveLayer(&bounds, nullptr); |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 119 | } |
| 120 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 121 | for (int invBits = 0; invBits < 4; ++invBits) { |
| 122 | canvas->save(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 123 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 124 | this->drawHairlines(canvas, path, clipA, clipB); |
| 125 | |
| 126 | bool doInvA = SkToBool(invBits & 1); |
| 127 | bool doInvB = SkToBool(invBits & 2); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 128 | canvas->save(); |
| 129 | // set clip |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 130 | clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType : |
| 131 | SkPath::kEvenOdd_FillType); |
| 132 | clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType : |
| 133 | SkPath::kEvenOdd_FillType); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 134 | canvas->clipPath(clipA, fDoAAClip); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 135 | canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 136 | |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 137 | // In the inverse case we need to prevent the draw from covering the whole |
| 138 | // canvas. |
| 139 | if (fInvertDraw) { |
| 140 | SkRect rectClip = clipA.getBounds(); |
| 141 | rectClip.join(path.getBounds()); |
| 142 | rectClip.join(path.getBounds()); |
| 143 | rectClip.outset(5, 5); |
| 144 | canvas->clipRect(rectClip); |
| 145 | } |
| 146 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 147 | // draw path clipped |
| 148 | canvas->drawPath(path, pathPaint); |
| 149 | canvas->restore(); |
| 150 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 151 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 152 | SkScalar txtX = SkIntToScalar(45); |
| 153 | paint.setColor(gClipAColor); |
| 154 | const char* aTxt = doInvA ? "InvA " : "A "; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 155 | canvas->drawString(aTxt, txtX, SkIntToScalar(220), paint); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 156 | txtX += paint.measureText(aTxt, strlen(aTxt)); |
| 157 | paint.setColor(SK_ColorBLACK); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 158 | canvas->drawString(gOps[op].fName, txtX, SkIntToScalar(220), paint); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 159 | txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName)); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 160 | paint.setColor(gClipBColor); |
| 161 | const char* bTxt = doInvB ? "InvB " : "B "; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 162 | canvas->drawString(bTxt, txtX, SkIntToScalar(220), paint); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 163 | |
| 164 | canvas->translate(SkIntToScalar(250),0); |
| 165 | } |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 166 | canvas->restore(); |
| 167 | canvas->translate(0, SkIntToScalar(250)); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 168 | } |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 169 | |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 170 | if (fDoSaveLayer) { |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 171 | canvas->restore(); |
| 172 | } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 173 | } |
| 174 | private: |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 175 | void drawHairlines(SkCanvas* canvas, const SkPath& path, |
| 176 | const SkPath& clipA, const SkPath& clipB) { |
| 177 | SkPaint paint; |
| 178 | paint.setAntiAlias(true); |
| 179 | paint.setStyle(SkPaint::kStroke_Style); |
| 180 | const SkAlpha fade = 0x33; |
| 181 | |
| 182 | // draw path in hairline |
| 183 | paint.setColor(gPathColor); paint.setAlpha(fade); |
| 184 | canvas->drawPath(path, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 185 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 186 | // draw clips in hair line |
| 187 | paint.setColor(gClipAColor); paint.setAlpha(fade); |
| 188 | canvas->drawPath(clipA, paint); |
| 189 | paint.setColor(gClipBColor); paint.setAlpha(fade); |
| 190 | canvas->drawPath(clipB, paint); |
| 191 | } |
| 192 | |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 193 | bool fDoAAClip; |
| 194 | bool fDoSaveLayer; |
| 195 | bool fInvertDraw; |
| 196 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 197 | typedef GM INHERITED; |
| 198 | }; |
| 199 | |
| 200 | ////////////////////////////////////////////////////////////////////////////// |
| 201 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 202 | DEF_GM(return new ComplexClipGM(false, false, false);) |
| 203 | DEF_GM(return new ComplexClipGM(false, false, true);) |
| 204 | DEF_GM(return new ComplexClipGM(false, true, false);) |
| 205 | DEF_GM(return new ComplexClipGM(false, true, true);) |
| 206 | DEF_GM(return new ComplexClipGM(true, false, false);) |
| 207 | DEF_GM(return new ComplexClipGM(true, false, true);) |
| 208 | DEF_GM(return new ComplexClipGM(true, true, false);) |
| 209 | DEF_GM(return new ComplexClipGM(true, true, true);) |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 210 | } |