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 | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 8 | #include "ToolUtils.h" |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 9 | #include "gm.h" |
| 10 | |
| 11 | #include "SkShader.h" |
| 12 | |
| 13 | // Inspired by svg/as-border-image/svg-as-border-image.html. Draws a four-color checker board bitmap |
| 14 | // such that it is stretched and repeat tiled with different filter qualities. It is testing whether |
| 15 | // the bmp filter respects the repeat mode at the tile seams. |
| 16 | class BmpFilterQualityRepeat : public skiagm::GM { |
| 17 | public: |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 18 | BmpFilterQualityRepeat() { this->setBGColor(ToolUtils::color_to_565(0xFFCCBBAA)); } |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 19 | |
| 20 | protected: |
| 21 | |
| 22 | void onOnceBeforeDraw() override { |
| 23 | fBmp.allocN32Pixels(40, 40, true); |
| 24 | SkCanvas canvas(fBmp); |
| 25 | SkBitmap colorBmp; |
| 26 | colorBmp.allocN32Pixels(20, 20, true); |
| 27 | colorBmp.eraseColor(0xFFFF0000); |
| 28 | canvas.drawBitmap(colorBmp, 0, 0); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 29 | colorBmp.eraseColor(ToolUtils::color_to_565(0xFF008200)); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 30 | canvas.drawBitmap(colorBmp, 20, 0); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 31 | colorBmp.eraseColor(ToolUtils::color_to_565(0xFFFF9000)); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 32 | canvas.drawBitmap(colorBmp, 0, 20); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 33 | colorBmp.eraseColor(ToolUtils::color_to_565(0xFF2000FF)); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 34 | canvas.drawBitmap(colorBmp, 20, 20); |
| 35 | } |
| 36 | |
| 37 | SkString onShortName() override { return SkString("bmp_filter_quality_repeat"); } |
| 38 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 39 | SkISize onISize() override { return SkISize::Make(1000, 400); } |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 40 | |
| 41 | void onDraw(SkCanvas* canvas) override { |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 42 | this->drawAll(canvas, 2.5f); |
| 43 | canvas->translate(0, 250); |
| 44 | canvas->scale(0.5, 0.5); |
| 45 | this->drawAll(canvas, 1); |
| 46 | } |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 47 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 48 | private: |
| 49 | void drawAll(SkCanvas* canvas, SkScalar scaleX) const { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 50 | constexpr struct { |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 51 | SkFilterQuality fQuality; |
| 52 | const char* fName; |
| 53 | } kQualities[] = { |
| 54 | {kNone_SkFilterQuality, "none"}, |
| 55 | {kLow_SkFilterQuality, "low"}, |
| 56 | {kMedium_SkFilterQuality, "medium"}, |
| 57 | {kHigh_SkFilterQuality, "high"}, |
| 58 | }; |
| 59 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 60 | SkRect rect = SkRect::MakeLTRB(20, 60, 220, 210); |
| 61 | SkMatrix lm = SkMatrix::I(); |
| 62 | lm.setScaleX(scaleX); |
| 63 | lm.setTranslateX(423); |
| 64 | lm.setTranslateY(330); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 65 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 66 | SkPaint textPaint; |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 67 | textPaint.setAntiAlias(true); |
| 68 | |
| 69 | SkPaint bmpPaint(textPaint); |
| 70 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 71 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 72 | |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 73 | SkAutoCanvasRestore acr(canvas, true); |
| 74 | |
| 75 | for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame^] | 76 | constexpr SkTileMode kTM = SkTileMode::kRepeat; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 77 | bmpPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTM, kTM, &lm)); |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 78 | bmpPaint.setFilterQuality(kQualities[q].fQuality); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 79 | canvas->drawRect(rect, bmpPaint); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 80 | canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint); |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 81 | canvas->translate(250, 0); |
| 82 | } |
fmalita | b1c7f88 | 2016-11-03 11:42:49 -0700 | [diff] [blame] | 83 | |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
bsalomon | 8cf1d95 | 2015-04-16 06:54:28 -0700 | [diff] [blame] | 86 | SkBitmap fBmp; |
| 87 | |
| 88 | typedef skiagm::GM INHERITED; |
| 89 | }; |
| 90 | |
| 91 | ////////////////////////////////////////////////////////////////////////////// |
| 92 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 93 | DEF_GM(return new BmpFilterQualityRepeat;) |