Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
blurs.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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"
13#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
23#include "src/core/SkBlurMask.h"
24#include "tools/DecodeUtils.h"
25#include "tools/Resources.h"
26#include "tools/ToolUtils.h"
28
29DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, 0xFFDDDDDD) {
30 SkBlurStyle NONE = SkBlurStyle(-999);
31 const struct {
33 int fCx, fCy;
34 } gRecs[] = {
35 { NONE, 0, 0 },
36 { kInner_SkBlurStyle, -1, 0 },
37 { kNormal_SkBlurStyle, 0, 1 },
38 { kSolid_SkBlurStyle, 0, -1 },
39 { kOuter_SkBlurStyle, 1, 0 },
40 };
41
43 paint.setAntiAlias(true);
44 paint.setColor(SK_ColorBLUE);
45
46 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
47
48 for (size_t i = 0; i < std::size(gRecs); i++) {
49 if (gRecs[i].fStyle != NONE) {
50 paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle,
52 } else {
53 paint.setMaskFilter(nullptr);
54 }
55 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
56 SkIntToScalar(200 + gRecs[i].fCy*100),
57 SkIntToScalar(50),
58 paint);
59 }
60 // draw text
61 {
67 paint.setColor(SK_ColorBLACK);
68 canvas->drawString("Hamburgefons Style", x, y, font, paint);
69 canvas->drawString("Hamburgefons Style",
70 x, y + SkIntToScalar(50), font, paint);
71 paint.setMaskFilter(nullptr);
72 paint.setColor(SK_ColorWHITE);
73 x -= SkIntToScalar(2);
74 y -= SkIntToScalar(2);
75 canvas->drawString("Hamburgefons Style", x, y, font, paint);
76 }
77}
78
79//////////////////////////////////////////////////////////////////////////////////////////////
80
81// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
82// and possibly cached.
83//
84// in particular, we want to notice that the 2nd rect draws slightly differently, since it
85// is translated a fractional amount.
86//
87DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) {
89
91
92 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
93 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
96 .detach();
97
98 canvas->drawPath(path, paint);
99 // important to translate by a factional amount to exercise a different "phase"
100 // of the same path w.r.t. the pixel grid
101 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
102 canvas->translate(dx, 0);
103 canvas->drawPath(path, paint);
104}
105
106DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) {
109
110 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
111 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
114 .detach();
115 canvas->drawPath(path, paint);
116
117 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
118 canvas->translate(dx, 0);
119 canvas->drawPath(path, paint);
120
121 // Translate to outside of clip bounds.
122 canvas->translate(-dx, 0);
123 canvas->translate(-30, -150);
124 canvas->drawPath(path, paint);
125}
126
127DEF_SIMPLE_GM(BlurDrawImage, canvas, 256, 256) {
130 canvas->clear(0xFF88FF88);
131 if (auto image = ToolUtils::GetResourceAsImage("images/mandrill_512_q075.jpg")) {
132 canvas->scale(0.25, 0.25);
133 canvas->drawImage(image, 256, 256, SkSamplingOptions(), &paint);
134 }
135}
136
137DEF_SIMPLE_GM(BlurBigSigma, canvas, 1024, 1024) {
138 SkPaint layerPaint, p;
139
140 p.setImageFilter(SkImageFilters::Blur(500, 500, nullptr));
141
142 canvas->drawRect(SkRect::MakeWH(700, 800), p);
143}
144
145DEF_SIMPLE_GM(BlurSmallSigma, canvas, 512, 256) {
146 {
147 // Normal sigma on x-axis, a small but non-zero sigma on y-axis that should
148 // be treated as identity.
150 paint.setImageFilter(SkImageFilters::Blur(16.f, 1e-5f, nullptr));
151 canvas->drawRect(SkRect::MakeLTRB(64, 64, 192, 192), paint);
152 }
153
154 {
155 // Small sigma on both axes, should be treated as identity and no red should show
157 paint.setColor(SK_ColorRED);
158 SkRect rect = SkRect::MakeLTRB(320, 64, 448, 192);
159 canvas->drawRect(rect, paint);
160 paint.setColor(SK_ColorBLACK);
161 paint.setImageFilter(SkImageFilters::Blur(1e-5f, 1e-5f, nullptr));
162 canvas->drawRect(rect, paint);
163 }
164}
165
166// Modeled after crbug.com/1500021, incorporates manual tiling to emulate Chrome's raster tiles
167// or the tiled rendering mode in Viewer.
168DEF_SIMPLE_GM(TiledBlurBigSigma, canvas, 1024, 768) {
169 static constexpr int kTileWidth = 342;
170 static constexpr int kTileHeight = 256;
171
172 SkM44 origCTM = canvas->getLocalToDevice();
173
174 for (int y = 0; y < 3; ++y) {
175 for (int x = 0; x < 3; ++x) {
176 // Define tiled grid in the canvas pixel space
177 canvas->save();
178 canvas->resetMatrix();
179
180 canvas->clipIRect(SkIRect::MakeXYWH(x*kTileWidth, y*kTileHeight,
182 canvas->setMatrix(origCTM);
183
187 std::move(flood), nullptr);
188 auto blur = SkImageFilters::Blur(206.f, 206.f, std::move(blend));
189
190 SkPaint p;
191 p.setImageFilter(std::move(blur));
192
193 canvas->clipRect({0, 0, 1970, 1223});
194 canvas->saveLayer(nullptr, &p);
195 SkPaint fill;
197 canvas->drawCircle(600, 150, 350, fill);
198 canvas->restore();
199 canvas->restore();
200 }
201 }
202}
SkStrokeRec::Style fStyle
@ kSrcOver
r = s + (1-sa)*d
SkBlurStyle
Definition SkBlurTypes.h:11
@ kOuter_SkBlurStyle
nothing inside, fuzzy outside
Definition SkBlurTypes.h:14
@ kSolid_SkBlurStyle
solid inside, fuzzy outside
Definition SkBlurTypes.h:13
@ kInner_SkBlurStyle
fuzzy inside, nothing outside
Definition SkBlurTypes.h:15
@ kNormal_SkBlurStyle
fuzzy inside and outside
Definition SkBlurTypes.h:12
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarRoundToScalar(x)
Definition SkScalar.h:32
static SkScalar SK_SPI ConvertRadiusToSigma(SkScalar radius)
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blend(SkBlendMode mode, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground=nullptr, const CropRect &cropRect={})
Definition SkM44.h:150
static sk_sp< SkMaskFilter > MakeBlur(SkBlurStyle style, SkScalar sigma, bool respectCTM=true)
void setColor(SkColor color)
Definition SkPaint.cpp:119
SkPathBuilder & addRect(const SkRect &, SkPathDirection, unsigned startIndex)
const Paint & paint
static constexpr SkScalar kTileHeight
static constexpr SkScalar kTileWidth
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM_BG(NAME, CANVAS, W, H, BGCOLOR)
Definition gm.h:52
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static SkColor blend(SkColor dst, SkColor src, void(*mode)(float, float, float, float *, float *, float *))
Definition hsl.cpp:142
double y
double x
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646