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