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