bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkShader.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 18 | #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" |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 24 | |
| 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. |
| 28 | class BmpFilterQualityRepeat : public skiagm::GM { |
| 29 | public: |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 30 | BmpFilterQualityRepeat() { this->setBGColor(ToolUtils::color_to_565(0xFFCCBBAA)); } |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 31 | |
| 32 | protected: |
| 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); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame^] | 40 | canvas.drawImage(colorBmp.asImage(), 0, 0); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 41 | colorBmp.eraseColor(ToolUtils::color_to_565(0xFF008200)); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame^] | 42 | canvas.drawImage(colorBmp.asImage(), 20, 0); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 43 | colorBmp.eraseColor(ToolUtils::color_to_565(0xFFFF9000)); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame^] | 44 | canvas.drawImage(colorBmp.asImage(), 0, 20); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 45 | colorBmp.eraseColor(ToolUtils::color_to_565(0xFF2000FF)); |
Mike Reed | fa582c8 | 2021-01-23 22:07:05 -0500 | [diff] [blame^] | 46 | canvas.drawImage(colorBmp.asImage(), 20, 20); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); } |
| 50 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 51 | SkISize onISize() override { return SkISize::Make(1000, 400); } |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 52 | |
| 53 | void onDraw(SkCanvas* canvas) override { |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 54 | this->drawAll(canvas, 2.5f); |
| 55 | canvas->translate(0, 250); |
| 56 | canvas->scale(0.5, 0.5); |
| 57 | this->drawAll(canvas, 1); |
| 58 | } |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 59 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 60 | private: |
| 61 | void drawAll(SkCanvas* canvas, SkScalar scaleX) const { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 62 | constexpr struct { |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 63 | 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 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 72 | 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); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 77 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 78 | SkPaint textPaint; |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 79 | textPaint.setAntiAlias(true); |
| 80 | |
| 81 | SkPaint bmpPaint(textPaint); |
| 82 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 83 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 84 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 85 | SkAutoCanvasRestore acr(canvas, true); |
| 86 | |
| 87 | for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 88 | constexpr SkTileMode kTM = SkTileMode::kRepeat; |
Mike Reed | 82abece | 2020-12-12 09:51:11 -0500 | [diff] [blame] | 89 | bmpPaint.setShader(fBmp.makeShader(kTM, kTM, |
| 90 | SkSamplingOptions(kQualities[q].fQuality), lm)); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 91 | canvas->drawRect(rect, bmpPaint); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 92 | canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 93 | canvas->translate(250, 0); |
| 94 | } |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 95 | |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 98 | SkBitmap fBmp; |
| 99 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 100 | using INHERITED = skiagm::GM; |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | ////////////////////////////////////////////////////////////////////////////// |
| 104 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 105 | DEF_GM(return new BmpFilterQualityRepeat;) |