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 
12 namespace skiagm {
13 
14 class MatrixImageFilterGM : public GM {
15 public:
MatrixImageFilterGM()16     MatrixImageFilterGM() {
17         this->setBGColor(0x00000000);
18     }
19 
20 protected:
onShortName()21     virtual SkString onShortName() {
22         return SkString("matriximagefilter");
23     }
24 
draw(SkCanvas * canvas,const SkRect & rect,const SkBitmap & bitmap,const SkMatrix & matrix,SkFilterQuality filter)25     void draw(SkCanvas* canvas, const SkRect& rect, const SkBitmap& bitmap,
26               const SkMatrix& matrix, SkFilterQuality filter) {
27         SkAutoTUnref<SkImageFilter> imageFilter(
28             SkImageFilter::CreateMatrixFilter(matrix, filter));
29         SkPaint paint;
30         paint.setImageFilter(imageFilter.get());
31         canvas->saveLayer(&rect, &paint);
32         canvas->drawBitmap(bitmap, 0, 0);
33         canvas->restore();
34     }
35 
onISize()36     virtual SkISize onISize() {
37         return SkISize::Make(420, 100);
38     }
39 
make_checkerboard(SkBitmap * bitmap)40     void make_checkerboard(SkBitmap* bitmap) {
41         bitmap->allocN32Pixels(64, 64);
42         SkCanvas canvas(*bitmap);
43         SkPaint darkPaint;
44         darkPaint.setColor(0xFF404040);
45         SkPaint lightPaint;
46         lightPaint.setColor(0xFFA0A0A0);
47         for (int y = 0; y < 64; y += 32) {
48             for (int x = 0; x < 64; x += 32) {
49                 canvas.save();
50                 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
51                 canvas.drawRect(SkRect::MakeXYWH(0, 0, 16, 16), darkPaint);
52                 canvas.drawRect(SkRect::MakeXYWH(16, 0, 16, 16), lightPaint);
53                 canvas.drawRect(SkRect::MakeXYWH(0, 16, 16, 16), lightPaint);
54                 canvas.drawRect(SkRect::MakeXYWH(16, 16, 16, 16), darkPaint);
55                 canvas.restore();
56             }
57         }
58     }
59 
onDraw(SkCanvas * canvas)60     virtual void onDraw(SkCanvas* canvas) {
61         canvas->clear(SK_ColorBLACK);
62         SkMatrix matrix;
63         SkScalar margin = SkIntToScalar(10);
64         matrix.setSkew(SkDoubleToScalar(0.5), SkDoubleToScalar(0.2));
65         SkBitmap checkerboard;
66         make_checkerboard(&checkerboard);
67 
68         SkRect srcRect = SkRect::MakeWH(96, 96);
69 
70         canvas->translate(margin, margin);
71         draw(canvas, srcRect, checkerboard, matrix, kNone_SkFilterQuality);
72 
73         canvas->translate(srcRect.width() + margin, 0);
74         draw(canvas, srcRect, checkerboard, matrix, kLow_SkFilterQuality);
75 
76 #if 0
77         // This may be causing Mac 10.6 to barf.
78         canvas->translate(srcRect.width() + margin, 0);
79         draw(canvas, srcRect, checkerboard, matrix, kMedium_SkFilterQuality);
80 
81         canvas->translate(srcRect.width() + margin, 0);
82         draw(canvas, srcRect, checkerboard, matrix, kHigh_SkFilterQuality);
83 #endif
84     }
85 
86 private:
87     typedef GM INHERITED;
88 };
89 
90 //////////////////////////////////////////////////////////////////////////////
91 
MyFactory(void *)92 static GM* MyFactory(void*) { return new MatrixImageFilterGM; }
93 static GMRegistry reg(MyFactory);
94 
95 }
96