• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
294 namespace {
295 
296 // Where is canvas->concat(persp) called relative to the clipShader calls.
297 enum 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.
304 enum ClipOrder {
305     kClipImageFirst,
306     kClipGradientFirst,
307 
308     kDoesntMatter = kClipImageFirst
309 };
310 // Which shaders have perspective applied as a local matrix.
311 enum LocalMatrix {
312     kNoLocalMat,
313     kImageWithLocalMat,
314     kGradientWithLocalMat,
315     kBothWithLocalMat
316 };
317 struct Config {
318     ConcatPerspective fConcat;
319     ClipOrder         fOrder;
320     LocalMatrix       fLM;
321 };
322 
draw_banner(SkCanvas * canvas,Config config)323 static 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 
345     static const SkFont kFont(ToolUtils::DefaultPortableTypeface(), 12);
346     canvas->drawString(banner.c_str(), 20.f, -30.f, kFont, SkPaint());
347 };
348 
349 }  // namespace
350 
351 DEF_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
370     auto img = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
371     // 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 
__anonc06578f00302(Config config) 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,
399                                          SkSamplingOptions(), imageLM ? perspScale : scale);
400 
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
423         canvas->clipIRect(img->bounds());
424         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 
435     for (size_t i = 0; i < std::size(matches); ++i) {
436         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 }
446 
447 DEF_SIMPLE_GM(clip_shader_difference, canvas, 512, 512) {
448     auto image = ToolUtils::GetResourceAsImage("images/yellow_rose.png");
449     canvas->clear(SK_ColorGRAY);
450 
451     SkRect rect = SkRect::MakeWH(256, 256);
452     SkMatrix local = SkMatrix::RectToRect(SkRect::MakeWH(image->width(), image->height()),
453                                           SkRect::MakeWH(64, 64));
454     auto shader = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
455                                     SkSamplingOptions(), &local);
456 
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);
502         SkFont font = SkFont(ToolUtils::DefaultPortableTypeface(), 64.f);
503         for (int y = 0; y < 4; ++y) {
504             canvas->drawString("Hello", 32.f, y * 64.f, font, paint);
505         }
506         canvas->restore();
507     }
508 }
509