blob: 95a9cccba60ac627ef947a9290d0ecd2abcf211e [file] [log] [blame]
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "tools/Resources.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "tools/ToolUtils.h"
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000021
Hal Canary594fe852019-07-18 13:35:49 -040022namespace {
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000023static 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
Florin Malitac54d8db2014-12-10 12:02:16 -050030static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
reed93a12152015-03-16 10:08:34 -070031 SkFilterQuality lvl) {
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000032 SkPaint paint;
reed93a12152015-03-16 10:08:34 -070033 paint.setFilterQuality(lvl);
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000034
35 SkAutoCanvasRestore acr(canvas, true);
36
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000037 canvas->translate(dx, 0);
Florin Malitac54d8db2014-12-10 12:02:16 -050038 canvas->concat(mat);
39 canvas->drawBitmap(bm, 0, 0, &paint);
40}
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000041
Florin Malitac54d8db2014-12-10 12:02:16 -050042static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
reed93a12152015-03-16 10:08:34 -070043 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);
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000047}
48
49class FilterIndiaBoxGM : public skiagm::GM {
Hal Canary594fe852019-07-18 13:35:49 -040050 SkBitmap fBM;
51 SkMatrix fMatrix[2];
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onOnceBeforeDraw() override {
Hal Canary594fe852019-07-18 13:35:49 -040054 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 }
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000059
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
Hal Canary594fe852019-07-18 13:35:49 -040070 SkString onShortName() override { return SkString("filterindiabox"); }
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000071
Hal Canary594fe852019-07-18 13:35:49 -040072 SkISize onISize() override { return {680, 130}; }
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000073
mtklein36352bf2015-03-25 18:17:31 -070074 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000075 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 }
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000085};
Hal Canary594fe852019-07-18 13:35:49 -040086} // namespace
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000087
Hal Canary594fe852019-07-18 13:35:49 -040088DEF_GM( return new FilterIndiaBoxGM(); )