Debian Codegraph @

gm/complexclip.cpp

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