Debian Codegraph @

gm/crbug_1174354.cpp

raw
1 /*
2 * Copyright 2021 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/SkGradient.h"
13 #include "include/effects/SkImageFilters.h"
14
15 static void draw_bg_blur(SkCanvas* canvas, SkIRect rect, float sigma) {
16 // First create an intermediate layer that has an opaque area that we blur with transparency
17 // all around it. We want to make sure the transparency doesn't affect the blur of the opaque
18 // content.
19 auto outsetRect = SkRect::Make(rect).makeOutset(10, 10);
20 canvas->saveLayer(outsetRect, nullptr);
21 const SkColor4f colors[] = {SkColors::kRed, SkColors::kBlue, SkColors::kGreen};
22 float cx = (rect.left() + rect.right() )/2.f;
23 float cy = (rect.top() + rect.bottom())/2.f;
24 auto g = SkShaders::SweepGradient({cx, cy}, 0, 45,
25 {{colors, {}, SkTileMode::kMirror}, {}});
26 SkPaint paint;
27 paint.setShader(std::move(g));
28 canvas->drawRect(SkRect::Make(rect), paint);
29 // Now do the bg-blur into another save-layer that should only read the opaque region.
30 SkCanvas::SaveLayerRec rec;
31 auto blur = SkImageFilters::Blur(sigma, sigma, SkTileMode::kClamp, nullptr, rect);
32 rec.fBounds = &outsetRect;
33 rec.fBackdrop = blur.get();
34 canvas->saveLayer(rec);
35 canvas->restore();
36 canvas->restore();
37 }
38
39 DEF_SIMPLE_GM(crbug_1174354, canvas, 70, 250) {
40 // The initial fix for crbug.com/1156805 had an issue where the border added to the downsampled
41 // texture that was used as input to the blur could actually bring in transparency when there
42 // wasn't any within the original src bounds. It was populated the border using a filtering draw
43 // from the full res source that could read from outside the pixels surrounding the original src
44 // bounds.
45 draw_bg_blur(canvas, SkIRect::MakeXYWH(10, 10, 50, 50), 5);
46 draw_bg_blur(canvas, SkIRect::MakeXYWH(10, 70, 50, 50), 15);
47 draw_bg_blur(canvas, SkIRect::MakeXYWH(10, 130, 50, 50), 30);
48 draw_bg_blur(canvas, SkIRect::MakeXYWH(10, 190, 50, 50), 70);
49 }