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