| 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/SkImage.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/effects/SkImageFilters.h" |
| 18 |
|
| 19 |
static void draw(SkCanvas* canvas, const SkRect& rect, const SkBitmap& bitmap, |
| 20 |
const SkMatrix& matrix, const SkSamplingOptions& sampling) { |
| 21 |
SkPaint paint; |
| 22 |
paint.setImageFilter(SkImageFilters::MatrixTransform(matrix, sampling, nullptr)); |
| 23 |
canvas->saveLayer(&rect, &paint); |
| 24 |
canvas->drawImage(bitmap.asImage(), 0, 0, sampling); |
| 25 |
canvas->restore(); |
| 26 |
} |
| 27 |
|
| 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, SkSamplingOptions()); |
| 59 |
|
| 60 |
canvas->translate(srcRect.width() + margin, 0); |
| 61 |
draw(canvas, srcRect, checkerboard, matrix, SkSamplingOptions(SkFilterMode::kLinear)); |
| 62 |
} |