Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkImageFilter.h" |
| 12 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPictureRecorder.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/effects/SkGradientShader.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 20 | #include "include/effects/SkImageFilters.h" |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 21 | |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 22 | #include <initializer_list> |
| 23 | |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 24 | // Make a noisy (with hard-edges) background, so we can see the effect of the blur |
| 25 | // |
| 26 | static sk_sp<SkShader> make_shader(SkScalar cx, SkScalar cy, SkScalar rad) { |
| 27 | const SkColor colors[] = { |
| 28 | SK_ColorRED, SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN, |
| 29 | SK_ColorRED, SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN, |
| 30 | }; |
| 31 | constexpr int count = SK_ARRAY_COUNT(colors); |
| 32 | SkScalar pos[count] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6 }; |
| 33 | for (int i = 0; i < count; ++i) { |
| 34 | pos[i] *= 1.0f/6; |
| 35 | } |
| 36 | return SkGradientShader::MakeSweep(cx, cy, colors, pos, count); |
| 37 | } |
| 38 | |
| 39 | static void do_draw(SkCanvas* canvas, bool useClip, bool useHintRect) { |
| 40 | SkAutoCanvasRestore acr(canvas, true); |
| 41 | canvas->clipRect({0, 0, 256, 256}); |
| 42 | |
| 43 | const SkScalar cx = 128, cy = 128, rad = 100; |
| 44 | SkPaint p; |
| 45 | p.setShader(make_shader(cx, cy, rad)); |
| 46 | p.setAntiAlias(true); |
| 47 | canvas->drawCircle(cx, cy, rad, p); |
| 48 | |
| 49 | // now setup a saveLayer that will pull in the backdrop and blur it |
| 50 | // |
| 51 | const SkRect r = {cx-50, cy-50, cx+50, cy+50}; |
| 52 | const SkRect* drawrptr = useHintRect ? &r : nullptr; |
| 53 | const SkScalar sigma = 10; |
| 54 | if (useClip) { |
| 55 | canvas->clipRect(r); |
| 56 | } |
Brian Salomon | f686fa9 | 2020-05-04 10:33:38 -0400 | [diff] [blame] | 57 | // Using kClamp because kDecal, the default, produces transparency near the edge of the canvas's |
| 58 | // device. |
| 59 | auto blur = SkImageFilters::Blur(sigma, sigma, SkTileMode::kClamp, nullptr); |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 60 | auto rec = SkCanvas::SaveLayerRec(drawrptr, nullptr, blur.get(), 0); |
| 61 | canvas->saveLayer(rec); |
| 62 | // draw something inside, just to demonstrate that we don't blur the new contents, |
| 63 | // just the backdrop. |
| 64 | p.setColor(SK_ColorYELLOW); |
| 65 | p.setShader(nullptr); |
| 66 | canvas->drawCircle(cx, cy, 30, p); |
| 67 | canvas->restore(); |
| 68 | } |
| 69 | |
| 70 | /* |
Mike Klein | a705cb9 | 2019-05-14 12:33:40 -0500 | [diff] [blame] | 71 | * Draws a 2x4 grid of sweep circles. |
| 72 | * - for a given row, each col should be identical (canvas, picture) |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 73 | * - row:0 no-hint-rect no-clip-rect expect big blur (except inner circle) |
| 74 | * - row:1 no-hint-rect clip-rect expect small blur (except inner circle) |
| 75 | * - row:2 hint-rect no-clip-rect expect big blur (except inner circle) |
| 76 | * - row:3 hint-rect clip-rect expect small blur (except inner circle) |
| 77 | * |
| 78 | * The test is that backdrop effects should be independent of the hint-rect, but should |
| 79 | * respect the clip-rect. |
| 80 | */ |
Mike Klein | a705cb9 | 2019-05-14 12:33:40 -0500 | [diff] [blame] | 81 | DEF_SIMPLE_GM(backdrop_hintrect_clipping, canvas, 512, 1024) { |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 82 | for (bool useHintRect : {false, true}) { |
| 83 | for (bool useClip : {false, true}) { |
Mike Klein | a705cb9 | 2019-05-14 12:33:40 -0500 | [diff] [blame] | 84 | canvas->save(); |
| 85 | do_draw(canvas, useClip, useHintRect); |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 86 | |
Mike Klein | a705cb9 | 2019-05-14 12:33:40 -0500 | [diff] [blame] | 87 | SkPictureRecorder rec; |
| 88 | do_draw(rec.beginRecording(256, 256), useClip, useHintRect); |
| 89 | canvas->translate(256, 0); |
| 90 | canvas->drawPicture(rec.finishRecordingAsPicture()); |
| 91 | canvas->restore(); |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 92 | |
Mike Reed | e36d408 | 2019-02-22 16:58:22 -0500 | [diff] [blame] | 93 | canvas->translate(0, 256); |
| 94 | } |
| 95 | } |
| 96 | } |