Flutter Engine
The Flutter Engine
Classes | Functions
imagefilters.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageFilter.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMaskFilter.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRRect.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkColorMatrix.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkHighContrastFilter.h"
#include "include/effects/SkImageFilters.h"
#include "include/effects/SkShaderMaskFilter.h"
#include "include/gpu/GrDirectContext.h"
#include "include/gpu/ganesh/SkImageGanesh.h"
#include "src/core/SkCanvasPriv.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include <utility>

Go to the source code of this file.

Classes

class  SaveLayerWithBackdropGM
 

Functions

static void do_draw (SkCanvas *canvas, SkBlendMode mode, sk_sp< SkImageFilter > imf)
 
 DEF_SIMPLE_GM (imagefilters_xfermodes, canvas, 480, 480)
 
static sk_sp< SkImagemake_image (SkCanvas *canvas)
 
 DEF_SIMPLE_GM (fast_slow_blurimagefilter, canvas, 620, 260)
 
static void draw_set (SkCanvas *canvas, sk_sp< SkImageFilter > filters[], int count)
 
 DEF_SIMPLE_GM (imagefilters_effect_order, canvas, 512, 512)
 
 DEF_SIMPLE_GM (multiple_filters, canvas, 415, 210)
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/4]

DEF_SIMPLE_GM ( fast_slow_blurimagefilter  ,
canvas  ,
620  ,
260   
)

Definition at line 105 of file imagefilters.cpp.

105 {
107 const SkRect r = SkRect::MakeIWH(image->width(), image->height());
108
109 canvas->translate(10, 10);
110 for (SkScalar sigma = 8; sigma <= 128; sigma *= 2) {
112 paint.setImageFilter(SkImageFilters::Blur(sigma, sigma, nullptr));
113
114 canvas->save();
115 // we outset the clip by 1, to fall out of the fast-case in drawImage
116 // i.e. the clip is larger than the image
117 for (SkScalar outset = 0; outset <= 1; ++outset) {
118 canvas->save();
119 canvas->clipRect(r.makeOutset(outset, outset));
120 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
121 canvas->restore();
122 canvas->translate(0, r.height() + 20);
123 }
124 canvas->restore();
125 canvas->translate(r.width() + 20, 0);
126 }
127}
static const int outset
Definition: BlurTest.cpp:58
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
static sk_sp< SkImage > make_image(SkCanvas *canvas)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SkSamplingOptions(SkFilterMode::kLinear))
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
SkRect makeOutset(float dx, float dy) const
Definition: SkRect.h:1002
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762

◆ DEF_SIMPLE_GM() [2/4]

DEF_SIMPLE_GM ( imagefilters_effect_order  ,
canvas  ,
512  ,
512   
)

Definition at line 204 of file imagefilters.cpp.

