blob: e2da27c2e9e3fc496252658fd9e721b05fa85850 [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#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 Ludwig49203842020-06-02 17:27:07 -040023#include "include/effects/SkGradientShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "src/core/SkClipOpPriv.h"
Mike Reed121c2af2020-03-10 14:02:56 -040025#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "tools/ToolUtils.h"
bsalomon@google.com807cec42011-03-10 19:20:15 +000027
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include <string.h>
29
bsalomon@google.com807cec42011-03-10 19:20:15 +000030namespace skiagm {
31
mtkleindbfd7ab2016-09-01 11:24:54 -070032constexpr SkColor gPathColor = SK_ColorBLACK;
33constexpr SkColor gClipAColor = SK_ColorBLUE;
34constexpr SkColor gClipBColor = SK_ColorRED;
reed@google.coma8f60f22011-12-08 16:18:29 +000035
bsalomon@google.com807cec42011-03-10 19:20:15 +000036class ComplexClipGM : public GM {
37public:
bsalomon6ae83cf2014-12-17 14:38:49 -080038 ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
robertphillips@google.com50a69a02012-07-12 13:48:46 +000039 : fDoAAClip(aaclip)
bsalomon6ae83cf2014-12-17 14:38:49 -080040 , fDoSaveLayer(saveLayer)
41 , fInvertDraw(invertDraw) {
caryclarkceb9f3b2015-06-12 10:00:11 -070042 this->setBGColor(0xFFDEDFDE);
bsalomon@google.com807cec42011-03-10 19:20:15 +000043 }
44
45protected:
Mike Reedbc414ed2018-08-16 22:49:55 -040046 SkString onShortName() override {
reed@google.coma8f60f22011-12-08 16:18:29 +000047 SkString str;
bsalomon6ae83cf2014-12-17 14:38:49 -080048 str.printf("complexclip_%s%s%s",
robertphillips@google.com50a69a02012-07-12 13:48:46 +000049 fDoAAClip ? "aa" : "bw",
bsalomon6ae83cf2014-12-17 14:38:49 -080050 fDoSaveLayer ? "_layer" : "",
51 fInvertDraw ? "_invert" : "");
reed@google.coma8f60f22011-12-08 16:18:29 +000052 return str;
bsalomon@google.com807cec42011-03-10 19:20:15 +000053 }
54
Michael Ludwig9689e392020-05-05 10:56:42 -040055 SkISize onISize() override { return SkISize::Make(388, 780); }
bsalomon@google.com807cec42011-03-10 19:20:15 +000056
Mike Reedbc414ed2018-08-16 22:49:55 -040057 void onDraw(SkCanvas* canvas) override {
Mike Reedd849a752020-09-08 20:47:09 -040058 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();
bsalomon6ae83cf2014-12-17 14:38:49 -080074 if (fInvertDraw) {
Mike Reed7d34dc72019-11-26 12:17:17 -050075 path.setFillType(SkPathFillType::kInverseEvenOdd);
bsalomon6ae83cf2014-12-17 14:38:49 -080076 } else {
Mike Reed7d34dc72019-11-26 12:17:17 -050077 path.setFillType(SkPathFillType::kEvenOdd);
bsalomon6ae83cf2014-12-17 14:38:49 -080078 }
bsalomon@google.com807cec42011-03-10 19:20:15 +000079 SkPaint pathPaint;
80 pathPaint.setAntiAlias(true);
reed@google.coma8f60f22011-12-08 16:18:29 +000081 pathPaint.setColor(gPathColor);
bsalomon@google.com807cec42011-03-10 19:20:15 +000082
Mike Reedd849a752020-09-08 20:47:09 -040083 SkPath clipA = SkPath::Polygon({{10, 20}, {165, 22}, {70, 105}, {165, 177}, {-5, 180}}, true);
bsalomon@google.com807cec42011-03-10 19:20:15 +000084
Mike Reedd849a752020-09-08 20:47:09 -040085 SkPath clipB = SkPath::Polygon({{40, 10}, {190, 15}, {195, 190}, {40, 185}, {155, 100}}, true);
bsalomon@google.com807cec42011-03-10 19:20:15 +000086
Mike Kleinea3f0142019-03-20 11:12:10 -050087 SkFont font(ToolUtils::create_portable_typeface(), 20);
bsalomon@google.com807cec42011-03-10 19:20:15 +000088
mtkleindbfd7ab2016-09-01 11:24:54 -070089 constexpr struct {
Mike Reedc1f77742016-12-09 09:00:50 -050090 SkClipOp fOp;
reed73603f32016-09-20 08:42:38 -070091 const char* fName;
bsalomon@google.com807cec42011-03-10 19:20:15 +000092 } gOps[] = { //extra spaces in names for measureText
Mike Reedc1f77742016-12-09 09:00:50 -050093 {kIntersect_SkClipOp, "Isect "},
94 {kDifference_SkClipOp, "Diff " },
bsalomon@google.com807cec42011-03-10 19:20:15 +000095 };
96
Mike Reedbc414ed2018-08-16 22:49:55 -040097 canvas->translate(20, 20);
bsalomon@google.com807cec42011-03-10 19:20:15 +000098 canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
bsalomon@google.com807cec42011-03-10 19:20:15 +000099
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000100 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.org4b413c82013-11-25 19:44:07 +0000105 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.com54bb7ab2012-07-13 14:55:25 +0000109
Mike Reedbc414ed2018-08-16 22:49:55 -0400110 bounds.inset(100, 100);
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000111 SkPaint boundPaint;
112 boundPaint.setColor(SK_ColorRED);
113 boundPaint.setStyle(SkPaint::kStroke_Style);
114 canvas->drawRect(bounds, boundPaint);
Ben Wagner788a2dc2018-05-09 13:23:38 -0400115 canvas->clipRect(bounds);
halcanary96fcdcc2015-08-27 07:41:13 -0700116 canvas->saveLayer(&bounds, nullptr);
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000117 }
118
reed@google.coma8f60f22011-12-08 16:18:29 +0000119 for (int invBits = 0; invBits < 4; ++invBits) {
120 canvas->save();
bsalomon@google.com807cec42011-03-10 19:20:15 +0000121 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
reed@google.coma8f60f22011-12-08 16:18:29 +0000122 this->drawHairlines(canvas, path, clipA, clipB);
123
124 bool doInvA = SkToBool(invBits & 1);
125 bool doInvB = SkToBool(invBits & 2);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000126 canvas->save();
127 // set clip
Mike Reed7d34dc72019-11-26 12:17:17 -0500128 clipA.setFillType(doInvA ? SkPathFillType::kInverseEvenOdd :
129 SkPathFillType::kEvenOdd);
130 clipB.setFillType(doInvB ? SkPathFillType::kInverseEvenOdd :
131 SkPathFillType::kEvenOdd);
reed66998382016-09-21 11:15:07 -0700132 canvas->clipPath(clipA, fDoAAClip);
reed@google.coma8f60f22011-12-08 16:18:29 +0000133 canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000134
bsalomon6ae83cf2014-12-17 14:38:49 -0800135 // 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.com807cec42011-03-10 19:20:15 +0000145 // draw path clipped
146 canvas->drawPath(path, pathPaint);
147 canvas->restore();
148
bsalomon@google.com807cec42011-03-10 19:20:15 +0000149
Mike Reed2e6db182018-12-15 13:45:33 -0500150 SkPaint paint;
Mike Reedbc414ed2018-08-16 22:49:55 -0400151 SkScalar txtX = 45;
reed@google.coma8f60f22011-12-08 16:18:29 +0000152 paint.setColor(gClipAColor);
153 const char* aTxt = doInvA ? "InvA " : "A ";
Ben Wagner51e15a62019-05-07 15:38:46 -0400154 canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
155 txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000156 paint.setColor(SK_ColorBLACK);
Ben Wagner51e15a62019-05-07 15:38:46 -0400157 canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, txtX, 220,
Mike Reed2e6db182018-12-15 13:45:33 -0500158 font, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -0400159 txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8);
reed@google.coma8f60f22011-12-08 16:18:29 +0000160 paint.setColor(gClipBColor);
161 const char* bTxt = doInvB ? "InvB " : "B ";
Ben Wagner51e15a62019-05-07 15:38:46 -0400162 canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000163
Mike Reedbc414ed2018-08-16 22:49:55 -0400164 canvas->translate(250,0);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000165 }
reed@google.coma8f60f22011-12-08 16:18:29 +0000166 canvas->restore();
Mike Reedbc414ed2018-08-16 22:49:55 -0400167 canvas->translate(0, 250);
bsalomon@google.com807cec42011-03-10 19:20:15 +0000168 }
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000169
robertphillips@google.com54bb7ab2012-07-13 14:55:25 +0000170 if (fDoSaveLayer) {
robertphillips@google.com50a69a02012-07-12 13:48:46 +0000171 canvas->restore();
172 }
bsalomon@google.com807cec42011-03-10 19:20:15 +0000173 }
174private:
reed@google.coma8f60f22011-12-08 16:18:29 +0000175 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.comd6176b02012-08-23 18:14:13 +0000185
reed@google.coma8f60f22011-12-08 16:18:29 +0000186 // 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
bsalomon6ae83cf2014-12-17 14:38:49 -0800193 bool fDoAAClip;
194 bool fDoSaveLayer;
195 bool fInvertDraw;
196
John Stiles7571f9e2020-09-02 22:42:33 -0400197 using INHERITED = GM;
bsalomon@google.com807cec42011-03-10 19:20:15 +0000198};
199
200//////////////////////////////////////////////////////////////////////////////
201
halcanary385fe4d2015-08-26 13:07:48 -0700202DEF_GM(return new ComplexClipGM(false, false, false);)
203DEF_GM(return new ComplexClipGM(false, false, true);)
204DEF_GM(return new ComplexClipGM(false, true, false);)
205DEF_GM(return new ComplexClipGM(false, true, true);)
206DEF_GM(return new ComplexClipGM(true, false, false);)
207DEF_GM(return new ComplexClipGM(true, false, true);)
208DEF_GM(return new ComplexClipGM(true, true, false);)
209DEF_GM(return new ComplexClipGM(true, true, true);)
John Stilesa6841be2020-08-06 14:11:56 -0400210} // namespace skiagm
Mike Reed121c2af2020-03-10 14:02:56 -0400211
212DEF_SIMPLE_GM(clip_shader, canvas, 840, 650) {
213 auto img = GetResourceAsImage("images/yellow_rose.png");
Mike Reed99c94462020-12-08 13:16:56 -0500214 auto sh = img->makeShader(SkSamplingOptions());
Mike Reed121c2af2020-03-10 14:02:56 -0400215
216 SkRect r = SkRect::MakeIWH(img->width(), img->height());
217 SkPaint p;
218
219 canvas->translate(10, 10);
220 canvas->drawImage(img, 0, 0, nullptr);
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 Reed1f607332020-05-21 12:11:27 -0400240 SkMatrix lm = SkMatrix::Scale(1.0f/5, 1.0f/5);
Mike Reed99c94462020-12-08 13:16:56 -0500241 canvas->clipShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
242 SkSamplingOptions(), lm));
Mike Reed121c2af2020-03-10 14:02:56 -0400243 canvas->drawImage(img, 0, 0, nullptr);
244
245 canvas->restore();
246 canvas->restore();
247}
Mike Reed84a9eb52020-03-12 13:55:44 -0400248
249DEF_SIMPLE_GM(clip_shader_layer, canvas, 430, 320) {
250 auto img = GetResourceAsImage("images/yellow_rose.png");
Mike Reed99c94462020-12-08 13:16:56 -0500251 auto sh = img->makeShader(SkSamplingOptions());
Mike Reed84a9eb52020-03-12 13:55:44 -0400252
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 Ludwig49203842020-06-02 17:27:07 -0400264
265DEF_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 Ludwig88b3b152020-06-03 10:22:49 -0400291
292namespace {
293
294// Where is canvas->concat(persp) called relative to the clipShader calls.
295enum 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.
302enum ClipOrder {
303 kClipImageFirst,
304 kClipGradientFirst,
305
306 kDoesntMatter = kClipImageFirst
307};
308// Which shaders have perspective applied as a local matrix.
309enum LocalMatrix {
310 kNoLocalMat,
311 kImageWithLocalMat,
312 kGradientWithLocalMat,
313 kBothWithLocalMat
314};
315struct Config {
316 ConcatPerspective fConcat;
317 ClipOrder fOrder;
318 LocalMatrix fLM;
319};
320
321static 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 Stilesa6841be2020-08-06 14:11:56 -0400347} // namespace
Michael Ludwig88b3b152020-06-03 10:22:49 -0400348
349DEF_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 Reedb612b6c2020-12-08 21:58:35 -0500397 SkSamplingOptions(), imageLM ? perspScale : scale);
Michael Ludwig88b3b152020-06-03 10:22:49 -0400398
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
421 canvas->clipRect(SkRect::MakeIWH(img->width(), img->height()));
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 Ludwig4ce77862020-10-27 18:07:29 -0400444
445DEF_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);
450 SkMatrix local = SkMatrix::MakeRectToRect(SkRect::MakeWH(image->width(), image->height()),
451 SkRect::MakeWH(64, 64), SkMatrix::kFill_ScaleToFit);
Mike Reedb612b6c2020-12-08 21:58:35 -0500452 auto shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
453 SkSamplingOptions(), &local);
Michael Ludwig4ce77862020-10-27 18:07:29 -0400454
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}