xref: /aosp_15_r20/external/skia/gm/complexclip.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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  */
7 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkClipOp.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorFilter.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPathBuilder.h"
17 #include "include/core/SkRRect.h"
18 #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"
24 #include "include/effects/SkGradientShader.h"
25 #include "tools/DecodeUtils.h"
26 #include "tools/Resources.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/fonts/FontToolUtils.h"
29 
30 #include <string.h>
31 
32 namespace skiagm {
33 
34 constexpr SkColor gPathColor = SK_ColorBLACK;
35 constexpr SkColor gClipAColor = SK_ColorBLUE;
36 constexpr SkColor gClipBColor = SK_ColorRED;
37 
38 class ComplexClipGM : public GM {
39 public:
ComplexClipGM(bool aaclip,bool saveLayer,bool invertDraw)40     ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
41     : fDoAAClip(aaclip)
42     , fDoSaveLayer(saveLayer)
43     , fInvertDraw(invertDraw) {
44         this->setBGColor(0xFFDEDFDE);
45     }
46 
47 protected:
getName() const48     SkString getName() const override {
49         SkString str;
50         str.printf("complexclip_%s%s%s",
51                    fDoAAClip ? "aa" : "bw",
52                    fDoSaveLayer ? "_layer" : "",
53                    fInvertDraw ? "_invert" : "");
54         return str;
55     }
56 
getISize()57     SkISize getISize() override { return SkISize::Make(388, 780); }
58 
onDraw(SkCanvas * canvas)59     void onDraw(SkCanvas* canvas) override {
60         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();
76         if (fInvertDraw) {
77             path.setFillType(SkPathFillType::kInverseEvenOdd);
78         } else {
79             path.setFillType(SkPathFillType::kEvenOdd);
80         }
81         SkPaint pathPaint;
82         pathPaint.setAntiAlias(true);
83         pathPaint.setColor(gPathColor);
84 
85         SkPath clipA = SkPath::Polygon({{10,  20}, {165, 22}, {70,  105}, {165, 177}, {-5,  180}}, true);
86 
87         SkPath clipB = SkPath::Polygon({{40,  10}, {190, 15}, {195, 190}, {40,  185}, {155, 100}}, true);
88 
89         SkFont font(ToolUtils::DefaultPortableTypeface(), 20);
90 
91         constexpr struct {
92             SkClipOp fOp;
93             const char*      fName;
94         } gOps[] = { //extra spaces in names for measureText
95             {SkClipOp::kIntersect,         "Isect "},
96             {SkClipOp::kDifference,        "Diff " },
97         };
98 
99         canvas->translate(20, 20);
100         canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
101 
102         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(
107               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));
111 
112             bounds.inset(100, 100);
113             SkPaint boundPaint;
114             boundPaint.setColor(SK_ColorRED);
115             boundPaint.setStyle(SkPaint::kStroke_Style);
116             canvas->drawRect(bounds, boundPaint);
117             canvas->clipRect(bounds);
118             canvas->saveLayer(&bounds, nullptr);
119         }
120 
121         for (int invBits = 0; invBits < 4; ++invBits) {
122             canvas->save();
123             for (size_t op = 0; op < std::size(gOps); ++op) {
124                 this->drawHairlines(canvas, path, clipA, clipB);
125 
126                 bool doInvA = SkToBool(invBits & 1);
127                 bool doInvB = SkToBool(invBits & 2);
128                 canvas->save();
129                     // set clip
130                     clipA.setFillType(doInvA ? SkPathFillType::kInverseEvenOdd :
131                                       SkPathFillType::kEvenOdd);
132                     clipB.setFillType(doInvB ? SkPathFillType::kInverseEvenOdd :
133                                       SkPathFillType::kEvenOdd);
134                     canvas->clipPath(clipA, fDoAAClip);
135                     canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
136 
137                     // 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 
147                     // draw path clipped
148                     canvas->drawPath(path, pathPaint);
149                 canvas->restore();
150 
151 
152                 SkPaint paint;
153                 SkScalar txtX = 45;
154                 paint.setColor(gClipAColor);
155                 const char* aTxt = doInvA ? "InvA " : "A ";
156                 canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
157                 txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8);
158                 paint.setColor(SK_ColorBLACK);
159                 canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, txtX, 220,
160                                        font, paint);
161                 txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8);
162                 paint.setColor(gClipBColor);
163                 const char* bTxt = doInvB ? "InvB " : "B ";
164                 canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
165 
166                 canvas->translate(250,0);
167             }
168             canvas->restore();
169             canvas->translate(0, 250);
170         }
171 
172         if (fDoSaveLayer) {
173             canvas->restore();
174         }
175     }
176 private:
drawHairlines(SkCanvas * canvas,const SkPath & path,const SkPath & clipA,const SkPath & clipB)177     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);
187 
188         // 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 
195     bool fDoAAClip;
196     bool fDoSaveLayer;
197     bool fInvertDraw;
198 
199     using INHERITED = GM;
200 };
201 
202 //////////////////////////////////////////////////////////////////////////////
203 
204 DEF_GM(return new ComplexClipGM(false, false, false);)
205 DEF_GM(return new ComplexClipGM(false, false, true);)
206 DEF_GM(return new ComplexClipGM(false, true, false);)
207 DEF_GM(return new ComplexClipGM(false, true, true);)
208 DEF_GM(return new ComplexClipGM(true, false, false);)
209 DEF_GM(return new ComplexClipGM(true, false, true);)
210 DEF_GM(return new ComplexClipGM(true, true, false);)
211 DEF_GM(return new ComplexClipGM(true, true, true);)
212 }  // namespace skiagm
213 
214 DEF_SIMPLE_GM(clip_shader, canvas, 840, 650) {
215     auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
216     auto sh = img->makeShader(SkSamplingOptions());
217 
218     SkRect r = SkRect::MakeIWH(img->width(), img->height());
219     SkPaint p;
220 
221     canvas->translate(10, 10);
222     canvas->drawImage(img, 0, 0);
223 
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();
242     SkMatrix lm = SkMatrix::Scale(1.0f/5, 1.0f/5);
243     canvas->clipShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
244                                        SkSamplingOptions(), lm));
245     canvas->drawImage(img, 0, 0);
246 
247     canvas->restore();
248     canvas->restore();
249 }
250 
251 DEF_SIMPLE_GM(clip_shader_layer, canvas, 430, 320) {
252     auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
253     auto sh = img->makeShader(SkSamplingOptions());
254 
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 }
266 
267 DEF_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 
293     canvas->translate(2.f * w, -2.f * h);
294 
295     // A small green rect, with clip shader and rrect clipping
296     canvas->save();
297     canvas->clipShader(s);
298     canvas->clipRRect(SkRRect::MakeRectXY(SkRect::MakeWH(w, h), 10, 10), /*doAntiAlias=*/true);
299     p.setColor(SK_ColorGREEN);
300     canvas->drawRect(SkRect::MakeWH(w, h), p);
301     canvas->restore();
302 
303     canvas->translate(0.f, 2.f * h);
304 
305     // A small blue rect, with clip shader and path clipping
306     SkPath starPath;
307     starPath.moveTo(0.0f, -33.3333f);
308     starPath.lineTo(9.62f, -16.6667f);
309     starPath.lineTo(28.867f, -16.6667f);
310     starPath.lineTo(19.24f, 0.0f);
311     starPath.lineTo(28.867f, 16.6667f);
312     starPath.lineTo(9.62f, 16.6667f);
313     starPath.lineTo(0.0f, 33.3333f);
314     starPath.lineTo(-9.62f, 16.6667f);
315     starPath.lineTo(-28.867f, 16.6667f);
316     starPath.lineTo(-19.24f, 0.0f);
317     starPath.lineTo(-28.867f, -16.6667f);
318     starPath.lineTo(-9.62f, -16.6667f);
319     starPath.close();
320 
321     canvas->save();
322     canvas->clipShader(s);
323     canvas->translate(w/2, h/2);
324     canvas->clipPath(starPath);
325     p.setColor(SK_ColorBLUE);
326     canvas->translate(-w/2, -h/2);
327     canvas->drawRect(SkRect::MakeWH(w, h), p);
328     canvas->restore();
329 }
330 
331 namespace {
332 
333 // Where is canvas->concat(persp) called relative to the clipShader calls.
334 enum ConcatPerspective {
335     kConcatBeforeClips,
336     kConcatAfterClips,
337     kConcatBetweenClips
338 };
339 // Order in which clipShader(image) and clipShader(gradient) are specified; only meaningful
340 // when CanvasPerspective is kConcatBetweenClips.
341 enum ClipOrder {
342     kClipImageFirst,
343     kClipGradientFirst,
344 
345     kDoesntMatter = kClipImageFirst
346 };
347 // Which shaders have perspective applied as a local matrix.
348 enum LocalMatrix {
349     kNoLocalMat,
350     kImageWithLocalMat,
351     kGradientWithLocalMat,
352     kBothWithLocalMat
353 };
354 struct Config {
355     ConcatPerspective fConcat;
356     ClipOrder         fOrder;
357     LocalMatrix       fLM;
358 };
359 
draw_banner(SkCanvas * canvas,Config config)360 static void draw_banner(SkCanvas* canvas, Config config) {
361     SkString banner;
362     banner.append("Persp: ");
363 
364     if (config.fConcat == kConcatBeforeClips || config.fLM == kBothWithLocalMat) {
365         banner.append("Both Clips");
366     } else {
367         SkASSERT((config.fConcat == kConcatBetweenClips && config.fLM == kNoLocalMat) ||
368                  (config.fConcat == kConcatAfterClips && (config.fLM == kImageWithLocalMat ||
369                                                           config.fLM == kGradientWithLocalMat)));
370         if ((config.fConcat == kConcatBetweenClips && config.fOrder == kClipImageFirst) ||
371             config.fLM == kGradientWithLocalMat) {
372             banner.append("Gradient");
373         } else {
374             SkASSERT(config.fOrder == kClipGradientFirst || config.fLM == kImageWithLocalMat);
375             banner.append("Image");
376         }
377     }
378     if (config.fLM != kNoLocalMat) {
379         banner.append(" (w/ LM, should equal top row)");
380     }
381 
382     static const SkFont kFont(ToolUtils::DefaultPortableTypeface(), 12);
383     canvas->drawString(banner.c_str(), 20.f, -30.f, kFont, SkPaint());
384 };
385 
386 }  // namespace
387 
388 DEF_SIMPLE_GM(clip_shader_persp, canvas, 1370, 1030) {
389     // Each draw has a clipShader(image-shader), a clipShader(gradient-shader), a concat(persp-mat),
390     // and each shader may or may not be wrapped with a perspective local matrix.
391 
392     // Pairs of configs that should match in appearance where first config doesn't use a local
393     // matrix (top row of GM) and the second does (bottom row of GM).
394     Config matches[][2] = {
395             // Everything has perspective
396             {{kConcatBeforeClips,  kDoesntMatter,      kNoLocalMat},
397              {kConcatAfterClips,   kDoesntMatter,      kBothWithLocalMat}},
398             // Image shader has perspective
399             {{kConcatBetweenClips, kClipGradientFirst, kNoLocalMat},
400              {kConcatAfterClips,   kDoesntMatter,      kImageWithLocalMat}},
401             // Gradient shader has perspective
402             {{kConcatBetweenClips, kClipImageFirst,    kNoLocalMat},
403              {kConcatAfterClips,   kDoesntMatter,      kGradientWithLocalMat}}
404     };
405 
406     // The image that is drawn
407     auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
408     // Scale factor always applied to the image shader so that it tiles
409     SkMatrix scale = SkMatrix::Scale(1.f / 4.f, 1.f / 4.f);
410     // The perspective matrix applied wherever needed
411     SkPoint src[4];
412     SkRect::Make(img->dimensions()).toQuad(src);
413     SkPoint dst[4] = {{0, 80.f},
414                       {img->width() + 28.f, -100.f},
415                       {img->width() - 28.f, img->height() + 100.f},
416                       {0.f, img->height() - 80.f}};
417     SkMatrix persp;
418     SkAssertResult(persp.setPolyToPoly(src, dst, 4));
419 
420     SkMatrix perspScale = SkMatrix::Concat(persp, scale);
421 
__anon4478d7920302(Config config) 422     auto drawConfig = [&](Config config) {
423         canvas->save();
424 
425         draw_banner(canvas, config);
426 
427         // Make clipShaders (possibly with local matrices)
428         bool gradLM = config.fLM == kGradientWithLocalMat || config.fLM == kBothWithLocalMat;
429         const SkColor gradColors[] = {SK_ColorBLACK, SkColorSetARGB(128, 128, 128, 128)};
430         auto gradShader = SkGradientShader::MakeRadial({0.5f * img->width(), 0.5f * img->height()},
431                                                         0.1f * img->width(), gradColors, nullptr, 2,
432                                                         SkTileMode::kRepeat, 0,
433                                                         gradLM ? &persp : nullptr);
434         bool imageLM = config.fLM == kImageWithLocalMat || config.fLM == kBothWithLocalMat;
435         auto imgShader = img->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
436                                          SkSamplingOptions(), imageLM ? perspScale : scale);
437 
438         // Perspective before any clipShader
439         if (config.fConcat == kConcatBeforeClips) {
440             canvas->concat(persp);
441         }
442 
443         // First clipshader
444         canvas->clipShader(config.fOrder == kClipImageFirst ? imgShader : gradShader);
445 
446         // Perspective between clipShader
447         if (config.fConcat == kConcatBetweenClips) {
448             canvas->concat(persp);
449         }
450 
451         // Second clipShader
452         canvas->clipShader(config.fOrder == kClipImageFirst ? gradShader : imgShader);
453 
454         // Perspective after clipShader
455         if (config.fConcat == kConcatAfterClips) {
456             canvas->concat(persp);
457         }
458 
459         // Actual draw and clip boundary are the same for all configs
460         canvas->clipIRect(img->bounds());
461         canvas->clear(SK_ColorBLACK);
462         canvas->drawImage(img, 0, 0);
463 
464         canvas->restore();
465     };
466 
467     SkIRect grid = persp.mapRect(SkRect::Make(img->dimensions())).roundOut();
468     grid.fLeft -= 20; // manual adjust to look nicer
469 
470     canvas->translate(10.f, 10.f);
471 
472     for (size_t i = 0; i < std::size(matches); ++i) {
473         canvas->save();
474         canvas->translate(-grid.fLeft, -grid.fTop);
475         drawConfig(matches[i][0]);
476         canvas->translate(0.f, grid.height());
477         drawConfig(matches[i][1]);
478         canvas->restore();
479 
480         canvas->translate(grid.width(), 0.f);
481     }
482 }
483 
484 DEF_SIMPLE_GM(clip_shader_difference, canvas, 512, 512) {
485     auto image = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
486     canvas->clear(SK_ColorGRAY);
487 
488     SkRect rect = SkRect::MakeWH(256, 256);
489     SkMatrix local = SkMatrix::RectToRect(SkRect::MakeWH(image->width(), image->height()),
490                                           SkRect::MakeWH(64, 64));
491     auto shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
492                                     SkSamplingOptions(), &local);
493 
494     SkPaint paint;
495     paint.setColor(SK_ColorRED);
496     paint.setAntiAlias(true);
497 
498     // TL: A rectangle
499     {
500         canvas->save();
501         canvas->translate(0, 0);
502         canvas->clipShader(shader, SkClipOp::kDifference);
503         canvas->drawRect(rect, paint);
504         canvas->restore();
505     }
506     // TR: A round rectangle
507     {
508         canvas->save();
509         canvas->translate(256, 0);
510         canvas->clipShader(shader, SkClipOp::kDifference);
511         canvas->drawRRect(SkRRect::MakeRectXY(rect, 64.f, 64.f), paint);
512         canvas->restore();
513     }
514     // BL: A path
515     {
516         canvas->save();
517         canvas->translate(0, 256);
518         canvas->clipShader(shader, SkClipOp::kDifference);
519 
520         SkPath path;
521         path.moveTo(0.f, 128.f);
522         path.lineTo(128.f, 256.f);
523         path.lineTo(256.f, 128.f);
524         path.lineTo(128.f, 0.f);
525 
526         SkScalar d = 64.f * SK_ScalarSqrt2;
527         path.moveTo(128.f - d, 128.f - d);
528         path.lineTo(128.f - d, 128.f + d);
529         path.lineTo(128.f + d, 128.f + d);
530         path.lineTo(128.f + d, 128.f - d);
531         canvas->drawPath(path, paint);
532         canvas->restore();
533     }
534     // BR: Text
535     {
536         canvas->save();
537         canvas->translate(256, 256);
538         canvas->clipShader(shader, SkClipOp::kDifference);
539         SkFont font = SkFont(ToolUtils::DefaultPortableTypeface(), 64.f);
540         for (int y = 0; y < 4; ++y) {
541             canvas->drawString("Hello", 32.f, y * 64.f, font, paint);
542         }
543         canvas->restore();
544     }
545 }
546