1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkBlurImageFilter.h"
#include "SkDropShadowImageFilter.h"
#include "SkImageSource.h"
#include "SkOffsetImageFilter.h"
#include "SkPictureImageFilter.h"
#include "SkPictureRecorder.h"
#include "SkRandom.h"
#include "SkSurface.h"
#include "SkTileImageFilter.h"
namespace skiagm {
// Each method of this type must draw its geometry inside 'r' using 'p'
typedef void(*drawMth)(SkCanvas* canvas, const SkRect& r, const SkPaint& p);
static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
canvas->drawRect(r, p);
}
static void draw_oval(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
canvas->drawOval(r, p);
}
static void draw_rrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
SkScalar xRad = r.width() / 4.0f;
SkScalar yRad = r.height() / 4.0f;
SkRRect rr;
rr.setRectXY(r, xRad, yRad);
canvas->drawRRect(rr, p);
}
static void draw_drrect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
SkScalar xRad = r.width() / 4.0f;
SkScalar yRad = r.height() / 4.0f;
SkRRect outer;
outer.setRectXY(r, xRad, yRad);
SkRRect inner = outer;
inner.inset(xRad, yRad);
canvas->drawDRRect(outer, inner, p);
}
static void draw_path(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
SkPath path;
path.moveTo(r.fLeft, r.fTop);
path.lineTo(r.fLeft, r.fBottom);
path.lineTo(r.fRight, r.fBottom);
path.close();
canvas->drawPath(path, p);
}
static void draw_points(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
SkPoint pts0[2] = { { r.fLeft, r.fTop }, { r.fRight, r.fBottom } };
SkPoint pts1[2] = { { r.fLeft, r.fBottom }, { r.fRight, r.fTop } };
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts0, p);
canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts1, p);
}
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
SkBitmap bm;
bm.allocN32Pixels(64, 64);
SkCanvas temp(bm);
temp.clear(SK_ColorMAGENTA);
canvas->drawBitmapRect(bm, r, &p);
}
static const drawMth gDrawMthds[] = {
draw_rect, draw_oval, draw_rrect, draw_drrect, draw_path, draw_points, draw_bitmap
};
static void add_paint(SkTArray<SkPaint>* paints, sk_sp<SkImageFilter> filter) {
SkPaint& p = paints->push_back();
p.setImageFilter(std::move(filter));
SkASSERT(p.canComputeFastBounds());
}
// Create a selection of imagefilter-based paints to test
static void create_paints(SkTArray<SkPaint>* paints, sk_sp<SkImageFilter> source) {
{
SkMatrix scale;
scale.setScale(2.0f, 2.0f);
sk_sp<SkImageFilter> scaleMIF(
SkImageFilter::MakeMatrixFilter(scale, kLow_SkFilterQuality, source));
add_paint(paints, std::move(scaleMIF));
}
{
SkMatrix rot;
rot.setRotate(-33.3f);
sk_sp<SkImageFilter> rotMIF(
SkImageFilter::MakeMatrixFilter(rot, kLow_SkFilterQuality, source));
add_paint(paints, std::move(rotMIF));
}
{
SkRect src = SkRect::MakeXYWH(20, 20, 10, 10);
SkRect dst = SkRect::MakeXYWH(30, 30, 30, 30);
sk_sp<SkImageFilter> tileIF(SkTileImageFilter::Create(src, dst, nullptr));
add_paint(paints, std::move(tileIF));
}
{
static const SkDropShadowImageFilter::ShadowMode kBoth =
SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode;
sk_sp<SkImageFilter> dsif(SkDropShadowImageFilter::Make(10.0f, 10.0f,
3.0f, 3.0f,
SK_ColorRED, kBoth,
source));
add_paint(paints, std::move(dsif));
}
{
sk_sp<SkImageFilter> dsif(
SkDropShadowImageFilter::Make(27.0f, 27.0f,
3.0f, 3.0f,
SK_ColorRED,
SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode,
source));
add_paint(paints, std::move(dsif));
}
add_paint(paints, SkBlurImageFilter::Make(3, 3, source));
add_paint(paints, SkOffsetImageFilter::Make(15, 15, source));
}
// This GM visualizes the fast bounds for various combinations of geometry
// and image filter
class ImageFilterFastBoundGM : public GM {
public:
ImageFilterFastBoundGM() {
this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
}
protected:
static const int kTileWidth = 100;
static const int kTileHeight = 100;
static const int kNumVertTiles = 7;
static const int kNumXtraCols = 2;
SkString onShortName() override{ return SkString("filterfastbounds"); }
SkISize onISize() override{
return SkISize::Make((SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols) * kTileWidth,
kNumVertTiles * kTileHeight);
}
static void draw_geom_with_paint(drawMth draw, const SkIPoint& off,
SkCanvas* canvas, const SkPaint& p) {
SkPaint redStroked;
redStroked.setColor(SK_ColorRED);
redStroked.setStyle(SkPaint::kStroke_Style);
SkPaint blueStroked;
blueStroked.setColor(SK_ColorBLUE);
blueStroked.setStyle(SkPaint::kStroke_Style);
const SkRect r = SkRect::MakeLTRB(20, 20, 30, 30);
SkRect storage;
canvas->save();
canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY));
canvas->scale(1.5f, 1.5f);
const SkRect& fastBound = p.computeFastBounds(r, &storage);
canvas->save();
canvas->clipRect(fastBound);
(*draw)(canvas, r, p);
canvas->restore();
canvas->drawRect(r, redStroked);
canvas->drawRect(fastBound, blueStroked);
canvas->restore();
}
static void draw_savelayer_with_paint(const SkIPoint& off,
SkCanvas* canvas,
const SkPaint& p) {
SkPaint redStroked;
redStroked.setColor(SK_ColorRED);
redStroked.setStyle(SkPaint::kStroke_Style);
SkPaint blueStroked;
blueStroked.setColor(SK_ColorBLUE);
blueStroked.setStyle(SkPaint::kStroke_Style);
const SkRect bounds = SkRect::MakeWH(10, 10);
SkRect storage;
canvas->save();
canvas->translate(30, 30);
canvas->translate(SkIntToScalar(off.fX), SkIntToScalar(off.fY));
canvas->scale(1.5f, 1.5f);
const SkRect& fastBound = p.computeFastBounds(bounds, &storage);
canvas->saveLayer(&fastBound, &p);
canvas->restore();
canvas->drawRect(bounds, redStroked);
canvas->drawRect(fastBound, blueStroked);
canvas->restore();
}
void onDraw(SkCanvas* canvas) override{
SkPaint blackFill;
//-----------
// Normal paints (no source)
SkTArray<SkPaint> paints;
create_paints(&paints, nullptr);
//-----------
// Paints with a PictureImageFilter as a source
sk_sp<SkPicture> pic;
{
SkPictureRecorder rec;
SkCanvas* c = rec.beginRecording(10, 10);
c->drawRect(SkRect::MakeWH(10, 10), blackFill);
pic = rec.finishRecordingAsPicture();
}
SkTArray<SkPaint> pifPaints;
create_paints(&pifPaints, SkPictureImageFilter::Make(pic));
//-----------
// Paints with a SkImageSource as a source
auto surface(SkSurface::MakeRasterN32Premul(10, 10));
{
SkPaint p;
SkCanvas* temp = surface->getCanvas();
temp->clear(SK_ColorYELLOW);
p.setColor(SK_ColorBLUE);
temp->drawRect(SkRect::MakeLTRB(5, 5, 10, 10), p);
p.setColor(SK_ColorGREEN);
temp->drawRect(SkRect::MakeLTRB(5, 0, 10, 5), p);
}
sk_sp<SkImage> image(surface->makeImageSnapshot());
sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
SkTArray<SkPaint> bmsPaints;
create_paints(&bmsPaints, std::move(imageSource));
//-----------
SkASSERT(paints.count() == kNumVertTiles);
SkASSERT(paints.count() == pifPaints.count());
SkASSERT(paints.count() == bmsPaints.count());
// horizontal separators
for (int i = 1; i < paints.count(); ++i) {
canvas->drawLine(0,
i*SkIntToScalar(kTileHeight),
SkIntToScalar((SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols)*kTileWidth),
i*SkIntToScalar(kTileHeight),
blackFill);
}
// vertical separators
for (int i = 0; i < (int)SK_ARRAY_COUNT(gDrawMthds) + kNumXtraCols; ++i) {
canvas->drawLine(SkIntToScalar(i * kTileWidth),
0,
SkIntToScalar(i * kTileWidth),
SkIntToScalar(paints.count() * kTileWidth),
blackFill);
}
// A column of saveLayers with PictureImageFilters
for (int i = 0; i < pifPaints.count(); ++i) {
draw_savelayer_with_paint(SkIPoint::Make(0, i*kTileHeight),
canvas, pifPaints[i]);
}
// A column of saveLayers with BitmapSources
for (int i = 0; i < pifPaints.count(); ++i) {
draw_savelayer_with_paint(SkIPoint::Make(kTileWidth, i*kTileHeight),
canvas, bmsPaints[i]);
}
// Multiple columns with different geometry
for (int i = 0; i < (int)SK_ARRAY_COUNT(gDrawMthds); ++i) {
for (int j = 0; j < paints.count(); ++j) {
draw_geom_with_paint(*gDrawMthds[i],
SkIPoint::Make((i+kNumXtraCols) * kTileWidth, j*kTileHeight),
canvas, paints[j]);
}
}
}
private:
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return new ImageFilterFastBoundGM;)
}
|