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.h"
9 #include "SkColor.h"
10 #include "SkImageFilter.h"
11
draw(SkCanvas * canvas,const SkRect & rect,const SkBitmap & bitmap,const SkMatrix & matrix,SkFilterQuality filter)12 static void draw(SkCanvas* canvas, const SkRect& rect, const SkBitmap& bitmap,
13 const SkMatrix& matrix, SkFilterQuality filter) {
14 SkPaint paint;
15 paint.setImageFilter(SkImageFilter::MakeMatrixFilter(matrix, filter, nullptr));
16 canvas->saveLayer(&rect, &paint);
17 canvas->drawBitmap(bitmap, 0, 0);
18 canvas->restore();
19 }
20
make_checkerboard(SkBitmap * bitmap)21 static void make_checkerboard(SkBitmap* bitmap) {
22 bitmap->allocN32Pixels(64, 64);
23 SkCanvas canvas(*bitmap);
24 SkPaint darkPaint;
25 darkPaint.setColor(0xFF404040);
26 SkPaint lightPaint;
27 lightPaint.setColor(0xFFA0A0A0);
28 for (int y = 0; y < 64; y += 32) {
29 for (int x = 0; x < 64; x += 32) {
30 canvas.save();
31 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
32 canvas.drawRect(SkRect::MakeXYWH(0, 0, 16, 16), darkPaint);
33 canvas.drawRect(SkRect::MakeXYWH(16, 0, 16, 16), lightPaint);
34 canvas.drawRect(SkRect::MakeXYWH(0, 16, 16, 16), lightPaint);
35 canvas.drawRect(SkRect::MakeXYWH(16, 16, 16, 16), darkPaint);
36 canvas.restore();
37 }
38 }
39 }
40
41 DEF_SIMPLE_GM_BG(matriximagefilter, canvas, 420, 100, SK_ColorBLACK) {
42 SkMatrix matrix;
43 SkScalar margin = SkIntToScalar(10);
44 matrix.setSkew(SkDoubleToScalar(0.5), SkDoubleToScalar(0.2));
45 SkBitmap checkerboard;
46 make_checkerboard(&checkerboard);
47
48 SkRect srcRect = SkRect::MakeWH(96, 96);
49
50 canvas->translate(margin, margin);
51 draw(canvas, srcRect, checkerboard, matrix, kNone_SkFilterQuality);
52
53 canvas->translate(srcRect.width() + margin, 0);
54 draw(canvas, srcRect, checkerboard, matrix, kLow_SkFilterQuality);
55
56 #if 0
57 // This may be causing Mac 10.6 to barf.
58 canvas->translate(srcRect.width() + margin, 0);
59 draw(canvas, srcRect, checkerboard, matrix, kMedium_SkFilterQuality);
60
61 canvas->translate(srcRect.width() + margin, 0);
62 draw(canvas, srcRect, checkerboard, matrix, kHigh_SkFilterQuality);
63 #endif
64 }
65