Brian Salomon | 1c86b63 | 2020-12-11 12:36:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 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/SkPaint.h" |
| 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/effects/SkImageFilters.h" |
| 13 | |
| 14 | static void drawOne(SkCanvas* canvas, SkRect rect, float saveBorder, float sigma, SkColor c) { |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 15 | SkRect borderRect = rect.makeOutset(saveBorder, saveBorder); |
| 16 | |
Brian Salomon | 1c86b63 | 2020-12-11 12:36:01 -0500 | [diff] [blame] | 17 | SkPaint p; |
| 18 | p.setColor(c); |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 19 | p.setImageFilter( |
| 20 | SkImageFilters::Blur(sigma, sigma, |
| 21 | // The blur's input is forced to have transparent padding because 'borderRect' is outset |
| 22 | // from the non-transparent content ('rect') that's drawn into the layer. |
Michael Ludwig | d78aba2 | 2023-09-25 12:18:14 -0400 | [diff] [blame] | 23 | SkImageFilters::Crop(borderRect, SkTileMode::kClamp, nullptr), |
Michael Ludwig | 02eecda | 2023-08-23 12:05:53 -0400 | [diff] [blame] | 24 | // The blur's output crop visually won't affect the output because the transparent |
| 25 | // padding is blurred out by the edge of 3*sigma. |
| 26 | borderRect.makeOutset(3 * sigma, 3 * sigma))); |
Brian Salomon | 1c86b63 | 2020-12-11 12:36:01 -0500 | [diff] [blame] | 27 | p.setAntiAlias(true); |
| 28 | |
Brian Salomon | 1c86b63 | 2020-12-11 12:36:01 -0500 | [diff] [blame] | 29 | canvas->drawRect(rect, p); |
Brian Salomon | 1c86b63 | 2020-12-11 12:36:01 -0500 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | DEF_SIMPLE_GM(crbug_1156804, canvas, 250, 250) { |
| 33 | drawOne(canvas, SkRect::MakeXYWH( 64, 64, 25, 25), 1, 3, SK_ColorGREEN); |
| 34 | drawOne(canvas, SkRect::MakeXYWH(164, 64, 25, 25), 30, 3, SK_ColorGREEN); |
| 35 | // This one would draw incorrectly because the large sigma causes downscaling of the source |
| 36 | // and the one-pixel border would make the downscaled image not contain trans-black at the |
| 37 | // edges. Combined with the clamp mode on the blur filter it would "harden" the edge. |
| 38 | drawOne(canvas, SkRect::MakeXYWH( 64, 164, 25, 25), 1, 20, SK_ColorRED); |
| 39 | drawOne(canvas, SkRect::MakeXYWH(164, 164, 25, 25), 30, 20, SK_ColorGREEN); |
| 40 | } |