blob: 47891a692b153dfd87fc51994300ca8e96606d7d [file] [log] [blame]
bsalomon8cf1d952015-04-16 06:54:28 -07001/*
2 * Copyright 2015 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 Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkFilterQuality.h"
12#include "include/core/SkFont.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkShader.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040018#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTileMode.h"
21#include "include/core/SkTypeface.h"
22#include "include/core/SkTypes.h"
23#include "tools/ToolUtils.h"
bsalomon8cf1d952015-04-16 06:54:28 -070024
25// Inspired by svg/as-border-image/svg-as-border-image.html. Draws a four-color checker board bitmap
26// such that it is stretched and repeat tiled with different filter qualities. It is testing whether
27// the bmp filter respects the repeat mode at the tile seams.
28class BmpFilterQualityRepeat : public skiagm::GM {
29public:
Mike Kleinea3f0142019-03-20 11:12:10 -050030 BmpFilterQualityRepeat() { this->setBGColor(ToolUtils::color_to_565(0xFFCCBBAA)); }
bsalomon8cf1d952015-04-16 06:54:28 -070031
32protected:
33
34 void onOnceBeforeDraw() override {
35 fBmp.allocN32Pixels(40, 40, true);
36 SkCanvas canvas(fBmp);
37 SkBitmap colorBmp;
38 colorBmp.allocN32Pixels(20, 20, true);
39 colorBmp.eraseColor(0xFFFF0000);
40 canvas.drawBitmap(colorBmp, 0, 0);
Mike Kleinea3f0142019-03-20 11:12:10 -050041 colorBmp.eraseColor(ToolUtils::color_to_565(0xFF008200));
bsalomon8cf1d952015-04-16 06:54:28 -070042 canvas.drawBitmap(colorBmp, 20, 0);
Mike Kleinea3f0142019-03-20 11:12:10 -050043 colorBmp.eraseColor(ToolUtils::color_to_565(0xFFFF9000));
bsalomon8cf1d952015-04-16 06:54:28 -070044 canvas.drawBitmap(colorBmp, 0, 20);
Mike Kleinea3f0142019-03-20 11:12:10 -050045 colorBmp.eraseColor(ToolUtils::color_to_565(0xFF2000FF));
bsalomon8cf1d952015-04-16 06:54:28 -070046 canvas.drawBitmap(colorBmp, 20, 20);
47 }
48
49 SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); }
50
fmalitab1c7f882016-11-03 11:42:49 -070051 SkISize onISize() override { return SkISize::Make(1000, 400); }
bsalomon8cf1d952015-04-16 06:54:28 -070052
53 void onDraw(SkCanvas* canvas) override {
fmalitab1c7f882016-11-03 11:42:49 -070054 this->drawAll(canvas, 2.5f);
55 canvas->translate(0, 250);
56 canvas->scale(0.5, 0.5);
57 this->drawAll(canvas, 1);
58 }
bsalomon8cf1d952015-04-16 06:54:28 -070059
fmalitab1c7f882016-11-03 11:42:49 -070060private:
61 void drawAll(SkCanvas* canvas, SkScalar scaleX) const {
mtkleindbfd7ab2016-09-01 11:24:54 -070062 constexpr struct {
bsalomon8cf1d952015-04-16 06:54:28 -070063 SkFilterQuality fQuality;
64 const char* fName;
65 } kQualities[] = {
66 {kNone_SkFilterQuality, "none"},
67 {kLow_SkFilterQuality, "low"},
68 {kMedium_SkFilterQuality, "medium"},
69 {kHigh_SkFilterQuality, "high"},
70 };
71
fmalitab1c7f882016-11-03 11:42:49 -070072 SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210);
73 SkMatrix lm = SkMatrix::I();
74 lm.setScaleX(scaleX);
75 lm.setTranslateX(423);
76 lm.setTranslateY(330);
bsalomon8cf1d952015-04-16 06:54:28 -070077
fmalitab1c7f882016-11-03 11:42:49 -070078 SkPaint textPaint;
fmalitab1c7f882016-11-03 11:42:49 -070079 textPaint.setAntiAlias(true);
80
81 SkPaint bmpPaint(textPaint);
82
Mike Kleinea3f0142019-03-20 11:12:10 -050083 SkFont font(ToolUtils::create_portable_typeface());
Mike Reed4de2f1f2019-01-05 16:35:13 -050084
fmalitab1c7f882016-11-03 11:42:49 -070085 SkAutoCanvasRestore acr(canvas, true);
86
87 for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) {
Mike Reedfae8fce2019-04-03 10:27:45 -040088 constexpr SkTileMode kTM = SkTileMode::kRepeat;
Mike Reed50acf8f2019-04-08 13:20:23 -040089 bmpPaint.setShader(fBmp.makeShader(kTM, kTM, &lm));
fmalitab1c7f882016-11-03 11:42:49 -070090 bmpPaint.setFilterQuality(kQualities[q].fQuality);
bsalomon8cf1d952015-04-16 06:54:28 -070091 canvas->drawRect(rect, bmpPaint);
Mike Reed4de2f1f2019-01-05 16:35:13 -050092 canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint);
bsalomon8cf1d952015-04-16 06:54:28 -070093 canvas->translate(250, 0);
94 }
fmalitab1c7f882016-11-03 11:42:49 -070095
bsalomon8cf1d952015-04-16 06:54:28 -070096 }
97
bsalomon8cf1d952015-04-16 06:54:28 -070098 SkBitmap fBmp;
99
100 typedef skiagm::GM INHERITED;
101};
102
103//////////////////////////////////////////////////////////////////////////////
104
halcanary385fe4d2015-08-26 13:07:48 -0700105DEF_GM(return new BmpFilterQualityRepeat;)