Flutter Engine
The Flutter Engine
tileimagefilter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
23#include "tools/ToolUtils.h"
25
26#include <stddef.h>
27#include <utility>
28
29#define WIDTH 400
30#define HEIGHT 200
31#define MARGIN 12
32
33namespace skiagm {
34
35class TileImageFilterGM : public GM {
36public:
38 this->setBGColor(0xFF000000);
39 }
40
41protected:
42 SkString getName() const override { return SkString("tileimagefilter"); }
43
44 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
45
46 void onOnceBeforeDraw() override {
47 fBitmap = ToolUtils::CreateStringImage(50, 50, 0xD000D000, 10, 45, 50, "e");
48
49 fCheckerboard = ToolUtils::create_checkerboard_image(80, 80, 0xFFA0A0A0, 0xFF404040, 8);
50 }
51
52 void onDraw(SkCanvas* canvas) override {
53 canvas->clear(SK_ColorBLACK);
54 SkPaint red;
57 SkPaint blue;
60
61 int x = 0, y = 0;
62
63 {
64 for (size_t i = 0; i < 4; i++) {
65 sk_sp<SkImage> image = (i & 0x01) ? fCheckerboard : fBitmap;
68 SkIntToScalar(image->width()/(i+1)),
69 SkIntToScalar(image->height()/(i+1)));
71 SkIntToScalar(i * 4),
72 SkIntToScalar(image->width() - i * 12),
73 SkIntToScalar(image->height()) - i * 12);
75 sk_sp<SkImageFilter> filter(SkImageFilters::Tile(srcRect, dstRect,
76 std::move(tileInput)));
77 canvas->save();
80 paint.setImageFilter(std::move(filter));
81 canvas->drawImage(fBitmap.get(), 0, 0, SkSamplingOptions(), &paint);
82 canvas->drawRect(srcRect, red);
83 canvas->drawRect(dstRect, blue);
84 canvas->restore();
85 x += image->width() + MARGIN;
86 if (x + image->width() > WIDTH) {
87 x = 0;
88 y += image->height() + MARGIN;
89 }
90 }
91 }
92
93 {
94 float matrix[20] = { 1, 0, 0, 0, 0,
95 0, 1, 0, 0, 0,
96 0, 0, 1, 0, 0,
97 0, 0, 0, 1, 0 };
98
99 SkRect srcRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width()),
100 SkIntToScalar(fBitmap->height()));
101 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width() * 2),
102 SkIntToScalar(fBitmap->height() * 2));
103 sk_sp<SkImageFilter> tile(SkImageFilters::Tile(srcRect, dstRect, nullptr));
105
107 paint.setImageFilter(SkImageFilters::ColorFilter(std::move(cf), std::move(tile)));
108 canvas->save();
110 canvas->clipRect(dstRect);
111 canvas->saveLayer(&dstRect, &paint);
112 canvas->drawImage(fBitmap.get(), 0, 0);
113 canvas->restore();
114 canvas->drawRect(srcRect, red);
115 canvas->drawRect(dstRect, blue);
116 canvas->restore();
117 }
118
119 // test that the crop rect properly applies to the tile image filter
120 {
121 canvas->translate(0, SkIntToScalar(100));
122
123 SkRect srcRect = SkRect::MakeXYWH(0, 0, 50, 50);
124 SkRect dstRect = SkRect::MakeXYWH(0, 0, 100, 100);
125 SkIRect cropRect = SkIRect::MakeXYWH(5, 5, 40, 40);
127 sk_sp<SkImageFilter> green(SkImageFilters::ColorFilter(std::move(greenCF),
128 nullptr,
129 &cropRect));
131 paint.setColor(SK_ColorRED);
132 paint.setImageFilter(SkImageFilters::Tile(srcRect, dstRect, std::move(green)));
133 canvas->drawRect(dstRect, paint);
134 }
135 }
136private:
137 sk_sp<SkImage> fBitmap, fCheckerboard;
138
139 using INHERITED = GM;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143
144DEF_GM(return new TileImageFilterGM;)
145} // namespace skiagm
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
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 restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, 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 > Tile(const SkRect &src, const SkRect &dst, sk_sp< SkImageFilter > input)
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
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
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
void onOnceBeforeDraw() override
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
const Paint & paint
Definition: color_source.cc:38
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
Definition: ToolUtils.cpp:168
sk_sp< SkImage > CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, const char *str)
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
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 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
#define WIDTH
#define MARGIN
#define HEIGHT