Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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)));
70 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(i * 8),
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
#define WIDTH
#define HEIGHT
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void clear(SkColor color)
Definition SkCanvas.h:1199
int save()
Definition SkCanvas.cpp:451
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
void setBGColor(SkColor)
Definition gm.cpp:159
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
const Paint & paint
#define MARGIN
sk_sp< SkImage > image
Definition examples.cpp:29
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
sk_sp< SkImage > create_checkerboard_image(int w, int h, SkColor c1, SkColor c2, int checkSize)
sk_sp< SkImage > CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, const char *str)
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
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