Flutter Engine
The Flutter Engine
imagefilterscropped.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"
14#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
26#include "tools/ToolUtils.h"
28
29#include <utility>
30
31///////////////////////////////////////////////////////////////////////////////
32
33static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
35 paint.setImageFilter(std::move(imf));
36 paint.setColor(SK_ColorBLACK);
37 canvas->save();
38 canvas->clipRect(r);
39 canvas->drawPaint(paint);
40 canvas->restore();
41}
42
43static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
45 paint.setColor(SK_ColorMAGENTA);
46 paint.setImageFilter(std::move(imf));
47 paint.setAntiAlias(true);
48 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
49}
50
51static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
53 paint.setImageFilter(std::move(imf));
54 paint.setColor(SK_ColorGREEN);
55
58}
59
60static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
62
64 r.roundOut(&bounds);
65
66 auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()));
67 draw_path(surf->getCanvas(), r, nullptr);
68
69 paint.setImageFilter(std::move(imf));
70 surf->draw(canvas, 0, 0, SkSamplingOptions(), &paint);
71}
72
73///////////////////////////////////////////////////////////////////////////////
74
76public:
78
79protected:
80 SkString getName() const override { return SkString("imagefilterscropped"); }
81
82 SkISize getISize() override { return SkISize::Make(400, 960); }
83
86 auto canvas = surf->getCanvas();
87 SkPaint darkPaint;
88 darkPaint.setColor(0xFF404040);
89 SkPaint lightPaint;
90 lightPaint.setColor(0xFFA0A0A0);
91 for (int y = 0; y < 80; y += 16) {
92 for (int x = 0; x < 80; x += 16) {
93 canvas->save();
94 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
95 canvas->drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
96 canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
97 canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
98 canvas->drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
99 canvas->restore();
100 }
101 }
102 fCheckerboard = surf->makeImageSnapshot();
103 }
104
105 void draw_frame(SkCanvas* canvas, const SkRect& r) {
108 paint.setColor(SK_ColorRED);
109 canvas->drawRect(r, paint);
110 }
111
112 void onOnceBeforeDraw() override {
114 }
115
116 void onDraw(SkCanvas* canvas) override {
117 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
119 };
120
123 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
124 SkIRect bogusRect = SkIRect::MakeXYWH(-100, -100, 10, 10);
125
127
129
130 // These are composed with an outer erode along the other axis, so don't add a cropRect to
131 // them or it will interfere with the second filter evaluation.
132 sk_sp<SkImageFilter> erodeX(SkImageFilters::Erode(8, 0, nullptr));
133 sk_sp<SkImageFilter> erodeY(SkImageFilters::Erode(0, 8, nullptr));
134
135 sk_sp<SkImageFilter> filters[] = {
136 nullptr,
137 SkImageFilters::ColorFilter(cf, nullptr, &cropRect),
138 SkImageFilters::Blur(0.0f, 0.0f, nullptr, &cropRect),
139 SkImageFilters::Blur(1.0f, 1.0f, nullptr, &cropRect),
140 SkImageFilters::Blur(8.0f, 0.0f, nullptr, &cropRect),
141 SkImageFilters::Blur(0.0f, 8.0f, nullptr, &cropRect),
142 SkImageFilters::Blur(8.0f, 8.0f, nullptr, &cropRect),
143 SkImageFilters::Erode(1, 1, nullptr, &cropRect),
144 SkImageFilters::Erode(8, 0, std::move(erodeY), &cropRect),
145 SkImageFilters::Erode(0, 8, std::move(erodeX), &cropRect),
146 SkImageFilters::Erode(8, 8, nullptr, &cropRect),
147 SkImageFilters::Merge(nullptr, std::move(cfOffset), &cropRect),
148 SkImageFilters::Blur(8.0f, 8.0f, nullptr, &bogusRect),
149 SkImageFilters::ColorFilter(cf, nullptr, &bogusRect),
150 };
151
154 SkScalar DX = r.width() + MARGIN;
155 SkScalar DY = r.height() + MARGIN;
156
157 canvas->translate(MARGIN, MARGIN);
158 for (size_t j = 0; j < std::size(drawProc); ++j) {
159 canvas->save();
160 for (size_t i = 0; i < std::size(filters); ++i) {
162 canvas->drawImage(fCheckerboard, 0, 0);
163 drawProc[j](canvas, r, filters[i]);
164 canvas->translate(0, DY);
165 }
166 canvas->restore();
167 canvas->translate(DX, 0);
168 }
169 }
170
171private:
172 sk_sp<SkImage> fCheckerboard;
173 using INHERITED = GM;
174};
175
176///////////////////////////////////////////////////////////////////////////////
177
178DEF_GM( return new ImageFiltersCroppedGM; )
@ kSrcIn
r = s * da
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
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_ColorGREEN
Definition: SkColor.h:131
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void draw_frame(SkCanvas *canvas, const SkRect &r)
void onOnceBeforeDraw() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
SkISize getISize() override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
Definition: SkFont.h:35
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Erode(SkScalar radiusX, SkScalar radiusY, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Merge(sk_sp< SkImageFilter > *const filters, int count, 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 > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
void setColor(SkColor color)
Definition: SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition: SkTextUtils.h:34
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
#define MARGIN
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
static void draw_bitmap(SkCanvas *canvas, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_text(SkCanvas *canvas, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_path(SkCanvas *canvas, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_paint(SkCanvas *canvas, const SkRect &r, sk_sp< SkImageFilter > imf)
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkTypeface > DefaultPortableTypeface()
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
font
Font Metadata and Metrics.
SkSamplingOptions(SkFilterMode::kLinear))
SeparatedVector2 offset
Definition: SkRect.h:32
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
void roundOut(SkIRect *dst) const
Definition: SkRect.h:1241
constexpr float centerX() const
Definition: SkRect.h:776
constexpr float height() const
Definition: SkRect.h:769
constexpr float centerY() const
Definition: SkRect.h:785
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609