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