blob: d0db1f3dd47d24420c72e81645e0f256ed1abe0a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 Reedebfce6d2016-12-12 10:02:12 -05007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkClipOp.h"
11#include "include/core/SkColor.h"
Michael Ludwig4ce77862020-10-27 18:07:29 -040012#include "include/core/SkColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkFontTypes.h"
15#include "include/core/SkPaint.h"
Mike Reedd849a752020-09-08 20:47:09 -040016#include "include/core/SkPathBuilder.h"
Kevin Lubick2d86f6f2022-02-23 12:17:12 -050017#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkRect.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTypeface.h"
23#include "include/core/SkTypes.h"
Michael Ludwig49203842020-06-02 17:27:07 -040024#include "include/effects/SkGradientShader.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040025#include "tools/DecodeUtils.h"
Mike Reed121c2af2020-03-10 14:02:56 -040026#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040028#include "tools/fonts/FontToolUtils.h"
bsalomon@google.com807cec42011-03-10 19:20:15 +000029
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include <string.h>
31
bsalomon@google.com807cec42011-03-10 19:20:15 +000032namespace skiagm {
33
mtkleindbfd7ab2016-09-01 11:24:54 -070034constexpr SkColor gPathColor = SK_ColorBLACK;
35constexpr SkColor gClipAColor = SK_ColorBLUE;
36constexpr SkColor gClipBColor = SK_ColorRED;
reed@google.coma8f60f22011-12-08 16:18:29 +000037
bsalomon@google.com807cec42011-03-10 19:20:15 +000038class ComplexClipGM : public GM {
39public:
bsalomon6ae83cf2014-12-17 14:38:49 -080040 ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
robertphillips@google.com50a69a02012-07-12 13:48:46 +000041 : fDoAAClip(aaclip)
bsalomon6ae83cf2014-12-17 14:38:49 -080042 , fDoSaveLayer(saveLayer)
43 , fInvertDraw(invertDraw) {
caryclarkceb9f3b2015-06-12 10:00:11 -070044 this->setBGColor(0xFFDEDFDE);
bsalomon@google.com807cec42011-03-10 19:20:15 +000045 }
46
47protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000048 SkString getName() const override {
reed@google.coma8f60f22011-12-08 16:18:29 +000049 SkString str;
bsalomon6ae83cf2014-12-17 14:38:49 -080050 str.printf("complexclip_%s%s%s",
robertphillips@google.com50a69a02012-07-12 13:48:46 +000051 fDoAAClip ? "aa" : "bw",
bsalomon6ae83cf2014-12-17 14:38:49 -080052 fDoSaveLayer ? "_layer" : "",
53 fInvertDraw ? "_invert" : "");
reed@google.coma8f60f22011-12-08 16:18:29 +000054 return str;
bsalomon@google.com807cec42011-03-10 19:20:15 +000055 }
56
Leandro Lovisolo8f023882023-08-15 21:13:52 +000057 SkISize getISize() override { return SkISize::Make(388, 780); }
bsalomon@google.com807cec42011-03-10 19:20:15 +000058
Mike Reedbc414ed2018-08-16 22:49:55 -040059 void onDraw(SkCanvas* canvas) override {
Mike Reedd849a752020-09-08 20:47:09 -040060 SkPath path = SkPathBuilder()
61 .moveTo(0, 50)
62 .quadTo(0, 0, 50, 0)
63 .lineTo(175, 0)
64 .quadTo(200, 0, 200, 25)
65 .lineTo(200, 150)
66 .quadTo(200, 200, 150, 200)
67 .lineTo(0, 200)
68 .close()
69 .moveTo(50, 50)
70 .lineTo(150, 50)
71 .lineTo(150, 125)
72 .quadTo(150, 150, 125, 150)
73 .lineTo(50, 150)
74 .close()
75 .detach();
bsalomon6ae83cf2014-12-17 14:38:49 -080076 if (fInvertDraw) {
Mike Reed7d34dc72019-11-26 12:17:17 -050077 path.setFillType(SkPathFillType::kInverseEvenOdd);
bsalomon6ae83cf2014-12-17 14:38:49 -080078 } else {
Mike Reed7d34dc72019-11-26 12:17:17 -050079 path.setFillType(SkPathFillType::kEvenOdd);
bsalomon6ae83cf2014-12-17 14:38:49 -080080 }
bsalomon@google.com807cec42011-03-10 19:20:15 +000081 SkPaint pathPaint;
82 pathPaint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000083 pathPaint.setColor(gPathColor);
bsalomon@google.com807cec42011-03-10 19:20:15 +000084
Mike Reedd849a752020-09-08 20:47:09 -040085 SkPath clipA = SkPath::Polygon({{10, 20}, {165, 22}, {70, 105}, {165, 177}, {-5, 180}}, true);
bsalomon@google.com807cec42011-03-10 19:20:15 +000086
Mike Reedd849a752020-09-08 20:47:09 -040087 SkPath clipB = SkPath::Polygon({{40, 10}, {190, 15}, {195, 190}, {40, 185}, {155, 100}}, true);
bsalomon@google.com807cec42011-03-10 19:20:15 +000088
Kevin Lubicke836c3a2023-10-20 06:55:35 -040089 SkFont font(ToolUtils::DefaultPortableTypeface(), 20);
bsalomon@google.com807cec42011-03-10 19:20:15 +000090
mtkleindbfd7ab2016-09-01 11:24:54 -070091 constexpr struct {
Mike Reedc1f77742016-12-09 09:00:50 -050092 SkClipOp fOp;
reed73603f32016-09-20 08:42:38 -070093 const char* fName;
bsalomon@google.com807cec42011-03-10 19:20:15 +000094 } gOps[] = { //extra spaces in names for measureText
Michael Ludwig2f6e2f82021-08-03 13:08:50 -040095 {SkClipOp::kIntersect, "Isect "},
96 {SkClipOp::kDifference, "Diff " },
bsalomon@google.com807cec42011-03-10 19:20:15 +000097 };
98
Mike Reedbc414ed2018-08-16 22:49:55 -040099 canvas->translate(20, 20);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000100 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000101
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000102 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.org4b413c82013-11-25 19:44:07 +0000107 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.com54bb7ab2012-07-13 14:55:25 +0000111
Mike Reedbc414ed2018-08-16 22:49:55 -0400112 bounds.inset(100, 100);
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000113 SkPaint boundPaint;
114 boundPaint.setColor(SK_ColorRED);
115 boundPaint.setStyle(SkPaint::kStroke_Style);
116 canvas->drawRect(bounds, boundPaint);
Ben Wagner788a2dc2018-05-09 13:23:38 -0400117 canvas->clipRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -0700118 canvas->saveLayer(&bounds, nullptr);
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000119 }
120
reed@google.coma8f60f22011-12-08 16:18:29 +0000121 for (int invBits = 0; invBits < 4; ++invBits) {
122 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -0400123 for (size_t op = 0; op < std::size(gOps); ++op) {
reed@google.coma8f60f22011-12-08 16:18:29 +0000124 this->drawHairlines(canvas, path, clipA, clipB);
125
126 bool doInvA = SkToBool(invBits & 1);
127 bool doInvB = SkToBool(invBits & 2);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000128 canvas->save();
129 // set clip
Mike Reed7d34dc72019-11-26 12:17:17 -0500130 clipA.setFillType(doInvA ? SkPathFillType::kInverseEvenOdd :
131 SkPathFillType::kEvenOdd);
132 clipB.setFillType(doInvB ? SkPathFillType::kInverseEvenOdd :
133 SkPathFillType::kEvenOdd);
reed66998382016-09-21 11:15:07 -0700134 canvas->clipPath(clipA, fDoAAClip);
reed@google.coma8f60f22011-12-08 16:18:29 +0000135 canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000136
bsalomon6ae83cf2014-12-17 14:38:49 -0800137 // 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.com807cec42011-03-10 19:20:15 +0000147 // draw path clipped
148 canvas->drawPath(path, pathPaint);
149 canvas->restore();
150
bsalomon@google.com807cec42011-03-10 19:20:15 +0000151
Mike Reed2e6db182018-12-15 13:45:33 -0500152 SkPaint paint;
Mike Reedbc414ed2018-08-16 22:49:55 -0400153 SkScalar txtX = 45;
reed@google.coma8f60f22011-12-08 16:18:29 +0000154 paint.setColor(gClipAColor);
155 const char* aTxt = doInvA ? "InvA " : "A ";
Ben Wagner51e15a62019-05-07 15:38:46 -0400156 canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
157 txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000158 paint.setColor(SK_ColorBLACK);
Ben Wagner51e15a62019-05-07 15:38:46 -0400159 canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, txtX, 220,
Mike Reed2e6db182018-12-15 13:45:33 -0500160 font, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -0400161 txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8);
reed@google.coma8f60f22011-12-08 16:18:29 +0000162 paint.setColor(gClipBColor);
163 const char* bTxt = doInvB ? "InvB " : "B ";
Ben Wagner51e15a62019-05-07 15:38:46 -0400164 canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000165
Mike Reedbc414ed2018-08-16 22:49:55 -0400166 canvas->translate(250,0);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000167 }
reed@google.coma8f60f22011-12-08 16:18:29 +0000168 canvas->restore();
Mike Reedbc414ed2018-08-16 22:49:55 -0400169 canvas->translate(0, 250);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000170 }
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000171
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000172 if (fDoSaveLayer) {
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000173 canvas->restore();
174 }
bsalomon@google.com807cec42011-03-10 19:20:15 +0000175 }
176private:
reed@google.coma8f60f22011-12-08 16:18:29 +0000177 void drawHairlines(SkCanvas* canvas, const SkPath& path,
178 const SkPath& clipA, const SkPath& clipB) {
179 SkPaint paint;
180 paint.setAntiAlias(true);
181 paint.setStyle(SkPaint::kStroke_Style);
182 const SkAlpha fade = 0x33;
183
184 // draw path in hairline
185 paint.setColor(gPathColor); paint.setAlpha(fade);
186 canvas->drawPath(path, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000187
reed@google.coma8f60f22011-12-08 16:18:29 +0000188 // draw clips in hair line
189 paint.setColor(gClipAColor); paint.setAlpha(fade);
190 canvas->drawPath(clipA, paint);
191 paint.setColor(gClipBColor); paint.setAlpha(fade);
192 canvas->drawPath(clipB, paint);
193 }
194
bsalomon6ae83cf2014-12-17 14:38:49 -0800195 bool fDoAAClip;
196 bool fDoSaveLayer;
197 bool fInvertDraw;
198
John Stiles7571f9e2020-09-02 22:42:33 -0400199 using INHERITED = GM;
bsalomon@google.com807cec42011-03-10 19:20:15 +0000200};
201
202//////////////////////////////////////////////////////////////////////////////
203
halcanary385fe4d2015-08-26 13:07:48 -0700204DEF_GM(return new ComplexClipGM(false, false, false);)
205DEF_GM(return new ComplexClipGM(false, false, true);)
206DEF_GM(return new ComplexClipGM(false, true, false);)
207DEF_GM(return new ComplexClipGM(false, true, true);)
208DEF_GM(return new ComplexClipGM(true, false, false);)
209DEF_GM(return new ComplexClipGM(true, false, true);)
210DEF_GM(return new ComplexClipGM(true, true, false);)
211DEF_GM(return new ComplexClipGM(true, true, true);)
John Stilesa6841be2020-08-06 14:11:56 -0400212} // namespace skiagm
Mike Reed121c2af2020-03-10 14:02:56 -0400213
214DEF_SIMPLE_GM(clip_shader, canvas, 840, 650) {
Kevin Lubick8b741882023-10-06 11:41:38 -0400215 auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
Mike Reed99c94462020-12-08 13:16:56 -0500216 auto sh = img->makeShader(SkSamplingOptions());
Mike Reed121c2af2020-03-10 14:02:56 -0400217
218 SkRect r = SkRect::MakeIWH(img->width(), img->height());
219 SkPaint p;
220
221 canvas->translate(10, 10);
Mike Reed8d29ab62021-01-23 18:10:39 -0500222 canvas->drawImage(img, 0, 0);
Mike Reed121c2af2020-03-10 14:02:56 -0400223
224 canvas->save();
225 canvas->translate(img->width() + 10, 0);
226 canvas->clipShader(sh, SkClipOp::kIntersect);
227 p.setColor(SK_ColorRED);
228 canvas->drawRect(r, p);
229 canvas->restore();
230
231 canvas->save();
232 canvas->translate(0, img->height() + 10);
233 canvas->clipShader(sh, SkClipOp::kDifference);
234 p.setColor(SK_ColorGREEN);
235 canvas->drawRect(r, p);
236 canvas->restore();
237
238 canvas->save();
239 canvas->translate(img->width() + 10, img->height() + 10);
240 canvas->clipShader(sh, SkClipOp::kIntersect);
241 canvas->save();
Mike Reed1f607332020-05-21 12:11:27 -0400242 SkMatrix lm = SkMatrix::Scale(1.0f/5, 1.0f/5);
Mike Reed99c94462020-12-08 13:16:56 -0500243 canvas->clipShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
244 SkSamplingOptions(), lm));
Mike Reed8d29ab62021-01-23 18:10:39 -0500245 canvas->drawImage(img, 0, 0);
Mike Reed121c2af2020-03-10 14:02:56 -0400246
247 canvas->restore();
248 canvas->restore();
249}
Mike Reed84a9eb52020-03-12 13:55:44 -0400250
251DEF_SIMPLE_GM(clip_shader_layer, canvas, 430, 320) {
Kevin Lubick8b741882023-10-06 11:41:38 -0400252 auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
Mike Reed99c94462020-12-08 13:16:56 -0500253 auto sh = img->makeShader(SkSamplingOptions());
Mike Reed84a9eb52020-03-12 13:55:44 -0400254
255 SkRect r = SkRect::MakeIWH(img->width(), img->height());
256
257 canvas->translate(10, 10);
258 // now add the cool clip
259 canvas->clipRect(r);
260 canvas->clipShader(sh);
261 // now draw a layer with the same image, and watch it get restored w/ the clip
262 canvas->saveLayer(&r, nullptr);
263 canvas->drawColor(0xFFFF0000);
264 canvas->restore();
265}
Michael Ludwig49203842020-06-02 17:27:07 -0400266
267DEF_SIMPLE_GM(clip_shader_nested, canvas, 256, 256) {
268 float w = 64.f;
269 float h = 64.f;
270
271 const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)};
272 auto s = SkGradientShader::MakeRadial({0.5f * w, 0.5f * h}, 0.1f * w, gradColors, nullptr,
273 2, SkTileMode::kRepeat, 0, nullptr);
274
275 SkPaint p;
276
277 // A large black rect affected by two gradient clips
278 canvas->save();
279 canvas->clipShader(s);
280 canvas->scale(2.f, 2.f);
281 canvas->clipShader(s);
282 canvas->drawRect(SkRect::MakeWH(w, h), p);
283 canvas->restore();
284
285 canvas->translate(0.f, 2.f * h);
286
287 // A small red rect, with no clipping
288 canvas->save();
289 p.setColor(SK_ColorRED);
290 canvas->drawRect(SkRect::MakeWH(w, h), p);
291 canvas->restore();
292}
Michael Ludwig88b3b152020-06-03 10:22:49 -0400293
294namespace {
295
296// Where is canvas->concat(persp) called relative to the clipShader calls.
297enum ConcatPerspective {
298 kConcatBeforeClips,
299 kConcatAfterClips,
300 kConcatBetweenClips
301};
302// Order in which clipShader(image) and clipShader(gradient) are specified; only meaningful
303// when CanvasPerspective is kConcatBetweenClips.
304enum ClipOrder {
305 kClipImageFirst,
306 kClipGradientFirst,
307
308 kDoesntMatter = kClipImageFirst
309};
310// Which shaders have perspective applied as a local matrix.
311enum LocalMatrix {
312 kNoLocalMat,
313 kImageWithLocalMat,
314 kGradientWithLocalMat,
315 kBothWithLocalMat
316};
317struct Config {
318 ConcatPerspective fConcat;
319 ClipOrder fOrder;
320 LocalMatrix fLM;
321};
322
323static void draw_banner(SkCanvas* canvas, Config config) {
324 SkString banner;
325 banner.append("Persp: ");
326
327 if (config.fConcat == kConcatBeforeClips || config.fLM == kBothWithLocalMat) {
328 banner.append("Both Clips");
329 } else {
330 SkASSERT((config.fConcat == kConcatBetweenClips && config.fLM == kNoLocalMat) ||
331 (config.fConcat == kConcatAfterClips && (config.fLM == kImageWithLocalMat ||
332 config.fLM == kGradientWithLocalMat)));
333 if ((config.fConcat == kConcatBetweenClips && config.fOrder == kClipImageFirst) ||
334 config.fLM == kGradientWithLocalMat) {
335 banner.append("Gradient");
336 } else {
337 SkASSERT(config.fOrder == kClipGradientFirst || config.fLM == kImageWithLocalMat);
338 banner.append("Image");
339 }
340 }
341 if (config.fLM != kNoLocalMat) {
342 banner.append(" (w/ LM, should equal top row)");
343 }
344
Kevin Lubicke836c3a2023-10-20 06:55:35 -0400345 static const SkFont kFont(ToolUtils::DefaultPortableTypeface(), 12);
Michael Ludwig88b3b152020-06-03 10:22:49 -0400346 canvas->drawString(banner.c_str(), 20.f, -30.f, kFont, SkPaint());
347};
348
John Stilesa6841be2020-08-06 14:11:56 -0400349} // namespace
Michael Ludwig88b3b152020-06-03 10:22:49 -0400350
351DEF_SIMPLE_GM(clip_shader_persp, canvas, 1370, 1030) {
352 // Each draw has a clipShader(image-shader), a clipShader(gradient-shader), a concat(persp-mat),
353 // and each shader may or may not be wrapped with a perspective local matrix.
354
355 // Pairs of configs that should match in appearance where first config doesn't use a local
356 // matrix (top row of GM) and the second does (bottom row of GM).
357 Config matches[][2] = {
358 // Everything has perspective
359 {{kConcatBeforeClips, kDoesntMatter, kNoLocalMat},
360 {kConcatAfterClips, kDoesntMatter, kBothWithLocalMat}},
361 // Image shader has perspective
362 {{kConcatBetweenClips, kClipGradientFirst, kNoLocalMat},
363 {kConcatAfterClips, kDoesntMatter, kImageWithLocalMat}},
364 // Gradient shader has perspective
365 {{kConcatBetweenClips, kClipImageFirst, kNoLocalMat},
366 {kConcatAfterClips, kDoesntMatter, kGradientWithLocalMat}}
367 };
368
369 // The image that is drawn
Kevin Lubick8b741882023-10-06 11:41:38 -0400370 auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
Michael Ludwig88b3b152020-06-03 10:22:49 -0400371 // Scale factor always applied to the image shader so that it tiles
372 SkMatrix scale = SkMatrix::Scale(1.f / 4.f, 1.f / 4.f);
373 // The perspective matrix applied wherever needed
374 SkPoint src[4];
375 SkRect::Make(img->dimensions()).toQuad(src);
376 SkPoint dst[4] = {{0, 80.f},
377 {img->width() + 28.f, -100.f},
378 {img->width() - 28.f, img->height() + 100.f},
379 {0.f, img->height() - 80.f}};
380 SkMatrix persp;
381 SkAssertResult(persp.setPolyToPoly(src, dst, 4));
382
383 SkMatrix perspScale = SkMatrix::Concat(persp, scale);
384
385 auto drawConfig = [&](Config config) {
386 canvas->save();
387
388 draw_banner(canvas, config);
389
390 // Make clipShaders (possibly with local matrices)
391 bool gradLM = config.fLM == kGradientWithLocalMat || config.fLM == kBothWithLocalMat;
392 const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)};
393 auto gradShader = SkGradientShader::MakeRadial({0.5f * img->width(), 0.5f * img->height()},
394 0.1f * img->width(), gradColors, nullptr, 2,
395 SkTileMode::kRepeat, 0,
396 gradLM ? &persp : nullptr);
397 bool imageLM = config.fLM == kImageWithLocalMat || config.fLM == kBothWithLocalMat;
398 auto imgShader = img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
Mike Reedb612b6c2020-12-08 21:58:35 -0500399 SkSamplingOptions(), imageLM ? perspScale : scale);
Michael Ludwig88b3b152020-06-03 10:22:49 -0400400
401 // Perspective before any clipShader
402 if (config.fConcat == kConcatBeforeClips) {
403 canvas->concat(persp);
404 }
405
406 // First clipshader
407 canvas->clipShader(config.fOrder == kClipImageFirst ? imgShader : gradShader);
408
409 // Perspective between clipShader
410 if (config.fConcat == kConcatBetweenClips) {
411 canvas->concat(persp);
412 }
413
414 // Second clipShader
415 canvas->clipShader(config.fOrder == kClipImageFirst ? gradShader : imgShader);
416
417 // Perspective after clipShader
418 if (config.fConcat == kConcatAfterClips) {
419 canvas->concat(persp);
420 }
421
422 // Actual draw and clip boundary are the same for all configs
Mike Reed568f0ae2021-01-24 08:57:23 -0500423 canvas->clipIRect(img->bounds());
Michael Ludwig88b3b152020-06-03 10:22:49 -0400424 canvas->clear(SK_ColorBLACK);
425 canvas->drawImage(img, 0, 0);
426
427 canvas->restore();
428 };
429
430 SkIRect grid = persp.mapRect(SkRect::Make(img->dimensions())).roundOut();
431 grid.fLeft -= 20; // manual adjust to look nicer
432
433 canvas->translate(10.f, 10.f);
434
Herb Derbyc37b3862022-06-21 09:49:17 -0400435 for (size_t i = 0; i < std::size(matches); ++i) {
Michael Ludwig88b3b152020-06-03 10:22:49 -0400436 canvas->save();
437 canvas->translate(-grid.fLeft, -grid.fTop);
438 drawConfig(matches[i][0]);
439 canvas->translate(0.f, grid.height());
440 drawConfig(matches[i][1]);
441 canvas->restore();
442
443 canvas->translate(grid.width(), 0.f);
444 }
445}
Michael Ludwig4ce77862020-10-27 18:07:29 -0400446
447DEF_SIMPLE_GM(clip_shader_difference, canvas, 512, 512) {
Kevin Lubick8b741882023-10-06 11:41:38 -0400448 auto image = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
Michael Ludwig4ce77862020-10-27 18:07:29 -0400449 canvas->clear(SK_ColorGRAY);
450
451 SkRect rect = SkRect::MakeWH(256, 256);
Mike Reed2ac6ce82021-01-15 12:26:22 -0500452 SkMatrix local = SkMatrix::RectToRect(SkRect::MakeWH(image->width(), image->height()),
453 SkRect::MakeWH(64, 64));
Mike Reedb612b6c2020-12-08 21:58:35 -0500454 auto shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
455 SkSamplingOptions(), &local);
Michael Ludwig4ce77862020-10-27 18:07:29 -0400456
457 SkPaint paint;
458 paint.setColor(SK_ColorRED);
459 paint.setAntiAlias(true);
460
461 // TL: A rectangle
462 {
463 canvas->save();
464 canvas->translate(0, 0);
465 canvas->clipShader(shader, SkClipOp::kDifference);
466 canvas->drawRect(rect, paint);
467 canvas->restore();
468 }
469 // TR: A round rectangle
470 {
471 canvas->save();
472 canvas->translate(256, 0);
473 canvas->clipShader(shader, SkClipOp::kDifference);
474 canvas->drawRRect(SkRRect::MakeRectXY(rect, 64.f, 64.f), paint);
475 canvas->restore();
476 }
477 // BL: A path
478 {
479 canvas->save();
480 canvas->translate(0, 256);
481 canvas->clipShader(shader, SkClipOp::kDifference);
482
483 SkPath path;
484 path.moveTo(0.f, 128.f);
485 path.lineTo(128.f, 256.f);
486 path.lineTo(256.f, 128.f);
487 path.lineTo(128.f, 0.f);
488
489 SkScalar d = 64.f * SK_ScalarSqrt2;
490 path.moveTo(128.f - d, 128.f - d);
491 path.lineTo(128.f - d, 128.f + d);
492 path.lineTo(128.f + d, 128.f + d);
493 path.lineTo(128.f + d, 128.f - d);
494 canvas->drawPath(path, paint);
495 canvas->restore();
496 }
497 // BR: Text
498 {
499 canvas->save();
500 canvas->translate(256, 256);
501 canvas->clipShader(shader, SkClipOp::kDifference);
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400502 SkFont font = SkFont(ToolUtils::DefaultPortableTypeface(), 64.f);
Michael Ludwig4ce77862020-10-27 18:07:29 -0400503 for (int y = 0; y < 4; ++y) {
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400504 canvas->drawString("Hello", 32.f, y * 64.f, font, paint);
Michael Ludwig4ce77862020-10-27 18:07:29 -0400505 }
506 canvas->restore();
507 }
508}