204 {
205 sk_sp<SkImage> image(ToolUtils::GetResourceAsImage("images/mandrill_256.png"));
206 auto direct = GrAsDirectContext(canvas->recordingContext());
207 if (direct) {
208 if (sk_sp<SkImage> gpuImage = SkImages::TextureFromImage(direct, image)) {
209 image = std::move(gpuImage);
210 }
211 }
212
213 SkISize kernelSize = SkISize::Make(3, 3);
214 SkIPoint kernelOffset = SkIPoint::Make(1, 1);
215 // A Laplacian edge detector, ie https://en.wikipedia.org/wiki/Kernel_(image_processing)
216 SkScalar kernel[9] = {-1.f, -1.f, -1.f,
217 -1.f, 8.f, -1.f,
218 -1.f, -1.f, -1.f};
219 auto edgeDetector = SkImageFilters::MatrixConvolution(
220 kernelSize, kernel, 1.f, 0.f, kernelOffset, SkTileMode::kClamp, false, nullptr);
221 // This uses the high contrast filter because it resembles a pre-processing step you may perform
222 // prior to edge detection. The specifics of the high contrast algorithm don't matter for the GM
223 auto edgeAmplify = SkHighContrastFilter::Make(
225
226 SkPaint testCFPaint;
227 testCFPaint.setColorFilter(edgeAmplify);
228 testCFPaint.setImageFilter(edgeDetector);
229
230 // The expected result is color filter then image filter, so represent this explicitly in the
231 // image filter graph.
232 SkPaint expectedCFPaint;
233 expectedCFPaint.setImageFilter(SkImageFilters::Compose(edgeDetector,
234 SkImageFilters::ColorFilter(edgeAmplify, nullptr)));
235
236 // Draw the image twice (expected on the left, test on the right that should match)
237 SkRect crop = SkRect::Make(image->bounds());
238 canvas->save();
239 canvas->clipRect(crop);
240 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedCFPaint); // Filter applied by draw's SkPaint
241 canvas->restore();
242
243 canvas->save();
244 canvas->translate(image->width(), 0);
245 canvas->clipRect(crop);
246 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testCFPaint);
247 canvas->restore();
248
249 // Now test mask filters. These should be run before the image filter, and thus have the same
250 // effect as multiplying by an alpha mask.
251
252 // This mask filter pokes a hole in the center of the image
253 static constexpr SkColor kAlphas[] = { SK_ColorBLACK, SK_ColorTRANSPARENT };
254 static constexpr SkScalar kPos[] = { 0.4f, 0.9f };
256 {128.f, 128.f}, 128.f, kAlphas, kPos, 2, SkTileMode::kClamp);
257 sk_sp<SkMaskFilter> maskFilter = SkShaderMaskFilter::Make(alphaMaskShader);
258
259 // If edge detector sees the mask filter, it'll have alpha and then blend with the original
260 // image; otherwise the mask filter will apply late (incorrectly) and none of the original
261 // image will be visible.
264
265 SkPaint testMaskPaint;
266 testMaskPaint.setMaskFilter(maskFilter);
267 testMaskPaint.setImageFilter(edgeBlend);
268
269 SkPaint expectedMaskPaint;
270 expectedMaskPaint.setImageFilter(SkImageFilters::Compose(edgeBlend,
272 SkImageFilters::Shader(alphaMaskShader))));
273
274 canvas->save();
275 canvas->translate(0, image->height());
276 canvas->clipRect(crop);
277 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &expectedMaskPaint);
278 canvas->restore();
279
280 canvas->save();
281 canvas->translate(image->width(), image->height());
282 canvas->clipRect(crop);
283 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &testMaskPaint);
284 canvas->restore();
285}
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kSrcOver
r = s + (1-sa)*d
@ kSrcIn
r = s * da
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkImageFilter > MatrixConvolution(const SkISize &kernelSize, const SkScalar kernel[], SkScalar gain, SkScalar bias, const SkIPoint &kernelOffset, SkTileMode tileMode, bool convolveAlpha, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Compose(sk_sp< SkImageFilter > outer, sk_sp< SkImageFilter > inner)
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > Blend(SkBlendMode mode, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground=nullptr, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Shader(sk_sp< SkShader > shader, const CropRect &cropRect={})
SkIRect bounds() const
Definition: SkImage.h:303
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
static sk_sp< SkMaskFilter > Make(sk_sp< SkShader > shader)
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
static sk_sp< SkColorFilter > Make(const SkHighContrastConfig &config)
static constexpr SkIPoint Make(int32_t x, int32_t y)
Definition: SkPoint_impl.h:38
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669

◆ DEF_SIMPLE_GM() [3/4]

DEF_SIMPLE_GM ( imagefilters_xfermodes  ,
canvas  ,
480  ,
480   
)

Definition at line 71 of file imagefilters.cpp.

71 {
72 canvas->translate(10, 10);
73
74 // just need an imagefilter to trigger the code-path (which creates a tmp layer)
77 nullptr));
78
79 const SkBlendMode modes[] = {
81 };
82
83 for (size_t i = 0; i < std::size(modes); ++i) {
84 canvas->save();
85 do_draw(canvas, modes[i], nullptr);
86 canvas->translate(240, 0);
87 do_draw(canvas, modes[i], imf);
88 canvas->restore();
89
90 canvas->translate(0, 240);
91 }
92}
SkBlendMode
Definition: SkBlendMode.h:38
@ kDstIn
r = d * sa
@ kSrcATop
r = s*da + d*(1-sa)
static sk_sp< SkImageFilter > MatrixTransform(const SkMatrix &matrix, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
static void do_draw(SkCanvas *canvas, SkBlendMode mode, sk_sp< SkImageFilter > imf)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ DEF_SIMPLE_GM() [4/4]

DEF_SIMPLE_GM ( multiple_filters  ,
canvas  ,
415  ,
210   
)

Definition at line 287 of file imagefilters.cpp.

287 {
289 canvas->translate(5, 5);
290
291 auto drawFilteredLayer = [=](SkCanvas::FilterSpan filters) {
292 SkPaint restorePaint;
293 restorePaint.setAlphaf(0.5f);
295 /*bounds=*/nullptr,
296 &restorePaint,
297 /*backdrop=*/nullptr,
298 /*backdropScale=*/1,
299 /*saveLayerFlags=*/0,
300 filters);
301 canvas->save();
302 canvas->clipRect({0, 0, 200, 200});
303 canvas->saveLayer(rec);
304
307 paint.setStrokeWidth(20);
308 paint.setColor(SK_ColorGREEN);
309 canvas->drawCircle(100, 100, 70, paint);
310
311 canvas->restore();
312 canvas->restore();
313 canvas->translate(205, 0);
314 };
315
316 {
317 // Test with two non-null filters that each change bounds in a different way:
318 sk_sp<SkImageFilter> filters[2] = {SkImageFilters::Dilate(5, 5, nullptr),
319 SkImageFilters::Erode(5, 5, nullptr)};
320 drawFilteredLayer(filters);
321 }
322
323 {
324 // Test with one null filter, to more closely mimic the canvas2D layers use-case:
325 sk_sp<SkImageFilter> filters[2] = {
326 SkImageFilters::DropShadowOnly(7, 7, 5, 5, SK_ColorBLUE, nullptr), nullptr};
327 drawFilteredLayer(filters);
328 }
329}
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
static SkCanvas::SaveLayerRec ScaledBackdropLayer(const SkRect *bounds, const SkPaint *paint, const SkImageFilter *backdrop, SkScalar backdropScale, SkCanvas::SaveLayerFlags saveLayerFlags, SkCanvas::FilterSpan filters={})
Definition: SkCanvasPriv.h:75
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > DropShadowOnly(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Dilate(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
void setAlphaf(float a)
Definition: SkPaint.cpp:130
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:174

◆ do_draw()

static void do_draw ( SkCanvas canvas,
SkBlendMode  mode,
sk_sp< SkImageFilter imf 
)
static

Test drawing a primitive w/ an imagefilter (in this case, just matrix w/ identity) to see that we apply the xfermode after the image has been created and filtered, and not during the creation step (i.e. before it is filtered).

see https://bug.skia.org/3741

Definition at line 46 of file imagefilters.cpp.

46 {
47 SkAutoCanvasRestore acr(canvas, true);
48 canvas->clipRect(SkRect::MakeWH(220, 220));
49
50 // want to force a layer, so modes like DstIn can combine meaningfully, but the final
51 // image can still be shown against our default (opaque) background. non-opaque GMs
52 // are a lot more trouble to compare/triage.
53 canvas->saveLayer(nullptr, nullptr);
54 canvas->drawColor(SK_ColorGREEN);
55
57 paint.setAntiAlias(true);
58
59 SkRect r0 = SkRect::MakeXYWH(10, 60, 200, 100);
60 SkRect r1 = SkRect::MakeXYWH(60, 10, 100, 200);
61
62 paint.setColor(SK_ColorRED);
63 canvas->drawOval(r0, paint);
64
65 paint.setColor(0x660000FF);
66 paint.setImageFilter(std::move(imf));
67 paint.setBlendMode(mode);
68 canvas->drawOval(r1, paint);
69}
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
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

◆ draw_set()

static void draw_set ( SkCanvas canvas,
sk_sp< SkImageFilter filters[],
int  count 
)
static

Definition at line 131 of file imagefilters.cpp.

131 {
132 const SkRect r = SkRect::MakeXYWH(30, 30, 200, 200);
133 const SkScalar offset = 250;
134 SkScalar dx = 0, dy = 0;
135
136 for (int i = 0; i < count; ++i) {
137 canvas->save();
138 SkRRect rr = SkRRect::MakeRectXY(r.makeOffset(dx, dy), 20, 20);
139 canvas->clipRRect(rr, true);
140 canvas->saveLayer(SkCanvas::SaveLayerRec(&rr.getBounds(), nullptr, filters[i].get(), 0));
141 canvas->drawColor(0x40FFFFFF);
142 canvas->restore();
143 canvas->restore();
144
145 if (0 == dx) {
146 dx = offset;
147 } else {
148 dx = 0;
149 dy = offset;
150 }
151 }
152}
int count
Definition: FontMgrTest.cpp:50
void restore()
Definition: SkCanvas.cpp:461
int save()
Definition: SkCanvas.cpp:447
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1439
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition: SkRRect.h:180
const SkRect & getBounds() const
Definition: SkRRect.h:279
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
SeparatedVector2 offset
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965

◆ make_image()

static sk_sp< SkImage > make_image ( SkCanvas canvas)
static

Definition at line 94 of file imagefilters.cpp.

94 {
96 auto surface(ToolUtils::makeSurface(canvas, info));
97 surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 25, 50, 50), SkPaint());
98 return surface->makeImageSnapshot();
99}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
static SkImageInfo MakeS32(int width, int height, SkAlphaType at)