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