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" |
Michael Ludwig | 4ce7786 | 2020-10-27 18:07:29 -0400 | [diff] [blame] | 12 | #include "include/core/SkColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkFont.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkFontTypes.h" |
| 15 | #include "include/core/SkPaint.h" |
Mike Reed | d849a75 | 2020-09-08 20:47:09 -0400 | [diff] [blame] | 16 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTypeface.h" |
| 22 | #include "include/core/SkTypes.h" |
Michael Ludwig | 4920384 | 2020-06-02 17:27:07 -0400 | [diff] [blame] | 23 | #include "include/effects/SkGradientShader.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "src/core/SkClipOpPriv.h" |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 25 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "tools/ToolUtils.h" |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 27 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 28 | #include <string.h> |
| 29 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 30 | namespace skiagm { |
| 31 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 32 | constexpr SkColor gPathColor = SK_ColorBLACK; |
| 33 | constexpr SkColor gClipAColor = SK_ColorBLUE; |
| 34 | constexpr SkColor gClipBColor = SK_ColorRED; |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 35 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 36 | class ComplexClipGM : public GM { |
| 37 | public: |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 38 | ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw) |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 39 | : fDoAAClip(aaclip) |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 40 | , fDoSaveLayer(saveLayer) |
| 41 | , fInvertDraw(invertDraw) { |
caryclark | ceb9f3b | 2015-06-12 10:00:11 -0700 | [diff] [blame] | 42 | this->setBGColor(0xFFDEDFDE); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | protected: |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 46 | SkString onShortName() override { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 47 | SkString str; |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 48 | str.printf("complexclip_%s%s%s", |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 49 | fDoAAClip ? "aa" : "bw", |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 50 | fDoSaveLayer ? "_layer" : "", |
| 51 | fInvertDraw ? "_invert" : ""); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 52 | return str; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Michael Ludwig | 9689e39 | 2020-05-05 10:56:42 -0400 | [diff] [blame] | 55 | SkISize onISize() override { return SkISize::Make(388, 780); } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 56 | |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 57 | void onDraw(SkCanvas* canvas) override { |
Mike Reed | d849a75 | 2020-09-08 20:47:09 -0400 | [diff] [blame] | 58 | SkPath path = SkPathBuilder() |
| 59 | .moveTo(0, 50) |
| 60 | .quadTo(0, 0, 50, 0) |
| 61 | .lineTo(175, 0) |
| 62 | .quadTo(200, 0, 200, 25) |
| 63 | .lineTo(200, 150) |
| 64 | .quadTo(200, 200, 150, 200) |
| 65 | .lineTo(0, 200) |
| 66 | .close() |
| 67 | .moveTo(50, 50) |
| 68 | .lineTo(150, 50) |
| 69 | .lineTo(150, 125) |
| 70 | .quadTo(150, 150, 125, 150) |
| 71 | .lineTo(50, 150) |
| 72 | .close() |
| 73 | .detach(); |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 74 | if (fInvertDraw) { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 75 | path.setFillType(SkPathFillType::kInverseEvenOdd); |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 76 | } else { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 77 | path.setFillType(SkPathFillType::kEvenOdd); |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 78 | } |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 79 | SkPaint pathPaint; |
| 80 | pathPaint.setAntiAlias(true); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 81 | pathPaint.setColor(gPathColor); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 82 | |
Mike Reed | d849a75 | 2020-09-08 20:47:09 -0400 | [diff] [blame] | 83 | SkPath clipA = SkPath::Polygon({{10, 20}, {165, 22}, {70, 105}, {165, 177}, {-5, 180}}, true); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 84 | |
Mike Reed | d849a75 | 2020-09-08 20:47:09 -0400 | [diff] [blame] | 85 | SkPath clipB = SkPath::Polygon({{40, 10}, {190, 15}, {195, 190}, {40, 185}, {155, 100}}, true); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 86 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 87 | SkFont font(ToolUtils::create_portable_typeface(), 20); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 88 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 89 | constexpr struct { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 90 | SkClipOp fOp; |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 91 | const char* fName; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 92 | } gOps[] = { //extra spaces in names for measureText |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 93 | {kIntersect_SkClipOp, "Isect "}, |
| 94 | {kDifference_SkClipOp, "Diff " }, |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 97 | canvas->translate(20, 20); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 98 | canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 99 | |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 100 | if (fDoSaveLayer) { |
| 101 | // We want the layer to appear symmetric relative to actual |
| 102 | // device boundaries so we need to "undo" the effect of the |
| 103 | // scale and translate |
| 104 | SkRect bounds = SkRect::MakeLTRB( |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 105 | 4.0f/3.0f * -20, |
| 106 | 4.0f/3.0f * -20, |
| 107 | 4.0f/3.0f * (this->getISize().fWidth - 20), |
| 108 | 4.0f/3.0f * (this->getISize().fHeight - 20)); |
robertphillips@google.com | 54bb7ab | 2012-07-13 14:55:25 +0000 | [diff] [blame] | 109 | |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 110 | bounds.inset(100, 100); |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 111 | SkPaint boundPaint; |
| 112 | boundPaint.setColor(SK_ColorRED); |
| 113 | boundPaint.setStyle(SkPaint::kStroke_Style); |
| 114 | canvas->drawRect(bounds, boundPaint); |
Ben Wagner | 788a2dc | 2018-05-09 13:23:38 -0400 | [diff] [blame] | 115 | canvas->clipRect(bounds); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 116 | canvas->saveLayer(&bounds, nullptr); |
robertphillips@google.com | 50a69a0 | 2012-07-12 13:48:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 119 | for (int invBits = 0; invBits < 4; ++invBits) { |
| 120 | canvas->save(); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 121 | for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) { |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 122 | this->drawHairlines(canvas, path, clipA, clipB); |
| 123 | |
| 124 | bool doInvA = SkToBool(invBits & 1); |
| 125 | bool doInvB = SkToBool(invBits & 2); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 126 | canvas->save(); |
| 127 | // set clip |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 128 | clipA.setFillType(doInvA ? SkPathFillType::kInverseEvenOdd : |
| 129 | SkPathFillType::kEvenOdd); |
| 130 | clipB.setFillType(doInvB ? SkPathFillType::kInverseEvenOdd : |
| 131 | SkPathFillType::kEvenOdd); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 132 | canvas->clipPath(clipA, fDoAAClip); |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 133 | canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 134 | |
bsalomon | 6ae83cf | 2014-12-17 14:38:49 -0800 | [diff] [blame] | 135 | // In the inverse case we need to prevent the draw from covering the whole |
| 136 | // canvas. |
| 137 | if (fInvertDraw) { |
| 138 | SkRect rectClip = clipA.getBounds(); |
| 139 | rectClip.join(path.getBounds()); |
| 140 | rectClip.join(path.getBounds()); |
| 141 | rectClip.outset(5, 5); |
| 142 | canvas->clipRect(rectClip); |
| 143 | } |
| 144 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 145 | // draw path clipped |
| 146 | canvas->drawPath(path, pathPaint); |
| 147 | canvas->restore(); |
| 148 | |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 149 | |
Mike Reed | 2e6db18 | 2018-12-15 13:45:33 -0500 | [diff] [blame] | 150 | SkPaint paint; |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 151 | SkScalar txtX = 45; |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 152 | paint.setColor(gClipAColor); |
| 153 | const char* aTxt = doInvA ? "InvA " : "A "; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 154 | canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint); |
| 155 | txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 156 | paint.setColor(SK_ColorBLACK); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 157 | 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] | 158 | font, paint); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 159 | 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] | 160 | paint.setColor(gClipBColor); |
| 161 | const char* bTxt = doInvB ? "InvB " : "B "; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 162 | canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 163 | |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 164 | canvas->translate(250,0); |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 165 | } |
reed@google.com | a8f60f2 | 2011-12-08 16:18:29 +0000 | [diff] [blame] | 166 | canvas->restore(); |
Mike Reed | bc414ed | 2018-08-16 22:49:55 -0400 | [diff] [blame] | 167 | canvas->translate(0, 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 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 197 | using INHERITED = GM; |
bsalomon@google.com | 807cec4 | 2011-03-10 19:20:15 +0000 | [diff] [blame] | 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);) |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 210 | } // namespace skiagm |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 211 | |
| 212 | DEF_SIMPLE_GM(clip_shader, canvas, 840, 650) { |
| 213 | auto img = GetResourceAsImage("images/yellow_rose.png"); |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 214 | auto sh = img->makeShader(SkSamplingOptions()); |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 215 | |
| 216 | SkRect r = SkRect::MakeIWH(img->width(), img->height()); |
| 217 | SkPaint p; |
| 218 | |
| 219 | canvas->translate(10, 10); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 220 | canvas->drawImage(img, 0, 0); |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 221 | |
| 222 | canvas->save(); |
| 223 | canvas->translate(img->width() + 10, 0); |
| 224 | canvas->clipShader(sh, SkClipOp::kIntersect); |
| 225 | p.setColor(SK_ColorRED); |
| 226 | canvas->drawRect(r, p); |
| 227 | canvas->restore(); |
| 228 | |
| 229 | canvas->save(); |
| 230 | canvas->translate(0, img->height() + 10); |
| 231 | canvas->clipShader(sh, SkClipOp::kDifference); |
| 232 | p.setColor(SK_ColorGREEN); |
| 233 | canvas->drawRect(r, p); |
| 234 | canvas->restore(); |
| 235 | |
| 236 | canvas->save(); |
| 237 | canvas->translate(img->width() + 10, img->height() + 10); |
| 238 | canvas->clipShader(sh, SkClipOp::kIntersect); |
| 239 | canvas->save(); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 240 | SkMatrix lm = SkMatrix::Scale(1.0f/5, 1.0f/5); |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 241 | canvas->clipShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 242 | SkSamplingOptions(), lm)); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 243 | canvas->drawImage(img, 0, 0); |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 244 | |
| 245 | canvas->restore(); |
| 246 | canvas->restore(); |
| 247 | } |
Mike Reed | 84a9eb5 | 2020-03-12 13:55:44 -0400 | [diff] [blame] | 248 | |
| 249 | DEF_SIMPLE_GM(clip_shader_layer, canvas, 430, 320) { |
| 250 | auto img = GetResourceAsImage("images/yellow_rose.png"); |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 251 | auto sh = img->makeShader(SkSamplingOptions()); |
Mike Reed | 84a9eb5 | 2020-03-12 13:55:44 -0400 | [diff] [blame] | 252 | |
| 253 | SkRect r = SkRect::MakeIWH(img->width(), img->height()); |
| 254 | |
| 255 | canvas->translate(10, 10); |
| 256 | // now add the cool clip |
| 257 | canvas->clipRect(r); |
| 258 | canvas->clipShader(sh); |
| 259 | // now draw a layer with the same image, and watch it get restored w/ the clip |
| 260 | canvas->saveLayer(&r, nullptr); |
| 261 | canvas->drawColor(0xFFFF0000); |
| 262 | canvas->restore(); |
| 263 | } |
Michael Ludwig | 4920384 | 2020-06-02 17:27:07 -0400 | [diff] [blame] | 264 | |
| 265 | DEF_SIMPLE_GM(clip_shader_nested, canvas, 256, 256) { |
| 266 | float w = 64.f; |
| 267 | float h = 64.f; |
| 268 | |
| 269 | const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)}; |
| 270 | auto s = SkGradientShader::MakeRadial({0.5f * w, 0.5f * h}, 0.1f * w, gradColors, nullptr, |
| 271 | 2, SkTileMode::kRepeat, 0, nullptr); |
| 272 | |
| 273 | SkPaint p; |
| 274 | |
| 275 | // A large black rect affected by two gradient clips |
| 276 | canvas->save(); |
| 277 | canvas->clipShader(s); |
| 278 | canvas->scale(2.f, 2.f); |
| 279 | canvas->clipShader(s); |
| 280 | canvas->drawRect(SkRect::MakeWH(w, h), p); |
| 281 | canvas->restore(); |
| 282 | |
| 283 | canvas->translate(0.f, 2.f * h); |
| 284 | |
| 285 | // A small red rect, with no clipping |
| 286 | canvas->save(); |
| 287 | p.setColor(SK_ColorRED); |
| 288 | canvas->drawRect(SkRect::MakeWH(w, h), p); |
| 289 | canvas->restore(); |
| 290 | } |
Michael Ludwig | 88b3b15 | 2020-06-03 10:22:49 -0400 | [diff] [blame] | 291 | |
| 292 | namespace { |
| 293 | |
| 294 | // Where is canvas->concat(persp) called relative to the clipShader calls. |
| 295 | enum ConcatPerspective { |
| 296 | kConcatBeforeClips, |
| 297 | kConcatAfterClips, |
| 298 | kConcatBetweenClips |
| 299 | }; |
| 300 | // Order in which clipShader(image) and clipShader(gradient) are specified; only meaningful |
| 301 | // when CanvasPerspective is kConcatBetweenClips. |
| 302 | enum ClipOrder { |
| 303 | kClipImageFirst, |
| 304 | kClipGradientFirst, |
| 305 | |
| 306 | kDoesntMatter = kClipImageFirst |
| 307 | }; |
| 308 | // Which shaders have perspective applied as a local matrix. |
| 309 | enum LocalMatrix { |
| 310 | kNoLocalMat, |
| 311 | kImageWithLocalMat, |
| 312 | kGradientWithLocalMat, |
| 313 | kBothWithLocalMat |
| 314 | }; |
| 315 | struct Config { |
| 316 | ConcatPerspective fConcat; |
| 317 | ClipOrder fOrder; |
| 318 | LocalMatrix fLM; |
| 319 | }; |
| 320 | |
| 321 | static void draw_banner(SkCanvas* canvas, Config config) { |
| 322 | SkString banner; |
| 323 | banner.append("Persp: "); |
| 324 | |
| 325 | if (config.fConcat == kConcatBeforeClips || config.fLM == kBothWithLocalMat) { |
| 326 | banner.append("Both Clips"); |
| 327 | } else { |
| 328 | SkASSERT((config.fConcat == kConcatBetweenClips && config.fLM == kNoLocalMat) || |
| 329 | (config.fConcat == kConcatAfterClips && (config.fLM == kImageWithLocalMat || |
| 330 | config.fLM == kGradientWithLocalMat))); |
| 331 | if ((config.fConcat == kConcatBetweenClips && config.fOrder == kClipImageFirst) || |
| 332 | config.fLM == kGradientWithLocalMat) { |
| 333 | banner.append("Gradient"); |
| 334 | } else { |
| 335 | SkASSERT(config.fOrder == kClipGradientFirst || config.fLM == kImageWithLocalMat); |
| 336 | banner.append("Image"); |
| 337 | } |
| 338 | } |
| 339 | if (config.fLM != kNoLocalMat) { |
| 340 | banner.append(" (w/ LM, should equal top row)"); |
| 341 | } |
| 342 | |
| 343 | static const SkFont kFont(ToolUtils::create_portable_typeface(), 12); |
| 344 | canvas->drawString(banner.c_str(), 20.f, -30.f, kFont, SkPaint()); |
| 345 | }; |
| 346 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 347 | } // namespace |
Michael Ludwig | 88b3b15 | 2020-06-03 10:22:49 -0400 | [diff] [blame] | 348 | |
| 349 | DEF_SIMPLE_GM(clip_shader_persp, canvas, 1370, 1030) { |
| 350 | // Each draw has a clipShader(image-shader), a clipShader(gradient-shader), a concat(persp-mat), |
| 351 | // and each shader may or may not be wrapped with a perspective local matrix. |
| 352 | |
| 353 | // Pairs of configs that should match in appearance where first config doesn't use a local |
| 354 | // matrix (top row of GM) and the second does (bottom row of GM). |
| 355 | Config matches[][2] = { |
| 356 | // Everything has perspective |
| 357 | {{kConcatBeforeClips, kDoesntMatter, kNoLocalMat}, |
| 358 | {kConcatAfterClips, kDoesntMatter, kBothWithLocalMat}}, |
| 359 | // Image shader has perspective |
| 360 | {{kConcatBetweenClips, kClipGradientFirst, kNoLocalMat}, |
| 361 | {kConcatAfterClips, kDoesntMatter, kImageWithLocalMat}}, |
| 362 | // Gradient shader has perspective |
| 363 | {{kConcatBetweenClips, kClipImageFirst, kNoLocalMat}, |
| 364 | {kConcatAfterClips, kDoesntMatter, kGradientWithLocalMat}} |
| 365 | }; |
| 366 | |
| 367 | // The image that is drawn |
| 368 | auto img = GetResourceAsImage("images/yellow_rose.png"); |
| 369 | // Scale factor always applied to the image shader so that it tiles |
| 370 | SkMatrix scale = SkMatrix::Scale(1.f / 4.f, 1.f / 4.f); |
| 371 | // The perspective matrix applied wherever needed |
| 372 | SkPoint src[4]; |
| 373 | SkRect::Make(img->dimensions()).toQuad(src); |
| 374 | SkPoint dst[4] = {{0, 80.f}, |
| 375 | {img->width() + 28.f, -100.f}, |
| 376 | {img->width() - 28.f, img->height() + 100.f}, |
| 377 | {0.f, img->height() - 80.f}}; |
| 378 | SkMatrix persp; |
| 379 | SkAssertResult(persp.setPolyToPoly(src, dst, 4)); |
| 380 | |
| 381 | SkMatrix perspScale = SkMatrix::Concat(persp, scale); |
| 382 | |
| 383 | auto drawConfig = [&](Config config) { |
| 384 | canvas->save(); |
| 385 | |
| 386 | draw_banner(canvas, config); |
| 387 | |
| 388 | // Make clipShaders (possibly with local matrices) |
| 389 | bool gradLM = config.fLM == kGradientWithLocalMat || config.fLM == kBothWithLocalMat; |
| 390 | const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)}; |
| 391 | auto gradShader = SkGradientShader::MakeRadial({0.5f * img->width(), 0.5f * img->height()}, |
| 392 | 0.1f * img->width(), gradColors, nullptr, 2, |
| 393 | SkTileMode::kRepeat, 0, |
| 394 | gradLM ? &persp : nullptr); |
| 395 | bool imageLM = config.fLM == kImageWithLocalMat || config.fLM == kBothWithLocalMat; |
| 396 | auto imgShader = img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
Mike Reed | b612b6c | 2020-12-08 21:58:35 -0500 | [diff] [blame] | 397 | SkSamplingOptions(), imageLM ? perspScale : scale); |
Michael Ludwig | 88b3b15 | 2020-06-03 10:22:49 -0400 | [diff] [blame] | 398 | |
| 399 | // Perspective before any clipShader |
| 400 | if (config.fConcat == kConcatBeforeClips) { |
| 401 | canvas->concat(persp); |
| 402 | } |
| 403 | |
| 404 | // First clipshader |
| 405 | canvas->clipShader(config.fOrder == kClipImageFirst ? imgShader : gradShader); |
| 406 | |
| 407 | // Perspective between clipShader |
| 408 | if (config.fConcat == kConcatBetweenClips) { |
| 409 | canvas->concat(persp); |
| 410 | } |
| 411 | |
| 412 | // Second clipShader |
| 413 | canvas->clipShader(config.fOrder == kClipImageFirst ? gradShader : imgShader); |
| 414 | |
| 415 | // Perspective after clipShader |
| 416 | if (config.fConcat == kConcatAfterClips) { |
| 417 | canvas->concat(persp); |
| 418 | } |
| 419 | |
| 420 | // Actual draw and clip boundary are the same for all configs |
Mike Reed | 568f0ae | 2021-01-24 08:57:23 -0500 | [diff] [blame^] | 421 | canvas->clipIRect(img->bounds()); |
Michael Ludwig | 88b3b15 | 2020-06-03 10:22:49 -0400 | [diff] [blame] | 422 | canvas->clear(SK_ColorBLACK); |
| 423 | canvas->drawImage(img, 0, 0); |
| 424 | |
| 425 | canvas->restore(); |
| 426 | }; |
| 427 | |
| 428 | SkIRect grid = persp.mapRect(SkRect::Make(img->dimensions())).roundOut(); |
| 429 | grid.fLeft -= 20; // manual adjust to look nicer |
| 430 | |
| 431 | canvas->translate(10.f, 10.f); |
| 432 | |
| 433 | for (size_t i = 0; i < SK_ARRAY_COUNT(matches); ++i) { |
| 434 | canvas->save(); |
| 435 | canvas->translate(-grid.fLeft, -grid.fTop); |
| 436 | drawConfig(matches[i][0]); |
| 437 | canvas->translate(0.f, grid.height()); |
| 438 | drawConfig(matches[i][1]); |
| 439 | canvas->restore(); |
| 440 | |
| 441 | canvas->translate(grid.width(), 0.f); |
| 442 | } |
| 443 | } |
Michael Ludwig | 4ce7786 | 2020-10-27 18:07:29 -0400 | [diff] [blame] | 444 | |
| 445 | DEF_SIMPLE_GM(clip_shader_difference, canvas, 512, 512) { |
| 446 | auto image = GetResourceAsImage("images/yellow_rose.png"); |
| 447 | canvas->clear(SK_ColorGRAY); |
| 448 | |
| 449 | SkRect rect = SkRect::MakeWH(256, 256); |
Mike Reed | 2ac6ce8 | 2021-01-15 12:26:22 -0500 | [diff] [blame] | 450 | SkMatrix local = SkMatrix::RectToRect(SkRect::MakeWH(image->width(), image->height()), |
| 451 | SkRect::MakeWH(64, 64)); |
Mike Reed | b612b6c | 2020-12-08 21:58:35 -0500 | [diff] [blame] | 452 | auto shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 453 | SkSamplingOptions(), &local); |
Michael Ludwig | 4ce7786 | 2020-10-27 18:07:29 -0400 | [diff] [blame] | 454 | |
| 455 | SkPaint paint; |
| 456 | paint.setColor(SK_ColorRED); |
| 457 | paint.setAntiAlias(true); |
| 458 | |
| 459 | // TL: A rectangle |
| 460 | { |
| 461 | canvas->save(); |
| 462 | canvas->translate(0, 0); |
| 463 | canvas->clipShader(shader, SkClipOp::kDifference); |
| 464 | canvas->drawRect(rect, paint); |
| 465 | canvas->restore(); |
| 466 | } |
| 467 | // TR: A round rectangle |
| 468 | { |
| 469 | canvas->save(); |
| 470 | canvas->translate(256, 0); |
| 471 | canvas->clipShader(shader, SkClipOp::kDifference); |
| 472 | canvas->drawRRect(SkRRect::MakeRectXY(rect, 64.f, 64.f), paint); |
| 473 | canvas->restore(); |
| 474 | } |
| 475 | // BL: A path |
| 476 | { |
| 477 | canvas->save(); |
| 478 | canvas->translate(0, 256); |
| 479 | canvas->clipShader(shader, SkClipOp::kDifference); |
| 480 | |
| 481 | SkPath path; |
| 482 | path.moveTo(0.f, 128.f); |
| 483 | path.lineTo(128.f, 256.f); |
| 484 | path.lineTo(256.f, 128.f); |
| 485 | path.lineTo(128.f, 0.f); |
| 486 | |
| 487 | SkScalar d = 64.f * SK_ScalarSqrt2; |
| 488 | path.moveTo(128.f - d, 128.f - d); |
| 489 | path.lineTo(128.f - d, 128.f + d); |
| 490 | path.lineTo(128.f + d, 128.f + d); |
| 491 | path.lineTo(128.f + d, 128.f - d); |
| 492 | canvas->drawPath(path, paint); |
| 493 | canvas->restore(); |
| 494 | } |
| 495 | // BR: Text |
| 496 | { |
| 497 | canvas->save(); |
| 498 | canvas->translate(256, 256); |
| 499 | canvas->clipShader(shader, SkClipOp::kDifference); |
| 500 | for (int y = 0; y < 4; ++y) { |
| 501 | canvas->drawString("Hello", 32.f, y * 64.f, SkFont(nullptr, 64.f), paint); |
| 502 | } |
| 503 | canvas->restore(); |
| 504 | } |
| 505 | } |