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