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