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