1 /*
2 * Copyright 2019 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/SkColor.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPictureRecorder.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkTypes.h"
19 #include "include/effects/SkGradientShader.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "src/core/SkCanvasPriv.h"
22
23 #include <initializer_list>
24
25 // Make a noisy (with hard-edges) background, so we can see the effect of the blur
26 //
make_shader(SkScalar cx,SkScalar cy,SkScalar rad)27 static sk_sp<SkShader> make_shader(SkScalar cx, SkScalar cy, SkScalar rad) {
28 const SkColor colors[] = {
29 SK_ColorRED, SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN,
30 SK_ColorRED, SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN,
31 };
32 constexpr int count = SK_ARRAY_COUNT(colors);
33 SkScalar pos[count] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6 };
34 for (int i = 0; i < count; ++i) {
35 pos[i] *= 1.0f/6;
36 }
37 return SkGradientShader::MakeSweep(cx, cy, colors, pos, count);
38 }
39
do_draw(SkCanvas * canvas,bool useClip,bool useHintRect,SkScalar scaleFactor)40 static void do_draw(SkCanvas* canvas, bool useClip, bool useHintRect, SkScalar scaleFactor) {
41 SkAutoCanvasRestore acr(canvas, true);
42 canvas->clipRect({0, 0, 256, 256});
43
44 const SkScalar cx = 128, cy = 128, rad = 100;
45 SkPaint p;
46 p.setShader(make_shader(cx, cy, rad));
47 p.setAntiAlias(true);
48 canvas->drawCircle(cx, cy, rad, p);
49
50 // now setup a saveLayer that will pull in the backdrop and blur it
51 //
52 const SkRect r = {cx-50, cy-50, cx+50, cy+50};
53 const SkRect* drawrptr = useHintRect ? &r : nullptr;
54 const SkScalar sigma = 10;
55 if (useClip) {
56 canvas->clipRect(r);
57 }
58 // Using kClamp because kDecal, the default, produces transparency near the edge of the canvas's
59 // device.
60 auto blur = SkImageFilters::Blur(sigma, sigma, SkTileMode::kClamp, nullptr);
61 auto rec = SkCanvasPriv::ScaledBackdropLayer(drawrptr, nullptr, blur.get(), scaleFactor, 0);
62 canvas->saveLayer(rec);
63 // draw something inside, just to demonstrate that we don't blur the new contents,
64 // just the backdrop.
65 p.setColor(SK_ColorYELLOW);
66 p.setShader(nullptr);
67 canvas->drawCircle(cx, cy, 30, p);
68 canvas->restore();
69 }
70
71 /*
72 * Draws a 2x4 grid of sweep circles.
73 * - for a given row, each col should be identical (canvas, picture)
74 * - row:0 no-hint-rect no-clip-rect expect big blur (except inner circle)
75 * - row:1 no-hint-rect clip-rect expect small blur (except inner circle)
76 * - row:2 hint-rect no-clip-rect expect big blur (except inner circle)
77 * - row:3 hint-rect clip-rect expect small blur (except inner circle)
78 *
79 * The test is that backdrop effects should be independent of the hint-rect, but should
80 * respect the clip-rect.
81 */
82 DEF_SIMPLE_GM(backdrop_hintrect_clipping, canvas, 512, 1024) {
83 for (bool useHintRect : {false, true}) {
84 for (bool useClip : {false, true}) {
85 canvas->save();
86 do_draw(canvas, useClip, useHintRect, 1.0f);
87
88 SkPictureRecorder rec;
89 do_draw(rec.beginRecording(256, 256), useClip, useHintRect, 1.0f);
90 canvas->translate(256, 0);
91 canvas->drawPicture(rec.finishRecordingAsPicture());
92 canvas->restore();
93
94 canvas->translate(0, 256);
95 }
96 }
97 }
98
99 /*
100 * Draws a 3x4 grid of sweep circles.
101 * - for a given row, each col should be identical except that the intermediate scale factor used
102 * to evaluate the backdrop follows (1.0, 0.25, 0.1). Rows follow same pattern as above.
103 *
104 * The test is that backdrop effects should be independent of the hint-rect, should respect the
105 * clip rect, and be logically consistent with the reduced intermediate scaling.
106 */
107 DEF_SIMPLE_GM(backdrop_scalefactor, canvas, 768, 1024) {
108 for (bool useHintRect : {false, true}) {
109 for (bool useClip : {false, true}) {
110 canvas->save();
111 do_draw(canvas, useClip, useHintRect, 1.0f);
112 canvas->translate(256, 0);
113 do_draw(canvas, useClip, useHintRect, 0.25f);
114 canvas->translate(256, 0);
115 do_draw(canvas, useClip, useHintRect, 0.1f);
116 canvas->restore();
117
118 canvas->translate(0, 256);
119 }
120 }
121 }
122