blob: aa27d70115c3f1a712616b550100fcd149c49c55 [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"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040011#include "include/core/SkFont.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkShader.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
22#include "tools/ToolUtils.h"
bsalomon8cf1d952015-04-16 06:54:28 -070023
24// Inspired by svg/as-border-image/svg-as-border-image.html. Draws a four-color checker board bitmap
25// such that it is stretched and repeat tiled with different filter qualities. It is testing whether
26// the bmp filter respects the repeat mode at the tile seams.
27class BmpFilterQualityRepeat : public skiagm::GM {
28public:
Mike Kleinea3f0142019-03-20 11:12:10 -050029 BmpFilterQualityRepeat() { this->setBGColor(ToolUtils::color_to_565(0xFFCCBBAA)); }
bsalomon8cf1d952015-04-16 06:54:28 -070030
31protected:
32
33 void onOnceBeforeDraw() override {
34 fBmp.allocN32Pixels(40, 40, true);
35 SkCanvas canvas(fBmp);
36 SkBitmap colorBmp;
37 colorBmp.allocN32Pixels(20, 20, true);
38 colorBmp.eraseColor(0xFFFF0000);
Mike Reedfa582c82021-01-23 22:07:05 -050039 canvas.drawImage(colorBmp.asImage(), 0, 0);
Mike Kleinea3f0142019-03-20 11:12:10 -050040 colorBmp.eraseColor(ToolUtils::color_to_565(0xFF008200));
Mike Reedfa582c82021-01-23 22:07:05 -050041 canvas.drawImage(colorBmp.asImage(), 20, 0);
Mike Kleinea3f0142019-03-20 11:12:10 -050042 colorBmp.eraseColor(ToolUtils::color_to_565(0xFFFF9000));
Mike Reedfa582c82021-01-23 22:07:05 -050043 canvas.drawImage(colorBmp.asImage(), 0, 20);
Mike Kleinea3f0142019-03-20 11:12:10 -050044 colorBmp.eraseColor(ToolUtils::color_to_565(0xFF2000FF));
Mike Reedfa582c82021-01-23 22:07:05 -050045 canvas.drawImage(colorBmp.asImage(), 20, 20);
bsalomon8cf1d952015-04-16 06:54:28 -070046 }
47
48 SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); }
49
fmalitab1c7f882016-11-03 11:42:49 -070050 SkISize onISize() override { return SkISize::Make(1000, 400); }
bsalomon8cf1d952015-04-16 06:54:28 -070051
52 void onDraw(SkCanvas* canvas) override {
fmalitab1c7f882016-11-03 11:42:49 -070053 this->drawAll(canvas, 2.5f);
54 canvas->translate(0, 250);
55 canvas->scale(0.5, 0.5);
56 this->drawAll(canvas, 1);
57 }
bsalomon8cf1d952015-04-16 06:54:28 -070058
fmalitab1c7f882016-11-03 11:42:49 -070059private:
60 void drawAll(SkCanvas* canvas, SkScalar scaleX) const {
fmalitab1c7f882016-11-03 11:42:49 -070061 SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210);
62 SkMatrix lm = SkMatrix::I();
63 lm.setScaleX(scaleX);
64 lm.setTranslateX(423);
65 lm.setTranslateY(330);
bsalomon8cf1d952015-04-16 06:54:28 -070066
fmalitab1c7f882016-11-03 11:42:49 -070067 SkPaint textPaint;
fmalitab1c7f882016-11-03 11:42:49 -070068 textPaint.setAntiAlias(true);
69
70 SkPaint bmpPaint(textPaint);
71
Mike Kleinea3f0142019-03-20 11:12:10 -050072 SkFont font(ToolUtils::create_portable_typeface());
Mike Reed4de2f1f2019-01-05 16:35:13 -050073
fmalitab1c7f882016-11-03 11:42:49 -070074 SkAutoCanvasRestore acr(canvas, true);
75
Mike Reedc82ab082021-07-16 22:19:26 -040076 const struct {
77 const char* name;
78 SkSamplingOptions sampling;
79 } recs[] = {
80 { "none", SkSamplingOptions(SkFilterMode::kNearest) },
81 { "low", SkSamplingOptions(SkFilterMode::kLinear) },
82 { "medium", SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear) },
83 { "high", SkSamplingOptions(SkCubicResampler::Mitchell()) },
84 };
85
86 for (const auto& rec : recs) {
Mike Reedfae8fce2019-04-03 10:27:45 -040087 constexpr SkTileMode kTM = SkTileMode::kRepeat;
Mike Reedc82ab082021-07-16 22:19:26 -040088 bmpPaint.setShader(fBmp.makeShader(kTM, kTM, rec.sampling, lm));
bsalomon8cf1d952015-04-16 06:54:28 -070089 canvas->drawRect(rect, bmpPaint);
Mike Reedc82ab082021-07-16 22:19:26 -040090 canvas->drawString(rec.name, 20, 40, font, textPaint);
bsalomon8cf1d952015-04-16 06:54:28 -070091 canvas->translate(250, 0);
92 }
fmalitab1c7f882016-11-03 11:42:49 -070093
bsalomon8cf1d952015-04-16 06:54:28 -070094 }
95
bsalomon8cf1d952015-04-16 06:54:28 -070096 SkBitmap fBmp;
97
John Stiles7571f9e2020-09-02 22:42:33 -040098 using INHERITED = skiagm::GM;
bsalomon8cf1d952015-04-16 06:54:28 -070099};
100
101//////////////////////////////////////////////////////////////////////////////
102
halcanary385fe4d2015-08-26 13:07:48 -0700103DEF_GM(return new BmpFilterQualityRepeat;)