1 /*
2 * Copyright 2011 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/SkFilterQuality.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSize.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "tools/Resources.h"
20 #include "tools/ToolUtils.h"
21
22 namespace {
computeSize(const SkBitmap & bm,const SkMatrix & mat)23 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
24 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
25 SkIntToScalar(bm.height()));
26 mat.mapRect(&bounds);
27 return SkSize::Make(bounds.width(), bounds.height());
28 }
29
draw_cell(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx,SkFilterQuality lvl)30 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
31 SkFilterQuality lvl) {
32 SkPaint paint;
33 paint.setFilterQuality(lvl);
34
35 SkAutoCanvasRestore acr(canvas, true);
36
37 canvas->translate(dx, 0);
38 canvas->concat(mat);
39 canvas->drawBitmap(bm, 0, 0, &paint);
40 }
41
draw_row(SkCanvas * canvas,const SkBitmap & bm,const SkMatrix & mat,SkScalar dx)42 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
43 draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality);
44 draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality);
45 draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality);
46 draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality);
47 }
48
49 class FilterIndiaBoxGM : public skiagm::GM {
50 SkBitmap fBM;
51 SkMatrix fMatrix[2];
52
onOnceBeforeDraw()53 void onOnceBeforeDraw() override {
54 constexpr char kResource[] = "images/box.gif";
55 if (!GetResourceAsBitmap(kResource, &fBM)) {
56 fBM.allocN32Pixels(1, 1);
57 fBM.eraseARGB(255, 255, 0 , 0); // red == bad
58 }
59
60 SkScalar cx = SkScalarHalf(fBM.width());
61 SkScalar cy = SkScalarHalf(fBM.height());
62
63 float vertScale = 30.0f/55.0f;
64 float horizScale = 150.0f/200.0f;
65
66 fMatrix[0].setScale(horizScale, vertScale);
67 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
68 }
69
onShortName()70 SkString onShortName() override { return SkString("filterindiabox"); }
71
onISize()72 SkISize onISize() override { return {680, 130}; }
73
onDraw(SkCanvas * canvas)74 void onDraw(SkCanvas* canvas) override {
75 canvas->translate(10, 10);
76 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
77 SkSize size = computeSize(fBM, fMatrix[i]);
78 size.fWidth += 20;
79 size.fHeight += 20;
80
81 draw_row(canvas, fBM, fMatrix[i], size.fWidth);
82 canvas->translate(0, size.fHeight);
83 }
84 }
85 };
86 } // namespace
87
88 DEF_GM( return new FilterIndiaBoxGM(); )
89