Flutter Engine
The Flutter Engine
imagefiltersbase.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"
15#include "include/core/SkFont.h"
20#include "include/core/SkRect.h"
23#include "include/core/SkSize.h"
32#include "tools/ToolUtils.h"
34
35#include <utility>
36
37class SkReadBuffer;
38
39static void draw_paint(SkCanvas* canvas, SkImage*, const SkRect& r, sk_sp<SkImageFilter> imf) {
41 paint.setImageFilter(std::move(imf));
42 paint.setColor(SK_ColorGREEN);
43 canvas->save();
44 canvas->clipRect(r);
45 canvas->drawPaint(paint);
46 canvas->restore();
47}
48
49static void draw_line(SkCanvas* canvas, SkImage*, const SkRect& r, sk_sp<SkImageFilter> imf) {
51 paint.setColor(SK_ColorBLUE);
52 paint.setImageFilter(imf);
53 paint.setStrokeWidth(r.width()/10);
54 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
55}
56
57static void draw_rect(SkCanvas* canvas, SkImage*, const SkRect& r, sk_sp<SkImageFilter> imf) {
59 paint.setColor(SK_ColorYELLOW);
60 paint.setImageFilter(imf);
61 SkRect rr(r);
62 rr.inset(r.width()/10, r.height()/10);
63 canvas->drawRect(rr, paint);
64}
65
66static void draw_path(SkCanvas* canvas, SkImage*, const SkRect& r, sk_sp<SkImageFilter> imf) {
68 paint.setColor(SK_ColorMAGENTA);
69 paint.setImageFilter(imf);
70 paint.setAntiAlias(true);
71 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
72}
73
74static void draw_text(SkCanvas* canvas, SkImage*, const SkRect& r, sk_sp<SkImageFilter> imf) {
76 paint.setImageFilter(imf);
77 paint.setColor(SK_ColorCYAN);
79 SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint,
81}
82
83static void draw_bitmap(SkCanvas* canvas, SkImage* i, const SkRect& r, sk_sp<SkImageFilter> imf) {
85 paint.setImageFilter(std::move(imf));
86
88 r.roundOut(&bounds);
89
90 SkBitmap bm;
91 bm.allocN32Pixels(bounds.width(), bounds.height());
93 SkCanvas c(bm);
94 draw_path(&c, i, r, nullptr);
95
96 canvas->drawImage(bm.asImage(), 0, 0, SkSamplingOptions(), &paint);
97}
98
99static void draw_patch(SkCanvas* canvas, SkImage*, const SkRect& r, sk_sp<SkImageFilter> imf) {
101 paint.setImageFilter(std::move(imf));
102
103 // The order of the colors and points is clockwise starting at upper-left corner.
104 static constexpr SkPoint gCubics[SkPatchUtils::kNumCtrlPts] = {
105 //top points
106 {100,100},{150,50},{250,150},{300,100},
107 //right points
108 {250,150},{350,250},
109 //bottom points
110 {300,300},{250,250},{150,350},{100,300},
111 //left points
112 {50,250},{150,150}
113 };
114
115 static constexpr SkColor colors[SkPatchUtils::kNumCorners] = {
117 };
118
119 SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
120 canvas->translate(-r.fLeft, -r.fTop);
121 canvas->scale(r.width() / 400.0, r.height() / 400.0);
122 canvas->drawPatch(gCubics, colors, /*texCoords=*/nullptr, SkBlendMode::kDst, paint);
123}
124
125static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRect& r,
127 const SkScalar rad = SkDegreesToRadians(15.0f);
128 SkRSXform xform = SkRSXform::Make(SkScalarCos(rad), SkScalarSin(rad), r.width() * 0.15f, 0);
129
131 paint.setImageFilter(std::move(imf));
132 paint.setAntiAlias(true);
134 canvas->drawAtlas(atlas, &xform, &r, /*colors=*/nullptr, /*count=*/1, SkBlendMode::kSrc,
135 sampling, /*cullRect=*/nullptr, &paint);
136}
137
138///////////////////////////////////////////////////////////////////////////////
139
141public:
143
144protected:
145 SkString getName() const override { return SkString("imagefiltersbase"); }
146
147 SkISize getISize() override { return SkISize::Make(700, 500); }
148
149 void draw_frame(SkCanvas* canvas, const SkRect& r) {
152 paint.setColor(SK_ColorRED);
153 canvas->drawRect(r, paint);
154 }
155
156 void onDraw(SkCanvas* canvas) override {
157 if (fAtlas == nullptr) {
158 fAtlas = create_atlas_image(canvas);
159 }
160
161 void (*drawProc[])(SkCanvas*, SkImage*, const SkRect&, sk_sp<SkImageFilter>) = {
165 };
166
168 sk_sp<SkImageFilter> filters[] = {
169 nullptr,
170 SkImageFilters::Offset(0.f, 0.f, nullptr), // "identity"
172 SkImageFilters::ColorFilter(std::move(cf), nullptr),
173 // The strange 0.29 value tickles an edge case where crop rect calculates
174 // a small border, but the blur really needs no border. This tickles
175 // an msan uninitialized value bug.
176 SkImageFilters::Blur(12.0f, 0.29f, nullptr),
177 SkImageFilters::DropShadow(10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
178 };
179
182 SkScalar DX = r.width() + MARGIN;
183 SkScalar DY = r.height() + MARGIN;
184
185 canvas->translate(MARGIN, MARGIN);
186 for (size_t i = 0; i < std::size(drawProc); ++i) {
187 canvas->save();
188 for (size_t j = 0; j < std::size(filters); ++j) {
189 drawProc[i](canvas, fAtlas.get(), r, filters[j]);
190
191 draw_frame(canvas, r);
192 canvas->translate(0, DY);
193 }
194 canvas->restore();
195 canvas->translate(DX, 0);
196 }
197 }
198
199private:
200 static sk_sp<SkImage> create_atlas_image(SkCanvas* canvas) {
201 static constexpr SkSize kSize = {64, 64};
202 SkImageInfo atlasInfo = SkImageInfo::MakeN32Premul(kSize.fWidth, kSize.fHeight);
203 sk_sp<SkSurface> atlasSurface(ToolUtils::makeSurface(canvas, atlasInfo));
204 SkCanvas* atlasCanvas = atlasSurface->getCanvas();
205
206 SkPaint atlasPaint;
207 atlasPaint.setColor(SK_ColorGRAY);
209 SkTextUtils::DrawString(atlasCanvas, "Atlas", kSize.fWidth * 0.5f, kSize.fHeight * 0.5f,
210 font, atlasPaint, SkTextUtils::kCenter_Align);
211 return atlasSurface->makeImageSnapshot();
212 }
213
214 sk_sp<SkImage> fAtlas;
215
216 using INHERITED = GM;
217};
218DEF_GM( return new ImageFiltersBaseGM; )
219
220///////////////////////////////////////////////////////////////////////////////
221
222/*
223 * Want to test combos of filter and LCD text, to be sure we disable LCD in the presence of
224 * a filter.
225 */
227 SkString fSuffix;
228public:
229 ImageFiltersTextBaseGM(const char suffix[]) : fSuffix(suffix) {}
230
231protected:
232 SkString getName() const override {
234 name.printf("%s_%s", "textfilter", fSuffix.c_str());
235 return name;
236 }
237
238 SkISize getISize() override { return SkISize::Make(512, 342); }
239
240 void drawWaterfall(SkCanvas* canvas, const SkPaint& paint) {
241 static const SkFont::Edging kEdgings[3] = {
245 };
247
248 SkAutoCanvasRestore acr(canvas, true);
249 for (SkFont::Edging edging : kEdgings) {
250 font.setEdging(edging);
251 canvas->drawString("Hamburgefon", 0, 0, font, paint);
252 canvas->translate(0, 40);
253 }
254 }
255
256 virtual void installFilter(SkPaint* paint) = 0;
257
258 void onDraw(SkCanvas* canvas) override {
259 canvas->translate(20, 40);
260
261 for (int doSaveLayer = 0; doSaveLayer <= 1; ++doSaveLayer) {
262 SkAutoCanvasRestore acr(canvas, true);
263 for (int useFilter = 0; useFilter <= 1; ++useFilter) {
264 SkAutoCanvasRestore acr2(canvas, true);
265
267 if (useFilter) {
268 this->installFilter(&paint);
269 }
270 if (doSaveLayer) {
271 canvas->saveLayer(nullptr, &paint);
272 paint.setImageFilter(nullptr);
273 }
274 this->drawWaterfall(canvas, paint);
275
276 acr2.restore();
277 canvas->translate(250, 0);
278 }
279 acr.restore();
280 canvas->translate(0, 200);
281 }
282 }
283
284private:
285 using INHERITED = GM;
286};
287
289public:
291
292 void installFilter(SkPaint* paint) override {
293 paint->setImageFilter(SkImageFilters::Blur(1.5f, 1.5f, nullptr));
294 }
295};
296DEF_GM( return new ImageFiltersText_IF; )
297
299public:
301
302 void installFilter(SkPaint* paint) override {
304 }
305};
306DEF_GM( return new ImageFiltersText_CF; )
@ kSrcIn
r = s * da
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SkDegreesToRadians(degrees)
Definition: SkScalar.h:77
#define SkScalarSin(radians)
Definition: SkScalar.h:45
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define SkScalarCos(radians)
Definition: SkScalar.h:46
SkISize getISize() override
void draw_frame(SkCanvas *canvas, const SkRect &r)
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
virtual void installFilter(SkPaint *paint)=0
void drawWaterfall(SkCanvas *canvas, const SkPaint &paint)
SkString getName() const override
ImageFiltersTextBaseGM(const char suffix[])
void onDraw(SkCanvas *canvas) override
void installFilter(SkPaint *paint) override
void installFilter(SkPaint *paint) override
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
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 drawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode mode, const SkPaint &paint)
Definition: SkCanvas.cpp:2529
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
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
int save()
Definition: SkCanvas.cpp:447
void drawAtlas(const SkImage *atlas, const SkRSXform xform[], const SkRect tex[], const SkColor colors[], int count, SkBlendMode mode, const SkSamplingOptions &sampling, const SkRect *cullRect, const SkPaint *paint)
Definition: SkCanvas.cpp:1810
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
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
Edging
Definition: SkFont.h:39
@ kAntiAlias
may have transparent pixels on glyph edges
@ kAlias
no transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > DropShadow(SkScalar dx, SkScalar dy, SkScalar sigmaX, SkScalar sigmaY, SkColor color, 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 > Empty()
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
const char * c_str() const
Definition: SkString.h:133
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
T * get() const
Definition: SkRefCnt.h:303
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, SkImage *i, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_text(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_patch(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_paint(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_rect(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_path(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_line(SkCanvas *canvas, SkImage *, const SkRect &r, sk_sp< SkImageFilter > imf)
static void draw_atlas(SkCanvas *canvas, SkImage *atlas, const SkRect &r, sk_sp< SkImageFilter > imf)
constexpr int kSize
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
Definition: ToolUtils.cpp:512
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
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.
const SkPoint gCubics[SkPatchUtils::kNumCtrlPts]
Definition: patch.cpp:81
static constexpr SkCubicResampler Mitchell()
Definition: SkRect.h:32
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 SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty)
Definition: SkRSXform.h:24
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
void inset(float dx, float dy)
Definition: SkRect.h:1060
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
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
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15
Definition: SkSize.h:52