Flutter Engine
The Flutter Engine
offsetimagefilter.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"
16#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
23#include "tools/ToolUtils.h"
25
26#include <utility>
27
28#define WIDTH 600
29#define HEIGHT 100
30#define MARGIN 12
31
33public:
35 this->setBGColor(0xFF000000);
36 }
37
38protected:
39 SkString getName() const override { return SkString("offsetimagefilter"); }
40
41 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
42
43 void onOnceBeforeDraw() override {
44 fBitmap = ToolUtils::CreateStringImage(80, 80, 0xD000D000, 15, 65, 96, "e");
45
46 fCheckerboard = ToolUtils::create_checkerboard_image(80, 80, 0xFFA0A0A0, 0xFF404040, 8);
47 }
48
49 void onDraw(SkCanvas* canvas) override {
50 canvas->clear(SK_ColorBLACK);
52
53 for (int i = 0; i < 4; i++) {
54 sk_sp<SkImage> image = (i & 0x01) ? fCheckerboard : fBitmap;
55 SkIRect cropRect = SkIRect::MakeXYWH(i * 12,
56 i * 8,
57 image->width() - i * 8,
58 image->height() - i * 12);
61 SkScalar dy = SkIntToScalar(i*10);
62 paint.setImageFilter(SkImageFilters::Offset(dx, dy, std::move(tileInput), &cropRect));
63 DrawClippedImage(canvas, image.get(), paint, 1, cropRect);
64 canvas->translate(SkIntToScalar(image->width() + MARGIN), 0);
65 }
66
67 SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100);
68 paint.setImageFilter(SkImageFilters::Offset(-5, -10, nullptr, &cropRect));
69 DrawClippedImage(canvas, fBitmap.get(), paint, 2, cropRect);
70 }
71private:
72 static void DrawClippedImage(SkCanvas* canvas, const SkImage* image, const SkPaint& paint,
73 SkScalar scale, const SkIRect& cropRect) {
75
76 canvas->save();
77 canvas->clipRect(clipRect);
78 canvas->scale(scale, scale);
79 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
80 canvas->restore();
81
82 // Draw a boundary rect around the intersection of the clip rect and crop rect.
83 SkRect cropRectFloat;
84 SkMatrix::Scale(scale, scale).mapRect(&cropRectFloat, SkRect::Make(cropRect));
85 if (clipRect.intersect(cropRectFloat)) {
86 SkPaint strokePaint;
88 strokePaint.setStrokeWidth(2);
89 strokePaint.setColor(SK_ColorRED);
90 canvas->drawRect(clipRect, strokePaint);
91 }
92 }
93
94 sk_sp<SkImage> fBitmap, fCheckerboard;
95
96 using INHERITED = skiagm::GM;
97};
98DEF_GM( return new OffsetImageFilterGM; )
99
100//////////////////////////////////////////////////////////////////////////////
101
103public:
105
106protected:
107 SkString getName() const override { return SkString("simple-offsetimagefilter"); }
108
109 SkISize getISize() override { return SkISize::Make(640, 200); }
110
111 void doDraw(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imgf,
112 const SkIRect* cropR = nullptr, const SkRect* clipR = nullptr) {
113 SkPaint p;
114
115 if (clipR) {
116 p.setColor(0xFF00FF00);
117 p.setStyle(SkPaint::kStroke_Style);
118 canvas->drawRect(clipR->makeInset(SK_ScalarHalf, SK_ScalarHalf), p);
119 p.setStyle(SkPaint::kFill_Style);
120 }
121
122 // Visualize the crop rect for debugging
123 if (imgf && cropR) {
124 p.setColor(0x66FF00FF);
125 p.setStyle(SkPaint::kStroke_Style);
126
128 canvas->drawRect(cr, p);
129 p.setStyle(SkPaint::kFill_Style);
130 }
131
132 p.setColor(0x660000FF);
133 canvas->drawRect(r, p);
134
135 if (clipR) {
136 canvas->save();
137 canvas->clipRect(*clipR);
138 }
139 if (imgf) {
140 p.setImageFilter(std::move(imgf));
141 }
142 p.setColor(0x66FF0000);
143 canvas->drawRect(r, p);
144
145 if (clipR) {
146 canvas->restore();
147 }
148 }
149
150 void onDraw(SkCanvas* canvas) override {
151 SkIRect cr0 = SkIRect::MakeWH(40, 40);
152 SkIRect cr1 = SkIRect::MakeWH(20, 20);
153 SkIRect cr2 = SkIRect::MakeXYWH(40, 0, 40, 40);
154 const SkRect r = SkRect::Make(cr0);
155 const SkRect r2 = SkRect::Make(cr2);
156
157 canvas->translate(40, 40);
158
159 canvas->save();
160 this->doDraw(canvas, r, nullptr);
161
162 canvas->translate(100, 0);
163 this->doDraw(canvas, r, SkImageFilters::Offset(20, 20, nullptr));
164
165 canvas->translate(100, 0);
166 this->doDraw(canvas, r, SkImageFilters::Offset(20, 20, nullptr, &cr0), &cr0);
167
168 canvas->translate(100, 0);
169 this->doDraw(canvas, r, SkImageFilters::Offset(20, 20, nullptr), /* cropR */ nullptr, &r);
170
171 canvas->translate(100, 0);
172 this->doDraw(canvas, r, SkImageFilters::Offset(20, 20, nullptr, &cr1), &cr1);
173
174 SkRect clipR = SkRect::MakeXYWH(40, 40, 40, 40);
175 canvas->translate(100, 0);
176 this->doDraw(canvas, r, SkImageFilters::Offset(20, 20, nullptr), /* cropR */ nullptr, &clipR);
177 canvas->restore();
178
179 // 2nd row
180 canvas->translate(0, 80);
181
182 /*
183 * combos of clip and crop rects that align with src and dst
184 */
185
186 // crop==clip==src
187 this->doDraw(canvas, r, SkImageFilters::Offset(40, 0, nullptr, &cr0), &cr0, &r);
188
189 // crop==src, clip==dst
190 canvas->translate(100, 0);
191 this->doDraw(canvas, r, SkImageFilters::Offset(40, 0, nullptr, &cr0), &cr0, &r2);
192
193 // crop==dst, clip==src
194 canvas->translate(100, 0);
195 this->doDraw(canvas, r, SkImageFilters::Offset(40, 0, nullptr, &cr2), &cr2, &r);
196
197 // crop==clip==dst
198 canvas->translate(100, 0);
199 this->doDraw(canvas, r, SkImageFilters::Offset(40, 0, nullptr, &cr2), &cr2, &r2);
200 }
201
202private:
203 using INHERITED = skiagm::GM;
204};
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SK_ScalarHalf
Definition: SkScalar.h:19
#define SkIntToScalar(x)
Definition: SkScalar.h:57
void onOnceBeforeDraw() override
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
void doDraw(SkCanvas *canvas, const SkRect &r, sk_sp< SkImageFilter > imgf, const SkIRect *cropR=nullptr, const SkRect *clipR=nullptr)
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 scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkImageFilter > Image(sk_sp< SkImage > image, const SkRect &srcRect, const SkRect &dstRect, const SkSamplingOptions &sampling)
static sk_sp< SkImageFilter > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkMatrix.cpp:1141
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
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
clipRect(r.rect, r.opAA.op(), r.opAA.aa())) template<> void Draw
sk_sp< const SkImage > image
Definition: SkRecords.h:269
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
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)
SkSamplingOptions(SkFilterMode::kLinear))
#define WIDTH
#define MARGIN
#define HEIGHT
const Scalar scale
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
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 SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
SkRect makeInset(float dx, float dy) const
Definition: SkRect.h:987