Flutter Engine
The Flutter Engine
filterfastbounds.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
16#include "include/core/SkPath.h"
21#include "include/core/SkRect.h"
24#include "include/core/SkSize.h"
30
31#include <utility>
32
33using namespace skia_private;
34
35namespace skiagm {
36
37// Each method of this type must draw its geometry inside 'r' using 'p'
38typedef void(*drawMth)(SkCanvas* canvas, const SkRect& r, const SkPaint& p);
39
40static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
41 canvas->drawRect(r, p);
42}
43
44static void draw_oval(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
45 canvas->drawOval(r, p);
46}
47
48static void draw_rrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
49 SkScalar xRad = r.width() / 4.0f;
50 SkScalar yRad = r.height() / 4.0f;
51
52 SkRRect rr;
53 rr.setRectXY(r, xRad, yRad);
54 canvas->drawRRect(rr, p);
55}
56
57static void draw_drrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
58 SkScalar xRad = r.width() / 4.0f;
59 SkScalar yRad = r.height() / 4.0f;
60
61 SkRRect outer;
62 outer.setRectXY(r, xRad, yRad);
63 SkRRect inner = outer;
64 inner.inset(xRad, yRad);
65 canvas->drawDRRect(outer, inner, p);
66}
67
68static void draw_path(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
70 {r.fLeft, r.fTop},
71 {r.fLeft, r.fBottom},
72 {r.fRight, r.fBottom},
73 }, true), p);
74}
75
76static void draw_points(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
77 SkPoint pts0[2] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
78 SkPoint pts1[2] = { { r.fLeft, r.fBottom }, { r.fRight, r.fTop } };
79
80 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts0, p);
81 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts1, p);
82}
83
84static void draw_bitmap(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
85 SkBitmap bm;
86
87 bm.allocN32Pixels(64, 64);
88 SkCanvas temp(bm);
90
91 canvas->drawImageRect(bm.asImage(), r, SkSamplingOptions(), &p);
92}
93
94constexpr drawMth gDrawMthds[] = {
96};
97
98static void add_paint(TArray<SkPaint>* paints, sk_sp<SkImageFilter> filter) {
99 SkPaint& p = paints->push_back();
100 p.setImageFilter(std::move(filter));
101 SkASSERT(p.canComputeFastBounds());
102}
103
104// Create a selection of imagefilter-based paints to test
106 {
108 scale.setScale(2.0f, 2.0f);
109
110 sk_sp<SkImageFilter> scaleMIF(
112 source));
113
114 add_paint(paints, std::move(scaleMIF));
115 }
116
117 {
118 SkMatrix rot;
119 rot.setRotate(-33.3f);
120
123
124 add_paint(paints, std::move(rotMIF));
125 }
126
127 {
128 SkRect src = SkRect::MakeXYWH(20, 20, 10, 10);
129 SkRect dst = SkRect::MakeXYWH(30, 30, 30, 30);
131
132 add_paint(paints, std::move(tileIF));
133 }
134
135 {
137 SkImageFilters::DropShadow(10.0f, 10.0f, 3.0f, 3.0f, SK_ColorRED, source);
138
139 add_paint(paints, std::move(dsif));
140 }
141
142 {
144 SkImageFilters::DropShadowOnly(27.0f, 27.0f, 3.0f, 3.0f, SK_ColorRED, source);
145
146 add_paint(paints, std::move(dsif));
147 }
148
149 add_paint(paints, SkImageFilters::Blur(3, 3, source));
150 add_paint(paints, SkImageFilters::Offset(15, 15, source));
151}
152
153// This GM visualizes the fast bounds for various combinations of geometry
154// and image filter
156public:
158 this->setBGColor(0xFFCCCCCC);
159 }
160
161protected:
162 inline static constexpr int kTileWidth = 100;
163 inline static constexpr int kTileHeight = 100;
164 inline static constexpr int kNumVertTiles = 7;
165 inline static constexpr int kNumXtraCols = 2;
166
167 SkString getName() const override { return SkString("filterfastbounds"); }
168
169 SkISize getISize() override {
172 }
173
174 static void draw_geom_with_paint(drawMth draw, const SkIPoint& off,
175 SkCanvas* canvas, const SkPaint& p) {
176 SkPaint redStroked;
177 redStroked.setColor(SK_ColorRED);
179
180 SkPaint blueStroked;
181 blueStroked.setColor(SK_ColorBLUE);
182 blueStroked.setStyle(SkPaint::kStroke_Style);
183
184 const SkRect r = SkRect::MakeLTRB(20, 20, 30, 30);
185 SkRect storage;
186
187 canvas->save();
188 canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY));
189 canvas->scale(1.5f, 1.5f);
190
191 const SkRect& fastBound = p.computeFastBounds(r, &storage);
192
193 canvas->save();
194 canvas->clipRect(fastBound);
195 (*draw)(canvas, r, p);
196 canvas->restore();
197
198 canvas->drawRect(r, redStroked);
199 canvas->drawRect(fastBound, blueStroked);
200 canvas->restore();
201 }
202
203 static void draw_savelayer_with_paint(const SkIPoint& off,
204 SkCanvas* canvas,
205 const SkPaint& p) {
206 SkPaint redStroked;
207 redStroked.setColor(SK_ColorRED);
209
210 SkPaint blueStroked;
211 blueStroked.setColor(SK_ColorBLUE);
212 blueStroked.setStyle(SkPaint::kStroke_Style);
213
214 const SkRect bounds = SkRect::MakeWH(10, 10);
215 SkRect storage;
216
217 canvas->save();
218 canvas->translate(30, 30);
219 canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY));
220 canvas->scale(1.5f, 1.5f);
221
222 const SkRect& fastBound = p.computeFastBounds(bounds, &storage);
223
224 canvas->saveLayer(&fastBound, &p);
225 canvas->restore();
226
227 canvas->drawRect(bounds, redStroked);
228 canvas->drawRect(fastBound, blueStroked);
229 canvas->restore();
230 }
231
232 void onDraw(SkCanvas* canvas) override {
233
234 SkPaint blackFill;
235
236 //-----------
237 // Normal paints (no source)
238 TArray<SkPaint> paints;
239 create_paints(&paints, nullptr);
240
241 //-----------
242 // Paints with a PictureImageFilter as a source
244
245 {
247
248 SkCanvas* c = rec.beginRecording(10, 10);
249 c->drawRect(SkRect::MakeWH(10, 10), blackFill);
250 pic = rec.finishRecordingAsPicture();
251 }
252
253 TArray<SkPaint> pifPaints;
254 create_paints(&pifPaints, SkImageFilters::Picture(pic));
255
256 //-----------
257 // Paints with a SkImageSource as a source
258
260 {
261 SkPaint p;
262 SkCanvas* temp = surface->getCanvas();
263 temp->clear(SK_ColorYELLOW);
264 p.setColor(SK_ColorBLUE);
265 temp->drawRect(SkRect::MakeLTRB(5, 5, 10, 10), p);
266 p.setColor(SK_ColorGREEN);
267 temp->drawRect(SkRect::MakeLTRB(5, 0, 10, 5), p);
268 }
269
270 sk_sp<SkImage> image(surface->makeImageSnapshot());
271 sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(std::move(image),
273 TArray<SkPaint> bmsPaints;
274 create_paints(&bmsPaints, std::move(imageSource));
275
276 //-----------
277 SkASSERT(paints.size() == kNumVertTiles);
278 SkASSERT(paints.size() == pifPaints.size());
279 SkASSERT(paints.size() == bmsPaints.size());
280
281 // horizontal separators
282 for (int i = 1; i < paints.size(); ++i) {
283 canvas->drawLine(0,
287 blackFill);
288 }
289 // vertical separators
290 for (int i = 0; i < (int)std::size(gDrawMthds) + kNumXtraCols; ++i) {
292 0,
294 SkIntToScalar(paints.size() * kTileWidth),
295 blackFill);
296 }
297
298 // A column of saveLayers with PictureImageFilters
299 for (int i = 0; i < pifPaints.size(); ++i) {
301 canvas, pifPaints[i]);
302 }
303
304 // A column of saveLayers with BitmapSources
305 for (int i = 0; i < pifPaints.size(); ++i) {
307 canvas, bmsPaints[i]);
308 }
309
310 // Multiple columns with different geometry
311 for (int i = 0; i < (int)std::size(gDrawMthds); ++i) {
312 for (int j = 0; j < paints.size(); ++j) {
315 canvas, paints[j]);
316 }
317 }
318
319 }
320
321private:
322 using INHERITED = GM;
323};
324
325//////////////////////////////////////////////////////////////////////////////
326
327DEF_GM(return new ImageFilterFastBoundGM;)
328} // namespace skiagm
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
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_ColorGREEN
Definition: SkColor.h:131
#define SkIntToScalar(x)
Definition: SkScalar.h:57
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
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 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 drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
Definition: SkCanvas.cpp:1710
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawRRect(const SkRRect &rrect, const SkPaint &paint)
Definition: SkCanvas.cpp:1705
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawDRRect(const SkRRect &outer, const SkRRect &inner, const SkPaint &paint)
Definition: SkCanvas.cpp:1645
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
@ kLines_PointMode
draw each pair of points as a line segment
Definition: SkCanvas.h:1242
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 > DropShadowOnly(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 > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > Picture(sk_sp< SkPicture > pic, const SkRect &targetRect)
static sk_sp< SkImageFilter > MatrixTransform(const SkMatrix &matrix, const SkSamplingOptions &sampling, sk_sp< SkImageFilter > input)
static sk_sp< SkImageFilter > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Tile(const SkRect &src, const SkRect &dst, sk_sp< SkImageFilter > input)
SkMatrix & setRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:452
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setColor(SkColor color)
Definition: SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
static SkPath Polygon(const SkPoint pts[], int count, bool isClosed, SkPathFillType=SkPathFillType::kWinding, bool isVolatile=false)
Definition: SkPath.cpp:3614
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
void inset(SkScalar dx, SkScalar dy, SkRRect *dst) const
Definition: SkRRect.cpp:562
void setRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition: SkRRect.cpp:52
int size() const
Definition: SkTArray.h:421
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
DrawResult draw(SkCanvas *canvas)
Definition: gm.h:140
void onDraw(SkCanvas *canvas) override
static constexpr int kNumVertTiles
SkString getName() const override
static void draw_savelayer_with_paint(const SkIPoint &off, SkCanvas *canvas, const SkPaint &p)
static void draw_geom_with_paint(drawMth draw, const SkIPoint &off, SkCanvas *canvas, const SkPaint &p)
VkSurfaceKHR surface
Definition: main.cc:49
SkBitmap source
Definition: examples.cpp:28
float SkScalar
Definition: extension.cpp:12
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
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
dst
Definition: cp.py:12
static void draw_oval(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
static void draw_drrect(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static void draw_bitmap(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
static void create_paints(TArray< SkPaint > *paints, sk_sp< SkImageFilter > source)
static void draw_rect(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
static void add_paint(TArray< SkPaint > *paints, sk_sp< SkImageFilter > filter)
static SkPath scale(const SkPath &path, SkScalar scale)
Definition: patheffects.cpp:68
SkSamplingOptions(SkFilterMode::kLinear))
static void draw_rrect(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
constexpr drawMth gDrawMthds[]
void(* drawMth)(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
static void draw_points(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
static void draw_path(SkCanvas *canvas, const SkRect &r, const SkPaint &p)
int32_t fX
x-axis value
Definition: SkPoint_impl.h:29
int32_t fY
y-axis value
Definition: SkPoint_impl.h:30
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 SkImageInfo MakeN32Premul(int width, int height)
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
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
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15